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 187e860f15SJohn Edward Broadbent #include <app.hpp> 1908777fb0SLewanczyk, Dawid #include <boost/algorithm/string/predicate.hpp> 2008777fb0SLewanczyk, Dawid #include <boost/algorithm/string/split.hpp> 2108777fb0SLewanczyk, Dawid #include <boost/container/flat_map.hpp> 2208777fb0SLewanczyk, Dawid #include <boost/range/algorithm/replace_copy_if.hpp> 231abe55efSEd Tanous #include <dbus_singleton.hpp> 24168e20c1SEd Tanous #include <dbus_utility.hpp> 25ed398213SEd Tanous #include <registries/privilege_registry.hpp> 261e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 27413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp> 281214b7e7SGunnar Mills 291214b7e7SGunnar Mills #include <cmath> 30b5a76932SEd Tanous #include <utility> 31abf2add6SEd Tanous #include <variant> 3208777fb0SLewanczyk, Dawid 331abe55efSEd Tanous namespace redfish 341abe55efSEd Tanous { 3508777fb0SLewanczyk, Dawid 3608777fb0SLewanczyk, Dawid using GetSubTreeType = std::vector< 3708777fb0SLewanczyk, Dawid std::pair<std::string, 3808777fb0SLewanczyk, Dawid std::vector<std::pair<std::string, std::vector<std::string>>>>>; 3908777fb0SLewanczyk, Dawid 4008777fb0SLewanczyk, Dawid using ManagedObjectsVectorType = std::vector<std::pair< 41aa2e59c1SEd Tanous sdbusplus::message::object_path, 4208777fb0SLewanczyk, Dawid boost::container::flat_map< 43168e20c1SEd Tanous std::string, boost::container::flat_map< 44168e20c1SEd Tanous std::string, dbus::utility::DbusVariantType>>>>; 4508777fb0SLewanczyk, Dawid 46a0ec28b6SAdrian Ambrożewicz namespace sensors 47a0ec28b6SAdrian Ambrożewicz { 48a0ec28b6SAdrian Ambrożewicz namespace node 49a0ec28b6SAdrian Ambrożewicz { 50a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power"; 51a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors"; 52a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal"; 53a0ec28b6SAdrian Ambrożewicz } // namespace node 54a0ec28b6SAdrian Ambrożewicz 55a0ec28b6SAdrian Ambrożewicz namespace dbus 56a0ec28b6SAdrian Ambrożewicz { 57c2bf7f99SWludzik, Jozef 58a0ec28b6SAdrian Ambrożewicz static const boost::container::flat_map<std::string_view, 59a0ec28b6SAdrian Ambrożewicz std::vector<const char*>> 60c2bf7f99SWludzik, Jozef paths = {{node::power, 61a0ec28b6SAdrian Ambrożewicz {"/xyz/openbmc_project/sensors/voltage", 62a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/power"}}, 63a0ec28b6SAdrian Ambrożewicz {node::sensors, 64a0ec28b6SAdrian Ambrożewicz {"/xyz/openbmc_project/sensors/power", 65a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/current", 667088690cSBasheer Ahmed Muddebihal "/xyz/openbmc_project/sensors/airflow", 67e8204933SGeorge Liu #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM 68e8204933SGeorge Liu "/xyz/openbmc_project/sensors/voltage", 69e8204933SGeorge Liu "/xyz/openbmc_project/sensors/fan_tach", 70e8204933SGeorge Liu "/xyz/openbmc_project/sensors/temperature", 71e8204933SGeorge Liu "/xyz/openbmc_project/sensors/fan_pwm", 72e8204933SGeorge Liu "/xyz/openbmc_project/sensors/altitude", 73e8204933SGeorge Liu "/xyz/openbmc_project/sensors/energy", 74e8204933SGeorge Liu #endif 75a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/utilization"}}, 76a0ec28b6SAdrian Ambrożewicz {node::thermal, 77a0ec28b6SAdrian Ambrożewicz {"/xyz/openbmc_project/sensors/fan_tach", 78a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/temperature", 79a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/fan_pwm"}}}; 80c2bf7f99SWludzik, Jozef } // namespace dbus 81c2bf7f99SWludzik, Jozef 82c2bf7f99SWludzik, Jozef inline const char* toReadingType(const std::string& sensorType) 83c2bf7f99SWludzik, Jozef { 84c2bf7f99SWludzik, Jozef if (sensorType == "voltage") 85c2bf7f99SWludzik, Jozef { 86c2bf7f99SWludzik, Jozef return "Voltage"; 87c2bf7f99SWludzik, Jozef } 88c2bf7f99SWludzik, Jozef if (sensorType == "power") 89c2bf7f99SWludzik, Jozef { 90c2bf7f99SWludzik, Jozef return "Power"; 91c2bf7f99SWludzik, Jozef } 92c2bf7f99SWludzik, Jozef if (sensorType == "current") 93c2bf7f99SWludzik, Jozef { 94c2bf7f99SWludzik, Jozef return "Current"; 95c2bf7f99SWludzik, Jozef } 96c2bf7f99SWludzik, Jozef if (sensorType == "fan_tach") 97c2bf7f99SWludzik, Jozef { 98c2bf7f99SWludzik, Jozef return "Rotational"; 99c2bf7f99SWludzik, Jozef } 100c2bf7f99SWludzik, Jozef if (sensorType == "temperature") 101c2bf7f99SWludzik, Jozef { 102c2bf7f99SWludzik, Jozef return "Temperature"; 103c2bf7f99SWludzik, Jozef } 104c2bf7f99SWludzik, Jozef if (sensorType == "fan_pwm" || sensorType == "utilization") 105c2bf7f99SWludzik, Jozef { 106c2bf7f99SWludzik, Jozef return "Percent"; 107c2bf7f99SWludzik, Jozef } 108c2bf7f99SWludzik, Jozef if (sensorType == "altitude") 109c2bf7f99SWludzik, Jozef { 110c2bf7f99SWludzik, Jozef return "Altitude"; 111c2bf7f99SWludzik, Jozef } 112c2bf7f99SWludzik, Jozef if (sensorType == "airflow") 113c2bf7f99SWludzik, Jozef { 114c2bf7f99SWludzik, Jozef return "AirFlow"; 115c2bf7f99SWludzik, Jozef } 116c2bf7f99SWludzik, Jozef if (sensorType == "energy") 117c2bf7f99SWludzik, Jozef { 118c2bf7f99SWludzik, Jozef return "EnergyJoules"; 119c2bf7f99SWludzik, Jozef } 120c2bf7f99SWludzik, Jozef return ""; 121c2bf7f99SWludzik, Jozef } 122c2bf7f99SWludzik, Jozef 123c2bf7f99SWludzik, Jozef inline const char* toReadingUnits(const std::string& sensorType) 124c2bf7f99SWludzik, Jozef { 125c2bf7f99SWludzik, Jozef if (sensorType == "voltage") 126c2bf7f99SWludzik, Jozef { 127c2bf7f99SWludzik, Jozef return "V"; 128c2bf7f99SWludzik, Jozef } 129c2bf7f99SWludzik, Jozef if (sensorType == "power") 130c2bf7f99SWludzik, Jozef { 131c2bf7f99SWludzik, Jozef return "W"; 132c2bf7f99SWludzik, Jozef } 133c2bf7f99SWludzik, Jozef if (sensorType == "current") 134c2bf7f99SWludzik, Jozef { 135c2bf7f99SWludzik, Jozef return "A"; 136c2bf7f99SWludzik, Jozef } 137c2bf7f99SWludzik, Jozef if (sensorType == "fan_tach") 138c2bf7f99SWludzik, Jozef { 139c2bf7f99SWludzik, Jozef return "RPM"; 140c2bf7f99SWludzik, Jozef } 141c2bf7f99SWludzik, Jozef if (sensorType == "temperature") 142c2bf7f99SWludzik, Jozef { 143c2bf7f99SWludzik, Jozef return "Cel"; 144c2bf7f99SWludzik, Jozef } 145c2bf7f99SWludzik, Jozef if (sensorType == "fan_pwm" || sensorType == "utilization") 146c2bf7f99SWludzik, Jozef { 147c2bf7f99SWludzik, Jozef return "%"; 148c2bf7f99SWludzik, Jozef } 149c2bf7f99SWludzik, Jozef if (sensorType == "altitude") 150c2bf7f99SWludzik, Jozef { 151c2bf7f99SWludzik, Jozef return "m"; 152c2bf7f99SWludzik, Jozef } 153c2bf7f99SWludzik, Jozef if (sensorType == "airflow") 154c2bf7f99SWludzik, Jozef { 155c2bf7f99SWludzik, Jozef return "cft_i/min"; 156c2bf7f99SWludzik, Jozef } 157c2bf7f99SWludzik, Jozef if (sensorType == "energy") 158c2bf7f99SWludzik, Jozef { 159c2bf7f99SWludzik, Jozef return "J"; 160c2bf7f99SWludzik, Jozef } 161c2bf7f99SWludzik, Jozef return ""; 162a0ec28b6SAdrian Ambrożewicz } 163a0ec28b6SAdrian Ambrożewicz } // namespace sensors 164a0ec28b6SAdrian Ambrożewicz 16508777fb0SLewanczyk, Dawid /** 166588c3f0dSKowalski, Kamil * SensorsAsyncResp 16708777fb0SLewanczyk, Dawid * Gathers data needed for response processing after async calls are done 16808777fb0SLewanczyk, Dawid */ 1691abe55efSEd Tanous class SensorsAsyncResp 1701abe55efSEd Tanous { 17108777fb0SLewanczyk, Dawid public: 172a0ec28b6SAdrian Ambrożewicz using DataCompleteCb = std::function<void( 173a0ec28b6SAdrian Ambrożewicz const boost::beast::http::status status, 174a0ec28b6SAdrian Ambrożewicz const boost::container::flat_map<std::string, std::string>& uriToDbus)>; 175a0ec28b6SAdrian Ambrożewicz 176a0ec28b6SAdrian Ambrożewicz struct SensorData 177a0ec28b6SAdrian Ambrożewicz { 178a0ec28b6SAdrian Ambrożewicz const std::string name; 179a0ec28b6SAdrian Ambrożewicz std::string uri; 180a0ec28b6SAdrian Ambrożewicz const std::string valueKey; 181a0ec28b6SAdrian Ambrożewicz const std::string dbusPath; 182a0ec28b6SAdrian Ambrożewicz }; 183a0ec28b6SAdrian Ambrożewicz 1848d1b46d7Szhanghch05 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1858d1b46d7Szhanghch05 const std::string& chassisIdIn, 186b5a76932SEd Tanous const std::vector<const char*>& typesIn, 187a0ec28b6SAdrian Ambrożewicz const std::string_view& subNode) : 1888d1b46d7Szhanghch05 asyncResp(asyncResp), 189271584abSEd Tanous chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode) 1901214b7e7SGunnar Mills {} 19108777fb0SLewanczyk, Dawid 192a0ec28b6SAdrian Ambrożewicz // Store extra data about sensor mapping and return it in callback 1938d1b46d7Szhanghch05 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1948d1b46d7Szhanghch05 const std::string& chassisIdIn, 195b5a76932SEd Tanous const std::vector<const char*>& typesIn, 196a0ec28b6SAdrian Ambrożewicz const std::string_view& subNode, 197a0ec28b6SAdrian Ambrożewicz DataCompleteCb&& creationComplete) : 1988d1b46d7Szhanghch05 asyncResp(asyncResp), 199a0ec28b6SAdrian Ambrożewicz chassisId(chassisIdIn), types(typesIn), 200a0ec28b6SAdrian Ambrożewicz chassisSubNode(subNode), metadata{std::vector<SensorData>()}, 201a0ec28b6SAdrian Ambrożewicz dataComplete{std::move(creationComplete)} 202a0ec28b6SAdrian Ambrożewicz {} 203a0ec28b6SAdrian Ambrożewicz 2041abe55efSEd Tanous ~SensorsAsyncResp() 2051abe55efSEd Tanous { 2068d1b46d7Szhanghch05 if (asyncResp->res.result() == 2078d1b46d7Szhanghch05 boost::beast::http::status::internal_server_error) 2081abe55efSEd Tanous { 2091abe55efSEd Tanous // Reset the json object to clear out any data that made it in 2101abe55efSEd Tanous // before the error happened todo(ed) handle error condition with 2111abe55efSEd Tanous // proper code 2128d1b46d7Szhanghch05 asyncResp->res.jsonValue = nlohmann::json::object(); 21308777fb0SLewanczyk, Dawid } 214a0ec28b6SAdrian Ambrożewicz 215a0ec28b6SAdrian Ambrożewicz if (dataComplete && metadata) 216a0ec28b6SAdrian Ambrożewicz { 217a0ec28b6SAdrian Ambrożewicz boost::container::flat_map<std::string, std::string> map; 2188d1b46d7Szhanghch05 if (asyncResp->res.result() == boost::beast::http::status::ok) 219a0ec28b6SAdrian Ambrożewicz { 220a0ec28b6SAdrian Ambrożewicz for (auto& sensor : *metadata) 221a0ec28b6SAdrian Ambrożewicz { 222a0ec28b6SAdrian Ambrożewicz map.insert(std::make_pair(sensor.uri + sensor.valueKey, 223a0ec28b6SAdrian Ambrożewicz sensor.dbusPath)); 224a0ec28b6SAdrian Ambrożewicz } 225a0ec28b6SAdrian Ambrożewicz } 2268d1b46d7Szhanghch05 dataComplete(asyncResp->res.result(), map); 227a0ec28b6SAdrian Ambrożewicz } 22808777fb0SLewanczyk, Dawid } 229588c3f0dSKowalski, Kamil 230a0ec28b6SAdrian Ambrożewicz void addMetadata(const nlohmann::json& sensorObject, 231a0ec28b6SAdrian Ambrożewicz const std::string& valueKey, const std::string& dbusPath) 232a0ec28b6SAdrian Ambrożewicz { 233a0ec28b6SAdrian Ambrożewicz if (metadata) 234a0ec28b6SAdrian Ambrożewicz { 235a0ec28b6SAdrian Ambrożewicz metadata->emplace_back(SensorData{sensorObject["Name"], 236a0ec28b6SAdrian Ambrożewicz sensorObject["@odata.id"], 237a0ec28b6SAdrian Ambrożewicz valueKey, dbusPath}); 238a0ec28b6SAdrian Ambrożewicz } 239a0ec28b6SAdrian Ambrożewicz } 240a0ec28b6SAdrian Ambrożewicz 241a0ec28b6SAdrian Ambrożewicz void updateUri(const std::string& name, const std::string& uri) 242a0ec28b6SAdrian Ambrożewicz { 243a0ec28b6SAdrian Ambrożewicz if (metadata) 244a0ec28b6SAdrian Ambrożewicz { 245a0ec28b6SAdrian Ambrożewicz for (auto& sensor : *metadata) 246a0ec28b6SAdrian Ambrożewicz { 247a0ec28b6SAdrian Ambrożewicz if (sensor.name == name) 248a0ec28b6SAdrian Ambrożewicz { 249a0ec28b6SAdrian Ambrożewicz sensor.uri = uri; 250a0ec28b6SAdrian Ambrożewicz } 251a0ec28b6SAdrian Ambrożewicz } 252a0ec28b6SAdrian Ambrożewicz } 253a0ec28b6SAdrian Ambrożewicz } 254a0ec28b6SAdrian Ambrożewicz 2558d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp> asyncResp; 256a0ec28b6SAdrian Ambrożewicz const std::string chassisId; 25708777fb0SLewanczyk, Dawid const std::vector<const char*> types; 258a0ec28b6SAdrian Ambrożewicz const std::string chassisSubNode; 259a0ec28b6SAdrian Ambrożewicz 260a0ec28b6SAdrian Ambrożewicz private: 261a0ec28b6SAdrian Ambrożewicz std::optional<std::vector<SensorData>> metadata; 262a0ec28b6SAdrian Ambrożewicz DataCompleteCb dataComplete; 26308777fb0SLewanczyk, Dawid }; 26408777fb0SLewanczyk, Dawid 26508777fb0SLewanczyk, Dawid /** 266d500549bSAnthony Wilson * Possible states for physical inventory leds 267d500549bSAnthony Wilson */ 268d500549bSAnthony Wilson enum class LedState 269d500549bSAnthony Wilson { 270d500549bSAnthony Wilson OFF, 271d500549bSAnthony Wilson ON, 272d500549bSAnthony Wilson BLINK, 273d500549bSAnthony Wilson UNKNOWN 274d500549bSAnthony Wilson }; 275d500549bSAnthony Wilson 276d500549bSAnthony Wilson /** 277adc4f0dbSShawn McCarney * D-Bus inventory item associated with one or more sensors. 278adc4f0dbSShawn McCarney */ 279adc4f0dbSShawn McCarney class InventoryItem 280adc4f0dbSShawn McCarney { 281adc4f0dbSShawn McCarney public: 282adc4f0dbSShawn McCarney InventoryItem(const std::string& objPath) : 283adc4f0dbSShawn McCarney objectPath(objPath), name(), isPresent(true), isFunctional(true), 28442cbe538SGunnar Mills isPowerSupply(false), powerSupplyEfficiencyPercent(-1), manufacturer(), 28542cbe538SGunnar Mills model(), partNumber(), serialNumber(), sensors(), ledObjectPath(""), 286d500549bSAnthony Wilson ledState(LedState::UNKNOWN) 287adc4f0dbSShawn McCarney { 288adc4f0dbSShawn McCarney // Set inventory item name to last node of object path 28928aa8de5SGeorge Liu sdbusplus::message::object_path path(objectPath); 29028aa8de5SGeorge Liu name = path.filename(); 29128aa8de5SGeorge Liu if (name.empty()) 292adc4f0dbSShawn McCarney { 29328aa8de5SGeorge Liu BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath; 294adc4f0dbSShawn McCarney } 295adc4f0dbSShawn McCarney } 296adc4f0dbSShawn McCarney 297adc4f0dbSShawn McCarney std::string objectPath; 298adc4f0dbSShawn McCarney std::string name; 299adc4f0dbSShawn McCarney bool isPresent; 300adc4f0dbSShawn McCarney bool isFunctional; 301adc4f0dbSShawn McCarney bool isPowerSupply; 30242cbe538SGunnar Mills int powerSupplyEfficiencyPercent; 303adc4f0dbSShawn McCarney std::string manufacturer; 304adc4f0dbSShawn McCarney std::string model; 305adc4f0dbSShawn McCarney std::string partNumber; 306adc4f0dbSShawn McCarney std::string serialNumber; 307adc4f0dbSShawn McCarney std::set<std::string> sensors; 308d500549bSAnthony Wilson std::string ledObjectPath; 309d500549bSAnthony Wilson LedState ledState; 310adc4f0dbSShawn McCarney }; 311adc4f0dbSShawn McCarney 312adc4f0dbSShawn McCarney /** 313413961deSRichard Marian Thomaiyar * @brief Get objects with connection necessary for sensors 314588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 31508777fb0SLewanczyk, Dawid * @param sensorNames Sensors retrieved from chassis 31608777fb0SLewanczyk, Dawid * @param callback Callback for processing gathered connections 31708777fb0SLewanczyk, Dawid */ 31808777fb0SLewanczyk, Dawid template <typename Callback> 319413961deSRichard Marian Thomaiyar void getObjectsWithConnection( 32081ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 321b5a76932SEd Tanous const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames, 3221abe55efSEd Tanous Callback&& callback) 3231abe55efSEd Tanous { 324413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter"; 32503b5bae3SJames Feist const std::string path = "/xyz/openbmc_project/sensors"; 32608777fb0SLewanczyk, Dawid const std::array<std::string, 1> interfaces = { 32708777fb0SLewanczyk, Dawid "xyz.openbmc_project.Sensor.Value"}; 32808777fb0SLewanczyk, Dawid 32908777fb0SLewanczyk, Dawid // Response handler for parsing objects subtree 33081ce609eSEd Tanous auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp, 3311abe55efSEd Tanous sensorNames](const boost::system::error_code ec, 3321abe55efSEd Tanous const GetSubTreeType& subtree) { 333413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter"; 3341abe55efSEd Tanous if (ec) 3351abe55efSEd Tanous { 3368d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 337413961deSRichard Marian Thomaiyar BMCWEB_LOG_ERROR 338413961deSRichard Marian Thomaiyar << "getObjectsWithConnection resp_handler: Dbus error " << ec; 33908777fb0SLewanczyk, Dawid return; 34008777fb0SLewanczyk, Dawid } 34108777fb0SLewanczyk, Dawid 34255c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees"; 34308777fb0SLewanczyk, Dawid 34408777fb0SLewanczyk, Dawid // Make unique list of connections only for requested sensor types and 34508777fb0SLewanczyk, Dawid // found in the chassis 34608777fb0SLewanczyk, Dawid boost::container::flat_set<std::string> connections; 347413961deSRichard Marian Thomaiyar std::set<std::pair<std::string, std::string>> objectsWithConnection; 3481abe55efSEd Tanous // Intrinsic to avoid malloc. Most systems will have < 8 sensor 3491abe55efSEd Tanous // producers 35008777fb0SLewanczyk, Dawid connections.reserve(8); 35108777fb0SLewanczyk, Dawid 35249c53ac9SJohnathan Mantey BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size(); 35349c53ac9SJohnathan Mantey for (const std::string& tsensor : *sensorNames) 3541abe55efSEd Tanous { 35555c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor; 35608777fb0SLewanczyk, Dawid } 35708777fb0SLewanczyk, Dawid 35808777fb0SLewanczyk, Dawid for (const std::pair< 35908777fb0SLewanczyk, Dawid std::string, 36008777fb0SLewanczyk, Dawid std::vector<std::pair<std::string, std::vector<std::string>>>>& 3611abe55efSEd Tanous object : subtree) 3621abe55efSEd Tanous { 36349c53ac9SJohnathan Mantey if (sensorNames->find(object.first) != sensorNames->end()) 3641abe55efSEd Tanous { 36549c53ac9SJohnathan Mantey for (const std::pair<std::string, std::vector<std::string>>& 3661abe55efSEd Tanous objData : object.second) 3671abe55efSEd Tanous { 36849c53ac9SJohnathan Mantey BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first; 36908777fb0SLewanczyk, Dawid connections.insert(objData.first); 370de629b6eSShawn McCarney objectsWithConnection.insert( 371de629b6eSShawn McCarney std::make_pair(object.first, objData.first)); 37208777fb0SLewanczyk, Dawid } 37308777fb0SLewanczyk, Dawid } 37408777fb0SLewanczyk, Dawid } 37555c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections"; 376413961deSRichard Marian Thomaiyar callback(std::move(connections), std::move(objectsWithConnection)); 377413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit"; 37808777fb0SLewanczyk, Dawid }; 37908777fb0SLewanczyk, Dawid // Make call to ObjectMapper to find all sensors objects 38055c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 38155c7b7a2SEd Tanous std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 3821abe55efSEd Tanous "/xyz/openbmc_project/object_mapper", 3831abe55efSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces); 384413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit"; 385413961deSRichard Marian Thomaiyar } 386413961deSRichard Marian Thomaiyar 387413961deSRichard Marian Thomaiyar /** 388413961deSRichard Marian Thomaiyar * @brief Create connections necessary for sensors 389413961deSRichard Marian Thomaiyar * @param SensorsAsyncResp Pointer to object holding response data 390413961deSRichard Marian Thomaiyar * @param sensorNames Sensors retrieved from chassis 391413961deSRichard Marian Thomaiyar * @param callback Callback for processing gathered connections 392413961deSRichard Marian Thomaiyar */ 393413961deSRichard Marian Thomaiyar template <typename Callback> 39449c53ac9SJohnathan Mantey void getConnections( 39581ce609eSEd Tanous std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 39649c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames, 397413961deSRichard Marian Thomaiyar Callback&& callback) 398413961deSRichard Marian Thomaiyar { 399413961deSRichard Marian Thomaiyar auto objectsWithConnectionCb = 400413961deSRichard Marian Thomaiyar [callback](const boost::container::flat_set<std::string>& connections, 401413961deSRichard Marian Thomaiyar const std::set<std::pair<std::string, std::string>>& 4023174e4dfSEd Tanous /*objectsWithConnection*/) { callback(connections); }; 40381ce609eSEd Tanous getObjectsWithConnection(sensorsAsyncResp, sensorNames, 404413961deSRichard Marian Thomaiyar std::move(objectsWithConnectionCb)); 40508777fb0SLewanczyk, Dawid } 40608777fb0SLewanczyk, Dawid 40708777fb0SLewanczyk, Dawid /** 40849c53ac9SJohnathan Mantey * @brief Shrinks the list of sensors for processing 40949c53ac9SJohnathan Mantey * @param SensorsAysncResp The class holding the Redfish response 41049c53ac9SJohnathan Mantey * @param allSensors A list of all the sensors associated to the 41149c53ac9SJohnathan Mantey * chassis element (i.e. baseboard, front panel, etc...) 41249c53ac9SJohnathan Mantey * @param activeSensors A list that is a reduction of the incoming 41349c53ac9SJohnathan Mantey * allSensors list. Eliminate Thermal sensors when a Power request is 41449c53ac9SJohnathan Mantey * made, and eliminate Power sensors when a Thermal request is made. 41549c53ac9SJohnathan Mantey */ 41623a21a1cSEd Tanous inline void reduceSensorList( 41781ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 41849c53ac9SJohnathan Mantey const std::vector<std::string>* allSensors, 419b5a76932SEd Tanous const std::shared_ptr<boost::container::flat_set<std::string>>& 420b5a76932SEd Tanous activeSensors) 42149c53ac9SJohnathan Mantey { 42281ce609eSEd Tanous if (sensorsAsyncResp == nullptr) 42349c53ac9SJohnathan Mantey { 42449c53ac9SJohnathan Mantey return; 42549c53ac9SJohnathan Mantey } 42649c53ac9SJohnathan Mantey if ((allSensors == nullptr) || (activeSensors == nullptr)) 42749c53ac9SJohnathan Mantey { 42849c53ac9SJohnathan Mantey messages::resourceNotFound( 4298d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode, 43081ce609eSEd Tanous sensorsAsyncResp->chassisSubNode == sensors::node::thermal 431a0ec28b6SAdrian Ambrożewicz ? "Temperatures" 43249c53ac9SJohnathan Mantey : "Voltages"); 43349c53ac9SJohnathan Mantey 43449c53ac9SJohnathan Mantey return; 43549c53ac9SJohnathan Mantey } 43649c53ac9SJohnathan Mantey if (allSensors->empty()) 43749c53ac9SJohnathan Mantey { 43849c53ac9SJohnathan Mantey // Nothing to do, the activeSensors object is also empty 43949c53ac9SJohnathan Mantey return; 44049c53ac9SJohnathan Mantey } 44149c53ac9SJohnathan Mantey 44281ce609eSEd Tanous for (const char* type : sensorsAsyncResp->types) 44349c53ac9SJohnathan Mantey { 44449c53ac9SJohnathan Mantey for (const std::string& sensor : *allSensors) 44549c53ac9SJohnathan Mantey { 44649c53ac9SJohnathan Mantey if (boost::starts_with(sensor, type)) 44749c53ac9SJohnathan Mantey { 44849c53ac9SJohnathan Mantey activeSensors->emplace(sensor); 44949c53ac9SJohnathan Mantey } 45049c53ac9SJohnathan Mantey } 45149c53ac9SJohnathan Mantey } 45249c53ac9SJohnathan Mantey } 45349c53ac9SJohnathan Mantey 45449c53ac9SJohnathan Mantey /** 4554bb3dc34SCarol Wang * @brief Retrieves valid chassis path 4564bb3dc34SCarol Wang * @param asyncResp Pointer to object holding response data 4574bb3dc34SCarol Wang * @param callback Callback for next step to get valid chassis path 4584bb3dc34SCarol Wang */ 4594bb3dc34SCarol Wang template <typename Callback> 460b5a76932SEd Tanous void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp, 4614bb3dc34SCarol Wang Callback&& callback) 4624bb3dc34SCarol Wang { 4634bb3dc34SCarol Wang BMCWEB_LOG_DEBUG << "checkChassisId enter"; 4644bb3dc34SCarol Wang const std::array<const char*, 2> interfaces = { 4654bb3dc34SCarol Wang "xyz.openbmc_project.Inventory.Item.Board", 4664bb3dc34SCarol Wang "xyz.openbmc_project.Inventory.Item.Chassis"}; 4674bb3dc34SCarol Wang 4684bb3dc34SCarol Wang auto respHandler = 4694bb3dc34SCarol Wang [callback{std::move(callback)}, 4704bb3dc34SCarol Wang asyncResp](const boost::system::error_code ec, 4714bb3dc34SCarol Wang const std::vector<std::string>& chassisPaths) mutable { 4724bb3dc34SCarol Wang BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter"; 4734bb3dc34SCarol Wang if (ec) 4744bb3dc34SCarol Wang { 4754bb3dc34SCarol Wang BMCWEB_LOG_ERROR 4764bb3dc34SCarol Wang << "getValidChassisPath respHandler DBUS error: " << ec; 4778d1b46d7Szhanghch05 messages::internalError(asyncResp->asyncResp->res); 4784bb3dc34SCarol Wang return; 4794bb3dc34SCarol Wang } 4804bb3dc34SCarol Wang 4814bb3dc34SCarol Wang std::optional<std::string> chassisPath; 4824bb3dc34SCarol Wang std::string chassisName; 4834bb3dc34SCarol Wang for (const std::string& chassis : chassisPaths) 4844bb3dc34SCarol Wang { 48528aa8de5SGeorge Liu sdbusplus::message::object_path path(chassis); 48628aa8de5SGeorge Liu chassisName = path.filename(); 48728aa8de5SGeorge Liu if (chassisName.empty()) 4884bb3dc34SCarol Wang { 4894bb3dc34SCarol Wang BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis; 4904bb3dc34SCarol Wang continue; 4914bb3dc34SCarol Wang } 4924bb3dc34SCarol Wang if (chassisName == asyncResp->chassisId) 4934bb3dc34SCarol Wang { 4944bb3dc34SCarol Wang chassisPath = chassis; 4954bb3dc34SCarol Wang break; 4964bb3dc34SCarol Wang } 4974bb3dc34SCarol Wang } 4984bb3dc34SCarol Wang callback(chassisPath); 4994bb3dc34SCarol Wang }; 5004bb3dc34SCarol Wang 5014bb3dc34SCarol Wang // Get the Chassis Collection 5024bb3dc34SCarol Wang crow::connections::systemBus->async_method_call( 5034bb3dc34SCarol Wang respHandler, "xyz.openbmc_project.ObjectMapper", 5044bb3dc34SCarol Wang "/xyz/openbmc_project/object_mapper", 5054bb3dc34SCarol Wang "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 5064bb3dc34SCarol Wang "/xyz/openbmc_project/inventory", 0, interfaces); 5074bb3dc34SCarol Wang BMCWEB_LOG_DEBUG << "checkChassisId exit"; 5084bb3dc34SCarol Wang } 5094bb3dc34SCarol Wang 5104bb3dc34SCarol Wang /** 51108777fb0SLewanczyk, Dawid * @brief Retrieves requested chassis sensors and redundancy data from DBus . 512588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 51308777fb0SLewanczyk, Dawid * @param callback Callback for next step in gathered sensor processing 51408777fb0SLewanczyk, Dawid */ 51508777fb0SLewanczyk, Dawid template <typename Callback> 516b5a76932SEd Tanous void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 5171abe55efSEd Tanous Callback&& callback) 5181abe55efSEd Tanous { 51955c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis enter"; 520adc4f0dbSShawn McCarney const std::array<const char*, 2> interfaces = { 52149c53ac9SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Board", 522adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.Chassis"}; 52349c53ac9SJohnathan Mantey auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp]( 52449c53ac9SJohnathan Mantey const boost::system::error_code ec, 52549c53ac9SJohnathan Mantey const std::vector<std::string>& chassisPaths) { 52655c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis respHandler enter"; 5271abe55efSEd Tanous if (ec) 5281abe55efSEd Tanous { 52955c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec; 5308d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 53108777fb0SLewanczyk, Dawid return; 53208777fb0SLewanczyk, Dawid } 53308777fb0SLewanczyk, Dawid 53449c53ac9SJohnathan Mantey const std::string* chassisPath = nullptr; 53549c53ac9SJohnathan Mantey std::string chassisName; 53649c53ac9SJohnathan Mantey for (const std::string& chassis : chassisPaths) 5371abe55efSEd Tanous { 53828aa8de5SGeorge Liu sdbusplus::message::object_path path(chassis); 53928aa8de5SGeorge Liu chassisName = path.filename(); 54028aa8de5SGeorge Liu if (chassisName.empty()) 5411abe55efSEd Tanous { 54249c53ac9SJohnathan Mantey BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis; 543daf36e2eSEd Tanous continue; 544daf36e2eSEd Tanous } 54549c53ac9SJohnathan Mantey if (chassisName == sensorsAsyncResp->chassisId) 5461abe55efSEd Tanous { 54749c53ac9SJohnathan Mantey chassisPath = &chassis; 54849c53ac9SJohnathan Mantey break; 549daf36e2eSEd Tanous } 55049c53ac9SJohnathan Mantey } 55149c53ac9SJohnathan Mantey if (chassisPath == nullptr) 5521abe55efSEd Tanous { 5538d1b46d7Szhanghch05 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, 5548d1b46d7Szhanghch05 "Chassis", sensorsAsyncResp->chassisId); 55549c53ac9SJohnathan Mantey return; 5561abe55efSEd Tanous } 55708777fb0SLewanczyk, Dawid 55849c53ac9SJohnathan Mantey const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode; 559a0ec28b6SAdrian Ambrożewicz if (chassisSubNode == sensors::node::power) 56049c53ac9SJohnathan Mantey { 5618d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] = 56249c53ac9SJohnathan Mantey "#Power.v1_5_2.Power"; 56349c53ac9SJohnathan Mantey } 564a0ec28b6SAdrian Ambrożewicz else if (chassisSubNode == sensors::node::thermal) 56549c53ac9SJohnathan Mantey { 5668d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] = 56749c53ac9SJohnathan Mantey "#Thermal.v1_4_0.Thermal"; 5688d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] = 5698d1b46d7Szhanghch05 nlohmann::json::array(); 5708d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] = 5714f9a2130SJennifer Lee nlohmann::json::array(); 57249c53ac9SJohnathan Mantey } 573a0ec28b6SAdrian Ambrożewicz else if (chassisSubNode == sensors::node::sensors) 57495a3ecadSAnthony Wilson { 5758d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] = 57695a3ecadSAnthony Wilson "#SensorCollection.SensorCollection"; 5778d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Description"] = 57895a3ecadSAnthony Wilson "Collection of Sensors for this Chassis"; 5798d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Members"] = 58095a3ecadSAnthony Wilson nlohmann::json::array(); 5818d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] = 5828d1b46d7Szhanghch05 0; 58395a3ecadSAnthony Wilson } 58495a3ecadSAnthony Wilson 585a0ec28b6SAdrian Ambrożewicz if (chassisSubNode != sensors::node::sensors) 58695a3ecadSAnthony Wilson { 5878d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode; 58895a3ecadSAnthony Wilson } 58995a3ecadSAnthony Wilson 5908d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] = 59149c53ac9SJohnathan Mantey "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" + 59249c53ac9SJohnathan Mantey chassisSubNode; 5938d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode; 5948fb49dd6SShawn McCarney // Get the list of all sensors for this Chassis element 5958fb49dd6SShawn McCarney std::string sensorPath = *chassisPath + "/all_sensors"; 5961e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::vector<std::string>>( 5971e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper", 5981e1e598dSJonathan Doman sensorPath, "xyz.openbmc_project.Association", "endpoints", 59949c53ac9SJohnathan Mantey [sensorsAsyncResp, callback{std::move(callback)}]( 600271584abSEd Tanous const boost::system::error_code& e, 6011e1e598dSJonathan Doman const std::vector<std::string>& nodeSensorList) { 602271584abSEd Tanous if (e) 60349c53ac9SJohnathan Mantey { 604271584abSEd Tanous if (e.value() != EBADR) 60549c53ac9SJohnathan Mantey { 6068d1b46d7Szhanghch05 messages::internalError( 6078d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 60849c53ac9SJohnathan Mantey return; 60949c53ac9SJohnathan Mantey } 61049c53ac9SJohnathan Mantey } 61149c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> 61249c53ac9SJohnathan Mantey culledSensorList = std::make_shared< 61349c53ac9SJohnathan Mantey boost::container::flat_set<std::string>>(); 6141e1e598dSJonathan Doman reduceSensorList(sensorsAsyncResp, &nodeSensorList, 61549c53ac9SJohnathan Mantey culledSensorList); 61649c53ac9SJohnathan Mantey callback(culledSensorList); 6171e1e598dSJonathan Doman }); 61849c53ac9SJohnathan Mantey }; 61949c53ac9SJohnathan Mantey 62049c53ac9SJohnathan Mantey // Get the Chassis Collection 62149c53ac9SJohnathan Mantey crow::connections::systemBus->async_method_call( 62249c53ac9SJohnathan Mantey respHandler, "xyz.openbmc_project.ObjectMapper", 62349c53ac9SJohnathan Mantey "/xyz/openbmc_project/object_mapper", 62449c53ac9SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 625271584abSEd Tanous "/xyz/openbmc_project/inventory", 0, interfaces); 62655c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis exit"; 62708777fb0SLewanczyk, Dawid } 62808777fb0SLewanczyk, Dawid 62908777fb0SLewanczyk, Dawid /** 630de629b6eSShawn McCarney * @brief Finds all DBus object paths that implement ObjectManager. 631de629b6eSShawn McCarney * 632de629b6eSShawn McCarney * Creates a mapping from the associated connection name to the object path. 633de629b6eSShawn McCarney * 634de629b6eSShawn McCarney * Finds the object paths asynchronously. Invokes callback when information has 635de629b6eSShawn McCarney * been obtained. 636de629b6eSShawn McCarney * 637de629b6eSShawn McCarney * The callback must have the following signature: 638de629b6eSShawn McCarney * @code 6398fb49dd6SShawn McCarney * callback(std::shared_ptr<boost::container::flat_map<std::string, 6408fb49dd6SShawn McCarney * std::string>> objectMgrPaths) 641de629b6eSShawn McCarney * @endcode 642de629b6eSShawn McCarney * 64349c53ac9SJohnathan Mantey * @param sensorsAsyncResp Pointer to object holding response data. 644de629b6eSShawn McCarney * @param callback Callback to invoke when object paths obtained. 645de629b6eSShawn McCarney */ 646de629b6eSShawn McCarney template <typename Callback> 647b5a76932SEd Tanous void getObjectManagerPaths( 64881ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 649de629b6eSShawn McCarney Callback&& callback) 650de629b6eSShawn McCarney { 651de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter"; 652de629b6eSShawn McCarney const std::array<std::string, 1> interfaces = { 653de629b6eSShawn McCarney "org.freedesktop.DBus.ObjectManager"}; 654de629b6eSShawn McCarney 655de629b6eSShawn McCarney // Response handler for GetSubTree DBus method 656de629b6eSShawn McCarney auto respHandler = [callback{std::move(callback)}, 65781ce609eSEd Tanous sensorsAsyncResp](const boost::system::error_code ec, 658de629b6eSShawn McCarney const GetSubTreeType& subtree) { 659de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter"; 660de629b6eSShawn McCarney if (ec) 661de629b6eSShawn McCarney { 6628d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 663de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error " 664de629b6eSShawn McCarney << ec; 665de629b6eSShawn McCarney return; 666de629b6eSShawn McCarney } 667de629b6eSShawn McCarney 668de629b6eSShawn McCarney // Loop over returned object paths 6698fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 6708fb49dd6SShawn McCarney objectMgrPaths = std::make_shared< 6718fb49dd6SShawn McCarney boost::container::flat_map<std::string, std::string>>(); 672de629b6eSShawn McCarney for (const std::pair< 673de629b6eSShawn McCarney std::string, 674de629b6eSShawn McCarney std::vector<std::pair<std::string, std::vector<std::string>>>>& 675de629b6eSShawn McCarney object : subtree) 676de629b6eSShawn McCarney { 677de629b6eSShawn McCarney // Loop over connections for current object path 678de629b6eSShawn McCarney const std::string& objectPath = object.first; 679de629b6eSShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 680de629b6eSShawn McCarney objData : object.second) 681de629b6eSShawn McCarney { 682de629b6eSShawn McCarney // Add mapping from connection to object path 683de629b6eSShawn McCarney const std::string& connection = objData.first; 6848fb49dd6SShawn McCarney (*objectMgrPaths)[connection] = objectPath; 685de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> " 686de629b6eSShawn McCarney << objectPath; 687de629b6eSShawn McCarney } 688de629b6eSShawn McCarney } 6898fb49dd6SShawn McCarney callback(objectMgrPaths); 690de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit"; 691de629b6eSShawn McCarney }; 692de629b6eSShawn McCarney 693de629b6eSShawn McCarney // Query mapper for all DBus object paths that implement ObjectManager 694de629b6eSShawn McCarney crow::connections::systemBus->async_method_call( 695de629b6eSShawn McCarney std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 696de629b6eSShawn McCarney "/xyz/openbmc_project/object_mapper", 697271584abSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces); 698de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit"; 699de629b6eSShawn McCarney } 700de629b6eSShawn McCarney 701de629b6eSShawn McCarney /** 702adc4f0dbSShawn McCarney * @brief Returns the Redfish State value for the specified inventory item. 703adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with a sensor. 704adc4f0dbSShawn McCarney * @return State value for inventory item. 70534dd179eSJames Feist */ 70623a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem) 707adc4f0dbSShawn McCarney { 708adc4f0dbSShawn McCarney if ((inventoryItem != nullptr) && !(inventoryItem->isPresent)) 709adc4f0dbSShawn McCarney { 710adc4f0dbSShawn McCarney return "Absent"; 711adc4f0dbSShawn McCarney } 71234dd179eSJames Feist 713adc4f0dbSShawn McCarney return "Enabled"; 714adc4f0dbSShawn McCarney } 715adc4f0dbSShawn McCarney 716adc4f0dbSShawn McCarney /** 717adc4f0dbSShawn McCarney * @brief Returns the Redfish Health value for the specified sensor. 718adc4f0dbSShawn McCarney * @param sensorJson Sensor JSON object. 719adc4f0dbSShawn McCarney * @param interfacesDict Map of all sensor interfaces. 720adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with the sensor. Will 721adc4f0dbSShawn McCarney * be nullptr if no associated inventory item was found. 722adc4f0dbSShawn McCarney * @return Health value for sensor. 723adc4f0dbSShawn McCarney */ 724*711ac7a9SEd Tanous inline std::string 725*711ac7a9SEd Tanous getHealth(nlohmann::json& sensorJson, 726*711ac7a9SEd Tanous const dbus::utility::DBusInteracesMap& interfacesDict, 727adc4f0dbSShawn McCarney const InventoryItem* inventoryItem) 72834dd179eSJames Feist { 729adc4f0dbSShawn McCarney // Get current health value (if any) in the sensor JSON object. Some JSON 730adc4f0dbSShawn McCarney // objects contain multiple sensors (such as PowerSupplies). We want to set 731adc4f0dbSShawn McCarney // the overall health to be the most severe of any of the sensors. 732adc4f0dbSShawn McCarney std::string currentHealth; 733adc4f0dbSShawn McCarney auto statusIt = sensorJson.find("Status"); 734adc4f0dbSShawn McCarney if (statusIt != sensorJson.end()) 735adc4f0dbSShawn McCarney { 736adc4f0dbSShawn McCarney auto healthIt = statusIt->find("Health"); 737adc4f0dbSShawn McCarney if (healthIt != statusIt->end()) 738adc4f0dbSShawn McCarney { 739adc4f0dbSShawn McCarney std::string* health = healthIt->get_ptr<std::string*>(); 740adc4f0dbSShawn McCarney if (health != nullptr) 741adc4f0dbSShawn McCarney { 742adc4f0dbSShawn McCarney currentHealth = *health; 743adc4f0dbSShawn McCarney } 744adc4f0dbSShawn McCarney } 745adc4f0dbSShawn McCarney } 746adc4f0dbSShawn McCarney 747adc4f0dbSShawn McCarney // If current health in JSON object is already Critical, return that. This 748adc4f0dbSShawn McCarney // should override the sensor health, which might be less severe. 749adc4f0dbSShawn McCarney if (currentHealth == "Critical") 750adc4f0dbSShawn McCarney { 751adc4f0dbSShawn McCarney return "Critical"; 752adc4f0dbSShawn McCarney } 753adc4f0dbSShawn McCarney 754adc4f0dbSShawn McCarney // Check if sensor has critical threshold alarm 755*711ac7a9SEd Tanous 756*711ac7a9SEd Tanous for (auto& [interface, values] : interfacesDict) 75734dd179eSJames Feist { 758*711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical") 75934dd179eSJames Feist { 760*711ac7a9SEd Tanous for (auto& [valueName, value] : values) 761*711ac7a9SEd Tanous { 762*711ac7a9SEd Tanous if (valueName == "CriticalAlarmHigh" || 763*711ac7a9SEd Tanous valueName == "CriticalAlarmLow") 764*711ac7a9SEd Tanous { 765*711ac7a9SEd Tanous const bool* asserted = std::get_if<bool>(&value); 76634dd179eSJames Feist if (asserted == nullptr) 76734dd179eSJames Feist { 76834dd179eSJames Feist BMCWEB_LOG_ERROR << "Illegal sensor threshold"; 76934dd179eSJames Feist } 77034dd179eSJames Feist else if (*asserted) 77134dd179eSJames Feist { 77234dd179eSJames Feist return "Critical"; 77334dd179eSJames Feist } 77434dd179eSJames Feist } 77534dd179eSJames Feist } 77634dd179eSJames Feist } 77734dd179eSJames Feist } 77834dd179eSJames Feist 779adc4f0dbSShawn McCarney // Check if associated inventory item is not functional 780adc4f0dbSShawn McCarney if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional)) 781adc4f0dbSShawn McCarney { 782adc4f0dbSShawn McCarney return "Critical"; 783adc4f0dbSShawn McCarney } 784adc4f0dbSShawn McCarney 785adc4f0dbSShawn McCarney // If current health in JSON object is already Warning, return that. This 786adc4f0dbSShawn McCarney // should override the sensor status, which might be less severe. 787adc4f0dbSShawn McCarney if (currentHealth == "Warning") 788adc4f0dbSShawn McCarney { 789adc4f0dbSShawn McCarney return "Warning"; 790adc4f0dbSShawn McCarney } 791adc4f0dbSShawn McCarney 792adc4f0dbSShawn McCarney // Check if sensor has warning threshold alarm 793*711ac7a9SEd Tanous for (auto& [interface, values] : interfacesDict) 79434dd179eSJames Feist { 795*711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning") 79634dd179eSJames Feist { 797*711ac7a9SEd Tanous for (auto& [valueName, value] : values) 798*711ac7a9SEd Tanous { 799*711ac7a9SEd Tanous if (valueName == "WarningAlarmHigh" || 800*711ac7a9SEd Tanous valueName == "WarningAlarmLow") 801*711ac7a9SEd Tanous { 802*711ac7a9SEd Tanous const bool* asserted = std::get_if<bool>(&value); 80334dd179eSJames Feist if (asserted == nullptr) 80434dd179eSJames Feist { 80534dd179eSJames Feist BMCWEB_LOG_ERROR << "Illegal sensor threshold"; 80634dd179eSJames Feist } 80734dd179eSJames Feist else if (*asserted) 80834dd179eSJames Feist { 809*711ac7a9SEd Tanous return "wARNING"; 81034dd179eSJames Feist } 81134dd179eSJames Feist } 81234dd179eSJames Feist } 81334dd179eSJames Feist } 81434dd179eSJames Feist } 815adc4f0dbSShawn McCarney 81634dd179eSJames Feist return "OK"; 81734dd179eSJames Feist } 81834dd179eSJames Feist 81923a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson, 820d500549bSAnthony Wilson const InventoryItem* inventoryItem) 821d500549bSAnthony Wilson { 822d500549bSAnthony Wilson if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty()) 823d500549bSAnthony Wilson { 824d500549bSAnthony Wilson switch (inventoryItem->ledState) 825d500549bSAnthony Wilson { 826d500549bSAnthony Wilson case LedState::OFF: 827d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Off"; 828d500549bSAnthony Wilson break; 829d500549bSAnthony Wilson case LedState::ON: 830d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Lit"; 831d500549bSAnthony Wilson break; 832d500549bSAnthony Wilson case LedState::BLINK: 833d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Blinking"; 834d500549bSAnthony Wilson break; 83523a21a1cSEd Tanous case LedState::UNKNOWN: 836d500549bSAnthony Wilson break; 837d500549bSAnthony Wilson } 838d500549bSAnthony Wilson } 839d500549bSAnthony Wilson } 840d500549bSAnthony Wilson 84134dd179eSJames Feist /** 84208777fb0SLewanczyk, Dawid * @brief Builds a json sensor representation of a sensor. 84308777fb0SLewanczyk, Dawid * @param sensorName The name of the sensor to be built 844274fad5aSGunnar Mills * @param sensorType The type (temperature, fan_tach, etc) of the sensor to 84508777fb0SLewanczyk, Dawid * build 846a0ec28b6SAdrian Ambrożewicz * @param sensorsAsyncResp Sensor metadata 84708777fb0SLewanczyk, Dawid * @param interfacesDict A dictionary of the interfaces and properties of said 84808777fb0SLewanczyk, Dawid * interfaces to be built from 84908777fb0SLewanczyk, Dawid * @param sensor_json The json object to fill 850adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with the sensor. Will 851adc4f0dbSShawn McCarney * be nullptr if no associated inventory item was found. 85208777fb0SLewanczyk, Dawid */ 85323a21a1cSEd Tanous inline void objectInterfacesToJson( 85408777fb0SLewanczyk, Dawid const std::string& sensorName, const std::string& sensorType, 855b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 856*711ac7a9SEd Tanous const dbus::utility::DBusInteracesMap& interfacesDict, 85781ce609eSEd Tanous nlohmann::json& sensorJson, InventoryItem* inventoryItem) 8581abe55efSEd Tanous { 85908777fb0SLewanczyk, Dawid // Assume values exist as is (10^0 == 1) if no scale exists 86008777fb0SLewanczyk, Dawid int64_t scaleMultiplier = 0; 861*711ac7a9SEd Tanous for (auto& [interface, values] : interfacesDict) 8621abe55efSEd Tanous { 863*711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Sensor.Value") 864*711ac7a9SEd Tanous { 865*711ac7a9SEd Tanous for (auto& [valueName, value] : values) 866*711ac7a9SEd Tanous { 867*711ac7a9SEd Tanous if (valueName == "Scale") 868*711ac7a9SEd Tanous { 869*711ac7a9SEd Tanous const int64_t* int64Value = std::get_if<int64_t>(&value); 8701abe55efSEd Tanous if (int64Value != nullptr) 8711abe55efSEd Tanous { 87208777fb0SLewanczyk, Dawid scaleMultiplier = *int64Value; 87308777fb0SLewanczyk, Dawid } 87408777fb0SLewanczyk, Dawid } 875*711ac7a9SEd Tanous } 876*711ac7a9SEd Tanous } 877*711ac7a9SEd Tanous } 87808777fb0SLewanczyk, Dawid 879a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors) 880adc4f0dbSShawn McCarney { 88195a3ecadSAnthony Wilson // For sensors in SensorCollection we set Id instead of MemberId, 88295a3ecadSAnthony Wilson // including power sensors. 88381ce609eSEd Tanous sensorJson["Id"] = sensorName; 88481ce609eSEd Tanous sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " "); 88595a3ecadSAnthony Wilson } 88695a3ecadSAnthony Wilson else if (sensorType != "power") 88795a3ecadSAnthony Wilson { 88895a3ecadSAnthony Wilson // Set MemberId and Name for non-power sensors. For PowerSupplies and 88995a3ecadSAnthony Wilson // PowerControl, those properties have more general values because 89095a3ecadSAnthony Wilson // multiple sensors can be stored in the same JSON object. 89181ce609eSEd Tanous sensorJson["MemberId"] = sensorName; 89281ce609eSEd Tanous sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " "); 893adc4f0dbSShawn McCarney } 894e742b6ccSEd Tanous 89581ce609eSEd Tanous sensorJson["Status"]["State"] = getState(inventoryItem); 89681ce609eSEd Tanous sensorJson["Status"]["Health"] = 89781ce609eSEd Tanous getHealth(sensorJson, interfacesDict, inventoryItem); 89808777fb0SLewanczyk, Dawid 89908777fb0SLewanczyk, Dawid // Parameter to set to override the type we get from dbus, and force it to 90008777fb0SLewanczyk, Dawid // int, regardless of what is available. This is used for schemas like fan, 90108777fb0SLewanczyk, Dawid // that require integers, not floats. 90208777fb0SLewanczyk, Dawid bool forceToInt = false; 90308777fb0SLewanczyk, Dawid 9043929aca1SAnthony Wilson nlohmann::json::json_pointer unit("/Reading"); 905a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors) 90695a3ecadSAnthony Wilson { 90781ce609eSEd Tanous sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor"; 908c2bf7f99SWludzik, Jozef 909c2bf7f99SWludzik, Jozef const std::string& readingType = sensors::toReadingType(sensorType); 910c2bf7f99SWludzik, Jozef if (readingType.empty()) 91195a3ecadSAnthony Wilson { 912c2bf7f99SWludzik, Jozef BMCWEB_LOG_ERROR << "Redfish cannot map reading type for " 913c2bf7f99SWludzik, Jozef << sensorType; 91495a3ecadSAnthony Wilson } 915c2bf7f99SWludzik, Jozef else 91695a3ecadSAnthony Wilson { 917c2bf7f99SWludzik, Jozef sensorJson["ReadingType"] = readingType; 91895a3ecadSAnthony Wilson } 919c2bf7f99SWludzik, Jozef 920c2bf7f99SWludzik, Jozef const std::string& readingUnits = sensors::toReadingUnits(sensorType); 921c2bf7f99SWludzik, Jozef if (readingUnits.empty()) 922f8ede15eSAdrian Ambrożewicz { 923c2bf7f99SWludzik, Jozef BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for " 924c2bf7f99SWludzik, Jozef << sensorType; 925c2bf7f99SWludzik, Jozef } 926c2bf7f99SWludzik, Jozef else 927c2bf7f99SWludzik, Jozef { 928c2bf7f99SWludzik, Jozef sensorJson["ReadingUnits"] = readingUnits; 929f8ede15eSAdrian Ambrożewicz } 93095a3ecadSAnthony Wilson } 93195a3ecadSAnthony Wilson else if (sensorType == "temperature") 9321abe55efSEd Tanous { 9333929aca1SAnthony Wilson unit = "/ReadingCelsius"_json_pointer; 93481ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature"; 93508777fb0SLewanczyk, Dawid // TODO(ed) Documentation says that path should be type fan_tach, 93608777fb0SLewanczyk, Dawid // implementation seems to implement fan 9371abe55efSEd Tanous } 9381abe55efSEd Tanous else if (sensorType == "fan" || sensorType == "fan_tach") 9391abe55efSEd Tanous { 9403929aca1SAnthony Wilson unit = "/Reading"_json_pointer; 94181ce609eSEd Tanous sensorJson["ReadingUnits"] = "RPM"; 94281ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan"; 94381ce609eSEd Tanous setLedState(sensorJson, inventoryItem); 94408777fb0SLewanczyk, Dawid forceToInt = true; 9451abe55efSEd Tanous } 9466f6d0d32SEd Tanous else if (sensorType == "fan_pwm") 9476f6d0d32SEd Tanous { 9483929aca1SAnthony Wilson unit = "/Reading"_json_pointer; 94981ce609eSEd Tanous sensorJson["ReadingUnits"] = "Percent"; 95081ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan"; 95181ce609eSEd Tanous setLedState(sensorJson, inventoryItem); 9526f6d0d32SEd Tanous forceToInt = true; 9536f6d0d32SEd Tanous } 9541abe55efSEd Tanous else if (sensorType == "voltage") 9551abe55efSEd Tanous { 9563929aca1SAnthony Wilson unit = "/ReadingVolts"_json_pointer; 95781ce609eSEd Tanous sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage"; 9581abe55efSEd Tanous } 9592474adfaSEd Tanous else if (sensorType == "power") 9602474adfaSEd Tanous { 96149c53ac9SJohnathan Mantey std::string sensorNameLower = 96249c53ac9SJohnathan Mantey boost::algorithm::to_lower_copy(sensorName); 96349c53ac9SJohnathan Mantey 964028f7ebcSEddie James if (!sensorName.compare("total_power")) 965028f7ebcSEddie James { 96681ce609eSEd Tanous sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl"; 9677ab06f49SGunnar Mills // Put multiple "sensors" into a single PowerControl, so have 9687ab06f49SGunnar Mills // generic names for MemberId and Name. Follows Redfish mockup. 96981ce609eSEd Tanous sensorJson["MemberId"] = "0"; 97081ce609eSEd Tanous sensorJson["Name"] = "Chassis Power Control"; 9713929aca1SAnthony Wilson unit = "/PowerConsumedWatts"_json_pointer; 972028f7ebcSEddie James } 973028f7ebcSEddie James else if (sensorNameLower.find("input") != std::string::npos) 97449c53ac9SJohnathan Mantey { 9753929aca1SAnthony Wilson unit = "/PowerInputWatts"_json_pointer; 97649c53ac9SJohnathan Mantey } 97749c53ac9SJohnathan Mantey else 97849c53ac9SJohnathan Mantey { 9793929aca1SAnthony Wilson unit = "/PowerOutputWatts"_json_pointer; 98049c53ac9SJohnathan Mantey } 9812474adfaSEd Tanous } 9821abe55efSEd Tanous else 9831abe55efSEd Tanous { 98455c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName; 98508777fb0SLewanczyk, Dawid return; 98608777fb0SLewanczyk, Dawid } 98708777fb0SLewanczyk, Dawid // Map of dbus interface name, dbus property name and redfish property_name 9883929aca1SAnthony Wilson std::vector< 9893929aca1SAnthony Wilson std::tuple<const char*, const char*, nlohmann::json::json_pointer>> 9903929aca1SAnthony Wilson properties; 99108777fb0SLewanczyk, Dawid properties.reserve(7); 99208777fb0SLewanczyk, Dawid 99308777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit); 994de629b6eSShawn McCarney 995a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors) 9963929aca1SAnthony Wilson { 9973929aca1SAnthony Wilson properties.emplace_back( 9983929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh", 9993929aca1SAnthony Wilson "/Thresholds/UpperCaution/Reading"_json_pointer); 10003929aca1SAnthony Wilson properties.emplace_back( 10013929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow", 10023929aca1SAnthony Wilson "/Thresholds/LowerCaution/Reading"_json_pointer); 10033929aca1SAnthony Wilson properties.emplace_back( 10043929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh", 10053929aca1SAnthony Wilson "/Thresholds/UpperCritical/Reading"_json_pointer); 10063929aca1SAnthony Wilson properties.emplace_back( 10073929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow", 10083929aca1SAnthony Wilson "/Thresholds/LowerCritical/Reading"_json_pointer); 10093929aca1SAnthony Wilson } 10103929aca1SAnthony Wilson else if (sensorType != "power") 1011de629b6eSShawn McCarney { 101208777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 10133929aca1SAnthony Wilson "WarningHigh", 10143929aca1SAnthony Wilson "/UpperThresholdNonCritical"_json_pointer); 101508777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 10163929aca1SAnthony Wilson "WarningLow", 10173929aca1SAnthony Wilson "/LowerThresholdNonCritical"_json_pointer); 101808777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 10193929aca1SAnthony Wilson "CriticalHigh", 10203929aca1SAnthony Wilson "/UpperThresholdCritical"_json_pointer); 102108777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 10223929aca1SAnthony Wilson "CriticalLow", 10233929aca1SAnthony Wilson "/LowerThresholdCritical"_json_pointer); 1024de629b6eSShawn McCarney } 102508777fb0SLewanczyk, Dawid 10262474adfaSEd Tanous // TODO Need to get UpperThresholdFatal and LowerThresholdFatal 10272474adfaSEd Tanous 1028a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors) 102995a3ecadSAnthony Wilson { 103095a3ecadSAnthony Wilson properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 10313929aca1SAnthony Wilson "/ReadingRangeMin"_json_pointer); 103295a3ecadSAnthony Wilson properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 10333929aca1SAnthony Wilson "/ReadingRangeMax"_json_pointer); 103495a3ecadSAnthony Wilson } 103595a3ecadSAnthony Wilson else if (sensorType == "temperature") 10361abe55efSEd Tanous { 103708777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 10383929aca1SAnthony Wilson "/MinReadingRangeTemp"_json_pointer); 103908777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 10403929aca1SAnthony Wilson "/MaxReadingRangeTemp"_json_pointer); 10411abe55efSEd Tanous } 1042adc4f0dbSShawn McCarney else if (sensorType != "power") 10431abe55efSEd Tanous { 104408777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 10453929aca1SAnthony Wilson "/MinReadingRange"_json_pointer); 104608777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 10473929aca1SAnthony Wilson "/MaxReadingRange"_json_pointer); 104808777fb0SLewanczyk, Dawid } 104908777fb0SLewanczyk, Dawid 10503929aca1SAnthony Wilson for (const std::tuple<const char*, const char*, 10513929aca1SAnthony Wilson nlohmann::json::json_pointer>& p : properties) 10521abe55efSEd Tanous { 1053*711ac7a9SEd Tanous for (auto& [interface, values] : interfacesDict) 10541abe55efSEd Tanous { 1055*711ac7a9SEd Tanous if (interface != std::get<0>(p)) 10561abe55efSEd Tanous { 1057*711ac7a9SEd Tanous continue; 1058*711ac7a9SEd Tanous } 1059*711ac7a9SEd Tanous for (auto& [valueName, valueVariant] : values) 1060*711ac7a9SEd Tanous { 1061*711ac7a9SEd Tanous if (valueName != std::get<1>(p)) 1062*711ac7a9SEd Tanous { 1063*711ac7a9SEd Tanous continue; 1064*711ac7a9SEd Tanous } 10653929aca1SAnthony Wilson 10663929aca1SAnthony Wilson // The property we want to set may be nested json, so use 10673929aca1SAnthony Wilson // a json_pointer for easy indexing into the json structure. 10683929aca1SAnthony Wilson const nlohmann::json::json_pointer& key = std::get<2>(p); 10693929aca1SAnthony Wilson 107008777fb0SLewanczyk, Dawid // Attempt to pull the int64 directly 1071abf2add6SEd Tanous const int64_t* int64Value = std::get_if<int64_t>(&valueVariant); 107208777fb0SLewanczyk, Dawid 1073abf2add6SEd Tanous const double* doubleValue = std::get_if<double>(&valueVariant); 1074028f7ebcSEddie James const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant); 10756f6d0d32SEd Tanous double temp = 0.0; 10766f6d0d32SEd Tanous if (int64Value != nullptr) 10771abe55efSEd Tanous { 1078271584abSEd Tanous temp = static_cast<double>(*int64Value); 10796f6d0d32SEd Tanous } 10806f6d0d32SEd Tanous else if (doubleValue != nullptr) 10811abe55efSEd Tanous { 10826f6d0d32SEd Tanous temp = *doubleValue; 10831abe55efSEd Tanous } 1084028f7ebcSEddie James else if (uValue != nullptr) 1085028f7ebcSEddie James { 1086028f7ebcSEddie James temp = *uValue; 1087028f7ebcSEddie James } 10881abe55efSEd Tanous else 10891abe55efSEd Tanous { 10906f6d0d32SEd Tanous BMCWEB_LOG_ERROR 10916f6d0d32SEd Tanous << "Got value interface that wasn't int or double"; 10926f6d0d32SEd Tanous continue; 109308777fb0SLewanczyk, Dawid } 10946f6d0d32SEd Tanous temp = temp * std::pow(10, scaleMultiplier); 10956f6d0d32SEd Tanous if (forceToInt) 10966f6d0d32SEd Tanous { 109781ce609eSEd Tanous sensorJson[key] = static_cast<int64_t>(temp); 10986f6d0d32SEd Tanous } 10996f6d0d32SEd Tanous else 11006f6d0d32SEd Tanous { 110181ce609eSEd Tanous sensorJson[key] = temp; 110208777fb0SLewanczyk, Dawid } 110308777fb0SLewanczyk, Dawid } 110408777fb0SLewanczyk, Dawid } 110508777fb0SLewanczyk, Dawid } 1106a0ec28b6SAdrian Ambrożewicz 110781ce609eSEd Tanous sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(), 1108a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/" + sensorType + 1109a0ec28b6SAdrian Ambrożewicz "/" + sensorName); 1110a0ec28b6SAdrian Ambrożewicz 111155c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Added sensor " << sensorName; 111208777fb0SLewanczyk, Dawid } 111308777fb0SLewanczyk, Dawid 1114b5a76932SEd Tanous inline void populateFanRedundancy( 1115b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 11168bd25ccdSJames Feist { 11178bd25ccdSJames Feist crow::connections::systemBus->async_method_call( 11188bd25ccdSJames Feist [sensorsAsyncResp](const boost::system::error_code ec, 11198bd25ccdSJames Feist const GetSubTreeType& resp) { 11208bd25ccdSJames Feist if (ec) 11218bd25ccdSJames Feist { 11228bd25ccdSJames Feist return; // don't have to have this interface 11238bd25ccdSJames Feist } 1124e278c18fSEd Tanous for (const std::pair<std::string, 1125e278c18fSEd Tanous std::vector<std::pair< 1126e278c18fSEd Tanous std::string, std::vector<std::string>>>>& 1127e278c18fSEd Tanous pathPair : resp) 11288bd25ccdSJames Feist { 1129e278c18fSEd Tanous const std::string& path = pathPair.first; 1130e278c18fSEd Tanous const std::vector< 1131e278c18fSEd Tanous std::pair<std::string, std::vector<std::string>>>& objDict = 1132e278c18fSEd Tanous pathPair.second; 11338bd25ccdSJames Feist if (objDict.empty()) 11348bd25ccdSJames Feist { 11358bd25ccdSJames Feist continue; // this should be impossible 11368bd25ccdSJames Feist } 11378bd25ccdSJames Feist 11388bd25ccdSJames Feist const std::string& owner = objDict.begin()->first; 11391e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::vector<std::string>>( 11401e1e598dSJonathan Doman *crow::connections::systemBus, 11411e1e598dSJonathan Doman "xyz.openbmc_project.ObjectMapper", path + "/chassis", 11421e1e598dSJonathan Doman "xyz.openbmc_project.Association", "endpoints", 11431e1e598dSJonathan Doman [path, owner, sensorsAsyncResp]( 11441e1e598dSJonathan Doman const boost::system::error_code e, 11451e1e598dSJonathan Doman const std::vector<std::string>& endpoints) { 1146271584abSEd Tanous if (e) 11478bd25ccdSJames Feist { 11488bd25ccdSJames Feist return; // if they don't have an association we 11498bd25ccdSJames Feist // can't tell what chassis is 11508bd25ccdSJames Feist } 11518bd25ccdSJames Feist auto found = std::find_if( 11521e1e598dSJonathan Doman endpoints.begin(), endpoints.end(), 11538bd25ccdSJames Feist [sensorsAsyncResp](const std::string& entry) { 11548bd25ccdSJames Feist return entry.find( 11558bd25ccdSJames Feist sensorsAsyncResp->chassisId) != 11568bd25ccdSJames Feist std::string::npos; 11578bd25ccdSJames Feist }); 11588bd25ccdSJames Feist 11591e1e598dSJonathan Doman if (found == endpoints.end()) 11608bd25ccdSJames Feist { 11618bd25ccdSJames Feist return; 11628bd25ccdSJames Feist } 11638bd25ccdSJames Feist crow::connections::systemBus->async_method_call( 11648bd25ccdSJames Feist [path, sensorsAsyncResp]( 1165271584abSEd Tanous const boost::system::error_code& err, 11668bd25ccdSJames Feist const boost::container::flat_map< 11678bd25ccdSJames Feist std::string, 1168168e20c1SEd Tanous dbus::utility::DbusVariantType>& ret) { 1169271584abSEd Tanous if (err) 11708bd25ccdSJames Feist { 11718bd25ccdSJames Feist return; // don't have to have this 11728bd25ccdSJames Feist // interface 11738bd25ccdSJames Feist } 11748bd25ccdSJames Feist auto findFailures = ret.find("AllowedFailures"); 11758bd25ccdSJames Feist auto findCollection = ret.find("Collection"); 11768bd25ccdSJames Feist auto findStatus = ret.find("Status"); 11778bd25ccdSJames Feist 11788bd25ccdSJames Feist if (findFailures == ret.end() || 11798bd25ccdSJames Feist findCollection == ret.end() || 11808bd25ccdSJames Feist findStatus == ret.end()) 11818bd25ccdSJames Feist { 11828bd25ccdSJames Feist BMCWEB_LOG_ERROR 11838bd25ccdSJames Feist << "Invalid redundancy interface"; 11848bd25ccdSJames Feist messages::internalError( 11858d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 11868bd25ccdSJames Feist return; 11878bd25ccdSJames Feist } 11888bd25ccdSJames Feist 11898bd25ccdSJames Feist auto allowedFailures = std::get_if<uint8_t>( 11908bd25ccdSJames Feist &(findFailures->second)); 11918bd25ccdSJames Feist auto collection = 11928bd25ccdSJames Feist std::get_if<std::vector<std::string>>( 11938bd25ccdSJames Feist &(findCollection->second)); 11948bd25ccdSJames Feist auto status = std::get_if<std::string>( 11958bd25ccdSJames Feist &(findStatus->second)); 11968bd25ccdSJames Feist 11978bd25ccdSJames Feist if (allowedFailures == nullptr || 11988bd25ccdSJames Feist collection == nullptr || status == nullptr) 11998bd25ccdSJames Feist { 12008bd25ccdSJames Feist 12018bd25ccdSJames Feist BMCWEB_LOG_ERROR 12020fda0f12SGeorge Liu << "Invalid redundancy interface types"; 12038bd25ccdSJames Feist messages::internalError( 12048d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 12058bd25ccdSJames Feist return; 12068bd25ccdSJames Feist } 120728aa8de5SGeorge Liu sdbusplus::message::object_path objectPath( 120828aa8de5SGeorge Liu path); 120928aa8de5SGeorge Liu std::string name = objectPath.filename(); 121028aa8de5SGeorge Liu if (name.empty()) 12118bd25ccdSJames Feist { 12128bd25ccdSJames Feist // this should be impossible 12138bd25ccdSJames Feist messages::internalError( 12148d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 12158bd25ccdSJames Feist return; 12168bd25ccdSJames Feist } 12178bd25ccdSJames Feist std::replace(name.begin(), name.end(), '_', 12188bd25ccdSJames Feist ' '); 12198bd25ccdSJames Feist 12208bd25ccdSJames Feist std::string health; 12218bd25ccdSJames Feist 12228bd25ccdSJames Feist if (boost::ends_with(*status, "Full")) 12238bd25ccdSJames Feist { 12248bd25ccdSJames Feist health = "OK"; 12258bd25ccdSJames Feist } 12268bd25ccdSJames Feist else if (boost::ends_with(*status, "Degraded")) 12278bd25ccdSJames Feist { 12288bd25ccdSJames Feist health = "Warning"; 12298bd25ccdSJames Feist } 12308bd25ccdSJames Feist else 12318bd25ccdSJames Feist { 12328bd25ccdSJames Feist health = "Critical"; 12338bd25ccdSJames Feist } 12348bd25ccdSJames Feist std::vector<nlohmann::json> redfishCollection; 12358bd25ccdSJames Feist const auto& fanRedfish = 12368d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res 12378d1b46d7Szhanghch05 .jsonValue["Fans"]; 12388bd25ccdSJames Feist for (const std::string& item : *collection) 12398bd25ccdSJames Feist { 124028aa8de5SGeorge Liu sdbusplus::message::object_path path(item); 124128aa8de5SGeorge Liu std::string itemName = path.filename(); 124228aa8de5SGeorge Liu if (itemName.empty()) 124328aa8de5SGeorge Liu { 124428aa8de5SGeorge Liu continue; 124528aa8de5SGeorge Liu } 12468bd25ccdSJames Feist /* 12478bd25ccdSJames Feist todo(ed): merge patch that fixes the names 12488bd25ccdSJames Feist std::replace(itemName.begin(), 12498bd25ccdSJames Feist itemName.end(), '_', ' ');*/ 12508bd25ccdSJames Feist auto schemaItem = std::find_if( 12518bd25ccdSJames Feist fanRedfish.begin(), fanRedfish.end(), 12528bd25ccdSJames Feist [itemName](const nlohmann::json& fan) { 12538bd25ccdSJames Feist return fan["MemberId"] == itemName; 12548bd25ccdSJames Feist }); 12558bd25ccdSJames Feist if (schemaItem != fanRedfish.end()) 12568bd25ccdSJames Feist { 12578bd25ccdSJames Feist redfishCollection.push_back( 12588bd25ccdSJames Feist {{"@odata.id", 12598bd25ccdSJames Feist (*schemaItem)["@odata.id"]}}); 12608bd25ccdSJames Feist } 12618bd25ccdSJames Feist else 12628bd25ccdSJames Feist { 12638bd25ccdSJames Feist BMCWEB_LOG_ERROR 12648bd25ccdSJames Feist << "failed to find fan in schema"; 12658bd25ccdSJames Feist messages::internalError( 12668d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 12678bd25ccdSJames Feist return; 12688bd25ccdSJames Feist } 12698bd25ccdSJames Feist } 12708bd25ccdSJames Feist 12713e9e72ebSKuiying Wang size_t minNumNeeded = 12723e9e72ebSKuiying Wang collection->size() > 0 12733e9e72ebSKuiying Wang ? collection->size() - *allowedFailures 12743e9e72ebSKuiying Wang : 0; 1275271584abSEd Tanous nlohmann::json& jResp = 12768d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res 12778bd25ccdSJames Feist .jsonValue["Redundancy"]; 1278271584abSEd Tanous jResp.push_back( 12798bd25ccdSJames Feist {{"@odata.id", 1280717794d5SAppaRao Puli "/redfish/v1/Chassis/" + 12818bd25ccdSJames Feist sensorsAsyncResp->chassisId + "/" + 12828bd25ccdSJames Feist sensorsAsyncResp->chassisSubNode + 12838bd25ccdSJames Feist "#/Redundancy/" + 1284271584abSEd Tanous std::to_string(jResp.size())}, 12858bd25ccdSJames Feist {"@odata.type", 12868bd25ccdSJames Feist "#Redundancy.v1_3_2.Redundancy"}, 12873e9e72ebSKuiying Wang {"MinNumNeeded", minNumNeeded}, 12888bd25ccdSJames Feist {"MemberId", name}, 12898bd25ccdSJames Feist {"Mode", "N+m"}, 12908bd25ccdSJames Feist {"Name", name}, 12918bd25ccdSJames Feist {"RedundancySet", redfishCollection}, 12928bd25ccdSJames Feist {"Status", 12938bd25ccdSJames Feist {{"Health", health}, 12948bd25ccdSJames Feist {"State", "Enabled"}}}}); 12958bd25ccdSJames Feist }, 12968bd25ccdSJames Feist owner, path, "org.freedesktop.DBus.Properties", 12978bd25ccdSJames Feist "GetAll", 12988bd25ccdSJames Feist "xyz.openbmc_project.Control.FanRedundancy"); 12991e1e598dSJonathan Doman }); 13008bd25ccdSJames Feist } 13018bd25ccdSJames Feist }, 13028bd25ccdSJames Feist "xyz.openbmc_project.ObjectMapper", 13038bd25ccdSJames Feist "/xyz/openbmc_project/object_mapper", 13048bd25ccdSJames Feist "xyz.openbmc_project.ObjectMapper", "GetSubTree", 13058bd25ccdSJames Feist "/xyz/openbmc_project/control", 2, 13068bd25ccdSJames Feist std::array<const char*, 1>{ 13078bd25ccdSJames Feist "xyz.openbmc_project.Control.FanRedundancy"}); 13088bd25ccdSJames Feist } 13098bd25ccdSJames Feist 1310b5a76932SEd Tanous inline void 131181ce609eSEd Tanous sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 131249c53ac9SJohnathan Mantey { 13138d1b46d7Szhanghch05 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue; 131449c53ac9SJohnathan Mantey std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"}; 131581ce609eSEd Tanous if (sensorsAsyncResp->chassisSubNode == sensors::node::power) 131649c53ac9SJohnathan Mantey { 131749c53ac9SJohnathan Mantey sensorHeaders = {"Voltages", "PowerSupplies"}; 131849c53ac9SJohnathan Mantey } 131949c53ac9SJohnathan Mantey for (const std::string& sensorGroup : sensorHeaders) 132049c53ac9SJohnathan Mantey { 132149c53ac9SJohnathan Mantey nlohmann::json::iterator entry = response.find(sensorGroup); 132249c53ac9SJohnathan Mantey if (entry != response.end()) 132349c53ac9SJohnathan Mantey { 132449c53ac9SJohnathan Mantey std::sort(entry->begin(), entry->end(), 132549c53ac9SJohnathan Mantey [](nlohmann::json& c1, nlohmann::json& c2) { 132649c53ac9SJohnathan Mantey return c1["Name"] < c2["Name"]; 132749c53ac9SJohnathan Mantey }); 132849c53ac9SJohnathan Mantey 132949c53ac9SJohnathan Mantey // add the index counts to the end of each entry 133049c53ac9SJohnathan Mantey size_t count = 0; 133149c53ac9SJohnathan Mantey for (nlohmann::json& sensorJson : *entry) 133249c53ac9SJohnathan Mantey { 133349c53ac9SJohnathan Mantey nlohmann::json::iterator odata = sensorJson.find("@odata.id"); 133449c53ac9SJohnathan Mantey if (odata == sensorJson.end()) 133549c53ac9SJohnathan Mantey { 133649c53ac9SJohnathan Mantey continue; 133749c53ac9SJohnathan Mantey } 133849c53ac9SJohnathan Mantey std::string* value = odata->get_ptr<std::string*>(); 133949c53ac9SJohnathan Mantey if (value != nullptr) 134049c53ac9SJohnathan Mantey { 134149c53ac9SJohnathan Mantey *value += std::to_string(count); 134249c53ac9SJohnathan Mantey count++; 134381ce609eSEd Tanous sensorsAsyncResp->updateUri(sensorJson["Name"], *value); 134449c53ac9SJohnathan Mantey } 134549c53ac9SJohnathan Mantey } 134649c53ac9SJohnathan Mantey } 134749c53ac9SJohnathan Mantey } 134849c53ac9SJohnathan Mantey } 134949c53ac9SJohnathan Mantey 135008777fb0SLewanczyk, Dawid /** 1351adc4f0dbSShawn McCarney * @brief Finds the inventory item with the specified object path. 1352adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1353adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 1354adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 13558fb49dd6SShawn McCarney */ 135623a21a1cSEd Tanous inline InventoryItem* findInventoryItem( 1357b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1358adc4f0dbSShawn McCarney const std::string& invItemObjPath) 13598fb49dd6SShawn McCarney { 1360adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 13618fb49dd6SShawn McCarney { 1362adc4f0dbSShawn McCarney if (inventoryItem.objectPath == invItemObjPath) 13638fb49dd6SShawn McCarney { 1364adc4f0dbSShawn McCarney return &inventoryItem; 13658fb49dd6SShawn McCarney } 13668fb49dd6SShawn McCarney } 13678fb49dd6SShawn McCarney return nullptr; 13688fb49dd6SShawn McCarney } 13698fb49dd6SShawn McCarney 13708fb49dd6SShawn McCarney /** 1371adc4f0dbSShawn McCarney * @brief Finds the inventory item associated with the specified sensor. 1372adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1373adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor. 1374adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 13758fb49dd6SShawn McCarney */ 137623a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor( 1377b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1378adc4f0dbSShawn McCarney const std::string& sensorObjPath) 1379adc4f0dbSShawn McCarney { 1380adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 1381adc4f0dbSShawn McCarney { 1382adc4f0dbSShawn McCarney if (inventoryItem.sensors.count(sensorObjPath) > 0) 1383adc4f0dbSShawn McCarney { 1384adc4f0dbSShawn McCarney return &inventoryItem; 1385adc4f0dbSShawn McCarney } 1386adc4f0dbSShawn McCarney } 1387adc4f0dbSShawn McCarney return nullptr; 1388adc4f0dbSShawn McCarney } 1389adc4f0dbSShawn McCarney 1390adc4f0dbSShawn McCarney /** 1391d500549bSAnthony Wilson * @brief Finds the inventory item associated with the specified led path. 1392d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1393d500549bSAnthony Wilson * @param ledObjPath D-Bus object path of led. 1394d500549bSAnthony Wilson * @return Inventory item within vector, or nullptr if no match found. 1395d500549bSAnthony Wilson */ 1396d500549bSAnthony Wilson inline InventoryItem* 1397d500549bSAnthony Wilson findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems, 1398d500549bSAnthony Wilson const std::string& ledObjPath) 1399d500549bSAnthony Wilson { 1400d500549bSAnthony Wilson for (InventoryItem& inventoryItem : inventoryItems) 1401d500549bSAnthony Wilson { 1402d500549bSAnthony Wilson if (inventoryItem.ledObjectPath == ledObjPath) 1403d500549bSAnthony Wilson { 1404d500549bSAnthony Wilson return &inventoryItem; 1405d500549bSAnthony Wilson } 1406d500549bSAnthony Wilson } 1407d500549bSAnthony Wilson return nullptr; 1408d500549bSAnthony Wilson } 1409d500549bSAnthony Wilson 1410d500549bSAnthony Wilson /** 1411adc4f0dbSShawn McCarney * @brief Adds inventory item and associated sensor to specified vector. 1412adc4f0dbSShawn McCarney * 1413adc4f0dbSShawn McCarney * Adds a new InventoryItem to the vector if necessary. Searches for an 1414adc4f0dbSShawn McCarney * existing InventoryItem with the specified object path. If not found, one is 1415adc4f0dbSShawn McCarney * added to the vector. 1416adc4f0dbSShawn McCarney * 1417adc4f0dbSShawn McCarney * Next, the specified sensor is added to the set of sensors associated with the 1418adc4f0dbSShawn McCarney * InventoryItem. 1419adc4f0dbSShawn McCarney * 1420adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1421adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 1422adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor 1423adc4f0dbSShawn McCarney */ 1424b5a76932SEd Tanous inline void addInventoryItem( 1425b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1426b5a76932SEd Tanous const std::string& invItemObjPath, const std::string& sensorObjPath) 1427adc4f0dbSShawn McCarney { 1428adc4f0dbSShawn McCarney // Look for inventory item in vector 1429adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1430adc4f0dbSShawn McCarney findInventoryItem(inventoryItems, invItemObjPath); 1431adc4f0dbSShawn McCarney 1432adc4f0dbSShawn McCarney // If inventory item doesn't exist in vector, add it 1433adc4f0dbSShawn McCarney if (inventoryItem == nullptr) 1434adc4f0dbSShawn McCarney { 1435adc4f0dbSShawn McCarney inventoryItems->emplace_back(invItemObjPath); 1436adc4f0dbSShawn McCarney inventoryItem = &(inventoryItems->back()); 1437adc4f0dbSShawn McCarney } 1438adc4f0dbSShawn McCarney 1439adc4f0dbSShawn McCarney // Add sensor to set of sensors associated with inventory item 1440adc4f0dbSShawn McCarney inventoryItem->sensors.emplace(sensorObjPath); 1441adc4f0dbSShawn McCarney } 1442adc4f0dbSShawn McCarney 1443adc4f0dbSShawn McCarney /** 1444adc4f0dbSShawn McCarney * @brief Stores D-Bus data in the specified inventory item. 1445adc4f0dbSShawn McCarney * 1446adc4f0dbSShawn McCarney * Finds D-Bus data in the specified map of interfaces. Stores the data in the 1447adc4f0dbSShawn McCarney * specified InventoryItem. 1448adc4f0dbSShawn McCarney * 1449adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1450adc4f0dbSShawn McCarney * response. 1451adc4f0dbSShawn McCarney * 1452adc4f0dbSShawn McCarney * @param inventoryItem Inventory item where data will be stored. 1453adc4f0dbSShawn McCarney * @param interfacesDict Map containing D-Bus interfaces and their properties 1454adc4f0dbSShawn McCarney * for the specified inventory item. 1455adc4f0dbSShawn McCarney */ 145623a21a1cSEd Tanous inline void storeInventoryItemData( 1457adc4f0dbSShawn McCarney InventoryItem& inventoryItem, 1458*711ac7a9SEd Tanous const dbus::utility::DBusInteracesMap& interfacesDict) 14598fb49dd6SShawn McCarney { 1460adc4f0dbSShawn McCarney // Get properties from Inventory.Item interface 1461*711ac7a9SEd Tanous 1462*711ac7a9SEd Tanous for (auto& [interface, values] : interfacesDict) 14638fb49dd6SShawn McCarney { 1464*711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Item") 14658fb49dd6SShawn McCarney { 1466*711ac7a9SEd Tanous for (auto& [name, dbusValue] : values) 1467*711ac7a9SEd Tanous { 1468*711ac7a9SEd Tanous if (name == "Present") 1469*711ac7a9SEd Tanous { 1470*711ac7a9SEd Tanous const bool* value = std::get_if<bool>(&dbusValue); 1471adc4f0dbSShawn McCarney if (value != nullptr) 14728fb49dd6SShawn McCarney { 1473adc4f0dbSShawn McCarney inventoryItem.isPresent = *value; 14748fb49dd6SShawn McCarney } 14758fb49dd6SShawn McCarney } 14768fb49dd6SShawn McCarney } 1477*711ac7a9SEd Tanous } 1478adc4f0dbSShawn McCarney // Check if Inventory.Item.PowerSupply interface is present 1479*711ac7a9SEd Tanous 1480*711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply") 14818fb49dd6SShawn McCarney { 1482adc4f0dbSShawn McCarney inventoryItem.isPowerSupply = true; 14838fb49dd6SShawn McCarney } 1484adc4f0dbSShawn McCarney 1485adc4f0dbSShawn McCarney // Get properties from Inventory.Decorator.Asset interface 1486*711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset") 1487adc4f0dbSShawn McCarney { 1488*711ac7a9SEd Tanous for (auto& [name, dbusValue] : values) 1489*711ac7a9SEd Tanous { 1490*711ac7a9SEd Tanous if (name == "Manufacturer") 1491adc4f0dbSShawn McCarney { 1492adc4f0dbSShawn McCarney const std::string* value = 1493*711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1494adc4f0dbSShawn McCarney if (value != nullptr) 1495adc4f0dbSShawn McCarney { 1496adc4f0dbSShawn McCarney inventoryItem.manufacturer = *value; 1497adc4f0dbSShawn McCarney } 1498adc4f0dbSShawn McCarney } 1499*711ac7a9SEd Tanous if (name == "Model") 1500adc4f0dbSShawn McCarney { 1501adc4f0dbSShawn McCarney const std::string* value = 1502*711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1503adc4f0dbSShawn McCarney if (value != nullptr) 1504adc4f0dbSShawn McCarney { 1505adc4f0dbSShawn McCarney inventoryItem.model = *value; 1506adc4f0dbSShawn McCarney } 1507adc4f0dbSShawn McCarney } 1508*711ac7a9SEd Tanous if (name == "SerialNumber") 1509adc4f0dbSShawn McCarney { 1510adc4f0dbSShawn McCarney const std::string* value = 1511*711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1512adc4f0dbSShawn McCarney if (value != nullptr) 1513adc4f0dbSShawn McCarney { 1514adc4f0dbSShawn McCarney inventoryItem.serialNumber = *value; 1515adc4f0dbSShawn McCarney } 1516adc4f0dbSShawn McCarney } 1517*711ac7a9SEd Tanous if (name == "PartNumber") 1518*711ac7a9SEd Tanous { 1519*711ac7a9SEd Tanous const std::string* value = 1520*711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1521*711ac7a9SEd Tanous if (value != nullptr) 1522*711ac7a9SEd Tanous { 1523*711ac7a9SEd Tanous inventoryItem.partNumber = *value; 1524*711ac7a9SEd Tanous } 1525*711ac7a9SEd Tanous } 1526*711ac7a9SEd Tanous } 1527adc4f0dbSShawn McCarney } 1528adc4f0dbSShawn McCarney 1529*711ac7a9SEd Tanous if (interface == 1530*711ac7a9SEd Tanous "xyz.openbmc_project.State.Decorator.OperationalStatus") 1531adc4f0dbSShawn McCarney { 1532*711ac7a9SEd Tanous for (auto& [name, dbusValue] : values) 1533adc4f0dbSShawn McCarney { 1534*711ac7a9SEd Tanous if (name == "Functional") 1535*711ac7a9SEd Tanous { 1536*711ac7a9SEd Tanous const bool* value = std::get_if<bool>(&dbusValue); 1537adc4f0dbSShawn McCarney if (value != nullptr) 1538adc4f0dbSShawn McCarney { 1539adc4f0dbSShawn McCarney inventoryItem.isFunctional = *value; 15408fb49dd6SShawn McCarney } 15418fb49dd6SShawn McCarney } 15428fb49dd6SShawn McCarney } 15438fb49dd6SShawn McCarney } 1544*711ac7a9SEd Tanous } 1545*711ac7a9SEd Tanous } 15468fb49dd6SShawn McCarney 15478fb49dd6SShawn McCarney /** 1548adc4f0dbSShawn McCarney * @brief Gets D-Bus data for inventory items associated with sensors. 15498fb49dd6SShawn McCarney * 1550adc4f0dbSShawn McCarney * Uses the specified connections (services) to obtain D-Bus data for inventory 1551adc4f0dbSShawn McCarney * items associated with sensors. Stores the resulting data in the 1552adc4f0dbSShawn McCarney * inventoryItems vector. 15538fb49dd6SShawn McCarney * 1554adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1555adc4f0dbSShawn McCarney * response. 1556adc4f0dbSShawn McCarney * 1557adc4f0dbSShawn McCarney * Finds the inventory item data asynchronously. Invokes callback when data has 1558adc4f0dbSShawn McCarney * been obtained. 1559adc4f0dbSShawn McCarney * 1560adc4f0dbSShawn McCarney * The callback must have the following signature: 1561adc4f0dbSShawn McCarney * @code 1562d500549bSAnthony Wilson * callback(void) 1563adc4f0dbSShawn McCarney * @endcode 1564adc4f0dbSShawn McCarney * 1565adc4f0dbSShawn McCarney * This function is called recursively, obtaining data asynchronously from one 1566adc4f0dbSShawn McCarney * connection in each call. This ensures the callback is not invoked until the 1567adc4f0dbSShawn McCarney * last asynchronous function has completed. 15688fb49dd6SShawn McCarney * 15698fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1570adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1571adc4f0dbSShawn McCarney * @param invConnections Connections that provide data for the inventory items. 15728fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 15738fb49dd6SShawn McCarney * implements ObjectManager. 1574adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory data has been obtained. 1575adc4f0dbSShawn McCarney * @param invConnectionsIndex Current index in invConnections. Only specified 1576adc4f0dbSShawn McCarney * in recursive calls to this function. 15778fb49dd6SShawn McCarney */ 1578adc4f0dbSShawn McCarney template <typename Callback> 1579adc4f0dbSShawn McCarney static void getInventoryItemsData( 15808fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1581adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 15828fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_set<std::string>> invConnections, 15838fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 1584adc4f0dbSShawn McCarney objectMgrPaths, 1585271584abSEd Tanous Callback&& callback, size_t invConnectionsIndex = 0) 15868fb49dd6SShawn McCarney { 1587adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData enter"; 15888fb49dd6SShawn McCarney 1589adc4f0dbSShawn McCarney // If no more connections left, call callback 1590adc4f0dbSShawn McCarney if (invConnectionsIndex >= invConnections->size()) 15918fb49dd6SShawn McCarney { 1592d500549bSAnthony Wilson callback(); 1593adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData exit"; 1594adc4f0dbSShawn McCarney return; 1595adc4f0dbSShawn McCarney } 1596adc4f0dbSShawn McCarney 1597adc4f0dbSShawn McCarney // Get inventory item data from current connection 1598adc4f0dbSShawn McCarney auto it = invConnections->nth(invConnectionsIndex); 1599adc4f0dbSShawn McCarney if (it != invConnections->end()) 1600adc4f0dbSShawn McCarney { 1601adc4f0dbSShawn McCarney const std::string& invConnection = *it; 1602adc4f0dbSShawn McCarney 16038fb49dd6SShawn McCarney // Response handler for GetManagedObjects 1604adc4f0dbSShawn McCarney auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections, 1605adc4f0dbSShawn McCarney objectMgrPaths, callback{std::move(callback)}, 1606adc4f0dbSShawn McCarney invConnectionsIndex]( 1607adc4f0dbSShawn McCarney const boost::system::error_code ec, 1608*711ac7a9SEd Tanous dbus::utility::ManagedObjectType& resp) { 1609adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter"; 16108fb49dd6SShawn McCarney if (ec) 16118fb49dd6SShawn McCarney { 16128fb49dd6SShawn McCarney BMCWEB_LOG_ERROR 1613adc4f0dbSShawn McCarney << "getInventoryItemsData respHandler DBus error " << ec; 16148d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 16158fb49dd6SShawn McCarney return; 16168fb49dd6SShawn McCarney } 16178fb49dd6SShawn McCarney 16188fb49dd6SShawn McCarney // Loop through returned object paths 16198fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 16208fb49dd6SShawn McCarney { 16218fb49dd6SShawn McCarney const std::string& objPath = 16228fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 16238fb49dd6SShawn McCarney 1624adc4f0dbSShawn McCarney // If this object path is one of the specified inventory items 1625adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1626adc4f0dbSShawn McCarney findInventoryItem(inventoryItems, objPath); 1627adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 16288fb49dd6SShawn McCarney { 1629adc4f0dbSShawn McCarney // Store inventory data in InventoryItem 1630adc4f0dbSShawn McCarney storeInventoryItemData(*inventoryItem, objDictEntry.second); 16318fb49dd6SShawn McCarney } 16328fb49dd6SShawn McCarney } 16338fb49dd6SShawn McCarney 1634adc4f0dbSShawn McCarney // Recurse to get inventory item data from next connection 1635adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 1636adc4f0dbSShawn McCarney invConnections, objectMgrPaths, 1637adc4f0dbSShawn McCarney std::move(callback), invConnectionsIndex + 1); 1638adc4f0dbSShawn McCarney 1639adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit"; 16408fb49dd6SShawn McCarney }; 16418fb49dd6SShawn McCarney 16428fb49dd6SShawn McCarney // Find DBus object path that implements ObjectManager for the current 16438fb49dd6SShawn McCarney // connection. If no mapping found, default to "/". 16448fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(invConnection); 16458fb49dd6SShawn McCarney const std::string& objectMgrPath = 16468fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 16478fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is " 16488fb49dd6SShawn McCarney << objectMgrPath; 16498fb49dd6SShawn McCarney 16508fb49dd6SShawn McCarney // Get all object paths and their interfaces for current connection 16518fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 16528fb49dd6SShawn McCarney std::move(respHandler), invConnection, objectMgrPath, 16538fb49dd6SShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 16548fb49dd6SShawn McCarney } 16558fb49dd6SShawn McCarney 1656adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData exit"; 16578fb49dd6SShawn McCarney } 16588fb49dd6SShawn McCarney 16598fb49dd6SShawn McCarney /** 1660adc4f0dbSShawn McCarney * @brief Gets connections that provide D-Bus data for inventory items. 16618fb49dd6SShawn McCarney * 1662adc4f0dbSShawn McCarney * Gets the D-Bus connections (services) that provide data for the inventory 1663adc4f0dbSShawn McCarney * items that are associated with sensors. 16648fb49dd6SShawn McCarney * 16658fb49dd6SShawn McCarney * Finds the connections asynchronously. Invokes callback when information has 16668fb49dd6SShawn McCarney * been obtained. 16678fb49dd6SShawn McCarney * 16688fb49dd6SShawn McCarney * The callback must have the following signature: 16698fb49dd6SShawn McCarney * @code 16708fb49dd6SShawn McCarney * callback(std::shared_ptr<boost::container::flat_set<std::string>> 16718fb49dd6SShawn McCarney * invConnections) 16728fb49dd6SShawn McCarney * @endcode 16738fb49dd6SShawn McCarney * 16748fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1675adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 16768fb49dd6SShawn McCarney * @param callback Callback to invoke when connections have been obtained. 16778fb49dd6SShawn McCarney */ 16788fb49dd6SShawn McCarney template <typename Callback> 16798fb49dd6SShawn McCarney static void getInventoryItemsConnections( 1680b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1681b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 16828fb49dd6SShawn McCarney Callback&& callback) 16838fb49dd6SShawn McCarney { 16848fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter"; 16858fb49dd6SShawn McCarney 16868fb49dd6SShawn McCarney const std::string path = "/xyz/openbmc_project/inventory"; 1687adc4f0dbSShawn McCarney const std::array<std::string, 4> interfaces = { 16888fb49dd6SShawn McCarney "xyz.openbmc_project.Inventory.Item", 1689adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.PowerSupply", 1690adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Decorator.Asset", 16918fb49dd6SShawn McCarney "xyz.openbmc_project.State.Decorator.OperationalStatus"}; 16928fb49dd6SShawn McCarney 16938fb49dd6SShawn McCarney // Response handler for parsing output from GetSubTree 16948fb49dd6SShawn McCarney auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp, 1695adc4f0dbSShawn McCarney inventoryItems](const boost::system::error_code ec, 16968fb49dd6SShawn McCarney const GetSubTreeType& subtree) { 16978fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter"; 16988fb49dd6SShawn McCarney if (ec) 16998fb49dd6SShawn McCarney { 17008d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 17018fb49dd6SShawn McCarney BMCWEB_LOG_ERROR 17028fb49dd6SShawn McCarney << "getInventoryItemsConnections respHandler DBus error " << ec; 17038fb49dd6SShawn McCarney return; 17048fb49dd6SShawn McCarney } 17058fb49dd6SShawn McCarney 17068fb49dd6SShawn McCarney // Make unique list of connections for desired inventory items 17078fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_set<std::string>> 17088fb49dd6SShawn McCarney invConnections = 17098fb49dd6SShawn McCarney std::make_shared<boost::container::flat_set<std::string>>(); 17108fb49dd6SShawn McCarney invConnections->reserve(8); 17118fb49dd6SShawn McCarney 17128fb49dd6SShawn McCarney // Loop through objects from GetSubTree 17138fb49dd6SShawn McCarney for (const std::pair< 17148fb49dd6SShawn McCarney std::string, 17158fb49dd6SShawn McCarney std::vector<std::pair<std::string, std::vector<std::string>>>>& 17168fb49dd6SShawn McCarney object : subtree) 17178fb49dd6SShawn McCarney { 1718adc4f0dbSShawn McCarney // Check if object path is one of the specified inventory items 17198fb49dd6SShawn McCarney const std::string& objPath = object.first; 1720adc4f0dbSShawn McCarney if (findInventoryItem(inventoryItems, objPath) != nullptr) 17218fb49dd6SShawn McCarney { 17228fb49dd6SShawn McCarney // Store all connections to inventory item 17238fb49dd6SShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 17248fb49dd6SShawn McCarney objData : object.second) 17258fb49dd6SShawn McCarney { 17268fb49dd6SShawn McCarney const std::string& invConnection = objData.first; 17278fb49dd6SShawn McCarney invConnections->insert(invConnection); 17288fb49dd6SShawn McCarney } 17298fb49dd6SShawn McCarney } 17308fb49dd6SShawn McCarney } 1731d500549bSAnthony Wilson 17328fb49dd6SShawn McCarney callback(invConnections); 17338fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit"; 17348fb49dd6SShawn McCarney }; 17358fb49dd6SShawn McCarney 17368fb49dd6SShawn McCarney // Make call to ObjectMapper to find all inventory items 17378fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 17388fb49dd6SShawn McCarney std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 17398fb49dd6SShawn McCarney "/xyz/openbmc_project/object_mapper", 17408fb49dd6SShawn McCarney "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); 17418fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit"; 17428fb49dd6SShawn McCarney } 17438fb49dd6SShawn McCarney 17448fb49dd6SShawn McCarney /** 1745adc4f0dbSShawn McCarney * @brief Gets associations from sensors to inventory items. 17468fb49dd6SShawn McCarney * 17478fb49dd6SShawn McCarney * Looks for ObjectMapper associations from the specified sensors to related 1748d500549bSAnthony Wilson * inventory items. Then finds the associations from those inventory items to 1749d500549bSAnthony Wilson * their LEDs, if any. 17508fb49dd6SShawn McCarney * 17518fb49dd6SShawn McCarney * Finds the inventory items asynchronously. Invokes callback when information 17528fb49dd6SShawn McCarney * has been obtained. 17538fb49dd6SShawn McCarney * 17548fb49dd6SShawn McCarney * The callback must have the following signature: 17558fb49dd6SShawn McCarney * @code 1756adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 17578fb49dd6SShawn McCarney * @endcode 17588fb49dd6SShawn McCarney * 17598fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 17608fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 17618fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 17628fb49dd6SShawn McCarney * implements ObjectManager. 17638fb49dd6SShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 17648fb49dd6SShawn McCarney */ 17658fb49dd6SShawn McCarney template <typename Callback> 1766adc4f0dbSShawn McCarney static void getInventoryItemAssociations( 1767b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1768b5a76932SEd Tanous const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames, 1769b5a76932SEd Tanous const std::shared_ptr<boost::container::flat_map<std::string, std::string>>& 17708fb49dd6SShawn McCarney objectMgrPaths, 17718fb49dd6SShawn McCarney Callback&& callback) 17728fb49dd6SShawn McCarney { 1773adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter"; 17748fb49dd6SShawn McCarney 17758fb49dd6SShawn McCarney // Response handler for GetManagedObjects 17768fb49dd6SShawn McCarney auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp, 17778fb49dd6SShawn McCarney sensorNames](const boost::system::error_code ec, 17788fb49dd6SShawn McCarney dbus::utility::ManagedObjectType& resp) { 1779adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter"; 17808fb49dd6SShawn McCarney if (ec) 17818fb49dd6SShawn McCarney { 1782adc4f0dbSShawn McCarney BMCWEB_LOG_ERROR 1783adc4f0dbSShawn McCarney << "getInventoryItemAssociations respHandler DBus error " << ec; 17848d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 17858fb49dd6SShawn McCarney return; 17868fb49dd6SShawn McCarney } 17878fb49dd6SShawn McCarney 1788adc4f0dbSShawn McCarney // Create vector to hold list of inventory items 1789adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems = 1790adc4f0dbSShawn McCarney std::make_shared<std::vector<InventoryItem>>(); 1791adc4f0dbSShawn McCarney 17928fb49dd6SShawn McCarney // Loop through returned object paths 17938fb49dd6SShawn McCarney std::string sensorAssocPath; 17948fb49dd6SShawn McCarney sensorAssocPath.reserve(128); // avoid memory allocations 17958fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 17968fb49dd6SShawn McCarney { 17978fb49dd6SShawn McCarney const std::string& objPath = 17988fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 17998fb49dd6SShawn McCarney 18008fb49dd6SShawn McCarney // If path is inventory association for one of the specified sensors 18018fb49dd6SShawn McCarney for (const std::string& sensorName : *sensorNames) 18028fb49dd6SShawn McCarney { 18038fb49dd6SShawn McCarney sensorAssocPath = sensorName; 18048fb49dd6SShawn McCarney sensorAssocPath += "/inventory"; 18058fb49dd6SShawn McCarney if (objPath == sensorAssocPath) 18068fb49dd6SShawn McCarney { 18078fb49dd6SShawn McCarney // Get Association interface for object path 1808*711ac7a9SEd Tanous for (const auto& [interface, values] : objDictEntry.second) 18098fb49dd6SShawn McCarney { 1810*711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Association") 1811*711ac7a9SEd Tanous { 1812*711ac7a9SEd Tanous for (const auto& [valueName, value] : values) 1813*711ac7a9SEd Tanous { 1814*711ac7a9SEd Tanous if (valueName == "endpoints") 18158fb49dd6SShawn McCarney { 18168fb49dd6SShawn McCarney const std::vector<std::string>* endpoints = 18178fb49dd6SShawn McCarney std::get_if<std::vector<std::string>>( 1818*711ac7a9SEd Tanous &value); 1819*711ac7a9SEd Tanous if ((endpoints != nullptr) && 1820*711ac7a9SEd Tanous !endpoints->empty()) 18218fb49dd6SShawn McCarney { 1822adc4f0dbSShawn McCarney // Add inventory item to vector 1823adc4f0dbSShawn McCarney const std::string& invItemPath = 1824adc4f0dbSShawn McCarney endpoints->front(); 1825*711ac7a9SEd Tanous addInventoryItem(inventoryItems, 1826*711ac7a9SEd Tanous invItemPath, 1827adc4f0dbSShawn McCarney sensorName); 18288fb49dd6SShawn McCarney } 18298fb49dd6SShawn McCarney } 18308fb49dd6SShawn McCarney } 1831*711ac7a9SEd Tanous } 1832*711ac7a9SEd Tanous } 18338fb49dd6SShawn McCarney break; 18348fb49dd6SShawn McCarney } 18358fb49dd6SShawn McCarney } 18368fb49dd6SShawn McCarney } 18378fb49dd6SShawn McCarney 1838d500549bSAnthony Wilson // Now loop through the returned object paths again, this time to 1839d500549bSAnthony Wilson // find the leds associated with the inventory items we just found 1840d500549bSAnthony Wilson std::string inventoryAssocPath; 1841d500549bSAnthony Wilson inventoryAssocPath.reserve(128); // avoid memory allocations 1842d500549bSAnthony Wilson for (const auto& objDictEntry : resp) 1843d500549bSAnthony Wilson { 1844d500549bSAnthony Wilson const std::string& objPath = 1845d500549bSAnthony Wilson static_cast<const std::string&>(objDictEntry.first); 1846d500549bSAnthony Wilson 1847d500549bSAnthony Wilson for (InventoryItem& inventoryItem : *inventoryItems) 1848d500549bSAnthony Wilson { 1849d500549bSAnthony Wilson inventoryAssocPath = inventoryItem.objectPath; 1850d500549bSAnthony Wilson inventoryAssocPath += "/leds"; 1851d500549bSAnthony Wilson if (objPath == inventoryAssocPath) 1852d500549bSAnthony Wilson { 1853*711ac7a9SEd Tanous for (const auto& [interface, values] : objDictEntry.second) 1854d500549bSAnthony Wilson { 1855*711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Association") 1856*711ac7a9SEd Tanous { 1857*711ac7a9SEd Tanous for (const auto& [valueName, value] : values) 1858*711ac7a9SEd Tanous { 1859*711ac7a9SEd Tanous if (valueName == "endpoints") 1860d500549bSAnthony Wilson { 1861d500549bSAnthony Wilson const std::vector<std::string>* endpoints = 1862d500549bSAnthony Wilson std::get_if<std::vector<std::string>>( 1863*711ac7a9SEd Tanous &value); 1864*711ac7a9SEd Tanous if ((endpoints != nullptr) && 1865*711ac7a9SEd Tanous !endpoints->empty()) 1866d500549bSAnthony Wilson { 1867*711ac7a9SEd Tanous // Add inventory item to vector 1868d500549bSAnthony Wilson // Store LED path in inventory item 1869*711ac7a9SEd Tanous const std::string& ledPath = 1870*711ac7a9SEd Tanous endpoints->front(); 1871d500549bSAnthony Wilson inventoryItem.ledObjectPath = ledPath; 1872d500549bSAnthony Wilson } 1873d500549bSAnthony Wilson } 1874d500549bSAnthony Wilson } 1875*711ac7a9SEd Tanous } 1876*711ac7a9SEd Tanous } 1877*711ac7a9SEd Tanous 1878d500549bSAnthony Wilson break; 1879d500549bSAnthony Wilson } 1880d500549bSAnthony Wilson } 1881d500549bSAnthony Wilson } 1882adc4f0dbSShawn McCarney callback(inventoryItems); 1883adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit"; 18848fb49dd6SShawn McCarney }; 18858fb49dd6SShawn McCarney 18868fb49dd6SShawn McCarney // Find DBus object path that implements ObjectManager for ObjectMapper 18878fb49dd6SShawn McCarney std::string connection = "xyz.openbmc_project.ObjectMapper"; 18888fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(connection); 18898fb49dd6SShawn McCarney const std::string& objectMgrPath = 18908fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 18918fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is " 18928fb49dd6SShawn McCarney << objectMgrPath; 18938fb49dd6SShawn McCarney 18948fb49dd6SShawn McCarney // Call GetManagedObjects on the ObjectMapper to get all associations 18958fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 18968fb49dd6SShawn McCarney std::move(respHandler), connection, objectMgrPath, 18978fb49dd6SShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 18988fb49dd6SShawn McCarney 1899adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit"; 19008fb49dd6SShawn McCarney } 19018fb49dd6SShawn McCarney 19028fb49dd6SShawn McCarney /** 1903d500549bSAnthony Wilson * @brief Gets D-Bus data for inventory item leds associated with sensors. 1904d500549bSAnthony Wilson * 1905d500549bSAnthony Wilson * Uses the specified connections (services) to obtain D-Bus data for inventory 1906d500549bSAnthony Wilson * item leds associated with sensors. Stores the resulting data in the 1907d500549bSAnthony Wilson * inventoryItems vector. 1908d500549bSAnthony Wilson * 1909d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 1910d500549bSAnthony Wilson * response. 1911d500549bSAnthony Wilson * 1912d500549bSAnthony Wilson * Finds the inventory item led data asynchronously. Invokes callback when data 1913d500549bSAnthony Wilson * has been obtained. 1914d500549bSAnthony Wilson * 1915d500549bSAnthony Wilson * The callback must have the following signature: 1916d500549bSAnthony Wilson * @code 191742cbe538SGunnar Mills * callback() 1918d500549bSAnthony Wilson * @endcode 1919d500549bSAnthony Wilson * 1920d500549bSAnthony Wilson * This function is called recursively, obtaining data asynchronously from one 1921d500549bSAnthony Wilson * connection in each call. This ensures the callback is not invoked until the 1922d500549bSAnthony Wilson * last asynchronous function has completed. 1923d500549bSAnthony Wilson * 1924d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 1925d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1926d500549bSAnthony Wilson * @param ledConnections Connections that provide data for the inventory leds. 1927d500549bSAnthony Wilson * @param callback Callback to invoke when inventory data has been obtained. 1928d500549bSAnthony Wilson * @param ledConnectionsIndex Current index in ledConnections. Only specified 1929d500549bSAnthony Wilson * in recursive calls to this function. 1930d500549bSAnthony Wilson */ 1931d500549bSAnthony Wilson template <typename Callback> 1932d500549bSAnthony Wilson void getInventoryLedData( 1933d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1934d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1935d500549bSAnthony Wilson std::shared_ptr<boost::container::flat_map<std::string, std::string>> 1936d500549bSAnthony Wilson ledConnections, 1937d500549bSAnthony Wilson Callback&& callback, size_t ledConnectionsIndex = 0) 1938d500549bSAnthony Wilson { 1939d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData enter"; 1940d500549bSAnthony Wilson 1941d500549bSAnthony Wilson // If no more connections left, call callback 1942d500549bSAnthony Wilson if (ledConnectionsIndex >= ledConnections->size()) 1943d500549bSAnthony Wilson { 194442cbe538SGunnar Mills callback(); 1945d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData exit"; 1946d500549bSAnthony Wilson return; 1947d500549bSAnthony Wilson } 1948d500549bSAnthony Wilson 1949d500549bSAnthony Wilson // Get inventory item data from current connection 1950d500549bSAnthony Wilson auto it = ledConnections->nth(ledConnectionsIndex); 1951d500549bSAnthony Wilson if (it != ledConnections->end()) 1952d500549bSAnthony Wilson { 1953d500549bSAnthony Wilson const std::string& ledPath = (*it).first; 1954d500549bSAnthony Wilson const std::string& ledConnection = (*it).second; 1955d500549bSAnthony Wilson // Response handler for Get State property 19561e1e598dSJonathan Doman auto respHandler = 19571e1e598dSJonathan Doman [sensorsAsyncResp, inventoryItems, ledConnections, ledPath, 19581e1e598dSJonathan Doman callback{std::move(callback)}, ledConnectionsIndex]( 19591e1e598dSJonathan Doman const boost::system::error_code ec, const std::string& state) { 1960d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter"; 1961d500549bSAnthony Wilson if (ec) 1962d500549bSAnthony Wilson { 1963d500549bSAnthony Wilson BMCWEB_LOG_ERROR 1964d500549bSAnthony Wilson << "getInventoryLedData respHandler DBus error " << ec; 19658d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 1966d500549bSAnthony Wilson return; 1967d500549bSAnthony Wilson } 1968d500549bSAnthony Wilson 19691e1e598dSJonathan Doman BMCWEB_LOG_DEBUG << "Led state: " << state; 1970d500549bSAnthony Wilson // Find inventory item with this LED object path 1971d500549bSAnthony Wilson InventoryItem* inventoryItem = 1972d500549bSAnthony Wilson findInventoryItemForLed(*inventoryItems, ledPath); 1973d500549bSAnthony Wilson if (inventoryItem != nullptr) 1974d500549bSAnthony Wilson { 1975d500549bSAnthony Wilson // Store LED state in InventoryItem 19761e1e598dSJonathan Doman if (boost::ends_with(state, "On")) 1977d500549bSAnthony Wilson { 1978d500549bSAnthony Wilson inventoryItem->ledState = LedState::ON; 1979d500549bSAnthony Wilson } 19801e1e598dSJonathan Doman else if (boost::ends_with(state, "Blink")) 1981d500549bSAnthony Wilson { 1982d500549bSAnthony Wilson inventoryItem->ledState = LedState::BLINK; 1983d500549bSAnthony Wilson } 19841e1e598dSJonathan Doman else if (boost::ends_with(state, "Off")) 1985d500549bSAnthony Wilson { 1986d500549bSAnthony Wilson inventoryItem->ledState = LedState::OFF; 1987d500549bSAnthony Wilson } 1988d500549bSAnthony Wilson else 1989d500549bSAnthony Wilson { 1990d500549bSAnthony Wilson inventoryItem->ledState = LedState::UNKNOWN; 1991d500549bSAnthony Wilson } 1992d500549bSAnthony Wilson } 1993d500549bSAnthony Wilson 1994d500549bSAnthony Wilson // Recurse to get LED data from next connection 1995d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, 1996d500549bSAnthony Wilson ledConnections, std::move(callback), 1997d500549bSAnthony Wilson ledConnectionsIndex + 1); 1998d500549bSAnthony Wilson 1999d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit"; 2000d500549bSAnthony Wilson }; 2001d500549bSAnthony Wilson 2002d500549bSAnthony Wilson // Get the State property for the current LED 20031e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 20041e1e598dSJonathan Doman *crow::connections::systemBus, ledConnection, ledPath, 20051e1e598dSJonathan Doman "xyz.openbmc_project.Led.Physical", "State", 20061e1e598dSJonathan Doman std::move(respHandler)); 2007d500549bSAnthony Wilson } 2008d500549bSAnthony Wilson 2009d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData exit"; 2010d500549bSAnthony Wilson } 2011d500549bSAnthony Wilson 2012d500549bSAnthony Wilson /** 2013d500549bSAnthony Wilson * @brief Gets LED data for LEDs associated with given inventory items. 2014d500549bSAnthony Wilson * 2015d500549bSAnthony Wilson * Gets the D-Bus connections (services) that provide LED data for the LEDs 2016d500549bSAnthony Wilson * associated with the specified inventory items. Then gets the LED data from 2017d500549bSAnthony Wilson * each connection and stores it in the inventory item. 2018d500549bSAnthony Wilson * 2019d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 2020d500549bSAnthony Wilson * response. 2021d500549bSAnthony Wilson * 2022d500549bSAnthony Wilson * Finds the LED data asynchronously. Invokes callback when information has 2023d500549bSAnthony Wilson * been obtained. 2024d500549bSAnthony Wilson * 2025d500549bSAnthony Wilson * The callback must have the following signature: 2026d500549bSAnthony Wilson * @code 202742cbe538SGunnar Mills * callback() 2028d500549bSAnthony Wilson * @endcode 2029d500549bSAnthony Wilson * 2030d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 2031d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 2032d500549bSAnthony Wilson * @param callback Callback to invoke when inventory items have been obtained. 2033d500549bSAnthony Wilson */ 2034d500549bSAnthony Wilson template <typename Callback> 2035d500549bSAnthony Wilson void getInventoryLeds( 2036d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 2037d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 2038d500549bSAnthony Wilson Callback&& callback) 2039d500549bSAnthony Wilson { 2040d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds enter"; 2041d500549bSAnthony Wilson 2042d500549bSAnthony Wilson const std::string path = "/xyz/openbmc_project"; 2043d500549bSAnthony Wilson const std::array<std::string, 1> interfaces = { 2044d500549bSAnthony Wilson "xyz.openbmc_project.Led.Physical"}; 2045d500549bSAnthony Wilson 2046d500549bSAnthony Wilson // Response handler for parsing output from GetSubTree 2047d500549bSAnthony Wilson auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp, 2048d500549bSAnthony Wilson inventoryItems](const boost::system::error_code ec, 2049d500549bSAnthony Wilson const GetSubTreeType& subtree) { 2050d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter"; 2051d500549bSAnthony Wilson if (ec) 2052d500549bSAnthony Wilson { 20538d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 2054d500549bSAnthony Wilson BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error " 2055d500549bSAnthony Wilson << ec; 2056d500549bSAnthony Wilson return; 2057d500549bSAnthony Wilson } 2058d500549bSAnthony Wilson 2059d500549bSAnthony Wilson // Build map of LED object paths to connections 2060d500549bSAnthony Wilson std::shared_ptr<boost::container::flat_map<std::string, std::string>> 2061d500549bSAnthony Wilson ledConnections = std::make_shared< 2062d500549bSAnthony Wilson boost::container::flat_map<std::string, std::string>>(); 2063d500549bSAnthony Wilson 2064d500549bSAnthony Wilson // Loop through objects from GetSubTree 2065d500549bSAnthony Wilson for (const std::pair< 2066d500549bSAnthony Wilson std::string, 2067d500549bSAnthony Wilson std::vector<std::pair<std::string, std::vector<std::string>>>>& 2068d500549bSAnthony Wilson object : subtree) 2069d500549bSAnthony Wilson { 2070d500549bSAnthony Wilson // Check if object path is LED for one of the specified inventory 2071d500549bSAnthony Wilson // items 2072d500549bSAnthony Wilson const std::string& ledPath = object.first; 2073d500549bSAnthony Wilson if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr) 2074d500549bSAnthony Wilson { 2075d500549bSAnthony Wilson // Add mapping from ledPath to connection 2076d500549bSAnthony Wilson const std::string& connection = object.second.begin()->first; 2077d500549bSAnthony Wilson (*ledConnections)[ledPath] = connection; 2078d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> " 2079d500549bSAnthony Wilson << connection; 2080d500549bSAnthony Wilson } 2081d500549bSAnthony Wilson } 2082d500549bSAnthony Wilson 2083d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections, 2084d500549bSAnthony Wilson std::move(callback)); 2085d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit"; 2086d500549bSAnthony Wilson }; 2087d500549bSAnthony Wilson // Make call to ObjectMapper to find all inventory items 2088d500549bSAnthony Wilson crow::connections::systemBus->async_method_call( 2089d500549bSAnthony Wilson std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 2090d500549bSAnthony Wilson "/xyz/openbmc_project/object_mapper", 2091d500549bSAnthony Wilson "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); 2092d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds exit"; 2093d500549bSAnthony Wilson } 2094d500549bSAnthony Wilson 2095d500549bSAnthony Wilson /** 209642cbe538SGunnar Mills * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent 209742cbe538SGunnar Mills * 209842cbe538SGunnar Mills * Uses the specified connections (services) (currently assumes just one) to 209942cbe538SGunnar Mills * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in 210042cbe538SGunnar Mills * the inventoryItems vector. Only stores data in Power Supply inventoryItems. 210142cbe538SGunnar Mills * 210242cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 210342cbe538SGunnar Mills * response. 210442cbe538SGunnar Mills * 210542cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 210642cbe538SGunnar Mills * when data has been obtained. 210742cbe538SGunnar Mills * 210842cbe538SGunnar Mills * The callback must have the following signature: 210942cbe538SGunnar Mills * @code 211042cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 211142cbe538SGunnar Mills * @endcode 211242cbe538SGunnar Mills * 211342cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 211442cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 211542cbe538SGunnar Mills * @param psAttributesConnections Connections that provide data for the Power 211642cbe538SGunnar Mills * Supply Attributes 211742cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 211842cbe538SGunnar Mills */ 211942cbe538SGunnar Mills template <typename Callback> 212042cbe538SGunnar Mills void getPowerSupplyAttributesData( 2121b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 212242cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 212342cbe538SGunnar Mills const boost::container::flat_map<std::string, std::string>& 212442cbe538SGunnar Mills psAttributesConnections, 212542cbe538SGunnar Mills Callback&& callback) 212642cbe538SGunnar Mills { 212742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter"; 212842cbe538SGunnar Mills 212942cbe538SGunnar Mills if (psAttributesConnections.empty()) 213042cbe538SGunnar Mills { 213142cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!"; 213242cbe538SGunnar Mills callback(inventoryItems); 213342cbe538SGunnar Mills return; 213442cbe538SGunnar Mills } 213542cbe538SGunnar Mills 213642cbe538SGunnar Mills // Assuming just one connection (service) for now 213742cbe538SGunnar Mills auto it = psAttributesConnections.nth(0); 213842cbe538SGunnar Mills 213942cbe538SGunnar Mills const std::string& psAttributesPath = (*it).first; 214042cbe538SGunnar Mills const std::string& psAttributesConnection = (*it).second; 214142cbe538SGunnar Mills 214242cbe538SGunnar Mills // Response handler for Get DeratingFactor property 214342cbe538SGunnar Mills auto respHandler = [sensorsAsyncResp, inventoryItems, 214442cbe538SGunnar Mills callback{std::move(callback)}]( 214542cbe538SGunnar Mills const boost::system::error_code ec, 21461e1e598dSJonathan Doman const uint32_t value) { 214742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter"; 214842cbe538SGunnar Mills if (ec) 214942cbe538SGunnar Mills { 215042cbe538SGunnar Mills BMCWEB_LOG_ERROR 215142cbe538SGunnar Mills << "getPowerSupplyAttributesData respHandler DBus error " << ec; 21528d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 215342cbe538SGunnar Mills return; 215442cbe538SGunnar Mills } 215542cbe538SGunnar Mills 21561e1e598dSJonathan Doman BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value; 215742cbe538SGunnar Mills // Store value in Power Supply Inventory Items 215842cbe538SGunnar Mills for (InventoryItem& inventoryItem : *inventoryItems) 215942cbe538SGunnar Mills { 216042cbe538SGunnar Mills if (inventoryItem.isPowerSupply == true) 216142cbe538SGunnar Mills { 216242cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent = 21631e1e598dSJonathan Doman static_cast<int>(value); 216442cbe538SGunnar Mills } 216542cbe538SGunnar Mills } 216642cbe538SGunnar Mills 216742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit"; 216842cbe538SGunnar Mills callback(inventoryItems); 216942cbe538SGunnar Mills }; 217042cbe538SGunnar Mills 217142cbe538SGunnar Mills // Get the DeratingFactor property for the PowerSupplyAttributes 217242cbe538SGunnar Mills // Currently only property on the interface/only one we care about 21731e1e598dSJonathan Doman sdbusplus::asio::getProperty<uint32_t>( 21741e1e598dSJonathan Doman *crow::connections::systemBus, psAttributesConnection, psAttributesPath, 21751e1e598dSJonathan Doman "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor", 21761e1e598dSJonathan Doman std::move(respHandler)); 217742cbe538SGunnar Mills 217842cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit"; 217942cbe538SGunnar Mills } 218042cbe538SGunnar Mills 218142cbe538SGunnar Mills /** 218242cbe538SGunnar Mills * @brief Gets the Power Supply Attributes such as EfficiencyPercent 218342cbe538SGunnar Mills * 218442cbe538SGunnar Mills * Gets the D-Bus connection (service) that provides Power Supply Attributes 218542cbe538SGunnar Mills * data. Then gets the Power Supply Attributes data from the connection 218642cbe538SGunnar Mills * (currently just assumes 1 connection) and stores the data in the inventory 218742cbe538SGunnar Mills * item. 218842cbe538SGunnar Mills * 218942cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 219042cbe538SGunnar Mills * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish. 219142cbe538SGunnar Mills * 219242cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 219342cbe538SGunnar Mills * when information has been obtained. 219442cbe538SGunnar Mills * 219542cbe538SGunnar Mills * The callback must have the following signature: 219642cbe538SGunnar Mills * @code 219742cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 219842cbe538SGunnar Mills * @endcode 219942cbe538SGunnar Mills * 220042cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 220142cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 220242cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 220342cbe538SGunnar Mills */ 220442cbe538SGunnar Mills template <typename Callback> 220542cbe538SGunnar Mills void getPowerSupplyAttributes( 220642cbe538SGunnar Mills std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 220742cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 220842cbe538SGunnar Mills Callback&& callback) 220942cbe538SGunnar Mills { 221042cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter"; 221142cbe538SGunnar Mills 221242cbe538SGunnar Mills // Only need the power supply attributes when the Power Schema 2213a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode != sensors::node::power) 221442cbe538SGunnar Mills { 221542cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power"; 221642cbe538SGunnar Mills callback(inventoryItems); 221742cbe538SGunnar Mills return; 221842cbe538SGunnar Mills } 221942cbe538SGunnar Mills 222042cbe538SGunnar Mills const std::array<std::string, 1> interfaces = { 222142cbe538SGunnar Mills "xyz.openbmc_project.Control.PowerSupplyAttributes"}; 222242cbe538SGunnar Mills 222342cbe538SGunnar Mills // Response handler for parsing output from GetSubTree 222442cbe538SGunnar Mills auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp, 222542cbe538SGunnar Mills inventoryItems](const boost::system::error_code ec, 222642cbe538SGunnar Mills const GetSubTreeType& subtree) { 222742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter"; 222842cbe538SGunnar Mills if (ec) 222942cbe538SGunnar Mills { 22308d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 223142cbe538SGunnar Mills BMCWEB_LOG_ERROR 223242cbe538SGunnar Mills << "getPowerSupplyAttributes respHandler DBus error " << ec; 223342cbe538SGunnar Mills return; 223442cbe538SGunnar Mills } 223542cbe538SGunnar Mills if (subtree.size() == 0) 223642cbe538SGunnar Mills { 223742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!"; 223842cbe538SGunnar Mills callback(inventoryItems); 223942cbe538SGunnar Mills return; 224042cbe538SGunnar Mills } 224142cbe538SGunnar Mills 224242cbe538SGunnar Mills // Currently we only support 1 power supply attribute, use this for 224342cbe538SGunnar Mills // all the power supplies. Build map of object path to connection. 224442cbe538SGunnar Mills // Assume just 1 connection and 1 path for now. 224542cbe538SGunnar Mills boost::container::flat_map<std::string, std::string> 224642cbe538SGunnar Mills psAttributesConnections; 224742cbe538SGunnar Mills 224842cbe538SGunnar Mills if (subtree[0].first.empty() || subtree[0].second.empty()) 224942cbe538SGunnar Mills { 225042cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!"; 225142cbe538SGunnar Mills callback(inventoryItems); 225242cbe538SGunnar Mills return; 225342cbe538SGunnar Mills } 225442cbe538SGunnar Mills 225542cbe538SGunnar Mills const std::string& psAttributesPath = subtree[0].first; 225642cbe538SGunnar Mills const std::string& connection = subtree[0].second.begin()->first; 225742cbe538SGunnar Mills 225842cbe538SGunnar Mills if (connection.empty()) 225942cbe538SGunnar Mills { 226042cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!"; 226142cbe538SGunnar Mills callback(inventoryItems); 226242cbe538SGunnar Mills return; 226342cbe538SGunnar Mills } 226442cbe538SGunnar Mills 226542cbe538SGunnar Mills psAttributesConnections[psAttributesPath] = connection; 226642cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> " 226742cbe538SGunnar Mills << connection; 226842cbe538SGunnar Mills 226942cbe538SGunnar Mills getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems, 227042cbe538SGunnar Mills psAttributesConnections, 227142cbe538SGunnar Mills std::move(callback)); 227242cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit"; 227342cbe538SGunnar Mills }; 227442cbe538SGunnar Mills // Make call to ObjectMapper to find the PowerSupplyAttributes service 227542cbe538SGunnar Mills crow::connections::systemBus->async_method_call( 227642cbe538SGunnar Mills std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 227742cbe538SGunnar Mills "/xyz/openbmc_project/object_mapper", 227842cbe538SGunnar Mills "xyz.openbmc_project.ObjectMapper", "GetSubTree", 227942cbe538SGunnar Mills "/xyz/openbmc_project", 0, interfaces); 228042cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit"; 228142cbe538SGunnar Mills } 228242cbe538SGunnar Mills 228342cbe538SGunnar Mills /** 2284adc4f0dbSShawn McCarney * @brief Gets inventory items associated with sensors. 22858fb49dd6SShawn McCarney * 22868fb49dd6SShawn McCarney * Finds the inventory items that are associated with the specified sensors. 2287adc4f0dbSShawn McCarney * Then gets D-Bus data for the inventory items, such as presence and VPD. 22888fb49dd6SShawn McCarney * 2289adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 2290adc4f0dbSShawn McCarney * response. 22918fb49dd6SShawn McCarney * 2292adc4f0dbSShawn McCarney * Finds the inventory items asynchronously. Invokes callback when the 2293adc4f0dbSShawn McCarney * inventory items have been obtained. 2294adc4f0dbSShawn McCarney * 2295adc4f0dbSShawn McCarney * The callback must have the following signature: 2296adc4f0dbSShawn McCarney * @code 2297adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 2298adc4f0dbSShawn McCarney * @endcode 22998fb49dd6SShawn McCarney * 23008fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 23018fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 23028fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 23038fb49dd6SShawn McCarney * implements ObjectManager. 2304adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 23058fb49dd6SShawn McCarney */ 2306adc4f0dbSShawn McCarney template <typename Callback> 2307adc4f0dbSShawn McCarney static void getInventoryItems( 23088fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 23098fb49dd6SShawn McCarney const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames, 23108fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 2311adc4f0dbSShawn McCarney objectMgrPaths, 2312adc4f0dbSShawn McCarney Callback&& callback) 23138fb49dd6SShawn McCarney { 2314adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItems enter"; 2315adc4f0dbSShawn McCarney auto getInventoryItemAssociationsCb = 2316adc4f0dbSShawn McCarney [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}]( 2317adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems) { 2318adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter"; 23198fb49dd6SShawn McCarney auto getInventoryItemsConnectionsCb = 2320adc4f0dbSShawn McCarney [sensorsAsyncResp, inventoryItems, objectMgrPaths, 2321adc4f0dbSShawn McCarney callback{std::move(callback)}]( 23228fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_set<std::string>> 23238fb49dd6SShawn McCarney invConnections) { 23248fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter"; 2325d500549bSAnthony Wilson auto getInventoryItemsDataCb = 2326d500549bSAnthony Wilson [sensorsAsyncResp, inventoryItems, 2327d500549bSAnthony Wilson callback{std::move(callback)}]() { 2328d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter"; 232942cbe538SGunnar Mills 233042cbe538SGunnar Mills auto getInventoryLedsCb = [sensorsAsyncResp, 233142cbe538SGunnar Mills inventoryItems, 233242cbe538SGunnar Mills callback{std::move( 233342cbe538SGunnar Mills callback)}]() { 233442cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter"; 233542cbe538SGunnar Mills // Find Power Supply Attributes and get the data 233642cbe538SGunnar Mills getPowerSupplyAttributes(sensorsAsyncResp, 233742cbe538SGunnar Mills inventoryItems, 233842cbe538SGunnar Mills std::move(callback)); 233942cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit"; 234042cbe538SGunnar Mills }; 234142cbe538SGunnar Mills 2342d500549bSAnthony Wilson // Find led connections and get the data 2343d500549bSAnthony Wilson getInventoryLeds(sensorsAsyncResp, inventoryItems, 234442cbe538SGunnar Mills std::move(getInventoryLedsCb)); 2345d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit"; 2346d500549bSAnthony Wilson }; 23478fb49dd6SShawn McCarney 2348adc4f0dbSShawn McCarney // Get inventory item data from connections 2349adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 2350adc4f0dbSShawn McCarney invConnections, objectMgrPaths, 2351d500549bSAnthony Wilson std::move(getInventoryItemsDataCb)); 23528fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit"; 23538fb49dd6SShawn McCarney }; 23548fb49dd6SShawn McCarney 2355adc4f0dbSShawn McCarney // Get connections that provide inventory item data 23568fb49dd6SShawn McCarney getInventoryItemsConnections( 2357adc4f0dbSShawn McCarney sensorsAsyncResp, inventoryItems, 23588fb49dd6SShawn McCarney std::move(getInventoryItemsConnectionsCb)); 2359adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit"; 23608fb49dd6SShawn McCarney }; 23618fb49dd6SShawn McCarney 2362adc4f0dbSShawn McCarney // Get associations from sensors to inventory items 2363adc4f0dbSShawn McCarney getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths, 2364adc4f0dbSShawn McCarney std::move(getInventoryItemAssociationsCb)); 2365adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItems exit"; 2366adc4f0dbSShawn McCarney } 2367adc4f0dbSShawn McCarney 2368adc4f0dbSShawn McCarney /** 2369adc4f0dbSShawn McCarney * @brief Returns JSON PowerSupply object for the specified inventory item. 2370adc4f0dbSShawn McCarney * 2371adc4f0dbSShawn McCarney * Searches for a JSON PowerSupply object that matches the specified inventory 2372adc4f0dbSShawn McCarney * item. If one is not found, a new PowerSupply object is added to the JSON 2373adc4f0dbSShawn McCarney * array. 2374adc4f0dbSShawn McCarney * 2375adc4f0dbSShawn McCarney * Multiple sensors are often associated with one power supply inventory item. 2376adc4f0dbSShawn McCarney * As a result, multiple sensor values are stored in one JSON PowerSupply 2377adc4f0dbSShawn McCarney * object. 2378adc4f0dbSShawn McCarney * 2379adc4f0dbSShawn McCarney * @param powerSupplyArray JSON array containing Redfish PowerSupply objects. 2380adc4f0dbSShawn McCarney * @param inventoryItem Inventory item for the power supply. 2381adc4f0dbSShawn McCarney * @param chassisId Chassis that contains the power supply. 2382adc4f0dbSShawn McCarney * @return JSON PowerSupply object for the specified inventory item. 2383adc4f0dbSShawn McCarney */ 238423a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray, 2385adc4f0dbSShawn McCarney const InventoryItem& inventoryItem, 2386adc4f0dbSShawn McCarney const std::string& chassisId) 2387adc4f0dbSShawn McCarney { 2388adc4f0dbSShawn McCarney // Check if matching PowerSupply object already exists in JSON array 2389adc4f0dbSShawn McCarney for (nlohmann::json& powerSupply : powerSupplyArray) 2390adc4f0dbSShawn McCarney { 2391adc4f0dbSShawn McCarney if (powerSupply["MemberId"] == inventoryItem.name) 2392adc4f0dbSShawn McCarney { 2393adc4f0dbSShawn McCarney return powerSupply; 2394adc4f0dbSShawn McCarney } 2395adc4f0dbSShawn McCarney } 2396adc4f0dbSShawn McCarney 2397adc4f0dbSShawn McCarney // Add new PowerSupply object to JSON array 2398adc4f0dbSShawn McCarney powerSupplyArray.push_back({}); 2399adc4f0dbSShawn McCarney nlohmann::json& powerSupply = powerSupplyArray.back(); 2400adc4f0dbSShawn McCarney powerSupply["@odata.id"] = 2401adc4f0dbSShawn McCarney "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/"; 2402adc4f0dbSShawn McCarney powerSupply["MemberId"] = inventoryItem.name; 2403adc4f0dbSShawn McCarney powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " "); 2404adc4f0dbSShawn McCarney powerSupply["Manufacturer"] = inventoryItem.manufacturer; 2405adc4f0dbSShawn McCarney powerSupply["Model"] = inventoryItem.model; 2406adc4f0dbSShawn McCarney powerSupply["PartNumber"] = inventoryItem.partNumber; 2407adc4f0dbSShawn McCarney powerSupply["SerialNumber"] = inventoryItem.serialNumber; 2408d500549bSAnthony Wilson setLedState(powerSupply, &inventoryItem); 2409adc4f0dbSShawn McCarney 241042cbe538SGunnar Mills if (inventoryItem.powerSupplyEfficiencyPercent >= 0) 241142cbe538SGunnar Mills { 241242cbe538SGunnar Mills powerSupply["EfficiencyPercent"] = 241342cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent; 241442cbe538SGunnar Mills } 241542cbe538SGunnar Mills 241642cbe538SGunnar Mills powerSupply["Status"]["State"] = getState(&inventoryItem); 2417adc4f0dbSShawn McCarney const char* health = inventoryItem.isFunctional ? "OK" : "Critical"; 2418adc4f0dbSShawn McCarney powerSupply["Status"]["Health"] = health; 2419adc4f0dbSShawn McCarney 2420adc4f0dbSShawn McCarney return powerSupply; 24218fb49dd6SShawn McCarney } 24228fb49dd6SShawn McCarney 24238fb49dd6SShawn McCarney /** 2424de629b6eSShawn McCarney * @brief Gets the values of the specified sensors. 2425de629b6eSShawn McCarney * 2426de629b6eSShawn McCarney * Stores the results as JSON in the SensorsAsyncResp. 2427de629b6eSShawn McCarney * 2428de629b6eSShawn McCarney * Gets the sensor values asynchronously. Stores the results later when the 2429de629b6eSShawn McCarney * information has been obtained. 2430de629b6eSShawn McCarney * 2431adc4f0dbSShawn McCarney * The sensorNames set contains all requested sensors for the current chassis. 2432de629b6eSShawn McCarney * 2433de629b6eSShawn McCarney * To minimize the number of DBus calls, the DBus method 2434de629b6eSShawn McCarney * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the 2435de629b6eSShawn McCarney * values of all sensors provided by a connection (service). 2436de629b6eSShawn McCarney * 2437de629b6eSShawn McCarney * The connections set contains all the connections that provide sensor values. 2438de629b6eSShawn McCarney * 2439de629b6eSShawn McCarney * The objectMgrPaths map contains mappings from a connection name to the 2440de629b6eSShawn McCarney * corresponding DBus object path that implements ObjectManager. 2441de629b6eSShawn McCarney * 2442adc4f0dbSShawn McCarney * The InventoryItem vector contains D-Bus inventory items associated with the 2443adc4f0dbSShawn McCarney * sensors. Inventory item data is needed for some Redfish sensor properties. 2444adc4f0dbSShawn McCarney * 2445de629b6eSShawn McCarney * @param SensorsAsyncResp Pointer to object holding response data. 2446adc4f0dbSShawn McCarney * @param sensorNames All requested sensors within the current chassis. 2447de629b6eSShawn McCarney * @param connections Connections that provide sensor values. 2448de629b6eSShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 2449de629b6eSShawn McCarney * implements ObjectManager. 2450adc4f0dbSShawn McCarney * @param inventoryItems Inventory items associated with the sensors. 2451de629b6eSShawn McCarney */ 245223a21a1cSEd Tanous inline void getSensorData( 245381ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 2454b5a76932SEd Tanous const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames, 2455de629b6eSShawn McCarney const boost::container::flat_set<std::string>& connections, 2456b5a76932SEd Tanous const std::shared_ptr<boost::container::flat_map<std::string, std::string>>& 2457adc4f0dbSShawn McCarney objectMgrPaths, 2458b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems) 2459de629b6eSShawn McCarney { 2460de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getSensorData enter"; 2461de629b6eSShawn McCarney // Get managed objects from all services exposing sensors 2462de629b6eSShawn McCarney for (const std::string& connection : connections) 2463de629b6eSShawn McCarney { 2464de629b6eSShawn McCarney // Response handler to process managed objects 246581ce609eSEd Tanous auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames, 2466adc4f0dbSShawn McCarney inventoryItems]( 2467de629b6eSShawn McCarney const boost::system::error_code ec, 2468*711ac7a9SEd Tanous dbus::utility::ManagedObjectType& resp) { 2469de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter"; 2470de629b6eSShawn McCarney if (ec) 2471de629b6eSShawn McCarney { 2472de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec; 24738d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 2474de629b6eSShawn McCarney return; 2475de629b6eSShawn McCarney } 2476de629b6eSShawn McCarney // Go through all objects and update response with sensor data 2477de629b6eSShawn McCarney for (const auto& objDictEntry : resp) 2478de629b6eSShawn McCarney { 2479de629b6eSShawn McCarney const std::string& objPath = 2480de629b6eSShawn McCarney static_cast<const std::string&>(objDictEntry.first); 2481de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object " 2482de629b6eSShawn McCarney << objPath; 2483de629b6eSShawn McCarney 2484de629b6eSShawn McCarney std::vector<std::string> split; 2485de629b6eSShawn McCarney // Reserve space for 2486de629b6eSShawn McCarney // /xyz/openbmc_project/sensors/<name>/<subname> 2487de629b6eSShawn McCarney split.reserve(6); 2488de629b6eSShawn McCarney boost::algorithm::split(split, objPath, boost::is_any_of("/")); 2489de629b6eSShawn McCarney if (split.size() < 6) 2490de629b6eSShawn McCarney { 2491de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "Got path that isn't long enough " 2492de629b6eSShawn McCarney << objPath; 2493de629b6eSShawn McCarney continue; 2494de629b6eSShawn McCarney } 2495de629b6eSShawn McCarney // These indexes aren't intuitive, as boost::split puts an empty 2496de629b6eSShawn McCarney // string at the beginning 2497de629b6eSShawn McCarney const std::string& sensorType = split[4]; 2498de629b6eSShawn McCarney const std::string& sensorName = split[5]; 2499de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "sensorName " << sensorName 2500de629b6eSShawn McCarney << " sensorType " << sensorType; 250149c53ac9SJohnathan Mantey if (sensorNames->find(objPath) == sensorNames->end()) 2502de629b6eSShawn McCarney { 2503accdbb2cSAndrew Geissler BMCWEB_LOG_DEBUG << sensorName << " not in sensor list "; 2504de629b6eSShawn McCarney continue; 2505de629b6eSShawn McCarney } 2506de629b6eSShawn McCarney 2507adc4f0dbSShawn McCarney // Find inventory item (if any) associated with sensor 2508adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 2509adc4f0dbSShawn McCarney findInventoryItemForSensor(inventoryItems, objPath); 2510adc4f0dbSShawn McCarney 251195a3ecadSAnthony Wilson const std::string& sensorSchema = 251281ce609eSEd Tanous sensorsAsyncResp->chassisSubNode; 251395a3ecadSAnthony Wilson 251495a3ecadSAnthony Wilson nlohmann::json* sensorJson = nullptr; 251595a3ecadSAnthony Wilson 2516a0ec28b6SAdrian Ambrożewicz if (sensorSchema == sensors::node::sensors) 251795a3ecadSAnthony Wilson { 25188d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] = 251981ce609eSEd Tanous "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + 252081ce609eSEd Tanous "/" + sensorsAsyncResp->chassisSubNode + "/" + 252195a3ecadSAnthony Wilson sensorName; 25228d1b46d7Szhanghch05 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue); 252395a3ecadSAnthony Wilson } 252495a3ecadSAnthony Wilson else 252595a3ecadSAnthony Wilson { 2526271584abSEd Tanous std::string fieldName; 2527de629b6eSShawn McCarney if (sensorType == "temperature") 2528de629b6eSShawn McCarney { 2529de629b6eSShawn McCarney fieldName = "Temperatures"; 2530de629b6eSShawn McCarney } 2531de629b6eSShawn McCarney else if (sensorType == "fan" || sensorType == "fan_tach" || 2532de629b6eSShawn McCarney sensorType == "fan_pwm") 2533de629b6eSShawn McCarney { 2534de629b6eSShawn McCarney fieldName = "Fans"; 2535de629b6eSShawn McCarney } 2536de629b6eSShawn McCarney else if (sensorType == "voltage") 2537de629b6eSShawn McCarney { 2538de629b6eSShawn McCarney fieldName = "Voltages"; 2539de629b6eSShawn McCarney } 2540de629b6eSShawn McCarney else if (sensorType == "power") 2541de629b6eSShawn McCarney { 2542028f7ebcSEddie James if (!sensorName.compare("total_power")) 2543028f7ebcSEddie James { 2544028f7ebcSEddie James fieldName = "PowerControl"; 2545028f7ebcSEddie James } 2546adc4f0dbSShawn McCarney else if ((inventoryItem != nullptr) && 2547adc4f0dbSShawn McCarney (inventoryItem->isPowerSupply)) 2548028f7ebcSEddie James { 2549de629b6eSShawn McCarney fieldName = "PowerSupplies"; 2550de629b6eSShawn McCarney } 2551adc4f0dbSShawn McCarney else 2552adc4f0dbSShawn McCarney { 2553adc4f0dbSShawn McCarney // Other power sensors are in SensorCollection 2554adc4f0dbSShawn McCarney continue; 2555adc4f0dbSShawn McCarney } 2556028f7ebcSEddie James } 2557de629b6eSShawn McCarney else 2558de629b6eSShawn McCarney { 2559de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "Unsure how to handle sensorType " 2560de629b6eSShawn McCarney << sensorType; 2561de629b6eSShawn McCarney continue; 2562de629b6eSShawn McCarney } 2563de629b6eSShawn McCarney 2564de629b6eSShawn McCarney nlohmann::json& tempArray = 25658d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName]; 2566adc4f0dbSShawn McCarney if (fieldName == "PowerControl") 256749c53ac9SJohnathan Mantey { 2568adc4f0dbSShawn McCarney if (tempArray.empty()) 25697ab06f49SGunnar Mills { 257095a3ecadSAnthony Wilson // Put multiple "sensors" into a single 257195a3ecadSAnthony Wilson // PowerControl. Follows MemberId naming and 257295a3ecadSAnthony Wilson // naming in power.hpp. 25737ab06f49SGunnar Mills tempArray.push_back( 2574adc4f0dbSShawn McCarney {{"@odata.id", 2575adc4f0dbSShawn McCarney "/redfish/v1/Chassis/" + 257681ce609eSEd Tanous sensorsAsyncResp->chassisId + "/" + 257781ce609eSEd Tanous sensorsAsyncResp->chassisSubNode + "#/" + 2578adc4f0dbSShawn McCarney fieldName + "/0"}}); 2579adc4f0dbSShawn McCarney } 2580adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 2581adc4f0dbSShawn McCarney } 2582adc4f0dbSShawn McCarney else if (fieldName == "PowerSupplies") 2583adc4f0dbSShawn McCarney { 2584adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 2585adc4f0dbSShawn McCarney { 2586adc4f0dbSShawn McCarney sensorJson = 2587adc4f0dbSShawn McCarney &(getPowerSupply(tempArray, *inventoryItem, 258881ce609eSEd Tanous sensorsAsyncResp->chassisId)); 2589adc4f0dbSShawn McCarney } 259049c53ac9SJohnathan Mantey } 259149c53ac9SJohnathan Mantey else 259249c53ac9SJohnathan Mantey { 2593de629b6eSShawn McCarney tempArray.push_back( 259495a3ecadSAnthony Wilson {{"@odata.id", 259595a3ecadSAnthony Wilson "/redfish/v1/Chassis/" + 259681ce609eSEd Tanous sensorsAsyncResp->chassisId + "/" + 259781ce609eSEd Tanous sensorsAsyncResp->chassisSubNode + "#/" + 259895a3ecadSAnthony Wilson fieldName + "/"}}); 2599adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 260049c53ac9SJohnathan Mantey } 260195a3ecadSAnthony Wilson } 2602de629b6eSShawn McCarney 2603adc4f0dbSShawn McCarney if (sensorJson != nullptr) 2604adc4f0dbSShawn McCarney { 2605a0ec28b6SAdrian Ambrożewicz objectInterfacesToJson( 260681ce609eSEd Tanous sensorName, sensorType, sensorsAsyncResp, 2607a0ec28b6SAdrian Ambrożewicz objDictEntry.second, *sensorJson, inventoryItem); 2608adc4f0dbSShawn McCarney } 2609de629b6eSShawn McCarney } 261081ce609eSEd Tanous if (sensorsAsyncResp.use_count() == 1) 261149c53ac9SJohnathan Mantey { 261281ce609eSEd Tanous sortJSONResponse(sensorsAsyncResp); 261381ce609eSEd Tanous if (sensorsAsyncResp->chassisSubNode == sensors::node::thermal) 26148bd25ccdSJames Feist { 261581ce609eSEd Tanous populateFanRedundancy(sensorsAsyncResp); 26168bd25ccdSJames Feist } 261749c53ac9SJohnathan Mantey } 2618de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit"; 2619de629b6eSShawn McCarney }; 2620de629b6eSShawn McCarney 2621de629b6eSShawn McCarney // Find DBus object path that implements ObjectManager for the current 2622de629b6eSShawn McCarney // connection. If no mapping found, default to "/". 26238fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(connection); 2624de629b6eSShawn McCarney const std::string& objectMgrPath = 26258fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 2626de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is " 2627de629b6eSShawn McCarney << objectMgrPath; 2628de629b6eSShawn McCarney 2629de629b6eSShawn McCarney crow::connections::systemBus->async_method_call( 2630de629b6eSShawn McCarney getManagedObjectsCb, connection, objectMgrPath, 2631de629b6eSShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 263223a21a1cSEd Tanous } 2633de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getSensorData exit"; 2634de629b6eSShawn McCarney } 2635de629b6eSShawn McCarney 263623a21a1cSEd Tanous inline void processSensorList( 263781ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 2638b5a76932SEd Tanous const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames) 26391abe55efSEd Tanous { 264095a3ecadSAnthony Wilson auto getConnectionCb = 264181ce609eSEd Tanous [sensorsAsyncResp, sensorNames]( 264295a3ecadSAnthony Wilson const boost::container::flat_set<std::string>& connections) { 264355c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getConnectionCb enter"; 2644de629b6eSShawn McCarney auto getObjectManagerPathsCb = 264581ce609eSEd Tanous [sensorsAsyncResp, sensorNames, 2646b5a76932SEd Tanous connections](const std::shared_ptr<boost::container::flat_map< 2647b5a76932SEd Tanous std::string, std::string>>& objectMgrPaths) { 2648de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter"; 2649adc4f0dbSShawn McCarney auto getInventoryItemsCb = 265081ce609eSEd Tanous [sensorsAsyncResp, sensorNames, connections, 2651adc4f0dbSShawn McCarney objectMgrPaths]( 2652f23b7296SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& 2653adc4f0dbSShawn McCarney inventoryItems) { 2654adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter"; 265549c53ac9SJohnathan Mantey // Get sensor data and store results in JSON 265681ce609eSEd Tanous getSensorData(sensorsAsyncResp, sensorNames, 2657adc4f0dbSShawn McCarney connections, objectMgrPaths, 2658f23b7296SEd Tanous inventoryItems); 2659adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit"; 2660adc4f0dbSShawn McCarney }; 2661adc4f0dbSShawn McCarney 2662adc4f0dbSShawn McCarney // Get inventory items associated with sensors 266381ce609eSEd Tanous getInventoryItems(sensorsAsyncResp, sensorNames, 2664adc4f0dbSShawn McCarney objectMgrPaths, 2665adc4f0dbSShawn McCarney std::move(getInventoryItemsCb)); 2666adc4f0dbSShawn McCarney 2667de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit"; 266808777fb0SLewanczyk, Dawid }; 2669de629b6eSShawn McCarney 267049c53ac9SJohnathan Mantey // Get mapping from connection names to the DBus object 267149c53ac9SJohnathan Mantey // paths that implement the ObjectManager interface 267281ce609eSEd Tanous getObjectManagerPaths(sensorsAsyncResp, 2673de629b6eSShawn McCarney std::move(getObjectManagerPathsCb)); 267455c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getConnectionCb exit"; 267508777fb0SLewanczyk, Dawid }; 2676de629b6eSShawn McCarney 2677de629b6eSShawn McCarney // Get set of connections that provide sensor values 267881ce609eSEd Tanous getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb)); 267995a3ecadSAnthony Wilson } 268095a3ecadSAnthony Wilson 268195a3ecadSAnthony Wilson /** 268295a3ecadSAnthony Wilson * @brief Entry point for retrieving sensors data related to requested 268395a3ecadSAnthony Wilson * chassis. 268495a3ecadSAnthony Wilson * @param SensorsAsyncResp Pointer to object holding response data 268595a3ecadSAnthony Wilson */ 2686b5a76932SEd Tanous inline void 268781ce609eSEd Tanous getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 268895a3ecadSAnthony Wilson { 268995a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisData enter"; 269095a3ecadSAnthony Wilson auto getChassisCb = 269181ce609eSEd Tanous [sensorsAsyncResp]( 2692f23b7296SEd Tanous const std::shared_ptr<boost::container::flat_set<std::string>>& 269395a3ecadSAnthony Wilson sensorNames) { 269495a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb enter"; 269581ce609eSEd Tanous processSensorList(sensorsAsyncResp, sensorNames); 269655c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassisCb exit"; 269708777fb0SLewanczyk, Dawid }; 26988d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] = 26998d1b46d7Szhanghch05 nlohmann::json::array(); 270008777fb0SLewanczyk, Dawid 270126f03899SShawn McCarney // Get set of sensors in chassis 270281ce609eSEd Tanous getChassis(sensorsAsyncResp, std::move(getChassisCb)); 270355c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassisData exit"; 2704271584abSEd Tanous } 270508777fb0SLewanczyk, Dawid 2706413961deSRichard Marian Thomaiyar /** 270749c53ac9SJohnathan Mantey * @brief Find the requested sensorName in the list of all sensors supplied by 270849c53ac9SJohnathan Mantey * the chassis node 270949c53ac9SJohnathan Mantey * 271049c53ac9SJohnathan Mantey * @param sensorName The sensor name supplied in the PATCH request 271149c53ac9SJohnathan Mantey * @param sensorsList The list of sensors managed by the chassis node 271249c53ac9SJohnathan Mantey * @param sensorsModified The list of sensors that were found as a result of 271349c53ac9SJohnathan Mantey * repeated calls to this function 271449c53ac9SJohnathan Mantey */ 271523a21a1cSEd Tanous inline bool findSensorNameUsingSensorPath( 27160a86febdSRichard Marian Thomaiyar std::string_view sensorName, 271749c53ac9SJohnathan Mantey boost::container::flat_set<std::string>& sensorsList, 271849c53ac9SJohnathan Mantey boost::container::flat_set<std::string>& sensorsModified) 271949c53ac9SJohnathan Mantey { 272028aa8de5SGeorge Liu for (auto& chassisSensor : sensorsList) 272149c53ac9SJohnathan Mantey { 272228aa8de5SGeorge Liu sdbusplus::message::object_path path(chassisSensor); 2723b00dcc27SEd Tanous std::string thisSensorName = path.filename(); 272428aa8de5SGeorge Liu if (thisSensorName.empty()) 272549c53ac9SJohnathan Mantey { 272649c53ac9SJohnathan Mantey continue; 272749c53ac9SJohnathan Mantey } 272849c53ac9SJohnathan Mantey if (thisSensorName == sensorName) 272949c53ac9SJohnathan Mantey { 273049c53ac9SJohnathan Mantey sensorsModified.emplace(chassisSensor); 273149c53ac9SJohnathan Mantey return true; 273249c53ac9SJohnathan Mantey } 273349c53ac9SJohnathan Mantey } 273449c53ac9SJohnathan Mantey return false; 273549c53ac9SJohnathan Mantey } 273649c53ac9SJohnathan Mantey 273749c53ac9SJohnathan Mantey /** 2738413961deSRichard Marian Thomaiyar * @brief Entry point for overriding sensor values of given sensor 2739413961deSRichard Marian Thomaiyar * 27408d1b46d7Szhanghch05 * @param sensorAsyncResp response object 27414bb3dc34SCarol Wang * @param allCollections Collections extract from sensors' request patch info 2742413961deSRichard Marian Thomaiyar * @param chassisSubNode Chassis Node for which the query has to happen 2743413961deSRichard Marian Thomaiyar */ 274423a21a1cSEd Tanous inline void setSensorsOverride( 2745b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp, 27464bb3dc34SCarol Wang std::unordered_map<std::string, std::vector<nlohmann::json>>& 2747397fd61fSjayaprakash Mutyala allCollections) 2748413961deSRichard Marian Thomaiyar { 274970d1d0aaSjayaprakash Mutyala BMCWEB_LOG_INFO << "setSensorsOverride for subNode" 27504bb3dc34SCarol Wang << sensorAsyncResp->chassisSubNode << "\n"; 2751413961deSRichard Marian Thomaiyar 2752f65af9e8SRichard Marian Thomaiyar const char* propertyValueName; 2753f65af9e8SRichard Marian Thomaiyar std::unordered_map<std::string, std::pair<double, std::string>> overrideMap; 2754413961deSRichard Marian Thomaiyar std::string memberId; 2755413961deSRichard Marian Thomaiyar double value; 2756f65af9e8SRichard Marian Thomaiyar for (auto& collectionItems : allCollections) 2757f65af9e8SRichard Marian Thomaiyar { 2758f65af9e8SRichard Marian Thomaiyar if (collectionItems.first == "Temperatures") 2759f65af9e8SRichard Marian Thomaiyar { 2760f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingCelsius"; 2761f65af9e8SRichard Marian Thomaiyar } 2762f65af9e8SRichard Marian Thomaiyar else if (collectionItems.first == "Fans") 2763f65af9e8SRichard Marian Thomaiyar { 2764f65af9e8SRichard Marian Thomaiyar propertyValueName = "Reading"; 2765f65af9e8SRichard Marian Thomaiyar } 2766f65af9e8SRichard Marian Thomaiyar else 2767f65af9e8SRichard Marian Thomaiyar { 2768f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingVolts"; 2769f65af9e8SRichard Marian Thomaiyar } 2770f65af9e8SRichard Marian Thomaiyar for (auto& item : collectionItems.second) 2771f65af9e8SRichard Marian Thomaiyar { 27728d1b46d7Szhanghch05 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res, 27738d1b46d7Szhanghch05 "MemberId", memberId, propertyValueName, 27748d1b46d7Szhanghch05 value)) 2775413961deSRichard Marian Thomaiyar { 2776413961deSRichard Marian Thomaiyar return; 2777413961deSRichard Marian Thomaiyar } 2778f65af9e8SRichard Marian Thomaiyar overrideMap.emplace(memberId, 2779f65af9e8SRichard Marian Thomaiyar std::make_pair(value, collectionItems.first)); 2780f65af9e8SRichard Marian Thomaiyar } 2781f65af9e8SRichard Marian Thomaiyar } 27824bb3dc34SCarol Wang 2783b5a76932SEd Tanous auto getChassisSensorListCb = [sensorAsyncResp, overrideMap]( 2784b5a76932SEd Tanous const std::shared_ptr< 278549c53ac9SJohnathan Mantey boost::container::flat_set< 2786b5a76932SEd Tanous std::string>>& sensorsList) { 278749c53ac9SJohnathan Mantey // Match sensor names in the PATCH request to those managed by the 278849c53ac9SJohnathan Mantey // chassis node 278949c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> 279049c53ac9SJohnathan Mantey sensorNames = 279149c53ac9SJohnathan Mantey std::make_shared<boost::container::flat_set<std::string>>(); 2792f65af9e8SRichard Marian Thomaiyar for (const auto& item : overrideMap) 2793413961deSRichard Marian Thomaiyar { 2794f65af9e8SRichard Marian Thomaiyar const auto& sensor = item.first; 279549c53ac9SJohnathan Mantey if (!findSensorNameUsingSensorPath(sensor, *sensorsList, 279649c53ac9SJohnathan Mantey *sensorNames)) 2797f65af9e8SRichard Marian Thomaiyar { 2798f65af9e8SRichard Marian Thomaiyar BMCWEB_LOG_INFO << "Unable to find memberId " << item.first; 27998d1b46d7Szhanghch05 messages::resourceNotFound(sensorAsyncResp->asyncResp->res, 2800f65af9e8SRichard Marian Thomaiyar item.second.second, item.first); 2801413961deSRichard Marian Thomaiyar return; 2802413961deSRichard Marian Thomaiyar } 2803f65af9e8SRichard Marian Thomaiyar } 2804413961deSRichard Marian Thomaiyar // Get the connection to which the memberId belongs 28054f277b54SJayaprakash Mutyala auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap]( 28064f277b54SJayaprakash Mutyala const boost::container::flat_set< 28074f277b54SJayaprakash Mutyala std::string>& /*connections*/, 28084f277b54SJayaprakash Mutyala const std::set<std::pair< 28094f277b54SJayaprakash Mutyala std::string, std::string>>& 2810413961deSRichard Marian Thomaiyar objectsWithConnection) { 2811f65af9e8SRichard Marian Thomaiyar if (objectsWithConnection.size() != overrideMap.size()) 2812413961deSRichard Marian Thomaiyar { 2813413961deSRichard Marian Thomaiyar BMCWEB_LOG_INFO 2814f65af9e8SRichard Marian Thomaiyar << "Unable to find all objects with proper connection " 2815f65af9e8SRichard Marian Thomaiyar << objectsWithConnection.size() << " requested " 2816f65af9e8SRichard Marian Thomaiyar << overrideMap.size() << "\n"; 28174f277b54SJayaprakash Mutyala messages::resourceNotFound(sensorAsyncResp->asyncResp->res, 2818a0ec28b6SAdrian Ambrożewicz sensorAsyncResp->chassisSubNode == 2819a0ec28b6SAdrian Ambrożewicz sensors::node::thermal 2820413961deSRichard Marian Thomaiyar ? "Temperatures" 2821413961deSRichard Marian Thomaiyar : "Voltages", 2822f65af9e8SRichard Marian Thomaiyar "Count"); 2823f65af9e8SRichard Marian Thomaiyar return; 2824f65af9e8SRichard Marian Thomaiyar } 2825f65af9e8SRichard Marian Thomaiyar for (const auto& item : objectsWithConnection) 2826f65af9e8SRichard Marian Thomaiyar { 282728aa8de5SGeorge Liu sdbusplus::message::object_path path(item.first); 282828aa8de5SGeorge Liu std::string sensorName = path.filename(); 282928aa8de5SGeorge Liu if (sensorName.empty()) 2830f65af9e8SRichard Marian Thomaiyar { 28314f277b54SJayaprakash Mutyala messages::internalError(sensorAsyncResp->asyncResp->res); 2832f65af9e8SRichard Marian Thomaiyar return; 2833f65af9e8SRichard Marian Thomaiyar } 2834f65af9e8SRichard Marian Thomaiyar 2835f65af9e8SRichard Marian Thomaiyar const auto& iterator = overrideMap.find(sensorName); 2836f65af9e8SRichard Marian Thomaiyar if (iterator == overrideMap.end()) 2837f65af9e8SRichard Marian Thomaiyar { 2838f65af9e8SRichard Marian Thomaiyar BMCWEB_LOG_INFO << "Unable to find sensor object" 2839f65af9e8SRichard Marian Thomaiyar << item.first << "\n"; 28404f277b54SJayaprakash Mutyala messages::internalError(sensorAsyncResp->asyncResp->res); 2841413961deSRichard Marian Thomaiyar return; 2842413961deSRichard Marian Thomaiyar } 2843413961deSRichard Marian Thomaiyar crow::connections::systemBus->async_method_call( 2844f65af9e8SRichard Marian Thomaiyar [sensorAsyncResp](const boost::system::error_code ec) { 2845413961deSRichard Marian Thomaiyar if (ec) 2846413961deSRichard Marian Thomaiyar { 28474f277b54SJayaprakash Mutyala if (ec.value() == 28484f277b54SJayaprakash Mutyala boost::system::errc::permission_denied) 28494f277b54SJayaprakash Mutyala { 28504f277b54SJayaprakash Mutyala BMCWEB_LOG_WARNING 28514f277b54SJayaprakash Mutyala << "Manufacturing mode is not Enabled...can't " 28524f277b54SJayaprakash Mutyala "Override the sensor value. "; 28534f277b54SJayaprakash Mutyala 28544f277b54SJayaprakash Mutyala messages::insufficientPrivilege( 28558d1b46d7Szhanghch05 sensorAsyncResp->asyncResp->res); 2856413961deSRichard Marian Thomaiyar return; 2857413961deSRichard Marian Thomaiyar } 28584f277b54SJayaprakash Mutyala BMCWEB_LOG_DEBUG 28594f277b54SJayaprakash Mutyala << "setOverrideValueStatus DBUS error: " << ec; 28604f277b54SJayaprakash Mutyala messages::internalError( 28614f277b54SJayaprakash Mutyala sensorAsyncResp->asyncResp->res); 28624f277b54SJayaprakash Mutyala } 2863413961deSRichard Marian Thomaiyar }, 28644f277b54SJayaprakash Mutyala item.second, item.first, "org.freedesktop.DBus.Properties", 28654f277b54SJayaprakash Mutyala "Set", "xyz.openbmc_project.Sensor.Value", "Value", 2866168e20c1SEd Tanous dbus::utility::DbusVariantType(iterator->second.first)); 2867f65af9e8SRichard Marian Thomaiyar } 2868413961deSRichard Marian Thomaiyar }; 2869413961deSRichard Marian Thomaiyar // Get object with connection for the given sensor name 2870413961deSRichard Marian Thomaiyar getObjectsWithConnection(sensorAsyncResp, sensorNames, 2871413961deSRichard Marian Thomaiyar std::move(getObjectsWithConnectionCb)); 2872413961deSRichard Marian Thomaiyar }; 2873413961deSRichard Marian Thomaiyar // get full sensor list for the given chassisId and cross verify the sensor. 2874413961deSRichard Marian Thomaiyar getChassis(sensorAsyncResp, std::move(getChassisSensorListCb)); 2875413961deSRichard Marian Thomaiyar } 2876413961deSRichard Marian Thomaiyar 2877a0ec28b6SAdrian Ambrożewicz /** 2878a0ec28b6SAdrian Ambrożewicz * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus 2879a0ec28b6SAdrian Ambrożewicz * path of the sensor. 2880a0ec28b6SAdrian Ambrożewicz * 2881a0ec28b6SAdrian Ambrożewicz * Function builds valid Redfish response for sensor query of given chassis and 2882a0ec28b6SAdrian Ambrożewicz * node. It then builds metadata about Redfish<->D-Bus correlations and provides 2883a0ec28b6SAdrian Ambrożewicz * it to caller in a callback. 2884a0ec28b6SAdrian Ambrożewicz * 2885a0ec28b6SAdrian Ambrożewicz * @param chassis Chassis for which retrieval should be performed 2886a0ec28b6SAdrian Ambrożewicz * @param node Node (group) of sensors. See sensors::node for supported values 2887a0ec28b6SAdrian Ambrożewicz * @param mapComplete Callback to be called with retrieval result 2888a0ec28b6SAdrian Ambrożewicz */ 2889021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis, 2890021d32cfSKrzysztof Grobelny const std::string& node, 2891a0ec28b6SAdrian Ambrożewicz SensorsAsyncResp::DataCompleteCb&& mapComplete) 2892a0ec28b6SAdrian Ambrożewicz { 2893c2bf7f99SWludzik, Jozef auto pathIt = sensors::dbus::paths.find(node); 2894c2bf7f99SWludzik, Jozef if (pathIt == sensors::dbus::paths.end()) 2895a0ec28b6SAdrian Ambrożewicz { 2896a0ec28b6SAdrian Ambrożewicz BMCWEB_LOG_ERROR << "Wrong node provided : " << node; 2897a0ec28b6SAdrian Ambrożewicz mapComplete(boost::beast::http::status::bad_request, {}); 2898a0ec28b6SAdrian Ambrożewicz return; 2899a0ec28b6SAdrian Ambrożewicz } 2900d51e072fSKrzysztof Grobelny 29019062d478SGunnar Mills auto res = std::make_shared<crow::Response>(); 29029062d478SGunnar Mills auto asyncResp = std::make_shared<bmcweb::AsyncResp>(*res); 2903a0ec28b6SAdrian Ambrożewicz auto callback = 29049062d478SGunnar Mills [res, asyncResp, mapCompleteCb{std::move(mapComplete)}]( 2905a0ec28b6SAdrian Ambrożewicz const boost::beast::http::status status, 2906a0ec28b6SAdrian Ambrożewicz const boost::container::flat_map<std::string, std::string>& 2907a0ec28b6SAdrian Ambrożewicz uriToDbus) { mapCompleteCb(status, uriToDbus); }; 2908a0ec28b6SAdrian Ambrożewicz 2909a0ec28b6SAdrian Ambrożewicz auto resp = std::make_shared<SensorsAsyncResp>( 2910d51e072fSKrzysztof Grobelny asyncResp, chassis, pathIt->second, node, std::move(callback)); 2911a0ec28b6SAdrian Ambrożewicz getChassisData(resp); 2912a0ec28b6SAdrian Ambrożewicz } 2913a0ec28b6SAdrian Ambrożewicz 29147e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app) 291595a3ecadSAnthony Wilson { 29167e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/") 2917ed398213SEd Tanous .privileges(redfish::privileges::getSensorCollection) 29187e860f15SJohn Edward Broadbent .methods( 29197e860f15SJohn Edward Broadbent boost::beast::http::verb::get)([](const crow::Request&, 29207e860f15SJohn Edward Broadbent const std::shared_ptr< 29217e860f15SJohn Edward Broadbent bmcweb::AsyncResp>& aResp, 29227e860f15SJohn Edward Broadbent const std::string& chassisId) { 292395a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "SensorCollection doGet enter"; 29248d1b46d7Szhanghch05 292595a3ecadSAnthony Wilson std::shared_ptr<SensorsAsyncResp> asyncResp = 2926a0ec28b6SAdrian Ambrożewicz std::make_shared<SensorsAsyncResp>( 29278d1b46d7Szhanghch05 aResp, chassisId, 29288d1b46d7Szhanghch05 sensors::dbus::paths.at(sensors::node::sensors), 2929a0ec28b6SAdrian Ambrożewicz sensors::node::sensors); 293095a3ecadSAnthony Wilson 293195a3ecadSAnthony Wilson auto getChassisCb = 2932b5a76932SEd Tanous [asyncResp]( 29337e860f15SJohn Edward Broadbent const std::shared_ptr< 29347e860f15SJohn Edward Broadbent boost::container::flat_set<std::string>>& sensorNames) { 293595a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb enter"; 293695a3ecadSAnthony Wilson 293795a3ecadSAnthony Wilson nlohmann::json& entriesArray = 29388d1b46d7Szhanghch05 asyncResp->asyncResp->res.jsonValue["Members"]; 293995a3ecadSAnthony Wilson for (auto& sensor : *sensorNames) 294095a3ecadSAnthony Wilson { 294195a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor; 294295a3ecadSAnthony Wilson 294328aa8de5SGeorge Liu sdbusplus::message::object_path path(sensor); 294428aa8de5SGeorge Liu std::string sensorName = path.filename(); 294528aa8de5SGeorge Liu if (sensorName.empty()) 294695a3ecadSAnthony Wilson { 29477e860f15SJohn Edward Broadbent BMCWEB_LOG_ERROR << "Invalid sensor path: " 29487e860f15SJohn Edward Broadbent << sensor; 29498d1b46d7Szhanghch05 messages::internalError(asyncResp->asyncResp->res); 295095a3ecadSAnthony Wilson return; 295195a3ecadSAnthony Wilson } 295295a3ecadSAnthony Wilson entriesArray.push_back( 29537e860f15SJohn Edward Broadbent {{"@odata.id", "/redfish/v1/Chassis/" + 29547e860f15SJohn Edward Broadbent asyncResp->chassisId + "/" + 29557e860f15SJohn Edward Broadbent asyncResp->chassisSubNode + "/" + 29567e860f15SJohn Edward Broadbent sensorName}}); 295795a3ecadSAnthony Wilson } 295895a3ecadSAnthony Wilson 29598d1b46d7Szhanghch05 asyncResp->asyncResp->res.jsonValue["Members@odata.count"] = 296095a3ecadSAnthony Wilson entriesArray.size(); 296195a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb exit"; 296295a3ecadSAnthony Wilson }; 296395a3ecadSAnthony Wilson 296495a3ecadSAnthony Wilson // Get set of sensors in chassis 296595a3ecadSAnthony Wilson getChassis(asyncResp, std::move(getChassisCb)); 296695a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "SensorCollection doGet exit"; 29677e860f15SJohn Edward Broadbent }); 296895a3ecadSAnthony Wilson } 296995a3ecadSAnthony Wilson 29707e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app) 297195a3ecadSAnthony Wilson { 29727e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/") 2973ed398213SEd Tanous .privileges(redfish::privileges::getSensor) 29747e860f15SJohn Edward Broadbent .methods( 29757e860f15SJohn Edward Broadbent boost::beast::http::verb::get)([](const crow::Request&, 29767e860f15SJohn Edward Broadbent const std::shared_ptr< 29777e860f15SJohn Edward Broadbent bmcweb::AsyncResp>& aResp, 29787e860f15SJohn Edward Broadbent const std::string& chassisId, 29797e860f15SJohn Edward Broadbent const std::string& sensorName) { 298095a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "Sensor doGet enter"; 298195a3ecadSAnthony Wilson std::shared_ptr<SensorsAsyncResp> asyncResp = 29828d1b46d7Szhanghch05 std::make_shared<SensorsAsyncResp>(aResp, chassisId, 2983a0ec28b6SAdrian Ambrożewicz std::vector<const char*>(), 2984a0ec28b6SAdrian Ambrożewicz sensors::node::sensors); 298595a3ecadSAnthony Wilson 298695a3ecadSAnthony Wilson const std::array<const char*, 1> interfaces = { 298795a3ecadSAnthony Wilson "xyz.openbmc_project.Sensor.Value"}; 298895a3ecadSAnthony Wilson 298995a3ecadSAnthony Wilson // Get a list of all of the sensors that implement Sensor.Value 299095a3ecadSAnthony Wilson // and get the path and service name associated with the sensor 299195a3ecadSAnthony Wilson crow::connections::systemBus->async_method_call( 299295a3ecadSAnthony Wilson [asyncResp, sensorName](const boost::system::error_code ec, 299395a3ecadSAnthony Wilson const GetSubTreeType& subtree) { 299495a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "respHandler1 enter"; 299595a3ecadSAnthony Wilson if (ec) 299695a3ecadSAnthony Wilson { 29978d1b46d7Szhanghch05 messages::internalError(asyncResp->asyncResp->res); 29987e860f15SJohn Edward Broadbent BMCWEB_LOG_ERROR 29997e860f15SJohn Edward Broadbent << "Sensor getSensorPaths resp_handler: " 300095a3ecadSAnthony Wilson << "Dbus error " << ec; 300195a3ecadSAnthony Wilson return; 300295a3ecadSAnthony Wilson } 300395a3ecadSAnthony Wilson 300495a3ecadSAnthony Wilson GetSubTreeType::const_iterator it = std::find_if( 300595a3ecadSAnthony Wilson subtree.begin(), subtree.end(), 300695a3ecadSAnthony Wilson [sensorName]( 300795a3ecadSAnthony Wilson const std::pair< 300895a3ecadSAnthony Wilson std::string, 30097e860f15SJohn Edward Broadbent std::vector<std::pair< 30107e860f15SJohn Edward Broadbent std::string, std::vector<std::string>>>>& 301195a3ecadSAnthony Wilson object) { 301228aa8de5SGeorge Liu sdbusplus::message::object_path path(object.first); 301328aa8de5SGeorge Liu std::string name = path.filename(); 301428aa8de5SGeorge Liu if (name.empty()) 301595a3ecadSAnthony Wilson { 301695a3ecadSAnthony Wilson BMCWEB_LOG_ERROR << "Invalid sensor path: " 301728aa8de5SGeorge Liu << object.first; 301895a3ecadSAnthony Wilson return false; 301995a3ecadSAnthony Wilson } 302095a3ecadSAnthony Wilson 302195a3ecadSAnthony Wilson return name == sensorName; 302295a3ecadSAnthony Wilson }); 302395a3ecadSAnthony Wilson 302495a3ecadSAnthony Wilson if (it == subtree.end()) 302595a3ecadSAnthony Wilson { 302695a3ecadSAnthony Wilson BMCWEB_LOG_ERROR << "Could not find path for sensor: " 302795a3ecadSAnthony Wilson << sensorName; 30288d1b46d7Szhanghch05 messages::resourceNotFound(asyncResp->asyncResp->res, 30298d1b46d7Szhanghch05 "Sensor", sensorName); 303095a3ecadSAnthony Wilson return; 303195a3ecadSAnthony Wilson } 303295a3ecadSAnthony Wilson std::string_view sensorPath = (*it).first; 303395a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" 303495a3ecadSAnthony Wilson << sensorName << "': " << sensorPath; 303595a3ecadSAnthony Wilson 30367e860f15SJohn Edward Broadbent const std::shared_ptr< 30377e860f15SJohn Edward Broadbent boost::container::flat_set<std::string>> 303895a3ecadSAnthony Wilson sensorList = std::make_shared< 303995a3ecadSAnthony Wilson boost::container::flat_set<std::string>>(); 304095a3ecadSAnthony Wilson 304195a3ecadSAnthony Wilson sensorList->emplace(sensorPath); 304295a3ecadSAnthony Wilson processSensorList(asyncResp, sensorList); 304395a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "respHandler1 exit"; 304495a3ecadSAnthony Wilson }, 304595a3ecadSAnthony Wilson "xyz.openbmc_project.ObjectMapper", 304695a3ecadSAnthony Wilson "/xyz/openbmc_project/object_mapper", 304795a3ecadSAnthony Wilson "xyz.openbmc_project.ObjectMapper", "GetSubTree", 304895a3ecadSAnthony Wilson "/xyz/openbmc_project/sensors", 2, interfaces); 30497e860f15SJohn Edward Broadbent }); 305095a3ecadSAnthony Wilson } 305195a3ecadSAnthony Wilson 305208777fb0SLewanczyk, Dawid } // namespace redfish 3053