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> 241abe55efSEd Tanous #include <dbus_singleton.hpp> 25413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp> 261214b7e7SGunnar Mills 271214b7e7SGunnar Mills #include <cmath> 28abf2add6SEd Tanous #include <variant> 2908777fb0SLewanczyk, Dawid 301abe55efSEd Tanous namespace redfish 311abe55efSEd Tanous { 3208777fb0SLewanczyk, Dawid 3308777fb0SLewanczyk, Dawid using GetSubTreeType = std::vector< 3408777fb0SLewanczyk, Dawid std::pair<std::string, 3508777fb0SLewanczyk, Dawid std::vector<std::pair<std::string, std::vector<std::string>>>>>; 3608777fb0SLewanczyk, Dawid 37adc4f0dbSShawn McCarney using SensorVariant = 38adc4f0dbSShawn McCarney std::variant<int64_t, double, uint32_t, bool, std::string>; 39aa2e59c1SEd Tanous 4008777fb0SLewanczyk, Dawid using ManagedObjectsVectorType = std::vector<std::pair< 41aa2e59c1SEd Tanous sdbusplus::message::object_path, 4208777fb0SLewanczyk, Dawid boost::container::flat_map< 43aa2e59c1SEd Tanous std::string, boost::container::flat_map<std::string, SensorVariant>>>>; 4408777fb0SLewanczyk, Dawid 45a0ec28b6SAdrian Ambrożewicz namespace sensors 46a0ec28b6SAdrian Ambrożewicz { 47a0ec28b6SAdrian Ambrożewicz namespace node 48a0ec28b6SAdrian Ambrożewicz { 49a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power"; 50a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors"; 51a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal"; 52a0ec28b6SAdrian Ambrożewicz } // namespace node 53a0ec28b6SAdrian Ambrożewicz 54a0ec28b6SAdrian Ambrożewicz namespace dbus 55a0ec28b6SAdrian Ambrożewicz { 56a0ec28b6SAdrian Ambrożewicz static const boost::container::flat_map<std::string_view, 57a0ec28b6SAdrian Ambrożewicz std::vector<const char*>> 58a0ec28b6SAdrian Ambrożewicz types = {{node::power, 59a0ec28b6SAdrian Ambrożewicz {"/xyz/openbmc_project/sensors/voltage", 60a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/power"}}, 61a0ec28b6SAdrian Ambrożewicz {node::sensors, 62a0ec28b6SAdrian Ambrożewicz {"/xyz/openbmc_project/sensors/power", 63a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/current", 64a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/utilization"}}, 65a0ec28b6SAdrian Ambrożewicz {node::thermal, 66a0ec28b6SAdrian Ambrożewicz {"/xyz/openbmc_project/sensors/fan_tach", 67a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/temperature", 68a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/fan_pwm"}}}; 69a0ec28b6SAdrian Ambrożewicz } 70a0ec28b6SAdrian Ambrożewicz } // namespace sensors 71a0ec28b6SAdrian Ambrożewicz 7208777fb0SLewanczyk, Dawid /** 73588c3f0dSKowalski, Kamil * SensorsAsyncResp 7408777fb0SLewanczyk, Dawid * Gathers data needed for response processing after async calls are done 7508777fb0SLewanczyk, Dawid */ 761abe55efSEd Tanous class SensorsAsyncResp 771abe55efSEd Tanous { 7808777fb0SLewanczyk, Dawid public: 79a0ec28b6SAdrian Ambrożewicz using DataCompleteCb = std::function<void( 80a0ec28b6SAdrian Ambrożewicz const boost::beast::http::status status, 81a0ec28b6SAdrian Ambrożewicz const boost::container::flat_map<std::string, std::string>& uriToDbus)>; 82a0ec28b6SAdrian Ambrożewicz 83a0ec28b6SAdrian Ambrożewicz struct SensorData 84a0ec28b6SAdrian Ambrożewicz { 85a0ec28b6SAdrian Ambrożewicz const std::string name; 86a0ec28b6SAdrian Ambrożewicz std::string uri; 87a0ec28b6SAdrian Ambrożewicz const std::string valueKey; 88a0ec28b6SAdrian Ambrożewicz const std::string dbusPath; 89a0ec28b6SAdrian Ambrożewicz }; 90a0ec28b6SAdrian Ambrożewicz 91271584abSEd Tanous SensorsAsyncResp(crow::Response& response, const std::string& chassisIdIn, 92271584abSEd Tanous const std::vector<const char*> typesIn, 93a0ec28b6SAdrian Ambrożewicz const std::string_view& subNode) : 9443b761d0SEd Tanous res(response), 95271584abSEd Tanous chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode) 961214b7e7SGunnar Mills {} 9708777fb0SLewanczyk, Dawid 98a0ec28b6SAdrian Ambrożewicz // Store extra data about sensor mapping and return it in callback 99a0ec28b6SAdrian Ambrożewicz SensorsAsyncResp(crow::Response& response, const std::string& chassisIdIn, 100a0ec28b6SAdrian Ambrożewicz const std::vector<const char*> typesIn, 101a0ec28b6SAdrian Ambrożewicz const std::string_view& subNode, 102a0ec28b6SAdrian Ambrożewicz DataCompleteCb&& creationComplete) : 103a0ec28b6SAdrian Ambrożewicz res(response), 104a0ec28b6SAdrian Ambrożewicz chassisId(chassisIdIn), types(typesIn), 105a0ec28b6SAdrian Ambrożewicz chassisSubNode(subNode), metadata{std::vector<SensorData>()}, 106a0ec28b6SAdrian Ambrożewicz dataComplete{std::move(creationComplete)} 107a0ec28b6SAdrian Ambrożewicz {} 108a0ec28b6SAdrian Ambrożewicz 1091abe55efSEd Tanous ~SensorsAsyncResp() 1101abe55efSEd Tanous { 1111abe55efSEd Tanous if (res.result() == boost::beast::http::status::internal_server_error) 1121abe55efSEd Tanous { 1131abe55efSEd Tanous // Reset the json object to clear out any data that made it in 1141abe55efSEd Tanous // before the error happened todo(ed) handle error condition with 1151abe55efSEd Tanous // proper code 11655c7b7a2SEd Tanous res.jsonValue = nlohmann::json::object(); 11708777fb0SLewanczyk, Dawid } 118a0ec28b6SAdrian Ambrożewicz 119a0ec28b6SAdrian Ambrożewicz if (dataComplete && metadata) 120a0ec28b6SAdrian Ambrożewicz { 121a0ec28b6SAdrian Ambrożewicz boost::container::flat_map<std::string, std::string> map; 122a0ec28b6SAdrian Ambrożewicz if (res.result() == boost::beast::http::status::ok) 123a0ec28b6SAdrian Ambrożewicz { 124a0ec28b6SAdrian Ambrożewicz for (auto& sensor : *metadata) 125a0ec28b6SAdrian Ambrożewicz { 126a0ec28b6SAdrian Ambrożewicz map.insert(std::make_pair(sensor.uri + sensor.valueKey, 127a0ec28b6SAdrian Ambrożewicz sensor.dbusPath)); 128a0ec28b6SAdrian Ambrożewicz } 129a0ec28b6SAdrian Ambrożewicz } 130a0ec28b6SAdrian Ambrożewicz dataComplete(res.result(), map); 131a0ec28b6SAdrian Ambrożewicz } 132a0ec28b6SAdrian Ambrożewicz 13308777fb0SLewanczyk, Dawid res.end(); 13408777fb0SLewanczyk, Dawid } 135588c3f0dSKowalski, Kamil 136a0ec28b6SAdrian Ambrożewicz void addMetadata(const nlohmann::json& sensorObject, 137a0ec28b6SAdrian Ambrożewicz const std::string& valueKey, const std::string& dbusPath) 138a0ec28b6SAdrian Ambrożewicz { 139a0ec28b6SAdrian Ambrożewicz if (metadata) 140a0ec28b6SAdrian Ambrożewicz { 141a0ec28b6SAdrian Ambrożewicz metadata->emplace_back(SensorData{sensorObject["Name"], 142a0ec28b6SAdrian Ambrożewicz sensorObject["@odata.id"], 143a0ec28b6SAdrian Ambrożewicz valueKey, dbusPath}); 144a0ec28b6SAdrian Ambrożewicz } 145a0ec28b6SAdrian Ambrożewicz } 146a0ec28b6SAdrian Ambrożewicz 147a0ec28b6SAdrian Ambrożewicz void updateUri(const std::string& name, const std::string& uri) 148a0ec28b6SAdrian Ambrożewicz { 149a0ec28b6SAdrian Ambrożewicz if (metadata) 150a0ec28b6SAdrian Ambrożewicz { 151a0ec28b6SAdrian Ambrożewicz for (auto& sensor : *metadata) 152a0ec28b6SAdrian Ambrożewicz { 153a0ec28b6SAdrian Ambrożewicz if (sensor.name == name) 154a0ec28b6SAdrian Ambrożewicz { 155a0ec28b6SAdrian Ambrożewicz sensor.uri = uri; 156a0ec28b6SAdrian Ambrożewicz } 157a0ec28b6SAdrian Ambrożewicz } 158a0ec28b6SAdrian Ambrożewicz } 159a0ec28b6SAdrian Ambrożewicz } 160a0ec28b6SAdrian Ambrożewicz 16155c7b7a2SEd Tanous crow::Response& res; 162a0ec28b6SAdrian Ambrożewicz const std::string chassisId; 16308777fb0SLewanczyk, Dawid const std::vector<const char*> types; 164a0ec28b6SAdrian Ambrożewicz const std::string chassisSubNode; 165a0ec28b6SAdrian Ambrożewicz 166a0ec28b6SAdrian Ambrożewicz private: 167a0ec28b6SAdrian Ambrożewicz std::optional<std::vector<SensorData>> metadata; 168a0ec28b6SAdrian Ambrożewicz DataCompleteCb dataComplete; 16908777fb0SLewanczyk, Dawid }; 17008777fb0SLewanczyk, Dawid 17108777fb0SLewanczyk, Dawid /** 172d500549bSAnthony Wilson * Possible states for physical inventory leds 173d500549bSAnthony Wilson */ 174d500549bSAnthony Wilson enum class LedState 175d500549bSAnthony Wilson { 176d500549bSAnthony Wilson OFF, 177d500549bSAnthony Wilson ON, 178d500549bSAnthony Wilson BLINK, 179d500549bSAnthony Wilson UNKNOWN 180d500549bSAnthony Wilson }; 181d500549bSAnthony Wilson 182d500549bSAnthony Wilson /** 183adc4f0dbSShawn McCarney * D-Bus inventory item associated with one or more sensors. 184adc4f0dbSShawn McCarney */ 185adc4f0dbSShawn McCarney class InventoryItem 186adc4f0dbSShawn McCarney { 187adc4f0dbSShawn McCarney public: 188adc4f0dbSShawn McCarney InventoryItem(const std::string& objPath) : 189adc4f0dbSShawn McCarney objectPath(objPath), name(), isPresent(true), isFunctional(true), 19042cbe538SGunnar Mills isPowerSupply(false), powerSupplyEfficiencyPercent(-1), manufacturer(), 19142cbe538SGunnar Mills model(), partNumber(), serialNumber(), sensors(), ledObjectPath(""), 192d500549bSAnthony Wilson ledState(LedState::UNKNOWN) 193adc4f0dbSShawn McCarney { 194adc4f0dbSShawn McCarney // Set inventory item name to last node of object path 195adc4f0dbSShawn McCarney auto pos = objectPath.rfind('/'); 196adc4f0dbSShawn McCarney if ((pos != std::string::npos) && ((pos + 1) < objectPath.size())) 197adc4f0dbSShawn McCarney { 198adc4f0dbSShawn McCarney name = objectPath.substr(pos + 1); 199adc4f0dbSShawn McCarney } 200adc4f0dbSShawn McCarney } 201adc4f0dbSShawn McCarney 202adc4f0dbSShawn McCarney std::string objectPath; 203adc4f0dbSShawn McCarney std::string name; 204adc4f0dbSShawn McCarney bool isPresent; 205adc4f0dbSShawn McCarney bool isFunctional; 206adc4f0dbSShawn McCarney bool isPowerSupply; 20742cbe538SGunnar Mills int powerSupplyEfficiencyPercent; 208adc4f0dbSShawn McCarney std::string manufacturer; 209adc4f0dbSShawn McCarney std::string model; 210adc4f0dbSShawn McCarney std::string partNumber; 211adc4f0dbSShawn McCarney std::string serialNumber; 212adc4f0dbSShawn McCarney std::set<std::string> sensors; 213d500549bSAnthony Wilson std::string ledObjectPath; 214d500549bSAnthony Wilson LedState ledState; 215adc4f0dbSShawn McCarney }; 216adc4f0dbSShawn McCarney 217adc4f0dbSShawn McCarney /** 218413961deSRichard Marian Thomaiyar * @brief Get objects with connection necessary for sensors 219588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 22008777fb0SLewanczyk, Dawid * @param sensorNames Sensors retrieved from chassis 22108777fb0SLewanczyk, Dawid * @param callback Callback for processing gathered connections 22208777fb0SLewanczyk, Dawid */ 22308777fb0SLewanczyk, Dawid template <typename Callback> 224413961deSRichard Marian Thomaiyar void getObjectsWithConnection( 225413961deSRichard Marian Thomaiyar std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp, 22649c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames, 2271abe55efSEd Tanous Callback&& callback) 2281abe55efSEd Tanous { 229413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter"; 23003b5bae3SJames Feist const std::string path = "/xyz/openbmc_project/sensors"; 23108777fb0SLewanczyk, Dawid const std::array<std::string, 1> interfaces = { 23208777fb0SLewanczyk, Dawid "xyz.openbmc_project.Sensor.Value"}; 23308777fb0SLewanczyk, Dawid 23408777fb0SLewanczyk, Dawid // Response handler for parsing objects subtree 2351abe55efSEd Tanous auto respHandler = [callback{std::move(callback)}, SensorsAsyncResp, 2361abe55efSEd Tanous sensorNames](const boost::system::error_code ec, 2371abe55efSEd Tanous const GetSubTreeType& subtree) { 238413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter"; 2391abe55efSEd Tanous if (ec) 2401abe55efSEd Tanous { 2415f7d88c4SEd Tanous messages::internalError(SensorsAsyncResp->res); 242413961deSRichard Marian Thomaiyar BMCWEB_LOG_ERROR 243413961deSRichard Marian Thomaiyar << "getObjectsWithConnection resp_handler: Dbus error " << ec; 24408777fb0SLewanczyk, Dawid return; 24508777fb0SLewanczyk, Dawid } 24608777fb0SLewanczyk, Dawid 24755c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees"; 24808777fb0SLewanczyk, Dawid 24908777fb0SLewanczyk, Dawid // Make unique list of connections only for requested sensor types and 25008777fb0SLewanczyk, Dawid // found in the chassis 25108777fb0SLewanczyk, Dawid boost::container::flat_set<std::string> connections; 252413961deSRichard Marian Thomaiyar std::set<std::pair<std::string, std::string>> objectsWithConnection; 2531abe55efSEd Tanous // Intrinsic to avoid malloc. Most systems will have < 8 sensor 2541abe55efSEd Tanous // producers 25508777fb0SLewanczyk, Dawid connections.reserve(8); 25608777fb0SLewanczyk, Dawid 25749c53ac9SJohnathan Mantey BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size(); 25849c53ac9SJohnathan Mantey for (const std::string& tsensor : *sensorNames) 2591abe55efSEd Tanous { 26055c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor; 26108777fb0SLewanczyk, Dawid } 26208777fb0SLewanczyk, Dawid 26308777fb0SLewanczyk, Dawid for (const std::pair< 26408777fb0SLewanczyk, Dawid std::string, 26508777fb0SLewanczyk, Dawid std::vector<std::pair<std::string, std::vector<std::string>>>>& 2661abe55efSEd Tanous object : subtree) 2671abe55efSEd Tanous { 26849c53ac9SJohnathan Mantey if (sensorNames->find(object.first) != sensorNames->end()) 2691abe55efSEd Tanous { 27049c53ac9SJohnathan Mantey for (const std::pair<std::string, std::vector<std::string>>& 2711abe55efSEd Tanous objData : object.second) 2721abe55efSEd Tanous { 27349c53ac9SJohnathan Mantey BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first; 27408777fb0SLewanczyk, Dawid connections.insert(objData.first); 275de629b6eSShawn McCarney objectsWithConnection.insert( 276de629b6eSShawn McCarney std::make_pair(object.first, objData.first)); 27708777fb0SLewanczyk, Dawid } 27808777fb0SLewanczyk, Dawid } 27908777fb0SLewanczyk, Dawid } 28055c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections"; 281413961deSRichard Marian Thomaiyar callback(std::move(connections), std::move(objectsWithConnection)); 282413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit"; 28308777fb0SLewanczyk, Dawid }; 28408777fb0SLewanczyk, Dawid // Make call to ObjectMapper to find all sensors objects 28555c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 28655c7b7a2SEd Tanous std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 2871abe55efSEd Tanous "/xyz/openbmc_project/object_mapper", 2881abe55efSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces); 289413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit"; 290413961deSRichard Marian Thomaiyar } 291413961deSRichard Marian Thomaiyar 292413961deSRichard Marian Thomaiyar /** 293413961deSRichard Marian Thomaiyar * @brief Create connections necessary for sensors 294413961deSRichard Marian Thomaiyar * @param SensorsAsyncResp Pointer to object holding response data 295413961deSRichard Marian Thomaiyar * @param sensorNames Sensors retrieved from chassis 296413961deSRichard Marian Thomaiyar * @param callback Callback for processing gathered connections 297413961deSRichard Marian Thomaiyar */ 298413961deSRichard Marian Thomaiyar template <typename Callback> 29949c53ac9SJohnathan Mantey void getConnections( 30049c53ac9SJohnathan Mantey std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp, 30149c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames, 302413961deSRichard Marian Thomaiyar Callback&& callback) 303413961deSRichard Marian Thomaiyar { 304413961deSRichard Marian Thomaiyar auto objectsWithConnectionCb = 305413961deSRichard Marian Thomaiyar [callback](const boost::container::flat_set<std::string>& connections, 306413961deSRichard Marian Thomaiyar const std::set<std::pair<std::string, std::string>>& 307413961deSRichard Marian Thomaiyar objectsWithConnection) { 308413961deSRichard Marian Thomaiyar callback(std::move(connections)); 309413961deSRichard Marian Thomaiyar }; 310413961deSRichard Marian Thomaiyar getObjectsWithConnection(SensorsAsyncResp, sensorNames, 311413961deSRichard Marian Thomaiyar std::move(objectsWithConnectionCb)); 31208777fb0SLewanczyk, Dawid } 31308777fb0SLewanczyk, Dawid 31408777fb0SLewanczyk, Dawid /** 31549c53ac9SJohnathan Mantey * @brief Shrinks the list of sensors for processing 31649c53ac9SJohnathan Mantey * @param SensorsAysncResp The class holding the Redfish response 31749c53ac9SJohnathan Mantey * @param allSensors A list of all the sensors associated to the 31849c53ac9SJohnathan Mantey * chassis element (i.e. baseboard, front panel, etc...) 31949c53ac9SJohnathan Mantey * @param activeSensors A list that is a reduction of the incoming 32049c53ac9SJohnathan Mantey * allSensors list. Eliminate Thermal sensors when a Power request is 32149c53ac9SJohnathan Mantey * made, and eliminate Power sensors when a Thermal request is made. 32249c53ac9SJohnathan Mantey */ 323*23a21a1cSEd Tanous inline void reduceSensorList( 32449c53ac9SJohnathan Mantey std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp, 32549c53ac9SJohnathan Mantey const std::vector<std::string>* allSensors, 32649c53ac9SJohnathan Mantey std::shared_ptr<boost::container::flat_set<std::string>> activeSensors) 32749c53ac9SJohnathan Mantey { 32849c53ac9SJohnathan Mantey if (SensorsAsyncResp == nullptr) 32949c53ac9SJohnathan Mantey { 33049c53ac9SJohnathan Mantey return; 33149c53ac9SJohnathan Mantey } 33249c53ac9SJohnathan Mantey if ((allSensors == nullptr) || (activeSensors == nullptr)) 33349c53ac9SJohnathan Mantey { 33449c53ac9SJohnathan Mantey messages::resourceNotFound( 33549c53ac9SJohnathan Mantey SensorsAsyncResp->res, SensorsAsyncResp->chassisSubNode, 336a0ec28b6SAdrian Ambrożewicz SensorsAsyncResp->chassisSubNode == sensors::node::thermal 337a0ec28b6SAdrian Ambrożewicz ? "Temperatures" 33849c53ac9SJohnathan Mantey : "Voltages"); 33949c53ac9SJohnathan Mantey 34049c53ac9SJohnathan Mantey return; 34149c53ac9SJohnathan Mantey } 34249c53ac9SJohnathan Mantey if (allSensors->empty()) 34349c53ac9SJohnathan Mantey { 34449c53ac9SJohnathan Mantey // Nothing to do, the activeSensors object is also empty 34549c53ac9SJohnathan Mantey return; 34649c53ac9SJohnathan Mantey } 34749c53ac9SJohnathan Mantey 34849c53ac9SJohnathan Mantey for (const char* type : SensorsAsyncResp->types) 34949c53ac9SJohnathan Mantey { 35049c53ac9SJohnathan Mantey for (const std::string& sensor : *allSensors) 35149c53ac9SJohnathan Mantey { 35249c53ac9SJohnathan Mantey if (boost::starts_with(sensor, type)) 35349c53ac9SJohnathan Mantey { 35449c53ac9SJohnathan Mantey activeSensors->emplace(sensor); 35549c53ac9SJohnathan Mantey } 35649c53ac9SJohnathan Mantey } 35749c53ac9SJohnathan Mantey } 35849c53ac9SJohnathan Mantey } 35949c53ac9SJohnathan Mantey 36049c53ac9SJohnathan Mantey /** 3614bb3dc34SCarol Wang * @brief Retrieves valid chassis path 3624bb3dc34SCarol Wang * @param asyncResp Pointer to object holding response data 3634bb3dc34SCarol Wang * @param callback Callback for next step to get valid chassis path 3644bb3dc34SCarol Wang */ 3654bb3dc34SCarol Wang template <typename Callback> 3664bb3dc34SCarol Wang void getValidChassisPath(std::shared_ptr<SensorsAsyncResp> asyncResp, 3674bb3dc34SCarol Wang Callback&& callback) 3684bb3dc34SCarol Wang { 3694bb3dc34SCarol Wang BMCWEB_LOG_DEBUG << "checkChassisId enter"; 3704bb3dc34SCarol Wang const std::array<const char*, 2> interfaces = { 3714bb3dc34SCarol Wang "xyz.openbmc_project.Inventory.Item.Board", 3724bb3dc34SCarol Wang "xyz.openbmc_project.Inventory.Item.Chassis"}; 3734bb3dc34SCarol Wang 3744bb3dc34SCarol Wang auto respHandler = 3754bb3dc34SCarol Wang [callback{std::move(callback)}, 3764bb3dc34SCarol Wang asyncResp](const boost::system::error_code ec, 3774bb3dc34SCarol Wang const std::vector<std::string>& chassisPaths) mutable { 3784bb3dc34SCarol Wang BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter"; 3794bb3dc34SCarol Wang if (ec) 3804bb3dc34SCarol Wang { 3814bb3dc34SCarol Wang BMCWEB_LOG_ERROR 3824bb3dc34SCarol Wang << "getValidChassisPath respHandler DBUS error: " << ec; 3834bb3dc34SCarol Wang messages::internalError(asyncResp->res); 3844bb3dc34SCarol Wang return; 3854bb3dc34SCarol Wang } 3864bb3dc34SCarol Wang 3874bb3dc34SCarol Wang std::optional<std::string> chassisPath; 3884bb3dc34SCarol Wang std::string chassisName; 3894bb3dc34SCarol Wang for (const std::string& chassis : chassisPaths) 3904bb3dc34SCarol Wang { 3914bb3dc34SCarol Wang std::size_t lastPos = chassis.rfind("/"); 3924bb3dc34SCarol Wang if (lastPos == std::string::npos) 3934bb3dc34SCarol Wang { 3944bb3dc34SCarol Wang BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis; 3954bb3dc34SCarol Wang continue; 3964bb3dc34SCarol Wang } 3974bb3dc34SCarol Wang chassisName = chassis.substr(lastPos + 1); 3984bb3dc34SCarol Wang if (chassisName == asyncResp->chassisId) 3994bb3dc34SCarol Wang { 4004bb3dc34SCarol Wang chassisPath = chassis; 4014bb3dc34SCarol Wang break; 4024bb3dc34SCarol Wang } 4034bb3dc34SCarol Wang } 4044bb3dc34SCarol Wang callback(chassisPath); 4054bb3dc34SCarol Wang }; 4064bb3dc34SCarol Wang 4074bb3dc34SCarol Wang // Get the Chassis Collection 4084bb3dc34SCarol Wang crow::connections::systemBus->async_method_call( 4094bb3dc34SCarol Wang respHandler, "xyz.openbmc_project.ObjectMapper", 4104bb3dc34SCarol Wang "/xyz/openbmc_project/object_mapper", 4114bb3dc34SCarol Wang "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 4124bb3dc34SCarol Wang "/xyz/openbmc_project/inventory", 0, interfaces); 4134bb3dc34SCarol Wang BMCWEB_LOG_DEBUG << "checkChassisId exit"; 4144bb3dc34SCarol Wang } 4154bb3dc34SCarol Wang 4164bb3dc34SCarol Wang /** 41708777fb0SLewanczyk, Dawid * @brief Retrieves requested chassis sensors and redundancy data from DBus . 418588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 41908777fb0SLewanczyk, Dawid * @param callback Callback for next step in gathered sensor processing 42008777fb0SLewanczyk, Dawid */ 42108777fb0SLewanczyk, Dawid template <typename Callback> 42249c53ac9SJohnathan Mantey void getChassis(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 4231abe55efSEd Tanous Callback&& callback) 4241abe55efSEd Tanous { 42555c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis enter"; 426adc4f0dbSShawn McCarney const std::array<const char*, 2> interfaces = { 42749c53ac9SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Board", 428adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.Chassis"}; 42949c53ac9SJohnathan Mantey auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp]( 43049c53ac9SJohnathan Mantey const boost::system::error_code ec, 43149c53ac9SJohnathan Mantey const std::vector<std::string>& chassisPaths) { 43255c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis respHandler enter"; 4331abe55efSEd Tanous if (ec) 4341abe55efSEd Tanous { 43555c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec; 43649c53ac9SJohnathan Mantey messages::internalError(sensorsAsyncResp->res); 43708777fb0SLewanczyk, Dawid return; 43808777fb0SLewanczyk, Dawid } 43908777fb0SLewanczyk, Dawid 44049c53ac9SJohnathan Mantey const std::string* chassisPath = nullptr; 44149c53ac9SJohnathan Mantey std::string chassisName; 44249c53ac9SJohnathan Mantey for (const std::string& chassis : chassisPaths) 4431abe55efSEd Tanous { 44449c53ac9SJohnathan Mantey std::size_t lastPos = chassis.rfind("/"); 44549c53ac9SJohnathan Mantey if (lastPos == std::string::npos) 4461abe55efSEd Tanous { 44749c53ac9SJohnathan Mantey BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis; 448daf36e2eSEd Tanous continue; 449daf36e2eSEd Tanous } 45049c53ac9SJohnathan Mantey chassisName = chassis.substr(lastPos + 1); 45149c53ac9SJohnathan Mantey if (chassisName == sensorsAsyncResp->chassisId) 4521abe55efSEd Tanous { 45349c53ac9SJohnathan Mantey chassisPath = &chassis; 45449c53ac9SJohnathan Mantey break; 455daf36e2eSEd Tanous } 45649c53ac9SJohnathan Mantey } 45749c53ac9SJohnathan Mantey if (chassisPath == nullptr) 4581abe55efSEd Tanous { 45949c53ac9SJohnathan Mantey messages::resourceNotFound(sensorsAsyncResp->res, "Chassis", 46049c53ac9SJohnathan Mantey sensorsAsyncResp->chassisId); 46149c53ac9SJohnathan Mantey return; 4621abe55efSEd Tanous } 46308777fb0SLewanczyk, Dawid 46449c53ac9SJohnathan Mantey const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode; 465a0ec28b6SAdrian Ambrożewicz if (chassisSubNode == sensors::node::power) 46649c53ac9SJohnathan Mantey { 46749c53ac9SJohnathan Mantey sensorsAsyncResp->res.jsonValue["@odata.type"] = 46849c53ac9SJohnathan Mantey "#Power.v1_5_2.Power"; 46949c53ac9SJohnathan Mantey } 470a0ec28b6SAdrian Ambrożewicz else if (chassisSubNode == sensors::node::thermal) 47149c53ac9SJohnathan Mantey { 47249c53ac9SJohnathan Mantey sensorsAsyncResp->res.jsonValue["@odata.type"] = 47349c53ac9SJohnathan Mantey "#Thermal.v1_4_0.Thermal"; 4744f9a2130SJennifer Lee sensorsAsyncResp->res.jsonValue["Fans"] = nlohmann::json::array(); 4754f9a2130SJennifer Lee sensorsAsyncResp->res.jsonValue["Temperatures"] = 4764f9a2130SJennifer Lee nlohmann::json::array(); 47749c53ac9SJohnathan Mantey } 478a0ec28b6SAdrian Ambrożewicz else if (chassisSubNode == sensors::node::sensors) 47995a3ecadSAnthony Wilson { 48095a3ecadSAnthony Wilson sensorsAsyncResp->res.jsonValue["@odata.type"] = 48195a3ecadSAnthony Wilson "#SensorCollection.SensorCollection"; 48295a3ecadSAnthony Wilson sensorsAsyncResp->res.jsonValue["Description"] = 48395a3ecadSAnthony Wilson "Collection of Sensors for this Chassis"; 48495a3ecadSAnthony Wilson sensorsAsyncResp->res.jsonValue["Members"] = 48595a3ecadSAnthony Wilson nlohmann::json::array(); 48695a3ecadSAnthony Wilson sensorsAsyncResp->res.jsonValue["Members@odata.count"] = 0; 48795a3ecadSAnthony Wilson } 48895a3ecadSAnthony Wilson 489a0ec28b6SAdrian Ambrożewicz if (chassisSubNode != sensors::node::sensors) 49095a3ecadSAnthony Wilson { 49195a3ecadSAnthony Wilson sensorsAsyncResp->res.jsonValue["Id"] = chassisSubNode; 49295a3ecadSAnthony Wilson } 49395a3ecadSAnthony Wilson 49449c53ac9SJohnathan Mantey sensorsAsyncResp->res.jsonValue["@odata.id"] = 49549c53ac9SJohnathan Mantey "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" + 49649c53ac9SJohnathan Mantey chassisSubNode; 49749c53ac9SJohnathan Mantey sensorsAsyncResp->res.jsonValue["Name"] = chassisSubNode; 49849c53ac9SJohnathan Mantey 4998fb49dd6SShawn McCarney // Get the list of all sensors for this Chassis element 5008fb49dd6SShawn McCarney std::string sensorPath = *chassisPath + "/all_sensors"; 50155c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 50249c53ac9SJohnathan Mantey [sensorsAsyncResp, callback{std::move(callback)}]( 503271584abSEd Tanous const boost::system::error_code& e, 50449c53ac9SJohnathan Mantey const std::variant<std::vector<std::string>>& 50549c53ac9SJohnathan Mantey variantEndpoints) { 506271584abSEd Tanous if (e) 50749c53ac9SJohnathan Mantey { 508271584abSEd Tanous if (e.value() != EBADR) 50949c53ac9SJohnathan Mantey { 51049c53ac9SJohnathan Mantey messages::internalError(sensorsAsyncResp->res); 51149c53ac9SJohnathan Mantey return; 51249c53ac9SJohnathan Mantey } 51349c53ac9SJohnathan Mantey } 51449c53ac9SJohnathan Mantey const std::vector<std::string>* nodeSensorList = 51549c53ac9SJohnathan Mantey std::get_if<std::vector<std::string>>(&(variantEndpoints)); 51649c53ac9SJohnathan Mantey if (nodeSensorList == nullptr) 51749c53ac9SJohnathan Mantey { 51849c53ac9SJohnathan Mantey messages::resourceNotFound( 51949c53ac9SJohnathan Mantey sensorsAsyncResp->res, sensorsAsyncResp->chassisSubNode, 520a0ec28b6SAdrian Ambrożewicz sensorsAsyncResp->chassisSubNode == 521a0ec28b6SAdrian Ambrożewicz sensors::node::thermal 52249c53ac9SJohnathan Mantey ? "Temperatures" 523a0ec28b6SAdrian Ambrożewicz : sensorsAsyncResp->chassisSubNode == 524a0ec28b6SAdrian Ambrożewicz sensors::node::power 52595a3ecadSAnthony Wilson ? "Voltages" 52695a3ecadSAnthony Wilson : "Sensors"); 52749c53ac9SJohnathan Mantey return; 52849c53ac9SJohnathan Mantey } 52949c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> 53049c53ac9SJohnathan Mantey culledSensorList = std::make_shared< 53149c53ac9SJohnathan Mantey boost::container::flat_set<std::string>>(); 53249c53ac9SJohnathan Mantey reduceSensorList(sensorsAsyncResp, nodeSensorList, 53349c53ac9SJohnathan Mantey culledSensorList); 53449c53ac9SJohnathan Mantey callback(culledSensorList); 53549c53ac9SJohnathan Mantey }, 53649c53ac9SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", sensorPath, 53749c53ac9SJohnathan Mantey "org.freedesktop.DBus.Properties", "Get", 53849c53ac9SJohnathan Mantey "xyz.openbmc_project.Association", "endpoints"); 53949c53ac9SJohnathan Mantey }; 54049c53ac9SJohnathan Mantey 54149c53ac9SJohnathan Mantey // Get the Chassis Collection 54249c53ac9SJohnathan Mantey crow::connections::systemBus->async_method_call( 54349c53ac9SJohnathan Mantey respHandler, "xyz.openbmc_project.ObjectMapper", 54449c53ac9SJohnathan Mantey "/xyz/openbmc_project/object_mapper", 54549c53ac9SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 546271584abSEd Tanous "/xyz/openbmc_project/inventory", 0, interfaces); 54755c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis exit"; 54808777fb0SLewanczyk, Dawid } 54908777fb0SLewanczyk, Dawid 55008777fb0SLewanczyk, Dawid /** 551de629b6eSShawn McCarney * @brief Finds all DBus object paths that implement ObjectManager. 552de629b6eSShawn McCarney * 553de629b6eSShawn McCarney * Creates a mapping from the associated connection name to the object path. 554de629b6eSShawn McCarney * 555de629b6eSShawn McCarney * Finds the object paths asynchronously. Invokes callback when information has 556de629b6eSShawn McCarney * been obtained. 557de629b6eSShawn McCarney * 558de629b6eSShawn McCarney * The callback must have the following signature: 559de629b6eSShawn McCarney * @code 5608fb49dd6SShawn McCarney * callback(std::shared_ptr<boost::container::flat_map<std::string, 5618fb49dd6SShawn McCarney * std::string>> objectMgrPaths) 562de629b6eSShawn McCarney * @endcode 563de629b6eSShawn McCarney * 56449c53ac9SJohnathan Mantey * @param sensorsAsyncResp Pointer to object holding response data. 565de629b6eSShawn McCarney * @param callback Callback to invoke when object paths obtained. 566de629b6eSShawn McCarney */ 567de629b6eSShawn McCarney template <typename Callback> 568de629b6eSShawn McCarney void getObjectManagerPaths(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp, 569de629b6eSShawn McCarney Callback&& callback) 570de629b6eSShawn McCarney { 571de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter"; 572de629b6eSShawn McCarney const std::array<std::string, 1> interfaces = { 573de629b6eSShawn McCarney "org.freedesktop.DBus.ObjectManager"}; 574de629b6eSShawn McCarney 575de629b6eSShawn McCarney // Response handler for GetSubTree DBus method 576de629b6eSShawn McCarney auto respHandler = [callback{std::move(callback)}, 577de629b6eSShawn McCarney SensorsAsyncResp](const boost::system::error_code ec, 578de629b6eSShawn McCarney const GetSubTreeType& subtree) { 579de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter"; 580de629b6eSShawn McCarney if (ec) 581de629b6eSShawn McCarney { 582de629b6eSShawn McCarney messages::internalError(SensorsAsyncResp->res); 583de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error " 584de629b6eSShawn McCarney << ec; 585de629b6eSShawn McCarney return; 586de629b6eSShawn McCarney } 587de629b6eSShawn McCarney 588de629b6eSShawn McCarney // Loop over returned object paths 5898fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 5908fb49dd6SShawn McCarney objectMgrPaths = std::make_shared< 5918fb49dd6SShawn McCarney boost::container::flat_map<std::string, std::string>>(); 592de629b6eSShawn McCarney for (const std::pair< 593de629b6eSShawn McCarney std::string, 594de629b6eSShawn McCarney std::vector<std::pair<std::string, std::vector<std::string>>>>& 595de629b6eSShawn McCarney object : subtree) 596de629b6eSShawn McCarney { 597de629b6eSShawn McCarney // Loop over connections for current object path 598de629b6eSShawn McCarney const std::string& objectPath = object.first; 599de629b6eSShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 600de629b6eSShawn McCarney objData : object.second) 601de629b6eSShawn McCarney { 602de629b6eSShawn McCarney // Add mapping from connection to object path 603de629b6eSShawn McCarney const std::string& connection = objData.first; 6048fb49dd6SShawn McCarney (*objectMgrPaths)[connection] = objectPath; 605de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> " 606de629b6eSShawn McCarney << objectPath; 607de629b6eSShawn McCarney } 608de629b6eSShawn McCarney } 6098fb49dd6SShawn McCarney callback(objectMgrPaths); 610de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit"; 611de629b6eSShawn McCarney }; 612de629b6eSShawn McCarney 613de629b6eSShawn McCarney // Query mapper for all DBus object paths that implement ObjectManager 614de629b6eSShawn McCarney crow::connections::systemBus->async_method_call( 615de629b6eSShawn McCarney std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 616de629b6eSShawn McCarney "/xyz/openbmc_project/object_mapper", 617271584abSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces); 618de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit"; 619de629b6eSShawn McCarney } 620de629b6eSShawn McCarney 621de629b6eSShawn McCarney /** 622adc4f0dbSShawn McCarney * @brief Returns the Redfish State value for the specified inventory item. 623adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with a sensor. 624adc4f0dbSShawn McCarney * @return State value for inventory item. 62534dd179eSJames Feist */ 626*23a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem) 627adc4f0dbSShawn McCarney { 628adc4f0dbSShawn McCarney if ((inventoryItem != nullptr) && !(inventoryItem->isPresent)) 629adc4f0dbSShawn McCarney { 630adc4f0dbSShawn McCarney return "Absent"; 631adc4f0dbSShawn McCarney } 63234dd179eSJames Feist 633adc4f0dbSShawn McCarney return "Enabled"; 634adc4f0dbSShawn McCarney } 635adc4f0dbSShawn McCarney 636adc4f0dbSShawn McCarney /** 637adc4f0dbSShawn McCarney * @brief Returns the Redfish Health value for the specified sensor. 638adc4f0dbSShawn McCarney * @param sensorJson Sensor JSON object. 639adc4f0dbSShawn McCarney * @param interfacesDict Map of all sensor interfaces. 640adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with the sensor. Will 641adc4f0dbSShawn McCarney * be nullptr if no associated inventory item was found. 642adc4f0dbSShawn McCarney * @return Health value for sensor. 643adc4f0dbSShawn McCarney */ 644*23a21a1cSEd Tanous inline std::string getHealth( 645adc4f0dbSShawn McCarney nlohmann::json& sensorJson, 64634dd179eSJames Feist const boost::container::flat_map< 64734dd179eSJames Feist std::string, boost::container::flat_map<std::string, SensorVariant>>& 648adc4f0dbSShawn McCarney interfacesDict, 649adc4f0dbSShawn McCarney const InventoryItem* inventoryItem) 65034dd179eSJames Feist { 651adc4f0dbSShawn McCarney // Get current health value (if any) in the sensor JSON object. Some JSON 652adc4f0dbSShawn McCarney // objects contain multiple sensors (such as PowerSupplies). We want to set 653adc4f0dbSShawn McCarney // the overall health to be the most severe of any of the sensors. 654adc4f0dbSShawn McCarney std::string currentHealth; 655adc4f0dbSShawn McCarney auto statusIt = sensorJson.find("Status"); 656adc4f0dbSShawn McCarney if (statusIt != sensorJson.end()) 657adc4f0dbSShawn McCarney { 658adc4f0dbSShawn McCarney auto healthIt = statusIt->find("Health"); 659adc4f0dbSShawn McCarney if (healthIt != statusIt->end()) 660adc4f0dbSShawn McCarney { 661adc4f0dbSShawn McCarney std::string* health = healthIt->get_ptr<std::string*>(); 662adc4f0dbSShawn McCarney if (health != nullptr) 663adc4f0dbSShawn McCarney { 664adc4f0dbSShawn McCarney currentHealth = *health; 665adc4f0dbSShawn McCarney } 666adc4f0dbSShawn McCarney } 667adc4f0dbSShawn McCarney } 668adc4f0dbSShawn McCarney 669adc4f0dbSShawn McCarney // If current health in JSON object is already Critical, return that. This 670adc4f0dbSShawn McCarney // should override the sensor health, which might be less severe. 671adc4f0dbSShawn McCarney if (currentHealth == "Critical") 672adc4f0dbSShawn McCarney { 673adc4f0dbSShawn McCarney return "Critical"; 674adc4f0dbSShawn McCarney } 675adc4f0dbSShawn McCarney 676adc4f0dbSShawn McCarney // Check if sensor has critical threshold alarm 67734dd179eSJames Feist auto criticalThresholdIt = 67834dd179eSJames Feist interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Critical"); 67934dd179eSJames Feist if (criticalThresholdIt != interfacesDict.end()) 68034dd179eSJames Feist { 68134dd179eSJames Feist auto thresholdHighIt = 68234dd179eSJames Feist criticalThresholdIt->second.find("CriticalAlarmHigh"); 68334dd179eSJames Feist auto thresholdLowIt = 68434dd179eSJames Feist criticalThresholdIt->second.find("CriticalAlarmLow"); 68534dd179eSJames Feist if (thresholdHighIt != criticalThresholdIt->second.end()) 68634dd179eSJames Feist { 68734dd179eSJames Feist const bool* asserted = std::get_if<bool>(&thresholdHighIt->second); 68834dd179eSJames Feist if (asserted == nullptr) 68934dd179eSJames Feist { 69034dd179eSJames Feist BMCWEB_LOG_ERROR << "Illegal sensor threshold"; 69134dd179eSJames Feist } 69234dd179eSJames Feist else if (*asserted) 69334dd179eSJames Feist { 69434dd179eSJames Feist return "Critical"; 69534dd179eSJames Feist } 69634dd179eSJames Feist } 69734dd179eSJames Feist if (thresholdLowIt != criticalThresholdIt->second.end()) 69834dd179eSJames Feist { 69934dd179eSJames Feist const bool* asserted = std::get_if<bool>(&thresholdLowIt->second); 70034dd179eSJames Feist if (asserted == nullptr) 70134dd179eSJames Feist { 70234dd179eSJames Feist BMCWEB_LOG_ERROR << "Illegal sensor threshold"; 70334dd179eSJames Feist } 70434dd179eSJames Feist else if (*asserted) 70534dd179eSJames Feist { 70634dd179eSJames Feist return "Critical"; 70734dd179eSJames Feist } 70834dd179eSJames Feist } 70934dd179eSJames Feist } 71034dd179eSJames Feist 711adc4f0dbSShawn McCarney // Check if associated inventory item is not functional 712adc4f0dbSShawn McCarney if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional)) 713adc4f0dbSShawn McCarney { 714adc4f0dbSShawn McCarney return "Critical"; 715adc4f0dbSShawn McCarney } 716adc4f0dbSShawn McCarney 717adc4f0dbSShawn McCarney // If current health in JSON object is already Warning, return that. This 718adc4f0dbSShawn McCarney // should override the sensor status, which might be less severe. 719adc4f0dbSShawn McCarney if (currentHealth == "Warning") 720adc4f0dbSShawn McCarney { 721adc4f0dbSShawn McCarney return "Warning"; 722adc4f0dbSShawn McCarney } 723adc4f0dbSShawn McCarney 724adc4f0dbSShawn McCarney // Check if sensor has warning threshold alarm 72534dd179eSJames Feist auto warningThresholdIt = 72634dd179eSJames Feist interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Warning"); 72734dd179eSJames Feist if (warningThresholdIt != interfacesDict.end()) 72834dd179eSJames Feist { 72934dd179eSJames Feist auto thresholdHighIt = 73034dd179eSJames Feist warningThresholdIt->second.find("WarningAlarmHigh"); 73134dd179eSJames Feist auto thresholdLowIt = 73234dd179eSJames Feist warningThresholdIt->second.find("WarningAlarmLow"); 73334dd179eSJames Feist if (thresholdHighIt != warningThresholdIt->second.end()) 73434dd179eSJames Feist { 73534dd179eSJames Feist const bool* asserted = std::get_if<bool>(&thresholdHighIt->second); 73634dd179eSJames Feist if (asserted == nullptr) 73734dd179eSJames Feist { 73834dd179eSJames Feist BMCWEB_LOG_ERROR << "Illegal sensor threshold"; 73934dd179eSJames Feist } 74034dd179eSJames Feist else if (*asserted) 74134dd179eSJames Feist { 74234dd179eSJames Feist return "Warning"; 74334dd179eSJames Feist } 74434dd179eSJames Feist } 74534dd179eSJames Feist if (thresholdLowIt != warningThresholdIt->second.end()) 74634dd179eSJames Feist { 74734dd179eSJames Feist const bool* asserted = std::get_if<bool>(&thresholdLowIt->second); 74834dd179eSJames Feist if (asserted == nullptr) 74934dd179eSJames Feist { 75034dd179eSJames Feist BMCWEB_LOG_ERROR << "Illegal sensor threshold"; 75134dd179eSJames Feist } 75234dd179eSJames Feist else if (*asserted) 75334dd179eSJames Feist { 75434dd179eSJames Feist return "Warning"; 75534dd179eSJames Feist } 75634dd179eSJames Feist } 75734dd179eSJames Feist } 758adc4f0dbSShawn McCarney 75934dd179eSJames Feist return "OK"; 76034dd179eSJames Feist } 76134dd179eSJames Feist 762*23a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson, 763d500549bSAnthony Wilson const InventoryItem* inventoryItem) 764d500549bSAnthony Wilson { 765d500549bSAnthony Wilson if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty()) 766d500549bSAnthony Wilson { 767d500549bSAnthony Wilson switch (inventoryItem->ledState) 768d500549bSAnthony Wilson { 769d500549bSAnthony Wilson case LedState::OFF: 770d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Off"; 771d500549bSAnthony Wilson break; 772d500549bSAnthony Wilson case LedState::ON: 773d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Lit"; 774d500549bSAnthony Wilson break; 775d500549bSAnthony Wilson case LedState::BLINK: 776d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Blinking"; 777d500549bSAnthony Wilson break; 778*23a21a1cSEd Tanous case LedState::UNKNOWN: 779d500549bSAnthony Wilson break; 780d500549bSAnthony Wilson } 781d500549bSAnthony Wilson } 782d500549bSAnthony Wilson } 783d500549bSAnthony Wilson 78434dd179eSJames Feist /** 78508777fb0SLewanczyk, Dawid * @brief Builds a json sensor representation of a sensor. 78608777fb0SLewanczyk, Dawid * @param sensorName The name of the sensor to be built 787274fad5aSGunnar Mills * @param sensorType The type (temperature, fan_tach, etc) of the sensor to 78808777fb0SLewanczyk, Dawid * build 789a0ec28b6SAdrian Ambrożewicz * @param sensorsAsyncResp Sensor metadata 79008777fb0SLewanczyk, Dawid * @param interfacesDict A dictionary of the interfaces and properties of said 79108777fb0SLewanczyk, Dawid * interfaces to be built from 79208777fb0SLewanczyk, Dawid * @param sensor_json The json object to fill 793adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with the sensor. Will 794adc4f0dbSShawn McCarney * be nullptr if no associated inventory item was found. 79508777fb0SLewanczyk, Dawid */ 796*23a21a1cSEd Tanous inline void objectInterfacesToJson( 79708777fb0SLewanczyk, Dawid const std::string& sensorName, const std::string& sensorType, 798a0ec28b6SAdrian Ambrożewicz std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 79908777fb0SLewanczyk, Dawid const boost::container::flat_map< 800aa2e59c1SEd Tanous std::string, boost::container::flat_map<std::string, SensorVariant>>& 80108777fb0SLewanczyk, Dawid interfacesDict, 802adc4f0dbSShawn McCarney nlohmann::json& sensor_json, InventoryItem* inventoryItem) 8031abe55efSEd Tanous { 80408777fb0SLewanczyk, Dawid // We need a value interface before we can do anything with it 80555c7b7a2SEd Tanous auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value"); 8061abe55efSEd Tanous if (valueIt == interfacesDict.end()) 8071abe55efSEd Tanous { 80855c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface"; 80908777fb0SLewanczyk, Dawid return; 81008777fb0SLewanczyk, Dawid } 81108777fb0SLewanczyk, Dawid 81208777fb0SLewanczyk, Dawid // Assume values exist as is (10^0 == 1) if no scale exists 81308777fb0SLewanczyk, Dawid int64_t scaleMultiplier = 0; 81408777fb0SLewanczyk, Dawid 81555c7b7a2SEd Tanous auto scaleIt = valueIt->second.find("Scale"); 81608777fb0SLewanczyk, Dawid // If a scale exists, pull value as int64, and use the scaling. 8171abe55efSEd Tanous if (scaleIt != valueIt->second.end()) 8181abe55efSEd Tanous { 819abf2add6SEd Tanous const int64_t* int64Value = std::get_if<int64_t>(&scaleIt->second); 8201abe55efSEd Tanous if (int64Value != nullptr) 8211abe55efSEd Tanous { 82208777fb0SLewanczyk, Dawid scaleMultiplier = *int64Value; 82308777fb0SLewanczyk, Dawid } 82408777fb0SLewanczyk, Dawid } 82508777fb0SLewanczyk, Dawid 826a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors) 827adc4f0dbSShawn McCarney { 82895a3ecadSAnthony Wilson // For sensors in SensorCollection we set Id instead of MemberId, 82995a3ecadSAnthony Wilson // including power sensors. 83095a3ecadSAnthony Wilson sensor_json["Id"] = sensorName; 83195a3ecadSAnthony Wilson sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " "); 83295a3ecadSAnthony Wilson } 83395a3ecadSAnthony Wilson else if (sensorType != "power") 83495a3ecadSAnthony Wilson { 83595a3ecadSAnthony Wilson // Set MemberId and Name for non-power sensors. For PowerSupplies and 83695a3ecadSAnthony Wilson // PowerControl, those properties have more general values because 83795a3ecadSAnthony Wilson // multiple sensors can be stored in the same JSON object. 83808777fb0SLewanczyk, Dawid sensor_json["MemberId"] = sensorName; 839e742b6ccSEd Tanous sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " "); 840adc4f0dbSShawn McCarney } 841e742b6ccSEd Tanous 842adc4f0dbSShawn McCarney sensor_json["Status"]["State"] = getState(inventoryItem); 843adc4f0dbSShawn McCarney sensor_json["Status"]["Health"] = 844adc4f0dbSShawn McCarney getHealth(sensor_json, interfacesDict, inventoryItem); 84508777fb0SLewanczyk, Dawid 84608777fb0SLewanczyk, Dawid // Parameter to set to override the type we get from dbus, and force it to 84708777fb0SLewanczyk, Dawid // int, regardless of what is available. This is used for schemas like fan, 84808777fb0SLewanczyk, Dawid // that require integers, not floats. 84908777fb0SLewanczyk, Dawid bool forceToInt = false; 85008777fb0SLewanczyk, Dawid 8513929aca1SAnthony Wilson nlohmann::json::json_pointer unit("/Reading"); 852a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors) 85395a3ecadSAnthony Wilson { 85495a3ecadSAnthony Wilson sensor_json["@odata.type"] = "#Sensor.v1_0_0.Sensor"; 85595a3ecadSAnthony Wilson if (sensorType == "power") 85695a3ecadSAnthony Wilson { 85795a3ecadSAnthony Wilson sensor_json["ReadingUnits"] = "Watts"; 85895a3ecadSAnthony Wilson } 85995a3ecadSAnthony Wilson else if (sensorType == "current") 86095a3ecadSAnthony Wilson { 86195a3ecadSAnthony Wilson sensor_json["ReadingUnits"] = "Amperes"; 86295a3ecadSAnthony Wilson } 863f8ede15eSAdrian Ambrożewicz else if (sensorType == "utilization") 864f8ede15eSAdrian Ambrożewicz { 865f8ede15eSAdrian Ambrożewicz sensor_json["ReadingUnits"] = "Percent"; 866f8ede15eSAdrian Ambrożewicz } 86795a3ecadSAnthony Wilson } 86895a3ecadSAnthony Wilson else if (sensorType == "temperature") 8691abe55efSEd Tanous { 8703929aca1SAnthony Wilson unit = "/ReadingCelsius"_json_pointer; 8717885954aSLewanczyk, Dawid sensor_json["@odata.type"] = "#Thermal.v1_3_0.Temperature"; 87208777fb0SLewanczyk, Dawid // TODO(ed) Documentation says that path should be type fan_tach, 87308777fb0SLewanczyk, Dawid // implementation seems to implement fan 8741abe55efSEd Tanous } 8751abe55efSEd Tanous else if (sensorType == "fan" || sensorType == "fan_tach") 8761abe55efSEd Tanous { 8773929aca1SAnthony Wilson unit = "/Reading"_json_pointer; 87808777fb0SLewanczyk, Dawid sensor_json["ReadingUnits"] = "RPM"; 8797885954aSLewanczyk, Dawid sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan"; 880d500549bSAnthony Wilson setLedState(sensor_json, inventoryItem); 88108777fb0SLewanczyk, Dawid forceToInt = true; 8821abe55efSEd Tanous } 8836f6d0d32SEd Tanous else if (sensorType == "fan_pwm") 8846f6d0d32SEd Tanous { 8853929aca1SAnthony Wilson unit = "/Reading"_json_pointer; 8866f6d0d32SEd Tanous sensor_json["ReadingUnits"] = "Percent"; 8876f6d0d32SEd Tanous sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan"; 888d500549bSAnthony Wilson setLedState(sensor_json, inventoryItem); 8896f6d0d32SEd Tanous forceToInt = true; 8906f6d0d32SEd Tanous } 8911abe55efSEd Tanous else if (sensorType == "voltage") 8921abe55efSEd Tanous { 8933929aca1SAnthony Wilson unit = "/ReadingVolts"_json_pointer; 8947885954aSLewanczyk, Dawid sensor_json["@odata.type"] = "#Power.v1_0_0.Voltage"; 8951abe55efSEd Tanous } 8962474adfaSEd Tanous else if (sensorType == "power") 8972474adfaSEd Tanous { 89849c53ac9SJohnathan Mantey std::string sensorNameLower = 89949c53ac9SJohnathan Mantey boost::algorithm::to_lower_copy(sensorName); 90049c53ac9SJohnathan Mantey 901028f7ebcSEddie James if (!sensorName.compare("total_power")) 902028f7ebcSEddie James { 9037ab06f49SGunnar Mills sensor_json["@odata.type"] = "#Power.v1_0_0.PowerControl"; 9047ab06f49SGunnar Mills // Put multiple "sensors" into a single PowerControl, so have 9057ab06f49SGunnar Mills // generic names for MemberId and Name. Follows Redfish mockup. 9067ab06f49SGunnar Mills sensor_json["MemberId"] = "0"; 9077ab06f49SGunnar Mills sensor_json["Name"] = "Chassis Power Control"; 9083929aca1SAnthony Wilson unit = "/PowerConsumedWatts"_json_pointer; 909028f7ebcSEddie James } 910028f7ebcSEddie James else if (sensorNameLower.find("input") != std::string::npos) 91149c53ac9SJohnathan Mantey { 9123929aca1SAnthony Wilson unit = "/PowerInputWatts"_json_pointer; 91349c53ac9SJohnathan Mantey } 91449c53ac9SJohnathan Mantey else 91549c53ac9SJohnathan Mantey { 9163929aca1SAnthony Wilson unit = "/PowerOutputWatts"_json_pointer; 91749c53ac9SJohnathan Mantey } 9182474adfaSEd Tanous } 9191abe55efSEd Tanous else 9201abe55efSEd Tanous { 92155c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName; 92208777fb0SLewanczyk, Dawid return; 92308777fb0SLewanczyk, Dawid } 92408777fb0SLewanczyk, Dawid // Map of dbus interface name, dbus property name and redfish property_name 9253929aca1SAnthony Wilson std::vector< 9263929aca1SAnthony Wilson std::tuple<const char*, const char*, nlohmann::json::json_pointer>> 9273929aca1SAnthony Wilson properties; 92808777fb0SLewanczyk, Dawid properties.reserve(7); 92908777fb0SLewanczyk, Dawid 93008777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit); 931de629b6eSShawn McCarney 932a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors) 9333929aca1SAnthony Wilson { 9343929aca1SAnthony Wilson properties.emplace_back( 9353929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh", 9363929aca1SAnthony Wilson "/Thresholds/UpperCaution/Reading"_json_pointer); 9373929aca1SAnthony Wilson properties.emplace_back( 9383929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow", 9393929aca1SAnthony Wilson "/Thresholds/LowerCaution/Reading"_json_pointer); 9403929aca1SAnthony Wilson properties.emplace_back( 9413929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh", 9423929aca1SAnthony Wilson "/Thresholds/UpperCritical/Reading"_json_pointer); 9433929aca1SAnthony Wilson properties.emplace_back( 9443929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow", 9453929aca1SAnthony Wilson "/Thresholds/LowerCritical/Reading"_json_pointer); 9463929aca1SAnthony Wilson } 9473929aca1SAnthony Wilson else if (sensorType != "power") 948de629b6eSShawn McCarney { 94908777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 9503929aca1SAnthony Wilson "WarningHigh", 9513929aca1SAnthony Wilson "/UpperThresholdNonCritical"_json_pointer); 95208777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 9533929aca1SAnthony Wilson "WarningLow", 9543929aca1SAnthony Wilson "/LowerThresholdNonCritical"_json_pointer); 95508777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 9563929aca1SAnthony Wilson "CriticalHigh", 9573929aca1SAnthony Wilson "/UpperThresholdCritical"_json_pointer); 95808777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 9593929aca1SAnthony Wilson "CriticalLow", 9603929aca1SAnthony Wilson "/LowerThresholdCritical"_json_pointer); 961de629b6eSShawn McCarney } 96208777fb0SLewanczyk, Dawid 9632474adfaSEd Tanous // TODO Need to get UpperThresholdFatal and LowerThresholdFatal 9642474adfaSEd Tanous 965a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors) 96695a3ecadSAnthony Wilson { 96795a3ecadSAnthony Wilson properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 9683929aca1SAnthony Wilson "/ReadingRangeMin"_json_pointer); 96995a3ecadSAnthony Wilson properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 9703929aca1SAnthony Wilson "/ReadingRangeMax"_json_pointer); 97195a3ecadSAnthony Wilson } 97295a3ecadSAnthony Wilson else if (sensorType == "temperature") 9731abe55efSEd Tanous { 97408777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 9753929aca1SAnthony Wilson "/MinReadingRangeTemp"_json_pointer); 97608777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 9773929aca1SAnthony Wilson "/MaxReadingRangeTemp"_json_pointer); 9781abe55efSEd Tanous } 979adc4f0dbSShawn McCarney else if (sensorType != "power") 9801abe55efSEd Tanous { 98108777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 9823929aca1SAnthony Wilson "/MinReadingRange"_json_pointer); 98308777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 9843929aca1SAnthony Wilson "/MaxReadingRange"_json_pointer); 98508777fb0SLewanczyk, Dawid } 98608777fb0SLewanczyk, Dawid 9873929aca1SAnthony Wilson for (const std::tuple<const char*, const char*, 9883929aca1SAnthony Wilson nlohmann::json::json_pointer>& p : properties) 9891abe55efSEd Tanous { 99008777fb0SLewanczyk, Dawid auto interfaceProperties = interfacesDict.find(std::get<0>(p)); 9911abe55efSEd Tanous if (interfaceProperties != interfacesDict.end()) 9921abe55efSEd Tanous { 993271584abSEd Tanous auto thisValueIt = interfaceProperties->second.find(std::get<1>(p)); 994271584abSEd Tanous if (thisValueIt != interfaceProperties->second.end()) 9951abe55efSEd Tanous { 996271584abSEd Tanous const SensorVariant& valueVariant = thisValueIt->second; 9973929aca1SAnthony Wilson 9983929aca1SAnthony Wilson // The property we want to set may be nested json, so use 9993929aca1SAnthony Wilson // a json_pointer for easy indexing into the json structure. 10003929aca1SAnthony Wilson const nlohmann::json::json_pointer& key = std::get<2>(p); 10013929aca1SAnthony Wilson 100208777fb0SLewanczyk, Dawid // Attempt to pull the int64 directly 1003abf2add6SEd Tanous const int64_t* int64Value = std::get_if<int64_t>(&valueVariant); 100408777fb0SLewanczyk, Dawid 1005abf2add6SEd Tanous const double* doubleValue = std::get_if<double>(&valueVariant); 1006028f7ebcSEddie James const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant); 10076f6d0d32SEd Tanous double temp = 0.0; 10086f6d0d32SEd Tanous if (int64Value != nullptr) 10091abe55efSEd Tanous { 1010271584abSEd Tanous temp = static_cast<double>(*int64Value); 10116f6d0d32SEd Tanous } 10126f6d0d32SEd Tanous else if (doubleValue != nullptr) 10131abe55efSEd Tanous { 10146f6d0d32SEd Tanous temp = *doubleValue; 10151abe55efSEd Tanous } 1016028f7ebcSEddie James else if (uValue != nullptr) 1017028f7ebcSEddie James { 1018028f7ebcSEddie James temp = *uValue; 1019028f7ebcSEddie James } 10201abe55efSEd Tanous else 10211abe55efSEd Tanous { 10226f6d0d32SEd Tanous BMCWEB_LOG_ERROR 10236f6d0d32SEd Tanous << "Got value interface that wasn't int or double"; 10246f6d0d32SEd Tanous continue; 102508777fb0SLewanczyk, Dawid } 10266f6d0d32SEd Tanous temp = temp * std::pow(10, scaleMultiplier); 10276f6d0d32SEd Tanous if (forceToInt) 10286f6d0d32SEd Tanous { 10293929aca1SAnthony Wilson sensor_json[key] = static_cast<int64_t>(temp); 10306f6d0d32SEd Tanous } 10316f6d0d32SEd Tanous else 10326f6d0d32SEd Tanous { 10333929aca1SAnthony Wilson sensor_json[key] = temp; 103408777fb0SLewanczyk, Dawid } 103508777fb0SLewanczyk, Dawid } 103608777fb0SLewanczyk, Dawid } 103708777fb0SLewanczyk, Dawid } 1038a0ec28b6SAdrian Ambrożewicz 1039a0ec28b6SAdrian Ambrożewicz sensorsAsyncResp->addMetadata(sensor_json, unit.to_string(), 1040a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/" + sensorType + 1041a0ec28b6SAdrian Ambrożewicz "/" + sensorName); 1042a0ec28b6SAdrian Ambrożewicz 104355c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Added sensor " << sensorName; 104408777fb0SLewanczyk, Dawid } 104508777fb0SLewanczyk, Dawid 1046*23a21a1cSEd Tanous inline void 10478bd25ccdSJames Feist populateFanRedundancy(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp) 10488bd25ccdSJames Feist { 10498bd25ccdSJames Feist crow::connections::systemBus->async_method_call( 10508bd25ccdSJames Feist [sensorsAsyncResp](const boost::system::error_code ec, 10518bd25ccdSJames Feist const GetSubTreeType& resp) { 10528bd25ccdSJames Feist if (ec) 10538bd25ccdSJames Feist { 10548bd25ccdSJames Feist return; // don't have to have this interface 10558bd25ccdSJames Feist } 1056e278c18fSEd Tanous for (const std::pair<std::string, 1057e278c18fSEd Tanous std::vector<std::pair< 1058e278c18fSEd Tanous std::string, std::vector<std::string>>>>& 1059e278c18fSEd Tanous pathPair : resp) 10608bd25ccdSJames Feist { 1061e278c18fSEd Tanous const std::string& path = pathPair.first; 1062e278c18fSEd Tanous const std::vector< 1063e278c18fSEd Tanous std::pair<std::string, std::vector<std::string>>>& objDict = 1064e278c18fSEd Tanous pathPair.second; 10658bd25ccdSJames Feist if (objDict.empty()) 10668bd25ccdSJames Feist { 10678bd25ccdSJames Feist continue; // this should be impossible 10688bd25ccdSJames Feist } 10698bd25ccdSJames Feist 10708bd25ccdSJames Feist const std::string& owner = objDict.begin()->first; 10718bd25ccdSJames Feist crow::connections::systemBus->async_method_call( 10728bd25ccdSJames Feist [path, owner, 1073271584abSEd Tanous sensorsAsyncResp](const boost::system::error_code e, 10748bd25ccdSJames Feist std::variant<std::vector<std::string>> 10758bd25ccdSJames Feist variantEndpoints) { 1076271584abSEd Tanous if (e) 10778bd25ccdSJames Feist { 10788bd25ccdSJames Feist return; // if they don't have an association we 10798bd25ccdSJames Feist // can't tell what chassis is 10808bd25ccdSJames Feist } 10818bd25ccdSJames Feist // verify part of the right chassis 10828bd25ccdSJames Feist auto endpoints = std::get_if<std::vector<std::string>>( 10838bd25ccdSJames Feist &variantEndpoints); 10848bd25ccdSJames Feist 10858bd25ccdSJames Feist if (endpoints == nullptr) 10868bd25ccdSJames Feist { 10878bd25ccdSJames Feist BMCWEB_LOG_ERROR << "Invalid association interface"; 10888bd25ccdSJames Feist messages::internalError(sensorsAsyncResp->res); 10898bd25ccdSJames Feist return; 10908bd25ccdSJames Feist } 10918bd25ccdSJames Feist 10928bd25ccdSJames Feist auto found = std::find_if( 10938bd25ccdSJames Feist endpoints->begin(), endpoints->end(), 10948bd25ccdSJames Feist [sensorsAsyncResp](const std::string& entry) { 10958bd25ccdSJames Feist return entry.find( 10968bd25ccdSJames Feist sensorsAsyncResp->chassisId) != 10978bd25ccdSJames Feist std::string::npos; 10988bd25ccdSJames Feist }); 10998bd25ccdSJames Feist 11008bd25ccdSJames Feist if (found == endpoints->end()) 11018bd25ccdSJames Feist { 11028bd25ccdSJames Feist return; 11038bd25ccdSJames Feist } 11048bd25ccdSJames Feist crow::connections::systemBus->async_method_call( 11058bd25ccdSJames Feist [path, sensorsAsyncResp]( 1106271584abSEd Tanous const boost::system::error_code& err, 11078bd25ccdSJames Feist const boost::container::flat_map< 11088bd25ccdSJames Feist std::string, 11098bd25ccdSJames Feist std::variant<uint8_t, 11108bd25ccdSJames Feist std::vector<std::string>, 11118bd25ccdSJames Feist std::string>>& ret) { 1112271584abSEd Tanous if (err) 11138bd25ccdSJames Feist { 11148bd25ccdSJames Feist return; // don't have to have this 11158bd25ccdSJames Feist // interface 11168bd25ccdSJames Feist } 11178bd25ccdSJames Feist auto findFailures = ret.find("AllowedFailures"); 11188bd25ccdSJames Feist auto findCollection = ret.find("Collection"); 11198bd25ccdSJames Feist auto findStatus = ret.find("Status"); 11208bd25ccdSJames Feist 11218bd25ccdSJames Feist if (findFailures == ret.end() || 11228bd25ccdSJames Feist findCollection == ret.end() || 11238bd25ccdSJames Feist findStatus == ret.end()) 11248bd25ccdSJames Feist { 11258bd25ccdSJames Feist BMCWEB_LOG_ERROR 11268bd25ccdSJames Feist << "Invalid redundancy interface"; 11278bd25ccdSJames Feist messages::internalError( 11288bd25ccdSJames Feist sensorsAsyncResp->res); 11298bd25ccdSJames Feist return; 11308bd25ccdSJames Feist } 11318bd25ccdSJames Feist 11328bd25ccdSJames Feist auto allowedFailures = std::get_if<uint8_t>( 11338bd25ccdSJames Feist &(findFailures->second)); 11348bd25ccdSJames Feist auto collection = 11358bd25ccdSJames Feist std::get_if<std::vector<std::string>>( 11368bd25ccdSJames Feist &(findCollection->second)); 11378bd25ccdSJames Feist auto status = std::get_if<std::string>( 11388bd25ccdSJames Feist &(findStatus->second)); 11398bd25ccdSJames Feist 11408bd25ccdSJames Feist if (allowedFailures == nullptr || 11418bd25ccdSJames Feist collection == nullptr || status == nullptr) 11428bd25ccdSJames Feist { 11438bd25ccdSJames Feist 11448bd25ccdSJames Feist BMCWEB_LOG_ERROR 11458bd25ccdSJames Feist << "Invalid redundancy interface " 11468bd25ccdSJames Feist "types"; 11478bd25ccdSJames Feist messages::internalError( 11488bd25ccdSJames Feist sensorsAsyncResp->res); 11498bd25ccdSJames Feist return; 11508bd25ccdSJames Feist } 11518bd25ccdSJames Feist size_t lastSlash = path.rfind("/"); 11528bd25ccdSJames Feist if (lastSlash == std::string::npos) 11538bd25ccdSJames Feist { 11548bd25ccdSJames Feist // this should be impossible 11558bd25ccdSJames Feist messages::internalError( 11568bd25ccdSJames Feist sensorsAsyncResp->res); 11578bd25ccdSJames Feist return; 11588bd25ccdSJames Feist } 11598bd25ccdSJames Feist std::string name = path.substr(lastSlash + 1); 11608bd25ccdSJames Feist std::replace(name.begin(), name.end(), '_', 11618bd25ccdSJames Feist ' '); 11628bd25ccdSJames Feist 11638bd25ccdSJames Feist std::string health; 11648bd25ccdSJames Feist 11658bd25ccdSJames Feist if (boost::ends_with(*status, "Full")) 11668bd25ccdSJames Feist { 11678bd25ccdSJames Feist health = "OK"; 11688bd25ccdSJames Feist } 11698bd25ccdSJames Feist else if (boost::ends_with(*status, "Degraded")) 11708bd25ccdSJames Feist { 11718bd25ccdSJames Feist health = "Warning"; 11728bd25ccdSJames Feist } 11738bd25ccdSJames Feist else 11748bd25ccdSJames Feist { 11758bd25ccdSJames Feist health = "Critical"; 11768bd25ccdSJames Feist } 11778bd25ccdSJames Feist std::vector<nlohmann::json> redfishCollection; 11788bd25ccdSJames Feist const auto& fanRedfish = 11798bd25ccdSJames Feist sensorsAsyncResp->res.jsonValue["Fans"]; 11808bd25ccdSJames Feist for (const std::string& item : *collection) 11818bd25ccdSJames Feist { 11828bd25ccdSJames Feist lastSlash = item.rfind("/"); 11838bd25ccdSJames Feist // make a copy as collection is const 11848bd25ccdSJames Feist std::string itemName = 11858bd25ccdSJames Feist item.substr(lastSlash + 1); 11868bd25ccdSJames Feist /* 11878bd25ccdSJames Feist todo(ed): merge patch that fixes the names 11888bd25ccdSJames Feist std::replace(itemName.begin(), 11898bd25ccdSJames Feist itemName.end(), '_', ' ');*/ 11908bd25ccdSJames Feist auto schemaItem = std::find_if( 11918bd25ccdSJames Feist fanRedfish.begin(), fanRedfish.end(), 11928bd25ccdSJames Feist [itemName](const nlohmann::json& fan) { 11938bd25ccdSJames Feist return fan["MemberId"] == itemName; 11948bd25ccdSJames Feist }); 11958bd25ccdSJames Feist if (schemaItem != fanRedfish.end()) 11968bd25ccdSJames Feist { 11978bd25ccdSJames Feist redfishCollection.push_back( 11988bd25ccdSJames Feist {{"@odata.id", 11998bd25ccdSJames Feist (*schemaItem)["@odata.id"]}}); 12008bd25ccdSJames Feist } 12018bd25ccdSJames Feist else 12028bd25ccdSJames Feist { 12038bd25ccdSJames Feist BMCWEB_LOG_ERROR 12048bd25ccdSJames Feist << "failed to find fan in schema"; 12058bd25ccdSJames Feist messages::internalError( 12068bd25ccdSJames Feist sensorsAsyncResp->res); 12078bd25ccdSJames Feist return; 12088bd25ccdSJames Feist } 12098bd25ccdSJames Feist } 12108bd25ccdSJames Feist 12113e9e72ebSKuiying Wang size_t minNumNeeded = 12123e9e72ebSKuiying Wang collection->size() > 0 12133e9e72ebSKuiying Wang ? collection->size() - *allowedFailures 12143e9e72ebSKuiying Wang : 0; 1215271584abSEd Tanous nlohmann::json& jResp = 1216271584abSEd Tanous sensorsAsyncResp->res 12178bd25ccdSJames Feist .jsonValue["Redundancy"]; 1218271584abSEd Tanous jResp.push_back( 12198bd25ccdSJames Feist {{"@odata.id", 1220717794d5SAppaRao Puli "/redfish/v1/Chassis/" + 12218bd25ccdSJames Feist sensorsAsyncResp->chassisId + "/" + 12228bd25ccdSJames Feist sensorsAsyncResp->chassisSubNode + 12238bd25ccdSJames Feist "#/Redundancy/" + 1224271584abSEd Tanous std::to_string(jResp.size())}, 12258bd25ccdSJames Feist {"@odata.type", 12268bd25ccdSJames Feist "#Redundancy.v1_3_2.Redundancy"}, 12273e9e72ebSKuiying Wang {"MinNumNeeded", minNumNeeded}, 12288bd25ccdSJames Feist {"MemberId", name}, 12298bd25ccdSJames Feist {"Mode", "N+m"}, 12308bd25ccdSJames Feist {"Name", name}, 12318bd25ccdSJames Feist {"RedundancySet", redfishCollection}, 12328bd25ccdSJames Feist {"Status", 12338bd25ccdSJames Feist {{"Health", health}, 12348bd25ccdSJames Feist {"State", "Enabled"}}}}); 12358bd25ccdSJames Feist }, 12368bd25ccdSJames Feist owner, path, "org.freedesktop.DBus.Properties", 12378bd25ccdSJames Feist "GetAll", 12388bd25ccdSJames Feist "xyz.openbmc_project.Control.FanRedundancy"); 12398bd25ccdSJames Feist }, 124002e92e32SJames Feist "xyz.openbmc_project.ObjectMapper", path + "/chassis", 12418bd25ccdSJames Feist "org.freedesktop.DBus.Properties", "Get", 12428bd25ccdSJames Feist "xyz.openbmc_project.Association", "endpoints"); 12438bd25ccdSJames Feist } 12448bd25ccdSJames Feist }, 12458bd25ccdSJames Feist "xyz.openbmc_project.ObjectMapper", 12468bd25ccdSJames Feist "/xyz/openbmc_project/object_mapper", 12478bd25ccdSJames Feist "xyz.openbmc_project.ObjectMapper", "GetSubTree", 12488bd25ccdSJames Feist "/xyz/openbmc_project/control", 2, 12498bd25ccdSJames Feist std::array<const char*, 1>{ 12508bd25ccdSJames Feist "xyz.openbmc_project.Control.FanRedundancy"}); 12518bd25ccdSJames Feist } 12528bd25ccdSJames Feist 1253*23a21a1cSEd Tanous inline void sortJSONResponse(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp) 125449c53ac9SJohnathan Mantey { 125549c53ac9SJohnathan Mantey nlohmann::json& response = SensorsAsyncResp->res.jsonValue; 125649c53ac9SJohnathan Mantey std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"}; 1257a0ec28b6SAdrian Ambrożewicz if (SensorsAsyncResp->chassisSubNode == sensors::node::power) 125849c53ac9SJohnathan Mantey { 125949c53ac9SJohnathan Mantey sensorHeaders = {"Voltages", "PowerSupplies"}; 126049c53ac9SJohnathan Mantey } 126149c53ac9SJohnathan Mantey for (const std::string& sensorGroup : sensorHeaders) 126249c53ac9SJohnathan Mantey { 126349c53ac9SJohnathan Mantey nlohmann::json::iterator entry = response.find(sensorGroup); 126449c53ac9SJohnathan Mantey if (entry != response.end()) 126549c53ac9SJohnathan Mantey { 126649c53ac9SJohnathan Mantey std::sort(entry->begin(), entry->end(), 126749c53ac9SJohnathan Mantey [](nlohmann::json& c1, nlohmann::json& c2) { 126849c53ac9SJohnathan Mantey return c1["Name"] < c2["Name"]; 126949c53ac9SJohnathan Mantey }); 127049c53ac9SJohnathan Mantey 127149c53ac9SJohnathan Mantey // add the index counts to the end of each entry 127249c53ac9SJohnathan Mantey size_t count = 0; 127349c53ac9SJohnathan Mantey for (nlohmann::json& sensorJson : *entry) 127449c53ac9SJohnathan Mantey { 127549c53ac9SJohnathan Mantey nlohmann::json::iterator odata = sensorJson.find("@odata.id"); 127649c53ac9SJohnathan Mantey if (odata == sensorJson.end()) 127749c53ac9SJohnathan Mantey { 127849c53ac9SJohnathan Mantey continue; 127949c53ac9SJohnathan Mantey } 128049c53ac9SJohnathan Mantey std::string* value = odata->get_ptr<std::string*>(); 128149c53ac9SJohnathan Mantey if (value != nullptr) 128249c53ac9SJohnathan Mantey { 128349c53ac9SJohnathan Mantey *value += std::to_string(count); 128449c53ac9SJohnathan Mantey count++; 1285a0ec28b6SAdrian Ambrożewicz SensorsAsyncResp->updateUri(sensorJson["Name"], *value); 128649c53ac9SJohnathan Mantey } 128749c53ac9SJohnathan Mantey } 128849c53ac9SJohnathan Mantey } 128949c53ac9SJohnathan Mantey } 129049c53ac9SJohnathan Mantey } 129149c53ac9SJohnathan Mantey 129208777fb0SLewanczyk, Dawid /** 1293adc4f0dbSShawn McCarney * @brief Finds the inventory item with the specified object path. 1294adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1295adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 1296adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 12978fb49dd6SShawn McCarney */ 1298*23a21a1cSEd Tanous inline InventoryItem* findInventoryItem( 1299adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1300adc4f0dbSShawn McCarney const std::string& invItemObjPath) 13018fb49dd6SShawn McCarney { 1302adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 13038fb49dd6SShawn McCarney { 1304adc4f0dbSShawn McCarney if (inventoryItem.objectPath == invItemObjPath) 13058fb49dd6SShawn McCarney { 1306adc4f0dbSShawn McCarney return &inventoryItem; 13078fb49dd6SShawn McCarney } 13088fb49dd6SShawn McCarney } 13098fb49dd6SShawn McCarney return nullptr; 13108fb49dd6SShawn McCarney } 13118fb49dd6SShawn McCarney 13128fb49dd6SShawn McCarney /** 1313adc4f0dbSShawn McCarney * @brief Finds the inventory item associated with the specified sensor. 1314adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1315adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor. 1316adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 13178fb49dd6SShawn McCarney */ 1318*23a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor( 1319adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1320adc4f0dbSShawn McCarney const std::string& sensorObjPath) 1321adc4f0dbSShawn McCarney { 1322adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 1323adc4f0dbSShawn McCarney { 1324adc4f0dbSShawn McCarney if (inventoryItem.sensors.count(sensorObjPath) > 0) 1325adc4f0dbSShawn McCarney { 1326adc4f0dbSShawn McCarney return &inventoryItem; 1327adc4f0dbSShawn McCarney } 1328adc4f0dbSShawn McCarney } 1329adc4f0dbSShawn McCarney return nullptr; 1330adc4f0dbSShawn McCarney } 1331adc4f0dbSShawn McCarney 1332adc4f0dbSShawn McCarney /** 1333d500549bSAnthony Wilson * @brief Finds the inventory item associated with the specified led path. 1334d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1335d500549bSAnthony Wilson * @param ledObjPath D-Bus object path of led. 1336d500549bSAnthony Wilson * @return Inventory item within vector, or nullptr if no match found. 1337d500549bSAnthony Wilson */ 1338d500549bSAnthony Wilson inline InventoryItem* 1339d500549bSAnthony Wilson findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems, 1340d500549bSAnthony Wilson const std::string& ledObjPath) 1341d500549bSAnthony Wilson { 1342d500549bSAnthony Wilson for (InventoryItem& inventoryItem : inventoryItems) 1343d500549bSAnthony Wilson { 1344d500549bSAnthony Wilson if (inventoryItem.ledObjectPath == ledObjPath) 1345d500549bSAnthony Wilson { 1346d500549bSAnthony Wilson return &inventoryItem; 1347d500549bSAnthony Wilson } 1348d500549bSAnthony Wilson } 1349d500549bSAnthony Wilson return nullptr; 1350d500549bSAnthony Wilson } 1351d500549bSAnthony Wilson 1352d500549bSAnthony Wilson /** 1353adc4f0dbSShawn McCarney * @brief Adds inventory item and associated sensor to specified vector. 1354adc4f0dbSShawn McCarney * 1355adc4f0dbSShawn McCarney * Adds a new InventoryItem to the vector if necessary. Searches for an 1356adc4f0dbSShawn McCarney * existing InventoryItem with the specified object path. If not found, one is 1357adc4f0dbSShawn McCarney * added to the vector. 1358adc4f0dbSShawn McCarney * 1359adc4f0dbSShawn McCarney * Next, the specified sensor is added to the set of sensors associated with the 1360adc4f0dbSShawn McCarney * InventoryItem. 1361adc4f0dbSShawn McCarney * 1362adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1363adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 1364adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor 1365adc4f0dbSShawn McCarney */ 1366*23a21a1cSEd Tanous inline void 1367adc4f0dbSShawn McCarney addInventoryItem(std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1368adc4f0dbSShawn McCarney const std::string& invItemObjPath, 1369adc4f0dbSShawn McCarney const std::string& sensorObjPath) 1370adc4f0dbSShawn McCarney { 1371adc4f0dbSShawn McCarney // Look for inventory item in vector 1372adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1373adc4f0dbSShawn McCarney findInventoryItem(inventoryItems, invItemObjPath); 1374adc4f0dbSShawn McCarney 1375adc4f0dbSShawn McCarney // If inventory item doesn't exist in vector, add it 1376adc4f0dbSShawn McCarney if (inventoryItem == nullptr) 1377adc4f0dbSShawn McCarney { 1378adc4f0dbSShawn McCarney inventoryItems->emplace_back(invItemObjPath); 1379adc4f0dbSShawn McCarney inventoryItem = &(inventoryItems->back()); 1380adc4f0dbSShawn McCarney } 1381adc4f0dbSShawn McCarney 1382adc4f0dbSShawn McCarney // Add sensor to set of sensors associated with inventory item 1383adc4f0dbSShawn McCarney inventoryItem->sensors.emplace(sensorObjPath); 1384adc4f0dbSShawn McCarney } 1385adc4f0dbSShawn McCarney 1386adc4f0dbSShawn McCarney /** 1387adc4f0dbSShawn McCarney * @brief Stores D-Bus data in the specified inventory item. 1388adc4f0dbSShawn McCarney * 1389adc4f0dbSShawn McCarney * Finds D-Bus data in the specified map of interfaces. Stores the data in the 1390adc4f0dbSShawn McCarney * specified InventoryItem. 1391adc4f0dbSShawn McCarney * 1392adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1393adc4f0dbSShawn McCarney * response. 1394adc4f0dbSShawn McCarney * 1395adc4f0dbSShawn McCarney * @param inventoryItem Inventory item where data will be stored. 1396adc4f0dbSShawn McCarney * @param interfacesDict Map containing D-Bus interfaces and their properties 1397adc4f0dbSShawn McCarney * for the specified inventory item. 1398adc4f0dbSShawn McCarney */ 1399*23a21a1cSEd Tanous inline void storeInventoryItemData( 1400adc4f0dbSShawn McCarney InventoryItem& inventoryItem, 14018fb49dd6SShawn McCarney const boost::container::flat_map< 14028fb49dd6SShawn McCarney std::string, boost::container::flat_map<std::string, SensorVariant>>& 14038fb49dd6SShawn McCarney interfacesDict) 14048fb49dd6SShawn McCarney { 1405adc4f0dbSShawn McCarney // Get properties from Inventory.Item interface 1406adc4f0dbSShawn McCarney auto interfaceIt = 1407adc4f0dbSShawn McCarney interfacesDict.find("xyz.openbmc_project.Inventory.Item"); 1408adc4f0dbSShawn McCarney if (interfaceIt != interfacesDict.end()) 14098fb49dd6SShawn McCarney { 1410adc4f0dbSShawn McCarney auto propertyIt = interfaceIt->second.find("Present"); 1411adc4f0dbSShawn McCarney if (propertyIt != interfaceIt->second.end()) 14128fb49dd6SShawn McCarney { 1413adc4f0dbSShawn McCarney const bool* value = std::get_if<bool>(&propertyIt->second); 1414adc4f0dbSShawn McCarney if (value != nullptr) 14158fb49dd6SShawn McCarney { 1416adc4f0dbSShawn McCarney inventoryItem.isPresent = *value; 14178fb49dd6SShawn McCarney } 14188fb49dd6SShawn McCarney } 14198fb49dd6SShawn McCarney } 14208fb49dd6SShawn McCarney 1421adc4f0dbSShawn McCarney // Check if Inventory.Item.PowerSupply interface is present 1422adc4f0dbSShawn McCarney interfaceIt = 1423adc4f0dbSShawn McCarney interfacesDict.find("xyz.openbmc_project.Inventory.Item.PowerSupply"); 1424adc4f0dbSShawn McCarney if (interfaceIt != interfacesDict.end()) 14258fb49dd6SShawn McCarney { 1426adc4f0dbSShawn McCarney inventoryItem.isPowerSupply = true; 14278fb49dd6SShawn McCarney } 1428adc4f0dbSShawn McCarney 1429adc4f0dbSShawn McCarney // Get properties from Inventory.Decorator.Asset interface 1430adc4f0dbSShawn McCarney interfaceIt = 1431adc4f0dbSShawn McCarney interfacesDict.find("xyz.openbmc_project.Inventory.Decorator.Asset"); 1432adc4f0dbSShawn McCarney if (interfaceIt != interfacesDict.end()) 1433adc4f0dbSShawn McCarney { 1434adc4f0dbSShawn McCarney auto propertyIt = interfaceIt->second.find("Manufacturer"); 1435adc4f0dbSShawn McCarney if (propertyIt != interfaceIt->second.end()) 1436adc4f0dbSShawn McCarney { 1437adc4f0dbSShawn McCarney const std::string* value = 1438adc4f0dbSShawn McCarney std::get_if<std::string>(&propertyIt->second); 1439adc4f0dbSShawn McCarney if (value != nullptr) 1440adc4f0dbSShawn McCarney { 1441adc4f0dbSShawn McCarney inventoryItem.manufacturer = *value; 1442adc4f0dbSShawn McCarney } 1443adc4f0dbSShawn McCarney } 1444adc4f0dbSShawn McCarney 1445adc4f0dbSShawn McCarney propertyIt = interfaceIt->second.find("Model"); 1446adc4f0dbSShawn McCarney if (propertyIt != interfaceIt->second.end()) 1447adc4f0dbSShawn McCarney { 1448adc4f0dbSShawn McCarney const std::string* value = 1449adc4f0dbSShawn McCarney std::get_if<std::string>(&propertyIt->second); 1450adc4f0dbSShawn McCarney if (value != nullptr) 1451adc4f0dbSShawn McCarney { 1452adc4f0dbSShawn McCarney inventoryItem.model = *value; 1453adc4f0dbSShawn McCarney } 1454adc4f0dbSShawn McCarney } 1455adc4f0dbSShawn McCarney 1456adc4f0dbSShawn McCarney propertyIt = interfaceIt->second.find("PartNumber"); 1457adc4f0dbSShawn McCarney if (propertyIt != interfaceIt->second.end()) 1458adc4f0dbSShawn McCarney { 1459adc4f0dbSShawn McCarney const std::string* value = 1460adc4f0dbSShawn McCarney std::get_if<std::string>(&propertyIt->second); 1461adc4f0dbSShawn McCarney if (value != nullptr) 1462adc4f0dbSShawn McCarney { 1463adc4f0dbSShawn McCarney inventoryItem.partNumber = *value; 1464adc4f0dbSShawn McCarney } 1465adc4f0dbSShawn McCarney } 1466adc4f0dbSShawn McCarney 1467adc4f0dbSShawn McCarney propertyIt = interfaceIt->second.find("SerialNumber"); 1468adc4f0dbSShawn McCarney if (propertyIt != interfaceIt->second.end()) 1469adc4f0dbSShawn McCarney { 1470adc4f0dbSShawn McCarney const std::string* value = 1471adc4f0dbSShawn McCarney std::get_if<std::string>(&propertyIt->second); 1472adc4f0dbSShawn McCarney if (value != nullptr) 1473adc4f0dbSShawn McCarney { 1474adc4f0dbSShawn McCarney inventoryItem.serialNumber = *value; 1475adc4f0dbSShawn McCarney } 1476adc4f0dbSShawn McCarney } 1477adc4f0dbSShawn McCarney } 1478adc4f0dbSShawn McCarney 1479adc4f0dbSShawn McCarney // Get properties from State.Decorator.OperationalStatus interface 1480adc4f0dbSShawn McCarney interfaceIt = interfacesDict.find( 1481adc4f0dbSShawn McCarney "xyz.openbmc_project.State.Decorator.OperationalStatus"); 1482adc4f0dbSShawn McCarney if (interfaceIt != interfacesDict.end()) 1483adc4f0dbSShawn McCarney { 1484adc4f0dbSShawn McCarney auto propertyIt = interfaceIt->second.find("Functional"); 1485adc4f0dbSShawn McCarney if (propertyIt != interfaceIt->second.end()) 1486adc4f0dbSShawn McCarney { 1487adc4f0dbSShawn McCarney const bool* value = std::get_if<bool>(&propertyIt->second); 1488adc4f0dbSShawn McCarney if (value != nullptr) 1489adc4f0dbSShawn McCarney { 1490adc4f0dbSShawn McCarney inventoryItem.isFunctional = *value; 14918fb49dd6SShawn McCarney } 14928fb49dd6SShawn McCarney } 14938fb49dd6SShawn McCarney } 14948fb49dd6SShawn McCarney } 14958fb49dd6SShawn McCarney 14968fb49dd6SShawn McCarney /** 1497adc4f0dbSShawn McCarney * @brief Gets D-Bus data for inventory items associated with sensors. 14988fb49dd6SShawn McCarney * 1499adc4f0dbSShawn McCarney * Uses the specified connections (services) to obtain D-Bus data for inventory 1500adc4f0dbSShawn McCarney * items associated with sensors. Stores the resulting data in the 1501adc4f0dbSShawn McCarney * inventoryItems vector. 15028fb49dd6SShawn McCarney * 1503adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1504adc4f0dbSShawn McCarney * response. 1505adc4f0dbSShawn McCarney * 1506adc4f0dbSShawn McCarney * Finds the inventory item data asynchronously. Invokes callback when data has 1507adc4f0dbSShawn McCarney * been obtained. 1508adc4f0dbSShawn McCarney * 1509adc4f0dbSShawn McCarney * The callback must have the following signature: 1510adc4f0dbSShawn McCarney * @code 1511d500549bSAnthony Wilson * callback(void) 1512adc4f0dbSShawn McCarney * @endcode 1513adc4f0dbSShawn McCarney * 1514adc4f0dbSShawn McCarney * This function is called recursively, obtaining data asynchronously from one 1515adc4f0dbSShawn McCarney * connection in each call. This ensures the callback is not invoked until the 1516adc4f0dbSShawn McCarney * last asynchronous function has completed. 15178fb49dd6SShawn McCarney * 15188fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1519adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1520adc4f0dbSShawn McCarney * @param invConnections Connections that provide data for the inventory items. 15218fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 15228fb49dd6SShawn McCarney * implements ObjectManager. 1523adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory data has been obtained. 1524adc4f0dbSShawn McCarney * @param invConnectionsIndex Current index in invConnections. Only specified 1525adc4f0dbSShawn McCarney * in recursive calls to this function. 15268fb49dd6SShawn McCarney */ 1527adc4f0dbSShawn McCarney template <typename Callback> 1528adc4f0dbSShawn McCarney static void getInventoryItemsData( 15298fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1530adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 15318fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_set<std::string>> invConnections, 15328fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 1533adc4f0dbSShawn McCarney objectMgrPaths, 1534271584abSEd Tanous Callback&& callback, size_t invConnectionsIndex = 0) 15358fb49dd6SShawn McCarney { 1536adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData enter"; 15378fb49dd6SShawn McCarney 1538adc4f0dbSShawn McCarney // If no more connections left, call callback 1539adc4f0dbSShawn McCarney if (invConnectionsIndex >= invConnections->size()) 15408fb49dd6SShawn McCarney { 1541d500549bSAnthony Wilson callback(); 1542adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData exit"; 1543adc4f0dbSShawn McCarney return; 1544adc4f0dbSShawn McCarney } 1545adc4f0dbSShawn McCarney 1546adc4f0dbSShawn McCarney // Get inventory item data from current connection 1547adc4f0dbSShawn McCarney auto it = invConnections->nth(invConnectionsIndex); 1548adc4f0dbSShawn McCarney if (it != invConnections->end()) 1549adc4f0dbSShawn McCarney { 1550adc4f0dbSShawn McCarney const std::string& invConnection = *it; 1551adc4f0dbSShawn McCarney 15528fb49dd6SShawn McCarney // Response handler for GetManagedObjects 1553adc4f0dbSShawn McCarney auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections, 1554adc4f0dbSShawn McCarney objectMgrPaths, callback{std::move(callback)}, 1555adc4f0dbSShawn McCarney invConnectionsIndex]( 1556adc4f0dbSShawn McCarney const boost::system::error_code ec, 15578fb49dd6SShawn McCarney ManagedObjectsVectorType& resp) { 1558adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter"; 15598fb49dd6SShawn McCarney if (ec) 15608fb49dd6SShawn McCarney { 15618fb49dd6SShawn McCarney BMCWEB_LOG_ERROR 1562adc4f0dbSShawn McCarney << "getInventoryItemsData respHandler DBus error " << ec; 15638fb49dd6SShawn McCarney messages::internalError(sensorsAsyncResp->res); 15648fb49dd6SShawn McCarney return; 15658fb49dd6SShawn McCarney } 15668fb49dd6SShawn McCarney 15678fb49dd6SShawn McCarney // Loop through returned object paths 15688fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 15698fb49dd6SShawn McCarney { 15708fb49dd6SShawn McCarney const std::string& objPath = 15718fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 15728fb49dd6SShawn McCarney 1573adc4f0dbSShawn McCarney // If this object path is one of the specified inventory items 1574adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1575adc4f0dbSShawn McCarney findInventoryItem(inventoryItems, objPath); 1576adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 15778fb49dd6SShawn McCarney { 1578adc4f0dbSShawn McCarney // Store inventory data in InventoryItem 1579adc4f0dbSShawn McCarney storeInventoryItemData(*inventoryItem, objDictEntry.second); 15808fb49dd6SShawn McCarney } 15818fb49dd6SShawn McCarney } 15828fb49dd6SShawn McCarney 1583adc4f0dbSShawn McCarney // Recurse to get inventory item data from next connection 1584adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 1585adc4f0dbSShawn McCarney invConnections, objectMgrPaths, 1586adc4f0dbSShawn McCarney std::move(callback), invConnectionsIndex + 1); 1587adc4f0dbSShawn McCarney 1588adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit"; 15898fb49dd6SShawn McCarney }; 15908fb49dd6SShawn McCarney 15918fb49dd6SShawn McCarney // Find DBus object path that implements ObjectManager for the current 15928fb49dd6SShawn McCarney // connection. If no mapping found, default to "/". 15938fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(invConnection); 15948fb49dd6SShawn McCarney const std::string& objectMgrPath = 15958fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 15968fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is " 15978fb49dd6SShawn McCarney << objectMgrPath; 15988fb49dd6SShawn McCarney 15998fb49dd6SShawn McCarney // Get all object paths and their interfaces for current connection 16008fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 16018fb49dd6SShawn McCarney std::move(respHandler), invConnection, objectMgrPath, 16028fb49dd6SShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 16038fb49dd6SShawn McCarney } 16048fb49dd6SShawn McCarney 1605adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData exit"; 16068fb49dd6SShawn McCarney } 16078fb49dd6SShawn McCarney 16088fb49dd6SShawn McCarney /** 1609adc4f0dbSShawn McCarney * @brief Gets connections that provide D-Bus data for inventory items. 16108fb49dd6SShawn McCarney * 1611adc4f0dbSShawn McCarney * Gets the D-Bus connections (services) that provide data for the inventory 1612adc4f0dbSShawn McCarney * items that are associated with sensors. 16138fb49dd6SShawn McCarney * 16148fb49dd6SShawn McCarney * Finds the connections asynchronously. Invokes callback when information has 16158fb49dd6SShawn McCarney * been obtained. 16168fb49dd6SShawn McCarney * 16178fb49dd6SShawn McCarney * The callback must have the following signature: 16188fb49dd6SShawn McCarney * @code 16198fb49dd6SShawn McCarney * callback(std::shared_ptr<boost::container::flat_set<std::string>> 16208fb49dd6SShawn McCarney * invConnections) 16218fb49dd6SShawn McCarney * @endcode 16228fb49dd6SShawn McCarney * 16238fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1624adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 16258fb49dd6SShawn McCarney * @param callback Callback to invoke when connections have been obtained. 16268fb49dd6SShawn McCarney */ 16278fb49dd6SShawn McCarney template <typename Callback> 16288fb49dd6SShawn McCarney static void getInventoryItemsConnections( 16298fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1630adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 16318fb49dd6SShawn McCarney Callback&& callback) 16328fb49dd6SShawn McCarney { 16338fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter"; 16348fb49dd6SShawn McCarney 16358fb49dd6SShawn McCarney const std::string path = "/xyz/openbmc_project/inventory"; 1636adc4f0dbSShawn McCarney const std::array<std::string, 4> interfaces = { 16378fb49dd6SShawn McCarney "xyz.openbmc_project.Inventory.Item", 1638adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.PowerSupply", 1639adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Decorator.Asset", 16408fb49dd6SShawn McCarney "xyz.openbmc_project.State.Decorator.OperationalStatus"}; 16418fb49dd6SShawn McCarney 16428fb49dd6SShawn McCarney // Response handler for parsing output from GetSubTree 16438fb49dd6SShawn McCarney auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp, 1644adc4f0dbSShawn McCarney inventoryItems](const boost::system::error_code ec, 16458fb49dd6SShawn McCarney const GetSubTreeType& subtree) { 16468fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter"; 16478fb49dd6SShawn McCarney if (ec) 16488fb49dd6SShawn McCarney { 16498fb49dd6SShawn McCarney messages::internalError(sensorsAsyncResp->res); 16508fb49dd6SShawn McCarney BMCWEB_LOG_ERROR 16518fb49dd6SShawn McCarney << "getInventoryItemsConnections respHandler DBus error " << ec; 16528fb49dd6SShawn McCarney return; 16538fb49dd6SShawn McCarney } 16548fb49dd6SShawn McCarney 16558fb49dd6SShawn McCarney // Make unique list of connections for desired inventory items 16568fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_set<std::string>> 16578fb49dd6SShawn McCarney invConnections = 16588fb49dd6SShawn McCarney std::make_shared<boost::container::flat_set<std::string>>(); 16598fb49dd6SShawn McCarney invConnections->reserve(8); 16608fb49dd6SShawn McCarney 16618fb49dd6SShawn McCarney // Loop through objects from GetSubTree 16628fb49dd6SShawn McCarney for (const std::pair< 16638fb49dd6SShawn McCarney std::string, 16648fb49dd6SShawn McCarney std::vector<std::pair<std::string, std::vector<std::string>>>>& 16658fb49dd6SShawn McCarney object : subtree) 16668fb49dd6SShawn McCarney { 1667adc4f0dbSShawn McCarney // Check if object path is one of the specified inventory items 16688fb49dd6SShawn McCarney const std::string& objPath = object.first; 1669adc4f0dbSShawn McCarney if (findInventoryItem(inventoryItems, objPath) != nullptr) 16708fb49dd6SShawn McCarney { 16718fb49dd6SShawn McCarney // Store all connections to inventory item 16728fb49dd6SShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 16738fb49dd6SShawn McCarney objData : object.second) 16748fb49dd6SShawn McCarney { 16758fb49dd6SShawn McCarney const std::string& invConnection = objData.first; 16768fb49dd6SShawn McCarney invConnections->insert(invConnection); 16778fb49dd6SShawn McCarney } 16788fb49dd6SShawn McCarney } 16798fb49dd6SShawn McCarney } 1680d500549bSAnthony Wilson 16818fb49dd6SShawn McCarney callback(invConnections); 16828fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit"; 16838fb49dd6SShawn McCarney }; 16848fb49dd6SShawn McCarney 16858fb49dd6SShawn McCarney // Make call to ObjectMapper to find all inventory items 16868fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 16878fb49dd6SShawn McCarney std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 16888fb49dd6SShawn McCarney "/xyz/openbmc_project/object_mapper", 16898fb49dd6SShawn McCarney "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); 16908fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit"; 16918fb49dd6SShawn McCarney } 16928fb49dd6SShawn McCarney 16938fb49dd6SShawn McCarney /** 1694adc4f0dbSShawn McCarney * @brief Gets associations from sensors to inventory items. 16958fb49dd6SShawn McCarney * 16968fb49dd6SShawn McCarney * Looks for ObjectMapper associations from the specified sensors to related 1697d500549bSAnthony Wilson * inventory items. Then finds the associations from those inventory items to 1698d500549bSAnthony Wilson * their LEDs, if any. 16998fb49dd6SShawn McCarney * 17008fb49dd6SShawn McCarney * Finds the inventory items asynchronously. Invokes callback when information 17018fb49dd6SShawn McCarney * has been obtained. 17028fb49dd6SShawn McCarney * 17038fb49dd6SShawn McCarney * The callback must have the following signature: 17048fb49dd6SShawn McCarney * @code 1705adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 17068fb49dd6SShawn McCarney * @endcode 17078fb49dd6SShawn McCarney * 17088fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 17098fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 17108fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 17118fb49dd6SShawn McCarney * implements ObjectManager. 17128fb49dd6SShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 17138fb49dd6SShawn McCarney */ 17148fb49dd6SShawn McCarney template <typename Callback> 1715adc4f0dbSShawn McCarney static void getInventoryItemAssociations( 17168fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 17178fb49dd6SShawn McCarney const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames, 17188fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 17198fb49dd6SShawn McCarney objectMgrPaths, 17208fb49dd6SShawn McCarney Callback&& callback) 17218fb49dd6SShawn McCarney { 1722adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter"; 17238fb49dd6SShawn McCarney 17248fb49dd6SShawn McCarney // Response handler for GetManagedObjects 17258fb49dd6SShawn McCarney auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp, 17268fb49dd6SShawn McCarney sensorNames](const boost::system::error_code ec, 17278fb49dd6SShawn McCarney dbus::utility::ManagedObjectType& resp) { 1728adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter"; 17298fb49dd6SShawn McCarney if (ec) 17308fb49dd6SShawn McCarney { 1731adc4f0dbSShawn McCarney BMCWEB_LOG_ERROR 1732adc4f0dbSShawn McCarney << "getInventoryItemAssociations respHandler DBus error " << ec; 17338fb49dd6SShawn McCarney messages::internalError(sensorsAsyncResp->res); 17348fb49dd6SShawn McCarney return; 17358fb49dd6SShawn McCarney } 17368fb49dd6SShawn McCarney 1737adc4f0dbSShawn McCarney // Create vector to hold list of inventory items 1738adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems = 1739adc4f0dbSShawn McCarney std::make_shared<std::vector<InventoryItem>>(); 1740adc4f0dbSShawn McCarney 17418fb49dd6SShawn McCarney // Loop through returned object paths 17428fb49dd6SShawn McCarney std::string sensorAssocPath; 17438fb49dd6SShawn McCarney sensorAssocPath.reserve(128); // avoid memory allocations 17448fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 17458fb49dd6SShawn McCarney { 17468fb49dd6SShawn McCarney const std::string& objPath = 17478fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 17488fb49dd6SShawn McCarney const boost::container::flat_map< 17498fb49dd6SShawn McCarney std::string, boost::container::flat_map< 17508fb49dd6SShawn McCarney std::string, dbus::utility::DbusVariantType>>& 17518fb49dd6SShawn McCarney interfacesDict = objDictEntry.second; 17528fb49dd6SShawn McCarney 17538fb49dd6SShawn McCarney // If path is inventory association for one of the specified sensors 17548fb49dd6SShawn McCarney for (const std::string& sensorName : *sensorNames) 17558fb49dd6SShawn McCarney { 17568fb49dd6SShawn McCarney sensorAssocPath = sensorName; 17578fb49dd6SShawn McCarney sensorAssocPath += "/inventory"; 17588fb49dd6SShawn McCarney if (objPath == sensorAssocPath) 17598fb49dd6SShawn McCarney { 17608fb49dd6SShawn McCarney // Get Association interface for object path 17618fb49dd6SShawn McCarney auto assocIt = 17628fb49dd6SShawn McCarney interfacesDict.find("xyz.openbmc_project.Association"); 17638fb49dd6SShawn McCarney if (assocIt != interfacesDict.end()) 17648fb49dd6SShawn McCarney { 17658fb49dd6SShawn McCarney // Get inventory item from end point 17668fb49dd6SShawn McCarney auto endpointsIt = assocIt->second.find("endpoints"); 17678fb49dd6SShawn McCarney if (endpointsIt != assocIt->second.end()) 17688fb49dd6SShawn McCarney { 17698fb49dd6SShawn McCarney const std::vector<std::string>* endpoints = 17708fb49dd6SShawn McCarney std::get_if<std::vector<std::string>>( 17718fb49dd6SShawn McCarney &endpointsIt->second); 17728fb49dd6SShawn McCarney if ((endpoints != nullptr) && !endpoints->empty()) 17738fb49dd6SShawn McCarney { 1774adc4f0dbSShawn McCarney // Add inventory item to vector 1775adc4f0dbSShawn McCarney const std::string& invItemPath = 1776adc4f0dbSShawn McCarney endpoints->front(); 1777adc4f0dbSShawn McCarney addInventoryItem(inventoryItems, invItemPath, 1778adc4f0dbSShawn McCarney sensorName); 17798fb49dd6SShawn McCarney } 17808fb49dd6SShawn McCarney } 17818fb49dd6SShawn McCarney } 17828fb49dd6SShawn McCarney break; 17838fb49dd6SShawn McCarney } 17848fb49dd6SShawn McCarney } 17858fb49dd6SShawn McCarney } 17868fb49dd6SShawn McCarney 1787d500549bSAnthony Wilson // Now loop through the returned object paths again, this time to 1788d500549bSAnthony Wilson // find the leds associated with the inventory items we just found 1789d500549bSAnthony Wilson std::string inventoryAssocPath; 1790d500549bSAnthony Wilson inventoryAssocPath.reserve(128); // avoid memory allocations 1791d500549bSAnthony Wilson for (const auto& objDictEntry : resp) 1792d500549bSAnthony Wilson { 1793d500549bSAnthony Wilson const std::string& objPath = 1794d500549bSAnthony Wilson static_cast<const std::string&>(objDictEntry.first); 1795d500549bSAnthony Wilson const boost::container::flat_map< 1796d500549bSAnthony Wilson std::string, boost::container::flat_map< 1797d500549bSAnthony Wilson std::string, dbus::utility::DbusVariantType>>& 1798d500549bSAnthony Wilson interfacesDict = objDictEntry.second; 1799d500549bSAnthony Wilson 1800d500549bSAnthony Wilson for (InventoryItem& inventoryItem : *inventoryItems) 1801d500549bSAnthony Wilson { 1802d500549bSAnthony Wilson inventoryAssocPath = inventoryItem.objectPath; 1803d500549bSAnthony Wilson inventoryAssocPath += "/leds"; 1804d500549bSAnthony Wilson if (objPath == inventoryAssocPath) 1805d500549bSAnthony Wilson { 1806d500549bSAnthony Wilson // Get Association interface for object path 1807d500549bSAnthony Wilson auto assocIt = 1808d500549bSAnthony Wilson interfacesDict.find("xyz.openbmc_project.Association"); 1809d500549bSAnthony Wilson if (assocIt != interfacesDict.end()) 1810d500549bSAnthony Wilson { 1811d500549bSAnthony Wilson // Get inventory item from end point 1812d500549bSAnthony Wilson auto endpointsIt = assocIt->second.find("endpoints"); 1813d500549bSAnthony Wilson if (endpointsIt != assocIt->second.end()) 1814d500549bSAnthony Wilson { 1815d500549bSAnthony Wilson const std::vector<std::string>* endpoints = 1816d500549bSAnthony Wilson std::get_if<std::vector<std::string>>( 1817d500549bSAnthony Wilson &endpointsIt->second); 1818d500549bSAnthony Wilson if ((endpoints != nullptr) && !endpoints->empty()) 1819d500549bSAnthony Wilson { 1820d500549bSAnthony Wilson // Store LED path in inventory item 1821d500549bSAnthony Wilson const std::string& ledPath = endpoints->front(); 1822d500549bSAnthony Wilson inventoryItem.ledObjectPath = ledPath; 1823d500549bSAnthony Wilson } 1824d500549bSAnthony Wilson } 1825d500549bSAnthony Wilson } 1826d500549bSAnthony Wilson break; 1827d500549bSAnthony Wilson } 1828d500549bSAnthony Wilson } 1829d500549bSAnthony Wilson } 1830adc4f0dbSShawn McCarney callback(inventoryItems); 1831adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit"; 18328fb49dd6SShawn McCarney }; 18338fb49dd6SShawn McCarney 18348fb49dd6SShawn McCarney // Find DBus object path that implements ObjectManager for ObjectMapper 18358fb49dd6SShawn McCarney std::string connection = "xyz.openbmc_project.ObjectMapper"; 18368fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(connection); 18378fb49dd6SShawn McCarney const std::string& objectMgrPath = 18388fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 18398fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is " 18408fb49dd6SShawn McCarney << objectMgrPath; 18418fb49dd6SShawn McCarney 18428fb49dd6SShawn McCarney // Call GetManagedObjects on the ObjectMapper to get all associations 18438fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 18448fb49dd6SShawn McCarney std::move(respHandler), connection, objectMgrPath, 18458fb49dd6SShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 18468fb49dd6SShawn McCarney 1847adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit"; 18488fb49dd6SShawn McCarney } 18498fb49dd6SShawn McCarney 18508fb49dd6SShawn McCarney /** 1851d500549bSAnthony Wilson * @brief Gets D-Bus data for inventory item leds associated with sensors. 1852d500549bSAnthony Wilson * 1853d500549bSAnthony Wilson * Uses the specified connections (services) to obtain D-Bus data for inventory 1854d500549bSAnthony Wilson * item leds associated with sensors. Stores the resulting data in the 1855d500549bSAnthony Wilson * inventoryItems vector. 1856d500549bSAnthony Wilson * 1857d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 1858d500549bSAnthony Wilson * response. 1859d500549bSAnthony Wilson * 1860d500549bSAnthony Wilson * Finds the inventory item led data asynchronously. Invokes callback when data 1861d500549bSAnthony Wilson * has been obtained. 1862d500549bSAnthony Wilson * 1863d500549bSAnthony Wilson * The callback must have the following signature: 1864d500549bSAnthony Wilson * @code 186542cbe538SGunnar Mills * callback() 1866d500549bSAnthony Wilson * @endcode 1867d500549bSAnthony Wilson * 1868d500549bSAnthony Wilson * This function is called recursively, obtaining data asynchronously from one 1869d500549bSAnthony Wilson * connection in each call. This ensures the callback is not invoked until the 1870d500549bSAnthony Wilson * last asynchronous function has completed. 1871d500549bSAnthony Wilson * 1872d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 1873d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1874d500549bSAnthony Wilson * @param ledConnections Connections that provide data for the inventory leds. 1875d500549bSAnthony Wilson * @param callback Callback to invoke when inventory data has been obtained. 1876d500549bSAnthony Wilson * @param ledConnectionsIndex Current index in ledConnections. Only specified 1877d500549bSAnthony Wilson * in recursive calls to this function. 1878d500549bSAnthony Wilson */ 1879d500549bSAnthony Wilson template <typename Callback> 1880d500549bSAnthony Wilson void getInventoryLedData( 1881d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1882d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1883d500549bSAnthony Wilson std::shared_ptr<boost::container::flat_map<std::string, std::string>> 1884d500549bSAnthony Wilson ledConnections, 1885d500549bSAnthony Wilson Callback&& callback, size_t ledConnectionsIndex = 0) 1886d500549bSAnthony Wilson { 1887d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData enter"; 1888d500549bSAnthony Wilson 1889d500549bSAnthony Wilson // If no more connections left, call callback 1890d500549bSAnthony Wilson if (ledConnectionsIndex >= ledConnections->size()) 1891d500549bSAnthony Wilson { 189242cbe538SGunnar Mills callback(); 1893d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData exit"; 1894d500549bSAnthony Wilson return; 1895d500549bSAnthony Wilson } 1896d500549bSAnthony Wilson 1897d500549bSAnthony Wilson // Get inventory item data from current connection 1898d500549bSAnthony Wilson auto it = ledConnections->nth(ledConnectionsIndex); 1899d500549bSAnthony Wilson if (it != ledConnections->end()) 1900d500549bSAnthony Wilson { 1901d500549bSAnthony Wilson const std::string& ledPath = (*it).first; 1902d500549bSAnthony Wilson const std::string& ledConnection = (*it).second; 1903d500549bSAnthony Wilson // Response handler for Get State property 1904d500549bSAnthony Wilson auto respHandler = 1905d500549bSAnthony Wilson [sensorsAsyncResp, inventoryItems, ledConnections, ledPath, 1906d500549bSAnthony Wilson callback{std::move(callback)}, 1907d500549bSAnthony Wilson ledConnectionsIndex](const boost::system::error_code ec, 1908d500549bSAnthony Wilson const std::variant<std::string>& ledState) { 1909d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter"; 1910d500549bSAnthony Wilson if (ec) 1911d500549bSAnthony Wilson { 1912d500549bSAnthony Wilson BMCWEB_LOG_ERROR 1913d500549bSAnthony Wilson << "getInventoryLedData respHandler DBus error " << ec; 1914d500549bSAnthony Wilson messages::internalError(sensorsAsyncResp->res); 1915d500549bSAnthony Wilson return; 1916d500549bSAnthony Wilson } 1917d500549bSAnthony Wilson 1918d500549bSAnthony Wilson const std::string* state = std::get_if<std::string>(&ledState); 1919d500549bSAnthony Wilson if (state != nullptr) 1920d500549bSAnthony Wilson { 1921d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "Led state: " << *state; 1922d500549bSAnthony Wilson // Find inventory item with this LED object path 1923d500549bSAnthony Wilson InventoryItem* inventoryItem = 1924d500549bSAnthony Wilson findInventoryItemForLed(*inventoryItems, ledPath); 1925d500549bSAnthony Wilson if (inventoryItem != nullptr) 1926d500549bSAnthony Wilson { 1927d500549bSAnthony Wilson // Store LED state in InventoryItem 1928d500549bSAnthony Wilson if (boost::ends_with(*state, "On")) 1929d500549bSAnthony Wilson { 1930d500549bSAnthony Wilson inventoryItem->ledState = LedState::ON; 1931d500549bSAnthony Wilson } 1932d500549bSAnthony Wilson else if (boost::ends_with(*state, "Blink")) 1933d500549bSAnthony Wilson { 1934d500549bSAnthony Wilson inventoryItem->ledState = LedState::BLINK; 1935d500549bSAnthony Wilson } 1936d500549bSAnthony Wilson else if (boost::ends_with(*state, "Off")) 1937d500549bSAnthony Wilson { 1938d500549bSAnthony Wilson inventoryItem->ledState = LedState::OFF; 1939d500549bSAnthony Wilson } 1940d500549bSAnthony Wilson else 1941d500549bSAnthony Wilson { 1942d500549bSAnthony Wilson inventoryItem->ledState = LedState::UNKNOWN; 1943d500549bSAnthony Wilson } 1944d500549bSAnthony Wilson } 1945d500549bSAnthony Wilson } 1946d500549bSAnthony Wilson else 1947d500549bSAnthony Wilson { 1948d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "Failed to find State data for LED: " 1949d500549bSAnthony Wilson << ledPath; 1950d500549bSAnthony Wilson } 1951d500549bSAnthony Wilson 1952d500549bSAnthony Wilson // Recurse to get LED data from next connection 1953d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, 1954d500549bSAnthony Wilson ledConnections, std::move(callback), 1955d500549bSAnthony Wilson ledConnectionsIndex + 1); 1956d500549bSAnthony Wilson 1957d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit"; 1958d500549bSAnthony Wilson }; 1959d500549bSAnthony Wilson 1960d500549bSAnthony Wilson // Get the State property for the current LED 1961d500549bSAnthony Wilson crow::connections::systemBus->async_method_call( 1962d500549bSAnthony Wilson std::move(respHandler), ledConnection, ledPath, 1963d500549bSAnthony Wilson "org.freedesktop.DBus.Properties", "Get", 1964d500549bSAnthony Wilson "xyz.openbmc_project.Led.Physical", "State"); 1965d500549bSAnthony Wilson } 1966d500549bSAnthony Wilson 1967d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData exit"; 1968d500549bSAnthony Wilson } 1969d500549bSAnthony Wilson 1970d500549bSAnthony Wilson /** 1971d500549bSAnthony Wilson * @brief Gets LED data for LEDs associated with given inventory items. 1972d500549bSAnthony Wilson * 1973d500549bSAnthony Wilson * Gets the D-Bus connections (services) that provide LED data for the LEDs 1974d500549bSAnthony Wilson * associated with the specified inventory items. Then gets the LED data from 1975d500549bSAnthony Wilson * each connection and stores it in the inventory item. 1976d500549bSAnthony Wilson * 1977d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 1978d500549bSAnthony Wilson * response. 1979d500549bSAnthony Wilson * 1980d500549bSAnthony Wilson * Finds the LED data asynchronously. Invokes callback when information has 1981d500549bSAnthony Wilson * been obtained. 1982d500549bSAnthony Wilson * 1983d500549bSAnthony Wilson * The callback must have the following signature: 1984d500549bSAnthony Wilson * @code 198542cbe538SGunnar Mills * callback() 1986d500549bSAnthony Wilson * @endcode 1987d500549bSAnthony Wilson * 1988d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 1989d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1990d500549bSAnthony Wilson * @param callback Callback to invoke when inventory items have been obtained. 1991d500549bSAnthony Wilson */ 1992d500549bSAnthony Wilson template <typename Callback> 1993d500549bSAnthony Wilson void getInventoryLeds( 1994d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1995d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1996d500549bSAnthony Wilson Callback&& callback) 1997d500549bSAnthony Wilson { 1998d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds enter"; 1999d500549bSAnthony Wilson 2000d500549bSAnthony Wilson const std::string path = "/xyz/openbmc_project"; 2001d500549bSAnthony Wilson const std::array<std::string, 1> interfaces = { 2002d500549bSAnthony Wilson "xyz.openbmc_project.Led.Physical"}; 2003d500549bSAnthony Wilson 2004d500549bSAnthony Wilson // Response handler for parsing output from GetSubTree 2005d500549bSAnthony Wilson auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp, 2006d500549bSAnthony Wilson inventoryItems](const boost::system::error_code ec, 2007d500549bSAnthony Wilson const GetSubTreeType& subtree) { 2008d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter"; 2009d500549bSAnthony Wilson if (ec) 2010d500549bSAnthony Wilson { 2011d500549bSAnthony Wilson messages::internalError(sensorsAsyncResp->res); 2012d500549bSAnthony Wilson BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error " 2013d500549bSAnthony Wilson << ec; 2014d500549bSAnthony Wilson return; 2015d500549bSAnthony Wilson } 2016d500549bSAnthony Wilson 2017d500549bSAnthony Wilson // Build map of LED object paths to connections 2018d500549bSAnthony Wilson std::shared_ptr<boost::container::flat_map<std::string, std::string>> 2019d500549bSAnthony Wilson ledConnections = std::make_shared< 2020d500549bSAnthony Wilson boost::container::flat_map<std::string, std::string>>(); 2021d500549bSAnthony Wilson 2022d500549bSAnthony Wilson // Loop through objects from GetSubTree 2023d500549bSAnthony Wilson for (const std::pair< 2024d500549bSAnthony Wilson std::string, 2025d500549bSAnthony Wilson std::vector<std::pair<std::string, std::vector<std::string>>>>& 2026d500549bSAnthony Wilson object : subtree) 2027d500549bSAnthony Wilson { 2028d500549bSAnthony Wilson // Check if object path is LED for one of the specified inventory 2029d500549bSAnthony Wilson // items 2030d500549bSAnthony Wilson const std::string& ledPath = object.first; 2031d500549bSAnthony Wilson if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr) 2032d500549bSAnthony Wilson { 2033d500549bSAnthony Wilson // Add mapping from ledPath to connection 2034d500549bSAnthony Wilson const std::string& connection = object.second.begin()->first; 2035d500549bSAnthony Wilson (*ledConnections)[ledPath] = connection; 2036d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> " 2037d500549bSAnthony Wilson << connection; 2038d500549bSAnthony Wilson } 2039d500549bSAnthony Wilson } 2040d500549bSAnthony Wilson 2041d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections, 2042d500549bSAnthony Wilson std::move(callback)); 2043d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit"; 2044d500549bSAnthony Wilson }; 2045d500549bSAnthony Wilson // Make call to ObjectMapper to find all inventory items 2046d500549bSAnthony Wilson crow::connections::systemBus->async_method_call( 2047d500549bSAnthony Wilson std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 2048d500549bSAnthony Wilson "/xyz/openbmc_project/object_mapper", 2049d500549bSAnthony Wilson "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); 2050d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds exit"; 2051d500549bSAnthony Wilson } 2052d500549bSAnthony Wilson 2053d500549bSAnthony Wilson /** 205442cbe538SGunnar Mills * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent 205542cbe538SGunnar Mills * 205642cbe538SGunnar Mills * Uses the specified connections (services) (currently assumes just one) to 205742cbe538SGunnar Mills * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in 205842cbe538SGunnar Mills * the inventoryItems vector. Only stores data in Power Supply inventoryItems. 205942cbe538SGunnar Mills * 206042cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 206142cbe538SGunnar Mills * response. 206242cbe538SGunnar Mills * 206342cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 206442cbe538SGunnar Mills * when data has been obtained. 206542cbe538SGunnar Mills * 206642cbe538SGunnar Mills * The callback must have the following signature: 206742cbe538SGunnar Mills * @code 206842cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 206942cbe538SGunnar Mills * @endcode 207042cbe538SGunnar Mills * 207142cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 207242cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 207342cbe538SGunnar Mills * @param psAttributesConnections Connections that provide data for the Power 207442cbe538SGunnar Mills * Supply Attributes 207542cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 207642cbe538SGunnar Mills */ 207742cbe538SGunnar Mills template <typename Callback> 207842cbe538SGunnar Mills void getPowerSupplyAttributesData( 207942cbe538SGunnar Mills std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 208042cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 208142cbe538SGunnar Mills const boost::container::flat_map<std::string, std::string>& 208242cbe538SGunnar Mills psAttributesConnections, 208342cbe538SGunnar Mills Callback&& callback) 208442cbe538SGunnar Mills { 208542cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter"; 208642cbe538SGunnar Mills 208742cbe538SGunnar Mills if (psAttributesConnections.empty()) 208842cbe538SGunnar Mills { 208942cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!"; 209042cbe538SGunnar Mills callback(inventoryItems); 209142cbe538SGunnar Mills return; 209242cbe538SGunnar Mills } 209342cbe538SGunnar Mills 209442cbe538SGunnar Mills // Assuming just one connection (service) for now 209542cbe538SGunnar Mills auto it = psAttributesConnections.nth(0); 209642cbe538SGunnar Mills 209742cbe538SGunnar Mills const std::string& psAttributesPath = (*it).first; 209842cbe538SGunnar Mills const std::string& psAttributesConnection = (*it).second; 209942cbe538SGunnar Mills 210042cbe538SGunnar Mills // Response handler for Get DeratingFactor property 210142cbe538SGunnar Mills auto respHandler = [sensorsAsyncResp, inventoryItems, 210242cbe538SGunnar Mills callback{std::move(callback)}]( 210342cbe538SGunnar Mills const boost::system::error_code ec, 210442cbe538SGunnar Mills const std::variant<uint32_t>& deratingFactor) { 210542cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter"; 210642cbe538SGunnar Mills if (ec) 210742cbe538SGunnar Mills { 210842cbe538SGunnar Mills BMCWEB_LOG_ERROR 210942cbe538SGunnar Mills << "getPowerSupplyAttributesData respHandler DBus error " << ec; 211042cbe538SGunnar Mills messages::internalError(sensorsAsyncResp->res); 211142cbe538SGunnar Mills return; 211242cbe538SGunnar Mills } 211342cbe538SGunnar Mills 211442cbe538SGunnar Mills const uint32_t* value = std::get_if<uint32_t>(&deratingFactor); 211542cbe538SGunnar Mills if (value != nullptr) 211642cbe538SGunnar Mills { 211742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << *value; 211842cbe538SGunnar Mills // Store value in Power Supply Inventory Items 211942cbe538SGunnar Mills for (InventoryItem& inventoryItem : *inventoryItems) 212042cbe538SGunnar Mills { 212142cbe538SGunnar Mills if (inventoryItem.isPowerSupply == true) 212242cbe538SGunnar Mills { 212342cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent = 212442cbe538SGunnar Mills static_cast<int>(*value); 212542cbe538SGunnar Mills } 212642cbe538SGunnar Mills } 212742cbe538SGunnar Mills } 212842cbe538SGunnar Mills else 212942cbe538SGunnar Mills { 213042cbe538SGunnar Mills BMCWEB_LOG_DEBUG 213142cbe538SGunnar Mills << "Failed to find EfficiencyPercent value for PowerSupplies"; 213242cbe538SGunnar Mills } 213342cbe538SGunnar Mills 213442cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit"; 213542cbe538SGunnar Mills callback(inventoryItems); 213642cbe538SGunnar Mills }; 213742cbe538SGunnar Mills 213842cbe538SGunnar Mills // Get the DeratingFactor property for the PowerSupplyAttributes 213942cbe538SGunnar Mills // Currently only property on the interface/only one we care about 214042cbe538SGunnar Mills crow::connections::systemBus->async_method_call( 214142cbe538SGunnar Mills std::move(respHandler), psAttributesConnection, psAttributesPath, 214242cbe538SGunnar Mills "org.freedesktop.DBus.Properties", "Get", 214342cbe538SGunnar Mills "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor"); 214442cbe538SGunnar Mills 214542cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit"; 214642cbe538SGunnar Mills } 214742cbe538SGunnar Mills 214842cbe538SGunnar Mills /** 214942cbe538SGunnar Mills * @brief Gets the Power Supply Attributes such as EfficiencyPercent 215042cbe538SGunnar Mills * 215142cbe538SGunnar Mills * Gets the D-Bus connection (service) that provides Power Supply Attributes 215242cbe538SGunnar Mills * data. Then gets the Power Supply Attributes data from the connection 215342cbe538SGunnar Mills * (currently just assumes 1 connection) and stores the data in the inventory 215442cbe538SGunnar Mills * item. 215542cbe538SGunnar Mills * 215642cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 215742cbe538SGunnar Mills * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish. 215842cbe538SGunnar Mills * 215942cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 216042cbe538SGunnar Mills * when information has been obtained. 216142cbe538SGunnar Mills * 216242cbe538SGunnar Mills * The callback must have the following signature: 216342cbe538SGunnar Mills * @code 216442cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 216542cbe538SGunnar Mills * @endcode 216642cbe538SGunnar Mills * 216742cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 216842cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 216942cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 217042cbe538SGunnar Mills */ 217142cbe538SGunnar Mills template <typename Callback> 217242cbe538SGunnar Mills void getPowerSupplyAttributes( 217342cbe538SGunnar Mills std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 217442cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 217542cbe538SGunnar Mills Callback&& callback) 217642cbe538SGunnar Mills { 217742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter"; 217842cbe538SGunnar Mills 217942cbe538SGunnar Mills // Only need the power supply attributes when the Power Schema 2180a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode != sensors::node::power) 218142cbe538SGunnar Mills { 218242cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power"; 218342cbe538SGunnar Mills callback(inventoryItems); 218442cbe538SGunnar Mills return; 218542cbe538SGunnar Mills } 218642cbe538SGunnar Mills 218742cbe538SGunnar Mills const std::array<std::string, 1> interfaces = { 218842cbe538SGunnar Mills "xyz.openbmc_project.Control.PowerSupplyAttributes"}; 218942cbe538SGunnar Mills 219042cbe538SGunnar Mills // Response handler for parsing output from GetSubTree 219142cbe538SGunnar Mills auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp, 219242cbe538SGunnar Mills inventoryItems](const boost::system::error_code ec, 219342cbe538SGunnar Mills const GetSubTreeType& subtree) { 219442cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter"; 219542cbe538SGunnar Mills if (ec) 219642cbe538SGunnar Mills { 219742cbe538SGunnar Mills messages::internalError(sensorsAsyncResp->res); 219842cbe538SGunnar Mills BMCWEB_LOG_ERROR 219942cbe538SGunnar Mills << "getPowerSupplyAttributes respHandler DBus error " << ec; 220042cbe538SGunnar Mills return; 220142cbe538SGunnar Mills } 220242cbe538SGunnar Mills if (subtree.size() == 0) 220342cbe538SGunnar Mills { 220442cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!"; 220542cbe538SGunnar Mills callback(inventoryItems); 220642cbe538SGunnar Mills return; 220742cbe538SGunnar Mills } 220842cbe538SGunnar Mills 220942cbe538SGunnar Mills // Currently we only support 1 power supply attribute, use this for 221042cbe538SGunnar Mills // all the power supplies. Build map of object path to connection. 221142cbe538SGunnar Mills // Assume just 1 connection and 1 path for now. 221242cbe538SGunnar Mills boost::container::flat_map<std::string, std::string> 221342cbe538SGunnar Mills psAttributesConnections; 221442cbe538SGunnar Mills 221542cbe538SGunnar Mills if (subtree[0].first.empty() || subtree[0].second.empty()) 221642cbe538SGunnar Mills { 221742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!"; 221842cbe538SGunnar Mills callback(inventoryItems); 221942cbe538SGunnar Mills return; 222042cbe538SGunnar Mills } 222142cbe538SGunnar Mills 222242cbe538SGunnar Mills const std::string& psAttributesPath = subtree[0].first; 222342cbe538SGunnar Mills const std::string& connection = subtree[0].second.begin()->first; 222442cbe538SGunnar Mills 222542cbe538SGunnar Mills if (connection.empty()) 222642cbe538SGunnar Mills { 222742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!"; 222842cbe538SGunnar Mills callback(inventoryItems); 222942cbe538SGunnar Mills return; 223042cbe538SGunnar Mills } 223142cbe538SGunnar Mills 223242cbe538SGunnar Mills psAttributesConnections[psAttributesPath] = connection; 223342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> " 223442cbe538SGunnar Mills << connection; 223542cbe538SGunnar Mills 223642cbe538SGunnar Mills getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems, 223742cbe538SGunnar Mills psAttributesConnections, 223842cbe538SGunnar Mills std::move(callback)); 223942cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit"; 224042cbe538SGunnar Mills }; 224142cbe538SGunnar Mills // Make call to ObjectMapper to find the PowerSupplyAttributes service 224242cbe538SGunnar Mills crow::connections::systemBus->async_method_call( 224342cbe538SGunnar Mills std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 224442cbe538SGunnar Mills "/xyz/openbmc_project/object_mapper", 224542cbe538SGunnar Mills "xyz.openbmc_project.ObjectMapper", "GetSubTree", 224642cbe538SGunnar Mills "/xyz/openbmc_project", 0, interfaces); 224742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit"; 224842cbe538SGunnar Mills } 224942cbe538SGunnar Mills 225042cbe538SGunnar Mills /** 2251adc4f0dbSShawn McCarney * @brief Gets inventory items associated with sensors. 22528fb49dd6SShawn McCarney * 22538fb49dd6SShawn McCarney * Finds the inventory items that are associated with the specified sensors. 2254adc4f0dbSShawn McCarney * Then gets D-Bus data for the inventory items, such as presence and VPD. 22558fb49dd6SShawn McCarney * 2256adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 2257adc4f0dbSShawn McCarney * response. 22588fb49dd6SShawn McCarney * 2259adc4f0dbSShawn McCarney * Finds the inventory items asynchronously. Invokes callback when the 2260adc4f0dbSShawn McCarney * inventory items have been obtained. 2261adc4f0dbSShawn McCarney * 2262adc4f0dbSShawn McCarney * The callback must have the following signature: 2263adc4f0dbSShawn McCarney * @code 2264adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 2265adc4f0dbSShawn McCarney * @endcode 22668fb49dd6SShawn McCarney * 22678fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 22688fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 22698fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 22708fb49dd6SShawn McCarney * implements ObjectManager. 2271adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 22728fb49dd6SShawn McCarney */ 2273adc4f0dbSShawn McCarney template <typename Callback> 2274adc4f0dbSShawn McCarney static void getInventoryItems( 22758fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 22768fb49dd6SShawn McCarney const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames, 22778fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 2278adc4f0dbSShawn McCarney objectMgrPaths, 2279adc4f0dbSShawn McCarney Callback&& callback) 22808fb49dd6SShawn McCarney { 2281adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItems enter"; 2282adc4f0dbSShawn McCarney auto getInventoryItemAssociationsCb = 2283adc4f0dbSShawn McCarney [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}]( 2284adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems) { 2285adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter"; 22868fb49dd6SShawn McCarney auto getInventoryItemsConnectionsCb = 2287adc4f0dbSShawn McCarney [sensorsAsyncResp, inventoryItems, objectMgrPaths, 2288adc4f0dbSShawn McCarney callback{std::move(callback)}]( 22898fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_set<std::string>> 22908fb49dd6SShawn McCarney invConnections) { 22918fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter"; 2292d500549bSAnthony Wilson auto getInventoryItemsDataCb = 2293d500549bSAnthony Wilson [sensorsAsyncResp, inventoryItems, 2294d500549bSAnthony Wilson callback{std::move(callback)}]() { 2295d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter"; 229642cbe538SGunnar Mills 229742cbe538SGunnar Mills auto getInventoryLedsCb = [sensorsAsyncResp, 229842cbe538SGunnar Mills inventoryItems, 229942cbe538SGunnar Mills callback{std::move( 230042cbe538SGunnar Mills callback)}]() { 230142cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter"; 230242cbe538SGunnar Mills // Find Power Supply Attributes and get the data 230342cbe538SGunnar Mills getPowerSupplyAttributes(sensorsAsyncResp, 230442cbe538SGunnar Mills inventoryItems, 230542cbe538SGunnar Mills std::move(callback)); 230642cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit"; 230742cbe538SGunnar Mills }; 230842cbe538SGunnar Mills 2309d500549bSAnthony Wilson // Find led connections and get the data 2310d500549bSAnthony Wilson getInventoryLeds(sensorsAsyncResp, inventoryItems, 231142cbe538SGunnar Mills std::move(getInventoryLedsCb)); 2312d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit"; 2313d500549bSAnthony Wilson }; 23148fb49dd6SShawn McCarney 2315adc4f0dbSShawn McCarney // Get inventory item data from connections 2316adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 2317adc4f0dbSShawn McCarney invConnections, objectMgrPaths, 2318d500549bSAnthony Wilson std::move(getInventoryItemsDataCb)); 23198fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit"; 23208fb49dd6SShawn McCarney }; 23218fb49dd6SShawn McCarney 2322adc4f0dbSShawn McCarney // Get connections that provide inventory item data 23238fb49dd6SShawn McCarney getInventoryItemsConnections( 2324adc4f0dbSShawn McCarney sensorsAsyncResp, inventoryItems, 23258fb49dd6SShawn McCarney std::move(getInventoryItemsConnectionsCb)); 2326adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit"; 23278fb49dd6SShawn McCarney }; 23288fb49dd6SShawn McCarney 2329adc4f0dbSShawn McCarney // Get associations from sensors to inventory items 2330adc4f0dbSShawn McCarney getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths, 2331adc4f0dbSShawn McCarney std::move(getInventoryItemAssociationsCb)); 2332adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItems exit"; 2333adc4f0dbSShawn McCarney } 2334adc4f0dbSShawn McCarney 2335adc4f0dbSShawn McCarney /** 2336adc4f0dbSShawn McCarney * @brief Returns JSON PowerSupply object for the specified inventory item. 2337adc4f0dbSShawn McCarney * 2338adc4f0dbSShawn McCarney * Searches for a JSON PowerSupply object that matches the specified inventory 2339adc4f0dbSShawn McCarney * item. If one is not found, a new PowerSupply object is added to the JSON 2340adc4f0dbSShawn McCarney * array. 2341adc4f0dbSShawn McCarney * 2342adc4f0dbSShawn McCarney * Multiple sensors are often associated with one power supply inventory item. 2343adc4f0dbSShawn McCarney * As a result, multiple sensor values are stored in one JSON PowerSupply 2344adc4f0dbSShawn McCarney * object. 2345adc4f0dbSShawn McCarney * 2346adc4f0dbSShawn McCarney * @param powerSupplyArray JSON array containing Redfish PowerSupply objects. 2347adc4f0dbSShawn McCarney * @param inventoryItem Inventory item for the power supply. 2348adc4f0dbSShawn McCarney * @param chassisId Chassis that contains the power supply. 2349adc4f0dbSShawn McCarney * @return JSON PowerSupply object for the specified inventory item. 2350adc4f0dbSShawn McCarney */ 2351*23a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray, 2352adc4f0dbSShawn McCarney const InventoryItem& inventoryItem, 2353adc4f0dbSShawn McCarney const std::string& chassisId) 2354adc4f0dbSShawn McCarney { 2355adc4f0dbSShawn McCarney // Check if matching PowerSupply object already exists in JSON array 2356adc4f0dbSShawn McCarney for (nlohmann::json& powerSupply : powerSupplyArray) 2357adc4f0dbSShawn McCarney { 2358adc4f0dbSShawn McCarney if (powerSupply["MemberId"] == inventoryItem.name) 2359adc4f0dbSShawn McCarney { 2360adc4f0dbSShawn McCarney return powerSupply; 2361adc4f0dbSShawn McCarney } 2362adc4f0dbSShawn McCarney } 2363adc4f0dbSShawn McCarney 2364adc4f0dbSShawn McCarney // Add new PowerSupply object to JSON array 2365adc4f0dbSShawn McCarney powerSupplyArray.push_back({}); 2366adc4f0dbSShawn McCarney nlohmann::json& powerSupply = powerSupplyArray.back(); 2367adc4f0dbSShawn McCarney powerSupply["@odata.id"] = 2368adc4f0dbSShawn McCarney "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/"; 2369adc4f0dbSShawn McCarney powerSupply["MemberId"] = inventoryItem.name; 2370adc4f0dbSShawn McCarney powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " "); 2371adc4f0dbSShawn McCarney powerSupply["Manufacturer"] = inventoryItem.manufacturer; 2372adc4f0dbSShawn McCarney powerSupply["Model"] = inventoryItem.model; 2373adc4f0dbSShawn McCarney powerSupply["PartNumber"] = inventoryItem.partNumber; 2374adc4f0dbSShawn McCarney powerSupply["SerialNumber"] = inventoryItem.serialNumber; 2375d500549bSAnthony Wilson setLedState(powerSupply, &inventoryItem); 2376adc4f0dbSShawn McCarney 237742cbe538SGunnar Mills if (inventoryItem.powerSupplyEfficiencyPercent >= 0) 237842cbe538SGunnar Mills { 237942cbe538SGunnar Mills powerSupply["EfficiencyPercent"] = 238042cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent; 238142cbe538SGunnar Mills } 238242cbe538SGunnar Mills 238342cbe538SGunnar Mills powerSupply["Status"]["State"] = getState(&inventoryItem); 2384adc4f0dbSShawn McCarney const char* health = inventoryItem.isFunctional ? "OK" : "Critical"; 2385adc4f0dbSShawn McCarney powerSupply["Status"]["Health"] = health; 2386adc4f0dbSShawn McCarney 2387adc4f0dbSShawn McCarney return powerSupply; 23888fb49dd6SShawn McCarney } 23898fb49dd6SShawn McCarney 23908fb49dd6SShawn McCarney /** 2391de629b6eSShawn McCarney * @brief Gets the values of the specified sensors. 2392de629b6eSShawn McCarney * 2393de629b6eSShawn McCarney * Stores the results as JSON in the SensorsAsyncResp. 2394de629b6eSShawn McCarney * 2395de629b6eSShawn McCarney * Gets the sensor values asynchronously. Stores the results later when the 2396de629b6eSShawn McCarney * information has been obtained. 2397de629b6eSShawn McCarney * 2398adc4f0dbSShawn McCarney * The sensorNames set contains all requested sensors for the current chassis. 2399de629b6eSShawn McCarney * 2400de629b6eSShawn McCarney * To minimize the number of DBus calls, the DBus method 2401de629b6eSShawn McCarney * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the 2402de629b6eSShawn McCarney * values of all sensors provided by a connection (service). 2403de629b6eSShawn McCarney * 2404de629b6eSShawn McCarney * The connections set contains all the connections that provide sensor values. 2405de629b6eSShawn McCarney * 2406de629b6eSShawn McCarney * The objectMgrPaths map contains mappings from a connection name to the 2407de629b6eSShawn McCarney * corresponding DBus object path that implements ObjectManager. 2408de629b6eSShawn McCarney * 2409adc4f0dbSShawn McCarney * The InventoryItem vector contains D-Bus inventory items associated with the 2410adc4f0dbSShawn McCarney * sensors. Inventory item data is needed for some Redfish sensor properties. 2411adc4f0dbSShawn McCarney * 2412de629b6eSShawn McCarney * @param SensorsAsyncResp Pointer to object holding response data. 2413adc4f0dbSShawn McCarney * @param sensorNames All requested sensors within the current chassis. 2414de629b6eSShawn McCarney * @param connections Connections that provide sensor values. 2415de629b6eSShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 2416de629b6eSShawn McCarney * implements ObjectManager. 2417adc4f0dbSShawn McCarney * @param inventoryItems Inventory items associated with the sensors. 2418de629b6eSShawn McCarney */ 2419*23a21a1cSEd Tanous inline void getSensorData( 2420de629b6eSShawn McCarney std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp, 242149c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames, 2422de629b6eSShawn McCarney const boost::container::flat_set<std::string>& connections, 24238fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 2424adc4f0dbSShawn McCarney objectMgrPaths, 2425adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 2426de629b6eSShawn McCarney { 2427de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getSensorData enter"; 2428de629b6eSShawn McCarney // Get managed objects from all services exposing sensors 2429de629b6eSShawn McCarney for (const std::string& connection : connections) 2430de629b6eSShawn McCarney { 2431de629b6eSShawn McCarney // Response handler to process managed objects 24328fb49dd6SShawn McCarney auto getManagedObjectsCb = [SensorsAsyncResp, sensorNames, 2433adc4f0dbSShawn McCarney inventoryItems]( 2434de629b6eSShawn McCarney const boost::system::error_code ec, 2435de629b6eSShawn McCarney ManagedObjectsVectorType& resp) { 2436de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter"; 2437de629b6eSShawn McCarney if (ec) 2438de629b6eSShawn McCarney { 2439de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec; 2440de629b6eSShawn McCarney messages::internalError(SensorsAsyncResp->res); 2441de629b6eSShawn McCarney return; 2442de629b6eSShawn McCarney } 2443de629b6eSShawn McCarney // Go through all objects and update response with sensor data 2444de629b6eSShawn McCarney for (const auto& objDictEntry : resp) 2445de629b6eSShawn McCarney { 2446de629b6eSShawn McCarney const std::string& objPath = 2447de629b6eSShawn McCarney static_cast<const std::string&>(objDictEntry.first); 2448de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object " 2449de629b6eSShawn McCarney << objPath; 2450de629b6eSShawn McCarney 2451de629b6eSShawn McCarney std::vector<std::string> split; 2452de629b6eSShawn McCarney // Reserve space for 2453de629b6eSShawn McCarney // /xyz/openbmc_project/sensors/<name>/<subname> 2454de629b6eSShawn McCarney split.reserve(6); 2455de629b6eSShawn McCarney boost::algorithm::split(split, objPath, boost::is_any_of("/")); 2456de629b6eSShawn McCarney if (split.size() < 6) 2457de629b6eSShawn McCarney { 2458de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "Got path that isn't long enough " 2459de629b6eSShawn McCarney << objPath; 2460de629b6eSShawn McCarney continue; 2461de629b6eSShawn McCarney } 2462de629b6eSShawn McCarney // These indexes aren't intuitive, as boost::split puts an empty 2463de629b6eSShawn McCarney // string at the beginning 2464de629b6eSShawn McCarney const std::string& sensorType = split[4]; 2465de629b6eSShawn McCarney const std::string& sensorName = split[5]; 2466de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "sensorName " << sensorName 2467de629b6eSShawn McCarney << " sensorType " << sensorType; 246849c53ac9SJohnathan Mantey if (sensorNames->find(objPath) == sensorNames->end()) 2469de629b6eSShawn McCarney { 2470de629b6eSShawn McCarney BMCWEB_LOG_ERROR << sensorName << " not in sensor list "; 2471de629b6eSShawn McCarney continue; 2472de629b6eSShawn McCarney } 2473de629b6eSShawn McCarney 2474adc4f0dbSShawn McCarney // Find inventory item (if any) associated with sensor 2475adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 2476adc4f0dbSShawn McCarney findInventoryItemForSensor(inventoryItems, objPath); 2477adc4f0dbSShawn McCarney 247895a3ecadSAnthony Wilson const std::string& sensorSchema = 247995a3ecadSAnthony Wilson SensorsAsyncResp->chassisSubNode; 248095a3ecadSAnthony Wilson 248195a3ecadSAnthony Wilson nlohmann::json* sensorJson = nullptr; 248295a3ecadSAnthony Wilson 2483a0ec28b6SAdrian Ambrożewicz if (sensorSchema == sensors::node::sensors) 248495a3ecadSAnthony Wilson { 248595a3ecadSAnthony Wilson SensorsAsyncResp->res.jsonValue["@odata.id"] = 248695a3ecadSAnthony Wilson "/redfish/v1/Chassis/" + SensorsAsyncResp->chassisId + 248795a3ecadSAnthony Wilson "/" + SensorsAsyncResp->chassisSubNode + "/" + 248895a3ecadSAnthony Wilson sensorName; 248995a3ecadSAnthony Wilson sensorJson = &(SensorsAsyncResp->res.jsonValue); 249095a3ecadSAnthony Wilson } 249195a3ecadSAnthony Wilson else 249295a3ecadSAnthony Wilson { 2493271584abSEd Tanous std::string fieldName; 2494de629b6eSShawn McCarney if (sensorType == "temperature") 2495de629b6eSShawn McCarney { 2496de629b6eSShawn McCarney fieldName = "Temperatures"; 2497de629b6eSShawn McCarney } 2498de629b6eSShawn McCarney else if (sensorType == "fan" || sensorType == "fan_tach" || 2499de629b6eSShawn McCarney sensorType == "fan_pwm") 2500de629b6eSShawn McCarney { 2501de629b6eSShawn McCarney fieldName = "Fans"; 2502de629b6eSShawn McCarney } 2503de629b6eSShawn McCarney else if (sensorType == "voltage") 2504de629b6eSShawn McCarney { 2505de629b6eSShawn McCarney fieldName = "Voltages"; 2506de629b6eSShawn McCarney } 2507de629b6eSShawn McCarney else if (sensorType == "power") 2508de629b6eSShawn McCarney { 2509028f7ebcSEddie James if (!sensorName.compare("total_power")) 2510028f7ebcSEddie James { 2511028f7ebcSEddie James fieldName = "PowerControl"; 2512028f7ebcSEddie James } 2513adc4f0dbSShawn McCarney else if ((inventoryItem != nullptr) && 2514adc4f0dbSShawn McCarney (inventoryItem->isPowerSupply)) 2515028f7ebcSEddie James { 2516de629b6eSShawn McCarney fieldName = "PowerSupplies"; 2517de629b6eSShawn McCarney } 2518adc4f0dbSShawn McCarney else 2519adc4f0dbSShawn McCarney { 2520adc4f0dbSShawn McCarney // Other power sensors are in SensorCollection 2521adc4f0dbSShawn McCarney continue; 2522adc4f0dbSShawn McCarney } 2523028f7ebcSEddie James } 2524de629b6eSShawn McCarney else 2525de629b6eSShawn McCarney { 2526de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "Unsure how to handle sensorType " 2527de629b6eSShawn McCarney << sensorType; 2528de629b6eSShawn McCarney continue; 2529de629b6eSShawn McCarney } 2530de629b6eSShawn McCarney 2531de629b6eSShawn McCarney nlohmann::json& tempArray = 2532de629b6eSShawn McCarney SensorsAsyncResp->res.jsonValue[fieldName]; 2533adc4f0dbSShawn McCarney if (fieldName == "PowerControl") 253449c53ac9SJohnathan Mantey { 2535adc4f0dbSShawn McCarney if (tempArray.empty()) 25367ab06f49SGunnar Mills { 253795a3ecadSAnthony Wilson // Put multiple "sensors" into a single 253895a3ecadSAnthony Wilson // PowerControl. Follows MemberId naming and 253995a3ecadSAnthony Wilson // naming in power.hpp. 25407ab06f49SGunnar Mills tempArray.push_back( 2541adc4f0dbSShawn McCarney {{"@odata.id", 2542adc4f0dbSShawn McCarney "/redfish/v1/Chassis/" + 25437ab06f49SGunnar Mills SensorsAsyncResp->chassisId + "/" + 2544adc4f0dbSShawn McCarney SensorsAsyncResp->chassisSubNode + "#/" + 2545adc4f0dbSShawn McCarney fieldName + "/0"}}); 2546adc4f0dbSShawn McCarney } 2547adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 2548adc4f0dbSShawn McCarney } 2549adc4f0dbSShawn McCarney else if (fieldName == "PowerSupplies") 2550adc4f0dbSShawn McCarney { 2551adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 2552adc4f0dbSShawn McCarney { 2553adc4f0dbSShawn McCarney sensorJson = 2554adc4f0dbSShawn McCarney &(getPowerSupply(tempArray, *inventoryItem, 2555adc4f0dbSShawn McCarney SensorsAsyncResp->chassisId)); 2556adc4f0dbSShawn McCarney } 255749c53ac9SJohnathan Mantey } 255849c53ac9SJohnathan Mantey else 255949c53ac9SJohnathan Mantey { 2560de629b6eSShawn McCarney tempArray.push_back( 256195a3ecadSAnthony Wilson {{"@odata.id", 256295a3ecadSAnthony Wilson "/redfish/v1/Chassis/" + 256349c53ac9SJohnathan Mantey SensorsAsyncResp->chassisId + "/" + 256495a3ecadSAnthony Wilson SensorsAsyncResp->chassisSubNode + "#/" + 256595a3ecadSAnthony Wilson fieldName + "/"}}); 2566adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 256749c53ac9SJohnathan Mantey } 256895a3ecadSAnthony Wilson } 2569de629b6eSShawn McCarney 2570adc4f0dbSShawn McCarney if (sensorJson != nullptr) 2571adc4f0dbSShawn McCarney { 2572a0ec28b6SAdrian Ambrożewicz objectInterfacesToJson( 2573a0ec28b6SAdrian Ambrożewicz sensorName, sensorType, SensorsAsyncResp, 2574a0ec28b6SAdrian Ambrożewicz objDictEntry.second, *sensorJson, inventoryItem); 2575adc4f0dbSShawn McCarney } 2576de629b6eSShawn McCarney } 257749c53ac9SJohnathan Mantey if (SensorsAsyncResp.use_count() == 1) 257849c53ac9SJohnathan Mantey { 257949c53ac9SJohnathan Mantey sortJSONResponse(SensorsAsyncResp); 2580a0ec28b6SAdrian Ambrożewicz if (SensorsAsyncResp->chassisSubNode == sensors::node::thermal) 25818bd25ccdSJames Feist { 25828bd25ccdSJames Feist populateFanRedundancy(SensorsAsyncResp); 25838bd25ccdSJames Feist } 258449c53ac9SJohnathan Mantey } 2585de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit"; 2586de629b6eSShawn McCarney }; 2587de629b6eSShawn McCarney 2588de629b6eSShawn McCarney // Find DBus object path that implements ObjectManager for the current 2589de629b6eSShawn McCarney // connection. If no mapping found, default to "/". 25908fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(connection); 2591de629b6eSShawn McCarney const std::string& objectMgrPath = 25928fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 2593de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is " 2594de629b6eSShawn McCarney << objectMgrPath; 2595de629b6eSShawn McCarney 2596de629b6eSShawn McCarney crow::connections::systemBus->async_method_call( 2597de629b6eSShawn McCarney getManagedObjectsCb, connection, objectMgrPath, 2598de629b6eSShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 2599*23a21a1cSEd Tanous } 2600de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getSensorData exit"; 2601de629b6eSShawn McCarney } 2602de629b6eSShawn McCarney 2603*23a21a1cSEd Tanous inline void processSensorList( 260495a3ecadSAnthony Wilson std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp, 260595a3ecadSAnthony Wilson std::shared_ptr<boost::container::flat_set<std::string>> sensorNames) 26061abe55efSEd Tanous { 260795a3ecadSAnthony Wilson auto getConnectionCb = 260895a3ecadSAnthony Wilson [SensorsAsyncResp, sensorNames]( 260995a3ecadSAnthony Wilson const boost::container::flat_set<std::string>& connections) { 261055c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getConnectionCb enter"; 2611de629b6eSShawn McCarney auto getObjectManagerPathsCb = 261249c53ac9SJohnathan Mantey [SensorsAsyncResp, sensorNames, connections]( 261395a3ecadSAnthony Wilson std::shared_ptr< 261495a3ecadSAnthony Wilson boost::container::flat_map<std::string, std::string>> 26158fb49dd6SShawn McCarney objectMgrPaths) { 2616de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter"; 2617adc4f0dbSShawn McCarney auto getInventoryItemsCb = 2618adc4f0dbSShawn McCarney [SensorsAsyncResp, sensorNames, connections, 2619adc4f0dbSShawn McCarney objectMgrPaths]( 2620adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> 2621adc4f0dbSShawn McCarney inventoryItems) { 2622adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter"; 262349c53ac9SJohnathan Mantey // Get sensor data and store results in JSON 2624de629b6eSShawn McCarney getSensorData(SensorsAsyncResp, sensorNames, 2625adc4f0dbSShawn McCarney connections, objectMgrPaths, 2626adc4f0dbSShawn McCarney inventoryItems); 2627adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit"; 2628adc4f0dbSShawn McCarney }; 2629adc4f0dbSShawn McCarney 2630adc4f0dbSShawn McCarney // Get inventory items associated with sensors 2631adc4f0dbSShawn McCarney getInventoryItems(SensorsAsyncResp, sensorNames, 2632adc4f0dbSShawn McCarney objectMgrPaths, 2633adc4f0dbSShawn McCarney std::move(getInventoryItemsCb)); 2634adc4f0dbSShawn McCarney 2635de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit"; 263608777fb0SLewanczyk, Dawid }; 2637de629b6eSShawn McCarney 263849c53ac9SJohnathan Mantey // Get mapping from connection names to the DBus object 263949c53ac9SJohnathan Mantey // paths that implement the ObjectManager interface 2640de629b6eSShawn McCarney getObjectManagerPaths(SensorsAsyncResp, 2641de629b6eSShawn McCarney std::move(getObjectManagerPathsCb)); 264255c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getConnectionCb exit"; 264308777fb0SLewanczyk, Dawid }; 2644de629b6eSShawn McCarney 2645de629b6eSShawn McCarney // Get set of connections that provide sensor values 264695a3ecadSAnthony Wilson getConnections(SensorsAsyncResp, sensorNames, std::move(getConnectionCb)); 264795a3ecadSAnthony Wilson } 264895a3ecadSAnthony Wilson 264995a3ecadSAnthony Wilson /** 265095a3ecadSAnthony Wilson * @brief Entry point for retrieving sensors data related to requested 265195a3ecadSAnthony Wilson * chassis. 265295a3ecadSAnthony Wilson * @param SensorsAsyncResp Pointer to object holding response data 265395a3ecadSAnthony Wilson */ 2654*23a21a1cSEd Tanous inline void getChassisData(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp) 265595a3ecadSAnthony Wilson { 265695a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisData enter"; 265795a3ecadSAnthony Wilson auto getChassisCb = 265895a3ecadSAnthony Wilson [SensorsAsyncResp]( 265995a3ecadSAnthony Wilson std::shared_ptr<boost::container::flat_set<std::string>> 266095a3ecadSAnthony Wilson sensorNames) { 266195a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb enter"; 266295a3ecadSAnthony Wilson processSensorList(SensorsAsyncResp, sensorNames); 266355c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassisCb exit"; 266408777fb0SLewanczyk, Dawid }; 26654f9a2130SJennifer Lee SensorsAsyncResp->res.jsonValue["Redundancy"] = nlohmann::json::array(); 266608777fb0SLewanczyk, Dawid 266726f03899SShawn McCarney // Get set of sensors in chassis 2668588c3f0dSKowalski, Kamil getChassis(SensorsAsyncResp, std::move(getChassisCb)); 266955c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassisData exit"; 2670271584abSEd Tanous } 267108777fb0SLewanczyk, Dawid 2672413961deSRichard Marian Thomaiyar /** 267349c53ac9SJohnathan Mantey * @brief Find the requested sensorName in the list of all sensors supplied by 267449c53ac9SJohnathan Mantey * the chassis node 267549c53ac9SJohnathan Mantey * 267649c53ac9SJohnathan Mantey * @param sensorName The sensor name supplied in the PATCH request 267749c53ac9SJohnathan Mantey * @param sensorsList The list of sensors managed by the chassis node 267849c53ac9SJohnathan Mantey * @param sensorsModified The list of sensors that were found as a result of 267949c53ac9SJohnathan Mantey * repeated calls to this function 268049c53ac9SJohnathan Mantey */ 2681*23a21a1cSEd Tanous inline bool findSensorNameUsingSensorPath( 26820a86febdSRichard Marian Thomaiyar std::string_view sensorName, 268349c53ac9SJohnathan Mantey boost::container::flat_set<std::string>& sensorsList, 268449c53ac9SJohnathan Mantey boost::container::flat_set<std::string>& sensorsModified) 268549c53ac9SJohnathan Mantey { 26860a86febdSRichard Marian Thomaiyar for (std::string_view chassisSensor : sensorsList) 268749c53ac9SJohnathan Mantey { 26880a86febdSRichard Marian Thomaiyar std::size_t pos = chassisSensor.rfind("/"); 26890a86febdSRichard Marian Thomaiyar if (pos >= (chassisSensor.size() - 1)) 269049c53ac9SJohnathan Mantey { 269149c53ac9SJohnathan Mantey continue; 269249c53ac9SJohnathan Mantey } 26930a86febdSRichard Marian Thomaiyar std::string_view thisSensorName = chassisSensor.substr(pos + 1); 269449c53ac9SJohnathan Mantey if (thisSensorName == sensorName) 269549c53ac9SJohnathan Mantey { 269649c53ac9SJohnathan Mantey sensorsModified.emplace(chassisSensor); 269749c53ac9SJohnathan Mantey return true; 269849c53ac9SJohnathan Mantey } 269949c53ac9SJohnathan Mantey } 270049c53ac9SJohnathan Mantey return false; 270149c53ac9SJohnathan Mantey } 270249c53ac9SJohnathan Mantey 270349c53ac9SJohnathan Mantey /** 2704413961deSRichard Marian Thomaiyar * @brief Entry point for overriding sensor values of given sensor 2705413961deSRichard Marian Thomaiyar * 2706413961deSRichard Marian Thomaiyar * @param res response object 27074bb3dc34SCarol Wang * @param allCollections Collections extract from sensors' request patch info 2708413961deSRichard Marian Thomaiyar * @param chassisSubNode Chassis Node for which the query has to happen 2709413961deSRichard Marian Thomaiyar */ 2710*23a21a1cSEd Tanous inline void setSensorsOverride( 27114bb3dc34SCarol Wang std::shared_ptr<SensorsAsyncResp> sensorAsyncResp, 27124bb3dc34SCarol Wang std::unordered_map<std::string, std::vector<nlohmann::json>>& 2713397fd61fSjayaprakash Mutyala allCollections) 2714413961deSRichard Marian Thomaiyar { 271570d1d0aaSjayaprakash Mutyala BMCWEB_LOG_INFO << "setSensorsOverride for subNode" 27164bb3dc34SCarol Wang << sensorAsyncResp->chassisSubNode << "\n"; 2717413961deSRichard Marian Thomaiyar 2718f65af9e8SRichard Marian Thomaiyar const char* propertyValueName; 2719f65af9e8SRichard Marian Thomaiyar std::unordered_map<std::string, std::pair<double, std::string>> overrideMap; 2720413961deSRichard Marian Thomaiyar std::string memberId; 2721413961deSRichard Marian Thomaiyar double value; 2722f65af9e8SRichard Marian Thomaiyar for (auto& collectionItems : allCollections) 2723f65af9e8SRichard Marian Thomaiyar { 2724f65af9e8SRichard Marian Thomaiyar if (collectionItems.first == "Temperatures") 2725f65af9e8SRichard Marian Thomaiyar { 2726f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingCelsius"; 2727f65af9e8SRichard Marian Thomaiyar } 2728f65af9e8SRichard Marian Thomaiyar else if (collectionItems.first == "Fans") 2729f65af9e8SRichard Marian Thomaiyar { 2730f65af9e8SRichard Marian Thomaiyar propertyValueName = "Reading"; 2731f65af9e8SRichard Marian Thomaiyar } 2732f65af9e8SRichard Marian Thomaiyar else 2733f65af9e8SRichard Marian Thomaiyar { 2734f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingVolts"; 2735f65af9e8SRichard Marian Thomaiyar } 2736f65af9e8SRichard Marian Thomaiyar for (auto& item : collectionItems.second) 2737f65af9e8SRichard Marian Thomaiyar { 27384bb3dc34SCarol Wang if (!json_util::readJson(item, sensorAsyncResp->res, "MemberId", 27394bb3dc34SCarol Wang memberId, propertyValueName, value)) 2740413961deSRichard Marian Thomaiyar { 2741413961deSRichard Marian Thomaiyar return; 2742413961deSRichard Marian Thomaiyar } 2743f65af9e8SRichard Marian Thomaiyar overrideMap.emplace(memberId, 2744f65af9e8SRichard Marian Thomaiyar std::make_pair(value, collectionItems.first)); 2745f65af9e8SRichard Marian Thomaiyar } 2746f65af9e8SRichard Marian Thomaiyar } 27474bb3dc34SCarol Wang 274849c53ac9SJohnathan Mantey auto getChassisSensorListCb = [sensorAsyncResp, 274949c53ac9SJohnathan Mantey overrideMap](const std::shared_ptr< 275049c53ac9SJohnathan Mantey boost::container::flat_set< 275149c53ac9SJohnathan Mantey std::string>> 275249c53ac9SJohnathan Mantey sensorsList) { 275349c53ac9SJohnathan Mantey // Match sensor names in the PATCH request to those managed by the 275449c53ac9SJohnathan Mantey // chassis node 275549c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> 275649c53ac9SJohnathan Mantey sensorNames = 275749c53ac9SJohnathan Mantey std::make_shared<boost::container::flat_set<std::string>>(); 2758f65af9e8SRichard Marian Thomaiyar for (const auto& item : overrideMap) 2759413961deSRichard Marian Thomaiyar { 2760f65af9e8SRichard Marian Thomaiyar const auto& sensor = item.first; 276149c53ac9SJohnathan Mantey if (!findSensorNameUsingSensorPath(sensor, *sensorsList, 276249c53ac9SJohnathan Mantey *sensorNames)) 2763f65af9e8SRichard Marian Thomaiyar { 2764f65af9e8SRichard Marian Thomaiyar BMCWEB_LOG_INFO << "Unable to find memberId " << item.first; 2765413961deSRichard Marian Thomaiyar messages::resourceNotFound(sensorAsyncResp->res, 2766f65af9e8SRichard Marian Thomaiyar item.second.second, item.first); 2767413961deSRichard Marian Thomaiyar return; 2768413961deSRichard Marian Thomaiyar } 2769f65af9e8SRichard Marian Thomaiyar } 2770413961deSRichard Marian Thomaiyar // Get the connection to which the memberId belongs 2771413961deSRichard Marian Thomaiyar auto getObjectsWithConnectionCb = 2772f65af9e8SRichard Marian Thomaiyar [sensorAsyncResp, overrideMap]( 2773413961deSRichard Marian Thomaiyar const boost::container::flat_set<std::string>& connections, 2774413961deSRichard Marian Thomaiyar const std::set<std::pair<std::string, std::string>>& 2775413961deSRichard Marian Thomaiyar objectsWithConnection) { 2776f65af9e8SRichard Marian Thomaiyar if (objectsWithConnection.size() != overrideMap.size()) 2777413961deSRichard Marian Thomaiyar { 2778413961deSRichard Marian Thomaiyar BMCWEB_LOG_INFO 2779f65af9e8SRichard Marian Thomaiyar << "Unable to find all objects with proper connection " 2780f65af9e8SRichard Marian Thomaiyar << objectsWithConnection.size() << " requested " 2781f65af9e8SRichard Marian Thomaiyar << overrideMap.size() << "\n"; 2782413961deSRichard Marian Thomaiyar messages::resourceNotFound( 2783413961deSRichard Marian Thomaiyar sensorAsyncResp->res, 2784a0ec28b6SAdrian Ambrożewicz sensorAsyncResp->chassisSubNode == 2785a0ec28b6SAdrian Ambrożewicz sensors::node::thermal 2786413961deSRichard Marian Thomaiyar ? "Temperatures" 2787413961deSRichard Marian Thomaiyar : "Voltages", 2788f65af9e8SRichard Marian Thomaiyar "Count"); 2789f65af9e8SRichard Marian Thomaiyar return; 2790f65af9e8SRichard Marian Thomaiyar } 2791f65af9e8SRichard Marian Thomaiyar for (const auto& item : objectsWithConnection) 2792f65af9e8SRichard Marian Thomaiyar { 2793f65af9e8SRichard Marian Thomaiyar 2794f65af9e8SRichard Marian Thomaiyar auto lastPos = item.first.rfind('/'); 2795f65af9e8SRichard Marian Thomaiyar if (lastPos == std::string::npos) 2796f65af9e8SRichard Marian Thomaiyar { 2797f65af9e8SRichard Marian Thomaiyar messages::internalError(sensorAsyncResp->res); 2798f65af9e8SRichard Marian Thomaiyar return; 2799f65af9e8SRichard Marian Thomaiyar } 2800f65af9e8SRichard Marian Thomaiyar std::string sensorName = item.first.substr(lastPos + 1); 2801f65af9e8SRichard Marian Thomaiyar 2802f65af9e8SRichard Marian Thomaiyar const auto& iterator = overrideMap.find(sensorName); 2803f65af9e8SRichard Marian Thomaiyar if (iterator == overrideMap.end()) 2804f65af9e8SRichard Marian Thomaiyar { 2805f65af9e8SRichard Marian Thomaiyar BMCWEB_LOG_INFO << "Unable to find sensor object" 2806f65af9e8SRichard Marian Thomaiyar << item.first << "\n"; 2807f65af9e8SRichard Marian Thomaiyar messages::internalError(sensorAsyncResp->res); 2808413961deSRichard Marian Thomaiyar return; 2809413961deSRichard Marian Thomaiyar } 2810413961deSRichard Marian Thomaiyar crow::connections::systemBus->async_method_call( 2811f65af9e8SRichard Marian Thomaiyar [sensorAsyncResp](const boost::system::error_code ec) { 2812413961deSRichard Marian Thomaiyar if (ec) 2813413961deSRichard Marian Thomaiyar { 2814413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG 2815f65af9e8SRichard Marian Thomaiyar << "setOverrideValueStatus DBUS error: " 2816413961deSRichard Marian Thomaiyar << ec; 2817413961deSRichard Marian Thomaiyar messages::internalError(sensorAsyncResp->res); 2818413961deSRichard Marian Thomaiyar return; 2819413961deSRichard Marian Thomaiyar } 2820413961deSRichard Marian Thomaiyar }, 2821f65af9e8SRichard Marian Thomaiyar item.second, item.first, 2822413961deSRichard Marian Thomaiyar "org.freedesktop.DBus.Properties", "Set", 2823413961deSRichard Marian Thomaiyar "xyz.openbmc_project.Sensor.Value", "Value", 282419bd78d9SPatrick Williams std::variant<double>(iterator->second.first)); 2825f65af9e8SRichard Marian Thomaiyar } 2826413961deSRichard Marian Thomaiyar }; 2827413961deSRichard Marian Thomaiyar // Get object with connection for the given sensor name 2828413961deSRichard Marian Thomaiyar getObjectsWithConnection(sensorAsyncResp, sensorNames, 2829413961deSRichard Marian Thomaiyar std::move(getObjectsWithConnectionCb)); 2830413961deSRichard Marian Thomaiyar }; 2831413961deSRichard Marian Thomaiyar // get full sensor list for the given chassisId and cross verify the sensor. 2832413961deSRichard Marian Thomaiyar getChassis(sensorAsyncResp, std::move(getChassisSensorListCb)); 2833413961deSRichard Marian Thomaiyar } 2834413961deSRichard Marian Thomaiyar 2835*23a21a1cSEd Tanous inline bool isOverridingAllowed(const std::string& manufacturingModeStatus) 283670d1d0aaSjayaprakash Mutyala { 283770d1d0aaSjayaprakash Mutyala if (manufacturingModeStatus == 283870d1d0aaSjayaprakash Mutyala "xyz.openbmc_project.Control.Security.SpecialMode.Modes.Manufacturing") 283970d1d0aaSjayaprakash Mutyala { 284070d1d0aaSjayaprakash Mutyala return true; 284170d1d0aaSjayaprakash Mutyala } 284270d1d0aaSjayaprakash Mutyala 284370d1d0aaSjayaprakash Mutyala #ifdef BMCWEB_ENABLE_VALIDATION_UNSECURE_FEATURE 284470d1d0aaSjayaprakash Mutyala if (manufacturingModeStatus == "xyz.openbmc_project.Control.Security." 284570d1d0aaSjayaprakash Mutyala "SpecialMode.Modes.ValidationUnsecure") 284670d1d0aaSjayaprakash Mutyala { 284770d1d0aaSjayaprakash Mutyala return true; 284870d1d0aaSjayaprakash Mutyala } 284970d1d0aaSjayaprakash Mutyala 285070d1d0aaSjayaprakash Mutyala #endif 285170d1d0aaSjayaprakash Mutyala 285270d1d0aaSjayaprakash Mutyala return false; 285370d1d0aaSjayaprakash Mutyala } 285470d1d0aaSjayaprakash Mutyala 285570d1d0aaSjayaprakash Mutyala /** 285670d1d0aaSjayaprakash Mutyala * @brief Entry point for Checking the manufacturing mode before doing sensor 285770d1d0aaSjayaprakash Mutyala * override values of given sensor 285870d1d0aaSjayaprakash Mutyala * 285970d1d0aaSjayaprakash Mutyala * @param res response object 286070d1d0aaSjayaprakash Mutyala * @param allCollections Collections extract from sensors' request patch info 286170d1d0aaSjayaprakash Mutyala * @param chassisSubNode Chassis Node for which the query has to happen 286270d1d0aaSjayaprakash Mutyala */ 2863*23a21a1cSEd Tanous inline void checkAndDoSensorsOverride( 286470d1d0aaSjayaprakash Mutyala std::shared_ptr<SensorsAsyncResp> sensorAsyncResp, 2865397fd61fSjayaprakash Mutyala std::unordered_map<std::string, std::vector<nlohmann::json>>& 2866397fd61fSjayaprakash Mutyala allCollections) 286770d1d0aaSjayaprakash Mutyala { 286870d1d0aaSjayaprakash Mutyala BMCWEB_LOG_INFO << "checkAndDoSensorsOverride for subnode" 286970d1d0aaSjayaprakash Mutyala << sensorAsyncResp->chassisSubNode << "\n"; 287070d1d0aaSjayaprakash Mutyala 287170d1d0aaSjayaprakash Mutyala const std::array<std::string, 1> interfaces = { 287270d1d0aaSjayaprakash Mutyala "xyz.openbmc_project.Security.SpecialMode"}; 287370d1d0aaSjayaprakash Mutyala 287470d1d0aaSjayaprakash Mutyala crow::connections::systemBus->async_method_call( 2875*23a21a1cSEd Tanous [sensorAsyncResp, allCollections](const boost::system::error_code ec2, 287670d1d0aaSjayaprakash Mutyala const GetSubTreeType& resp) mutable { 2877*23a21a1cSEd Tanous if (ec2) 287870d1d0aaSjayaprakash Mutyala { 287970d1d0aaSjayaprakash Mutyala BMCWEB_LOG_DEBUG 288070d1d0aaSjayaprakash Mutyala << "Error in querying GetSubTree with Object Mapper. " 2881*23a21a1cSEd Tanous << ec2; 288270d1d0aaSjayaprakash Mutyala messages::internalError(sensorAsyncResp->res); 288370d1d0aaSjayaprakash Mutyala return; 288470d1d0aaSjayaprakash Mutyala } 288591e130a3Sjayaprakash Mutyala #ifdef BMCWEB_INSECURE_UNRESTRICTED_SENSOR_OVERRIDE 288691e130a3Sjayaprakash Mutyala // Proceed with sensor override 2887397fd61fSjayaprakash Mutyala setSensorsOverride(sensorAsyncResp, allCollections); 288870d1d0aaSjayaprakash Mutyala return; 288991e130a3Sjayaprakash Mutyala #endif 289070d1d0aaSjayaprakash Mutyala 289170d1d0aaSjayaprakash Mutyala if (resp.size() != 1) 289270d1d0aaSjayaprakash Mutyala { 289391e130a3Sjayaprakash Mutyala BMCWEB_LOG_WARNING 289491e130a3Sjayaprakash Mutyala << "Overriding sensor value is not allowed - Internal " 289591e130a3Sjayaprakash Mutyala "error in querying SpecialMode property."; 289670d1d0aaSjayaprakash Mutyala messages::internalError(sensorAsyncResp->res); 289770d1d0aaSjayaprakash Mutyala return; 289870d1d0aaSjayaprakash Mutyala } 289970d1d0aaSjayaprakash Mutyala const std::string& path = resp[0].first; 290070d1d0aaSjayaprakash Mutyala const std::string& serviceName = resp[0].second.begin()->first; 290170d1d0aaSjayaprakash Mutyala 290270d1d0aaSjayaprakash Mutyala if (path.empty() || serviceName.empty()) 290370d1d0aaSjayaprakash Mutyala { 290470d1d0aaSjayaprakash Mutyala BMCWEB_LOG_DEBUG 290570d1d0aaSjayaprakash Mutyala << "Path or service name is returned as empty. "; 290670d1d0aaSjayaprakash Mutyala messages::internalError(sensorAsyncResp->res); 290770d1d0aaSjayaprakash Mutyala return; 290870d1d0aaSjayaprakash Mutyala } 290970d1d0aaSjayaprakash Mutyala 291070d1d0aaSjayaprakash Mutyala // Sensor override is allowed only in manufacturing mode or 291170d1d0aaSjayaprakash Mutyala // validation unsecure mode . 291270d1d0aaSjayaprakash Mutyala crow::connections::systemBus->async_method_call( 2913397fd61fSjayaprakash Mutyala [sensorAsyncResp, allCollections, 291470d1d0aaSjayaprakash Mutyala path](const boost::system::error_code ec, 291570d1d0aaSjayaprakash Mutyala std::variant<std::string>& getManufactMode) mutable { 291670d1d0aaSjayaprakash Mutyala if (ec) 291770d1d0aaSjayaprakash Mutyala { 291870d1d0aaSjayaprakash Mutyala BMCWEB_LOG_DEBUG 291970d1d0aaSjayaprakash Mutyala << "Error in querying Special mode property " << ec; 292070d1d0aaSjayaprakash Mutyala messages::internalError(sensorAsyncResp->res); 292170d1d0aaSjayaprakash Mutyala return; 292270d1d0aaSjayaprakash Mutyala } 292370d1d0aaSjayaprakash Mutyala 292470d1d0aaSjayaprakash Mutyala const std::string* manufacturingModeStatus = 292570d1d0aaSjayaprakash Mutyala std::get_if<std::string>(&getManufactMode); 292670d1d0aaSjayaprakash Mutyala 292770d1d0aaSjayaprakash Mutyala if (nullptr == manufacturingModeStatus) 292870d1d0aaSjayaprakash Mutyala { 292970d1d0aaSjayaprakash Mutyala BMCWEB_LOG_DEBUG << "Sensor override mode is not " 293070d1d0aaSjayaprakash Mutyala "Enabled. Returning ... "; 293170d1d0aaSjayaprakash Mutyala messages::internalError(sensorAsyncResp->res); 293270d1d0aaSjayaprakash Mutyala return; 293370d1d0aaSjayaprakash Mutyala } 293470d1d0aaSjayaprakash Mutyala 293570d1d0aaSjayaprakash Mutyala if (isOverridingAllowed(*manufacturingModeStatus)) 293670d1d0aaSjayaprakash Mutyala { 293770d1d0aaSjayaprakash Mutyala BMCWEB_LOG_INFO << "Manufacturing mode is Enabled. " 293870d1d0aaSjayaprakash Mutyala "Proceeding further... "; 2939397fd61fSjayaprakash Mutyala setSensorsOverride(sensorAsyncResp, allCollections); 294070d1d0aaSjayaprakash Mutyala } 294170d1d0aaSjayaprakash Mutyala else 294270d1d0aaSjayaprakash Mutyala { 294370d1d0aaSjayaprakash Mutyala BMCWEB_LOG_WARNING 294470d1d0aaSjayaprakash Mutyala << "Manufacturing mode is not Enabled...can't " 294570d1d0aaSjayaprakash Mutyala "Override the sensor value. "; 294670d1d0aaSjayaprakash Mutyala 294770d1d0aaSjayaprakash Mutyala messages::actionNotSupported( 294870d1d0aaSjayaprakash Mutyala sensorAsyncResp->res, 294970d1d0aaSjayaprakash Mutyala "Overriding of Sensor Value for non " 295070d1d0aaSjayaprakash Mutyala "manufacturing mode"); 295170d1d0aaSjayaprakash Mutyala return; 295270d1d0aaSjayaprakash Mutyala } 295370d1d0aaSjayaprakash Mutyala }, 295470d1d0aaSjayaprakash Mutyala serviceName, path, "org.freedesktop.DBus.Properties", "Get", 295570d1d0aaSjayaprakash Mutyala "xyz.openbmc_project.Security.SpecialMode", "SpecialMode"); 295670d1d0aaSjayaprakash Mutyala }, 295770d1d0aaSjayaprakash Mutyala 295870d1d0aaSjayaprakash Mutyala "xyz.openbmc_project.ObjectMapper", 295970d1d0aaSjayaprakash Mutyala "/xyz/openbmc_project/object_mapper", 296070d1d0aaSjayaprakash Mutyala "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 5, interfaces); 296170d1d0aaSjayaprakash Mutyala } 296270d1d0aaSjayaprakash Mutyala 2963a0ec28b6SAdrian Ambrożewicz /** 2964a0ec28b6SAdrian Ambrożewicz * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus 2965a0ec28b6SAdrian Ambrożewicz * path of the sensor. 2966a0ec28b6SAdrian Ambrożewicz * 2967a0ec28b6SAdrian Ambrożewicz * Function builds valid Redfish response for sensor query of given chassis and 2968a0ec28b6SAdrian Ambrożewicz * node. It then builds metadata about Redfish<->D-Bus correlations and provides 2969a0ec28b6SAdrian Ambrożewicz * it to caller in a callback. 2970a0ec28b6SAdrian Ambrożewicz * 2971a0ec28b6SAdrian Ambrożewicz * @param chassis Chassis for which retrieval should be performed 2972a0ec28b6SAdrian Ambrożewicz * @param node Node (group) of sensors. See sensors::node for supported values 2973a0ec28b6SAdrian Ambrożewicz * @param mapComplete Callback to be called with retrieval result 2974a0ec28b6SAdrian Ambrożewicz */ 2975*23a21a1cSEd Tanous inline void retrieveUriToDbusMap(const std::string& chassis, 2976*23a21a1cSEd Tanous const std::string& node, 2977a0ec28b6SAdrian Ambrożewicz SensorsAsyncResp::DataCompleteCb&& mapComplete) 2978a0ec28b6SAdrian Ambrożewicz { 2979a0ec28b6SAdrian Ambrożewicz auto typesIt = sensors::dbus::types.find(node); 2980a0ec28b6SAdrian Ambrożewicz if (typesIt == sensors::dbus::types.end()) 2981a0ec28b6SAdrian Ambrożewicz { 2982a0ec28b6SAdrian Ambrożewicz BMCWEB_LOG_ERROR << "Wrong node provided : " << node; 2983a0ec28b6SAdrian Ambrożewicz mapComplete(boost::beast::http::status::bad_request, {}); 2984a0ec28b6SAdrian Ambrożewicz return; 2985a0ec28b6SAdrian Ambrożewicz } 2986a0ec28b6SAdrian Ambrożewicz 2987a0ec28b6SAdrian Ambrożewicz auto respBuffer = std::make_shared<crow::Response>(); 2988a0ec28b6SAdrian Ambrożewicz auto callback = 2989a0ec28b6SAdrian Ambrożewicz [respBuffer, mapCompleteCb{std::move(mapComplete)}]( 2990a0ec28b6SAdrian Ambrożewicz const boost::beast::http::status status, 2991a0ec28b6SAdrian Ambrożewicz const boost::container::flat_map<std::string, std::string>& 2992a0ec28b6SAdrian Ambrożewicz uriToDbus) { mapCompleteCb(status, uriToDbus); }; 2993a0ec28b6SAdrian Ambrożewicz 2994a0ec28b6SAdrian Ambrożewicz auto resp = std::make_shared<SensorsAsyncResp>( 2995a0ec28b6SAdrian Ambrożewicz *respBuffer, chassis, typesIt->second, node, std::move(callback)); 2996a0ec28b6SAdrian Ambrożewicz getChassisData(resp); 2997a0ec28b6SAdrian Ambrożewicz } 2998a0ec28b6SAdrian Ambrożewicz 299995a3ecadSAnthony Wilson class SensorCollection : public Node 300095a3ecadSAnthony Wilson { 300195a3ecadSAnthony Wilson public: 300252cc112dSEd Tanous SensorCollection(App& app) : 3003f99c379dSGunnar Mills Node(app, "/redfish/v1/Chassis/<str>/Sensors/", std::string()) 300495a3ecadSAnthony Wilson { 300595a3ecadSAnthony Wilson entityPrivileges = { 300695a3ecadSAnthony Wilson {boost::beast::http::verb::get, {{"Login"}}}, 300795a3ecadSAnthony Wilson {boost::beast::http::verb::head, {{"Login"}}}, 300895a3ecadSAnthony Wilson {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, 300995a3ecadSAnthony Wilson {boost::beast::http::verb::put, {{"ConfigureManager"}}}, 301095a3ecadSAnthony Wilson {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, 301195a3ecadSAnthony Wilson {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; 301295a3ecadSAnthony Wilson } 301395a3ecadSAnthony Wilson 301495a3ecadSAnthony Wilson private: 301595a3ecadSAnthony Wilson void doGet(crow::Response& res, const crow::Request& req, 301695a3ecadSAnthony Wilson const std::vector<std::string>& params) override 301795a3ecadSAnthony Wilson { 301895a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "SensorCollection doGet enter"; 301995a3ecadSAnthony Wilson if (params.size() != 1) 302095a3ecadSAnthony Wilson { 302195a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "SensorCollection doGet param size < 1"; 302295a3ecadSAnthony Wilson messages::internalError(res); 302395a3ecadSAnthony Wilson res.end(); 302495a3ecadSAnthony Wilson return; 302595a3ecadSAnthony Wilson } 302695a3ecadSAnthony Wilson 302795a3ecadSAnthony Wilson const std::string& chassisId = params[0]; 302895a3ecadSAnthony Wilson std::shared_ptr<SensorsAsyncResp> asyncResp = 3029a0ec28b6SAdrian Ambrożewicz std::make_shared<SensorsAsyncResp>( 3030a0ec28b6SAdrian Ambrożewicz res, chassisId, sensors::dbus::types.at(sensors::node::sensors), 3031a0ec28b6SAdrian Ambrożewicz sensors::node::sensors); 303295a3ecadSAnthony Wilson 303395a3ecadSAnthony Wilson auto getChassisCb = 303495a3ecadSAnthony Wilson [asyncResp](std::shared_ptr<boost::container::flat_set<std::string>> 303595a3ecadSAnthony Wilson sensorNames) { 303695a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb enter"; 303795a3ecadSAnthony Wilson 303895a3ecadSAnthony Wilson nlohmann::json& entriesArray = 303995a3ecadSAnthony Wilson asyncResp->res.jsonValue["Members"]; 304095a3ecadSAnthony Wilson for (auto& sensor : *sensorNames) 304195a3ecadSAnthony Wilson { 304295a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor; 304395a3ecadSAnthony Wilson 304495a3ecadSAnthony Wilson std::size_t lastPos = sensor.rfind("/"); 304595a3ecadSAnthony Wilson if (lastPos == std::string::npos || 304695a3ecadSAnthony Wilson lastPos + 1 >= sensor.size()) 304795a3ecadSAnthony Wilson { 304895a3ecadSAnthony Wilson BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor; 304995a3ecadSAnthony Wilson messages::internalError(asyncResp->res); 305095a3ecadSAnthony Wilson return; 305195a3ecadSAnthony Wilson } 305295a3ecadSAnthony Wilson std::string sensorName = sensor.substr(lastPos + 1); 305395a3ecadSAnthony Wilson entriesArray.push_back( 305495a3ecadSAnthony Wilson {{"@odata.id", 305595a3ecadSAnthony Wilson "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" + 305695a3ecadSAnthony Wilson asyncResp->chassisSubNode + "/" + sensorName}}); 305795a3ecadSAnthony Wilson } 305895a3ecadSAnthony Wilson 305995a3ecadSAnthony Wilson asyncResp->res.jsonValue["Members@odata.count"] = 306095a3ecadSAnthony Wilson entriesArray.size(); 306195a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb exit"; 306295a3ecadSAnthony Wilson }; 306395a3ecadSAnthony Wilson 306495a3ecadSAnthony Wilson // Get set of sensors in chassis 306595a3ecadSAnthony Wilson getChassis(asyncResp, std::move(getChassisCb)); 306695a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "SensorCollection doGet exit"; 306795a3ecadSAnthony Wilson } 306895a3ecadSAnthony Wilson }; 306995a3ecadSAnthony Wilson 307095a3ecadSAnthony Wilson class Sensor : public Node 307195a3ecadSAnthony Wilson { 307295a3ecadSAnthony Wilson public: 307352cc112dSEd Tanous Sensor(App& app) : 307495a3ecadSAnthony Wilson Node(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/", std::string(), 307595a3ecadSAnthony Wilson std::string()) 307695a3ecadSAnthony Wilson { 307795a3ecadSAnthony Wilson entityPrivileges = { 307895a3ecadSAnthony Wilson {boost::beast::http::verb::get, {{"Login"}}}, 307995a3ecadSAnthony Wilson {boost::beast::http::verb::head, {{"Login"}}}, 308095a3ecadSAnthony Wilson {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, 308195a3ecadSAnthony Wilson {boost::beast::http::verb::put, {{"ConfigureManager"}}}, 308295a3ecadSAnthony Wilson {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, 308395a3ecadSAnthony Wilson {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; 308495a3ecadSAnthony Wilson } 308595a3ecadSAnthony Wilson 308695a3ecadSAnthony Wilson private: 308795a3ecadSAnthony Wilson void doGet(crow::Response& res, const crow::Request& req, 308895a3ecadSAnthony Wilson const std::vector<std::string>& params) override 308995a3ecadSAnthony Wilson { 309095a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "Sensor doGet enter"; 309195a3ecadSAnthony Wilson if (params.size() != 2) 309295a3ecadSAnthony Wilson { 309395a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "Sensor doGet param size < 2"; 309495a3ecadSAnthony Wilson messages::internalError(res); 309595a3ecadSAnthony Wilson res.end(); 309695a3ecadSAnthony Wilson return; 309795a3ecadSAnthony Wilson } 309895a3ecadSAnthony Wilson const std::string& chassisId = params[0]; 309995a3ecadSAnthony Wilson std::shared_ptr<SensorsAsyncResp> asyncResp = 3100a0ec28b6SAdrian Ambrożewicz std::make_shared<SensorsAsyncResp>(res, chassisId, 3101a0ec28b6SAdrian Ambrożewicz std::vector<const char*>(), 3102a0ec28b6SAdrian Ambrożewicz sensors::node::sensors); 310395a3ecadSAnthony Wilson 310495a3ecadSAnthony Wilson const std::string& sensorName = params[1]; 310595a3ecadSAnthony Wilson const std::array<const char*, 1> interfaces = { 310695a3ecadSAnthony Wilson "xyz.openbmc_project.Sensor.Value"}; 310795a3ecadSAnthony Wilson 310895a3ecadSAnthony Wilson // Get a list of all of the sensors that implement Sensor.Value 310995a3ecadSAnthony Wilson // and get the path and service name associated with the sensor 311095a3ecadSAnthony Wilson crow::connections::systemBus->async_method_call( 311195a3ecadSAnthony Wilson [asyncResp, sensorName](const boost::system::error_code ec, 311295a3ecadSAnthony Wilson const GetSubTreeType& subtree) { 311395a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "respHandler1 enter"; 311495a3ecadSAnthony Wilson if (ec) 311595a3ecadSAnthony Wilson { 311695a3ecadSAnthony Wilson messages::internalError(asyncResp->res); 311795a3ecadSAnthony Wilson BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: " 311895a3ecadSAnthony Wilson << "Dbus error " << ec; 311995a3ecadSAnthony Wilson return; 312095a3ecadSAnthony Wilson } 312195a3ecadSAnthony Wilson 312295a3ecadSAnthony Wilson GetSubTreeType::const_iterator it = std::find_if( 312395a3ecadSAnthony Wilson subtree.begin(), subtree.end(), 312495a3ecadSAnthony Wilson [sensorName]( 312595a3ecadSAnthony Wilson const std::pair< 312695a3ecadSAnthony Wilson std::string, 312795a3ecadSAnthony Wilson std::vector<std::pair<std::string, 312895a3ecadSAnthony Wilson std::vector<std::string>>>>& 312995a3ecadSAnthony Wilson object) { 313095a3ecadSAnthony Wilson std::string_view sensor = object.first; 313195a3ecadSAnthony Wilson std::size_t lastPos = sensor.rfind("/"); 313295a3ecadSAnthony Wilson if (lastPos == std::string::npos || 313395a3ecadSAnthony Wilson lastPos + 1 >= sensor.size()) 313495a3ecadSAnthony Wilson { 313595a3ecadSAnthony Wilson BMCWEB_LOG_ERROR << "Invalid sensor path: " 313695a3ecadSAnthony Wilson << sensor; 313795a3ecadSAnthony Wilson return false; 313895a3ecadSAnthony Wilson } 313995a3ecadSAnthony Wilson std::string_view name = sensor.substr(lastPos + 1); 314095a3ecadSAnthony Wilson 314195a3ecadSAnthony Wilson return name == sensorName; 314295a3ecadSAnthony Wilson }); 314395a3ecadSAnthony Wilson 314495a3ecadSAnthony Wilson if (it == subtree.end()) 314595a3ecadSAnthony Wilson { 314695a3ecadSAnthony Wilson BMCWEB_LOG_ERROR << "Could not find path for sensor: " 314795a3ecadSAnthony Wilson << sensorName; 314895a3ecadSAnthony Wilson messages::resourceNotFound(asyncResp->res, "Sensor", 314995a3ecadSAnthony Wilson sensorName); 315095a3ecadSAnthony Wilson return; 315195a3ecadSAnthony Wilson } 315295a3ecadSAnthony Wilson std::string_view sensorPath = (*it).first; 315395a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" 315495a3ecadSAnthony Wilson << sensorName << "': " << sensorPath; 315595a3ecadSAnthony Wilson 315695a3ecadSAnthony Wilson const std::shared_ptr<boost::container::flat_set<std::string>> 315795a3ecadSAnthony Wilson sensorList = std::make_shared< 315895a3ecadSAnthony Wilson boost::container::flat_set<std::string>>(); 315995a3ecadSAnthony Wilson 316095a3ecadSAnthony Wilson sensorList->emplace(sensorPath); 316195a3ecadSAnthony Wilson processSensorList(asyncResp, sensorList); 316295a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "respHandler1 exit"; 316395a3ecadSAnthony Wilson }, 316495a3ecadSAnthony Wilson "xyz.openbmc_project.ObjectMapper", 316595a3ecadSAnthony Wilson "/xyz/openbmc_project/object_mapper", 316695a3ecadSAnthony Wilson "xyz.openbmc_project.ObjectMapper", "GetSubTree", 316795a3ecadSAnthony Wilson "/xyz/openbmc_project/sensors", 2, interfaces); 316895a3ecadSAnthony Wilson } 316995a3ecadSAnthony Wilson }; 317095a3ecadSAnthony Wilson 317108777fb0SLewanczyk, Dawid } // namespace redfish 3172