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 1895a3ecadSAnthony Wilson #include "node.hpp" 1995a3ecadSAnthony Wilson 2008777fb0SLewanczyk, Dawid #include <boost/algorithm/string/predicate.hpp> 2108777fb0SLewanczyk, Dawid #include <boost/algorithm/string/split.hpp> 2208777fb0SLewanczyk, Dawid #include <boost/container/flat_map.hpp> 2308777fb0SLewanczyk, Dawid #include <boost/range/algorithm/replace_copy_if.hpp> 249e27a22eSEd Tanous #include <cmath> 251abe55efSEd Tanous #include <dbus_singleton.hpp> 26413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp> 27abf2add6SEd Tanous #include <variant> 2808777fb0SLewanczyk, Dawid 291abe55efSEd Tanous namespace redfish 301abe55efSEd Tanous { 3108777fb0SLewanczyk, Dawid 3208777fb0SLewanczyk, Dawid using GetSubTreeType = std::vector< 3308777fb0SLewanczyk, Dawid std::pair<std::string, 3408777fb0SLewanczyk, Dawid std::vector<std::pair<std::string, std::vector<std::string>>>>>; 3508777fb0SLewanczyk, Dawid 36adc4f0dbSShawn McCarney using SensorVariant = 37adc4f0dbSShawn McCarney std::variant<int64_t, double, uint32_t, bool, std::string>; 38aa2e59c1SEd Tanous 3908777fb0SLewanczyk, Dawid using ManagedObjectsVectorType = std::vector<std::pair< 40aa2e59c1SEd Tanous sdbusplus::message::object_path, 4108777fb0SLewanczyk, Dawid boost::container::flat_map< 42aa2e59c1SEd Tanous std::string, boost::container::flat_map<std::string, SensorVariant>>>>; 4308777fb0SLewanczyk, Dawid 4408777fb0SLewanczyk, Dawid /** 45588c3f0dSKowalski, Kamil * SensorsAsyncResp 4608777fb0SLewanczyk, Dawid * Gathers data needed for response processing after async calls are done 4708777fb0SLewanczyk, Dawid */ 481abe55efSEd Tanous class SensorsAsyncResp 491abe55efSEd Tanous { 5008777fb0SLewanczyk, Dawid public: 51271584abSEd Tanous SensorsAsyncResp(crow::Response& response, const std::string& chassisIdIn, 52271584abSEd Tanous const std::vector<const char*> typesIn, 532474adfaSEd Tanous const std::string& subNode) : 5443b761d0SEd Tanous res(response), 55271584abSEd Tanous chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode) 561abe55efSEd Tanous { 5708777fb0SLewanczyk, Dawid } 5808777fb0SLewanczyk, Dawid 591abe55efSEd Tanous ~SensorsAsyncResp() 601abe55efSEd Tanous { 611abe55efSEd Tanous if (res.result() == boost::beast::http::status::internal_server_error) 621abe55efSEd Tanous { 631abe55efSEd Tanous // Reset the json object to clear out any data that made it in 641abe55efSEd Tanous // before the error happened todo(ed) handle error condition with 651abe55efSEd Tanous // proper code 6655c7b7a2SEd Tanous res.jsonValue = nlohmann::json::object(); 6708777fb0SLewanczyk, Dawid } 6808777fb0SLewanczyk, Dawid res.end(); 6908777fb0SLewanczyk, Dawid } 70588c3f0dSKowalski, Kamil 7155c7b7a2SEd Tanous crow::Response& res; 72588c3f0dSKowalski, Kamil std::string chassisId{}; 7308777fb0SLewanczyk, Dawid const std::vector<const char*> types; 742474adfaSEd Tanous std::string chassisSubNode{}; 7508777fb0SLewanczyk, Dawid }; 7608777fb0SLewanczyk, Dawid 7708777fb0SLewanczyk, Dawid /** 78d500549bSAnthony Wilson * Possible states for physical inventory leds 79d500549bSAnthony Wilson */ 80d500549bSAnthony Wilson enum class LedState 81d500549bSAnthony Wilson { 82d500549bSAnthony Wilson OFF, 83d500549bSAnthony Wilson ON, 84d500549bSAnthony Wilson BLINK, 85d500549bSAnthony Wilson UNKNOWN 86d500549bSAnthony Wilson }; 87d500549bSAnthony Wilson 88d500549bSAnthony Wilson /** 89adc4f0dbSShawn McCarney * D-Bus inventory item associated with one or more sensors. 90adc4f0dbSShawn McCarney */ 91adc4f0dbSShawn McCarney class InventoryItem 92adc4f0dbSShawn McCarney { 93adc4f0dbSShawn McCarney public: 94adc4f0dbSShawn McCarney InventoryItem(const std::string& objPath) : 95adc4f0dbSShawn McCarney objectPath(objPath), name(), isPresent(true), isFunctional(true), 9642cbe538SGunnar Mills isPowerSupply(false), powerSupplyEfficiencyPercent(-1), manufacturer(), 9742cbe538SGunnar Mills model(), partNumber(), serialNumber(), sensors(), ledObjectPath(""), 98d500549bSAnthony Wilson ledState(LedState::UNKNOWN) 99adc4f0dbSShawn McCarney { 100adc4f0dbSShawn McCarney // Set inventory item name to last node of object path 101adc4f0dbSShawn McCarney auto pos = objectPath.rfind('/'); 102adc4f0dbSShawn McCarney if ((pos != std::string::npos) && ((pos + 1) < objectPath.size())) 103adc4f0dbSShawn McCarney { 104adc4f0dbSShawn McCarney name = objectPath.substr(pos + 1); 105adc4f0dbSShawn McCarney } 106adc4f0dbSShawn McCarney } 107adc4f0dbSShawn McCarney 108adc4f0dbSShawn McCarney std::string objectPath; 109adc4f0dbSShawn McCarney std::string name; 110adc4f0dbSShawn McCarney bool isPresent; 111adc4f0dbSShawn McCarney bool isFunctional; 112adc4f0dbSShawn McCarney bool isPowerSupply; 11342cbe538SGunnar Mills int powerSupplyEfficiencyPercent; 114adc4f0dbSShawn McCarney std::string manufacturer; 115adc4f0dbSShawn McCarney std::string model; 116adc4f0dbSShawn McCarney std::string partNumber; 117adc4f0dbSShawn McCarney std::string serialNumber; 118adc4f0dbSShawn McCarney std::set<std::string> sensors; 119d500549bSAnthony Wilson std::string ledObjectPath; 120d500549bSAnthony Wilson LedState ledState; 121adc4f0dbSShawn McCarney }; 122adc4f0dbSShawn McCarney 123adc4f0dbSShawn McCarney /** 124413961deSRichard Marian Thomaiyar * @brief Get objects with connection necessary for sensors 125588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 12608777fb0SLewanczyk, Dawid * @param sensorNames Sensors retrieved from chassis 12708777fb0SLewanczyk, Dawid * @param callback Callback for processing gathered connections 12808777fb0SLewanczyk, Dawid */ 12908777fb0SLewanczyk, Dawid template <typename Callback> 130413961deSRichard Marian Thomaiyar void getObjectsWithConnection( 131413961deSRichard Marian Thomaiyar std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp, 13249c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames, 1331abe55efSEd Tanous Callback&& callback) 1341abe55efSEd Tanous { 135413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter"; 13603b5bae3SJames Feist const std::string path = "/xyz/openbmc_project/sensors"; 13708777fb0SLewanczyk, Dawid const std::array<std::string, 1> interfaces = { 13808777fb0SLewanczyk, Dawid "xyz.openbmc_project.Sensor.Value"}; 13908777fb0SLewanczyk, Dawid 14008777fb0SLewanczyk, Dawid // Response handler for parsing objects subtree 1411abe55efSEd Tanous auto respHandler = [callback{std::move(callback)}, SensorsAsyncResp, 1421abe55efSEd Tanous sensorNames](const boost::system::error_code ec, 1431abe55efSEd Tanous const GetSubTreeType& subtree) { 144413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter"; 1451abe55efSEd Tanous if (ec) 1461abe55efSEd Tanous { 1475f7d88c4SEd Tanous messages::internalError(SensorsAsyncResp->res); 148413961deSRichard Marian Thomaiyar BMCWEB_LOG_ERROR 149413961deSRichard Marian Thomaiyar << "getObjectsWithConnection resp_handler: Dbus error " << ec; 15008777fb0SLewanczyk, Dawid return; 15108777fb0SLewanczyk, Dawid } 15208777fb0SLewanczyk, Dawid 15355c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees"; 15408777fb0SLewanczyk, Dawid 15508777fb0SLewanczyk, Dawid // Make unique list of connections only for requested sensor types and 15608777fb0SLewanczyk, Dawid // found in the chassis 15708777fb0SLewanczyk, Dawid boost::container::flat_set<std::string> connections; 158413961deSRichard Marian Thomaiyar std::set<std::pair<std::string, std::string>> objectsWithConnection; 1591abe55efSEd Tanous // Intrinsic to avoid malloc. Most systems will have < 8 sensor 1601abe55efSEd Tanous // producers 16108777fb0SLewanczyk, Dawid connections.reserve(8); 16208777fb0SLewanczyk, Dawid 16349c53ac9SJohnathan Mantey BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size(); 16449c53ac9SJohnathan Mantey for (const std::string& tsensor : *sensorNames) 1651abe55efSEd Tanous { 16655c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor; 16708777fb0SLewanczyk, Dawid } 16808777fb0SLewanczyk, Dawid 16908777fb0SLewanczyk, Dawid for (const std::pair< 17008777fb0SLewanczyk, Dawid std::string, 17108777fb0SLewanczyk, Dawid std::vector<std::pair<std::string, std::vector<std::string>>>>& 1721abe55efSEd Tanous object : subtree) 1731abe55efSEd Tanous { 17449c53ac9SJohnathan Mantey if (sensorNames->find(object.first) != sensorNames->end()) 1751abe55efSEd Tanous { 17649c53ac9SJohnathan Mantey for (const std::pair<std::string, std::vector<std::string>>& 1771abe55efSEd Tanous objData : object.second) 1781abe55efSEd Tanous { 17949c53ac9SJohnathan Mantey BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first; 18008777fb0SLewanczyk, Dawid connections.insert(objData.first); 181de629b6eSShawn McCarney objectsWithConnection.insert( 182de629b6eSShawn McCarney std::make_pair(object.first, objData.first)); 18308777fb0SLewanczyk, Dawid } 18408777fb0SLewanczyk, Dawid } 18508777fb0SLewanczyk, Dawid } 18655c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections"; 187413961deSRichard Marian Thomaiyar callback(std::move(connections), std::move(objectsWithConnection)); 188413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit"; 18908777fb0SLewanczyk, Dawid }; 19008777fb0SLewanczyk, Dawid // Make call to ObjectMapper to find all sensors objects 19155c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 19255c7b7a2SEd Tanous std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 1931abe55efSEd Tanous "/xyz/openbmc_project/object_mapper", 1941abe55efSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces); 195413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit"; 196413961deSRichard Marian Thomaiyar } 197413961deSRichard Marian Thomaiyar 198413961deSRichard Marian Thomaiyar /** 199413961deSRichard Marian Thomaiyar * @brief Create connections necessary for sensors 200413961deSRichard Marian Thomaiyar * @param SensorsAsyncResp Pointer to object holding response data 201413961deSRichard Marian Thomaiyar * @param sensorNames Sensors retrieved from chassis 202413961deSRichard Marian Thomaiyar * @param callback Callback for processing gathered connections 203413961deSRichard Marian Thomaiyar */ 204413961deSRichard Marian Thomaiyar template <typename Callback> 20549c53ac9SJohnathan Mantey void getConnections( 20649c53ac9SJohnathan Mantey std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp, 20749c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames, 208413961deSRichard Marian Thomaiyar Callback&& callback) 209413961deSRichard Marian Thomaiyar { 210413961deSRichard Marian Thomaiyar auto objectsWithConnectionCb = 211413961deSRichard Marian Thomaiyar [callback](const boost::container::flat_set<std::string>& connections, 212413961deSRichard Marian Thomaiyar const std::set<std::pair<std::string, std::string>>& 213413961deSRichard Marian Thomaiyar objectsWithConnection) { 214413961deSRichard Marian Thomaiyar callback(std::move(connections)); 215413961deSRichard Marian Thomaiyar }; 216413961deSRichard Marian Thomaiyar getObjectsWithConnection(SensorsAsyncResp, sensorNames, 217413961deSRichard Marian Thomaiyar std::move(objectsWithConnectionCb)); 21808777fb0SLewanczyk, Dawid } 21908777fb0SLewanczyk, Dawid 22008777fb0SLewanczyk, Dawid /** 22149c53ac9SJohnathan Mantey * @brief Shrinks the list of sensors for processing 22249c53ac9SJohnathan Mantey * @param SensorsAysncResp The class holding the Redfish response 22349c53ac9SJohnathan Mantey * @param allSensors A list of all the sensors associated to the 22449c53ac9SJohnathan Mantey * chassis element (i.e. baseboard, front panel, etc...) 22549c53ac9SJohnathan Mantey * @param activeSensors A list that is a reduction of the incoming 22649c53ac9SJohnathan Mantey * allSensors list. Eliminate Thermal sensors when a Power request is 22749c53ac9SJohnathan Mantey * made, and eliminate Power sensors when a Thermal request is made. 22849c53ac9SJohnathan Mantey */ 22949c53ac9SJohnathan Mantey void reduceSensorList( 23049c53ac9SJohnathan Mantey std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp, 23149c53ac9SJohnathan Mantey const std::vector<std::string>* allSensors, 23249c53ac9SJohnathan Mantey std::shared_ptr<boost::container::flat_set<std::string>> activeSensors) 23349c53ac9SJohnathan Mantey { 23449c53ac9SJohnathan Mantey if (SensorsAsyncResp == nullptr) 23549c53ac9SJohnathan Mantey { 23649c53ac9SJohnathan Mantey return; 23749c53ac9SJohnathan Mantey } 23849c53ac9SJohnathan Mantey if ((allSensors == nullptr) || (activeSensors == nullptr)) 23949c53ac9SJohnathan Mantey { 24049c53ac9SJohnathan Mantey messages::resourceNotFound( 24149c53ac9SJohnathan Mantey SensorsAsyncResp->res, SensorsAsyncResp->chassisSubNode, 24249c53ac9SJohnathan Mantey SensorsAsyncResp->chassisSubNode == "Thermal" ? "Temperatures" 24349c53ac9SJohnathan Mantey : "Voltages"); 24449c53ac9SJohnathan Mantey 24549c53ac9SJohnathan Mantey return; 24649c53ac9SJohnathan Mantey } 24749c53ac9SJohnathan Mantey if (allSensors->empty()) 24849c53ac9SJohnathan Mantey { 24949c53ac9SJohnathan Mantey // Nothing to do, the activeSensors object is also empty 25049c53ac9SJohnathan Mantey return; 25149c53ac9SJohnathan Mantey } 25249c53ac9SJohnathan Mantey 25349c53ac9SJohnathan Mantey for (const char* type : SensorsAsyncResp->types) 25449c53ac9SJohnathan Mantey { 25549c53ac9SJohnathan Mantey for (const std::string& sensor : *allSensors) 25649c53ac9SJohnathan Mantey { 25749c53ac9SJohnathan Mantey if (boost::starts_with(sensor, type)) 25849c53ac9SJohnathan Mantey { 25949c53ac9SJohnathan Mantey activeSensors->emplace(sensor); 26049c53ac9SJohnathan Mantey } 26149c53ac9SJohnathan Mantey } 26249c53ac9SJohnathan Mantey } 26349c53ac9SJohnathan Mantey } 26449c53ac9SJohnathan Mantey 26549c53ac9SJohnathan Mantey /** 2664bb3dc34SCarol Wang * @brief Retrieves valid chassis path 2674bb3dc34SCarol Wang * @param asyncResp Pointer to object holding response data 2684bb3dc34SCarol Wang * @param callback Callback for next step to get valid chassis path 2694bb3dc34SCarol Wang */ 2704bb3dc34SCarol Wang template <typename Callback> 2714bb3dc34SCarol Wang void getValidChassisPath(std::shared_ptr<SensorsAsyncResp> asyncResp, 2724bb3dc34SCarol Wang Callback&& callback) 2734bb3dc34SCarol Wang { 2744bb3dc34SCarol Wang BMCWEB_LOG_DEBUG << "checkChassisId enter"; 2754bb3dc34SCarol Wang const std::array<const char*, 2> interfaces = { 2764bb3dc34SCarol Wang "xyz.openbmc_project.Inventory.Item.Board", 2774bb3dc34SCarol Wang "xyz.openbmc_project.Inventory.Item.Chassis"}; 2784bb3dc34SCarol Wang 2794bb3dc34SCarol Wang auto respHandler = 2804bb3dc34SCarol Wang [callback{std::move(callback)}, 2814bb3dc34SCarol Wang asyncResp](const boost::system::error_code ec, 2824bb3dc34SCarol Wang const std::vector<std::string>& chassisPaths) mutable { 2834bb3dc34SCarol Wang BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter"; 2844bb3dc34SCarol Wang if (ec) 2854bb3dc34SCarol Wang { 2864bb3dc34SCarol Wang BMCWEB_LOG_ERROR 2874bb3dc34SCarol Wang << "getValidChassisPath respHandler DBUS error: " << ec; 2884bb3dc34SCarol Wang messages::internalError(asyncResp->res); 2894bb3dc34SCarol Wang return; 2904bb3dc34SCarol Wang } 2914bb3dc34SCarol Wang 2924bb3dc34SCarol Wang std::optional<std::string> chassisPath; 2934bb3dc34SCarol Wang std::string chassisName; 2944bb3dc34SCarol Wang for (const std::string& chassis : chassisPaths) 2954bb3dc34SCarol Wang { 2964bb3dc34SCarol Wang std::size_t lastPos = chassis.rfind("/"); 2974bb3dc34SCarol Wang if (lastPos == std::string::npos) 2984bb3dc34SCarol Wang { 2994bb3dc34SCarol Wang BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis; 3004bb3dc34SCarol Wang continue; 3014bb3dc34SCarol Wang } 3024bb3dc34SCarol Wang chassisName = chassis.substr(lastPos + 1); 3034bb3dc34SCarol Wang if (chassisName == asyncResp->chassisId) 3044bb3dc34SCarol Wang { 3054bb3dc34SCarol Wang chassisPath = chassis; 3064bb3dc34SCarol Wang break; 3074bb3dc34SCarol Wang } 3084bb3dc34SCarol Wang } 3094bb3dc34SCarol Wang callback(chassisPath); 3104bb3dc34SCarol Wang }; 3114bb3dc34SCarol Wang 3124bb3dc34SCarol Wang // Get the Chassis Collection 3134bb3dc34SCarol Wang crow::connections::systemBus->async_method_call( 3144bb3dc34SCarol Wang respHandler, "xyz.openbmc_project.ObjectMapper", 3154bb3dc34SCarol Wang "/xyz/openbmc_project/object_mapper", 3164bb3dc34SCarol Wang "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 3174bb3dc34SCarol Wang "/xyz/openbmc_project/inventory", 0, interfaces); 3184bb3dc34SCarol Wang BMCWEB_LOG_DEBUG << "checkChassisId exit"; 3194bb3dc34SCarol Wang } 3204bb3dc34SCarol Wang 3214bb3dc34SCarol Wang /** 32208777fb0SLewanczyk, Dawid * @brief Retrieves requested chassis sensors and redundancy data from DBus . 323588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 32408777fb0SLewanczyk, Dawid * @param callback Callback for next step in gathered sensor processing 32508777fb0SLewanczyk, Dawid */ 32608777fb0SLewanczyk, Dawid template <typename Callback> 32749c53ac9SJohnathan Mantey void getChassis(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 3281abe55efSEd Tanous Callback&& callback) 3291abe55efSEd Tanous { 33055c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis enter"; 331adc4f0dbSShawn McCarney const std::array<const char*, 2> interfaces = { 33249c53ac9SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Board", 333adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.Chassis"}; 33449c53ac9SJohnathan Mantey auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp]( 33549c53ac9SJohnathan Mantey const boost::system::error_code ec, 33649c53ac9SJohnathan Mantey const std::vector<std::string>& chassisPaths) { 33755c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis respHandler enter"; 3381abe55efSEd Tanous if (ec) 3391abe55efSEd Tanous { 34055c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec; 34149c53ac9SJohnathan Mantey messages::internalError(sensorsAsyncResp->res); 34208777fb0SLewanczyk, Dawid return; 34308777fb0SLewanczyk, Dawid } 34408777fb0SLewanczyk, Dawid 34549c53ac9SJohnathan Mantey const std::string* chassisPath = nullptr; 34649c53ac9SJohnathan Mantey std::string chassisName; 34749c53ac9SJohnathan Mantey for (const std::string& chassis : chassisPaths) 3481abe55efSEd Tanous { 34949c53ac9SJohnathan Mantey std::size_t lastPos = chassis.rfind("/"); 35049c53ac9SJohnathan Mantey if (lastPos == std::string::npos) 3511abe55efSEd Tanous { 35249c53ac9SJohnathan Mantey BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis; 353daf36e2eSEd Tanous continue; 354daf36e2eSEd Tanous } 35549c53ac9SJohnathan Mantey chassisName = chassis.substr(lastPos + 1); 35649c53ac9SJohnathan Mantey if (chassisName == sensorsAsyncResp->chassisId) 3571abe55efSEd Tanous { 35849c53ac9SJohnathan Mantey chassisPath = &chassis; 35949c53ac9SJohnathan Mantey break; 360daf36e2eSEd Tanous } 36149c53ac9SJohnathan Mantey } 36249c53ac9SJohnathan Mantey if (chassisPath == nullptr) 3631abe55efSEd Tanous { 36449c53ac9SJohnathan Mantey messages::resourceNotFound(sensorsAsyncResp->res, "Chassis", 36549c53ac9SJohnathan Mantey sensorsAsyncResp->chassisId); 36649c53ac9SJohnathan Mantey return; 3671abe55efSEd Tanous } 36808777fb0SLewanczyk, Dawid 36949c53ac9SJohnathan Mantey const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode; 37049c53ac9SJohnathan Mantey if (chassisSubNode == "Power") 37149c53ac9SJohnathan Mantey { 37249c53ac9SJohnathan Mantey sensorsAsyncResp->res.jsonValue["@odata.type"] = 37349c53ac9SJohnathan Mantey "#Power.v1_5_2.Power"; 37449c53ac9SJohnathan Mantey } 37549c53ac9SJohnathan Mantey else if (chassisSubNode == "Thermal") 37649c53ac9SJohnathan Mantey { 37749c53ac9SJohnathan Mantey sensorsAsyncResp->res.jsonValue["@odata.type"] = 37849c53ac9SJohnathan Mantey "#Thermal.v1_4_0.Thermal"; 3794f9a2130SJennifer Lee sensorsAsyncResp->res.jsonValue["Fans"] = nlohmann::json::array(); 3804f9a2130SJennifer Lee sensorsAsyncResp->res.jsonValue["Temperatures"] = 3814f9a2130SJennifer Lee nlohmann::json::array(); 38249c53ac9SJohnathan Mantey } 38395a3ecadSAnthony Wilson else if (chassisSubNode == "Sensors") 38495a3ecadSAnthony Wilson { 38595a3ecadSAnthony Wilson sensorsAsyncResp->res.jsonValue["@odata.type"] = 38695a3ecadSAnthony Wilson "#SensorCollection.SensorCollection"; 38795a3ecadSAnthony Wilson sensorsAsyncResp->res.jsonValue["Description"] = 38895a3ecadSAnthony Wilson "Collection of Sensors for this Chassis"; 38995a3ecadSAnthony Wilson sensorsAsyncResp->res.jsonValue["Members"] = 39095a3ecadSAnthony Wilson nlohmann::json::array(); 39195a3ecadSAnthony Wilson sensorsAsyncResp->res.jsonValue["Members@odata.count"] = 0; 39295a3ecadSAnthony Wilson } 39395a3ecadSAnthony Wilson 39495a3ecadSAnthony Wilson if (chassisSubNode != "Sensors") 39595a3ecadSAnthony Wilson { 39695a3ecadSAnthony Wilson sensorsAsyncResp->res.jsonValue["Id"] = chassisSubNode; 39795a3ecadSAnthony Wilson } 39895a3ecadSAnthony Wilson 39949c53ac9SJohnathan Mantey sensorsAsyncResp->res.jsonValue["@odata.id"] = 40049c53ac9SJohnathan Mantey "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" + 40149c53ac9SJohnathan Mantey chassisSubNode; 40249c53ac9SJohnathan Mantey sensorsAsyncResp->res.jsonValue["Name"] = chassisSubNode; 40349c53ac9SJohnathan Mantey 4048fb49dd6SShawn McCarney // Get the list of all sensors for this Chassis element 4058fb49dd6SShawn McCarney std::string sensorPath = *chassisPath + "/all_sensors"; 40655c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 40749c53ac9SJohnathan Mantey [sensorsAsyncResp, callback{std::move(callback)}]( 408271584abSEd Tanous const boost::system::error_code& e, 40949c53ac9SJohnathan Mantey const std::variant<std::vector<std::string>>& 41049c53ac9SJohnathan Mantey variantEndpoints) { 411271584abSEd Tanous if (e) 41249c53ac9SJohnathan Mantey { 413271584abSEd Tanous if (e.value() != EBADR) 41449c53ac9SJohnathan Mantey { 41549c53ac9SJohnathan Mantey messages::internalError(sensorsAsyncResp->res); 41649c53ac9SJohnathan Mantey return; 41749c53ac9SJohnathan Mantey } 41849c53ac9SJohnathan Mantey } 41949c53ac9SJohnathan Mantey const std::vector<std::string>* nodeSensorList = 42049c53ac9SJohnathan Mantey std::get_if<std::vector<std::string>>(&(variantEndpoints)); 42149c53ac9SJohnathan Mantey if (nodeSensorList == nullptr) 42249c53ac9SJohnathan Mantey { 42349c53ac9SJohnathan Mantey messages::resourceNotFound( 42449c53ac9SJohnathan Mantey sensorsAsyncResp->res, sensorsAsyncResp->chassisSubNode, 42549c53ac9SJohnathan Mantey sensorsAsyncResp->chassisSubNode == "Thermal" 42649c53ac9SJohnathan Mantey ? "Temperatures" 42795a3ecadSAnthony Wilson : sensorsAsyncResp->chassisSubNode == "Power" 42895a3ecadSAnthony Wilson ? "Voltages" 42995a3ecadSAnthony Wilson : "Sensors"); 43049c53ac9SJohnathan Mantey return; 43149c53ac9SJohnathan Mantey } 43249c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> 43349c53ac9SJohnathan Mantey culledSensorList = std::make_shared< 43449c53ac9SJohnathan Mantey boost::container::flat_set<std::string>>(); 43549c53ac9SJohnathan Mantey reduceSensorList(sensorsAsyncResp, nodeSensorList, 43649c53ac9SJohnathan Mantey culledSensorList); 43749c53ac9SJohnathan Mantey callback(culledSensorList); 43849c53ac9SJohnathan Mantey }, 43949c53ac9SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", sensorPath, 44049c53ac9SJohnathan Mantey "org.freedesktop.DBus.Properties", "Get", 44149c53ac9SJohnathan Mantey "xyz.openbmc_project.Association", "endpoints"); 44249c53ac9SJohnathan Mantey }; 44349c53ac9SJohnathan Mantey 44449c53ac9SJohnathan Mantey // Get the Chassis Collection 44549c53ac9SJohnathan Mantey crow::connections::systemBus->async_method_call( 44649c53ac9SJohnathan Mantey respHandler, "xyz.openbmc_project.ObjectMapper", 44749c53ac9SJohnathan Mantey "/xyz/openbmc_project/object_mapper", 44849c53ac9SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 449271584abSEd Tanous "/xyz/openbmc_project/inventory", 0, interfaces); 45055c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis exit"; 45108777fb0SLewanczyk, Dawid } 45208777fb0SLewanczyk, Dawid 45308777fb0SLewanczyk, Dawid /** 454de629b6eSShawn McCarney * @brief Finds all DBus object paths that implement ObjectManager. 455de629b6eSShawn McCarney * 456de629b6eSShawn McCarney * Creates a mapping from the associated connection name to the object path. 457de629b6eSShawn McCarney * 458de629b6eSShawn McCarney * Finds the object paths asynchronously. Invokes callback when information has 459de629b6eSShawn McCarney * been obtained. 460de629b6eSShawn McCarney * 461de629b6eSShawn McCarney * The callback must have the following signature: 462de629b6eSShawn McCarney * @code 4638fb49dd6SShawn McCarney * callback(std::shared_ptr<boost::container::flat_map<std::string, 4648fb49dd6SShawn McCarney * std::string>> objectMgrPaths) 465de629b6eSShawn McCarney * @endcode 466de629b6eSShawn McCarney * 46749c53ac9SJohnathan Mantey * @param sensorsAsyncResp Pointer to object holding response data. 468de629b6eSShawn McCarney * @param callback Callback to invoke when object paths obtained. 469de629b6eSShawn McCarney */ 470de629b6eSShawn McCarney template <typename Callback> 471de629b6eSShawn McCarney void getObjectManagerPaths(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp, 472de629b6eSShawn McCarney Callback&& callback) 473de629b6eSShawn McCarney { 474de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter"; 475de629b6eSShawn McCarney const std::array<std::string, 1> interfaces = { 476de629b6eSShawn McCarney "org.freedesktop.DBus.ObjectManager"}; 477de629b6eSShawn McCarney 478de629b6eSShawn McCarney // Response handler for GetSubTree DBus method 479de629b6eSShawn McCarney auto respHandler = [callback{std::move(callback)}, 480de629b6eSShawn McCarney SensorsAsyncResp](const boost::system::error_code ec, 481de629b6eSShawn McCarney const GetSubTreeType& subtree) { 482de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter"; 483de629b6eSShawn McCarney if (ec) 484de629b6eSShawn McCarney { 485de629b6eSShawn McCarney messages::internalError(SensorsAsyncResp->res); 486de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error " 487de629b6eSShawn McCarney << ec; 488de629b6eSShawn McCarney return; 489de629b6eSShawn McCarney } 490de629b6eSShawn McCarney 491de629b6eSShawn McCarney // Loop over returned object paths 4928fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 4938fb49dd6SShawn McCarney objectMgrPaths = std::make_shared< 4948fb49dd6SShawn McCarney boost::container::flat_map<std::string, std::string>>(); 495de629b6eSShawn McCarney for (const std::pair< 496de629b6eSShawn McCarney std::string, 497de629b6eSShawn McCarney std::vector<std::pair<std::string, std::vector<std::string>>>>& 498de629b6eSShawn McCarney object : subtree) 499de629b6eSShawn McCarney { 500de629b6eSShawn McCarney // Loop over connections for current object path 501de629b6eSShawn McCarney const std::string& objectPath = object.first; 502de629b6eSShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 503de629b6eSShawn McCarney objData : object.second) 504de629b6eSShawn McCarney { 505de629b6eSShawn McCarney // Add mapping from connection to object path 506de629b6eSShawn McCarney const std::string& connection = objData.first; 5078fb49dd6SShawn McCarney (*objectMgrPaths)[connection] = objectPath; 508de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> " 509de629b6eSShawn McCarney << objectPath; 510de629b6eSShawn McCarney } 511de629b6eSShawn McCarney } 5128fb49dd6SShawn McCarney callback(objectMgrPaths); 513de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit"; 514de629b6eSShawn McCarney }; 515de629b6eSShawn McCarney 516de629b6eSShawn McCarney // Query mapper for all DBus object paths that implement ObjectManager 517de629b6eSShawn McCarney crow::connections::systemBus->async_method_call( 518de629b6eSShawn McCarney std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 519de629b6eSShawn McCarney "/xyz/openbmc_project/object_mapper", 520271584abSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces); 521de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit"; 522de629b6eSShawn McCarney } 523de629b6eSShawn McCarney 524de629b6eSShawn McCarney /** 525adc4f0dbSShawn McCarney * @brief Returns the Redfish State value for the specified inventory item. 526adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with a sensor. 527adc4f0dbSShawn McCarney * @return State value for inventory item. 52834dd179eSJames Feist */ 529adc4f0dbSShawn McCarney static std::string getState(const InventoryItem* inventoryItem) 530adc4f0dbSShawn McCarney { 531adc4f0dbSShawn McCarney if ((inventoryItem != nullptr) && !(inventoryItem->isPresent)) 532adc4f0dbSShawn McCarney { 533adc4f0dbSShawn McCarney return "Absent"; 534adc4f0dbSShawn McCarney } 53534dd179eSJames Feist 536adc4f0dbSShawn McCarney return "Enabled"; 537adc4f0dbSShawn McCarney } 538adc4f0dbSShawn McCarney 539adc4f0dbSShawn McCarney /** 540adc4f0dbSShawn McCarney * @brief Returns the Redfish Health value for the specified sensor. 541adc4f0dbSShawn McCarney * @param sensorJson Sensor JSON object. 542adc4f0dbSShawn McCarney * @param interfacesDict Map of all sensor interfaces. 543adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with the sensor. Will 544adc4f0dbSShawn McCarney * be nullptr if no associated inventory item was found. 545adc4f0dbSShawn McCarney * @return Health value for sensor. 546adc4f0dbSShawn McCarney */ 54734dd179eSJames Feist static std::string getHealth( 548adc4f0dbSShawn McCarney nlohmann::json& sensorJson, 54934dd179eSJames Feist const boost::container::flat_map< 55034dd179eSJames Feist std::string, boost::container::flat_map<std::string, SensorVariant>>& 551adc4f0dbSShawn McCarney interfacesDict, 552adc4f0dbSShawn McCarney const InventoryItem* inventoryItem) 55334dd179eSJames Feist { 554adc4f0dbSShawn McCarney // Get current health value (if any) in the sensor JSON object. Some JSON 555adc4f0dbSShawn McCarney // objects contain multiple sensors (such as PowerSupplies). We want to set 556adc4f0dbSShawn McCarney // the overall health to be the most severe of any of the sensors. 557adc4f0dbSShawn McCarney std::string currentHealth; 558adc4f0dbSShawn McCarney auto statusIt = sensorJson.find("Status"); 559adc4f0dbSShawn McCarney if (statusIt != sensorJson.end()) 560adc4f0dbSShawn McCarney { 561adc4f0dbSShawn McCarney auto healthIt = statusIt->find("Health"); 562adc4f0dbSShawn McCarney if (healthIt != statusIt->end()) 563adc4f0dbSShawn McCarney { 564adc4f0dbSShawn McCarney std::string* health = healthIt->get_ptr<std::string*>(); 565adc4f0dbSShawn McCarney if (health != nullptr) 566adc4f0dbSShawn McCarney { 567adc4f0dbSShawn McCarney currentHealth = *health; 568adc4f0dbSShawn McCarney } 569adc4f0dbSShawn McCarney } 570adc4f0dbSShawn McCarney } 571adc4f0dbSShawn McCarney 572adc4f0dbSShawn McCarney // If current health in JSON object is already Critical, return that. This 573adc4f0dbSShawn McCarney // should override the sensor health, which might be less severe. 574adc4f0dbSShawn McCarney if (currentHealth == "Critical") 575adc4f0dbSShawn McCarney { 576adc4f0dbSShawn McCarney return "Critical"; 577adc4f0dbSShawn McCarney } 578adc4f0dbSShawn McCarney 579adc4f0dbSShawn McCarney // Check if sensor has critical threshold alarm 58034dd179eSJames Feist auto criticalThresholdIt = 58134dd179eSJames Feist interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Critical"); 58234dd179eSJames Feist if (criticalThresholdIt != interfacesDict.end()) 58334dd179eSJames Feist { 58434dd179eSJames Feist auto thresholdHighIt = 58534dd179eSJames Feist criticalThresholdIt->second.find("CriticalAlarmHigh"); 58634dd179eSJames Feist auto thresholdLowIt = 58734dd179eSJames Feist criticalThresholdIt->second.find("CriticalAlarmLow"); 58834dd179eSJames Feist if (thresholdHighIt != criticalThresholdIt->second.end()) 58934dd179eSJames Feist { 59034dd179eSJames Feist const bool* asserted = std::get_if<bool>(&thresholdHighIt->second); 59134dd179eSJames Feist if (asserted == nullptr) 59234dd179eSJames Feist { 59334dd179eSJames Feist BMCWEB_LOG_ERROR << "Illegal sensor threshold"; 59434dd179eSJames Feist } 59534dd179eSJames Feist else if (*asserted) 59634dd179eSJames Feist { 59734dd179eSJames Feist return "Critical"; 59834dd179eSJames Feist } 59934dd179eSJames Feist } 60034dd179eSJames Feist if (thresholdLowIt != criticalThresholdIt->second.end()) 60134dd179eSJames Feist { 60234dd179eSJames Feist const bool* asserted = std::get_if<bool>(&thresholdLowIt->second); 60334dd179eSJames Feist if (asserted == nullptr) 60434dd179eSJames Feist { 60534dd179eSJames Feist BMCWEB_LOG_ERROR << "Illegal sensor threshold"; 60634dd179eSJames Feist } 60734dd179eSJames Feist else if (*asserted) 60834dd179eSJames Feist { 60934dd179eSJames Feist return "Critical"; 61034dd179eSJames Feist } 61134dd179eSJames Feist } 61234dd179eSJames Feist } 61334dd179eSJames Feist 614adc4f0dbSShawn McCarney // Check if associated inventory item is not functional 615adc4f0dbSShawn McCarney if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional)) 616adc4f0dbSShawn McCarney { 617adc4f0dbSShawn McCarney return "Critical"; 618adc4f0dbSShawn McCarney } 619adc4f0dbSShawn McCarney 620adc4f0dbSShawn McCarney // If current health in JSON object is already Warning, return that. This 621adc4f0dbSShawn McCarney // should override the sensor status, which might be less severe. 622adc4f0dbSShawn McCarney if (currentHealth == "Warning") 623adc4f0dbSShawn McCarney { 624adc4f0dbSShawn McCarney return "Warning"; 625adc4f0dbSShawn McCarney } 626adc4f0dbSShawn McCarney 627adc4f0dbSShawn McCarney // Check if sensor has warning threshold alarm 62834dd179eSJames Feist auto warningThresholdIt = 62934dd179eSJames Feist interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Warning"); 63034dd179eSJames Feist if (warningThresholdIt != interfacesDict.end()) 63134dd179eSJames Feist { 63234dd179eSJames Feist auto thresholdHighIt = 63334dd179eSJames Feist warningThresholdIt->second.find("WarningAlarmHigh"); 63434dd179eSJames Feist auto thresholdLowIt = 63534dd179eSJames Feist warningThresholdIt->second.find("WarningAlarmLow"); 63634dd179eSJames Feist if (thresholdHighIt != warningThresholdIt->second.end()) 63734dd179eSJames Feist { 63834dd179eSJames Feist const bool* asserted = std::get_if<bool>(&thresholdHighIt->second); 63934dd179eSJames Feist if (asserted == nullptr) 64034dd179eSJames Feist { 64134dd179eSJames Feist BMCWEB_LOG_ERROR << "Illegal sensor threshold"; 64234dd179eSJames Feist } 64334dd179eSJames Feist else if (*asserted) 64434dd179eSJames Feist { 64534dd179eSJames Feist return "Warning"; 64634dd179eSJames Feist } 64734dd179eSJames Feist } 64834dd179eSJames Feist if (thresholdLowIt != warningThresholdIt->second.end()) 64934dd179eSJames Feist { 65034dd179eSJames Feist const bool* asserted = std::get_if<bool>(&thresholdLowIt->second); 65134dd179eSJames Feist if (asserted == nullptr) 65234dd179eSJames Feist { 65334dd179eSJames Feist BMCWEB_LOG_ERROR << "Illegal sensor threshold"; 65434dd179eSJames Feist } 65534dd179eSJames Feist else if (*asserted) 65634dd179eSJames Feist { 65734dd179eSJames Feist return "Warning"; 65834dd179eSJames Feist } 65934dd179eSJames Feist } 66034dd179eSJames Feist } 661adc4f0dbSShawn McCarney 66234dd179eSJames Feist return "OK"; 66334dd179eSJames Feist } 66434dd179eSJames Feist 665d500549bSAnthony Wilson static void setLedState(nlohmann::json& sensorJson, 666d500549bSAnthony Wilson const InventoryItem* inventoryItem) 667d500549bSAnthony Wilson { 668d500549bSAnthony Wilson if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty()) 669d500549bSAnthony Wilson { 670d500549bSAnthony Wilson switch (inventoryItem->ledState) 671d500549bSAnthony Wilson { 672d500549bSAnthony Wilson case LedState::OFF: 673d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Off"; 674d500549bSAnthony Wilson break; 675d500549bSAnthony Wilson case LedState::ON: 676d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Lit"; 677d500549bSAnthony Wilson break; 678d500549bSAnthony Wilson case LedState::BLINK: 679d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Blinking"; 680d500549bSAnthony Wilson break; 681d500549bSAnthony Wilson default: 682d500549bSAnthony Wilson break; 683d500549bSAnthony Wilson } 684d500549bSAnthony Wilson } 685d500549bSAnthony Wilson } 686d500549bSAnthony Wilson 68734dd179eSJames Feist /** 68808777fb0SLewanczyk, Dawid * @brief Builds a json sensor representation of a sensor. 68908777fb0SLewanczyk, Dawid * @param sensorName The name of the sensor to be built 690274fad5aSGunnar Mills * @param sensorType The type (temperature, fan_tach, etc) of the sensor to 69108777fb0SLewanczyk, Dawid * build 69295a3ecadSAnthony Wilson * @param sensorSchema The schema (Power, Thermal, etc) being associated with 69395a3ecadSAnthony Wilson * the sensor to build 69408777fb0SLewanczyk, Dawid * @param interfacesDict A dictionary of the interfaces and properties of said 69508777fb0SLewanczyk, Dawid * interfaces to be built from 69608777fb0SLewanczyk, Dawid * @param sensor_json The json object to fill 697adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with the sensor. Will 698adc4f0dbSShawn McCarney * be nullptr if no associated inventory item was found. 69908777fb0SLewanczyk, Dawid */ 70008777fb0SLewanczyk, Dawid void objectInterfacesToJson( 70108777fb0SLewanczyk, Dawid const std::string& sensorName, const std::string& sensorType, 70295a3ecadSAnthony Wilson const std::string& sensorSchema, 70308777fb0SLewanczyk, Dawid const boost::container::flat_map< 704aa2e59c1SEd Tanous std::string, boost::container::flat_map<std::string, SensorVariant>>& 70508777fb0SLewanczyk, Dawid interfacesDict, 706adc4f0dbSShawn McCarney nlohmann::json& sensor_json, InventoryItem* inventoryItem) 7071abe55efSEd Tanous { 70808777fb0SLewanczyk, Dawid // We need a value interface before we can do anything with it 70955c7b7a2SEd Tanous auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value"); 7101abe55efSEd Tanous if (valueIt == interfacesDict.end()) 7111abe55efSEd Tanous { 71255c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface"; 71308777fb0SLewanczyk, Dawid return; 71408777fb0SLewanczyk, Dawid } 71508777fb0SLewanczyk, Dawid 71608777fb0SLewanczyk, Dawid // Assume values exist as is (10^0 == 1) if no scale exists 71708777fb0SLewanczyk, Dawid int64_t scaleMultiplier = 0; 71808777fb0SLewanczyk, Dawid 71955c7b7a2SEd Tanous auto scaleIt = valueIt->second.find("Scale"); 72008777fb0SLewanczyk, Dawid // If a scale exists, pull value as int64, and use the scaling. 7211abe55efSEd Tanous if (scaleIt != valueIt->second.end()) 7221abe55efSEd Tanous { 723abf2add6SEd Tanous const int64_t* int64Value = std::get_if<int64_t>(&scaleIt->second); 7241abe55efSEd Tanous if (int64Value != nullptr) 7251abe55efSEd Tanous { 72608777fb0SLewanczyk, Dawid scaleMultiplier = *int64Value; 72708777fb0SLewanczyk, Dawid } 72808777fb0SLewanczyk, Dawid } 72908777fb0SLewanczyk, Dawid 73095a3ecadSAnthony Wilson if (sensorSchema == "Sensors") 731adc4f0dbSShawn McCarney { 73295a3ecadSAnthony Wilson // For sensors in SensorCollection we set Id instead of MemberId, 73395a3ecadSAnthony Wilson // including power sensors. 73495a3ecadSAnthony Wilson sensor_json["Id"] = sensorName; 73595a3ecadSAnthony Wilson sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " "); 73695a3ecadSAnthony Wilson } 73795a3ecadSAnthony Wilson else if (sensorType != "power") 73895a3ecadSAnthony Wilson { 73995a3ecadSAnthony Wilson // Set MemberId and Name for non-power sensors. For PowerSupplies and 74095a3ecadSAnthony Wilson // PowerControl, those properties have more general values because 74195a3ecadSAnthony Wilson // multiple sensors can be stored in the same JSON object. 74208777fb0SLewanczyk, Dawid sensor_json["MemberId"] = sensorName; 743e742b6ccSEd Tanous sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " "); 744adc4f0dbSShawn McCarney } 745e742b6ccSEd Tanous 746adc4f0dbSShawn McCarney sensor_json["Status"]["State"] = getState(inventoryItem); 747adc4f0dbSShawn McCarney sensor_json["Status"]["Health"] = 748adc4f0dbSShawn McCarney getHealth(sensor_json, interfacesDict, inventoryItem); 74908777fb0SLewanczyk, Dawid 75008777fb0SLewanczyk, Dawid // Parameter to set to override the type we get from dbus, and force it to 75108777fb0SLewanczyk, Dawid // int, regardless of what is available. This is used for schemas like fan, 75208777fb0SLewanczyk, Dawid // that require integers, not floats. 75308777fb0SLewanczyk, Dawid bool forceToInt = false; 75408777fb0SLewanczyk, Dawid 7553929aca1SAnthony Wilson nlohmann::json::json_pointer unit("/Reading"); 75695a3ecadSAnthony Wilson if (sensorSchema == "Sensors") 75795a3ecadSAnthony Wilson { 75895a3ecadSAnthony Wilson sensor_json["@odata.type"] = "#Sensor.v1_0_0.Sensor"; 75995a3ecadSAnthony Wilson if (sensorType == "power") 76095a3ecadSAnthony Wilson { 76195a3ecadSAnthony Wilson sensor_json["ReadingUnits"] = "Watts"; 76295a3ecadSAnthony Wilson } 76395a3ecadSAnthony Wilson else if (sensorType == "current") 76495a3ecadSAnthony Wilson { 76595a3ecadSAnthony Wilson sensor_json["ReadingUnits"] = "Amperes"; 76695a3ecadSAnthony Wilson } 76795a3ecadSAnthony Wilson } 76895a3ecadSAnthony Wilson else if (sensorType == "temperature") 7691abe55efSEd Tanous { 7703929aca1SAnthony Wilson unit = "/ReadingCelsius"_json_pointer; 7717885954aSLewanczyk, Dawid sensor_json["@odata.type"] = "#Thermal.v1_3_0.Temperature"; 77208777fb0SLewanczyk, Dawid // TODO(ed) Documentation says that path should be type fan_tach, 77308777fb0SLewanczyk, Dawid // implementation seems to implement fan 7741abe55efSEd Tanous } 7751abe55efSEd Tanous else if (sensorType == "fan" || sensorType == "fan_tach") 7761abe55efSEd Tanous { 7773929aca1SAnthony Wilson unit = "/Reading"_json_pointer; 77808777fb0SLewanczyk, Dawid sensor_json["ReadingUnits"] = "RPM"; 7797885954aSLewanczyk, Dawid sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan"; 780d500549bSAnthony Wilson setLedState(sensor_json, inventoryItem); 78108777fb0SLewanczyk, Dawid forceToInt = true; 7821abe55efSEd Tanous } 7836f6d0d32SEd Tanous else if (sensorType == "fan_pwm") 7846f6d0d32SEd Tanous { 7853929aca1SAnthony Wilson unit = "/Reading"_json_pointer; 7866f6d0d32SEd Tanous sensor_json["ReadingUnits"] = "Percent"; 7876f6d0d32SEd Tanous sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan"; 788d500549bSAnthony Wilson setLedState(sensor_json, inventoryItem); 7896f6d0d32SEd Tanous forceToInt = true; 7906f6d0d32SEd Tanous } 7911abe55efSEd Tanous else if (sensorType == "voltage") 7921abe55efSEd Tanous { 7933929aca1SAnthony Wilson unit = "/ReadingVolts"_json_pointer; 7947885954aSLewanczyk, Dawid sensor_json["@odata.type"] = "#Power.v1_0_0.Voltage"; 7951abe55efSEd Tanous } 7962474adfaSEd Tanous else if (sensorType == "power") 7972474adfaSEd Tanous { 79849c53ac9SJohnathan Mantey std::string sensorNameLower = 79949c53ac9SJohnathan Mantey boost::algorithm::to_lower_copy(sensorName); 80049c53ac9SJohnathan Mantey 801028f7ebcSEddie James if (!sensorName.compare("total_power")) 802028f7ebcSEddie James { 8037ab06f49SGunnar Mills sensor_json["@odata.type"] = "#Power.v1_0_0.PowerControl"; 8047ab06f49SGunnar Mills // Put multiple "sensors" into a single PowerControl, so have 8057ab06f49SGunnar Mills // generic names for MemberId and Name. Follows Redfish mockup. 8067ab06f49SGunnar Mills sensor_json["MemberId"] = "0"; 8077ab06f49SGunnar Mills sensor_json["Name"] = "Chassis Power Control"; 8083929aca1SAnthony Wilson unit = "/PowerConsumedWatts"_json_pointer; 809028f7ebcSEddie James } 810028f7ebcSEddie James else if (sensorNameLower.find("input") != std::string::npos) 81149c53ac9SJohnathan Mantey { 8123929aca1SAnthony Wilson unit = "/PowerInputWatts"_json_pointer; 81349c53ac9SJohnathan Mantey } 81449c53ac9SJohnathan Mantey else 81549c53ac9SJohnathan Mantey { 8163929aca1SAnthony Wilson unit = "/PowerOutputWatts"_json_pointer; 81749c53ac9SJohnathan Mantey } 8182474adfaSEd Tanous } 8191abe55efSEd Tanous else 8201abe55efSEd Tanous { 82155c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName; 82208777fb0SLewanczyk, Dawid return; 82308777fb0SLewanczyk, Dawid } 82408777fb0SLewanczyk, Dawid // Map of dbus interface name, dbus property name and redfish property_name 8253929aca1SAnthony Wilson std::vector< 8263929aca1SAnthony Wilson std::tuple<const char*, const char*, nlohmann::json::json_pointer>> 8273929aca1SAnthony Wilson properties; 82808777fb0SLewanczyk, Dawid properties.reserve(7); 82908777fb0SLewanczyk, Dawid 83008777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit); 831de629b6eSShawn McCarney 8323929aca1SAnthony Wilson if (sensorSchema == "Sensors") 8333929aca1SAnthony Wilson { 8343929aca1SAnthony Wilson properties.emplace_back( 8353929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh", 8363929aca1SAnthony Wilson "/Thresholds/UpperCaution/Reading"_json_pointer); 8373929aca1SAnthony Wilson properties.emplace_back( 8383929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow", 8393929aca1SAnthony Wilson "/Thresholds/LowerCaution/Reading"_json_pointer); 8403929aca1SAnthony Wilson properties.emplace_back( 8413929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh", 8423929aca1SAnthony Wilson "/Thresholds/UpperCritical/Reading"_json_pointer); 8433929aca1SAnthony Wilson properties.emplace_back( 8443929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow", 8453929aca1SAnthony Wilson "/Thresholds/LowerCritical/Reading"_json_pointer); 8463929aca1SAnthony Wilson } 8473929aca1SAnthony Wilson else if (sensorType != "power") 848de629b6eSShawn McCarney { 84908777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 8503929aca1SAnthony Wilson "WarningHigh", 8513929aca1SAnthony Wilson "/UpperThresholdNonCritical"_json_pointer); 85208777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 8533929aca1SAnthony Wilson "WarningLow", 8543929aca1SAnthony Wilson "/LowerThresholdNonCritical"_json_pointer); 85508777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 8563929aca1SAnthony Wilson "CriticalHigh", 8573929aca1SAnthony Wilson "/UpperThresholdCritical"_json_pointer); 85808777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 8593929aca1SAnthony Wilson "CriticalLow", 8603929aca1SAnthony Wilson "/LowerThresholdCritical"_json_pointer); 861de629b6eSShawn McCarney } 86208777fb0SLewanczyk, Dawid 8632474adfaSEd Tanous // TODO Need to get UpperThresholdFatal and LowerThresholdFatal 8642474adfaSEd Tanous 86595a3ecadSAnthony Wilson if (sensorSchema == "Sensors") 86695a3ecadSAnthony Wilson { 86795a3ecadSAnthony Wilson properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 8683929aca1SAnthony Wilson "/ReadingRangeMin"_json_pointer); 86995a3ecadSAnthony Wilson properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 8703929aca1SAnthony Wilson "/ReadingRangeMax"_json_pointer); 87195a3ecadSAnthony Wilson } 87295a3ecadSAnthony Wilson else if (sensorType == "temperature") 8731abe55efSEd Tanous { 87408777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 8753929aca1SAnthony Wilson "/MinReadingRangeTemp"_json_pointer); 87608777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 8773929aca1SAnthony Wilson "/MaxReadingRangeTemp"_json_pointer); 8781abe55efSEd Tanous } 879adc4f0dbSShawn McCarney else if (sensorType != "power") 8801abe55efSEd Tanous { 88108777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 8823929aca1SAnthony Wilson "/MinReadingRange"_json_pointer); 88308777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 8843929aca1SAnthony Wilson "/MaxReadingRange"_json_pointer); 88508777fb0SLewanczyk, Dawid } 88608777fb0SLewanczyk, Dawid 8873929aca1SAnthony Wilson for (const std::tuple<const char*, const char*, 8883929aca1SAnthony Wilson nlohmann::json::json_pointer>& p : properties) 8891abe55efSEd Tanous { 89008777fb0SLewanczyk, Dawid auto interfaceProperties = interfacesDict.find(std::get<0>(p)); 8911abe55efSEd Tanous if (interfaceProperties != interfacesDict.end()) 8921abe55efSEd Tanous { 893271584abSEd Tanous auto thisValueIt = interfaceProperties->second.find(std::get<1>(p)); 894271584abSEd Tanous if (thisValueIt != interfaceProperties->second.end()) 8951abe55efSEd Tanous { 896271584abSEd Tanous const SensorVariant& valueVariant = thisValueIt->second; 8973929aca1SAnthony Wilson 8983929aca1SAnthony Wilson // The property we want to set may be nested json, so use 8993929aca1SAnthony Wilson // a json_pointer for easy indexing into the json structure. 9003929aca1SAnthony Wilson const nlohmann::json::json_pointer& key = std::get<2>(p); 9013929aca1SAnthony Wilson 90208777fb0SLewanczyk, Dawid // Attempt to pull the int64 directly 903abf2add6SEd Tanous const int64_t* int64Value = std::get_if<int64_t>(&valueVariant); 90408777fb0SLewanczyk, Dawid 905abf2add6SEd Tanous const double* doubleValue = std::get_if<double>(&valueVariant); 906028f7ebcSEddie James const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant); 9076f6d0d32SEd Tanous double temp = 0.0; 9086f6d0d32SEd Tanous if (int64Value != nullptr) 9091abe55efSEd Tanous { 910271584abSEd Tanous temp = static_cast<double>(*int64Value); 9116f6d0d32SEd Tanous } 9126f6d0d32SEd Tanous else if (doubleValue != nullptr) 9131abe55efSEd Tanous { 9146f6d0d32SEd Tanous temp = *doubleValue; 9151abe55efSEd Tanous } 916028f7ebcSEddie James else if (uValue != nullptr) 917028f7ebcSEddie James { 918028f7ebcSEddie James temp = *uValue; 919028f7ebcSEddie James } 9201abe55efSEd Tanous else 9211abe55efSEd Tanous { 9226f6d0d32SEd Tanous BMCWEB_LOG_ERROR 9236f6d0d32SEd Tanous << "Got value interface that wasn't int or double"; 9246f6d0d32SEd Tanous continue; 92508777fb0SLewanczyk, Dawid } 9266f6d0d32SEd Tanous temp = temp * std::pow(10, scaleMultiplier); 9276f6d0d32SEd Tanous if (forceToInt) 9286f6d0d32SEd Tanous { 9293929aca1SAnthony Wilson sensor_json[key] = static_cast<int64_t>(temp); 9306f6d0d32SEd Tanous } 9316f6d0d32SEd Tanous else 9326f6d0d32SEd Tanous { 9333929aca1SAnthony Wilson sensor_json[key] = temp; 93408777fb0SLewanczyk, Dawid } 93508777fb0SLewanczyk, Dawid } 93608777fb0SLewanczyk, Dawid } 93708777fb0SLewanczyk, Dawid } 93855c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Added sensor " << sensorName; 93908777fb0SLewanczyk, Dawid } 94008777fb0SLewanczyk, Dawid 9418bd25ccdSJames Feist static void 9428bd25ccdSJames Feist populateFanRedundancy(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp) 9438bd25ccdSJames Feist { 9448bd25ccdSJames Feist crow::connections::systemBus->async_method_call( 9458bd25ccdSJames Feist [sensorsAsyncResp](const boost::system::error_code ec, 9468bd25ccdSJames Feist const GetSubTreeType& resp) { 9478bd25ccdSJames Feist if (ec) 9488bd25ccdSJames Feist { 9498bd25ccdSJames Feist return; // don't have to have this interface 9508bd25ccdSJames Feist } 951e278c18fSEd Tanous for (const std::pair<std::string, 952e278c18fSEd Tanous std::vector<std::pair< 953e278c18fSEd Tanous std::string, std::vector<std::string>>>>& 954e278c18fSEd Tanous pathPair : resp) 9558bd25ccdSJames Feist { 956e278c18fSEd Tanous const std::string& path = pathPair.first; 957e278c18fSEd Tanous const std::vector< 958e278c18fSEd Tanous std::pair<std::string, std::vector<std::string>>>& objDict = 959e278c18fSEd Tanous pathPair.second; 9608bd25ccdSJames Feist if (objDict.empty()) 9618bd25ccdSJames Feist { 9628bd25ccdSJames Feist continue; // this should be impossible 9638bd25ccdSJames Feist } 9648bd25ccdSJames Feist 9658bd25ccdSJames Feist const std::string& owner = objDict.begin()->first; 9668bd25ccdSJames Feist crow::connections::systemBus->async_method_call( 9678bd25ccdSJames Feist [path, owner, 968271584abSEd Tanous sensorsAsyncResp](const boost::system::error_code e, 9698bd25ccdSJames Feist std::variant<std::vector<std::string>> 9708bd25ccdSJames Feist variantEndpoints) { 971271584abSEd Tanous if (e) 9728bd25ccdSJames Feist { 9738bd25ccdSJames Feist return; // if they don't have an association we 9748bd25ccdSJames Feist // can't tell what chassis is 9758bd25ccdSJames Feist } 9768bd25ccdSJames Feist // verify part of the right chassis 9778bd25ccdSJames Feist auto endpoints = std::get_if<std::vector<std::string>>( 9788bd25ccdSJames Feist &variantEndpoints); 9798bd25ccdSJames Feist 9808bd25ccdSJames Feist if (endpoints == nullptr) 9818bd25ccdSJames Feist { 9828bd25ccdSJames Feist BMCWEB_LOG_ERROR << "Invalid association interface"; 9838bd25ccdSJames Feist messages::internalError(sensorsAsyncResp->res); 9848bd25ccdSJames Feist return; 9858bd25ccdSJames Feist } 9868bd25ccdSJames Feist 9878bd25ccdSJames Feist auto found = std::find_if( 9888bd25ccdSJames Feist endpoints->begin(), endpoints->end(), 9898bd25ccdSJames Feist [sensorsAsyncResp](const std::string& entry) { 9908bd25ccdSJames Feist return entry.find( 9918bd25ccdSJames Feist sensorsAsyncResp->chassisId) != 9928bd25ccdSJames Feist std::string::npos; 9938bd25ccdSJames Feist }); 9948bd25ccdSJames Feist 9958bd25ccdSJames Feist if (found == endpoints->end()) 9968bd25ccdSJames Feist { 9978bd25ccdSJames Feist return; 9988bd25ccdSJames Feist } 9998bd25ccdSJames Feist crow::connections::systemBus->async_method_call( 10008bd25ccdSJames Feist [path, sensorsAsyncResp]( 1001271584abSEd Tanous const boost::system::error_code& err, 10028bd25ccdSJames Feist const boost::container::flat_map< 10038bd25ccdSJames Feist std::string, 10048bd25ccdSJames Feist std::variant<uint8_t, 10058bd25ccdSJames Feist std::vector<std::string>, 10068bd25ccdSJames Feist std::string>>& ret) { 1007271584abSEd Tanous if (err) 10088bd25ccdSJames Feist { 10098bd25ccdSJames Feist return; // don't have to have this 10108bd25ccdSJames Feist // interface 10118bd25ccdSJames Feist } 10128bd25ccdSJames Feist auto findFailures = ret.find("AllowedFailures"); 10138bd25ccdSJames Feist auto findCollection = ret.find("Collection"); 10148bd25ccdSJames Feist auto findStatus = ret.find("Status"); 10158bd25ccdSJames Feist 10168bd25ccdSJames Feist if (findFailures == ret.end() || 10178bd25ccdSJames Feist findCollection == ret.end() || 10188bd25ccdSJames Feist findStatus == ret.end()) 10198bd25ccdSJames Feist { 10208bd25ccdSJames Feist BMCWEB_LOG_ERROR 10218bd25ccdSJames Feist << "Invalid redundancy interface"; 10228bd25ccdSJames Feist messages::internalError( 10238bd25ccdSJames Feist sensorsAsyncResp->res); 10248bd25ccdSJames Feist return; 10258bd25ccdSJames Feist } 10268bd25ccdSJames Feist 10278bd25ccdSJames Feist auto allowedFailures = std::get_if<uint8_t>( 10288bd25ccdSJames Feist &(findFailures->second)); 10298bd25ccdSJames Feist auto collection = 10308bd25ccdSJames Feist std::get_if<std::vector<std::string>>( 10318bd25ccdSJames Feist &(findCollection->second)); 10328bd25ccdSJames Feist auto status = std::get_if<std::string>( 10338bd25ccdSJames Feist &(findStatus->second)); 10348bd25ccdSJames Feist 10358bd25ccdSJames Feist if (allowedFailures == nullptr || 10368bd25ccdSJames Feist collection == nullptr || status == nullptr) 10378bd25ccdSJames Feist { 10388bd25ccdSJames Feist 10398bd25ccdSJames Feist BMCWEB_LOG_ERROR 10408bd25ccdSJames Feist << "Invalid redundancy interface " 10418bd25ccdSJames Feist "types"; 10428bd25ccdSJames Feist messages::internalError( 10438bd25ccdSJames Feist sensorsAsyncResp->res); 10448bd25ccdSJames Feist return; 10458bd25ccdSJames Feist } 10468bd25ccdSJames Feist size_t lastSlash = path.rfind("/"); 10478bd25ccdSJames Feist if (lastSlash == std::string::npos) 10488bd25ccdSJames Feist { 10498bd25ccdSJames Feist // this should be impossible 10508bd25ccdSJames Feist messages::internalError( 10518bd25ccdSJames Feist sensorsAsyncResp->res); 10528bd25ccdSJames Feist return; 10538bd25ccdSJames Feist } 10548bd25ccdSJames Feist std::string name = path.substr(lastSlash + 1); 10558bd25ccdSJames Feist std::replace(name.begin(), name.end(), '_', 10568bd25ccdSJames Feist ' '); 10578bd25ccdSJames Feist 10588bd25ccdSJames Feist std::string health; 10598bd25ccdSJames Feist 10608bd25ccdSJames Feist if (boost::ends_with(*status, "Full")) 10618bd25ccdSJames Feist { 10628bd25ccdSJames Feist health = "OK"; 10638bd25ccdSJames Feist } 10648bd25ccdSJames Feist else if (boost::ends_with(*status, "Degraded")) 10658bd25ccdSJames Feist { 10668bd25ccdSJames Feist health = "Warning"; 10678bd25ccdSJames Feist } 10688bd25ccdSJames Feist else 10698bd25ccdSJames Feist { 10708bd25ccdSJames Feist health = "Critical"; 10718bd25ccdSJames Feist } 10728bd25ccdSJames Feist std::vector<nlohmann::json> redfishCollection; 10738bd25ccdSJames Feist const auto& fanRedfish = 10748bd25ccdSJames Feist sensorsAsyncResp->res.jsonValue["Fans"]; 10758bd25ccdSJames Feist for (const std::string& item : *collection) 10768bd25ccdSJames Feist { 10778bd25ccdSJames Feist lastSlash = item.rfind("/"); 10788bd25ccdSJames Feist // make a copy as collection is const 10798bd25ccdSJames Feist std::string itemName = 10808bd25ccdSJames Feist item.substr(lastSlash + 1); 10818bd25ccdSJames Feist /* 10828bd25ccdSJames Feist todo(ed): merge patch that fixes the names 10838bd25ccdSJames Feist std::replace(itemName.begin(), 10848bd25ccdSJames Feist itemName.end(), '_', ' ');*/ 10858bd25ccdSJames Feist auto schemaItem = std::find_if( 10868bd25ccdSJames Feist fanRedfish.begin(), fanRedfish.end(), 10878bd25ccdSJames Feist [itemName](const nlohmann::json& fan) { 10888bd25ccdSJames Feist return fan["MemberId"] == itemName; 10898bd25ccdSJames Feist }); 10908bd25ccdSJames Feist if (schemaItem != fanRedfish.end()) 10918bd25ccdSJames Feist { 10928bd25ccdSJames Feist redfishCollection.push_back( 10938bd25ccdSJames Feist {{"@odata.id", 10948bd25ccdSJames Feist (*schemaItem)["@odata.id"]}}); 10958bd25ccdSJames Feist } 10968bd25ccdSJames Feist else 10978bd25ccdSJames Feist { 10988bd25ccdSJames Feist BMCWEB_LOG_ERROR 10998bd25ccdSJames Feist << "failed to find fan in schema"; 11008bd25ccdSJames Feist messages::internalError( 11018bd25ccdSJames Feist sensorsAsyncResp->res); 11028bd25ccdSJames Feist return; 11038bd25ccdSJames Feist } 11048bd25ccdSJames Feist } 11058bd25ccdSJames Feist 1106271584abSEd Tanous nlohmann::json& jResp = 1107271584abSEd Tanous sensorsAsyncResp->res 11088bd25ccdSJames Feist .jsonValue["Redundancy"]; 1109271584abSEd Tanous jResp.push_back( 11108bd25ccdSJames Feist {{"@odata.id", 1111717794d5SAppaRao Puli "/redfish/v1/Chassis/" + 11128bd25ccdSJames Feist sensorsAsyncResp->chassisId + "/" + 11138bd25ccdSJames Feist sensorsAsyncResp->chassisSubNode + 11148bd25ccdSJames Feist "#/Redundancy/" + 1115271584abSEd Tanous std::to_string(jResp.size())}, 11168bd25ccdSJames Feist {"@odata.type", 11178bd25ccdSJames Feist "#Redundancy.v1_3_2.Redundancy"}, 11188bd25ccdSJames Feist {"MinNumNeeded", 11198bd25ccdSJames Feist collection->size() - *allowedFailures}, 11208bd25ccdSJames Feist {"MemberId", name}, 11218bd25ccdSJames Feist {"Mode", "N+m"}, 11228bd25ccdSJames Feist {"Name", name}, 11238bd25ccdSJames Feist {"RedundancySet", redfishCollection}, 11248bd25ccdSJames Feist {"Status", 11258bd25ccdSJames Feist {{"Health", health}, 11268bd25ccdSJames Feist {"State", "Enabled"}}}}); 11278bd25ccdSJames Feist }, 11288bd25ccdSJames Feist owner, path, "org.freedesktop.DBus.Properties", 11298bd25ccdSJames Feist "GetAll", 11308bd25ccdSJames Feist "xyz.openbmc_project.Control.FanRedundancy"); 11318bd25ccdSJames Feist }, 113202e92e32SJames Feist "xyz.openbmc_project.ObjectMapper", path + "/chassis", 11338bd25ccdSJames Feist "org.freedesktop.DBus.Properties", "Get", 11348bd25ccdSJames Feist "xyz.openbmc_project.Association", "endpoints"); 11358bd25ccdSJames Feist } 11368bd25ccdSJames Feist }, 11378bd25ccdSJames Feist "xyz.openbmc_project.ObjectMapper", 11388bd25ccdSJames Feist "/xyz/openbmc_project/object_mapper", 11398bd25ccdSJames Feist "xyz.openbmc_project.ObjectMapper", "GetSubTree", 11408bd25ccdSJames Feist "/xyz/openbmc_project/control", 2, 11418bd25ccdSJames Feist std::array<const char*, 1>{ 11428bd25ccdSJames Feist "xyz.openbmc_project.Control.FanRedundancy"}); 11438bd25ccdSJames Feist } 11448bd25ccdSJames Feist 114549c53ac9SJohnathan Mantey void sortJSONResponse(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp) 114649c53ac9SJohnathan Mantey { 114749c53ac9SJohnathan Mantey nlohmann::json& response = SensorsAsyncResp->res.jsonValue; 114849c53ac9SJohnathan Mantey std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"}; 114949c53ac9SJohnathan Mantey if (SensorsAsyncResp->chassisSubNode == "Power") 115049c53ac9SJohnathan Mantey { 115149c53ac9SJohnathan Mantey sensorHeaders = {"Voltages", "PowerSupplies"}; 115249c53ac9SJohnathan Mantey } 115349c53ac9SJohnathan Mantey for (const std::string& sensorGroup : sensorHeaders) 115449c53ac9SJohnathan Mantey { 115549c53ac9SJohnathan Mantey nlohmann::json::iterator entry = response.find(sensorGroup); 115649c53ac9SJohnathan Mantey if (entry != response.end()) 115749c53ac9SJohnathan Mantey { 115849c53ac9SJohnathan Mantey std::sort(entry->begin(), entry->end(), 115949c53ac9SJohnathan Mantey [](nlohmann::json& c1, nlohmann::json& c2) { 116049c53ac9SJohnathan Mantey return c1["Name"] < c2["Name"]; 116149c53ac9SJohnathan Mantey }); 116249c53ac9SJohnathan Mantey 116349c53ac9SJohnathan Mantey // add the index counts to the end of each entry 116449c53ac9SJohnathan Mantey size_t count = 0; 116549c53ac9SJohnathan Mantey for (nlohmann::json& sensorJson : *entry) 116649c53ac9SJohnathan Mantey { 116749c53ac9SJohnathan Mantey nlohmann::json::iterator odata = sensorJson.find("@odata.id"); 116849c53ac9SJohnathan Mantey if (odata == sensorJson.end()) 116949c53ac9SJohnathan Mantey { 117049c53ac9SJohnathan Mantey continue; 117149c53ac9SJohnathan Mantey } 117249c53ac9SJohnathan Mantey std::string* value = odata->get_ptr<std::string*>(); 117349c53ac9SJohnathan Mantey if (value != nullptr) 117449c53ac9SJohnathan Mantey { 117549c53ac9SJohnathan Mantey *value += std::to_string(count); 117649c53ac9SJohnathan Mantey count++; 117749c53ac9SJohnathan Mantey } 117849c53ac9SJohnathan Mantey } 117949c53ac9SJohnathan Mantey } 118049c53ac9SJohnathan Mantey } 118149c53ac9SJohnathan Mantey } 118249c53ac9SJohnathan Mantey 118308777fb0SLewanczyk, Dawid /** 1184adc4f0dbSShawn McCarney * @brief Finds the inventory item with the specified object path. 1185adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1186adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 1187adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 11888fb49dd6SShawn McCarney */ 1189adc4f0dbSShawn McCarney static InventoryItem* findInventoryItem( 1190adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1191adc4f0dbSShawn McCarney const std::string& invItemObjPath) 11928fb49dd6SShawn McCarney { 1193adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 11948fb49dd6SShawn McCarney { 1195adc4f0dbSShawn McCarney if (inventoryItem.objectPath == invItemObjPath) 11968fb49dd6SShawn McCarney { 1197adc4f0dbSShawn McCarney return &inventoryItem; 11988fb49dd6SShawn McCarney } 11998fb49dd6SShawn McCarney } 12008fb49dd6SShawn McCarney return nullptr; 12018fb49dd6SShawn McCarney } 12028fb49dd6SShawn McCarney 12038fb49dd6SShawn McCarney /** 1204adc4f0dbSShawn McCarney * @brief Finds the inventory item associated with the specified sensor. 1205adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1206adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor. 1207adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 12088fb49dd6SShawn McCarney */ 1209adc4f0dbSShawn McCarney static InventoryItem* findInventoryItemForSensor( 1210adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1211adc4f0dbSShawn McCarney const std::string& sensorObjPath) 1212adc4f0dbSShawn McCarney { 1213adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 1214adc4f0dbSShawn McCarney { 1215adc4f0dbSShawn McCarney if (inventoryItem.sensors.count(sensorObjPath) > 0) 1216adc4f0dbSShawn McCarney { 1217adc4f0dbSShawn McCarney return &inventoryItem; 1218adc4f0dbSShawn McCarney } 1219adc4f0dbSShawn McCarney } 1220adc4f0dbSShawn McCarney return nullptr; 1221adc4f0dbSShawn McCarney } 1222adc4f0dbSShawn McCarney 1223adc4f0dbSShawn McCarney /** 1224d500549bSAnthony Wilson * @brief Finds the inventory item associated with the specified led path. 1225d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1226d500549bSAnthony Wilson * @param ledObjPath D-Bus object path of led. 1227d500549bSAnthony Wilson * @return Inventory item within vector, or nullptr if no match found. 1228d500549bSAnthony Wilson */ 1229d500549bSAnthony Wilson inline InventoryItem* 1230d500549bSAnthony Wilson findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems, 1231d500549bSAnthony Wilson const std::string& ledObjPath) 1232d500549bSAnthony Wilson { 1233d500549bSAnthony Wilson for (InventoryItem& inventoryItem : inventoryItems) 1234d500549bSAnthony Wilson { 1235d500549bSAnthony Wilson if (inventoryItem.ledObjectPath == ledObjPath) 1236d500549bSAnthony Wilson { 1237d500549bSAnthony Wilson return &inventoryItem; 1238d500549bSAnthony Wilson } 1239d500549bSAnthony Wilson } 1240d500549bSAnthony Wilson return nullptr; 1241d500549bSAnthony Wilson } 1242d500549bSAnthony Wilson 1243d500549bSAnthony Wilson /** 1244adc4f0dbSShawn McCarney * @brief Adds inventory item and associated sensor to specified vector. 1245adc4f0dbSShawn McCarney * 1246adc4f0dbSShawn McCarney * Adds a new InventoryItem to the vector if necessary. Searches for an 1247adc4f0dbSShawn McCarney * existing InventoryItem with the specified object path. If not found, one is 1248adc4f0dbSShawn McCarney * added to the vector. 1249adc4f0dbSShawn McCarney * 1250adc4f0dbSShawn McCarney * Next, the specified sensor is added to the set of sensors associated with the 1251adc4f0dbSShawn McCarney * InventoryItem. 1252adc4f0dbSShawn McCarney * 1253adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1254adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 1255adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor 1256adc4f0dbSShawn McCarney */ 1257adc4f0dbSShawn McCarney static void 1258adc4f0dbSShawn McCarney addInventoryItem(std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1259adc4f0dbSShawn McCarney const std::string& invItemObjPath, 1260adc4f0dbSShawn McCarney const std::string& sensorObjPath) 1261adc4f0dbSShawn McCarney { 1262adc4f0dbSShawn McCarney // Look for inventory item in vector 1263adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1264adc4f0dbSShawn McCarney findInventoryItem(inventoryItems, invItemObjPath); 1265adc4f0dbSShawn McCarney 1266adc4f0dbSShawn McCarney // If inventory item doesn't exist in vector, add it 1267adc4f0dbSShawn McCarney if (inventoryItem == nullptr) 1268adc4f0dbSShawn McCarney { 1269adc4f0dbSShawn McCarney inventoryItems->emplace_back(invItemObjPath); 1270adc4f0dbSShawn McCarney inventoryItem = &(inventoryItems->back()); 1271adc4f0dbSShawn McCarney } 1272adc4f0dbSShawn McCarney 1273adc4f0dbSShawn McCarney // Add sensor to set of sensors associated with inventory item 1274adc4f0dbSShawn McCarney inventoryItem->sensors.emplace(sensorObjPath); 1275adc4f0dbSShawn McCarney } 1276adc4f0dbSShawn McCarney 1277adc4f0dbSShawn McCarney /** 1278adc4f0dbSShawn McCarney * @brief Stores D-Bus data in the specified inventory item. 1279adc4f0dbSShawn McCarney * 1280adc4f0dbSShawn McCarney * Finds D-Bus data in the specified map of interfaces. Stores the data in the 1281adc4f0dbSShawn McCarney * specified InventoryItem. 1282adc4f0dbSShawn McCarney * 1283adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1284adc4f0dbSShawn McCarney * response. 1285adc4f0dbSShawn McCarney * 1286adc4f0dbSShawn McCarney * @param inventoryItem Inventory item where data will be stored. 1287adc4f0dbSShawn McCarney * @param interfacesDict Map containing D-Bus interfaces and their properties 1288adc4f0dbSShawn McCarney * for the specified inventory item. 1289adc4f0dbSShawn McCarney */ 1290adc4f0dbSShawn McCarney static void storeInventoryItemData( 1291adc4f0dbSShawn McCarney InventoryItem& inventoryItem, 12928fb49dd6SShawn McCarney const boost::container::flat_map< 12938fb49dd6SShawn McCarney std::string, boost::container::flat_map<std::string, SensorVariant>>& 12948fb49dd6SShawn McCarney interfacesDict) 12958fb49dd6SShawn McCarney { 1296adc4f0dbSShawn McCarney // Get properties from Inventory.Item interface 1297adc4f0dbSShawn McCarney auto interfaceIt = 1298adc4f0dbSShawn McCarney interfacesDict.find("xyz.openbmc_project.Inventory.Item"); 1299adc4f0dbSShawn McCarney if (interfaceIt != interfacesDict.end()) 13008fb49dd6SShawn McCarney { 1301adc4f0dbSShawn McCarney auto propertyIt = interfaceIt->second.find("Present"); 1302adc4f0dbSShawn McCarney if (propertyIt != interfaceIt->second.end()) 13038fb49dd6SShawn McCarney { 1304adc4f0dbSShawn McCarney const bool* value = std::get_if<bool>(&propertyIt->second); 1305adc4f0dbSShawn McCarney if (value != nullptr) 13068fb49dd6SShawn McCarney { 1307adc4f0dbSShawn McCarney inventoryItem.isPresent = *value; 13088fb49dd6SShawn McCarney } 13098fb49dd6SShawn McCarney } 13108fb49dd6SShawn McCarney } 13118fb49dd6SShawn McCarney 1312adc4f0dbSShawn McCarney // Check if Inventory.Item.PowerSupply interface is present 1313adc4f0dbSShawn McCarney interfaceIt = 1314adc4f0dbSShawn McCarney interfacesDict.find("xyz.openbmc_project.Inventory.Item.PowerSupply"); 1315adc4f0dbSShawn McCarney if (interfaceIt != interfacesDict.end()) 13168fb49dd6SShawn McCarney { 1317adc4f0dbSShawn McCarney inventoryItem.isPowerSupply = true; 13188fb49dd6SShawn McCarney } 1319adc4f0dbSShawn McCarney 1320adc4f0dbSShawn McCarney // Get properties from Inventory.Decorator.Asset interface 1321adc4f0dbSShawn McCarney interfaceIt = 1322adc4f0dbSShawn McCarney interfacesDict.find("xyz.openbmc_project.Inventory.Decorator.Asset"); 1323adc4f0dbSShawn McCarney if (interfaceIt != interfacesDict.end()) 1324adc4f0dbSShawn McCarney { 1325adc4f0dbSShawn McCarney auto propertyIt = interfaceIt->second.find("Manufacturer"); 1326adc4f0dbSShawn McCarney if (propertyIt != interfaceIt->second.end()) 1327adc4f0dbSShawn McCarney { 1328adc4f0dbSShawn McCarney const std::string* value = 1329adc4f0dbSShawn McCarney std::get_if<std::string>(&propertyIt->second); 1330adc4f0dbSShawn McCarney if (value != nullptr) 1331adc4f0dbSShawn McCarney { 1332adc4f0dbSShawn McCarney inventoryItem.manufacturer = *value; 1333adc4f0dbSShawn McCarney } 1334adc4f0dbSShawn McCarney } 1335adc4f0dbSShawn McCarney 1336adc4f0dbSShawn McCarney propertyIt = interfaceIt->second.find("Model"); 1337adc4f0dbSShawn McCarney if (propertyIt != interfaceIt->second.end()) 1338adc4f0dbSShawn McCarney { 1339adc4f0dbSShawn McCarney const std::string* value = 1340adc4f0dbSShawn McCarney std::get_if<std::string>(&propertyIt->second); 1341adc4f0dbSShawn McCarney if (value != nullptr) 1342adc4f0dbSShawn McCarney { 1343adc4f0dbSShawn McCarney inventoryItem.model = *value; 1344adc4f0dbSShawn McCarney } 1345adc4f0dbSShawn McCarney } 1346adc4f0dbSShawn McCarney 1347adc4f0dbSShawn McCarney propertyIt = interfaceIt->second.find("PartNumber"); 1348adc4f0dbSShawn McCarney if (propertyIt != interfaceIt->second.end()) 1349adc4f0dbSShawn McCarney { 1350adc4f0dbSShawn McCarney const std::string* value = 1351adc4f0dbSShawn McCarney std::get_if<std::string>(&propertyIt->second); 1352adc4f0dbSShawn McCarney if (value != nullptr) 1353adc4f0dbSShawn McCarney { 1354adc4f0dbSShawn McCarney inventoryItem.partNumber = *value; 1355adc4f0dbSShawn McCarney } 1356adc4f0dbSShawn McCarney } 1357adc4f0dbSShawn McCarney 1358adc4f0dbSShawn McCarney propertyIt = interfaceIt->second.find("SerialNumber"); 1359adc4f0dbSShawn McCarney if (propertyIt != interfaceIt->second.end()) 1360adc4f0dbSShawn McCarney { 1361adc4f0dbSShawn McCarney const std::string* value = 1362adc4f0dbSShawn McCarney std::get_if<std::string>(&propertyIt->second); 1363adc4f0dbSShawn McCarney if (value != nullptr) 1364adc4f0dbSShawn McCarney { 1365adc4f0dbSShawn McCarney inventoryItem.serialNumber = *value; 1366adc4f0dbSShawn McCarney } 1367adc4f0dbSShawn McCarney } 1368adc4f0dbSShawn McCarney } 1369adc4f0dbSShawn McCarney 1370adc4f0dbSShawn McCarney // Get properties from State.Decorator.OperationalStatus interface 1371adc4f0dbSShawn McCarney interfaceIt = interfacesDict.find( 1372adc4f0dbSShawn McCarney "xyz.openbmc_project.State.Decorator.OperationalStatus"); 1373adc4f0dbSShawn McCarney if (interfaceIt != interfacesDict.end()) 1374adc4f0dbSShawn McCarney { 1375adc4f0dbSShawn McCarney auto propertyIt = interfaceIt->second.find("Functional"); 1376adc4f0dbSShawn McCarney if (propertyIt != interfaceIt->second.end()) 1377adc4f0dbSShawn McCarney { 1378adc4f0dbSShawn McCarney const bool* value = std::get_if<bool>(&propertyIt->second); 1379adc4f0dbSShawn McCarney if (value != nullptr) 1380adc4f0dbSShawn McCarney { 1381adc4f0dbSShawn McCarney inventoryItem.isFunctional = *value; 13828fb49dd6SShawn McCarney } 13838fb49dd6SShawn McCarney } 13848fb49dd6SShawn McCarney } 13858fb49dd6SShawn McCarney } 13868fb49dd6SShawn McCarney 13878fb49dd6SShawn McCarney /** 1388adc4f0dbSShawn McCarney * @brief Gets D-Bus data for inventory items associated with sensors. 13898fb49dd6SShawn McCarney * 1390adc4f0dbSShawn McCarney * Uses the specified connections (services) to obtain D-Bus data for inventory 1391adc4f0dbSShawn McCarney * items associated with sensors. Stores the resulting data in the 1392adc4f0dbSShawn McCarney * inventoryItems vector. 13938fb49dd6SShawn McCarney * 1394adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1395adc4f0dbSShawn McCarney * response. 1396adc4f0dbSShawn McCarney * 1397adc4f0dbSShawn McCarney * Finds the inventory item data asynchronously. Invokes callback when data has 1398adc4f0dbSShawn McCarney * been obtained. 1399adc4f0dbSShawn McCarney * 1400adc4f0dbSShawn McCarney * The callback must have the following signature: 1401adc4f0dbSShawn McCarney * @code 1402d500549bSAnthony Wilson * callback(void) 1403adc4f0dbSShawn McCarney * @endcode 1404adc4f0dbSShawn McCarney * 1405adc4f0dbSShawn McCarney * This function is called recursively, obtaining data asynchronously from one 1406adc4f0dbSShawn McCarney * connection in each call. This ensures the callback is not invoked until the 1407adc4f0dbSShawn McCarney * last asynchronous function has completed. 14088fb49dd6SShawn McCarney * 14098fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1410adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1411adc4f0dbSShawn McCarney * @param invConnections Connections that provide data for the inventory items. 14128fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 14138fb49dd6SShawn McCarney * implements ObjectManager. 1414adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory data has been obtained. 1415adc4f0dbSShawn McCarney * @param invConnectionsIndex Current index in invConnections. Only specified 1416adc4f0dbSShawn McCarney * in recursive calls to this function. 14178fb49dd6SShawn McCarney */ 1418adc4f0dbSShawn McCarney template <typename Callback> 1419adc4f0dbSShawn McCarney static void getInventoryItemsData( 14208fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1421adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 14228fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_set<std::string>> invConnections, 14238fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 1424adc4f0dbSShawn McCarney objectMgrPaths, 1425271584abSEd Tanous Callback&& callback, size_t invConnectionsIndex = 0) 14268fb49dd6SShawn McCarney { 1427adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData enter"; 14288fb49dd6SShawn McCarney 1429adc4f0dbSShawn McCarney // If no more connections left, call callback 1430adc4f0dbSShawn McCarney if (invConnectionsIndex >= invConnections->size()) 14318fb49dd6SShawn McCarney { 1432d500549bSAnthony Wilson callback(); 1433adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData exit"; 1434adc4f0dbSShawn McCarney return; 1435adc4f0dbSShawn McCarney } 1436adc4f0dbSShawn McCarney 1437adc4f0dbSShawn McCarney // Get inventory item data from current connection 1438adc4f0dbSShawn McCarney auto it = invConnections->nth(invConnectionsIndex); 1439adc4f0dbSShawn McCarney if (it != invConnections->end()) 1440adc4f0dbSShawn McCarney { 1441adc4f0dbSShawn McCarney const std::string& invConnection = *it; 1442adc4f0dbSShawn McCarney 14438fb49dd6SShawn McCarney // Response handler for GetManagedObjects 1444adc4f0dbSShawn McCarney auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections, 1445adc4f0dbSShawn McCarney objectMgrPaths, callback{std::move(callback)}, 1446adc4f0dbSShawn McCarney invConnectionsIndex]( 1447adc4f0dbSShawn McCarney const boost::system::error_code ec, 14488fb49dd6SShawn McCarney ManagedObjectsVectorType& resp) { 1449adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter"; 14508fb49dd6SShawn McCarney if (ec) 14518fb49dd6SShawn McCarney { 14528fb49dd6SShawn McCarney BMCWEB_LOG_ERROR 1453adc4f0dbSShawn McCarney << "getInventoryItemsData respHandler DBus error " << ec; 14548fb49dd6SShawn McCarney messages::internalError(sensorsAsyncResp->res); 14558fb49dd6SShawn McCarney return; 14568fb49dd6SShawn McCarney } 14578fb49dd6SShawn McCarney 14588fb49dd6SShawn McCarney // Loop through returned object paths 14598fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 14608fb49dd6SShawn McCarney { 14618fb49dd6SShawn McCarney const std::string& objPath = 14628fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 14638fb49dd6SShawn McCarney 1464adc4f0dbSShawn McCarney // If this object path is one of the specified inventory items 1465adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1466adc4f0dbSShawn McCarney findInventoryItem(inventoryItems, objPath); 1467adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 14688fb49dd6SShawn McCarney { 1469adc4f0dbSShawn McCarney // Store inventory data in InventoryItem 1470adc4f0dbSShawn McCarney storeInventoryItemData(*inventoryItem, objDictEntry.second); 14718fb49dd6SShawn McCarney } 14728fb49dd6SShawn McCarney } 14738fb49dd6SShawn McCarney 1474adc4f0dbSShawn McCarney // Recurse to get inventory item data from next connection 1475adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 1476adc4f0dbSShawn McCarney invConnections, objectMgrPaths, 1477adc4f0dbSShawn McCarney std::move(callback), invConnectionsIndex + 1); 1478adc4f0dbSShawn McCarney 1479adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit"; 14808fb49dd6SShawn McCarney }; 14818fb49dd6SShawn McCarney 14828fb49dd6SShawn McCarney // Find DBus object path that implements ObjectManager for the current 14838fb49dd6SShawn McCarney // connection. If no mapping found, default to "/". 14848fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(invConnection); 14858fb49dd6SShawn McCarney const std::string& objectMgrPath = 14868fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 14878fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is " 14888fb49dd6SShawn McCarney << objectMgrPath; 14898fb49dd6SShawn McCarney 14908fb49dd6SShawn McCarney // Get all object paths and their interfaces for current connection 14918fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 14928fb49dd6SShawn McCarney std::move(respHandler), invConnection, objectMgrPath, 14938fb49dd6SShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 14948fb49dd6SShawn McCarney } 14958fb49dd6SShawn McCarney 1496adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData exit"; 14978fb49dd6SShawn McCarney } 14988fb49dd6SShawn McCarney 14998fb49dd6SShawn McCarney /** 1500adc4f0dbSShawn McCarney * @brief Gets connections that provide D-Bus data for inventory items. 15018fb49dd6SShawn McCarney * 1502adc4f0dbSShawn McCarney * Gets the D-Bus connections (services) that provide data for the inventory 1503adc4f0dbSShawn McCarney * items that are associated with sensors. 15048fb49dd6SShawn McCarney * 15058fb49dd6SShawn McCarney * Finds the connections asynchronously. Invokes callback when information has 15068fb49dd6SShawn McCarney * been obtained. 15078fb49dd6SShawn McCarney * 15088fb49dd6SShawn McCarney * The callback must have the following signature: 15098fb49dd6SShawn McCarney * @code 15108fb49dd6SShawn McCarney * callback(std::shared_ptr<boost::container::flat_set<std::string>> 15118fb49dd6SShawn McCarney * invConnections) 15128fb49dd6SShawn McCarney * @endcode 15138fb49dd6SShawn McCarney * 15148fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1515adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 15168fb49dd6SShawn McCarney * @param callback Callback to invoke when connections have been obtained. 15178fb49dd6SShawn McCarney */ 15188fb49dd6SShawn McCarney template <typename Callback> 15198fb49dd6SShawn McCarney static void getInventoryItemsConnections( 15208fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1521adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 15228fb49dd6SShawn McCarney Callback&& callback) 15238fb49dd6SShawn McCarney { 15248fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter"; 15258fb49dd6SShawn McCarney 15268fb49dd6SShawn McCarney const std::string path = "/xyz/openbmc_project/inventory"; 1527adc4f0dbSShawn McCarney const std::array<std::string, 4> interfaces = { 15288fb49dd6SShawn McCarney "xyz.openbmc_project.Inventory.Item", 1529adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.PowerSupply", 1530adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Decorator.Asset", 15318fb49dd6SShawn McCarney "xyz.openbmc_project.State.Decorator.OperationalStatus"}; 15328fb49dd6SShawn McCarney 15338fb49dd6SShawn McCarney // Response handler for parsing output from GetSubTree 15348fb49dd6SShawn McCarney auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp, 1535adc4f0dbSShawn McCarney inventoryItems](const boost::system::error_code ec, 15368fb49dd6SShawn McCarney const GetSubTreeType& subtree) { 15378fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter"; 15388fb49dd6SShawn McCarney if (ec) 15398fb49dd6SShawn McCarney { 15408fb49dd6SShawn McCarney messages::internalError(sensorsAsyncResp->res); 15418fb49dd6SShawn McCarney BMCWEB_LOG_ERROR 15428fb49dd6SShawn McCarney << "getInventoryItemsConnections respHandler DBus error " << ec; 15438fb49dd6SShawn McCarney return; 15448fb49dd6SShawn McCarney } 15458fb49dd6SShawn McCarney 15468fb49dd6SShawn McCarney // Make unique list of connections for desired inventory items 15478fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_set<std::string>> 15488fb49dd6SShawn McCarney invConnections = 15498fb49dd6SShawn McCarney std::make_shared<boost::container::flat_set<std::string>>(); 15508fb49dd6SShawn McCarney invConnections->reserve(8); 15518fb49dd6SShawn McCarney 15528fb49dd6SShawn McCarney // Loop through objects from GetSubTree 15538fb49dd6SShawn McCarney for (const std::pair< 15548fb49dd6SShawn McCarney std::string, 15558fb49dd6SShawn McCarney std::vector<std::pair<std::string, std::vector<std::string>>>>& 15568fb49dd6SShawn McCarney object : subtree) 15578fb49dd6SShawn McCarney { 1558adc4f0dbSShawn McCarney // Check if object path is one of the specified inventory items 15598fb49dd6SShawn McCarney const std::string& objPath = object.first; 1560adc4f0dbSShawn McCarney if (findInventoryItem(inventoryItems, objPath) != nullptr) 15618fb49dd6SShawn McCarney { 15628fb49dd6SShawn McCarney // Store all connections to inventory item 15638fb49dd6SShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 15648fb49dd6SShawn McCarney objData : object.second) 15658fb49dd6SShawn McCarney { 15668fb49dd6SShawn McCarney const std::string& invConnection = objData.first; 15678fb49dd6SShawn McCarney invConnections->insert(invConnection); 15688fb49dd6SShawn McCarney } 15698fb49dd6SShawn McCarney } 15708fb49dd6SShawn McCarney } 1571d500549bSAnthony Wilson 15728fb49dd6SShawn McCarney callback(invConnections); 15738fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit"; 15748fb49dd6SShawn McCarney }; 15758fb49dd6SShawn McCarney 15768fb49dd6SShawn McCarney // Make call to ObjectMapper to find all inventory items 15778fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 15788fb49dd6SShawn McCarney std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 15798fb49dd6SShawn McCarney "/xyz/openbmc_project/object_mapper", 15808fb49dd6SShawn McCarney "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); 15818fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit"; 15828fb49dd6SShawn McCarney } 15838fb49dd6SShawn McCarney 15848fb49dd6SShawn McCarney /** 1585adc4f0dbSShawn McCarney * @brief Gets associations from sensors to inventory items. 15868fb49dd6SShawn McCarney * 15878fb49dd6SShawn McCarney * Looks for ObjectMapper associations from the specified sensors to related 1588d500549bSAnthony Wilson * inventory items. Then finds the associations from those inventory items to 1589d500549bSAnthony Wilson * their LEDs, if any. 15908fb49dd6SShawn McCarney * 15918fb49dd6SShawn McCarney * Finds the inventory items asynchronously. Invokes callback when information 15928fb49dd6SShawn McCarney * has been obtained. 15938fb49dd6SShawn McCarney * 15948fb49dd6SShawn McCarney * The callback must have the following signature: 15958fb49dd6SShawn McCarney * @code 1596adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 15978fb49dd6SShawn McCarney * @endcode 15988fb49dd6SShawn McCarney * 15998fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 16008fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 16018fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 16028fb49dd6SShawn McCarney * implements ObjectManager. 16038fb49dd6SShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 16048fb49dd6SShawn McCarney */ 16058fb49dd6SShawn McCarney template <typename Callback> 1606adc4f0dbSShawn McCarney static void getInventoryItemAssociations( 16078fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 16088fb49dd6SShawn McCarney const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames, 16098fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 16108fb49dd6SShawn McCarney objectMgrPaths, 16118fb49dd6SShawn McCarney Callback&& callback) 16128fb49dd6SShawn McCarney { 1613adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter"; 16148fb49dd6SShawn McCarney 16158fb49dd6SShawn McCarney // Response handler for GetManagedObjects 16168fb49dd6SShawn McCarney auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp, 16178fb49dd6SShawn McCarney sensorNames](const boost::system::error_code ec, 16188fb49dd6SShawn McCarney dbus::utility::ManagedObjectType& resp) { 1619adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter"; 16208fb49dd6SShawn McCarney if (ec) 16218fb49dd6SShawn McCarney { 1622adc4f0dbSShawn McCarney BMCWEB_LOG_ERROR 1623adc4f0dbSShawn McCarney << "getInventoryItemAssociations respHandler DBus error " << ec; 16248fb49dd6SShawn McCarney messages::internalError(sensorsAsyncResp->res); 16258fb49dd6SShawn McCarney return; 16268fb49dd6SShawn McCarney } 16278fb49dd6SShawn McCarney 1628adc4f0dbSShawn McCarney // Create vector to hold list of inventory items 1629adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems = 1630adc4f0dbSShawn McCarney std::make_shared<std::vector<InventoryItem>>(); 1631adc4f0dbSShawn McCarney 16328fb49dd6SShawn McCarney // Loop through returned object paths 16338fb49dd6SShawn McCarney std::string sensorAssocPath; 16348fb49dd6SShawn McCarney sensorAssocPath.reserve(128); // avoid memory allocations 16358fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 16368fb49dd6SShawn McCarney { 16378fb49dd6SShawn McCarney const std::string& objPath = 16388fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 16398fb49dd6SShawn McCarney const boost::container::flat_map< 16408fb49dd6SShawn McCarney std::string, boost::container::flat_map< 16418fb49dd6SShawn McCarney std::string, dbus::utility::DbusVariantType>>& 16428fb49dd6SShawn McCarney interfacesDict = objDictEntry.second; 16438fb49dd6SShawn McCarney 16448fb49dd6SShawn McCarney // If path is inventory association for one of the specified sensors 16458fb49dd6SShawn McCarney for (const std::string& sensorName : *sensorNames) 16468fb49dd6SShawn McCarney { 16478fb49dd6SShawn McCarney sensorAssocPath = sensorName; 16488fb49dd6SShawn McCarney sensorAssocPath += "/inventory"; 16498fb49dd6SShawn McCarney if (objPath == sensorAssocPath) 16508fb49dd6SShawn McCarney { 16518fb49dd6SShawn McCarney // Get Association interface for object path 16528fb49dd6SShawn McCarney auto assocIt = 16538fb49dd6SShawn McCarney interfacesDict.find("xyz.openbmc_project.Association"); 16548fb49dd6SShawn McCarney if (assocIt != interfacesDict.end()) 16558fb49dd6SShawn McCarney { 16568fb49dd6SShawn McCarney // Get inventory item from end point 16578fb49dd6SShawn McCarney auto endpointsIt = assocIt->second.find("endpoints"); 16588fb49dd6SShawn McCarney if (endpointsIt != assocIt->second.end()) 16598fb49dd6SShawn McCarney { 16608fb49dd6SShawn McCarney const std::vector<std::string>* endpoints = 16618fb49dd6SShawn McCarney std::get_if<std::vector<std::string>>( 16628fb49dd6SShawn McCarney &endpointsIt->second); 16638fb49dd6SShawn McCarney if ((endpoints != nullptr) && !endpoints->empty()) 16648fb49dd6SShawn McCarney { 1665adc4f0dbSShawn McCarney // Add inventory item to vector 1666adc4f0dbSShawn McCarney const std::string& invItemPath = 1667adc4f0dbSShawn McCarney endpoints->front(); 1668adc4f0dbSShawn McCarney addInventoryItem(inventoryItems, invItemPath, 1669adc4f0dbSShawn McCarney sensorName); 16708fb49dd6SShawn McCarney } 16718fb49dd6SShawn McCarney } 16728fb49dd6SShawn McCarney } 16738fb49dd6SShawn McCarney break; 16748fb49dd6SShawn McCarney } 16758fb49dd6SShawn McCarney } 16768fb49dd6SShawn McCarney } 16778fb49dd6SShawn McCarney 1678d500549bSAnthony Wilson // Now loop through the returned object paths again, this time to 1679d500549bSAnthony Wilson // find the leds associated with the inventory items we just found 1680d500549bSAnthony Wilson std::string inventoryAssocPath; 1681d500549bSAnthony Wilson inventoryAssocPath.reserve(128); // avoid memory allocations 1682d500549bSAnthony Wilson for (const auto& objDictEntry : resp) 1683d500549bSAnthony Wilson { 1684d500549bSAnthony Wilson const std::string& objPath = 1685d500549bSAnthony Wilson static_cast<const std::string&>(objDictEntry.first); 1686d500549bSAnthony Wilson const boost::container::flat_map< 1687d500549bSAnthony Wilson std::string, boost::container::flat_map< 1688d500549bSAnthony Wilson std::string, dbus::utility::DbusVariantType>>& 1689d500549bSAnthony Wilson interfacesDict = objDictEntry.second; 1690d500549bSAnthony Wilson 1691d500549bSAnthony Wilson for (InventoryItem& inventoryItem : *inventoryItems) 1692d500549bSAnthony Wilson { 1693d500549bSAnthony Wilson inventoryAssocPath = inventoryItem.objectPath; 1694d500549bSAnthony Wilson inventoryAssocPath += "/leds"; 1695d500549bSAnthony Wilson if (objPath == inventoryAssocPath) 1696d500549bSAnthony Wilson { 1697d500549bSAnthony Wilson // Get Association interface for object path 1698d500549bSAnthony Wilson auto assocIt = 1699d500549bSAnthony Wilson interfacesDict.find("xyz.openbmc_project.Association"); 1700d500549bSAnthony Wilson if (assocIt != interfacesDict.end()) 1701d500549bSAnthony Wilson { 1702d500549bSAnthony Wilson // Get inventory item from end point 1703d500549bSAnthony Wilson auto endpointsIt = assocIt->second.find("endpoints"); 1704d500549bSAnthony Wilson if (endpointsIt != assocIt->second.end()) 1705d500549bSAnthony Wilson { 1706d500549bSAnthony Wilson const std::vector<std::string>* endpoints = 1707d500549bSAnthony Wilson std::get_if<std::vector<std::string>>( 1708d500549bSAnthony Wilson &endpointsIt->second); 1709d500549bSAnthony Wilson if ((endpoints != nullptr) && !endpoints->empty()) 1710d500549bSAnthony Wilson { 1711d500549bSAnthony Wilson // Store LED path in inventory item 1712d500549bSAnthony Wilson const std::string& ledPath = endpoints->front(); 1713d500549bSAnthony Wilson inventoryItem.ledObjectPath = ledPath; 1714d500549bSAnthony Wilson } 1715d500549bSAnthony Wilson } 1716d500549bSAnthony Wilson } 1717d500549bSAnthony Wilson break; 1718d500549bSAnthony Wilson } 1719d500549bSAnthony Wilson } 1720d500549bSAnthony Wilson } 1721adc4f0dbSShawn McCarney callback(inventoryItems); 1722adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit"; 17238fb49dd6SShawn McCarney }; 17248fb49dd6SShawn McCarney 17258fb49dd6SShawn McCarney // Find DBus object path that implements ObjectManager for ObjectMapper 17268fb49dd6SShawn McCarney std::string connection = "xyz.openbmc_project.ObjectMapper"; 17278fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(connection); 17288fb49dd6SShawn McCarney const std::string& objectMgrPath = 17298fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 17308fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is " 17318fb49dd6SShawn McCarney << objectMgrPath; 17328fb49dd6SShawn McCarney 17338fb49dd6SShawn McCarney // Call GetManagedObjects on the ObjectMapper to get all associations 17348fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 17358fb49dd6SShawn McCarney std::move(respHandler), connection, objectMgrPath, 17368fb49dd6SShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 17378fb49dd6SShawn McCarney 1738adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit"; 17398fb49dd6SShawn McCarney } 17408fb49dd6SShawn McCarney 17418fb49dd6SShawn McCarney /** 1742d500549bSAnthony Wilson * @brief Gets D-Bus data for inventory item leds associated with sensors. 1743d500549bSAnthony Wilson * 1744d500549bSAnthony Wilson * Uses the specified connections (services) to obtain D-Bus data for inventory 1745d500549bSAnthony Wilson * item leds associated with sensors. Stores the resulting data in the 1746d500549bSAnthony Wilson * inventoryItems vector. 1747d500549bSAnthony Wilson * 1748d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 1749d500549bSAnthony Wilson * response. 1750d500549bSAnthony Wilson * 1751d500549bSAnthony Wilson * Finds the inventory item led data asynchronously. Invokes callback when data 1752d500549bSAnthony Wilson * has been obtained. 1753d500549bSAnthony Wilson * 1754d500549bSAnthony Wilson * The callback must have the following signature: 1755d500549bSAnthony Wilson * @code 175642cbe538SGunnar Mills * callback() 1757d500549bSAnthony Wilson * @endcode 1758d500549bSAnthony Wilson * 1759d500549bSAnthony Wilson * This function is called recursively, obtaining data asynchronously from one 1760d500549bSAnthony Wilson * connection in each call. This ensures the callback is not invoked until the 1761d500549bSAnthony Wilson * last asynchronous function has completed. 1762d500549bSAnthony Wilson * 1763d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 1764d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1765d500549bSAnthony Wilson * @param ledConnections Connections that provide data for the inventory leds. 1766d500549bSAnthony Wilson * @param callback Callback to invoke when inventory data has been obtained. 1767d500549bSAnthony Wilson * @param ledConnectionsIndex Current index in ledConnections. Only specified 1768d500549bSAnthony Wilson * in recursive calls to this function. 1769d500549bSAnthony Wilson */ 1770d500549bSAnthony Wilson template <typename Callback> 1771d500549bSAnthony Wilson void getInventoryLedData( 1772d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1773d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1774d500549bSAnthony Wilson std::shared_ptr<boost::container::flat_map<std::string, std::string>> 1775d500549bSAnthony Wilson ledConnections, 1776d500549bSAnthony Wilson Callback&& callback, size_t ledConnectionsIndex = 0) 1777d500549bSAnthony Wilson { 1778d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData enter"; 1779d500549bSAnthony Wilson 1780d500549bSAnthony Wilson // If no more connections left, call callback 1781d500549bSAnthony Wilson if (ledConnectionsIndex >= ledConnections->size()) 1782d500549bSAnthony Wilson { 178342cbe538SGunnar Mills callback(); 1784d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData exit"; 1785d500549bSAnthony Wilson return; 1786d500549bSAnthony Wilson } 1787d500549bSAnthony Wilson 1788d500549bSAnthony Wilson // Get inventory item data from current connection 1789d500549bSAnthony Wilson auto it = ledConnections->nth(ledConnectionsIndex); 1790d500549bSAnthony Wilson if (it != ledConnections->end()) 1791d500549bSAnthony Wilson { 1792d500549bSAnthony Wilson const std::string& ledPath = (*it).first; 1793d500549bSAnthony Wilson const std::string& ledConnection = (*it).second; 1794d500549bSAnthony Wilson // Response handler for Get State property 1795d500549bSAnthony Wilson auto respHandler = 1796d500549bSAnthony Wilson [sensorsAsyncResp, inventoryItems, ledConnections, ledPath, 1797d500549bSAnthony Wilson callback{std::move(callback)}, 1798d500549bSAnthony Wilson ledConnectionsIndex](const boost::system::error_code ec, 1799d500549bSAnthony Wilson const std::variant<std::string>& ledState) { 1800d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter"; 1801d500549bSAnthony Wilson if (ec) 1802d500549bSAnthony Wilson { 1803d500549bSAnthony Wilson BMCWEB_LOG_ERROR 1804d500549bSAnthony Wilson << "getInventoryLedData respHandler DBus error " << ec; 1805d500549bSAnthony Wilson messages::internalError(sensorsAsyncResp->res); 1806d500549bSAnthony Wilson return; 1807d500549bSAnthony Wilson } 1808d500549bSAnthony Wilson 1809d500549bSAnthony Wilson const std::string* state = std::get_if<std::string>(&ledState); 1810d500549bSAnthony Wilson if (state != nullptr) 1811d500549bSAnthony Wilson { 1812d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "Led state: " << *state; 1813d500549bSAnthony Wilson // Find inventory item with this LED object path 1814d500549bSAnthony Wilson InventoryItem* inventoryItem = 1815d500549bSAnthony Wilson findInventoryItemForLed(*inventoryItems, ledPath); 1816d500549bSAnthony Wilson if (inventoryItem != nullptr) 1817d500549bSAnthony Wilson { 1818d500549bSAnthony Wilson // Store LED state in InventoryItem 1819d500549bSAnthony Wilson if (boost::ends_with(*state, "On")) 1820d500549bSAnthony Wilson { 1821d500549bSAnthony Wilson inventoryItem->ledState = LedState::ON; 1822d500549bSAnthony Wilson } 1823d500549bSAnthony Wilson else if (boost::ends_with(*state, "Blink")) 1824d500549bSAnthony Wilson { 1825d500549bSAnthony Wilson inventoryItem->ledState = LedState::BLINK; 1826d500549bSAnthony Wilson } 1827d500549bSAnthony Wilson else if (boost::ends_with(*state, "Off")) 1828d500549bSAnthony Wilson { 1829d500549bSAnthony Wilson inventoryItem->ledState = LedState::OFF; 1830d500549bSAnthony Wilson } 1831d500549bSAnthony Wilson else 1832d500549bSAnthony Wilson { 1833d500549bSAnthony Wilson inventoryItem->ledState = LedState::UNKNOWN; 1834d500549bSAnthony Wilson } 1835d500549bSAnthony Wilson } 1836d500549bSAnthony Wilson } 1837d500549bSAnthony Wilson else 1838d500549bSAnthony Wilson { 1839d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "Failed to find State data for LED: " 1840d500549bSAnthony Wilson << ledPath; 1841d500549bSAnthony Wilson } 1842d500549bSAnthony Wilson 1843d500549bSAnthony Wilson // Recurse to get LED data from next connection 1844d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, 1845d500549bSAnthony Wilson ledConnections, std::move(callback), 1846d500549bSAnthony Wilson ledConnectionsIndex + 1); 1847d500549bSAnthony Wilson 1848d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit"; 1849d500549bSAnthony Wilson }; 1850d500549bSAnthony Wilson 1851d500549bSAnthony Wilson // Get the State property for the current LED 1852d500549bSAnthony Wilson crow::connections::systemBus->async_method_call( 1853d500549bSAnthony Wilson std::move(respHandler), ledConnection, ledPath, 1854d500549bSAnthony Wilson "org.freedesktop.DBus.Properties", "Get", 1855d500549bSAnthony Wilson "xyz.openbmc_project.Led.Physical", "State"); 1856d500549bSAnthony Wilson } 1857d500549bSAnthony Wilson 1858d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData exit"; 1859d500549bSAnthony Wilson } 1860d500549bSAnthony Wilson 1861d500549bSAnthony Wilson /** 1862d500549bSAnthony Wilson * @brief Gets LED data for LEDs associated with given inventory items. 1863d500549bSAnthony Wilson * 1864d500549bSAnthony Wilson * Gets the D-Bus connections (services) that provide LED data for the LEDs 1865d500549bSAnthony Wilson * associated with the specified inventory items. Then gets the LED data from 1866d500549bSAnthony Wilson * each connection and stores it in the inventory item. 1867d500549bSAnthony Wilson * 1868d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 1869d500549bSAnthony Wilson * response. 1870d500549bSAnthony Wilson * 1871d500549bSAnthony Wilson * Finds the LED data asynchronously. Invokes callback when information has 1872d500549bSAnthony Wilson * been obtained. 1873d500549bSAnthony Wilson * 1874d500549bSAnthony Wilson * The callback must have the following signature: 1875d500549bSAnthony Wilson * @code 187642cbe538SGunnar Mills * callback() 1877d500549bSAnthony Wilson * @endcode 1878d500549bSAnthony Wilson * 1879d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 1880d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1881d500549bSAnthony Wilson * @param callback Callback to invoke when inventory items have been obtained. 1882d500549bSAnthony Wilson */ 1883d500549bSAnthony Wilson template <typename Callback> 1884d500549bSAnthony Wilson void getInventoryLeds( 1885d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1886d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1887d500549bSAnthony Wilson Callback&& callback) 1888d500549bSAnthony Wilson { 1889d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds enter"; 1890d500549bSAnthony Wilson 1891d500549bSAnthony Wilson const std::string path = "/xyz/openbmc_project"; 1892d500549bSAnthony Wilson const std::array<std::string, 1> interfaces = { 1893d500549bSAnthony Wilson "xyz.openbmc_project.Led.Physical"}; 1894d500549bSAnthony Wilson 1895d500549bSAnthony Wilson // Response handler for parsing output from GetSubTree 1896d500549bSAnthony Wilson auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp, 1897d500549bSAnthony Wilson inventoryItems](const boost::system::error_code ec, 1898d500549bSAnthony Wilson const GetSubTreeType& subtree) { 1899d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter"; 1900d500549bSAnthony Wilson if (ec) 1901d500549bSAnthony Wilson { 1902d500549bSAnthony Wilson messages::internalError(sensorsAsyncResp->res); 1903d500549bSAnthony Wilson BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error " 1904d500549bSAnthony Wilson << ec; 1905d500549bSAnthony Wilson return; 1906d500549bSAnthony Wilson } 1907d500549bSAnthony Wilson 1908d500549bSAnthony Wilson // Build map of LED object paths to connections 1909d500549bSAnthony Wilson std::shared_ptr<boost::container::flat_map<std::string, std::string>> 1910d500549bSAnthony Wilson ledConnections = std::make_shared< 1911d500549bSAnthony Wilson boost::container::flat_map<std::string, std::string>>(); 1912d500549bSAnthony Wilson 1913d500549bSAnthony Wilson // Loop through objects from GetSubTree 1914d500549bSAnthony Wilson for (const std::pair< 1915d500549bSAnthony Wilson std::string, 1916d500549bSAnthony Wilson std::vector<std::pair<std::string, std::vector<std::string>>>>& 1917d500549bSAnthony Wilson object : subtree) 1918d500549bSAnthony Wilson { 1919d500549bSAnthony Wilson // Check if object path is LED for one of the specified inventory 1920d500549bSAnthony Wilson // items 1921d500549bSAnthony Wilson const std::string& ledPath = object.first; 1922d500549bSAnthony Wilson if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr) 1923d500549bSAnthony Wilson { 1924d500549bSAnthony Wilson // Add mapping from ledPath to connection 1925d500549bSAnthony Wilson const std::string& connection = object.second.begin()->first; 1926d500549bSAnthony Wilson (*ledConnections)[ledPath] = connection; 1927d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> " 1928d500549bSAnthony Wilson << connection; 1929d500549bSAnthony Wilson } 1930d500549bSAnthony Wilson } 1931d500549bSAnthony Wilson 1932d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections, 1933d500549bSAnthony Wilson std::move(callback)); 1934d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit"; 1935d500549bSAnthony Wilson }; 1936d500549bSAnthony Wilson // Make call to ObjectMapper to find all inventory items 1937d500549bSAnthony Wilson crow::connections::systemBus->async_method_call( 1938d500549bSAnthony Wilson std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 1939d500549bSAnthony Wilson "/xyz/openbmc_project/object_mapper", 1940d500549bSAnthony Wilson "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); 1941d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds exit"; 1942d500549bSAnthony Wilson } 1943d500549bSAnthony Wilson 1944d500549bSAnthony Wilson /** 194542cbe538SGunnar Mills * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent 194642cbe538SGunnar Mills * 194742cbe538SGunnar Mills * Uses the specified connections (services) (currently assumes just one) to 194842cbe538SGunnar Mills * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in 194942cbe538SGunnar Mills * the inventoryItems vector. Only stores data in Power Supply inventoryItems. 195042cbe538SGunnar Mills * 195142cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 195242cbe538SGunnar Mills * response. 195342cbe538SGunnar Mills * 195442cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 195542cbe538SGunnar Mills * when data has been obtained. 195642cbe538SGunnar Mills * 195742cbe538SGunnar Mills * The callback must have the following signature: 195842cbe538SGunnar Mills * @code 195942cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 196042cbe538SGunnar Mills * @endcode 196142cbe538SGunnar Mills * 196242cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 196342cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 196442cbe538SGunnar Mills * @param psAttributesConnections Connections that provide data for the Power 196542cbe538SGunnar Mills * Supply Attributes 196642cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 196742cbe538SGunnar Mills */ 196842cbe538SGunnar Mills template <typename Callback> 196942cbe538SGunnar Mills void getPowerSupplyAttributesData( 197042cbe538SGunnar Mills std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 197142cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 197242cbe538SGunnar Mills const boost::container::flat_map<std::string, std::string>& 197342cbe538SGunnar Mills psAttributesConnections, 197442cbe538SGunnar Mills Callback&& callback) 197542cbe538SGunnar Mills { 197642cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter"; 197742cbe538SGunnar Mills 197842cbe538SGunnar Mills if (psAttributesConnections.empty()) 197942cbe538SGunnar Mills { 198042cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!"; 198142cbe538SGunnar Mills callback(inventoryItems); 198242cbe538SGunnar Mills return; 198342cbe538SGunnar Mills } 198442cbe538SGunnar Mills 198542cbe538SGunnar Mills // Assuming just one connection (service) for now 198642cbe538SGunnar Mills auto it = psAttributesConnections.nth(0); 198742cbe538SGunnar Mills 198842cbe538SGunnar Mills const std::string& psAttributesPath = (*it).first; 198942cbe538SGunnar Mills const std::string& psAttributesConnection = (*it).second; 199042cbe538SGunnar Mills 199142cbe538SGunnar Mills // Response handler for Get DeratingFactor property 199242cbe538SGunnar Mills auto respHandler = [sensorsAsyncResp, inventoryItems, 199342cbe538SGunnar Mills callback{std::move(callback)}]( 199442cbe538SGunnar Mills const boost::system::error_code ec, 199542cbe538SGunnar Mills const std::variant<uint32_t>& deratingFactor) { 199642cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter"; 199742cbe538SGunnar Mills if (ec) 199842cbe538SGunnar Mills { 199942cbe538SGunnar Mills BMCWEB_LOG_ERROR 200042cbe538SGunnar Mills << "getPowerSupplyAttributesData respHandler DBus error " << ec; 200142cbe538SGunnar Mills messages::internalError(sensorsAsyncResp->res); 200242cbe538SGunnar Mills return; 200342cbe538SGunnar Mills } 200442cbe538SGunnar Mills 200542cbe538SGunnar Mills const uint32_t* value = std::get_if<uint32_t>(&deratingFactor); 200642cbe538SGunnar Mills if (value != nullptr) 200742cbe538SGunnar Mills { 200842cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << *value; 200942cbe538SGunnar Mills // Store value in Power Supply Inventory Items 201042cbe538SGunnar Mills for (InventoryItem& inventoryItem : *inventoryItems) 201142cbe538SGunnar Mills { 201242cbe538SGunnar Mills if (inventoryItem.isPowerSupply == true) 201342cbe538SGunnar Mills { 201442cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent = 201542cbe538SGunnar Mills static_cast<int>(*value); 201642cbe538SGunnar Mills } 201742cbe538SGunnar Mills } 201842cbe538SGunnar Mills } 201942cbe538SGunnar Mills else 202042cbe538SGunnar Mills { 202142cbe538SGunnar Mills BMCWEB_LOG_DEBUG 202242cbe538SGunnar Mills << "Failed to find EfficiencyPercent value for PowerSupplies"; 202342cbe538SGunnar Mills } 202442cbe538SGunnar Mills 202542cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit"; 202642cbe538SGunnar Mills callback(inventoryItems); 202742cbe538SGunnar Mills }; 202842cbe538SGunnar Mills 202942cbe538SGunnar Mills // Get the DeratingFactor property for the PowerSupplyAttributes 203042cbe538SGunnar Mills // Currently only property on the interface/only one we care about 203142cbe538SGunnar Mills crow::connections::systemBus->async_method_call( 203242cbe538SGunnar Mills std::move(respHandler), psAttributesConnection, psAttributesPath, 203342cbe538SGunnar Mills "org.freedesktop.DBus.Properties", "Get", 203442cbe538SGunnar Mills "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor"); 203542cbe538SGunnar Mills 203642cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit"; 203742cbe538SGunnar Mills } 203842cbe538SGunnar Mills 203942cbe538SGunnar Mills /** 204042cbe538SGunnar Mills * @brief Gets the Power Supply Attributes such as EfficiencyPercent 204142cbe538SGunnar Mills * 204242cbe538SGunnar Mills * Gets the D-Bus connection (service) that provides Power Supply Attributes 204342cbe538SGunnar Mills * data. Then gets the Power Supply Attributes data from the connection 204442cbe538SGunnar Mills * (currently just assumes 1 connection) and stores the data in the inventory 204542cbe538SGunnar Mills * item. 204642cbe538SGunnar Mills * 204742cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 204842cbe538SGunnar Mills * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish. 204942cbe538SGunnar Mills * 205042cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 205142cbe538SGunnar Mills * when information has been obtained. 205242cbe538SGunnar Mills * 205342cbe538SGunnar Mills * The callback must have the following signature: 205442cbe538SGunnar Mills * @code 205542cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 205642cbe538SGunnar Mills * @endcode 205742cbe538SGunnar Mills * 205842cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 205942cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 206042cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 206142cbe538SGunnar Mills */ 206242cbe538SGunnar Mills template <typename Callback> 206342cbe538SGunnar Mills void getPowerSupplyAttributes( 206442cbe538SGunnar Mills std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 206542cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 206642cbe538SGunnar Mills Callback&& callback) 206742cbe538SGunnar Mills { 206842cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter"; 206942cbe538SGunnar Mills 207042cbe538SGunnar Mills // Only need the power supply attributes when the Power Schema 207142cbe538SGunnar Mills if (sensorsAsyncResp->chassisSubNode != "Power") 207242cbe538SGunnar Mills { 207342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power"; 207442cbe538SGunnar Mills callback(inventoryItems); 207542cbe538SGunnar Mills return; 207642cbe538SGunnar Mills } 207742cbe538SGunnar Mills 207842cbe538SGunnar Mills const std::array<std::string, 1> interfaces = { 207942cbe538SGunnar Mills "xyz.openbmc_project.Control.PowerSupplyAttributes"}; 208042cbe538SGunnar Mills 208142cbe538SGunnar Mills // Response handler for parsing output from GetSubTree 208242cbe538SGunnar Mills auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp, 208342cbe538SGunnar Mills inventoryItems](const boost::system::error_code ec, 208442cbe538SGunnar Mills const GetSubTreeType& subtree) { 208542cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter"; 208642cbe538SGunnar Mills if (ec) 208742cbe538SGunnar Mills { 208842cbe538SGunnar Mills messages::internalError(sensorsAsyncResp->res); 208942cbe538SGunnar Mills BMCWEB_LOG_ERROR 209042cbe538SGunnar Mills << "getPowerSupplyAttributes respHandler DBus error " << ec; 209142cbe538SGunnar Mills return; 209242cbe538SGunnar Mills } 209342cbe538SGunnar Mills if (subtree.size() == 0) 209442cbe538SGunnar Mills { 209542cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!"; 209642cbe538SGunnar Mills callback(inventoryItems); 209742cbe538SGunnar Mills return; 209842cbe538SGunnar Mills } 209942cbe538SGunnar Mills 210042cbe538SGunnar Mills // Currently we only support 1 power supply attribute, use this for 210142cbe538SGunnar Mills // all the power supplies. Build map of object path to connection. 210242cbe538SGunnar Mills // Assume just 1 connection and 1 path for now. 210342cbe538SGunnar Mills boost::container::flat_map<std::string, std::string> 210442cbe538SGunnar Mills psAttributesConnections; 210542cbe538SGunnar Mills 210642cbe538SGunnar Mills if (subtree[0].first.empty() || subtree[0].second.empty()) 210742cbe538SGunnar Mills { 210842cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!"; 210942cbe538SGunnar Mills callback(inventoryItems); 211042cbe538SGunnar Mills return; 211142cbe538SGunnar Mills } 211242cbe538SGunnar Mills 211342cbe538SGunnar Mills const std::string& psAttributesPath = subtree[0].first; 211442cbe538SGunnar Mills const std::string& connection = subtree[0].second.begin()->first; 211542cbe538SGunnar Mills 211642cbe538SGunnar Mills if (connection.empty()) 211742cbe538SGunnar Mills { 211842cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!"; 211942cbe538SGunnar Mills callback(inventoryItems); 212042cbe538SGunnar Mills return; 212142cbe538SGunnar Mills } 212242cbe538SGunnar Mills 212342cbe538SGunnar Mills psAttributesConnections[psAttributesPath] = connection; 212442cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> " 212542cbe538SGunnar Mills << connection; 212642cbe538SGunnar Mills 212742cbe538SGunnar Mills getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems, 212842cbe538SGunnar Mills psAttributesConnections, 212942cbe538SGunnar Mills std::move(callback)); 213042cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit"; 213142cbe538SGunnar Mills }; 213242cbe538SGunnar Mills // Make call to ObjectMapper to find the PowerSupplyAttributes service 213342cbe538SGunnar Mills crow::connections::systemBus->async_method_call( 213442cbe538SGunnar Mills std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 213542cbe538SGunnar Mills "/xyz/openbmc_project/object_mapper", 213642cbe538SGunnar Mills "xyz.openbmc_project.ObjectMapper", "GetSubTree", 213742cbe538SGunnar Mills "/xyz/openbmc_project", 0, interfaces); 213842cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit"; 213942cbe538SGunnar Mills } 214042cbe538SGunnar Mills 214142cbe538SGunnar Mills /** 2142adc4f0dbSShawn McCarney * @brief Gets inventory items associated with sensors. 21438fb49dd6SShawn McCarney * 21448fb49dd6SShawn McCarney * Finds the inventory items that are associated with the specified sensors. 2145adc4f0dbSShawn McCarney * Then gets D-Bus data for the inventory items, such as presence and VPD. 21468fb49dd6SShawn McCarney * 2147adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 2148adc4f0dbSShawn McCarney * response. 21498fb49dd6SShawn McCarney * 2150adc4f0dbSShawn McCarney * Finds the inventory items asynchronously. Invokes callback when the 2151adc4f0dbSShawn McCarney * inventory items have been obtained. 2152adc4f0dbSShawn McCarney * 2153adc4f0dbSShawn McCarney * The callback must have the following signature: 2154adc4f0dbSShawn McCarney * @code 2155adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 2156adc4f0dbSShawn McCarney * @endcode 21578fb49dd6SShawn McCarney * 21588fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 21598fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 21608fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 21618fb49dd6SShawn McCarney * implements ObjectManager. 2162adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 21638fb49dd6SShawn McCarney */ 2164adc4f0dbSShawn McCarney template <typename Callback> 2165adc4f0dbSShawn McCarney static void getInventoryItems( 21668fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 21678fb49dd6SShawn McCarney const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames, 21688fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 2169adc4f0dbSShawn McCarney objectMgrPaths, 2170adc4f0dbSShawn McCarney Callback&& callback) 21718fb49dd6SShawn McCarney { 2172adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItems enter"; 2173adc4f0dbSShawn McCarney auto getInventoryItemAssociationsCb = 2174adc4f0dbSShawn McCarney [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}]( 2175adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems) { 2176adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter"; 21778fb49dd6SShawn McCarney auto getInventoryItemsConnectionsCb = 2178adc4f0dbSShawn McCarney [sensorsAsyncResp, inventoryItems, objectMgrPaths, 2179adc4f0dbSShawn McCarney callback{std::move(callback)}]( 21808fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_set<std::string>> 21818fb49dd6SShawn McCarney invConnections) { 21828fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter"; 2183d500549bSAnthony Wilson auto getInventoryItemsDataCb = 2184d500549bSAnthony Wilson [sensorsAsyncResp, inventoryItems, 2185d500549bSAnthony Wilson callback{std::move(callback)}]() { 2186d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter"; 218742cbe538SGunnar Mills 218842cbe538SGunnar Mills auto getInventoryLedsCb = [sensorsAsyncResp, 218942cbe538SGunnar Mills inventoryItems, 219042cbe538SGunnar Mills callback{std::move( 219142cbe538SGunnar Mills callback)}]() { 219242cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter"; 219342cbe538SGunnar Mills // Find Power Supply Attributes and get the data 219442cbe538SGunnar Mills getPowerSupplyAttributes(sensorsAsyncResp, 219542cbe538SGunnar Mills inventoryItems, 219642cbe538SGunnar Mills std::move(callback)); 219742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit"; 219842cbe538SGunnar Mills }; 219942cbe538SGunnar Mills 2200d500549bSAnthony Wilson // Find led connections and get the data 2201d500549bSAnthony Wilson getInventoryLeds(sensorsAsyncResp, inventoryItems, 220242cbe538SGunnar Mills std::move(getInventoryLedsCb)); 2203d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit"; 2204d500549bSAnthony Wilson }; 22058fb49dd6SShawn McCarney 2206adc4f0dbSShawn McCarney // Get inventory item data from connections 2207adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 2208adc4f0dbSShawn McCarney invConnections, objectMgrPaths, 2209d500549bSAnthony Wilson std::move(getInventoryItemsDataCb)); 22108fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit"; 22118fb49dd6SShawn McCarney }; 22128fb49dd6SShawn McCarney 2213adc4f0dbSShawn McCarney // Get connections that provide inventory item data 22148fb49dd6SShawn McCarney getInventoryItemsConnections( 2215adc4f0dbSShawn McCarney sensorsAsyncResp, inventoryItems, 22168fb49dd6SShawn McCarney std::move(getInventoryItemsConnectionsCb)); 2217adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit"; 22188fb49dd6SShawn McCarney }; 22198fb49dd6SShawn McCarney 2220adc4f0dbSShawn McCarney // Get associations from sensors to inventory items 2221adc4f0dbSShawn McCarney getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths, 2222adc4f0dbSShawn McCarney std::move(getInventoryItemAssociationsCb)); 2223adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItems exit"; 2224adc4f0dbSShawn McCarney } 2225adc4f0dbSShawn McCarney 2226adc4f0dbSShawn McCarney /** 2227adc4f0dbSShawn McCarney * @brief Returns JSON PowerSupply object for the specified inventory item. 2228adc4f0dbSShawn McCarney * 2229adc4f0dbSShawn McCarney * Searches for a JSON PowerSupply object that matches the specified inventory 2230adc4f0dbSShawn McCarney * item. If one is not found, a new PowerSupply object is added to the JSON 2231adc4f0dbSShawn McCarney * array. 2232adc4f0dbSShawn McCarney * 2233adc4f0dbSShawn McCarney * Multiple sensors are often associated with one power supply inventory item. 2234adc4f0dbSShawn McCarney * As a result, multiple sensor values are stored in one JSON PowerSupply 2235adc4f0dbSShawn McCarney * object. 2236adc4f0dbSShawn McCarney * 2237adc4f0dbSShawn McCarney * @param powerSupplyArray JSON array containing Redfish PowerSupply objects. 2238adc4f0dbSShawn McCarney * @param inventoryItem Inventory item for the power supply. 2239adc4f0dbSShawn McCarney * @param chassisId Chassis that contains the power supply. 2240adc4f0dbSShawn McCarney * @return JSON PowerSupply object for the specified inventory item. 2241adc4f0dbSShawn McCarney */ 2242adc4f0dbSShawn McCarney static nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray, 2243adc4f0dbSShawn McCarney const InventoryItem& inventoryItem, 2244adc4f0dbSShawn McCarney const std::string& chassisId) 2245adc4f0dbSShawn McCarney { 2246adc4f0dbSShawn McCarney // Check if matching PowerSupply object already exists in JSON array 2247adc4f0dbSShawn McCarney for (nlohmann::json& powerSupply : powerSupplyArray) 2248adc4f0dbSShawn McCarney { 2249adc4f0dbSShawn McCarney if (powerSupply["MemberId"] == inventoryItem.name) 2250adc4f0dbSShawn McCarney { 2251adc4f0dbSShawn McCarney return powerSupply; 2252adc4f0dbSShawn McCarney } 2253adc4f0dbSShawn McCarney } 2254adc4f0dbSShawn McCarney 2255adc4f0dbSShawn McCarney // Add new PowerSupply object to JSON array 2256adc4f0dbSShawn McCarney powerSupplyArray.push_back({}); 2257adc4f0dbSShawn McCarney nlohmann::json& powerSupply = powerSupplyArray.back(); 2258adc4f0dbSShawn McCarney powerSupply["@odata.id"] = 2259adc4f0dbSShawn McCarney "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/"; 2260adc4f0dbSShawn McCarney powerSupply["MemberId"] = inventoryItem.name; 2261adc4f0dbSShawn McCarney powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " "); 2262adc4f0dbSShawn McCarney powerSupply["Manufacturer"] = inventoryItem.manufacturer; 2263adc4f0dbSShawn McCarney powerSupply["Model"] = inventoryItem.model; 2264adc4f0dbSShawn McCarney powerSupply["PartNumber"] = inventoryItem.partNumber; 2265adc4f0dbSShawn McCarney powerSupply["SerialNumber"] = inventoryItem.serialNumber; 2266d500549bSAnthony Wilson setLedState(powerSupply, &inventoryItem); 2267adc4f0dbSShawn McCarney 226842cbe538SGunnar Mills if (inventoryItem.powerSupplyEfficiencyPercent >= 0) 226942cbe538SGunnar Mills { 227042cbe538SGunnar Mills powerSupply["EfficiencyPercent"] = 227142cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent; 227242cbe538SGunnar Mills } 227342cbe538SGunnar Mills 227442cbe538SGunnar Mills powerSupply["Status"]["State"] = getState(&inventoryItem); 2275adc4f0dbSShawn McCarney const char* health = inventoryItem.isFunctional ? "OK" : "Critical"; 2276adc4f0dbSShawn McCarney powerSupply["Status"]["Health"] = health; 2277adc4f0dbSShawn McCarney 2278adc4f0dbSShawn McCarney return powerSupply; 22798fb49dd6SShawn McCarney } 22808fb49dd6SShawn McCarney 22818fb49dd6SShawn McCarney /** 2282de629b6eSShawn McCarney * @brief Gets the values of the specified sensors. 2283de629b6eSShawn McCarney * 2284de629b6eSShawn McCarney * Stores the results as JSON in the SensorsAsyncResp. 2285de629b6eSShawn McCarney * 2286de629b6eSShawn McCarney * Gets the sensor values asynchronously. Stores the results later when the 2287de629b6eSShawn McCarney * information has been obtained. 2288de629b6eSShawn McCarney * 2289adc4f0dbSShawn McCarney * The sensorNames set contains all requested sensors for the current chassis. 2290de629b6eSShawn McCarney * 2291de629b6eSShawn McCarney * To minimize the number of DBus calls, the DBus method 2292de629b6eSShawn McCarney * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the 2293de629b6eSShawn McCarney * values of all sensors provided by a connection (service). 2294de629b6eSShawn McCarney * 2295de629b6eSShawn McCarney * The connections set contains all the connections that provide sensor values. 2296de629b6eSShawn McCarney * 2297de629b6eSShawn McCarney * The objectMgrPaths map contains mappings from a connection name to the 2298de629b6eSShawn McCarney * corresponding DBus object path that implements ObjectManager. 2299de629b6eSShawn McCarney * 2300adc4f0dbSShawn McCarney * The InventoryItem vector contains D-Bus inventory items associated with the 2301adc4f0dbSShawn McCarney * sensors. Inventory item data is needed for some Redfish sensor properties. 2302adc4f0dbSShawn McCarney * 2303de629b6eSShawn McCarney * @param SensorsAsyncResp Pointer to object holding response data. 2304adc4f0dbSShawn McCarney * @param sensorNames All requested sensors within the current chassis. 2305de629b6eSShawn McCarney * @param connections Connections that provide sensor values. 2306de629b6eSShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 2307de629b6eSShawn McCarney * implements ObjectManager. 2308adc4f0dbSShawn McCarney * @param inventoryItems Inventory items associated with the sensors. 2309de629b6eSShawn McCarney */ 2310de629b6eSShawn McCarney void getSensorData( 2311de629b6eSShawn McCarney std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp, 231249c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames, 2313de629b6eSShawn McCarney const boost::container::flat_set<std::string>& connections, 23148fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 2315adc4f0dbSShawn McCarney objectMgrPaths, 2316adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 2317de629b6eSShawn McCarney { 2318de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getSensorData enter"; 2319de629b6eSShawn McCarney // Get managed objects from all services exposing sensors 2320de629b6eSShawn McCarney for (const std::string& connection : connections) 2321de629b6eSShawn McCarney { 2322de629b6eSShawn McCarney // Response handler to process managed objects 23238fb49dd6SShawn McCarney auto getManagedObjectsCb = [SensorsAsyncResp, sensorNames, 2324adc4f0dbSShawn McCarney inventoryItems]( 2325de629b6eSShawn McCarney const boost::system::error_code ec, 2326de629b6eSShawn McCarney ManagedObjectsVectorType& resp) { 2327de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter"; 2328de629b6eSShawn McCarney if (ec) 2329de629b6eSShawn McCarney { 2330de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec; 2331de629b6eSShawn McCarney messages::internalError(SensorsAsyncResp->res); 2332de629b6eSShawn McCarney return; 2333de629b6eSShawn McCarney } 2334de629b6eSShawn McCarney // Go through all objects and update response with sensor data 2335de629b6eSShawn McCarney for (const auto& objDictEntry : resp) 2336de629b6eSShawn McCarney { 2337de629b6eSShawn McCarney const std::string& objPath = 2338de629b6eSShawn McCarney static_cast<const std::string&>(objDictEntry.first); 2339de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object " 2340de629b6eSShawn McCarney << objPath; 2341de629b6eSShawn McCarney 2342de629b6eSShawn McCarney std::vector<std::string> split; 2343de629b6eSShawn McCarney // Reserve space for 2344de629b6eSShawn McCarney // /xyz/openbmc_project/sensors/<name>/<subname> 2345de629b6eSShawn McCarney split.reserve(6); 2346de629b6eSShawn McCarney boost::algorithm::split(split, objPath, boost::is_any_of("/")); 2347de629b6eSShawn McCarney if (split.size() < 6) 2348de629b6eSShawn McCarney { 2349de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "Got path that isn't long enough " 2350de629b6eSShawn McCarney << objPath; 2351de629b6eSShawn McCarney continue; 2352de629b6eSShawn McCarney } 2353de629b6eSShawn McCarney // These indexes aren't intuitive, as boost::split puts an empty 2354de629b6eSShawn McCarney // string at the beginning 2355de629b6eSShawn McCarney const std::string& sensorType = split[4]; 2356de629b6eSShawn McCarney const std::string& sensorName = split[5]; 2357de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "sensorName " << sensorName 2358de629b6eSShawn McCarney << " sensorType " << sensorType; 235949c53ac9SJohnathan Mantey if (sensorNames->find(objPath) == sensorNames->end()) 2360de629b6eSShawn McCarney { 2361de629b6eSShawn McCarney BMCWEB_LOG_ERROR << sensorName << " not in sensor list "; 2362de629b6eSShawn McCarney continue; 2363de629b6eSShawn McCarney } 2364de629b6eSShawn McCarney 2365adc4f0dbSShawn McCarney // Find inventory item (if any) associated with sensor 2366adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 2367adc4f0dbSShawn McCarney findInventoryItemForSensor(inventoryItems, objPath); 2368adc4f0dbSShawn McCarney 236995a3ecadSAnthony Wilson const std::string& sensorSchema = 237095a3ecadSAnthony Wilson SensorsAsyncResp->chassisSubNode; 237195a3ecadSAnthony Wilson 237295a3ecadSAnthony Wilson nlohmann::json* sensorJson = nullptr; 237395a3ecadSAnthony Wilson 237495a3ecadSAnthony Wilson if (sensorSchema == "Sensors") 237595a3ecadSAnthony Wilson { 237695a3ecadSAnthony Wilson SensorsAsyncResp->res.jsonValue["@odata.id"] = 237795a3ecadSAnthony Wilson "/redfish/v1/Chassis/" + SensorsAsyncResp->chassisId + 237895a3ecadSAnthony Wilson "/" + SensorsAsyncResp->chassisSubNode + "/" + 237995a3ecadSAnthony Wilson sensorName; 238095a3ecadSAnthony Wilson sensorJson = &(SensorsAsyncResp->res.jsonValue); 238195a3ecadSAnthony Wilson } 238295a3ecadSAnthony Wilson else 238395a3ecadSAnthony Wilson { 2384271584abSEd Tanous std::string fieldName; 2385de629b6eSShawn McCarney if (sensorType == "temperature") 2386de629b6eSShawn McCarney { 2387de629b6eSShawn McCarney fieldName = "Temperatures"; 2388de629b6eSShawn McCarney } 2389de629b6eSShawn McCarney else if (sensorType == "fan" || sensorType == "fan_tach" || 2390de629b6eSShawn McCarney sensorType == "fan_pwm") 2391de629b6eSShawn McCarney { 2392de629b6eSShawn McCarney fieldName = "Fans"; 2393de629b6eSShawn McCarney } 2394de629b6eSShawn McCarney else if (sensorType == "voltage") 2395de629b6eSShawn McCarney { 2396de629b6eSShawn McCarney fieldName = "Voltages"; 2397de629b6eSShawn McCarney } 2398de629b6eSShawn McCarney else if (sensorType == "power") 2399de629b6eSShawn McCarney { 2400028f7ebcSEddie James if (!sensorName.compare("total_power")) 2401028f7ebcSEddie James { 2402028f7ebcSEddie James fieldName = "PowerControl"; 2403028f7ebcSEddie James } 2404adc4f0dbSShawn McCarney else if ((inventoryItem != nullptr) && 2405adc4f0dbSShawn McCarney (inventoryItem->isPowerSupply)) 2406028f7ebcSEddie James { 2407de629b6eSShawn McCarney fieldName = "PowerSupplies"; 2408de629b6eSShawn McCarney } 2409adc4f0dbSShawn McCarney else 2410adc4f0dbSShawn McCarney { 2411adc4f0dbSShawn McCarney // Other power sensors are in SensorCollection 2412adc4f0dbSShawn McCarney continue; 2413adc4f0dbSShawn McCarney } 2414028f7ebcSEddie James } 2415de629b6eSShawn McCarney else 2416de629b6eSShawn McCarney { 2417de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "Unsure how to handle sensorType " 2418de629b6eSShawn McCarney << sensorType; 2419de629b6eSShawn McCarney continue; 2420de629b6eSShawn McCarney } 2421de629b6eSShawn McCarney 2422de629b6eSShawn McCarney nlohmann::json& tempArray = 2423de629b6eSShawn McCarney SensorsAsyncResp->res.jsonValue[fieldName]; 2424adc4f0dbSShawn McCarney if (fieldName == "PowerControl") 242549c53ac9SJohnathan Mantey { 2426adc4f0dbSShawn McCarney if (tempArray.empty()) 24277ab06f49SGunnar Mills { 242895a3ecadSAnthony Wilson // Put multiple "sensors" into a single 242995a3ecadSAnthony Wilson // PowerControl. Follows MemberId naming and 243095a3ecadSAnthony Wilson // naming in power.hpp. 24317ab06f49SGunnar Mills tempArray.push_back( 2432adc4f0dbSShawn McCarney {{"@odata.id", 2433adc4f0dbSShawn McCarney "/redfish/v1/Chassis/" + 24347ab06f49SGunnar Mills SensorsAsyncResp->chassisId + "/" + 2435adc4f0dbSShawn McCarney SensorsAsyncResp->chassisSubNode + "#/" + 2436adc4f0dbSShawn McCarney fieldName + "/0"}}); 2437adc4f0dbSShawn McCarney } 2438adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 2439adc4f0dbSShawn McCarney } 2440adc4f0dbSShawn McCarney else if (fieldName == "PowerSupplies") 2441adc4f0dbSShawn McCarney { 2442adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 2443adc4f0dbSShawn McCarney { 2444adc4f0dbSShawn McCarney sensorJson = 2445adc4f0dbSShawn McCarney &(getPowerSupply(tempArray, *inventoryItem, 2446adc4f0dbSShawn McCarney SensorsAsyncResp->chassisId)); 2447adc4f0dbSShawn McCarney } 244849c53ac9SJohnathan Mantey } 244949c53ac9SJohnathan Mantey else 245049c53ac9SJohnathan Mantey { 2451de629b6eSShawn McCarney tempArray.push_back( 245295a3ecadSAnthony Wilson {{"@odata.id", 245395a3ecadSAnthony Wilson "/redfish/v1/Chassis/" + 245449c53ac9SJohnathan Mantey SensorsAsyncResp->chassisId + "/" + 245595a3ecadSAnthony Wilson SensorsAsyncResp->chassisSubNode + "#/" + 245695a3ecadSAnthony Wilson fieldName + "/"}}); 2457adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 245849c53ac9SJohnathan Mantey } 245995a3ecadSAnthony Wilson } 2460de629b6eSShawn McCarney 2461adc4f0dbSShawn McCarney if (sensorJson != nullptr) 2462adc4f0dbSShawn McCarney { 2463de629b6eSShawn McCarney objectInterfacesToJson(sensorName, sensorType, 246495a3ecadSAnthony Wilson SensorsAsyncResp->chassisSubNode, 2465adc4f0dbSShawn McCarney objDictEntry.second, *sensorJson, 2466adc4f0dbSShawn McCarney inventoryItem); 2467adc4f0dbSShawn McCarney } 2468de629b6eSShawn McCarney } 246949c53ac9SJohnathan Mantey if (SensorsAsyncResp.use_count() == 1) 247049c53ac9SJohnathan Mantey { 247149c53ac9SJohnathan Mantey sortJSONResponse(SensorsAsyncResp); 247249c53ac9SJohnathan Mantey if (SensorsAsyncResp->chassisSubNode == "Thermal") 24738bd25ccdSJames Feist { 24748bd25ccdSJames Feist populateFanRedundancy(SensorsAsyncResp); 24758bd25ccdSJames Feist } 247649c53ac9SJohnathan Mantey } 2477de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit"; 2478de629b6eSShawn McCarney }; 2479de629b6eSShawn McCarney 2480de629b6eSShawn McCarney // Find DBus object path that implements ObjectManager for the current 2481de629b6eSShawn McCarney // connection. If no mapping found, default to "/". 24828fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(connection); 2483de629b6eSShawn McCarney const std::string& objectMgrPath = 24848fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 2485de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is " 2486de629b6eSShawn McCarney << objectMgrPath; 2487de629b6eSShawn McCarney 2488de629b6eSShawn McCarney crow::connections::systemBus->async_method_call( 2489de629b6eSShawn McCarney getManagedObjectsCb, connection, objectMgrPath, 2490de629b6eSShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 2491de629b6eSShawn McCarney }; 2492de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getSensorData exit"; 2493de629b6eSShawn McCarney } 2494de629b6eSShawn McCarney 249595a3ecadSAnthony Wilson void processSensorList( 249695a3ecadSAnthony Wilson std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp, 249795a3ecadSAnthony Wilson std::shared_ptr<boost::container::flat_set<std::string>> sensorNames) 24981abe55efSEd Tanous { 249995a3ecadSAnthony Wilson auto getConnectionCb = 250095a3ecadSAnthony Wilson [SensorsAsyncResp, sensorNames]( 250195a3ecadSAnthony Wilson const boost::container::flat_set<std::string>& connections) { 250255c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getConnectionCb enter"; 2503de629b6eSShawn McCarney auto getObjectManagerPathsCb = 250449c53ac9SJohnathan Mantey [SensorsAsyncResp, sensorNames, connections]( 250595a3ecadSAnthony Wilson std::shared_ptr< 250695a3ecadSAnthony Wilson boost::container::flat_map<std::string, std::string>> 25078fb49dd6SShawn McCarney objectMgrPaths) { 2508de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter"; 2509adc4f0dbSShawn McCarney auto getInventoryItemsCb = 2510adc4f0dbSShawn McCarney [SensorsAsyncResp, sensorNames, connections, 2511adc4f0dbSShawn McCarney objectMgrPaths]( 2512adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> 2513adc4f0dbSShawn McCarney inventoryItems) { 2514adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter"; 251549c53ac9SJohnathan Mantey // Get sensor data and store results in JSON 2516de629b6eSShawn McCarney getSensorData(SensorsAsyncResp, sensorNames, 2517adc4f0dbSShawn McCarney connections, objectMgrPaths, 2518adc4f0dbSShawn McCarney inventoryItems); 2519adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit"; 2520adc4f0dbSShawn McCarney }; 2521adc4f0dbSShawn McCarney 2522adc4f0dbSShawn McCarney // Get inventory items associated with sensors 2523adc4f0dbSShawn McCarney getInventoryItems(SensorsAsyncResp, sensorNames, 2524adc4f0dbSShawn McCarney objectMgrPaths, 2525adc4f0dbSShawn McCarney std::move(getInventoryItemsCb)); 2526adc4f0dbSShawn McCarney 2527de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit"; 252808777fb0SLewanczyk, Dawid }; 2529de629b6eSShawn McCarney 253049c53ac9SJohnathan Mantey // Get mapping from connection names to the DBus object 253149c53ac9SJohnathan Mantey // paths that implement the ObjectManager interface 2532de629b6eSShawn McCarney getObjectManagerPaths(SensorsAsyncResp, 2533de629b6eSShawn McCarney std::move(getObjectManagerPathsCb)); 253455c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getConnectionCb exit"; 253508777fb0SLewanczyk, Dawid }; 2536de629b6eSShawn McCarney 2537de629b6eSShawn McCarney // Get set of connections that provide sensor values 253895a3ecadSAnthony Wilson getConnections(SensorsAsyncResp, sensorNames, std::move(getConnectionCb)); 253995a3ecadSAnthony Wilson } 254095a3ecadSAnthony Wilson 254195a3ecadSAnthony Wilson /** 254295a3ecadSAnthony Wilson * @brief Entry point for retrieving sensors data related to requested 254395a3ecadSAnthony Wilson * chassis. 254495a3ecadSAnthony Wilson * @param SensorsAsyncResp Pointer to object holding response data 254595a3ecadSAnthony Wilson */ 254695a3ecadSAnthony Wilson void getChassisData(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp) 254795a3ecadSAnthony Wilson { 254895a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisData enter"; 254995a3ecadSAnthony Wilson auto getChassisCb = 255095a3ecadSAnthony Wilson [SensorsAsyncResp]( 255195a3ecadSAnthony Wilson std::shared_ptr<boost::container::flat_set<std::string>> 255295a3ecadSAnthony Wilson sensorNames) { 255395a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb enter"; 255495a3ecadSAnthony Wilson processSensorList(SensorsAsyncResp, sensorNames); 255555c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassisCb exit"; 255608777fb0SLewanczyk, Dawid }; 25574f9a2130SJennifer Lee SensorsAsyncResp->res.jsonValue["Redundancy"] = nlohmann::json::array(); 255808777fb0SLewanczyk, Dawid 255926f03899SShawn McCarney // Get set of sensors in chassis 2560588c3f0dSKowalski, Kamil getChassis(SensorsAsyncResp, std::move(getChassisCb)); 256155c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassisData exit"; 2562271584abSEd Tanous } 256308777fb0SLewanczyk, Dawid 2564413961deSRichard Marian Thomaiyar /** 256549c53ac9SJohnathan Mantey * @brief Find the requested sensorName in the list of all sensors supplied by 256649c53ac9SJohnathan Mantey * the chassis node 256749c53ac9SJohnathan Mantey * 256849c53ac9SJohnathan Mantey * @param sensorName The sensor name supplied in the PATCH request 256949c53ac9SJohnathan Mantey * @param sensorsList The list of sensors managed by the chassis node 257049c53ac9SJohnathan Mantey * @param sensorsModified The list of sensors that were found as a result of 257149c53ac9SJohnathan Mantey * repeated calls to this function 257249c53ac9SJohnathan Mantey */ 257349c53ac9SJohnathan Mantey bool findSensorNameUsingSensorPath( 25740a86febdSRichard Marian Thomaiyar std::string_view sensorName, 257549c53ac9SJohnathan Mantey boost::container::flat_set<std::string>& sensorsList, 257649c53ac9SJohnathan Mantey boost::container::flat_set<std::string>& sensorsModified) 257749c53ac9SJohnathan Mantey { 25780a86febdSRichard Marian Thomaiyar for (std::string_view chassisSensor : sensorsList) 257949c53ac9SJohnathan Mantey { 25800a86febdSRichard Marian Thomaiyar std::size_t pos = chassisSensor.rfind("/"); 25810a86febdSRichard Marian Thomaiyar if (pos >= (chassisSensor.size() - 1)) 258249c53ac9SJohnathan Mantey { 258349c53ac9SJohnathan Mantey continue; 258449c53ac9SJohnathan Mantey } 25850a86febdSRichard Marian Thomaiyar std::string_view thisSensorName = chassisSensor.substr(pos + 1); 258649c53ac9SJohnathan Mantey if (thisSensorName == sensorName) 258749c53ac9SJohnathan Mantey { 258849c53ac9SJohnathan Mantey sensorsModified.emplace(chassisSensor); 258949c53ac9SJohnathan Mantey return true; 259049c53ac9SJohnathan Mantey } 259149c53ac9SJohnathan Mantey } 259249c53ac9SJohnathan Mantey return false; 259349c53ac9SJohnathan Mantey } 259449c53ac9SJohnathan Mantey 259549c53ac9SJohnathan Mantey /** 2596413961deSRichard Marian Thomaiyar * @brief Entry point for overriding sensor values of given sensor 2597413961deSRichard Marian Thomaiyar * 2598413961deSRichard Marian Thomaiyar * @param res response object 25994bb3dc34SCarol Wang * @param allCollections Collections extract from sensors' request patch info 2600413961deSRichard Marian Thomaiyar * @param chassisSubNode Chassis Node for which the query has to happen 2601413961deSRichard Marian Thomaiyar */ 260270d1d0aaSjayaprakash Mutyala void setSensorsOverride( 26034bb3dc34SCarol Wang std::shared_ptr<SensorsAsyncResp> sensorAsyncResp, 26044bb3dc34SCarol Wang std::unordered_map<std::string, std::vector<nlohmann::json>>& 2605*397fd61fSjayaprakash Mutyala allCollections) 2606413961deSRichard Marian Thomaiyar { 260770d1d0aaSjayaprakash Mutyala BMCWEB_LOG_INFO << "setSensorsOverride for subNode" 26084bb3dc34SCarol Wang << sensorAsyncResp->chassisSubNode << "\n"; 2609413961deSRichard Marian Thomaiyar 2610f65af9e8SRichard Marian Thomaiyar const char* propertyValueName; 2611f65af9e8SRichard Marian Thomaiyar std::unordered_map<std::string, std::pair<double, std::string>> overrideMap; 2612413961deSRichard Marian Thomaiyar std::string memberId; 2613413961deSRichard Marian Thomaiyar double value; 2614f65af9e8SRichard Marian Thomaiyar for (auto& collectionItems : allCollections) 2615f65af9e8SRichard Marian Thomaiyar { 2616f65af9e8SRichard Marian Thomaiyar if (collectionItems.first == "Temperatures") 2617f65af9e8SRichard Marian Thomaiyar { 2618f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingCelsius"; 2619f65af9e8SRichard Marian Thomaiyar } 2620f65af9e8SRichard Marian Thomaiyar else if (collectionItems.first == "Fans") 2621f65af9e8SRichard Marian Thomaiyar { 2622f65af9e8SRichard Marian Thomaiyar propertyValueName = "Reading"; 2623f65af9e8SRichard Marian Thomaiyar } 2624f65af9e8SRichard Marian Thomaiyar else 2625f65af9e8SRichard Marian Thomaiyar { 2626f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingVolts"; 2627f65af9e8SRichard Marian Thomaiyar } 2628f65af9e8SRichard Marian Thomaiyar for (auto& item : collectionItems.second) 2629f65af9e8SRichard Marian Thomaiyar { 26304bb3dc34SCarol Wang if (!json_util::readJson(item, sensorAsyncResp->res, "MemberId", 26314bb3dc34SCarol Wang memberId, propertyValueName, value)) 2632413961deSRichard Marian Thomaiyar { 2633413961deSRichard Marian Thomaiyar return; 2634413961deSRichard Marian Thomaiyar } 2635f65af9e8SRichard Marian Thomaiyar overrideMap.emplace(memberId, 2636f65af9e8SRichard Marian Thomaiyar std::make_pair(value, collectionItems.first)); 2637f65af9e8SRichard Marian Thomaiyar } 2638f65af9e8SRichard Marian Thomaiyar } 26394bb3dc34SCarol Wang 264049c53ac9SJohnathan Mantey auto getChassisSensorListCb = [sensorAsyncResp, 264149c53ac9SJohnathan Mantey overrideMap](const std::shared_ptr< 264249c53ac9SJohnathan Mantey boost::container::flat_set< 264349c53ac9SJohnathan Mantey std::string>> 264449c53ac9SJohnathan Mantey sensorsList) { 264549c53ac9SJohnathan Mantey // Match sensor names in the PATCH request to those managed by the 264649c53ac9SJohnathan Mantey // chassis node 264749c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> 264849c53ac9SJohnathan Mantey sensorNames = 264949c53ac9SJohnathan Mantey std::make_shared<boost::container::flat_set<std::string>>(); 2650f65af9e8SRichard Marian Thomaiyar for (const auto& item : overrideMap) 2651413961deSRichard Marian Thomaiyar { 2652f65af9e8SRichard Marian Thomaiyar const auto& sensor = item.first; 265349c53ac9SJohnathan Mantey if (!findSensorNameUsingSensorPath(sensor, *sensorsList, 265449c53ac9SJohnathan Mantey *sensorNames)) 2655f65af9e8SRichard Marian Thomaiyar { 2656f65af9e8SRichard Marian Thomaiyar BMCWEB_LOG_INFO << "Unable to find memberId " << item.first; 2657413961deSRichard Marian Thomaiyar messages::resourceNotFound(sensorAsyncResp->res, 2658f65af9e8SRichard Marian Thomaiyar item.second.second, item.first); 2659413961deSRichard Marian Thomaiyar return; 2660413961deSRichard Marian Thomaiyar } 2661f65af9e8SRichard Marian Thomaiyar } 2662413961deSRichard Marian Thomaiyar // Get the connection to which the memberId belongs 2663413961deSRichard Marian Thomaiyar auto getObjectsWithConnectionCb = 2664f65af9e8SRichard Marian Thomaiyar [sensorAsyncResp, overrideMap]( 2665413961deSRichard Marian Thomaiyar const boost::container::flat_set<std::string>& connections, 2666413961deSRichard Marian Thomaiyar const std::set<std::pair<std::string, std::string>>& 2667413961deSRichard Marian Thomaiyar objectsWithConnection) { 2668f65af9e8SRichard Marian Thomaiyar if (objectsWithConnection.size() != overrideMap.size()) 2669413961deSRichard Marian Thomaiyar { 2670413961deSRichard Marian Thomaiyar BMCWEB_LOG_INFO 2671f65af9e8SRichard Marian Thomaiyar << "Unable to find all objects with proper connection " 2672f65af9e8SRichard Marian Thomaiyar << objectsWithConnection.size() << " requested " 2673f65af9e8SRichard Marian Thomaiyar << overrideMap.size() << "\n"; 2674413961deSRichard Marian Thomaiyar messages::resourceNotFound( 2675413961deSRichard Marian Thomaiyar sensorAsyncResp->res, 2676413961deSRichard Marian Thomaiyar sensorAsyncResp->chassisSubNode == "Thermal" 2677413961deSRichard Marian Thomaiyar ? "Temperatures" 2678413961deSRichard Marian Thomaiyar : "Voltages", 2679f65af9e8SRichard Marian Thomaiyar "Count"); 2680f65af9e8SRichard Marian Thomaiyar return; 2681f65af9e8SRichard Marian Thomaiyar } 2682f65af9e8SRichard Marian Thomaiyar for (const auto& item : objectsWithConnection) 2683f65af9e8SRichard Marian Thomaiyar { 2684f65af9e8SRichard Marian Thomaiyar 2685f65af9e8SRichard Marian Thomaiyar auto lastPos = item.first.rfind('/'); 2686f65af9e8SRichard Marian Thomaiyar if (lastPos == std::string::npos) 2687f65af9e8SRichard Marian Thomaiyar { 2688f65af9e8SRichard Marian Thomaiyar messages::internalError(sensorAsyncResp->res); 2689f65af9e8SRichard Marian Thomaiyar return; 2690f65af9e8SRichard Marian Thomaiyar } 2691f65af9e8SRichard Marian Thomaiyar std::string sensorName = item.first.substr(lastPos + 1); 2692f65af9e8SRichard Marian Thomaiyar 2693f65af9e8SRichard Marian Thomaiyar const auto& iterator = overrideMap.find(sensorName); 2694f65af9e8SRichard Marian Thomaiyar if (iterator == overrideMap.end()) 2695f65af9e8SRichard Marian Thomaiyar { 2696f65af9e8SRichard Marian Thomaiyar BMCWEB_LOG_INFO << "Unable to find sensor object" 2697f65af9e8SRichard Marian Thomaiyar << item.first << "\n"; 2698f65af9e8SRichard Marian Thomaiyar messages::internalError(sensorAsyncResp->res); 2699413961deSRichard Marian Thomaiyar return; 2700413961deSRichard Marian Thomaiyar } 2701413961deSRichard Marian Thomaiyar crow::connections::systemBus->async_method_call( 2702f65af9e8SRichard Marian Thomaiyar [sensorAsyncResp](const boost::system::error_code ec) { 2703413961deSRichard Marian Thomaiyar if (ec) 2704413961deSRichard Marian Thomaiyar { 2705413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG 2706f65af9e8SRichard Marian Thomaiyar << "setOverrideValueStatus DBUS error: " 2707413961deSRichard Marian Thomaiyar << ec; 2708413961deSRichard Marian Thomaiyar messages::internalError(sensorAsyncResp->res); 2709413961deSRichard Marian Thomaiyar return; 2710413961deSRichard Marian Thomaiyar } 2711413961deSRichard Marian Thomaiyar }, 2712f65af9e8SRichard Marian Thomaiyar item.second, item.first, 2713413961deSRichard Marian Thomaiyar "org.freedesktop.DBus.Properties", "Set", 2714413961deSRichard Marian Thomaiyar "xyz.openbmc_project.Sensor.Value", "Value", 2715f65af9e8SRichard Marian Thomaiyar sdbusplus::message::variant<double>( 2716f65af9e8SRichard Marian Thomaiyar iterator->second.first)); 2717f65af9e8SRichard Marian Thomaiyar } 2718413961deSRichard Marian Thomaiyar }; 2719413961deSRichard Marian Thomaiyar // Get object with connection for the given sensor name 2720413961deSRichard Marian Thomaiyar getObjectsWithConnection(sensorAsyncResp, sensorNames, 2721413961deSRichard Marian Thomaiyar std::move(getObjectsWithConnectionCb)); 2722413961deSRichard Marian Thomaiyar }; 2723413961deSRichard Marian Thomaiyar // get full sensor list for the given chassisId and cross verify the sensor. 2724413961deSRichard Marian Thomaiyar getChassis(sensorAsyncResp, std::move(getChassisSensorListCb)); 2725413961deSRichard Marian Thomaiyar } 2726413961deSRichard Marian Thomaiyar 272770d1d0aaSjayaprakash Mutyala bool isOverridingAllowed(const std::string& manufacturingModeStatus) 272870d1d0aaSjayaprakash Mutyala { 272970d1d0aaSjayaprakash Mutyala if (manufacturingModeStatus == 273070d1d0aaSjayaprakash Mutyala "xyz.openbmc_project.Control.Security.SpecialMode.Modes.Manufacturing") 273170d1d0aaSjayaprakash Mutyala { 273270d1d0aaSjayaprakash Mutyala return true; 273370d1d0aaSjayaprakash Mutyala } 273470d1d0aaSjayaprakash Mutyala 273570d1d0aaSjayaprakash Mutyala #ifdef BMCWEB_ENABLE_VALIDATION_UNSECURE_FEATURE 273670d1d0aaSjayaprakash Mutyala if (manufacturingModeStatus == "xyz.openbmc_project.Control.Security." 273770d1d0aaSjayaprakash Mutyala "SpecialMode.Modes.ValidationUnsecure") 273870d1d0aaSjayaprakash Mutyala { 273970d1d0aaSjayaprakash Mutyala return true; 274070d1d0aaSjayaprakash Mutyala } 274170d1d0aaSjayaprakash Mutyala 274270d1d0aaSjayaprakash Mutyala #endif 274370d1d0aaSjayaprakash Mutyala 274470d1d0aaSjayaprakash Mutyala return false; 274570d1d0aaSjayaprakash Mutyala } 274670d1d0aaSjayaprakash Mutyala 274770d1d0aaSjayaprakash Mutyala /** 274870d1d0aaSjayaprakash Mutyala * @brief Entry point for Checking the manufacturing mode before doing sensor 274970d1d0aaSjayaprakash Mutyala * override values of given sensor 275070d1d0aaSjayaprakash Mutyala * 275170d1d0aaSjayaprakash Mutyala * @param res response object 275270d1d0aaSjayaprakash Mutyala * @param allCollections Collections extract from sensors' request patch info 275370d1d0aaSjayaprakash Mutyala * @param chassisSubNode Chassis Node for which the query has to happen 275470d1d0aaSjayaprakash Mutyala */ 275570d1d0aaSjayaprakash Mutyala void checkAndDoSensorsOverride( 275670d1d0aaSjayaprakash Mutyala std::shared_ptr<SensorsAsyncResp> sensorAsyncResp, 2757*397fd61fSjayaprakash Mutyala std::unordered_map<std::string, std::vector<nlohmann::json>>& 2758*397fd61fSjayaprakash Mutyala allCollections) 275970d1d0aaSjayaprakash Mutyala { 276070d1d0aaSjayaprakash Mutyala BMCWEB_LOG_INFO << "checkAndDoSensorsOverride for subnode" 276170d1d0aaSjayaprakash Mutyala << sensorAsyncResp->chassisSubNode << "\n"; 276270d1d0aaSjayaprakash Mutyala 276370d1d0aaSjayaprakash Mutyala const std::array<std::string, 1> interfaces = { 276470d1d0aaSjayaprakash Mutyala "xyz.openbmc_project.Security.SpecialMode"}; 276570d1d0aaSjayaprakash Mutyala 276670d1d0aaSjayaprakash Mutyala crow::connections::systemBus->async_method_call( 2767*397fd61fSjayaprakash Mutyala [sensorAsyncResp, allCollections](const boost::system::error_code ec, 276870d1d0aaSjayaprakash Mutyala const GetSubTreeType& resp) mutable { 276970d1d0aaSjayaprakash Mutyala if (ec) 277070d1d0aaSjayaprakash Mutyala { 277170d1d0aaSjayaprakash Mutyala BMCWEB_LOG_DEBUG 277270d1d0aaSjayaprakash Mutyala << "Error in querying GetSubTree with Object Mapper. " 277370d1d0aaSjayaprakash Mutyala << ec; 277470d1d0aaSjayaprakash Mutyala messages::internalError(sensorAsyncResp->res); 277570d1d0aaSjayaprakash Mutyala return; 277670d1d0aaSjayaprakash Mutyala } 277770d1d0aaSjayaprakash Mutyala if (!resp.size()) 277870d1d0aaSjayaprakash Mutyala { 277970d1d0aaSjayaprakash Mutyala // Special mode manager doesn't exist, proceed with sensor 278070d1d0aaSjayaprakash Mutyala // override 2781*397fd61fSjayaprakash Mutyala setSensorsOverride(sensorAsyncResp, allCollections); 278270d1d0aaSjayaprakash Mutyala return; 278370d1d0aaSjayaprakash Mutyala } 278470d1d0aaSjayaprakash Mutyala 278570d1d0aaSjayaprakash Mutyala if (resp.size() != 1) 278670d1d0aaSjayaprakash Mutyala { 278770d1d0aaSjayaprakash Mutyala BMCWEB_LOG_DEBUG << "Queried object count mismatch. "; 278870d1d0aaSjayaprakash Mutyala messages::internalError(sensorAsyncResp->res); 278970d1d0aaSjayaprakash Mutyala return; 279070d1d0aaSjayaprakash Mutyala } 279170d1d0aaSjayaprakash Mutyala const std::string& path = resp[0].first; 279270d1d0aaSjayaprakash Mutyala const std::string& serviceName = resp[0].second.begin()->first; 279370d1d0aaSjayaprakash Mutyala 279470d1d0aaSjayaprakash Mutyala if (path.empty() || serviceName.empty()) 279570d1d0aaSjayaprakash Mutyala { 279670d1d0aaSjayaprakash Mutyala BMCWEB_LOG_DEBUG 279770d1d0aaSjayaprakash Mutyala << "Path or service name is returned as empty. "; 279870d1d0aaSjayaprakash Mutyala messages::internalError(sensorAsyncResp->res); 279970d1d0aaSjayaprakash Mutyala return; 280070d1d0aaSjayaprakash Mutyala } 280170d1d0aaSjayaprakash Mutyala 280270d1d0aaSjayaprakash Mutyala // Sensor override is allowed only in manufacturing mode or 280370d1d0aaSjayaprakash Mutyala // validation unsecure mode . 280470d1d0aaSjayaprakash Mutyala crow::connections::systemBus->async_method_call( 2805*397fd61fSjayaprakash Mutyala [sensorAsyncResp, allCollections, 280670d1d0aaSjayaprakash Mutyala path](const boost::system::error_code ec, 280770d1d0aaSjayaprakash Mutyala std::variant<std::string>& getManufactMode) mutable { 280870d1d0aaSjayaprakash Mutyala if (ec) 280970d1d0aaSjayaprakash Mutyala { 281070d1d0aaSjayaprakash Mutyala BMCWEB_LOG_DEBUG 281170d1d0aaSjayaprakash Mutyala << "Error in querying Special mode property " << ec; 281270d1d0aaSjayaprakash Mutyala messages::internalError(sensorAsyncResp->res); 281370d1d0aaSjayaprakash Mutyala return; 281470d1d0aaSjayaprakash Mutyala } 281570d1d0aaSjayaprakash Mutyala 281670d1d0aaSjayaprakash Mutyala const std::string* manufacturingModeStatus = 281770d1d0aaSjayaprakash Mutyala std::get_if<std::string>(&getManufactMode); 281870d1d0aaSjayaprakash Mutyala 281970d1d0aaSjayaprakash Mutyala if (nullptr == manufacturingModeStatus) 282070d1d0aaSjayaprakash Mutyala { 282170d1d0aaSjayaprakash Mutyala BMCWEB_LOG_DEBUG << "Sensor override mode is not " 282270d1d0aaSjayaprakash Mutyala "Enabled. Returning ... "; 282370d1d0aaSjayaprakash Mutyala messages::internalError(sensorAsyncResp->res); 282470d1d0aaSjayaprakash Mutyala return; 282570d1d0aaSjayaprakash Mutyala } 282670d1d0aaSjayaprakash Mutyala 282770d1d0aaSjayaprakash Mutyala if (isOverridingAllowed(*manufacturingModeStatus)) 282870d1d0aaSjayaprakash Mutyala { 282970d1d0aaSjayaprakash Mutyala BMCWEB_LOG_INFO << "Manufacturing mode is Enabled. " 283070d1d0aaSjayaprakash Mutyala "Proceeding further... "; 2831*397fd61fSjayaprakash Mutyala setSensorsOverride(sensorAsyncResp, allCollections); 283270d1d0aaSjayaprakash Mutyala } 283370d1d0aaSjayaprakash Mutyala else 283470d1d0aaSjayaprakash Mutyala { 283570d1d0aaSjayaprakash Mutyala BMCWEB_LOG_WARNING 283670d1d0aaSjayaprakash Mutyala << "Manufacturing mode is not Enabled...can't " 283770d1d0aaSjayaprakash Mutyala "Override the sensor value. "; 283870d1d0aaSjayaprakash Mutyala 283970d1d0aaSjayaprakash Mutyala messages::actionNotSupported( 284070d1d0aaSjayaprakash Mutyala sensorAsyncResp->res, 284170d1d0aaSjayaprakash Mutyala "Overriding of Sensor Value for non " 284270d1d0aaSjayaprakash Mutyala "manufacturing mode"); 284370d1d0aaSjayaprakash Mutyala return; 284470d1d0aaSjayaprakash Mutyala } 284570d1d0aaSjayaprakash Mutyala }, 284670d1d0aaSjayaprakash Mutyala serviceName, path, "org.freedesktop.DBus.Properties", "Get", 284770d1d0aaSjayaprakash Mutyala "xyz.openbmc_project.Security.SpecialMode", "SpecialMode"); 284870d1d0aaSjayaprakash Mutyala }, 284970d1d0aaSjayaprakash Mutyala 285070d1d0aaSjayaprakash Mutyala "xyz.openbmc_project.ObjectMapper", 285170d1d0aaSjayaprakash Mutyala "/xyz/openbmc_project/object_mapper", 285270d1d0aaSjayaprakash Mutyala "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 5, interfaces); 285370d1d0aaSjayaprakash Mutyala } 285470d1d0aaSjayaprakash Mutyala 285595a3ecadSAnthony Wilson class SensorCollection : public Node 285695a3ecadSAnthony Wilson { 285795a3ecadSAnthony Wilson public: 285895a3ecadSAnthony Wilson SensorCollection(CrowApp& app) : 285995a3ecadSAnthony Wilson Node(app, "/redfish/v1/Chassis/<str>/Sensors", std::string()) 286095a3ecadSAnthony Wilson { 286195a3ecadSAnthony Wilson entityPrivileges = { 286295a3ecadSAnthony Wilson {boost::beast::http::verb::get, {{"Login"}}}, 286395a3ecadSAnthony Wilson {boost::beast::http::verb::head, {{"Login"}}}, 286495a3ecadSAnthony Wilson {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, 286595a3ecadSAnthony Wilson {boost::beast::http::verb::put, {{"ConfigureManager"}}}, 286695a3ecadSAnthony Wilson {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, 286795a3ecadSAnthony Wilson {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; 286895a3ecadSAnthony Wilson } 286995a3ecadSAnthony Wilson 287095a3ecadSAnthony Wilson private: 287195a3ecadSAnthony Wilson std::vector<const char*> typeList = { 287295a3ecadSAnthony Wilson "/xyz/openbmc_project/sensors/power", 287395a3ecadSAnthony Wilson "/xyz/openbmc_project/sensors/current"}; 287495a3ecadSAnthony Wilson void doGet(crow::Response& res, const crow::Request& req, 287595a3ecadSAnthony Wilson const std::vector<std::string>& params) override 287695a3ecadSAnthony Wilson { 287795a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "SensorCollection doGet enter"; 287895a3ecadSAnthony Wilson if (params.size() != 1) 287995a3ecadSAnthony Wilson { 288095a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "SensorCollection doGet param size < 1"; 288195a3ecadSAnthony Wilson messages::internalError(res); 288295a3ecadSAnthony Wilson res.end(); 288395a3ecadSAnthony Wilson return; 288495a3ecadSAnthony Wilson } 288595a3ecadSAnthony Wilson 288695a3ecadSAnthony Wilson const std::string& chassisId = params[0]; 288795a3ecadSAnthony Wilson std::shared_ptr<SensorsAsyncResp> asyncResp = 288895a3ecadSAnthony Wilson std::make_shared<SensorsAsyncResp>(res, chassisId, typeList, 288995a3ecadSAnthony Wilson "Sensors"); 289095a3ecadSAnthony Wilson 289195a3ecadSAnthony Wilson auto getChassisCb = 289295a3ecadSAnthony Wilson [asyncResp](std::shared_ptr<boost::container::flat_set<std::string>> 289395a3ecadSAnthony Wilson sensorNames) { 289495a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb enter"; 289595a3ecadSAnthony Wilson 289695a3ecadSAnthony Wilson nlohmann::json& entriesArray = 289795a3ecadSAnthony Wilson asyncResp->res.jsonValue["Members"]; 289895a3ecadSAnthony Wilson for (auto& sensor : *sensorNames) 289995a3ecadSAnthony Wilson { 290095a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor; 290195a3ecadSAnthony Wilson 290295a3ecadSAnthony Wilson std::size_t lastPos = sensor.rfind("/"); 290395a3ecadSAnthony Wilson if (lastPos == std::string::npos || 290495a3ecadSAnthony Wilson lastPos + 1 >= sensor.size()) 290595a3ecadSAnthony Wilson { 290695a3ecadSAnthony Wilson BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor; 290795a3ecadSAnthony Wilson messages::internalError(asyncResp->res); 290895a3ecadSAnthony Wilson return; 290995a3ecadSAnthony Wilson } 291095a3ecadSAnthony Wilson std::string sensorName = sensor.substr(lastPos + 1); 291195a3ecadSAnthony Wilson entriesArray.push_back( 291295a3ecadSAnthony Wilson {{"@odata.id", 291395a3ecadSAnthony Wilson "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" + 291495a3ecadSAnthony Wilson asyncResp->chassisSubNode + "/" + sensorName}}); 291595a3ecadSAnthony Wilson } 291695a3ecadSAnthony Wilson 291795a3ecadSAnthony Wilson asyncResp->res.jsonValue["Members@odata.count"] = 291895a3ecadSAnthony Wilson entriesArray.size(); 291995a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb exit"; 292095a3ecadSAnthony Wilson }; 292195a3ecadSAnthony Wilson 292295a3ecadSAnthony Wilson // Get set of sensors in chassis 292395a3ecadSAnthony Wilson getChassis(asyncResp, std::move(getChassisCb)); 292495a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "SensorCollection doGet exit"; 292595a3ecadSAnthony Wilson } 292695a3ecadSAnthony Wilson }; 292795a3ecadSAnthony Wilson 292895a3ecadSAnthony Wilson class Sensor : public Node 292995a3ecadSAnthony Wilson { 293095a3ecadSAnthony Wilson public: 293195a3ecadSAnthony Wilson Sensor(CrowApp& app) : 293295a3ecadSAnthony Wilson Node(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/", std::string(), 293395a3ecadSAnthony Wilson std::string()) 293495a3ecadSAnthony Wilson { 293595a3ecadSAnthony Wilson entityPrivileges = { 293695a3ecadSAnthony Wilson {boost::beast::http::verb::get, {{"Login"}}}, 293795a3ecadSAnthony Wilson {boost::beast::http::verb::head, {{"Login"}}}, 293895a3ecadSAnthony Wilson {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, 293995a3ecadSAnthony Wilson {boost::beast::http::verb::put, {{"ConfigureManager"}}}, 294095a3ecadSAnthony Wilson {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, 294195a3ecadSAnthony Wilson {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; 294295a3ecadSAnthony Wilson } 294395a3ecadSAnthony Wilson 294495a3ecadSAnthony Wilson private: 294595a3ecadSAnthony Wilson void doGet(crow::Response& res, const crow::Request& req, 294695a3ecadSAnthony Wilson const std::vector<std::string>& params) override 294795a3ecadSAnthony Wilson { 294895a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "Sensor doGet enter"; 294995a3ecadSAnthony Wilson if (params.size() != 2) 295095a3ecadSAnthony Wilson { 295195a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "Sensor doGet param size < 2"; 295295a3ecadSAnthony Wilson messages::internalError(res); 295395a3ecadSAnthony Wilson res.end(); 295495a3ecadSAnthony Wilson return; 295595a3ecadSAnthony Wilson } 295695a3ecadSAnthony Wilson const std::string& chassisId = params[0]; 295795a3ecadSAnthony Wilson std::shared_ptr<SensorsAsyncResp> asyncResp = 295895a3ecadSAnthony Wilson std::make_shared<SensorsAsyncResp>( 295995a3ecadSAnthony Wilson res, chassisId, std::vector<const char*>(), "Sensors"); 296095a3ecadSAnthony Wilson 296195a3ecadSAnthony Wilson const std::string& sensorName = params[1]; 296295a3ecadSAnthony Wilson const std::array<const char*, 1> interfaces = { 296395a3ecadSAnthony Wilson "xyz.openbmc_project.Sensor.Value"}; 296495a3ecadSAnthony Wilson 296595a3ecadSAnthony Wilson // Get a list of all of the sensors that implement Sensor.Value 296695a3ecadSAnthony Wilson // and get the path and service name associated with the sensor 296795a3ecadSAnthony Wilson crow::connections::systemBus->async_method_call( 296895a3ecadSAnthony Wilson [asyncResp, sensorName](const boost::system::error_code ec, 296995a3ecadSAnthony Wilson const GetSubTreeType& subtree) { 297095a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "respHandler1 enter"; 297195a3ecadSAnthony Wilson if (ec) 297295a3ecadSAnthony Wilson { 297395a3ecadSAnthony Wilson messages::internalError(asyncResp->res); 297495a3ecadSAnthony Wilson BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: " 297595a3ecadSAnthony Wilson << "Dbus error " << ec; 297695a3ecadSAnthony Wilson return; 297795a3ecadSAnthony Wilson } 297895a3ecadSAnthony Wilson 297995a3ecadSAnthony Wilson GetSubTreeType::const_iterator it = std::find_if( 298095a3ecadSAnthony Wilson subtree.begin(), subtree.end(), 298195a3ecadSAnthony Wilson [sensorName]( 298295a3ecadSAnthony Wilson const std::pair< 298395a3ecadSAnthony Wilson std::string, 298495a3ecadSAnthony Wilson std::vector<std::pair<std::string, 298595a3ecadSAnthony Wilson std::vector<std::string>>>>& 298695a3ecadSAnthony Wilson object) { 298795a3ecadSAnthony Wilson std::string_view sensor = object.first; 298895a3ecadSAnthony Wilson std::size_t lastPos = sensor.rfind("/"); 298995a3ecadSAnthony Wilson if (lastPos == std::string::npos || 299095a3ecadSAnthony Wilson lastPos + 1 >= sensor.size()) 299195a3ecadSAnthony Wilson { 299295a3ecadSAnthony Wilson BMCWEB_LOG_ERROR << "Invalid sensor path: " 299395a3ecadSAnthony Wilson << sensor; 299495a3ecadSAnthony Wilson return false; 299595a3ecadSAnthony Wilson } 299695a3ecadSAnthony Wilson std::string_view name = sensor.substr(lastPos + 1); 299795a3ecadSAnthony Wilson 299895a3ecadSAnthony Wilson return name == sensorName; 299995a3ecadSAnthony Wilson }); 300095a3ecadSAnthony Wilson 300195a3ecadSAnthony Wilson if (it == subtree.end()) 300295a3ecadSAnthony Wilson { 300395a3ecadSAnthony Wilson BMCWEB_LOG_ERROR << "Could not find path for sensor: " 300495a3ecadSAnthony Wilson << sensorName; 300595a3ecadSAnthony Wilson messages::resourceNotFound(asyncResp->res, "Sensor", 300695a3ecadSAnthony Wilson sensorName); 300795a3ecadSAnthony Wilson return; 300895a3ecadSAnthony Wilson } 300995a3ecadSAnthony Wilson std::string_view sensorPath = (*it).first; 301095a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" 301195a3ecadSAnthony Wilson << sensorName << "': " << sensorPath; 301295a3ecadSAnthony Wilson 301395a3ecadSAnthony Wilson const std::shared_ptr<boost::container::flat_set<std::string>> 301495a3ecadSAnthony Wilson sensorList = std::make_shared< 301595a3ecadSAnthony Wilson boost::container::flat_set<std::string>>(); 301695a3ecadSAnthony Wilson 301795a3ecadSAnthony Wilson sensorList->emplace(sensorPath); 301895a3ecadSAnthony Wilson processSensorList(asyncResp, sensorList); 301995a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "respHandler1 exit"; 302095a3ecadSAnthony Wilson }, 302195a3ecadSAnthony Wilson "xyz.openbmc_project.ObjectMapper", 302295a3ecadSAnthony Wilson "/xyz/openbmc_project/object_mapper", 302395a3ecadSAnthony Wilson "xyz.openbmc_project.ObjectMapper", "GetSubTree", 302495a3ecadSAnthony Wilson "/xyz/openbmc_project/sensors", 2, interfaces); 302595a3ecadSAnthony Wilson } 302695a3ecadSAnthony Wilson }; 302795a3ecadSAnthony Wilson 302808777fb0SLewanczyk, Dawid } // namespace redfish 3029