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 230ecd6a3a2SEd Tanous SensorsAsyncResp(const SensorsAsyncResp&) = delete; 231ecd6a3a2SEd Tanous SensorsAsyncResp(SensorsAsyncResp&&) = delete; 232ecd6a3a2SEd Tanous SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete; 233ecd6a3a2SEd Tanous SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete; 234ecd6a3a2SEd Tanous 235a0ec28b6SAdrian Ambrożewicz void addMetadata(const nlohmann::json& sensorObject, 236a0ec28b6SAdrian Ambrożewicz const std::string& valueKey, const std::string& dbusPath) 237a0ec28b6SAdrian Ambrożewicz { 238a0ec28b6SAdrian Ambrożewicz if (metadata) 239a0ec28b6SAdrian Ambrożewicz { 240a0ec28b6SAdrian Ambrożewicz metadata->emplace_back(SensorData{sensorObject["Name"], 241a0ec28b6SAdrian Ambrożewicz sensorObject["@odata.id"], 242a0ec28b6SAdrian Ambrożewicz valueKey, dbusPath}); 243a0ec28b6SAdrian Ambrożewicz } 244a0ec28b6SAdrian Ambrożewicz } 245a0ec28b6SAdrian Ambrożewicz 246a0ec28b6SAdrian Ambrożewicz void updateUri(const std::string& name, const std::string& uri) 247a0ec28b6SAdrian Ambrożewicz { 248a0ec28b6SAdrian Ambrożewicz if (metadata) 249a0ec28b6SAdrian Ambrożewicz { 250a0ec28b6SAdrian Ambrożewicz for (auto& sensor : *metadata) 251a0ec28b6SAdrian Ambrożewicz { 252a0ec28b6SAdrian Ambrożewicz if (sensor.name == name) 253a0ec28b6SAdrian Ambrożewicz { 254a0ec28b6SAdrian Ambrożewicz sensor.uri = uri; 255a0ec28b6SAdrian Ambrożewicz } 256a0ec28b6SAdrian Ambrożewicz } 257a0ec28b6SAdrian Ambrożewicz } 258a0ec28b6SAdrian Ambrożewicz } 259a0ec28b6SAdrian Ambrożewicz 2608d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp> asyncResp; 261a0ec28b6SAdrian Ambrożewicz const std::string chassisId; 26208777fb0SLewanczyk, Dawid const std::vector<const char*> types; 263a0ec28b6SAdrian Ambrożewicz const std::string chassisSubNode; 264a0ec28b6SAdrian Ambrożewicz 265a0ec28b6SAdrian Ambrożewicz private: 266a0ec28b6SAdrian Ambrożewicz std::optional<std::vector<SensorData>> metadata; 267a0ec28b6SAdrian Ambrożewicz DataCompleteCb dataComplete; 26808777fb0SLewanczyk, Dawid }; 26908777fb0SLewanczyk, Dawid 27008777fb0SLewanczyk, Dawid /** 271d500549bSAnthony Wilson * Possible states for physical inventory leds 272d500549bSAnthony Wilson */ 273d500549bSAnthony Wilson enum class LedState 274d500549bSAnthony Wilson { 275d500549bSAnthony Wilson OFF, 276d500549bSAnthony Wilson ON, 277d500549bSAnthony Wilson BLINK, 278d500549bSAnthony Wilson UNKNOWN 279d500549bSAnthony Wilson }; 280d500549bSAnthony Wilson 281d500549bSAnthony Wilson /** 282adc4f0dbSShawn McCarney * D-Bus inventory item associated with one or more sensors. 283adc4f0dbSShawn McCarney */ 284adc4f0dbSShawn McCarney class InventoryItem 285adc4f0dbSShawn McCarney { 286adc4f0dbSShawn McCarney public: 287adc4f0dbSShawn McCarney InventoryItem(const std::string& objPath) : 288adc4f0dbSShawn McCarney objectPath(objPath), name(), isPresent(true), isFunctional(true), 28942cbe538SGunnar Mills isPowerSupply(false), powerSupplyEfficiencyPercent(-1), manufacturer(), 29042cbe538SGunnar Mills model(), partNumber(), serialNumber(), sensors(), ledObjectPath(""), 291d500549bSAnthony Wilson ledState(LedState::UNKNOWN) 292adc4f0dbSShawn McCarney { 293adc4f0dbSShawn McCarney // Set inventory item name to last node of object path 29428aa8de5SGeorge Liu sdbusplus::message::object_path path(objectPath); 29528aa8de5SGeorge Liu name = path.filename(); 29628aa8de5SGeorge Liu if (name.empty()) 297adc4f0dbSShawn McCarney { 29828aa8de5SGeorge Liu BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath; 299adc4f0dbSShawn McCarney } 300adc4f0dbSShawn McCarney } 301adc4f0dbSShawn McCarney 302adc4f0dbSShawn McCarney std::string objectPath; 303adc4f0dbSShawn McCarney std::string name; 304adc4f0dbSShawn McCarney bool isPresent; 305adc4f0dbSShawn McCarney bool isFunctional; 306adc4f0dbSShawn McCarney bool isPowerSupply; 30742cbe538SGunnar Mills int powerSupplyEfficiencyPercent; 308adc4f0dbSShawn McCarney std::string manufacturer; 309adc4f0dbSShawn McCarney std::string model; 310adc4f0dbSShawn McCarney std::string partNumber; 311adc4f0dbSShawn McCarney std::string serialNumber; 312adc4f0dbSShawn McCarney std::set<std::string> sensors; 313d500549bSAnthony Wilson std::string ledObjectPath; 314d500549bSAnthony Wilson LedState ledState; 315adc4f0dbSShawn McCarney }; 316adc4f0dbSShawn McCarney 317adc4f0dbSShawn McCarney /** 318413961deSRichard Marian Thomaiyar * @brief Get objects with connection necessary for sensors 319588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 32008777fb0SLewanczyk, Dawid * @param sensorNames Sensors retrieved from chassis 32108777fb0SLewanczyk, Dawid * @param callback Callback for processing gathered connections 32208777fb0SLewanczyk, Dawid */ 32308777fb0SLewanczyk, Dawid template <typename Callback> 324413961deSRichard Marian Thomaiyar void getObjectsWithConnection( 32581ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 326b5a76932SEd Tanous const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames, 3271abe55efSEd Tanous Callback&& callback) 3281abe55efSEd Tanous { 329413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter"; 33003b5bae3SJames Feist const std::string path = "/xyz/openbmc_project/sensors"; 33108777fb0SLewanczyk, Dawid const std::array<std::string, 1> interfaces = { 33208777fb0SLewanczyk, Dawid "xyz.openbmc_project.Sensor.Value"}; 33308777fb0SLewanczyk, Dawid 33408777fb0SLewanczyk, Dawid // Response handler for parsing objects subtree 335f94c4ecfSEd Tanous auto respHandler = [callback{std::forward<Callback>(callback)}, 336f94c4ecfSEd Tanous sensorsAsyncResp, 3371abe55efSEd Tanous sensorNames](const boost::system::error_code ec, 3381abe55efSEd Tanous const GetSubTreeType& subtree) { 339413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter"; 3401abe55efSEd Tanous if (ec) 3411abe55efSEd Tanous { 3428d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 343413961deSRichard Marian Thomaiyar BMCWEB_LOG_ERROR 344413961deSRichard Marian Thomaiyar << "getObjectsWithConnection resp_handler: Dbus error " << ec; 34508777fb0SLewanczyk, Dawid return; 34608777fb0SLewanczyk, Dawid } 34708777fb0SLewanczyk, Dawid 34855c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees"; 34908777fb0SLewanczyk, Dawid 35008777fb0SLewanczyk, Dawid // Make unique list of connections only for requested sensor types and 35108777fb0SLewanczyk, Dawid // found in the chassis 35208777fb0SLewanczyk, Dawid boost::container::flat_set<std::string> connections; 353413961deSRichard Marian Thomaiyar std::set<std::pair<std::string, std::string>> objectsWithConnection; 3541abe55efSEd Tanous // Intrinsic to avoid malloc. Most systems will have < 8 sensor 3551abe55efSEd Tanous // producers 35608777fb0SLewanczyk, Dawid connections.reserve(8); 35708777fb0SLewanczyk, Dawid 35849c53ac9SJohnathan Mantey BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size(); 35949c53ac9SJohnathan Mantey for (const std::string& tsensor : *sensorNames) 3601abe55efSEd Tanous { 36155c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor; 36208777fb0SLewanczyk, Dawid } 36308777fb0SLewanczyk, Dawid 36408777fb0SLewanczyk, Dawid for (const std::pair< 36508777fb0SLewanczyk, Dawid std::string, 36608777fb0SLewanczyk, Dawid std::vector<std::pair<std::string, std::vector<std::string>>>>& 3671abe55efSEd Tanous object : subtree) 3681abe55efSEd Tanous { 36949c53ac9SJohnathan Mantey if (sensorNames->find(object.first) != sensorNames->end()) 3701abe55efSEd Tanous { 37149c53ac9SJohnathan Mantey for (const std::pair<std::string, std::vector<std::string>>& 3721abe55efSEd Tanous objData : object.second) 3731abe55efSEd Tanous { 37449c53ac9SJohnathan Mantey BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first; 37508777fb0SLewanczyk, Dawid connections.insert(objData.first); 376de629b6eSShawn McCarney objectsWithConnection.insert( 377de629b6eSShawn McCarney std::make_pair(object.first, objData.first)); 37808777fb0SLewanczyk, Dawid } 37908777fb0SLewanczyk, Dawid } 38008777fb0SLewanczyk, Dawid } 38155c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections"; 382413961deSRichard Marian Thomaiyar callback(std::move(connections), std::move(objectsWithConnection)); 383413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit"; 38408777fb0SLewanczyk, Dawid }; 38508777fb0SLewanczyk, Dawid // Make call to ObjectMapper to find all sensors objects 38655c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 38755c7b7a2SEd Tanous std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 3881abe55efSEd Tanous "/xyz/openbmc_project/object_mapper", 3891abe55efSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces); 390413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit"; 391413961deSRichard Marian Thomaiyar } 392413961deSRichard Marian Thomaiyar 393413961deSRichard Marian Thomaiyar /** 394413961deSRichard Marian Thomaiyar * @brief Create connections necessary for sensors 395413961deSRichard Marian Thomaiyar * @param SensorsAsyncResp Pointer to object holding response data 396413961deSRichard Marian Thomaiyar * @param sensorNames Sensors retrieved from chassis 397413961deSRichard Marian Thomaiyar * @param callback Callback for processing gathered connections 398413961deSRichard Marian Thomaiyar */ 399413961deSRichard Marian Thomaiyar template <typename Callback> 40049c53ac9SJohnathan Mantey void getConnections( 40181ce609eSEd Tanous std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 40249c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames, 403413961deSRichard Marian Thomaiyar Callback&& callback) 404413961deSRichard Marian Thomaiyar { 405413961deSRichard Marian Thomaiyar auto objectsWithConnectionCb = 406413961deSRichard Marian Thomaiyar [callback](const boost::container::flat_set<std::string>& connections, 407413961deSRichard Marian Thomaiyar const std::set<std::pair<std::string, std::string>>& 4083174e4dfSEd Tanous /*objectsWithConnection*/) { callback(connections); }; 40981ce609eSEd Tanous getObjectsWithConnection(sensorsAsyncResp, sensorNames, 410413961deSRichard Marian Thomaiyar std::move(objectsWithConnectionCb)); 41108777fb0SLewanczyk, Dawid } 41208777fb0SLewanczyk, Dawid 41308777fb0SLewanczyk, Dawid /** 41449c53ac9SJohnathan Mantey * @brief Shrinks the list of sensors for processing 41549c53ac9SJohnathan Mantey * @param SensorsAysncResp The class holding the Redfish response 41649c53ac9SJohnathan Mantey * @param allSensors A list of all the sensors associated to the 41749c53ac9SJohnathan Mantey * chassis element (i.e. baseboard, front panel, etc...) 41849c53ac9SJohnathan Mantey * @param activeSensors A list that is a reduction of the incoming 41949c53ac9SJohnathan Mantey * allSensors list. Eliminate Thermal sensors when a Power request is 42049c53ac9SJohnathan Mantey * made, and eliminate Power sensors when a Thermal request is made. 42149c53ac9SJohnathan Mantey */ 42223a21a1cSEd Tanous inline void reduceSensorList( 42381ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 42449c53ac9SJohnathan Mantey const std::vector<std::string>* allSensors, 425b5a76932SEd Tanous const std::shared_ptr<boost::container::flat_set<std::string>>& 426b5a76932SEd Tanous activeSensors) 42749c53ac9SJohnathan Mantey { 42881ce609eSEd Tanous if (sensorsAsyncResp == nullptr) 42949c53ac9SJohnathan Mantey { 43049c53ac9SJohnathan Mantey return; 43149c53ac9SJohnathan Mantey } 43249c53ac9SJohnathan Mantey if ((allSensors == nullptr) || (activeSensors == nullptr)) 43349c53ac9SJohnathan Mantey { 43449c53ac9SJohnathan Mantey messages::resourceNotFound( 4358d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode, 43681ce609eSEd Tanous sensorsAsyncResp->chassisSubNode == sensors::node::thermal 437a0ec28b6SAdrian Ambrożewicz ? "Temperatures" 43849c53ac9SJohnathan Mantey : "Voltages"); 43949c53ac9SJohnathan Mantey 44049c53ac9SJohnathan Mantey return; 44149c53ac9SJohnathan Mantey } 44249c53ac9SJohnathan Mantey if (allSensors->empty()) 44349c53ac9SJohnathan Mantey { 44449c53ac9SJohnathan Mantey // Nothing to do, the activeSensors object is also empty 44549c53ac9SJohnathan Mantey return; 44649c53ac9SJohnathan Mantey } 44749c53ac9SJohnathan Mantey 44881ce609eSEd Tanous for (const char* type : sensorsAsyncResp->types) 44949c53ac9SJohnathan Mantey { 45049c53ac9SJohnathan Mantey for (const std::string& sensor : *allSensors) 45149c53ac9SJohnathan Mantey { 45249c53ac9SJohnathan Mantey if (boost::starts_with(sensor, type)) 45349c53ac9SJohnathan Mantey { 45449c53ac9SJohnathan Mantey activeSensors->emplace(sensor); 45549c53ac9SJohnathan Mantey } 45649c53ac9SJohnathan Mantey } 45749c53ac9SJohnathan Mantey } 45849c53ac9SJohnathan Mantey } 45949c53ac9SJohnathan Mantey 46049c53ac9SJohnathan Mantey /** 4614bb3dc34SCarol Wang * @brief Retrieves valid chassis path 4624bb3dc34SCarol Wang * @param asyncResp Pointer to object holding response data 4634bb3dc34SCarol Wang * @param callback Callback for next step to get valid chassis path 4644bb3dc34SCarol Wang */ 4654bb3dc34SCarol Wang template <typename Callback> 466b5a76932SEd Tanous void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp, 4674bb3dc34SCarol Wang Callback&& callback) 4684bb3dc34SCarol Wang { 4694bb3dc34SCarol Wang BMCWEB_LOG_DEBUG << "checkChassisId enter"; 4704bb3dc34SCarol Wang const std::array<const char*, 2> interfaces = { 4714bb3dc34SCarol Wang "xyz.openbmc_project.Inventory.Item.Board", 4724bb3dc34SCarol Wang "xyz.openbmc_project.Inventory.Item.Chassis"}; 4734bb3dc34SCarol Wang 4744bb3dc34SCarol Wang auto respHandler = 475f94c4ecfSEd Tanous [callback{std::forward<Callback>(callback)}, 4764bb3dc34SCarol Wang asyncResp](const boost::system::error_code ec, 4774bb3dc34SCarol Wang const std::vector<std::string>& chassisPaths) mutable { 4784bb3dc34SCarol Wang BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter"; 4794bb3dc34SCarol Wang if (ec) 4804bb3dc34SCarol Wang { 4814bb3dc34SCarol Wang BMCWEB_LOG_ERROR 4824bb3dc34SCarol Wang << "getValidChassisPath respHandler DBUS error: " << ec; 4838d1b46d7Szhanghch05 messages::internalError(asyncResp->asyncResp->res); 4844bb3dc34SCarol Wang return; 4854bb3dc34SCarol Wang } 4864bb3dc34SCarol Wang 4874bb3dc34SCarol Wang std::optional<std::string> chassisPath; 4884bb3dc34SCarol Wang std::string chassisName; 4894bb3dc34SCarol Wang for (const std::string& chassis : chassisPaths) 4904bb3dc34SCarol Wang { 49128aa8de5SGeorge Liu sdbusplus::message::object_path path(chassis); 49228aa8de5SGeorge Liu chassisName = path.filename(); 49328aa8de5SGeorge Liu if (chassisName.empty()) 4944bb3dc34SCarol Wang { 4954bb3dc34SCarol Wang BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis; 4964bb3dc34SCarol Wang continue; 4974bb3dc34SCarol Wang } 4984bb3dc34SCarol Wang if (chassisName == asyncResp->chassisId) 4994bb3dc34SCarol Wang { 5004bb3dc34SCarol Wang chassisPath = chassis; 5014bb3dc34SCarol Wang break; 5024bb3dc34SCarol Wang } 5034bb3dc34SCarol Wang } 5044bb3dc34SCarol Wang callback(chassisPath); 5054bb3dc34SCarol Wang }; 5064bb3dc34SCarol Wang 5074bb3dc34SCarol Wang // Get the Chassis Collection 5084bb3dc34SCarol Wang crow::connections::systemBus->async_method_call( 5094bb3dc34SCarol Wang respHandler, "xyz.openbmc_project.ObjectMapper", 5104bb3dc34SCarol Wang "/xyz/openbmc_project/object_mapper", 5114bb3dc34SCarol Wang "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 5124bb3dc34SCarol Wang "/xyz/openbmc_project/inventory", 0, interfaces); 5134bb3dc34SCarol Wang BMCWEB_LOG_DEBUG << "checkChassisId exit"; 5144bb3dc34SCarol Wang } 5154bb3dc34SCarol Wang 5164bb3dc34SCarol Wang /** 51708777fb0SLewanczyk, Dawid * @brief Retrieves requested chassis sensors and redundancy data from DBus . 518588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 51908777fb0SLewanczyk, Dawid * @param callback Callback for next step in gathered sensor processing 52008777fb0SLewanczyk, Dawid */ 52108777fb0SLewanczyk, Dawid template <typename Callback> 522b5a76932SEd Tanous void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 5231abe55efSEd Tanous Callback&& callback) 5241abe55efSEd Tanous { 52555c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis enter"; 526adc4f0dbSShawn McCarney const std::array<const char*, 2> interfaces = { 52749c53ac9SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Board", 528adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.Chassis"}; 529f94c4ecfSEd Tanous auto respHandler = [callback{std::forward<Callback>(callback)}, 530f94c4ecfSEd Tanous sensorsAsyncResp]( 53149c53ac9SJohnathan Mantey const boost::system::error_code ec, 53249c53ac9SJohnathan Mantey const std::vector<std::string>& chassisPaths) { 53355c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis respHandler enter"; 5341abe55efSEd Tanous if (ec) 5351abe55efSEd Tanous { 53655c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec; 5378d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 53808777fb0SLewanczyk, Dawid return; 53908777fb0SLewanczyk, Dawid } 54008777fb0SLewanczyk, Dawid 54149c53ac9SJohnathan Mantey const std::string* chassisPath = nullptr; 54249c53ac9SJohnathan Mantey std::string chassisName; 54349c53ac9SJohnathan Mantey for (const std::string& chassis : chassisPaths) 5441abe55efSEd Tanous { 54528aa8de5SGeorge Liu sdbusplus::message::object_path path(chassis); 54628aa8de5SGeorge Liu chassisName = path.filename(); 54728aa8de5SGeorge Liu if (chassisName.empty()) 5481abe55efSEd Tanous { 54949c53ac9SJohnathan Mantey BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis; 550daf36e2eSEd Tanous continue; 551daf36e2eSEd Tanous } 55249c53ac9SJohnathan Mantey if (chassisName == sensorsAsyncResp->chassisId) 5531abe55efSEd Tanous { 55449c53ac9SJohnathan Mantey chassisPath = &chassis; 55549c53ac9SJohnathan Mantey break; 556daf36e2eSEd Tanous } 55749c53ac9SJohnathan Mantey } 55849c53ac9SJohnathan Mantey if (chassisPath == nullptr) 5591abe55efSEd Tanous { 5608d1b46d7Szhanghch05 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, 5618d1b46d7Szhanghch05 "Chassis", sensorsAsyncResp->chassisId); 56249c53ac9SJohnathan Mantey return; 5631abe55efSEd Tanous } 56408777fb0SLewanczyk, Dawid 56549c53ac9SJohnathan Mantey const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode; 566a0ec28b6SAdrian Ambrożewicz if (chassisSubNode == sensors::node::power) 56749c53ac9SJohnathan Mantey { 5688d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] = 56949c53ac9SJohnathan Mantey "#Power.v1_5_2.Power"; 57049c53ac9SJohnathan Mantey } 571a0ec28b6SAdrian Ambrożewicz else if (chassisSubNode == sensors::node::thermal) 57249c53ac9SJohnathan Mantey { 5738d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] = 57449c53ac9SJohnathan Mantey "#Thermal.v1_4_0.Thermal"; 5758d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] = 5768d1b46d7Szhanghch05 nlohmann::json::array(); 5778d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] = 5784f9a2130SJennifer Lee nlohmann::json::array(); 57949c53ac9SJohnathan Mantey } 580a0ec28b6SAdrian Ambrożewicz else if (chassisSubNode == sensors::node::sensors) 58195a3ecadSAnthony Wilson { 5828d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] = 58395a3ecadSAnthony Wilson "#SensorCollection.SensorCollection"; 5848d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Description"] = 58595a3ecadSAnthony Wilson "Collection of Sensors for this Chassis"; 5868d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Members"] = 58795a3ecadSAnthony Wilson nlohmann::json::array(); 5888d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] = 5898d1b46d7Szhanghch05 0; 59095a3ecadSAnthony Wilson } 59195a3ecadSAnthony Wilson 592a0ec28b6SAdrian Ambrożewicz if (chassisSubNode != sensors::node::sensors) 59395a3ecadSAnthony Wilson { 5948d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode; 59595a3ecadSAnthony Wilson } 59695a3ecadSAnthony Wilson 5978d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] = 59849c53ac9SJohnathan Mantey "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" + 59949c53ac9SJohnathan Mantey chassisSubNode; 6008d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode; 6018fb49dd6SShawn McCarney // Get the list of all sensors for this Chassis element 6028fb49dd6SShawn McCarney std::string sensorPath = *chassisPath + "/all_sensors"; 6031e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::vector<std::string>>( 6041e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper", 6051e1e598dSJonathan Doman sensorPath, "xyz.openbmc_project.Association", "endpoints", 606f94c4ecfSEd Tanous [sensorsAsyncResp, 607f94c4ecfSEd Tanous callback{std::forward<const Callback>(callback)}]( 608271584abSEd Tanous const boost::system::error_code& e, 6091e1e598dSJonathan Doman const std::vector<std::string>& nodeSensorList) { 610271584abSEd Tanous if (e) 61149c53ac9SJohnathan Mantey { 612271584abSEd Tanous if (e.value() != EBADR) 61349c53ac9SJohnathan Mantey { 6148d1b46d7Szhanghch05 messages::internalError( 6158d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 61649c53ac9SJohnathan Mantey return; 61749c53ac9SJohnathan Mantey } 61849c53ac9SJohnathan Mantey } 61949c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> 62049c53ac9SJohnathan Mantey culledSensorList = std::make_shared< 62149c53ac9SJohnathan Mantey boost::container::flat_set<std::string>>(); 6221e1e598dSJonathan Doman reduceSensorList(sensorsAsyncResp, &nodeSensorList, 62349c53ac9SJohnathan Mantey culledSensorList); 62449c53ac9SJohnathan Mantey callback(culledSensorList); 6251e1e598dSJonathan Doman }); 62649c53ac9SJohnathan Mantey }; 62749c53ac9SJohnathan Mantey 62849c53ac9SJohnathan Mantey // Get the Chassis Collection 62949c53ac9SJohnathan Mantey crow::connections::systemBus->async_method_call( 63049c53ac9SJohnathan Mantey respHandler, "xyz.openbmc_project.ObjectMapper", 63149c53ac9SJohnathan Mantey "/xyz/openbmc_project/object_mapper", 63249c53ac9SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 633271584abSEd Tanous "/xyz/openbmc_project/inventory", 0, interfaces); 63455c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis exit"; 63508777fb0SLewanczyk, Dawid } 63608777fb0SLewanczyk, Dawid 63708777fb0SLewanczyk, Dawid /** 638de629b6eSShawn McCarney * @brief Finds all DBus object paths that implement ObjectManager. 639de629b6eSShawn McCarney * 640de629b6eSShawn McCarney * Creates a mapping from the associated connection name to the object path. 641de629b6eSShawn McCarney * 642de629b6eSShawn McCarney * Finds the object paths asynchronously. Invokes callback when information has 643de629b6eSShawn McCarney * been obtained. 644de629b6eSShawn McCarney * 645de629b6eSShawn McCarney * The callback must have the following signature: 646de629b6eSShawn McCarney * @code 6478fb49dd6SShawn McCarney * callback(std::shared_ptr<boost::container::flat_map<std::string, 6488fb49dd6SShawn McCarney * std::string>> objectMgrPaths) 649de629b6eSShawn McCarney * @endcode 650de629b6eSShawn McCarney * 65149c53ac9SJohnathan Mantey * @param sensorsAsyncResp Pointer to object holding response data. 652de629b6eSShawn McCarney * @param callback Callback to invoke when object paths obtained. 653de629b6eSShawn McCarney */ 654de629b6eSShawn McCarney template <typename Callback> 655b5a76932SEd Tanous void getObjectManagerPaths( 65681ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 657de629b6eSShawn McCarney Callback&& callback) 658de629b6eSShawn McCarney { 659de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter"; 660de629b6eSShawn McCarney const std::array<std::string, 1> interfaces = { 661de629b6eSShawn McCarney "org.freedesktop.DBus.ObjectManager"}; 662de629b6eSShawn McCarney 663de629b6eSShawn McCarney // Response handler for GetSubTree DBus method 664f94c4ecfSEd Tanous auto respHandler = [callback{std::forward<Callback>(callback)}, 66581ce609eSEd Tanous sensorsAsyncResp](const boost::system::error_code ec, 666de629b6eSShawn McCarney const GetSubTreeType& subtree) { 667de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter"; 668de629b6eSShawn McCarney if (ec) 669de629b6eSShawn McCarney { 6708d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 671de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error " 672de629b6eSShawn McCarney << ec; 673de629b6eSShawn McCarney return; 674de629b6eSShawn McCarney } 675de629b6eSShawn McCarney 676de629b6eSShawn McCarney // Loop over returned object paths 6778fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 6788fb49dd6SShawn McCarney objectMgrPaths = std::make_shared< 6798fb49dd6SShawn McCarney boost::container::flat_map<std::string, std::string>>(); 680de629b6eSShawn McCarney for (const std::pair< 681de629b6eSShawn McCarney std::string, 682de629b6eSShawn McCarney std::vector<std::pair<std::string, std::vector<std::string>>>>& 683de629b6eSShawn McCarney object : subtree) 684de629b6eSShawn McCarney { 685de629b6eSShawn McCarney // Loop over connections for current object path 686de629b6eSShawn McCarney const std::string& objectPath = object.first; 687de629b6eSShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 688de629b6eSShawn McCarney objData : object.second) 689de629b6eSShawn McCarney { 690de629b6eSShawn McCarney // Add mapping from connection to object path 691de629b6eSShawn McCarney const std::string& connection = objData.first; 6928fb49dd6SShawn McCarney (*objectMgrPaths)[connection] = objectPath; 693de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> " 694de629b6eSShawn McCarney << objectPath; 695de629b6eSShawn McCarney } 696de629b6eSShawn McCarney } 6978fb49dd6SShawn McCarney callback(objectMgrPaths); 698de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit"; 699de629b6eSShawn McCarney }; 700de629b6eSShawn McCarney 701de629b6eSShawn McCarney // Query mapper for all DBus object paths that implement ObjectManager 702de629b6eSShawn McCarney crow::connections::systemBus->async_method_call( 703de629b6eSShawn McCarney std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 704de629b6eSShawn McCarney "/xyz/openbmc_project/object_mapper", 705271584abSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces); 706de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit"; 707de629b6eSShawn McCarney } 708de629b6eSShawn McCarney 709de629b6eSShawn McCarney /** 710adc4f0dbSShawn McCarney * @brief Returns the Redfish State value for the specified inventory item. 711adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with a sensor. 712adc4f0dbSShawn McCarney * @return State value for inventory item. 71334dd179eSJames Feist */ 71423a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem) 715adc4f0dbSShawn McCarney { 716adc4f0dbSShawn McCarney if ((inventoryItem != nullptr) && !(inventoryItem->isPresent)) 717adc4f0dbSShawn McCarney { 718adc4f0dbSShawn McCarney return "Absent"; 719adc4f0dbSShawn McCarney } 72034dd179eSJames Feist 721adc4f0dbSShawn McCarney return "Enabled"; 722adc4f0dbSShawn McCarney } 723adc4f0dbSShawn McCarney 724adc4f0dbSShawn McCarney /** 725adc4f0dbSShawn McCarney * @brief Returns the Redfish Health value for the specified sensor. 726adc4f0dbSShawn McCarney * @param sensorJson Sensor JSON object. 727adc4f0dbSShawn McCarney * @param interfacesDict Map of all sensor interfaces. 728adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with the sensor. Will 729adc4f0dbSShawn McCarney * be nullptr if no associated inventory item was found. 730adc4f0dbSShawn McCarney * @return Health value for sensor. 731adc4f0dbSShawn McCarney */ 732711ac7a9SEd Tanous inline std::string 733711ac7a9SEd Tanous getHealth(nlohmann::json& sensorJson, 734711ac7a9SEd Tanous const dbus::utility::DBusInteracesMap& interfacesDict, 735adc4f0dbSShawn McCarney const InventoryItem* inventoryItem) 73634dd179eSJames Feist { 737adc4f0dbSShawn McCarney // Get current health value (if any) in the sensor JSON object. Some JSON 738adc4f0dbSShawn McCarney // objects contain multiple sensors (such as PowerSupplies). We want to set 739adc4f0dbSShawn McCarney // the overall health to be the most severe of any of the sensors. 740adc4f0dbSShawn McCarney std::string currentHealth; 741adc4f0dbSShawn McCarney auto statusIt = sensorJson.find("Status"); 742adc4f0dbSShawn McCarney if (statusIt != sensorJson.end()) 743adc4f0dbSShawn McCarney { 744adc4f0dbSShawn McCarney auto healthIt = statusIt->find("Health"); 745adc4f0dbSShawn McCarney if (healthIt != statusIt->end()) 746adc4f0dbSShawn McCarney { 747adc4f0dbSShawn McCarney std::string* health = healthIt->get_ptr<std::string*>(); 748adc4f0dbSShawn McCarney if (health != nullptr) 749adc4f0dbSShawn McCarney { 750adc4f0dbSShawn McCarney currentHealth = *health; 751adc4f0dbSShawn McCarney } 752adc4f0dbSShawn McCarney } 753adc4f0dbSShawn McCarney } 754adc4f0dbSShawn McCarney 755adc4f0dbSShawn McCarney // If current health in JSON object is already Critical, return that. This 756adc4f0dbSShawn McCarney // should override the sensor health, which might be less severe. 757adc4f0dbSShawn McCarney if (currentHealth == "Critical") 758adc4f0dbSShawn McCarney { 759adc4f0dbSShawn McCarney return "Critical"; 760adc4f0dbSShawn McCarney } 761adc4f0dbSShawn McCarney 762adc4f0dbSShawn McCarney // Check if sensor has critical threshold alarm 763711ac7a9SEd Tanous 764711ac7a9SEd Tanous for (auto& [interface, values] : interfacesDict) 76534dd179eSJames Feist { 766711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical") 76734dd179eSJames Feist { 768711ac7a9SEd Tanous for (auto& [valueName, value] : values) 769711ac7a9SEd Tanous { 770711ac7a9SEd Tanous if (valueName == "CriticalAlarmHigh" || 771711ac7a9SEd Tanous valueName == "CriticalAlarmLow") 772711ac7a9SEd Tanous { 773711ac7a9SEd Tanous const bool* asserted = std::get_if<bool>(&value); 77434dd179eSJames Feist if (asserted == nullptr) 77534dd179eSJames Feist { 77634dd179eSJames Feist BMCWEB_LOG_ERROR << "Illegal sensor threshold"; 77734dd179eSJames Feist } 77834dd179eSJames Feist else if (*asserted) 77934dd179eSJames Feist { 78034dd179eSJames Feist return "Critical"; 78134dd179eSJames Feist } 78234dd179eSJames Feist } 78334dd179eSJames Feist } 78434dd179eSJames Feist } 78534dd179eSJames Feist } 78634dd179eSJames Feist 787adc4f0dbSShawn McCarney // Check if associated inventory item is not functional 788adc4f0dbSShawn McCarney if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional)) 789adc4f0dbSShawn McCarney { 790adc4f0dbSShawn McCarney return "Critical"; 791adc4f0dbSShawn McCarney } 792adc4f0dbSShawn McCarney 793adc4f0dbSShawn McCarney // If current health in JSON object is already Warning, return that. This 794adc4f0dbSShawn McCarney // should override the sensor status, which might be less severe. 795adc4f0dbSShawn McCarney if (currentHealth == "Warning") 796adc4f0dbSShawn McCarney { 797adc4f0dbSShawn McCarney return "Warning"; 798adc4f0dbSShawn McCarney } 799adc4f0dbSShawn McCarney 800adc4f0dbSShawn McCarney // Check if sensor has warning threshold alarm 801711ac7a9SEd Tanous for (auto& [interface, values] : interfacesDict) 80234dd179eSJames Feist { 803711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning") 80434dd179eSJames Feist { 805711ac7a9SEd Tanous for (auto& [valueName, value] : values) 806711ac7a9SEd Tanous { 807711ac7a9SEd Tanous if (valueName == "WarningAlarmHigh" || 808711ac7a9SEd Tanous valueName == "WarningAlarmLow") 809711ac7a9SEd Tanous { 810711ac7a9SEd Tanous const bool* asserted = std::get_if<bool>(&value); 81134dd179eSJames Feist if (asserted == nullptr) 81234dd179eSJames Feist { 81334dd179eSJames Feist BMCWEB_LOG_ERROR << "Illegal sensor threshold"; 81434dd179eSJames Feist } 81534dd179eSJames Feist else if (*asserted) 81634dd179eSJames Feist { 817ebe4d91eSEd Tanous return "Warning"; 81834dd179eSJames Feist } 81934dd179eSJames Feist } 82034dd179eSJames Feist } 82134dd179eSJames Feist } 82234dd179eSJames Feist } 823adc4f0dbSShawn McCarney 82434dd179eSJames Feist return "OK"; 82534dd179eSJames Feist } 82634dd179eSJames Feist 82723a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson, 828d500549bSAnthony Wilson const InventoryItem* inventoryItem) 829d500549bSAnthony Wilson { 830d500549bSAnthony Wilson if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty()) 831d500549bSAnthony Wilson { 832d500549bSAnthony Wilson switch (inventoryItem->ledState) 833d500549bSAnthony Wilson { 834d500549bSAnthony Wilson case LedState::OFF: 835d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Off"; 836d500549bSAnthony Wilson break; 837d500549bSAnthony Wilson case LedState::ON: 838d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Lit"; 839d500549bSAnthony Wilson break; 840d500549bSAnthony Wilson case LedState::BLINK: 841d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Blinking"; 842d500549bSAnthony Wilson break; 84323a21a1cSEd Tanous case LedState::UNKNOWN: 844d500549bSAnthony Wilson break; 845d500549bSAnthony Wilson } 846d500549bSAnthony Wilson } 847d500549bSAnthony Wilson } 848d500549bSAnthony Wilson 84934dd179eSJames Feist /** 85008777fb0SLewanczyk, Dawid * @brief Builds a json sensor representation of a sensor. 85108777fb0SLewanczyk, Dawid * @param sensorName The name of the sensor to be built 852274fad5aSGunnar Mills * @param sensorType The type (temperature, fan_tach, etc) of the sensor to 85308777fb0SLewanczyk, Dawid * build 854a0ec28b6SAdrian Ambrożewicz * @param sensorsAsyncResp Sensor metadata 85508777fb0SLewanczyk, Dawid * @param interfacesDict A dictionary of the interfaces and properties of said 85608777fb0SLewanczyk, Dawid * interfaces to be built from 85708777fb0SLewanczyk, Dawid * @param sensor_json The json object to fill 858adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with the sensor. Will 859adc4f0dbSShawn McCarney * be nullptr if no associated inventory item was found. 86008777fb0SLewanczyk, Dawid */ 86123a21a1cSEd Tanous inline void objectInterfacesToJson( 86208777fb0SLewanczyk, Dawid const std::string& sensorName, const std::string& sensorType, 863b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 864711ac7a9SEd Tanous const dbus::utility::DBusInteracesMap& interfacesDict, 86581ce609eSEd Tanous nlohmann::json& sensorJson, InventoryItem* inventoryItem) 8661abe55efSEd Tanous { 86708777fb0SLewanczyk, Dawid // Assume values exist as is (10^0 == 1) if no scale exists 86808777fb0SLewanczyk, Dawid int64_t scaleMultiplier = 0; 869711ac7a9SEd Tanous for (auto& [interface, values] : interfacesDict) 8701abe55efSEd Tanous { 871711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Sensor.Value") 872711ac7a9SEd Tanous { 873711ac7a9SEd Tanous for (auto& [valueName, value] : values) 874711ac7a9SEd Tanous { 875711ac7a9SEd Tanous if (valueName == "Scale") 876711ac7a9SEd Tanous { 877711ac7a9SEd Tanous const int64_t* int64Value = std::get_if<int64_t>(&value); 8781abe55efSEd Tanous if (int64Value != nullptr) 8791abe55efSEd Tanous { 88008777fb0SLewanczyk, Dawid scaleMultiplier = *int64Value; 88108777fb0SLewanczyk, Dawid } 88208777fb0SLewanczyk, Dawid } 883711ac7a9SEd Tanous } 884711ac7a9SEd Tanous } 885711ac7a9SEd Tanous } 88608777fb0SLewanczyk, Dawid 887a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors) 888adc4f0dbSShawn McCarney { 88995a3ecadSAnthony Wilson // For sensors in SensorCollection we set Id instead of MemberId, 89095a3ecadSAnthony Wilson // including power sensors. 89181ce609eSEd Tanous sensorJson["Id"] = sensorName; 89281ce609eSEd Tanous sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " "); 89395a3ecadSAnthony Wilson } 89495a3ecadSAnthony Wilson else if (sensorType != "power") 89595a3ecadSAnthony Wilson { 89695a3ecadSAnthony Wilson // Set MemberId and Name for non-power sensors. For PowerSupplies and 89795a3ecadSAnthony Wilson // PowerControl, those properties have more general values because 89895a3ecadSAnthony Wilson // multiple sensors can be stored in the same JSON object. 89981ce609eSEd Tanous sensorJson["MemberId"] = sensorName; 90081ce609eSEd Tanous sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " "); 901adc4f0dbSShawn McCarney } 902e742b6ccSEd Tanous 90381ce609eSEd Tanous sensorJson["Status"]["State"] = getState(inventoryItem); 90481ce609eSEd Tanous sensorJson["Status"]["Health"] = 90581ce609eSEd Tanous getHealth(sensorJson, interfacesDict, inventoryItem); 90608777fb0SLewanczyk, Dawid 90708777fb0SLewanczyk, Dawid // Parameter to set to override the type we get from dbus, and force it to 90808777fb0SLewanczyk, Dawid // int, regardless of what is available. This is used for schemas like fan, 90908777fb0SLewanczyk, Dawid // that require integers, not floats. 91008777fb0SLewanczyk, Dawid bool forceToInt = false; 91108777fb0SLewanczyk, Dawid 9123929aca1SAnthony Wilson nlohmann::json::json_pointer unit("/Reading"); 913a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors) 91495a3ecadSAnthony Wilson { 91581ce609eSEd Tanous sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor"; 916c2bf7f99SWludzik, Jozef 917c2bf7f99SWludzik, Jozef const std::string& readingType = sensors::toReadingType(sensorType); 918c2bf7f99SWludzik, Jozef if (readingType.empty()) 91995a3ecadSAnthony Wilson { 920c2bf7f99SWludzik, Jozef BMCWEB_LOG_ERROR << "Redfish cannot map reading type for " 921c2bf7f99SWludzik, Jozef << sensorType; 92295a3ecadSAnthony Wilson } 923c2bf7f99SWludzik, Jozef else 92495a3ecadSAnthony Wilson { 925c2bf7f99SWludzik, Jozef sensorJson["ReadingType"] = readingType; 92695a3ecadSAnthony Wilson } 927c2bf7f99SWludzik, Jozef 928c2bf7f99SWludzik, Jozef const std::string& readingUnits = sensors::toReadingUnits(sensorType); 929c2bf7f99SWludzik, Jozef if (readingUnits.empty()) 930f8ede15eSAdrian Ambrożewicz { 931c2bf7f99SWludzik, Jozef BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for " 932c2bf7f99SWludzik, Jozef << sensorType; 933c2bf7f99SWludzik, Jozef } 934c2bf7f99SWludzik, Jozef else 935c2bf7f99SWludzik, Jozef { 936c2bf7f99SWludzik, Jozef sensorJson["ReadingUnits"] = readingUnits; 937f8ede15eSAdrian Ambrożewicz } 93895a3ecadSAnthony Wilson } 93995a3ecadSAnthony Wilson else if (sensorType == "temperature") 9401abe55efSEd Tanous { 9413929aca1SAnthony Wilson unit = "/ReadingCelsius"_json_pointer; 94281ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature"; 94308777fb0SLewanczyk, Dawid // TODO(ed) Documentation says that path should be type fan_tach, 94408777fb0SLewanczyk, Dawid // implementation seems to implement fan 9451abe55efSEd Tanous } 9461abe55efSEd Tanous else if (sensorType == "fan" || sensorType == "fan_tach") 9471abe55efSEd Tanous { 9483929aca1SAnthony Wilson unit = "/Reading"_json_pointer; 94981ce609eSEd Tanous sensorJson["ReadingUnits"] = "RPM"; 95081ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan"; 95181ce609eSEd Tanous setLedState(sensorJson, inventoryItem); 95208777fb0SLewanczyk, Dawid forceToInt = true; 9531abe55efSEd Tanous } 9546f6d0d32SEd Tanous else if (sensorType == "fan_pwm") 9556f6d0d32SEd Tanous { 9563929aca1SAnthony Wilson unit = "/Reading"_json_pointer; 95781ce609eSEd Tanous sensorJson["ReadingUnits"] = "Percent"; 95881ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan"; 95981ce609eSEd Tanous setLedState(sensorJson, inventoryItem); 9606f6d0d32SEd Tanous forceToInt = true; 9616f6d0d32SEd Tanous } 9621abe55efSEd Tanous else if (sensorType == "voltage") 9631abe55efSEd Tanous { 9643929aca1SAnthony Wilson unit = "/ReadingVolts"_json_pointer; 96581ce609eSEd Tanous sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage"; 9661abe55efSEd Tanous } 9672474adfaSEd Tanous else if (sensorType == "power") 9682474adfaSEd Tanous { 96949c53ac9SJohnathan Mantey std::string sensorNameLower = 97049c53ac9SJohnathan Mantey boost::algorithm::to_lower_copy(sensorName); 97149c53ac9SJohnathan Mantey 972028f7ebcSEddie James if (!sensorName.compare("total_power")) 973028f7ebcSEddie James { 97481ce609eSEd Tanous sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl"; 9757ab06f49SGunnar Mills // Put multiple "sensors" into a single PowerControl, so have 9767ab06f49SGunnar Mills // generic names for MemberId and Name. Follows Redfish mockup. 97781ce609eSEd Tanous sensorJson["MemberId"] = "0"; 97881ce609eSEd Tanous sensorJson["Name"] = "Chassis Power Control"; 9793929aca1SAnthony Wilson unit = "/PowerConsumedWatts"_json_pointer; 980028f7ebcSEddie James } 981028f7ebcSEddie James else if (sensorNameLower.find("input") != std::string::npos) 98249c53ac9SJohnathan Mantey { 9833929aca1SAnthony Wilson unit = "/PowerInputWatts"_json_pointer; 98449c53ac9SJohnathan Mantey } 98549c53ac9SJohnathan Mantey else 98649c53ac9SJohnathan Mantey { 9873929aca1SAnthony Wilson unit = "/PowerOutputWatts"_json_pointer; 98849c53ac9SJohnathan Mantey } 9892474adfaSEd Tanous } 9901abe55efSEd Tanous else 9911abe55efSEd Tanous { 99255c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName; 99308777fb0SLewanczyk, Dawid return; 99408777fb0SLewanczyk, Dawid } 99508777fb0SLewanczyk, Dawid // Map of dbus interface name, dbus property name and redfish property_name 9963929aca1SAnthony Wilson std::vector< 9973929aca1SAnthony Wilson std::tuple<const char*, const char*, nlohmann::json::json_pointer>> 9983929aca1SAnthony Wilson properties; 99908777fb0SLewanczyk, Dawid properties.reserve(7); 100008777fb0SLewanczyk, Dawid 100108777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit); 1002de629b6eSShawn McCarney 1003a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors) 10043929aca1SAnthony Wilson { 10053929aca1SAnthony Wilson properties.emplace_back( 10063929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh", 10073929aca1SAnthony Wilson "/Thresholds/UpperCaution/Reading"_json_pointer); 10083929aca1SAnthony Wilson properties.emplace_back( 10093929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow", 10103929aca1SAnthony Wilson "/Thresholds/LowerCaution/Reading"_json_pointer); 10113929aca1SAnthony Wilson properties.emplace_back( 10123929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh", 10133929aca1SAnthony Wilson "/Thresholds/UpperCritical/Reading"_json_pointer); 10143929aca1SAnthony Wilson properties.emplace_back( 10153929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow", 10163929aca1SAnthony Wilson "/Thresholds/LowerCritical/Reading"_json_pointer); 10173929aca1SAnthony Wilson } 10183929aca1SAnthony Wilson else if (sensorType != "power") 1019de629b6eSShawn McCarney { 102008777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 10213929aca1SAnthony Wilson "WarningHigh", 10223929aca1SAnthony Wilson "/UpperThresholdNonCritical"_json_pointer); 102308777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 10243929aca1SAnthony Wilson "WarningLow", 10253929aca1SAnthony Wilson "/LowerThresholdNonCritical"_json_pointer); 102608777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 10273929aca1SAnthony Wilson "CriticalHigh", 10283929aca1SAnthony Wilson "/UpperThresholdCritical"_json_pointer); 102908777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 10303929aca1SAnthony Wilson "CriticalLow", 10313929aca1SAnthony Wilson "/LowerThresholdCritical"_json_pointer); 1032de629b6eSShawn McCarney } 103308777fb0SLewanczyk, Dawid 10342474adfaSEd Tanous // TODO Need to get UpperThresholdFatal and LowerThresholdFatal 10352474adfaSEd Tanous 1036a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors) 103795a3ecadSAnthony Wilson { 103895a3ecadSAnthony Wilson properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 10393929aca1SAnthony Wilson "/ReadingRangeMin"_json_pointer); 104095a3ecadSAnthony Wilson properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 10413929aca1SAnthony Wilson "/ReadingRangeMax"_json_pointer); 104295a3ecadSAnthony Wilson } 104395a3ecadSAnthony Wilson else if (sensorType == "temperature") 10441abe55efSEd Tanous { 104508777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 10463929aca1SAnthony Wilson "/MinReadingRangeTemp"_json_pointer); 104708777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 10483929aca1SAnthony Wilson "/MaxReadingRangeTemp"_json_pointer); 10491abe55efSEd Tanous } 1050adc4f0dbSShawn McCarney else if (sensorType != "power") 10511abe55efSEd Tanous { 105208777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 10533929aca1SAnthony Wilson "/MinReadingRange"_json_pointer); 105408777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 10553929aca1SAnthony Wilson "/MaxReadingRange"_json_pointer); 105608777fb0SLewanczyk, Dawid } 105708777fb0SLewanczyk, Dawid 10583929aca1SAnthony Wilson for (const std::tuple<const char*, const char*, 10593929aca1SAnthony Wilson nlohmann::json::json_pointer>& p : properties) 10601abe55efSEd Tanous { 1061711ac7a9SEd Tanous for (auto& [interface, values] : interfacesDict) 10621abe55efSEd Tanous { 1063711ac7a9SEd Tanous if (interface != std::get<0>(p)) 10641abe55efSEd Tanous { 1065711ac7a9SEd Tanous continue; 1066711ac7a9SEd Tanous } 1067711ac7a9SEd Tanous for (auto& [valueName, valueVariant] : values) 1068711ac7a9SEd Tanous { 1069711ac7a9SEd Tanous if (valueName != std::get<1>(p)) 1070711ac7a9SEd Tanous { 1071711ac7a9SEd Tanous continue; 1072711ac7a9SEd Tanous } 10733929aca1SAnthony Wilson 10743929aca1SAnthony Wilson // The property we want to set may be nested json, so use 10753929aca1SAnthony Wilson // a json_pointer for easy indexing into the json structure. 10763929aca1SAnthony Wilson const nlohmann::json::json_pointer& key = std::get<2>(p); 10773929aca1SAnthony Wilson 107808777fb0SLewanczyk, Dawid // Attempt to pull the int64 directly 1079abf2add6SEd Tanous const int64_t* int64Value = std::get_if<int64_t>(&valueVariant); 108008777fb0SLewanczyk, Dawid 1081abf2add6SEd Tanous const double* doubleValue = std::get_if<double>(&valueVariant); 1082028f7ebcSEddie James const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant); 10836f6d0d32SEd Tanous double temp = 0.0; 10846f6d0d32SEd Tanous if (int64Value != nullptr) 10851abe55efSEd Tanous { 1086271584abSEd Tanous temp = static_cast<double>(*int64Value); 10876f6d0d32SEd Tanous } 10886f6d0d32SEd Tanous else if (doubleValue != nullptr) 10891abe55efSEd Tanous { 10906f6d0d32SEd Tanous temp = *doubleValue; 10911abe55efSEd Tanous } 1092028f7ebcSEddie James else if (uValue != nullptr) 1093028f7ebcSEddie James { 1094028f7ebcSEddie James temp = *uValue; 1095028f7ebcSEddie James } 10961abe55efSEd Tanous else 10971abe55efSEd Tanous { 10986f6d0d32SEd Tanous BMCWEB_LOG_ERROR 10996f6d0d32SEd Tanous << "Got value interface that wasn't int or double"; 11006f6d0d32SEd Tanous continue; 110108777fb0SLewanczyk, Dawid } 11026f6d0d32SEd Tanous temp = temp * std::pow(10, scaleMultiplier); 11036f6d0d32SEd Tanous if (forceToInt) 11046f6d0d32SEd Tanous { 110581ce609eSEd Tanous sensorJson[key] = static_cast<int64_t>(temp); 11066f6d0d32SEd Tanous } 11076f6d0d32SEd Tanous else 11086f6d0d32SEd Tanous { 110981ce609eSEd Tanous sensorJson[key] = temp; 111008777fb0SLewanczyk, Dawid } 111108777fb0SLewanczyk, Dawid } 111208777fb0SLewanczyk, Dawid } 111308777fb0SLewanczyk, Dawid } 1114a0ec28b6SAdrian Ambrożewicz 111581ce609eSEd Tanous sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(), 1116a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/" + sensorType + 1117a0ec28b6SAdrian Ambrożewicz "/" + sensorName); 1118a0ec28b6SAdrian Ambrożewicz 111955c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Added sensor " << sensorName; 112008777fb0SLewanczyk, Dawid } 112108777fb0SLewanczyk, Dawid 1122b5a76932SEd Tanous inline void populateFanRedundancy( 1123b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 11248bd25ccdSJames Feist { 11258bd25ccdSJames Feist crow::connections::systemBus->async_method_call( 11268bd25ccdSJames Feist [sensorsAsyncResp](const boost::system::error_code ec, 11278bd25ccdSJames Feist const GetSubTreeType& resp) { 11288bd25ccdSJames Feist if (ec) 11298bd25ccdSJames Feist { 11308bd25ccdSJames Feist return; // don't have to have this interface 11318bd25ccdSJames Feist } 1132e278c18fSEd Tanous for (const std::pair<std::string, 1133e278c18fSEd Tanous std::vector<std::pair< 1134e278c18fSEd Tanous std::string, std::vector<std::string>>>>& 1135e278c18fSEd Tanous pathPair : resp) 11368bd25ccdSJames Feist { 1137e278c18fSEd Tanous const std::string& path = pathPair.first; 1138e278c18fSEd Tanous const std::vector< 1139e278c18fSEd Tanous std::pair<std::string, std::vector<std::string>>>& objDict = 1140e278c18fSEd Tanous pathPair.second; 11418bd25ccdSJames Feist if (objDict.empty()) 11428bd25ccdSJames Feist { 11438bd25ccdSJames Feist continue; // this should be impossible 11448bd25ccdSJames Feist } 11458bd25ccdSJames Feist 11468bd25ccdSJames Feist const std::string& owner = objDict.begin()->first; 11471e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::vector<std::string>>( 11481e1e598dSJonathan Doman *crow::connections::systemBus, 11491e1e598dSJonathan Doman "xyz.openbmc_project.ObjectMapper", path + "/chassis", 11501e1e598dSJonathan Doman "xyz.openbmc_project.Association", "endpoints", 11511e1e598dSJonathan Doman [path, owner, sensorsAsyncResp]( 11521e1e598dSJonathan Doman const boost::system::error_code e, 11531e1e598dSJonathan Doman const std::vector<std::string>& endpoints) { 1154271584abSEd Tanous if (e) 11558bd25ccdSJames Feist { 11568bd25ccdSJames Feist return; // if they don't have an association we 11578bd25ccdSJames Feist // can't tell what chassis is 11588bd25ccdSJames Feist } 11598bd25ccdSJames Feist auto found = std::find_if( 11601e1e598dSJonathan Doman endpoints.begin(), endpoints.end(), 11618bd25ccdSJames Feist [sensorsAsyncResp](const std::string& entry) { 11628bd25ccdSJames Feist return entry.find( 11638bd25ccdSJames Feist sensorsAsyncResp->chassisId) != 11648bd25ccdSJames Feist std::string::npos; 11658bd25ccdSJames Feist }); 11668bd25ccdSJames Feist 11671e1e598dSJonathan Doman if (found == endpoints.end()) 11688bd25ccdSJames Feist { 11698bd25ccdSJames Feist return; 11708bd25ccdSJames Feist } 11718bd25ccdSJames Feist crow::connections::systemBus->async_method_call( 11728bd25ccdSJames Feist [path, sensorsAsyncResp]( 1173271584abSEd Tanous const boost::system::error_code& err, 11748bd25ccdSJames Feist const boost::container::flat_map< 11758bd25ccdSJames Feist std::string, 1176168e20c1SEd Tanous dbus::utility::DbusVariantType>& ret) { 1177271584abSEd Tanous if (err) 11788bd25ccdSJames Feist { 11798bd25ccdSJames Feist return; // don't have to have this 11808bd25ccdSJames Feist // interface 11818bd25ccdSJames Feist } 11828bd25ccdSJames Feist auto findFailures = ret.find("AllowedFailures"); 11838bd25ccdSJames Feist auto findCollection = ret.find("Collection"); 11848bd25ccdSJames Feist auto findStatus = ret.find("Status"); 11858bd25ccdSJames Feist 11868bd25ccdSJames Feist if (findFailures == ret.end() || 11878bd25ccdSJames Feist findCollection == ret.end() || 11888bd25ccdSJames Feist findStatus == ret.end()) 11898bd25ccdSJames Feist { 11908bd25ccdSJames Feist BMCWEB_LOG_ERROR 11918bd25ccdSJames Feist << "Invalid redundancy interface"; 11928bd25ccdSJames Feist messages::internalError( 11938d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 11948bd25ccdSJames Feist return; 11958bd25ccdSJames Feist } 11968bd25ccdSJames Feist 11978bd25ccdSJames Feist auto allowedFailures = std::get_if<uint8_t>( 11988bd25ccdSJames Feist &(findFailures->second)); 11998bd25ccdSJames Feist auto collection = 12008bd25ccdSJames Feist std::get_if<std::vector<std::string>>( 12018bd25ccdSJames Feist &(findCollection->second)); 12028bd25ccdSJames Feist auto status = std::get_if<std::string>( 12038bd25ccdSJames Feist &(findStatus->second)); 12048bd25ccdSJames Feist 12058bd25ccdSJames Feist if (allowedFailures == nullptr || 12068bd25ccdSJames Feist collection == nullptr || status == nullptr) 12078bd25ccdSJames Feist { 12088bd25ccdSJames Feist 12098bd25ccdSJames Feist BMCWEB_LOG_ERROR 12100fda0f12SGeorge Liu << "Invalid redundancy interface types"; 12118bd25ccdSJames Feist messages::internalError( 12128d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 12138bd25ccdSJames Feist return; 12148bd25ccdSJames Feist } 121528aa8de5SGeorge Liu sdbusplus::message::object_path objectPath( 121628aa8de5SGeorge Liu path); 121728aa8de5SGeorge Liu std::string name = objectPath.filename(); 121828aa8de5SGeorge Liu if (name.empty()) 12198bd25ccdSJames Feist { 12208bd25ccdSJames Feist // this should be impossible 12218bd25ccdSJames Feist messages::internalError( 12228d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 12238bd25ccdSJames Feist return; 12248bd25ccdSJames Feist } 12258bd25ccdSJames Feist std::replace(name.begin(), name.end(), '_', 12268bd25ccdSJames Feist ' '); 12278bd25ccdSJames Feist 12288bd25ccdSJames Feist std::string health; 12298bd25ccdSJames Feist 12308bd25ccdSJames Feist if (boost::ends_with(*status, "Full")) 12318bd25ccdSJames Feist { 12328bd25ccdSJames Feist health = "OK"; 12338bd25ccdSJames Feist } 12348bd25ccdSJames Feist else if (boost::ends_with(*status, "Degraded")) 12358bd25ccdSJames Feist { 12368bd25ccdSJames Feist health = "Warning"; 12378bd25ccdSJames Feist } 12388bd25ccdSJames Feist else 12398bd25ccdSJames Feist { 12408bd25ccdSJames Feist health = "Critical"; 12418bd25ccdSJames Feist } 12428bd25ccdSJames Feist std::vector<nlohmann::json> redfishCollection; 12438bd25ccdSJames Feist const auto& fanRedfish = 12448d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res 12458d1b46d7Szhanghch05 .jsonValue["Fans"]; 12468bd25ccdSJames Feist for (const std::string& item : *collection) 12478bd25ccdSJames Feist { 124828aa8de5SGeorge Liu sdbusplus::message::object_path path(item); 124928aa8de5SGeorge Liu std::string itemName = path.filename(); 125028aa8de5SGeorge Liu if (itemName.empty()) 125128aa8de5SGeorge Liu { 125228aa8de5SGeorge Liu continue; 125328aa8de5SGeorge Liu } 12548bd25ccdSJames Feist /* 12558bd25ccdSJames Feist todo(ed): merge patch that fixes the names 12568bd25ccdSJames Feist std::replace(itemName.begin(), 12578bd25ccdSJames Feist itemName.end(), '_', ' ');*/ 12588bd25ccdSJames Feist auto schemaItem = std::find_if( 12598bd25ccdSJames Feist fanRedfish.begin(), fanRedfish.end(), 12608bd25ccdSJames Feist [itemName](const nlohmann::json& fan) { 12618bd25ccdSJames Feist return fan["MemberId"] == itemName; 12628bd25ccdSJames Feist }); 12638bd25ccdSJames Feist if (schemaItem != fanRedfish.end()) 12648bd25ccdSJames Feist { 12658bd25ccdSJames Feist redfishCollection.push_back( 12668bd25ccdSJames Feist {{"@odata.id", 12678bd25ccdSJames Feist (*schemaItem)["@odata.id"]}}); 12688bd25ccdSJames Feist } 12698bd25ccdSJames Feist else 12708bd25ccdSJames Feist { 12718bd25ccdSJames Feist BMCWEB_LOG_ERROR 12728bd25ccdSJames Feist << "failed to find fan in schema"; 12738bd25ccdSJames Feist messages::internalError( 12748d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 12758bd25ccdSJames Feist return; 12768bd25ccdSJames Feist } 12778bd25ccdSJames Feist } 12788bd25ccdSJames Feist 12793e9e72ebSKuiying Wang size_t minNumNeeded = 1280*26f6976fSEd Tanous collection->empty() 1281*26f6976fSEd Tanous ? 0 1282*26f6976fSEd Tanous : collection->size() - *allowedFailures; 1283271584abSEd Tanous nlohmann::json& jResp = 12848d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res 12858bd25ccdSJames Feist .jsonValue["Redundancy"]; 1286271584abSEd Tanous jResp.push_back( 12878bd25ccdSJames Feist {{"@odata.id", 1288717794d5SAppaRao Puli "/redfish/v1/Chassis/" + 12898bd25ccdSJames Feist sensorsAsyncResp->chassisId + "/" + 12908bd25ccdSJames Feist sensorsAsyncResp->chassisSubNode + 12918bd25ccdSJames Feist "#/Redundancy/" + 1292271584abSEd Tanous std::to_string(jResp.size())}, 12938bd25ccdSJames Feist {"@odata.type", 12948bd25ccdSJames Feist "#Redundancy.v1_3_2.Redundancy"}, 12953e9e72ebSKuiying Wang {"MinNumNeeded", minNumNeeded}, 12968bd25ccdSJames Feist {"MemberId", name}, 12978bd25ccdSJames Feist {"Mode", "N+m"}, 12988bd25ccdSJames Feist {"Name", name}, 12998bd25ccdSJames Feist {"RedundancySet", redfishCollection}, 13008bd25ccdSJames Feist {"Status", 13018bd25ccdSJames Feist {{"Health", health}, 13028bd25ccdSJames Feist {"State", "Enabled"}}}}); 13038bd25ccdSJames Feist }, 13048bd25ccdSJames Feist owner, path, "org.freedesktop.DBus.Properties", 13058bd25ccdSJames Feist "GetAll", 13068bd25ccdSJames Feist "xyz.openbmc_project.Control.FanRedundancy"); 13071e1e598dSJonathan Doman }); 13088bd25ccdSJames Feist } 13098bd25ccdSJames Feist }, 13108bd25ccdSJames Feist "xyz.openbmc_project.ObjectMapper", 13118bd25ccdSJames Feist "/xyz/openbmc_project/object_mapper", 13128bd25ccdSJames Feist "xyz.openbmc_project.ObjectMapper", "GetSubTree", 13138bd25ccdSJames Feist "/xyz/openbmc_project/control", 2, 13148bd25ccdSJames Feist std::array<const char*, 1>{ 13158bd25ccdSJames Feist "xyz.openbmc_project.Control.FanRedundancy"}); 13168bd25ccdSJames Feist } 13178bd25ccdSJames Feist 1318b5a76932SEd Tanous inline void 131981ce609eSEd Tanous sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 132049c53ac9SJohnathan Mantey { 13218d1b46d7Szhanghch05 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue; 132249c53ac9SJohnathan Mantey std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"}; 132381ce609eSEd Tanous if (sensorsAsyncResp->chassisSubNode == sensors::node::power) 132449c53ac9SJohnathan Mantey { 132549c53ac9SJohnathan Mantey sensorHeaders = {"Voltages", "PowerSupplies"}; 132649c53ac9SJohnathan Mantey } 132749c53ac9SJohnathan Mantey for (const std::string& sensorGroup : sensorHeaders) 132849c53ac9SJohnathan Mantey { 132949c53ac9SJohnathan Mantey nlohmann::json::iterator entry = response.find(sensorGroup); 133049c53ac9SJohnathan Mantey if (entry != response.end()) 133149c53ac9SJohnathan Mantey { 133249c53ac9SJohnathan Mantey std::sort(entry->begin(), entry->end(), 133349c53ac9SJohnathan Mantey [](nlohmann::json& c1, nlohmann::json& c2) { 133449c53ac9SJohnathan Mantey return c1["Name"] < c2["Name"]; 133549c53ac9SJohnathan Mantey }); 133649c53ac9SJohnathan Mantey 133749c53ac9SJohnathan Mantey // add the index counts to the end of each entry 133849c53ac9SJohnathan Mantey size_t count = 0; 133949c53ac9SJohnathan Mantey for (nlohmann::json& sensorJson : *entry) 134049c53ac9SJohnathan Mantey { 134149c53ac9SJohnathan Mantey nlohmann::json::iterator odata = sensorJson.find("@odata.id"); 134249c53ac9SJohnathan Mantey if (odata == sensorJson.end()) 134349c53ac9SJohnathan Mantey { 134449c53ac9SJohnathan Mantey continue; 134549c53ac9SJohnathan Mantey } 134649c53ac9SJohnathan Mantey std::string* value = odata->get_ptr<std::string*>(); 134749c53ac9SJohnathan Mantey if (value != nullptr) 134849c53ac9SJohnathan Mantey { 134949c53ac9SJohnathan Mantey *value += std::to_string(count); 135049c53ac9SJohnathan Mantey count++; 135181ce609eSEd Tanous sensorsAsyncResp->updateUri(sensorJson["Name"], *value); 135249c53ac9SJohnathan Mantey } 135349c53ac9SJohnathan Mantey } 135449c53ac9SJohnathan Mantey } 135549c53ac9SJohnathan Mantey } 135649c53ac9SJohnathan Mantey } 135749c53ac9SJohnathan Mantey 135808777fb0SLewanczyk, Dawid /** 1359adc4f0dbSShawn McCarney * @brief Finds the inventory item with the specified object path. 1360adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1361adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 1362adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 13638fb49dd6SShawn McCarney */ 136423a21a1cSEd Tanous inline InventoryItem* findInventoryItem( 1365b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1366adc4f0dbSShawn McCarney const std::string& invItemObjPath) 13678fb49dd6SShawn McCarney { 1368adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 13698fb49dd6SShawn McCarney { 1370adc4f0dbSShawn McCarney if (inventoryItem.objectPath == invItemObjPath) 13718fb49dd6SShawn McCarney { 1372adc4f0dbSShawn McCarney return &inventoryItem; 13738fb49dd6SShawn McCarney } 13748fb49dd6SShawn McCarney } 13758fb49dd6SShawn McCarney return nullptr; 13768fb49dd6SShawn McCarney } 13778fb49dd6SShawn McCarney 13788fb49dd6SShawn McCarney /** 1379adc4f0dbSShawn McCarney * @brief Finds the inventory item associated with the specified sensor. 1380adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1381adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor. 1382adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 13838fb49dd6SShawn McCarney */ 138423a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor( 1385b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1386adc4f0dbSShawn McCarney const std::string& sensorObjPath) 1387adc4f0dbSShawn McCarney { 1388adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 1389adc4f0dbSShawn McCarney { 1390adc4f0dbSShawn McCarney if (inventoryItem.sensors.count(sensorObjPath) > 0) 1391adc4f0dbSShawn McCarney { 1392adc4f0dbSShawn McCarney return &inventoryItem; 1393adc4f0dbSShawn McCarney } 1394adc4f0dbSShawn McCarney } 1395adc4f0dbSShawn McCarney return nullptr; 1396adc4f0dbSShawn McCarney } 1397adc4f0dbSShawn McCarney 1398adc4f0dbSShawn McCarney /** 1399d500549bSAnthony Wilson * @brief Finds the inventory item associated with the specified led path. 1400d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1401d500549bSAnthony Wilson * @param ledObjPath D-Bus object path of led. 1402d500549bSAnthony Wilson * @return Inventory item within vector, or nullptr if no match found. 1403d500549bSAnthony Wilson */ 1404d500549bSAnthony Wilson inline InventoryItem* 1405d500549bSAnthony Wilson findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems, 1406d500549bSAnthony Wilson const std::string& ledObjPath) 1407d500549bSAnthony Wilson { 1408d500549bSAnthony Wilson for (InventoryItem& inventoryItem : inventoryItems) 1409d500549bSAnthony Wilson { 1410d500549bSAnthony Wilson if (inventoryItem.ledObjectPath == ledObjPath) 1411d500549bSAnthony Wilson { 1412d500549bSAnthony Wilson return &inventoryItem; 1413d500549bSAnthony Wilson } 1414d500549bSAnthony Wilson } 1415d500549bSAnthony Wilson return nullptr; 1416d500549bSAnthony Wilson } 1417d500549bSAnthony Wilson 1418d500549bSAnthony Wilson /** 1419adc4f0dbSShawn McCarney * @brief Adds inventory item and associated sensor to specified vector. 1420adc4f0dbSShawn McCarney * 1421adc4f0dbSShawn McCarney * Adds a new InventoryItem to the vector if necessary. Searches for an 1422adc4f0dbSShawn McCarney * existing InventoryItem with the specified object path. If not found, one is 1423adc4f0dbSShawn McCarney * added to the vector. 1424adc4f0dbSShawn McCarney * 1425adc4f0dbSShawn McCarney * Next, the specified sensor is added to the set of sensors associated with the 1426adc4f0dbSShawn McCarney * InventoryItem. 1427adc4f0dbSShawn McCarney * 1428adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1429adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 1430adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor 1431adc4f0dbSShawn McCarney */ 1432b5a76932SEd Tanous inline void addInventoryItem( 1433b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1434b5a76932SEd Tanous const std::string& invItemObjPath, const std::string& sensorObjPath) 1435adc4f0dbSShawn McCarney { 1436adc4f0dbSShawn McCarney // Look for inventory item in vector 1437adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1438adc4f0dbSShawn McCarney findInventoryItem(inventoryItems, invItemObjPath); 1439adc4f0dbSShawn McCarney 1440adc4f0dbSShawn McCarney // If inventory item doesn't exist in vector, add it 1441adc4f0dbSShawn McCarney if (inventoryItem == nullptr) 1442adc4f0dbSShawn McCarney { 1443adc4f0dbSShawn McCarney inventoryItems->emplace_back(invItemObjPath); 1444adc4f0dbSShawn McCarney inventoryItem = &(inventoryItems->back()); 1445adc4f0dbSShawn McCarney } 1446adc4f0dbSShawn McCarney 1447adc4f0dbSShawn McCarney // Add sensor to set of sensors associated with inventory item 1448adc4f0dbSShawn McCarney inventoryItem->sensors.emplace(sensorObjPath); 1449adc4f0dbSShawn McCarney } 1450adc4f0dbSShawn McCarney 1451adc4f0dbSShawn McCarney /** 1452adc4f0dbSShawn McCarney * @brief Stores D-Bus data in the specified inventory item. 1453adc4f0dbSShawn McCarney * 1454adc4f0dbSShawn McCarney * Finds D-Bus data in the specified map of interfaces. Stores the data in the 1455adc4f0dbSShawn McCarney * specified InventoryItem. 1456adc4f0dbSShawn McCarney * 1457adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1458adc4f0dbSShawn McCarney * response. 1459adc4f0dbSShawn McCarney * 1460adc4f0dbSShawn McCarney * @param inventoryItem Inventory item where data will be stored. 1461adc4f0dbSShawn McCarney * @param interfacesDict Map containing D-Bus interfaces and their properties 1462adc4f0dbSShawn McCarney * for the specified inventory item. 1463adc4f0dbSShawn McCarney */ 146423a21a1cSEd Tanous inline void storeInventoryItemData( 1465adc4f0dbSShawn McCarney InventoryItem& inventoryItem, 1466711ac7a9SEd Tanous const dbus::utility::DBusInteracesMap& interfacesDict) 14678fb49dd6SShawn McCarney { 1468adc4f0dbSShawn McCarney // Get properties from Inventory.Item interface 1469711ac7a9SEd Tanous 1470711ac7a9SEd Tanous for (auto& [interface, values] : interfacesDict) 14718fb49dd6SShawn McCarney { 1472711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Item") 14738fb49dd6SShawn McCarney { 1474711ac7a9SEd Tanous for (auto& [name, dbusValue] : values) 1475711ac7a9SEd Tanous { 1476711ac7a9SEd Tanous if (name == "Present") 1477711ac7a9SEd Tanous { 1478711ac7a9SEd Tanous const bool* value = std::get_if<bool>(&dbusValue); 1479adc4f0dbSShawn McCarney if (value != nullptr) 14808fb49dd6SShawn McCarney { 1481adc4f0dbSShawn McCarney inventoryItem.isPresent = *value; 14828fb49dd6SShawn McCarney } 14838fb49dd6SShawn McCarney } 14848fb49dd6SShawn McCarney } 1485711ac7a9SEd Tanous } 1486adc4f0dbSShawn McCarney // Check if Inventory.Item.PowerSupply interface is present 1487711ac7a9SEd Tanous 1488711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply") 14898fb49dd6SShawn McCarney { 1490adc4f0dbSShawn McCarney inventoryItem.isPowerSupply = true; 14918fb49dd6SShawn McCarney } 1492adc4f0dbSShawn McCarney 1493adc4f0dbSShawn McCarney // Get properties from Inventory.Decorator.Asset interface 1494711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset") 1495adc4f0dbSShawn McCarney { 1496711ac7a9SEd Tanous for (auto& [name, dbusValue] : values) 1497711ac7a9SEd Tanous { 1498711ac7a9SEd Tanous if (name == "Manufacturer") 1499adc4f0dbSShawn McCarney { 1500adc4f0dbSShawn McCarney const std::string* value = 1501711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1502adc4f0dbSShawn McCarney if (value != nullptr) 1503adc4f0dbSShawn McCarney { 1504adc4f0dbSShawn McCarney inventoryItem.manufacturer = *value; 1505adc4f0dbSShawn McCarney } 1506adc4f0dbSShawn McCarney } 1507711ac7a9SEd Tanous if (name == "Model") 1508adc4f0dbSShawn McCarney { 1509adc4f0dbSShawn McCarney const std::string* value = 1510711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1511adc4f0dbSShawn McCarney if (value != nullptr) 1512adc4f0dbSShawn McCarney { 1513adc4f0dbSShawn McCarney inventoryItem.model = *value; 1514adc4f0dbSShawn McCarney } 1515adc4f0dbSShawn McCarney } 1516711ac7a9SEd Tanous if (name == "SerialNumber") 1517adc4f0dbSShawn McCarney { 1518adc4f0dbSShawn McCarney const std::string* value = 1519711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1520adc4f0dbSShawn McCarney if (value != nullptr) 1521adc4f0dbSShawn McCarney { 1522adc4f0dbSShawn McCarney inventoryItem.serialNumber = *value; 1523adc4f0dbSShawn McCarney } 1524adc4f0dbSShawn McCarney } 1525711ac7a9SEd Tanous if (name == "PartNumber") 1526711ac7a9SEd Tanous { 1527711ac7a9SEd Tanous const std::string* value = 1528711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1529711ac7a9SEd Tanous if (value != nullptr) 1530711ac7a9SEd Tanous { 1531711ac7a9SEd Tanous inventoryItem.partNumber = *value; 1532711ac7a9SEd Tanous } 1533711ac7a9SEd Tanous } 1534711ac7a9SEd Tanous } 1535adc4f0dbSShawn McCarney } 1536adc4f0dbSShawn McCarney 1537711ac7a9SEd Tanous if (interface == 1538711ac7a9SEd Tanous "xyz.openbmc_project.State.Decorator.OperationalStatus") 1539adc4f0dbSShawn McCarney { 1540711ac7a9SEd Tanous for (auto& [name, dbusValue] : values) 1541adc4f0dbSShawn McCarney { 1542711ac7a9SEd Tanous if (name == "Functional") 1543711ac7a9SEd Tanous { 1544711ac7a9SEd Tanous const bool* value = std::get_if<bool>(&dbusValue); 1545adc4f0dbSShawn McCarney if (value != nullptr) 1546adc4f0dbSShawn McCarney { 1547adc4f0dbSShawn McCarney inventoryItem.isFunctional = *value; 15488fb49dd6SShawn McCarney } 15498fb49dd6SShawn McCarney } 15508fb49dd6SShawn McCarney } 15518fb49dd6SShawn McCarney } 1552711ac7a9SEd Tanous } 1553711ac7a9SEd Tanous } 15548fb49dd6SShawn McCarney 15558fb49dd6SShawn McCarney /** 1556adc4f0dbSShawn McCarney * @brief Gets D-Bus data for inventory items associated with sensors. 15578fb49dd6SShawn McCarney * 1558adc4f0dbSShawn McCarney * Uses the specified connections (services) to obtain D-Bus data for inventory 1559adc4f0dbSShawn McCarney * items associated with sensors. Stores the resulting data in the 1560adc4f0dbSShawn McCarney * inventoryItems vector. 15618fb49dd6SShawn McCarney * 1562adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1563adc4f0dbSShawn McCarney * response. 1564adc4f0dbSShawn McCarney * 1565adc4f0dbSShawn McCarney * Finds the inventory item data asynchronously. Invokes callback when data has 1566adc4f0dbSShawn McCarney * been obtained. 1567adc4f0dbSShawn McCarney * 1568adc4f0dbSShawn McCarney * The callback must have the following signature: 1569adc4f0dbSShawn McCarney * @code 1570d500549bSAnthony Wilson * callback(void) 1571adc4f0dbSShawn McCarney * @endcode 1572adc4f0dbSShawn McCarney * 1573adc4f0dbSShawn McCarney * This function is called recursively, obtaining data asynchronously from one 1574adc4f0dbSShawn McCarney * connection in each call. This ensures the callback is not invoked until the 1575adc4f0dbSShawn McCarney * last asynchronous function has completed. 15768fb49dd6SShawn McCarney * 15778fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1578adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1579adc4f0dbSShawn McCarney * @param invConnections Connections that provide data for the inventory items. 15808fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 15818fb49dd6SShawn McCarney * implements ObjectManager. 1582adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory data has been obtained. 1583adc4f0dbSShawn McCarney * @param invConnectionsIndex Current index in invConnections. Only specified 1584adc4f0dbSShawn McCarney * in recursive calls to this function. 15858fb49dd6SShawn McCarney */ 1586adc4f0dbSShawn McCarney template <typename Callback> 1587adc4f0dbSShawn McCarney static void getInventoryItemsData( 15888fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1589adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 15908fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_set<std::string>> invConnections, 15918fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 1592adc4f0dbSShawn McCarney objectMgrPaths, 1593271584abSEd Tanous Callback&& callback, size_t invConnectionsIndex = 0) 15948fb49dd6SShawn McCarney { 1595adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData enter"; 15968fb49dd6SShawn McCarney 1597adc4f0dbSShawn McCarney // If no more connections left, call callback 1598adc4f0dbSShawn McCarney if (invConnectionsIndex >= invConnections->size()) 15998fb49dd6SShawn McCarney { 1600d500549bSAnthony Wilson callback(); 1601adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData exit"; 1602adc4f0dbSShawn McCarney return; 1603adc4f0dbSShawn McCarney } 1604adc4f0dbSShawn McCarney 1605adc4f0dbSShawn McCarney // Get inventory item data from current connection 1606adc4f0dbSShawn McCarney auto it = invConnections->nth(invConnectionsIndex); 1607adc4f0dbSShawn McCarney if (it != invConnections->end()) 1608adc4f0dbSShawn McCarney { 1609adc4f0dbSShawn McCarney const std::string& invConnection = *it; 1610adc4f0dbSShawn McCarney 16118fb49dd6SShawn McCarney // Response handler for GetManagedObjects 1612adc4f0dbSShawn McCarney auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections, 1613f94c4ecfSEd Tanous objectMgrPaths, 1614f94c4ecfSEd Tanous callback{std::forward<Callback>(callback)}, 1615adc4f0dbSShawn McCarney invConnectionsIndex]( 1616adc4f0dbSShawn McCarney const boost::system::error_code ec, 1617711ac7a9SEd Tanous dbus::utility::ManagedObjectType& resp) { 1618adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter"; 16198fb49dd6SShawn McCarney if (ec) 16208fb49dd6SShawn McCarney { 16218fb49dd6SShawn McCarney BMCWEB_LOG_ERROR 1622adc4f0dbSShawn McCarney << "getInventoryItemsData respHandler DBus error " << ec; 16238d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 16248fb49dd6SShawn McCarney return; 16258fb49dd6SShawn McCarney } 16268fb49dd6SShawn McCarney 16278fb49dd6SShawn McCarney // Loop through returned object paths 16288fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 16298fb49dd6SShawn McCarney { 16308fb49dd6SShawn McCarney const std::string& objPath = 16318fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 16328fb49dd6SShawn McCarney 1633adc4f0dbSShawn McCarney // If this object path is one of the specified inventory items 1634adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1635adc4f0dbSShawn McCarney findInventoryItem(inventoryItems, objPath); 1636adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 16378fb49dd6SShawn McCarney { 1638adc4f0dbSShawn McCarney // Store inventory data in InventoryItem 1639adc4f0dbSShawn McCarney storeInventoryItemData(*inventoryItem, objDictEntry.second); 16408fb49dd6SShawn McCarney } 16418fb49dd6SShawn McCarney } 16428fb49dd6SShawn McCarney 1643adc4f0dbSShawn McCarney // Recurse to get inventory item data from next connection 1644adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 1645adc4f0dbSShawn McCarney invConnections, objectMgrPaths, 1646adc4f0dbSShawn McCarney std::move(callback), invConnectionsIndex + 1); 1647adc4f0dbSShawn McCarney 1648adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit"; 16498fb49dd6SShawn McCarney }; 16508fb49dd6SShawn McCarney 16518fb49dd6SShawn McCarney // Find DBus object path that implements ObjectManager for the current 16528fb49dd6SShawn McCarney // connection. If no mapping found, default to "/". 16538fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(invConnection); 16548fb49dd6SShawn McCarney const std::string& objectMgrPath = 16558fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 16568fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is " 16578fb49dd6SShawn McCarney << objectMgrPath; 16588fb49dd6SShawn McCarney 16598fb49dd6SShawn McCarney // Get all object paths and their interfaces for current connection 16608fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 16618fb49dd6SShawn McCarney std::move(respHandler), invConnection, objectMgrPath, 16628fb49dd6SShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 16638fb49dd6SShawn McCarney } 16648fb49dd6SShawn McCarney 1665adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData exit"; 16668fb49dd6SShawn McCarney } 16678fb49dd6SShawn McCarney 16688fb49dd6SShawn McCarney /** 1669adc4f0dbSShawn McCarney * @brief Gets connections that provide D-Bus data for inventory items. 16708fb49dd6SShawn McCarney * 1671adc4f0dbSShawn McCarney * Gets the D-Bus connections (services) that provide data for the inventory 1672adc4f0dbSShawn McCarney * items that are associated with sensors. 16738fb49dd6SShawn McCarney * 16748fb49dd6SShawn McCarney * Finds the connections asynchronously. Invokes callback when information has 16758fb49dd6SShawn McCarney * been obtained. 16768fb49dd6SShawn McCarney * 16778fb49dd6SShawn McCarney * The callback must have the following signature: 16788fb49dd6SShawn McCarney * @code 16798fb49dd6SShawn McCarney * callback(std::shared_ptr<boost::container::flat_set<std::string>> 16808fb49dd6SShawn McCarney * invConnections) 16818fb49dd6SShawn McCarney * @endcode 16828fb49dd6SShawn McCarney * 16838fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1684adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 16858fb49dd6SShawn McCarney * @param callback Callback to invoke when connections have been obtained. 16868fb49dd6SShawn McCarney */ 16878fb49dd6SShawn McCarney template <typename Callback> 16888fb49dd6SShawn McCarney static void getInventoryItemsConnections( 1689b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1690b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 16918fb49dd6SShawn McCarney Callback&& callback) 16928fb49dd6SShawn McCarney { 16938fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter"; 16948fb49dd6SShawn McCarney 16958fb49dd6SShawn McCarney const std::string path = "/xyz/openbmc_project/inventory"; 1696adc4f0dbSShawn McCarney const std::array<std::string, 4> interfaces = { 16978fb49dd6SShawn McCarney "xyz.openbmc_project.Inventory.Item", 1698adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.PowerSupply", 1699adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Decorator.Asset", 17008fb49dd6SShawn McCarney "xyz.openbmc_project.State.Decorator.OperationalStatus"}; 17018fb49dd6SShawn McCarney 17028fb49dd6SShawn McCarney // Response handler for parsing output from GetSubTree 1703f94c4ecfSEd Tanous auto respHandler = [callback{std::forward<Callback>(callback)}, 1704f94c4ecfSEd Tanous sensorsAsyncResp, 1705adc4f0dbSShawn McCarney inventoryItems](const boost::system::error_code ec, 17068fb49dd6SShawn McCarney const GetSubTreeType& subtree) { 17078fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter"; 17088fb49dd6SShawn McCarney if (ec) 17098fb49dd6SShawn McCarney { 17108d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 17118fb49dd6SShawn McCarney BMCWEB_LOG_ERROR 17128fb49dd6SShawn McCarney << "getInventoryItemsConnections respHandler DBus error " << ec; 17138fb49dd6SShawn McCarney return; 17148fb49dd6SShawn McCarney } 17158fb49dd6SShawn McCarney 17168fb49dd6SShawn McCarney // Make unique list of connections for desired inventory items 17178fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_set<std::string>> 17188fb49dd6SShawn McCarney invConnections = 17198fb49dd6SShawn McCarney std::make_shared<boost::container::flat_set<std::string>>(); 17208fb49dd6SShawn McCarney invConnections->reserve(8); 17218fb49dd6SShawn McCarney 17228fb49dd6SShawn McCarney // Loop through objects from GetSubTree 17238fb49dd6SShawn McCarney for (const std::pair< 17248fb49dd6SShawn McCarney std::string, 17258fb49dd6SShawn McCarney std::vector<std::pair<std::string, std::vector<std::string>>>>& 17268fb49dd6SShawn McCarney object : subtree) 17278fb49dd6SShawn McCarney { 1728adc4f0dbSShawn McCarney // Check if object path is one of the specified inventory items 17298fb49dd6SShawn McCarney const std::string& objPath = object.first; 1730adc4f0dbSShawn McCarney if (findInventoryItem(inventoryItems, objPath) != nullptr) 17318fb49dd6SShawn McCarney { 17328fb49dd6SShawn McCarney // Store all connections to inventory item 17338fb49dd6SShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 17348fb49dd6SShawn McCarney objData : object.second) 17358fb49dd6SShawn McCarney { 17368fb49dd6SShawn McCarney const std::string& invConnection = objData.first; 17378fb49dd6SShawn McCarney invConnections->insert(invConnection); 17388fb49dd6SShawn McCarney } 17398fb49dd6SShawn McCarney } 17408fb49dd6SShawn McCarney } 1741d500549bSAnthony Wilson 17428fb49dd6SShawn McCarney callback(invConnections); 17438fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit"; 17448fb49dd6SShawn McCarney }; 17458fb49dd6SShawn McCarney 17468fb49dd6SShawn McCarney // Make call to ObjectMapper to find all inventory items 17478fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 17488fb49dd6SShawn McCarney std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 17498fb49dd6SShawn McCarney "/xyz/openbmc_project/object_mapper", 17508fb49dd6SShawn McCarney "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); 17518fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit"; 17528fb49dd6SShawn McCarney } 17538fb49dd6SShawn McCarney 17548fb49dd6SShawn McCarney /** 1755adc4f0dbSShawn McCarney * @brief Gets associations from sensors to inventory items. 17568fb49dd6SShawn McCarney * 17578fb49dd6SShawn McCarney * Looks for ObjectMapper associations from the specified sensors to related 1758d500549bSAnthony Wilson * inventory items. Then finds the associations from those inventory items to 1759d500549bSAnthony Wilson * their LEDs, if any. 17608fb49dd6SShawn McCarney * 17618fb49dd6SShawn McCarney * Finds the inventory items asynchronously. Invokes callback when information 17628fb49dd6SShawn McCarney * has been obtained. 17638fb49dd6SShawn McCarney * 17648fb49dd6SShawn McCarney * The callback must have the following signature: 17658fb49dd6SShawn McCarney * @code 1766adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 17678fb49dd6SShawn McCarney * @endcode 17688fb49dd6SShawn McCarney * 17698fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 17708fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 17718fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 17728fb49dd6SShawn McCarney * implements ObjectManager. 17738fb49dd6SShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 17748fb49dd6SShawn McCarney */ 17758fb49dd6SShawn McCarney template <typename Callback> 1776adc4f0dbSShawn McCarney static void getInventoryItemAssociations( 1777b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1778b5a76932SEd Tanous const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames, 1779b5a76932SEd Tanous const std::shared_ptr<boost::container::flat_map<std::string, std::string>>& 17808fb49dd6SShawn McCarney objectMgrPaths, 17818fb49dd6SShawn McCarney Callback&& callback) 17828fb49dd6SShawn McCarney { 1783adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter"; 17848fb49dd6SShawn McCarney 17858fb49dd6SShawn McCarney // Response handler for GetManagedObjects 1786f94c4ecfSEd Tanous auto respHandler = [callback{std::forward<Callback>(callback)}, 1787f94c4ecfSEd Tanous sensorsAsyncResp, 17888fb49dd6SShawn McCarney sensorNames](const boost::system::error_code ec, 17898fb49dd6SShawn McCarney dbus::utility::ManagedObjectType& resp) { 1790adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter"; 17918fb49dd6SShawn McCarney if (ec) 17928fb49dd6SShawn McCarney { 1793adc4f0dbSShawn McCarney BMCWEB_LOG_ERROR 1794adc4f0dbSShawn McCarney << "getInventoryItemAssociations respHandler DBus error " << ec; 17958d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 17968fb49dd6SShawn McCarney return; 17978fb49dd6SShawn McCarney } 17988fb49dd6SShawn McCarney 1799adc4f0dbSShawn McCarney // Create vector to hold list of inventory items 1800adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems = 1801adc4f0dbSShawn McCarney std::make_shared<std::vector<InventoryItem>>(); 1802adc4f0dbSShawn McCarney 18038fb49dd6SShawn McCarney // Loop through returned object paths 18048fb49dd6SShawn McCarney std::string sensorAssocPath; 18058fb49dd6SShawn McCarney sensorAssocPath.reserve(128); // avoid memory allocations 18068fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 18078fb49dd6SShawn McCarney { 18088fb49dd6SShawn McCarney const std::string& objPath = 18098fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 18108fb49dd6SShawn McCarney 18118fb49dd6SShawn McCarney // If path is inventory association for one of the specified sensors 18128fb49dd6SShawn McCarney for (const std::string& sensorName : *sensorNames) 18138fb49dd6SShawn McCarney { 18148fb49dd6SShawn McCarney sensorAssocPath = sensorName; 18158fb49dd6SShawn McCarney sensorAssocPath += "/inventory"; 18168fb49dd6SShawn McCarney if (objPath == sensorAssocPath) 18178fb49dd6SShawn McCarney { 18188fb49dd6SShawn McCarney // Get Association interface for object path 1819711ac7a9SEd Tanous for (const auto& [interface, values] : objDictEntry.second) 18208fb49dd6SShawn McCarney { 1821711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Association") 1822711ac7a9SEd Tanous { 1823711ac7a9SEd Tanous for (const auto& [valueName, value] : values) 1824711ac7a9SEd Tanous { 1825711ac7a9SEd Tanous if (valueName == "endpoints") 18268fb49dd6SShawn McCarney { 18278fb49dd6SShawn McCarney const std::vector<std::string>* endpoints = 18288fb49dd6SShawn McCarney std::get_if<std::vector<std::string>>( 1829711ac7a9SEd Tanous &value); 1830711ac7a9SEd Tanous if ((endpoints != nullptr) && 1831711ac7a9SEd Tanous !endpoints->empty()) 18328fb49dd6SShawn McCarney { 1833adc4f0dbSShawn McCarney // Add inventory item to vector 1834adc4f0dbSShawn McCarney const std::string& invItemPath = 1835adc4f0dbSShawn McCarney endpoints->front(); 1836711ac7a9SEd Tanous addInventoryItem(inventoryItems, 1837711ac7a9SEd Tanous invItemPath, 1838adc4f0dbSShawn McCarney sensorName); 18398fb49dd6SShawn McCarney } 18408fb49dd6SShawn McCarney } 18418fb49dd6SShawn McCarney } 1842711ac7a9SEd Tanous } 1843711ac7a9SEd Tanous } 18448fb49dd6SShawn McCarney break; 18458fb49dd6SShawn McCarney } 18468fb49dd6SShawn McCarney } 18478fb49dd6SShawn McCarney } 18488fb49dd6SShawn McCarney 1849d500549bSAnthony Wilson // Now loop through the returned object paths again, this time to 1850d500549bSAnthony Wilson // find the leds associated with the inventory items we just found 1851d500549bSAnthony Wilson std::string inventoryAssocPath; 1852d500549bSAnthony Wilson inventoryAssocPath.reserve(128); // avoid memory allocations 1853d500549bSAnthony Wilson for (const auto& objDictEntry : resp) 1854d500549bSAnthony Wilson { 1855d500549bSAnthony Wilson const std::string& objPath = 1856d500549bSAnthony Wilson static_cast<const std::string&>(objDictEntry.first); 1857d500549bSAnthony Wilson 1858d500549bSAnthony Wilson for (InventoryItem& inventoryItem : *inventoryItems) 1859d500549bSAnthony Wilson { 1860d500549bSAnthony Wilson inventoryAssocPath = inventoryItem.objectPath; 1861d500549bSAnthony Wilson inventoryAssocPath += "/leds"; 1862d500549bSAnthony Wilson if (objPath == inventoryAssocPath) 1863d500549bSAnthony Wilson { 1864711ac7a9SEd Tanous for (const auto& [interface, values] : objDictEntry.second) 1865d500549bSAnthony Wilson { 1866711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Association") 1867711ac7a9SEd Tanous { 1868711ac7a9SEd Tanous for (const auto& [valueName, value] : values) 1869711ac7a9SEd Tanous { 1870711ac7a9SEd Tanous if (valueName == "endpoints") 1871d500549bSAnthony Wilson { 1872d500549bSAnthony Wilson const std::vector<std::string>* endpoints = 1873d500549bSAnthony Wilson std::get_if<std::vector<std::string>>( 1874711ac7a9SEd Tanous &value); 1875711ac7a9SEd Tanous if ((endpoints != nullptr) && 1876711ac7a9SEd Tanous !endpoints->empty()) 1877d500549bSAnthony Wilson { 1878711ac7a9SEd Tanous // Add inventory item to vector 1879d500549bSAnthony Wilson // Store LED path in inventory item 1880711ac7a9SEd Tanous const std::string& ledPath = 1881711ac7a9SEd Tanous endpoints->front(); 1882d500549bSAnthony Wilson inventoryItem.ledObjectPath = ledPath; 1883d500549bSAnthony Wilson } 1884d500549bSAnthony Wilson } 1885d500549bSAnthony Wilson } 1886711ac7a9SEd Tanous } 1887711ac7a9SEd Tanous } 1888711ac7a9SEd Tanous 1889d500549bSAnthony Wilson break; 1890d500549bSAnthony Wilson } 1891d500549bSAnthony Wilson } 1892d500549bSAnthony Wilson } 1893adc4f0dbSShawn McCarney callback(inventoryItems); 1894adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit"; 18958fb49dd6SShawn McCarney }; 18968fb49dd6SShawn McCarney 18978fb49dd6SShawn McCarney // Find DBus object path that implements ObjectManager for ObjectMapper 18988fb49dd6SShawn McCarney std::string connection = "xyz.openbmc_project.ObjectMapper"; 18998fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(connection); 19008fb49dd6SShawn McCarney const std::string& objectMgrPath = 19018fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 19028fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is " 19038fb49dd6SShawn McCarney << objectMgrPath; 19048fb49dd6SShawn McCarney 19058fb49dd6SShawn McCarney // Call GetManagedObjects on the ObjectMapper to get all associations 19068fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 19078fb49dd6SShawn McCarney std::move(respHandler), connection, objectMgrPath, 19088fb49dd6SShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 19098fb49dd6SShawn McCarney 1910adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit"; 19118fb49dd6SShawn McCarney } 19128fb49dd6SShawn McCarney 19138fb49dd6SShawn McCarney /** 1914d500549bSAnthony Wilson * @brief Gets D-Bus data for inventory item leds associated with sensors. 1915d500549bSAnthony Wilson * 1916d500549bSAnthony Wilson * Uses the specified connections (services) to obtain D-Bus data for inventory 1917d500549bSAnthony Wilson * item leds associated with sensors. Stores the resulting data in the 1918d500549bSAnthony Wilson * inventoryItems vector. 1919d500549bSAnthony Wilson * 1920d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 1921d500549bSAnthony Wilson * response. 1922d500549bSAnthony Wilson * 1923d500549bSAnthony Wilson * Finds the inventory item led data asynchronously. Invokes callback when data 1924d500549bSAnthony Wilson * has been obtained. 1925d500549bSAnthony Wilson * 1926d500549bSAnthony Wilson * The callback must have the following signature: 1927d500549bSAnthony Wilson * @code 192842cbe538SGunnar Mills * callback() 1929d500549bSAnthony Wilson * @endcode 1930d500549bSAnthony Wilson * 1931d500549bSAnthony Wilson * This function is called recursively, obtaining data asynchronously from one 1932d500549bSAnthony Wilson * connection in each call. This ensures the callback is not invoked until the 1933d500549bSAnthony Wilson * last asynchronous function has completed. 1934d500549bSAnthony Wilson * 1935d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 1936d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1937d500549bSAnthony Wilson * @param ledConnections Connections that provide data for the inventory leds. 1938d500549bSAnthony Wilson * @param callback Callback to invoke when inventory data has been obtained. 1939d500549bSAnthony Wilson * @param ledConnectionsIndex Current index in ledConnections. Only specified 1940d500549bSAnthony Wilson * in recursive calls to this function. 1941d500549bSAnthony Wilson */ 1942d500549bSAnthony Wilson template <typename Callback> 1943d500549bSAnthony Wilson void getInventoryLedData( 1944d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1945d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1946d500549bSAnthony Wilson std::shared_ptr<boost::container::flat_map<std::string, std::string>> 1947d500549bSAnthony Wilson ledConnections, 1948d500549bSAnthony Wilson Callback&& callback, size_t ledConnectionsIndex = 0) 1949d500549bSAnthony Wilson { 1950d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData enter"; 1951d500549bSAnthony Wilson 1952d500549bSAnthony Wilson // If no more connections left, call callback 1953d500549bSAnthony Wilson if (ledConnectionsIndex >= ledConnections->size()) 1954d500549bSAnthony Wilson { 195542cbe538SGunnar Mills callback(); 1956d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData exit"; 1957d500549bSAnthony Wilson return; 1958d500549bSAnthony Wilson } 1959d500549bSAnthony Wilson 1960d500549bSAnthony Wilson // Get inventory item data from current connection 1961d500549bSAnthony Wilson auto it = ledConnections->nth(ledConnectionsIndex); 1962d500549bSAnthony Wilson if (it != ledConnections->end()) 1963d500549bSAnthony Wilson { 1964d500549bSAnthony Wilson const std::string& ledPath = (*it).first; 1965d500549bSAnthony Wilson const std::string& ledConnection = (*it).second; 1966d500549bSAnthony Wilson // Response handler for Get State property 19671e1e598dSJonathan Doman auto respHandler = 19681e1e598dSJonathan Doman [sensorsAsyncResp, inventoryItems, ledConnections, ledPath, 1969f94c4ecfSEd Tanous callback{std::forward<Callback>(callback)}, ledConnectionsIndex]( 19701e1e598dSJonathan Doman const boost::system::error_code ec, const std::string& state) { 1971d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter"; 1972d500549bSAnthony Wilson if (ec) 1973d500549bSAnthony Wilson { 1974d500549bSAnthony Wilson BMCWEB_LOG_ERROR 1975d500549bSAnthony Wilson << "getInventoryLedData respHandler DBus error " << ec; 19768d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 1977d500549bSAnthony Wilson return; 1978d500549bSAnthony Wilson } 1979d500549bSAnthony Wilson 19801e1e598dSJonathan Doman BMCWEB_LOG_DEBUG << "Led state: " << state; 1981d500549bSAnthony Wilson // Find inventory item with this LED object path 1982d500549bSAnthony Wilson InventoryItem* inventoryItem = 1983d500549bSAnthony Wilson findInventoryItemForLed(*inventoryItems, ledPath); 1984d500549bSAnthony Wilson if (inventoryItem != nullptr) 1985d500549bSAnthony Wilson { 1986d500549bSAnthony Wilson // Store LED state in InventoryItem 19871e1e598dSJonathan Doman if (boost::ends_with(state, "On")) 1988d500549bSAnthony Wilson { 1989d500549bSAnthony Wilson inventoryItem->ledState = LedState::ON; 1990d500549bSAnthony Wilson } 19911e1e598dSJonathan Doman else if (boost::ends_with(state, "Blink")) 1992d500549bSAnthony Wilson { 1993d500549bSAnthony Wilson inventoryItem->ledState = LedState::BLINK; 1994d500549bSAnthony Wilson } 19951e1e598dSJonathan Doman else if (boost::ends_with(state, "Off")) 1996d500549bSAnthony Wilson { 1997d500549bSAnthony Wilson inventoryItem->ledState = LedState::OFF; 1998d500549bSAnthony Wilson } 1999d500549bSAnthony Wilson else 2000d500549bSAnthony Wilson { 2001d500549bSAnthony Wilson inventoryItem->ledState = LedState::UNKNOWN; 2002d500549bSAnthony Wilson } 2003d500549bSAnthony Wilson } 2004d500549bSAnthony Wilson 2005d500549bSAnthony Wilson // Recurse to get LED data from next connection 2006d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, 2007d500549bSAnthony Wilson ledConnections, std::move(callback), 2008d500549bSAnthony Wilson ledConnectionsIndex + 1); 2009d500549bSAnthony Wilson 2010d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit"; 2011d500549bSAnthony Wilson }; 2012d500549bSAnthony Wilson 2013d500549bSAnthony Wilson // Get the State property for the current LED 20141e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 20151e1e598dSJonathan Doman *crow::connections::systemBus, ledConnection, ledPath, 20161e1e598dSJonathan Doman "xyz.openbmc_project.Led.Physical", "State", 20171e1e598dSJonathan Doman std::move(respHandler)); 2018d500549bSAnthony Wilson } 2019d500549bSAnthony Wilson 2020d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData exit"; 2021d500549bSAnthony Wilson } 2022d500549bSAnthony Wilson 2023d500549bSAnthony Wilson /** 2024d500549bSAnthony Wilson * @brief Gets LED data for LEDs associated with given inventory items. 2025d500549bSAnthony Wilson * 2026d500549bSAnthony Wilson * Gets the D-Bus connections (services) that provide LED data for the LEDs 2027d500549bSAnthony Wilson * associated with the specified inventory items. Then gets the LED data from 2028d500549bSAnthony Wilson * each connection and stores it in the inventory item. 2029d500549bSAnthony Wilson * 2030d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 2031d500549bSAnthony Wilson * response. 2032d500549bSAnthony Wilson * 2033d500549bSAnthony Wilson * Finds the LED data asynchronously. Invokes callback when information has 2034d500549bSAnthony Wilson * been obtained. 2035d500549bSAnthony Wilson * 2036d500549bSAnthony Wilson * The callback must have the following signature: 2037d500549bSAnthony Wilson * @code 203842cbe538SGunnar Mills * callback() 2039d500549bSAnthony Wilson * @endcode 2040d500549bSAnthony Wilson * 2041d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 2042d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 2043d500549bSAnthony Wilson * @param callback Callback to invoke when inventory items have been obtained. 2044d500549bSAnthony Wilson */ 2045d500549bSAnthony Wilson template <typename Callback> 2046d500549bSAnthony Wilson void getInventoryLeds( 2047d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 2048d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 2049d500549bSAnthony Wilson Callback&& callback) 2050d500549bSAnthony Wilson { 2051d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds enter"; 2052d500549bSAnthony Wilson 2053d500549bSAnthony Wilson const std::string path = "/xyz/openbmc_project"; 2054d500549bSAnthony Wilson const std::array<std::string, 1> interfaces = { 2055d500549bSAnthony Wilson "xyz.openbmc_project.Led.Physical"}; 2056d500549bSAnthony Wilson 2057d500549bSAnthony Wilson // Response handler for parsing output from GetSubTree 2058f94c4ecfSEd Tanous auto respHandler = [callback{std::forward<Callback>(callback)}, 2059f94c4ecfSEd Tanous sensorsAsyncResp, 2060d500549bSAnthony Wilson inventoryItems](const boost::system::error_code ec, 2061d500549bSAnthony Wilson const GetSubTreeType& subtree) { 2062d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter"; 2063d500549bSAnthony Wilson if (ec) 2064d500549bSAnthony Wilson { 20658d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 2066d500549bSAnthony Wilson BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error " 2067d500549bSAnthony Wilson << ec; 2068d500549bSAnthony Wilson return; 2069d500549bSAnthony Wilson } 2070d500549bSAnthony Wilson 2071d500549bSAnthony Wilson // Build map of LED object paths to connections 2072d500549bSAnthony Wilson std::shared_ptr<boost::container::flat_map<std::string, std::string>> 2073d500549bSAnthony Wilson ledConnections = std::make_shared< 2074d500549bSAnthony Wilson boost::container::flat_map<std::string, std::string>>(); 2075d500549bSAnthony Wilson 2076d500549bSAnthony Wilson // Loop through objects from GetSubTree 2077d500549bSAnthony Wilson for (const std::pair< 2078d500549bSAnthony Wilson std::string, 2079d500549bSAnthony Wilson std::vector<std::pair<std::string, std::vector<std::string>>>>& 2080d500549bSAnthony Wilson object : subtree) 2081d500549bSAnthony Wilson { 2082d500549bSAnthony Wilson // Check if object path is LED for one of the specified inventory 2083d500549bSAnthony Wilson // items 2084d500549bSAnthony Wilson const std::string& ledPath = object.first; 2085d500549bSAnthony Wilson if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr) 2086d500549bSAnthony Wilson { 2087d500549bSAnthony Wilson // Add mapping from ledPath to connection 2088d500549bSAnthony Wilson const std::string& connection = object.second.begin()->first; 2089d500549bSAnthony Wilson (*ledConnections)[ledPath] = connection; 2090d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> " 2091d500549bSAnthony Wilson << connection; 2092d500549bSAnthony Wilson } 2093d500549bSAnthony Wilson } 2094d500549bSAnthony Wilson 2095d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections, 2096d500549bSAnthony Wilson std::move(callback)); 2097d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit"; 2098d500549bSAnthony Wilson }; 2099d500549bSAnthony Wilson // Make call to ObjectMapper to find all inventory items 2100d500549bSAnthony Wilson crow::connections::systemBus->async_method_call( 2101d500549bSAnthony Wilson std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 2102d500549bSAnthony Wilson "/xyz/openbmc_project/object_mapper", 2103d500549bSAnthony Wilson "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); 2104d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds exit"; 2105d500549bSAnthony Wilson } 2106d500549bSAnthony Wilson 2107d500549bSAnthony Wilson /** 210842cbe538SGunnar Mills * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent 210942cbe538SGunnar Mills * 211042cbe538SGunnar Mills * Uses the specified connections (services) (currently assumes just one) to 211142cbe538SGunnar Mills * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in 211242cbe538SGunnar Mills * the inventoryItems vector. Only stores data in Power Supply inventoryItems. 211342cbe538SGunnar Mills * 211442cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 211542cbe538SGunnar Mills * response. 211642cbe538SGunnar Mills * 211742cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 211842cbe538SGunnar Mills * when data has been obtained. 211942cbe538SGunnar Mills * 212042cbe538SGunnar Mills * The callback must have the following signature: 212142cbe538SGunnar Mills * @code 212242cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 212342cbe538SGunnar Mills * @endcode 212442cbe538SGunnar Mills * 212542cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 212642cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 212742cbe538SGunnar Mills * @param psAttributesConnections Connections that provide data for the Power 212842cbe538SGunnar Mills * Supply Attributes 212942cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 213042cbe538SGunnar Mills */ 213142cbe538SGunnar Mills template <typename Callback> 213242cbe538SGunnar Mills void getPowerSupplyAttributesData( 2133b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 213442cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 213542cbe538SGunnar Mills const boost::container::flat_map<std::string, std::string>& 213642cbe538SGunnar Mills psAttributesConnections, 213742cbe538SGunnar Mills Callback&& callback) 213842cbe538SGunnar Mills { 213942cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter"; 214042cbe538SGunnar Mills 214142cbe538SGunnar Mills if (psAttributesConnections.empty()) 214242cbe538SGunnar Mills { 214342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!"; 214442cbe538SGunnar Mills callback(inventoryItems); 214542cbe538SGunnar Mills return; 214642cbe538SGunnar Mills } 214742cbe538SGunnar Mills 214842cbe538SGunnar Mills // Assuming just one connection (service) for now 214942cbe538SGunnar Mills auto it = psAttributesConnections.nth(0); 215042cbe538SGunnar Mills 215142cbe538SGunnar Mills const std::string& psAttributesPath = (*it).first; 215242cbe538SGunnar Mills const std::string& psAttributesConnection = (*it).second; 215342cbe538SGunnar Mills 215442cbe538SGunnar Mills // Response handler for Get DeratingFactor property 215542cbe538SGunnar Mills auto respHandler = [sensorsAsyncResp, inventoryItems, 2156f94c4ecfSEd Tanous callback{std::forward<Callback>(callback)}]( 215742cbe538SGunnar Mills const boost::system::error_code ec, 21581e1e598dSJonathan Doman const uint32_t value) { 215942cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter"; 216042cbe538SGunnar Mills if (ec) 216142cbe538SGunnar Mills { 216242cbe538SGunnar Mills BMCWEB_LOG_ERROR 216342cbe538SGunnar Mills << "getPowerSupplyAttributesData respHandler DBus error " << ec; 21648d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 216542cbe538SGunnar Mills return; 216642cbe538SGunnar Mills } 216742cbe538SGunnar Mills 21681e1e598dSJonathan Doman BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value; 216942cbe538SGunnar Mills // Store value in Power Supply Inventory Items 217042cbe538SGunnar Mills for (InventoryItem& inventoryItem : *inventoryItems) 217142cbe538SGunnar Mills { 217242cbe538SGunnar Mills if (inventoryItem.isPowerSupply == true) 217342cbe538SGunnar Mills { 217442cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent = 21751e1e598dSJonathan Doman static_cast<int>(value); 217642cbe538SGunnar Mills } 217742cbe538SGunnar Mills } 217842cbe538SGunnar Mills 217942cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit"; 218042cbe538SGunnar Mills callback(inventoryItems); 218142cbe538SGunnar Mills }; 218242cbe538SGunnar Mills 218342cbe538SGunnar Mills // Get the DeratingFactor property for the PowerSupplyAttributes 218442cbe538SGunnar Mills // Currently only property on the interface/only one we care about 21851e1e598dSJonathan Doman sdbusplus::asio::getProperty<uint32_t>( 21861e1e598dSJonathan Doman *crow::connections::systemBus, psAttributesConnection, psAttributesPath, 21871e1e598dSJonathan Doman "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor", 21881e1e598dSJonathan Doman std::move(respHandler)); 218942cbe538SGunnar Mills 219042cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit"; 219142cbe538SGunnar Mills } 219242cbe538SGunnar Mills 219342cbe538SGunnar Mills /** 219442cbe538SGunnar Mills * @brief Gets the Power Supply Attributes such as EfficiencyPercent 219542cbe538SGunnar Mills * 219642cbe538SGunnar Mills * Gets the D-Bus connection (service) that provides Power Supply Attributes 219742cbe538SGunnar Mills * data. Then gets the Power Supply Attributes data from the connection 219842cbe538SGunnar Mills * (currently just assumes 1 connection) and stores the data in the inventory 219942cbe538SGunnar Mills * item. 220042cbe538SGunnar Mills * 220142cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 220242cbe538SGunnar Mills * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish. 220342cbe538SGunnar Mills * 220442cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 220542cbe538SGunnar Mills * when information has been obtained. 220642cbe538SGunnar Mills * 220742cbe538SGunnar Mills * The callback must have the following signature: 220842cbe538SGunnar Mills * @code 220942cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 221042cbe538SGunnar Mills * @endcode 221142cbe538SGunnar Mills * 221242cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 221342cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 221442cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 221542cbe538SGunnar Mills */ 221642cbe538SGunnar Mills template <typename Callback> 221742cbe538SGunnar Mills void getPowerSupplyAttributes( 221842cbe538SGunnar Mills std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 221942cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 222042cbe538SGunnar Mills Callback&& callback) 222142cbe538SGunnar Mills { 222242cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter"; 222342cbe538SGunnar Mills 222442cbe538SGunnar Mills // Only need the power supply attributes when the Power Schema 2225a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode != sensors::node::power) 222642cbe538SGunnar Mills { 222742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power"; 222842cbe538SGunnar Mills callback(inventoryItems); 222942cbe538SGunnar Mills return; 223042cbe538SGunnar Mills } 223142cbe538SGunnar Mills 223242cbe538SGunnar Mills const std::array<std::string, 1> interfaces = { 223342cbe538SGunnar Mills "xyz.openbmc_project.Control.PowerSupplyAttributes"}; 223442cbe538SGunnar Mills 223542cbe538SGunnar Mills // Response handler for parsing output from GetSubTree 2236f94c4ecfSEd Tanous auto respHandler = [callback{std::forward<Callback>(callback)}, 2237f94c4ecfSEd Tanous sensorsAsyncResp, 223842cbe538SGunnar Mills inventoryItems](const boost::system::error_code ec, 223942cbe538SGunnar Mills const GetSubTreeType& subtree) { 224042cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter"; 224142cbe538SGunnar Mills if (ec) 224242cbe538SGunnar Mills { 22438d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 224442cbe538SGunnar Mills BMCWEB_LOG_ERROR 224542cbe538SGunnar Mills << "getPowerSupplyAttributes respHandler DBus error " << ec; 224642cbe538SGunnar Mills return; 224742cbe538SGunnar Mills } 2248*26f6976fSEd Tanous if (subtree.empty()) 224942cbe538SGunnar Mills { 225042cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!"; 225142cbe538SGunnar Mills callback(inventoryItems); 225242cbe538SGunnar Mills return; 225342cbe538SGunnar Mills } 225442cbe538SGunnar Mills 225542cbe538SGunnar Mills // Currently we only support 1 power supply attribute, use this for 225642cbe538SGunnar Mills // all the power supplies. Build map of object path to connection. 225742cbe538SGunnar Mills // Assume just 1 connection and 1 path for now. 225842cbe538SGunnar Mills boost::container::flat_map<std::string, std::string> 225942cbe538SGunnar Mills psAttributesConnections; 226042cbe538SGunnar Mills 226142cbe538SGunnar Mills if (subtree[0].first.empty() || subtree[0].second.empty()) 226242cbe538SGunnar Mills { 226342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!"; 226442cbe538SGunnar Mills callback(inventoryItems); 226542cbe538SGunnar Mills return; 226642cbe538SGunnar Mills } 226742cbe538SGunnar Mills 226842cbe538SGunnar Mills const std::string& psAttributesPath = subtree[0].first; 226942cbe538SGunnar Mills const std::string& connection = subtree[0].second.begin()->first; 227042cbe538SGunnar Mills 227142cbe538SGunnar Mills if (connection.empty()) 227242cbe538SGunnar Mills { 227342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!"; 227442cbe538SGunnar Mills callback(inventoryItems); 227542cbe538SGunnar Mills return; 227642cbe538SGunnar Mills } 227742cbe538SGunnar Mills 227842cbe538SGunnar Mills psAttributesConnections[psAttributesPath] = connection; 227942cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> " 228042cbe538SGunnar Mills << connection; 228142cbe538SGunnar Mills 228242cbe538SGunnar Mills getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems, 228342cbe538SGunnar Mills psAttributesConnections, 228442cbe538SGunnar Mills std::move(callback)); 228542cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit"; 228642cbe538SGunnar Mills }; 228742cbe538SGunnar Mills // Make call to ObjectMapper to find the PowerSupplyAttributes service 228842cbe538SGunnar Mills crow::connections::systemBus->async_method_call( 228942cbe538SGunnar Mills std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 229042cbe538SGunnar Mills "/xyz/openbmc_project/object_mapper", 229142cbe538SGunnar Mills "xyz.openbmc_project.ObjectMapper", "GetSubTree", 229242cbe538SGunnar Mills "/xyz/openbmc_project", 0, interfaces); 229342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit"; 229442cbe538SGunnar Mills } 229542cbe538SGunnar Mills 229642cbe538SGunnar Mills /** 2297adc4f0dbSShawn McCarney * @brief Gets inventory items associated with sensors. 22988fb49dd6SShawn McCarney * 22998fb49dd6SShawn McCarney * Finds the inventory items that are associated with the specified sensors. 2300adc4f0dbSShawn McCarney * Then gets D-Bus data for the inventory items, such as presence and VPD. 23018fb49dd6SShawn McCarney * 2302adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 2303adc4f0dbSShawn McCarney * response. 23048fb49dd6SShawn McCarney * 2305adc4f0dbSShawn McCarney * Finds the inventory items asynchronously. Invokes callback when the 2306adc4f0dbSShawn McCarney * inventory items have been obtained. 2307adc4f0dbSShawn McCarney * 2308adc4f0dbSShawn McCarney * The callback must have the following signature: 2309adc4f0dbSShawn McCarney * @code 2310adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 2311adc4f0dbSShawn McCarney * @endcode 23128fb49dd6SShawn McCarney * 23138fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 23148fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 23158fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 23168fb49dd6SShawn McCarney * implements ObjectManager. 2317adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 23188fb49dd6SShawn McCarney */ 2319adc4f0dbSShawn McCarney template <typename Callback> 2320adc4f0dbSShawn McCarney static void getInventoryItems( 23218fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 23228fb49dd6SShawn McCarney const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames, 23238fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_map<std::string, std::string>> 2324adc4f0dbSShawn McCarney objectMgrPaths, 2325adc4f0dbSShawn McCarney Callback&& callback) 23268fb49dd6SShawn McCarney { 2327adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItems enter"; 2328adc4f0dbSShawn McCarney auto getInventoryItemAssociationsCb = 2329f94c4ecfSEd Tanous [sensorsAsyncResp, objectMgrPaths, 2330f94c4ecfSEd Tanous callback{std::forward<Callback>(callback)}]( 2331adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems) { 2332adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter"; 23338fb49dd6SShawn McCarney auto getInventoryItemsConnectionsCb = 2334adc4f0dbSShawn McCarney [sensorsAsyncResp, inventoryItems, objectMgrPaths, 2335f94c4ecfSEd Tanous callback{std::forward<const Callback>(callback)}]( 23368fb49dd6SShawn McCarney std::shared_ptr<boost::container::flat_set<std::string>> 23378fb49dd6SShawn McCarney invConnections) { 23388fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter"; 2339d500549bSAnthony Wilson auto getInventoryItemsDataCb = 2340d500549bSAnthony Wilson [sensorsAsyncResp, inventoryItems, 2341d500549bSAnthony Wilson callback{std::move(callback)}]() { 2342d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter"; 234342cbe538SGunnar Mills 234442cbe538SGunnar Mills auto getInventoryLedsCb = [sensorsAsyncResp, 234542cbe538SGunnar Mills inventoryItems, 234642cbe538SGunnar Mills callback{std::move( 234742cbe538SGunnar Mills callback)}]() { 234842cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter"; 234942cbe538SGunnar Mills // Find Power Supply Attributes and get the data 235042cbe538SGunnar Mills getPowerSupplyAttributes(sensorsAsyncResp, 235142cbe538SGunnar Mills inventoryItems, 235242cbe538SGunnar Mills std::move(callback)); 235342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit"; 235442cbe538SGunnar Mills }; 235542cbe538SGunnar Mills 2356d500549bSAnthony Wilson // Find led connections and get the data 2357d500549bSAnthony Wilson getInventoryLeds(sensorsAsyncResp, inventoryItems, 235842cbe538SGunnar Mills std::move(getInventoryLedsCb)); 2359d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit"; 2360d500549bSAnthony Wilson }; 23618fb49dd6SShawn McCarney 2362adc4f0dbSShawn McCarney // Get inventory item data from connections 2363adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 2364adc4f0dbSShawn McCarney invConnections, objectMgrPaths, 2365d500549bSAnthony Wilson std::move(getInventoryItemsDataCb)); 23668fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit"; 23678fb49dd6SShawn McCarney }; 23688fb49dd6SShawn McCarney 2369adc4f0dbSShawn McCarney // Get connections that provide inventory item data 23708fb49dd6SShawn McCarney getInventoryItemsConnections( 2371adc4f0dbSShawn McCarney sensorsAsyncResp, inventoryItems, 23728fb49dd6SShawn McCarney std::move(getInventoryItemsConnectionsCb)); 2373adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit"; 23748fb49dd6SShawn McCarney }; 23758fb49dd6SShawn McCarney 2376adc4f0dbSShawn McCarney // Get associations from sensors to inventory items 2377adc4f0dbSShawn McCarney getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths, 2378adc4f0dbSShawn McCarney std::move(getInventoryItemAssociationsCb)); 2379adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItems exit"; 2380adc4f0dbSShawn McCarney } 2381adc4f0dbSShawn McCarney 2382adc4f0dbSShawn McCarney /** 2383adc4f0dbSShawn McCarney * @brief Returns JSON PowerSupply object for the specified inventory item. 2384adc4f0dbSShawn McCarney * 2385adc4f0dbSShawn McCarney * Searches for a JSON PowerSupply object that matches the specified inventory 2386adc4f0dbSShawn McCarney * item. If one is not found, a new PowerSupply object is added to the JSON 2387adc4f0dbSShawn McCarney * array. 2388adc4f0dbSShawn McCarney * 2389adc4f0dbSShawn McCarney * Multiple sensors are often associated with one power supply inventory item. 2390adc4f0dbSShawn McCarney * As a result, multiple sensor values are stored in one JSON PowerSupply 2391adc4f0dbSShawn McCarney * object. 2392adc4f0dbSShawn McCarney * 2393adc4f0dbSShawn McCarney * @param powerSupplyArray JSON array containing Redfish PowerSupply objects. 2394adc4f0dbSShawn McCarney * @param inventoryItem Inventory item for the power supply. 2395adc4f0dbSShawn McCarney * @param chassisId Chassis that contains the power supply. 2396adc4f0dbSShawn McCarney * @return JSON PowerSupply object for the specified inventory item. 2397adc4f0dbSShawn McCarney */ 239823a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray, 2399adc4f0dbSShawn McCarney const InventoryItem& inventoryItem, 2400adc4f0dbSShawn McCarney const std::string& chassisId) 2401adc4f0dbSShawn McCarney { 2402adc4f0dbSShawn McCarney // Check if matching PowerSupply object already exists in JSON array 2403adc4f0dbSShawn McCarney for (nlohmann::json& powerSupply : powerSupplyArray) 2404adc4f0dbSShawn McCarney { 2405adc4f0dbSShawn McCarney if (powerSupply["MemberId"] == inventoryItem.name) 2406adc4f0dbSShawn McCarney { 2407adc4f0dbSShawn McCarney return powerSupply; 2408adc4f0dbSShawn McCarney } 2409adc4f0dbSShawn McCarney } 2410adc4f0dbSShawn McCarney 2411adc4f0dbSShawn McCarney // Add new PowerSupply object to JSON array 2412adc4f0dbSShawn McCarney powerSupplyArray.push_back({}); 2413adc4f0dbSShawn McCarney nlohmann::json& powerSupply = powerSupplyArray.back(); 2414adc4f0dbSShawn McCarney powerSupply["@odata.id"] = 2415adc4f0dbSShawn McCarney "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/"; 2416adc4f0dbSShawn McCarney powerSupply["MemberId"] = inventoryItem.name; 2417adc4f0dbSShawn McCarney powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " "); 2418adc4f0dbSShawn McCarney powerSupply["Manufacturer"] = inventoryItem.manufacturer; 2419adc4f0dbSShawn McCarney powerSupply["Model"] = inventoryItem.model; 2420adc4f0dbSShawn McCarney powerSupply["PartNumber"] = inventoryItem.partNumber; 2421adc4f0dbSShawn McCarney powerSupply["SerialNumber"] = inventoryItem.serialNumber; 2422d500549bSAnthony Wilson setLedState(powerSupply, &inventoryItem); 2423adc4f0dbSShawn McCarney 242442cbe538SGunnar Mills if (inventoryItem.powerSupplyEfficiencyPercent >= 0) 242542cbe538SGunnar Mills { 242642cbe538SGunnar Mills powerSupply["EfficiencyPercent"] = 242742cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent; 242842cbe538SGunnar Mills } 242942cbe538SGunnar Mills 243042cbe538SGunnar Mills powerSupply["Status"]["State"] = getState(&inventoryItem); 2431adc4f0dbSShawn McCarney const char* health = inventoryItem.isFunctional ? "OK" : "Critical"; 2432adc4f0dbSShawn McCarney powerSupply["Status"]["Health"] = health; 2433adc4f0dbSShawn McCarney 2434adc4f0dbSShawn McCarney return powerSupply; 24358fb49dd6SShawn McCarney } 24368fb49dd6SShawn McCarney 24378fb49dd6SShawn McCarney /** 2438de629b6eSShawn McCarney * @brief Gets the values of the specified sensors. 2439de629b6eSShawn McCarney * 2440de629b6eSShawn McCarney * Stores the results as JSON in the SensorsAsyncResp. 2441de629b6eSShawn McCarney * 2442de629b6eSShawn McCarney * Gets the sensor values asynchronously. Stores the results later when the 2443de629b6eSShawn McCarney * information has been obtained. 2444de629b6eSShawn McCarney * 2445adc4f0dbSShawn McCarney * The sensorNames set contains all requested sensors for the current chassis. 2446de629b6eSShawn McCarney * 2447de629b6eSShawn McCarney * To minimize the number of DBus calls, the DBus method 2448de629b6eSShawn McCarney * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the 2449de629b6eSShawn McCarney * values of all sensors provided by a connection (service). 2450de629b6eSShawn McCarney * 2451de629b6eSShawn McCarney * The connections set contains all the connections that provide sensor values. 2452de629b6eSShawn McCarney * 2453de629b6eSShawn McCarney * The objectMgrPaths map contains mappings from a connection name to the 2454de629b6eSShawn McCarney * corresponding DBus object path that implements ObjectManager. 2455de629b6eSShawn McCarney * 2456adc4f0dbSShawn McCarney * The InventoryItem vector contains D-Bus inventory items associated with the 2457adc4f0dbSShawn McCarney * sensors. Inventory item data is needed for some Redfish sensor properties. 2458adc4f0dbSShawn McCarney * 2459de629b6eSShawn McCarney * @param SensorsAsyncResp Pointer to object holding response data. 2460adc4f0dbSShawn McCarney * @param sensorNames All requested sensors within the current chassis. 2461de629b6eSShawn McCarney * @param connections Connections that provide sensor values. 2462de629b6eSShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 2463de629b6eSShawn McCarney * implements ObjectManager. 2464adc4f0dbSShawn McCarney * @param inventoryItems Inventory items associated with the sensors. 2465de629b6eSShawn McCarney */ 246623a21a1cSEd Tanous inline void getSensorData( 246781ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 2468b5a76932SEd Tanous const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames, 2469de629b6eSShawn McCarney const boost::container::flat_set<std::string>& connections, 2470b5a76932SEd Tanous const std::shared_ptr<boost::container::flat_map<std::string, std::string>>& 2471adc4f0dbSShawn McCarney objectMgrPaths, 2472b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems) 2473de629b6eSShawn McCarney { 2474de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getSensorData enter"; 2475de629b6eSShawn McCarney // Get managed objects from all services exposing sensors 2476de629b6eSShawn McCarney for (const std::string& connection : connections) 2477de629b6eSShawn McCarney { 2478de629b6eSShawn McCarney // Response handler to process managed objects 247981ce609eSEd Tanous auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames, 2480adc4f0dbSShawn McCarney inventoryItems]( 2481de629b6eSShawn McCarney const boost::system::error_code ec, 2482711ac7a9SEd Tanous dbus::utility::ManagedObjectType& resp) { 2483de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter"; 2484de629b6eSShawn McCarney if (ec) 2485de629b6eSShawn McCarney { 2486de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec; 24878d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 2488de629b6eSShawn McCarney return; 2489de629b6eSShawn McCarney } 2490de629b6eSShawn McCarney // Go through all objects and update response with sensor data 2491de629b6eSShawn McCarney for (const auto& objDictEntry : resp) 2492de629b6eSShawn McCarney { 2493de629b6eSShawn McCarney const std::string& objPath = 2494de629b6eSShawn McCarney static_cast<const std::string&>(objDictEntry.first); 2495de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object " 2496de629b6eSShawn McCarney << objPath; 2497de629b6eSShawn McCarney 2498de629b6eSShawn McCarney std::vector<std::string> split; 2499de629b6eSShawn McCarney // Reserve space for 2500de629b6eSShawn McCarney // /xyz/openbmc_project/sensors/<name>/<subname> 2501de629b6eSShawn McCarney split.reserve(6); 2502de629b6eSShawn McCarney boost::algorithm::split(split, objPath, boost::is_any_of("/")); 2503de629b6eSShawn McCarney if (split.size() < 6) 2504de629b6eSShawn McCarney { 2505de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "Got path that isn't long enough " 2506de629b6eSShawn McCarney << objPath; 2507de629b6eSShawn McCarney continue; 2508de629b6eSShawn McCarney } 2509de629b6eSShawn McCarney // These indexes aren't intuitive, as boost::split puts an empty 2510de629b6eSShawn McCarney // string at the beginning 2511de629b6eSShawn McCarney const std::string& sensorType = split[4]; 2512de629b6eSShawn McCarney const std::string& sensorName = split[5]; 2513de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "sensorName " << sensorName 2514de629b6eSShawn McCarney << " sensorType " << sensorType; 251549c53ac9SJohnathan Mantey if (sensorNames->find(objPath) == sensorNames->end()) 2516de629b6eSShawn McCarney { 2517accdbb2cSAndrew Geissler BMCWEB_LOG_DEBUG << sensorName << " not in sensor list "; 2518de629b6eSShawn McCarney continue; 2519de629b6eSShawn McCarney } 2520de629b6eSShawn McCarney 2521adc4f0dbSShawn McCarney // Find inventory item (if any) associated with sensor 2522adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 2523adc4f0dbSShawn McCarney findInventoryItemForSensor(inventoryItems, objPath); 2524adc4f0dbSShawn McCarney 252595a3ecadSAnthony Wilson const std::string& sensorSchema = 252681ce609eSEd Tanous sensorsAsyncResp->chassisSubNode; 252795a3ecadSAnthony Wilson 252895a3ecadSAnthony Wilson nlohmann::json* sensorJson = nullptr; 252995a3ecadSAnthony Wilson 2530a0ec28b6SAdrian Ambrożewicz if (sensorSchema == sensors::node::sensors) 253195a3ecadSAnthony Wilson { 25328d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] = 253381ce609eSEd Tanous "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + 253481ce609eSEd Tanous "/" + sensorsAsyncResp->chassisSubNode + "/" + 253595a3ecadSAnthony Wilson sensorName; 25368d1b46d7Szhanghch05 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue); 253795a3ecadSAnthony Wilson } 253895a3ecadSAnthony Wilson else 253995a3ecadSAnthony Wilson { 2540271584abSEd Tanous std::string fieldName; 2541de629b6eSShawn McCarney if (sensorType == "temperature") 2542de629b6eSShawn McCarney { 2543de629b6eSShawn McCarney fieldName = "Temperatures"; 2544de629b6eSShawn McCarney } 2545de629b6eSShawn McCarney else if (sensorType == "fan" || sensorType == "fan_tach" || 2546de629b6eSShawn McCarney sensorType == "fan_pwm") 2547de629b6eSShawn McCarney { 2548de629b6eSShawn McCarney fieldName = "Fans"; 2549de629b6eSShawn McCarney } 2550de629b6eSShawn McCarney else if (sensorType == "voltage") 2551de629b6eSShawn McCarney { 2552de629b6eSShawn McCarney fieldName = "Voltages"; 2553de629b6eSShawn McCarney } 2554de629b6eSShawn McCarney else if (sensorType == "power") 2555de629b6eSShawn McCarney { 2556028f7ebcSEddie James if (!sensorName.compare("total_power")) 2557028f7ebcSEddie James { 2558028f7ebcSEddie James fieldName = "PowerControl"; 2559028f7ebcSEddie James } 2560adc4f0dbSShawn McCarney else if ((inventoryItem != nullptr) && 2561adc4f0dbSShawn McCarney (inventoryItem->isPowerSupply)) 2562028f7ebcSEddie James { 2563de629b6eSShawn McCarney fieldName = "PowerSupplies"; 2564de629b6eSShawn McCarney } 2565adc4f0dbSShawn McCarney else 2566adc4f0dbSShawn McCarney { 2567adc4f0dbSShawn McCarney // Other power sensors are in SensorCollection 2568adc4f0dbSShawn McCarney continue; 2569adc4f0dbSShawn McCarney } 2570028f7ebcSEddie James } 2571de629b6eSShawn McCarney else 2572de629b6eSShawn McCarney { 2573de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "Unsure how to handle sensorType " 2574de629b6eSShawn McCarney << sensorType; 2575de629b6eSShawn McCarney continue; 2576de629b6eSShawn McCarney } 2577de629b6eSShawn McCarney 2578de629b6eSShawn McCarney nlohmann::json& tempArray = 25798d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName]; 2580adc4f0dbSShawn McCarney if (fieldName == "PowerControl") 258149c53ac9SJohnathan Mantey { 2582adc4f0dbSShawn McCarney if (tempArray.empty()) 25837ab06f49SGunnar Mills { 258495a3ecadSAnthony Wilson // Put multiple "sensors" into a single 258595a3ecadSAnthony Wilson // PowerControl. Follows MemberId naming and 258695a3ecadSAnthony Wilson // naming in power.hpp. 25877ab06f49SGunnar Mills tempArray.push_back( 2588adc4f0dbSShawn McCarney {{"@odata.id", 2589adc4f0dbSShawn McCarney "/redfish/v1/Chassis/" + 259081ce609eSEd Tanous sensorsAsyncResp->chassisId + "/" + 259181ce609eSEd Tanous sensorsAsyncResp->chassisSubNode + "#/" + 2592adc4f0dbSShawn McCarney fieldName + "/0"}}); 2593adc4f0dbSShawn McCarney } 2594adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 2595adc4f0dbSShawn McCarney } 2596adc4f0dbSShawn McCarney else if (fieldName == "PowerSupplies") 2597adc4f0dbSShawn McCarney { 2598adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 2599adc4f0dbSShawn McCarney { 2600adc4f0dbSShawn McCarney sensorJson = 2601adc4f0dbSShawn McCarney &(getPowerSupply(tempArray, *inventoryItem, 260281ce609eSEd Tanous sensorsAsyncResp->chassisId)); 2603adc4f0dbSShawn McCarney } 260449c53ac9SJohnathan Mantey } 260549c53ac9SJohnathan Mantey else 260649c53ac9SJohnathan Mantey { 2607de629b6eSShawn McCarney tempArray.push_back( 260895a3ecadSAnthony Wilson {{"@odata.id", 260995a3ecadSAnthony Wilson "/redfish/v1/Chassis/" + 261081ce609eSEd Tanous sensorsAsyncResp->chassisId + "/" + 261181ce609eSEd Tanous sensorsAsyncResp->chassisSubNode + "#/" + 261295a3ecadSAnthony Wilson fieldName + "/"}}); 2613adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 261449c53ac9SJohnathan Mantey } 261595a3ecadSAnthony Wilson } 2616de629b6eSShawn McCarney 2617adc4f0dbSShawn McCarney if (sensorJson != nullptr) 2618adc4f0dbSShawn McCarney { 2619a0ec28b6SAdrian Ambrożewicz objectInterfacesToJson( 262081ce609eSEd Tanous sensorName, sensorType, sensorsAsyncResp, 2621a0ec28b6SAdrian Ambrożewicz objDictEntry.second, *sensorJson, inventoryItem); 2622adc4f0dbSShawn McCarney } 2623de629b6eSShawn McCarney } 262481ce609eSEd Tanous if (sensorsAsyncResp.use_count() == 1) 262549c53ac9SJohnathan Mantey { 262681ce609eSEd Tanous sortJSONResponse(sensorsAsyncResp); 262781ce609eSEd Tanous if (sensorsAsyncResp->chassisSubNode == sensors::node::thermal) 26288bd25ccdSJames Feist { 262981ce609eSEd Tanous populateFanRedundancy(sensorsAsyncResp); 26308bd25ccdSJames Feist } 263149c53ac9SJohnathan Mantey } 2632de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit"; 2633de629b6eSShawn McCarney }; 2634de629b6eSShawn McCarney 2635de629b6eSShawn McCarney // Find DBus object path that implements ObjectManager for the current 2636de629b6eSShawn McCarney // connection. If no mapping found, default to "/". 26378fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(connection); 2638de629b6eSShawn McCarney const std::string& objectMgrPath = 26398fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 2640de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is " 2641de629b6eSShawn McCarney << objectMgrPath; 2642de629b6eSShawn McCarney 2643de629b6eSShawn McCarney crow::connections::systemBus->async_method_call( 2644de629b6eSShawn McCarney getManagedObjectsCb, connection, objectMgrPath, 2645de629b6eSShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 264623a21a1cSEd Tanous } 2647de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getSensorData exit"; 2648de629b6eSShawn McCarney } 2649de629b6eSShawn McCarney 265023a21a1cSEd Tanous inline void processSensorList( 265181ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 2652b5a76932SEd Tanous const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames) 26531abe55efSEd Tanous { 265495a3ecadSAnthony Wilson auto getConnectionCb = 265581ce609eSEd Tanous [sensorsAsyncResp, sensorNames]( 265695a3ecadSAnthony Wilson const boost::container::flat_set<std::string>& connections) { 265755c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getConnectionCb enter"; 2658de629b6eSShawn McCarney auto getObjectManagerPathsCb = 265981ce609eSEd Tanous [sensorsAsyncResp, sensorNames, 2660b5a76932SEd Tanous connections](const std::shared_ptr<boost::container::flat_map< 2661b5a76932SEd Tanous std::string, std::string>>& objectMgrPaths) { 2662de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter"; 2663adc4f0dbSShawn McCarney auto getInventoryItemsCb = 266481ce609eSEd Tanous [sensorsAsyncResp, sensorNames, connections, 2665adc4f0dbSShawn McCarney objectMgrPaths]( 2666f23b7296SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& 2667adc4f0dbSShawn McCarney inventoryItems) { 2668adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter"; 266949c53ac9SJohnathan Mantey // Get sensor data and store results in JSON 267081ce609eSEd Tanous getSensorData(sensorsAsyncResp, sensorNames, 2671adc4f0dbSShawn McCarney connections, objectMgrPaths, 2672f23b7296SEd Tanous inventoryItems); 2673adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit"; 2674adc4f0dbSShawn McCarney }; 2675adc4f0dbSShawn McCarney 2676adc4f0dbSShawn McCarney // Get inventory items associated with sensors 267781ce609eSEd Tanous getInventoryItems(sensorsAsyncResp, sensorNames, 2678adc4f0dbSShawn McCarney objectMgrPaths, 2679adc4f0dbSShawn McCarney std::move(getInventoryItemsCb)); 2680adc4f0dbSShawn McCarney 2681de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit"; 268208777fb0SLewanczyk, Dawid }; 2683de629b6eSShawn McCarney 268449c53ac9SJohnathan Mantey // Get mapping from connection names to the DBus object 268549c53ac9SJohnathan Mantey // paths that implement the ObjectManager interface 268681ce609eSEd Tanous getObjectManagerPaths(sensorsAsyncResp, 2687de629b6eSShawn McCarney std::move(getObjectManagerPathsCb)); 268855c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getConnectionCb exit"; 268908777fb0SLewanczyk, Dawid }; 2690de629b6eSShawn McCarney 2691de629b6eSShawn McCarney // Get set of connections that provide sensor values 269281ce609eSEd Tanous getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb)); 269395a3ecadSAnthony Wilson } 269495a3ecadSAnthony Wilson 269595a3ecadSAnthony Wilson /** 269695a3ecadSAnthony Wilson * @brief Entry point for retrieving sensors data related to requested 269795a3ecadSAnthony Wilson * chassis. 269895a3ecadSAnthony Wilson * @param SensorsAsyncResp Pointer to object holding response data 269995a3ecadSAnthony Wilson */ 2700b5a76932SEd Tanous inline void 270181ce609eSEd Tanous getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 270295a3ecadSAnthony Wilson { 270395a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisData enter"; 270495a3ecadSAnthony Wilson auto getChassisCb = 270581ce609eSEd Tanous [sensorsAsyncResp]( 2706f23b7296SEd Tanous const std::shared_ptr<boost::container::flat_set<std::string>>& 270795a3ecadSAnthony Wilson sensorNames) { 270895a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb enter"; 270981ce609eSEd Tanous processSensorList(sensorsAsyncResp, sensorNames); 271055c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassisCb exit"; 271108777fb0SLewanczyk, Dawid }; 27128d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] = 27138d1b46d7Szhanghch05 nlohmann::json::array(); 271408777fb0SLewanczyk, Dawid 271526f03899SShawn McCarney // Get set of sensors in chassis 271681ce609eSEd Tanous getChassis(sensorsAsyncResp, std::move(getChassisCb)); 271755c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassisData exit"; 2718271584abSEd Tanous } 271908777fb0SLewanczyk, Dawid 2720413961deSRichard Marian Thomaiyar /** 272149c53ac9SJohnathan Mantey * @brief Find the requested sensorName in the list of all sensors supplied by 272249c53ac9SJohnathan Mantey * the chassis node 272349c53ac9SJohnathan Mantey * 272449c53ac9SJohnathan Mantey * @param sensorName The sensor name supplied in the PATCH request 272549c53ac9SJohnathan Mantey * @param sensorsList The list of sensors managed by the chassis node 272649c53ac9SJohnathan Mantey * @param sensorsModified The list of sensors that were found as a result of 272749c53ac9SJohnathan Mantey * repeated calls to this function 272849c53ac9SJohnathan Mantey */ 272923a21a1cSEd Tanous inline bool findSensorNameUsingSensorPath( 27300a86febdSRichard Marian Thomaiyar std::string_view sensorName, 273149c53ac9SJohnathan Mantey boost::container::flat_set<std::string>& sensorsList, 273249c53ac9SJohnathan Mantey boost::container::flat_set<std::string>& sensorsModified) 273349c53ac9SJohnathan Mantey { 273428aa8de5SGeorge Liu for (auto& chassisSensor : sensorsList) 273549c53ac9SJohnathan Mantey { 273628aa8de5SGeorge Liu sdbusplus::message::object_path path(chassisSensor); 2737b00dcc27SEd Tanous std::string thisSensorName = path.filename(); 273828aa8de5SGeorge Liu if (thisSensorName.empty()) 273949c53ac9SJohnathan Mantey { 274049c53ac9SJohnathan Mantey continue; 274149c53ac9SJohnathan Mantey } 274249c53ac9SJohnathan Mantey if (thisSensorName == sensorName) 274349c53ac9SJohnathan Mantey { 274449c53ac9SJohnathan Mantey sensorsModified.emplace(chassisSensor); 274549c53ac9SJohnathan Mantey return true; 274649c53ac9SJohnathan Mantey } 274749c53ac9SJohnathan Mantey } 274849c53ac9SJohnathan Mantey return false; 274949c53ac9SJohnathan Mantey } 275049c53ac9SJohnathan Mantey 275149c53ac9SJohnathan Mantey /** 2752413961deSRichard Marian Thomaiyar * @brief Entry point for overriding sensor values of given sensor 2753413961deSRichard Marian Thomaiyar * 27548d1b46d7Szhanghch05 * @param sensorAsyncResp response object 27554bb3dc34SCarol Wang * @param allCollections Collections extract from sensors' request patch info 2756413961deSRichard Marian Thomaiyar * @param chassisSubNode Chassis Node for which the query has to happen 2757413961deSRichard Marian Thomaiyar */ 275823a21a1cSEd Tanous inline void setSensorsOverride( 2759b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp, 27604bb3dc34SCarol Wang std::unordered_map<std::string, std::vector<nlohmann::json>>& 2761397fd61fSjayaprakash Mutyala allCollections) 2762413961deSRichard Marian Thomaiyar { 276370d1d0aaSjayaprakash Mutyala BMCWEB_LOG_INFO << "setSensorsOverride for subNode" 27644bb3dc34SCarol Wang << sensorAsyncResp->chassisSubNode << "\n"; 2765413961deSRichard Marian Thomaiyar 2766543f4400SEd Tanous const char* propertyValueName = nullptr; 2767f65af9e8SRichard Marian Thomaiyar std::unordered_map<std::string, std::pair<double, std::string>> overrideMap; 2768413961deSRichard Marian Thomaiyar std::string memberId; 2769543f4400SEd Tanous double value = 0.0; 2770f65af9e8SRichard Marian Thomaiyar for (auto& collectionItems : allCollections) 2771f65af9e8SRichard Marian Thomaiyar { 2772f65af9e8SRichard Marian Thomaiyar if (collectionItems.first == "Temperatures") 2773f65af9e8SRichard Marian Thomaiyar { 2774f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingCelsius"; 2775f65af9e8SRichard Marian Thomaiyar } 2776f65af9e8SRichard Marian Thomaiyar else if (collectionItems.first == "Fans") 2777f65af9e8SRichard Marian Thomaiyar { 2778f65af9e8SRichard Marian Thomaiyar propertyValueName = "Reading"; 2779f65af9e8SRichard Marian Thomaiyar } 2780f65af9e8SRichard Marian Thomaiyar else 2781f65af9e8SRichard Marian Thomaiyar { 2782f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingVolts"; 2783f65af9e8SRichard Marian Thomaiyar } 2784f65af9e8SRichard Marian Thomaiyar for (auto& item : collectionItems.second) 2785f65af9e8SRichard Marian Thomaiyar { 27868d1b46d7Szhanghch05 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res, 27878d1b46d7Szhanghch05 "MemberId", memberId, propertyValueName, 27888d1b46d7Szhanghch05 value)) 2789413961deSRichard Marian Thomaiyar { 2790413961deSRichard Marian Thomaiyar return; 2791413961deSRichard Marian Thomaiyar } 2792f65af9e8SRichard Marian Thomaiyar overrideMap.emplace(memberId, 2793f65af9e8SRichard Marian Thomaiyar std::make_pair(value, collectionItems.first)); 2794f65af9e8SRichard Marian Thomaiyar } 2795f65af9e8SRichard Marian Thomaiyar } 27964bb3dc34SCarol Wang 2797b5a76932SEd Tanous auto getChassisSensorListCb = [sensorAsyncResp, overrideMap]( 2798b5a76932SEd Tanous const std::shared_ptr< 279949c53ac9SJohnathan Mantey boost::container::flat_set< 2800b5a76932SEd Tanous std::string>>& sensorsList) { 280149c53ac9SJohnathan Mantey // Match sensor names in the PATCH request to those managed by the 280249c53ac9SJohnathan Mantey // chassis node 280349c53ac9SJohnathan Mantey const std::shared_ptr<boost::container::flat_set<std::string>> 280449c53ac9SJohnathan Mantey sensorNames = 280549c53ac9SJohnathan Mantey std::make_shared<boost::container::flat_set<std::string>>(); 2806f65af9e8SRichard Marian Thomaiyar for (const auto& item : overrideMap) 2807413961deSRichard Marian Thomaiyar { 2808f65af9e8SRichard Marian Thomaiyar const auto& sensor = item.first; 280949c53ac9SJohnathan Mantey if (!findSensorNameUsingSensorPath(sensor, *sensorsList, 281049c53ac9SJohnathan Mantey *sensorNames)) 2811f65af9e8SRichard Marian Thomaiyar { 2812f65af9e8SRichard Marian Thomaiyar BMCWEB_LOG_INFO << "Unable to find memberId " << item.first; 28138d1b46d7Szhanghch05 messages::resourceNotFound(sensorAsyncResp->asyncResp->res, 2814f65af9e8SRichard Marian Thomaiyar item.second.second, item.first); 2815413961deSRichard Marian Thomaiyar return; 2816413961deSRichard Marian Thomaiyar } 2817f65af9e8SRichard Marian Thomaiyar } 2818413961deSRichard Marian Thomaiyar // Get the connection to which the memberId belongs 28194f277b54SJayaprakash Mutyala auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap]( 28204f277b54SJayaprakash Mutyala const boost::container::flat_set< 28214f277b54SJayaprakash Mutyala std::string>& /*connections*/, 28224f277b54SJayaprakash Mutyala const std::set<std::pair< 28234f277b54SJayaprakash Mutyala std::string, std::string>>& 2824413961deSRichard Marian Thomaiyar objectsWithConnection) { 2825f65af9e8SRichard Marian Thomaiyar if (objectsWithConnection.size() != overrideMap.size()) 2826413961deSRichard Marian Thomaiyar { 2827413961deSRichard Marian Thomaiyar BMCWEB_LOG_INFO 2828f65af9e8SRichard Marian Thomaiyar << "Unable to find all objects with proper connection " 2829f65af9e8SRichard Marian Thomaiyar << objectsWithConnection.size() << " requested " 2830f65af9e8SRichard Marian Thomaiyar << overrideMap.size() << "\n"; 28314f277b54SJayaprakash Mutyala messages::resourceNotFound(sensorAsyncResp->asyncResp->res, 2832a0ec28b6SAdrian Ambrożewicz sensorAsyncResp->chassisSubNode == 2833a0ec28b6SAdrian Ambrożewicz sensors::node::thermal 2834413961deSRichard Marian Thomaiyar ? "Temperatures" 2835413961deSRichard Marian Thomaiyar : "Voltages", 2836f65af9e8SRichard Marian Thomaiyar "Count"); 2837f65af9e8SRichard Marian Thomaiyar return; 2838f65af9e8SRichard Marian Thomaiyar } 2839f65af9e8SRichard Marian Thomaiyar for (const auto& item : objectsWithConnection) 2840f65af9e8SRichard Marian Thomaiyar { 284128aa8de5SGeorge Liu sdbusplus::message::object_path path(item.first); 284228aa8de5SGeorge Liu std::string sensorName = path.filename(); 284328aa8de5SGeorge Liu if (sensorName.empty()) 2844f65af9e8SRichard Marian Thomaiyar { 28454f277b54SJayaprakash Mutyala messages::internalError(sensorAsyncResp->asyncResp->res); 2846f65af9e8SRichard Marian Thomaiyar return; 2847f65af9e8SRichard Marian Thomaiyar } 2848f65af9e8SRichard Marian Thomaiyar 2849f65af9e8SRichard Marian Thomaiyar const auto& iterator = overrideMap.find(sensorName); 2850f65af9e8SRichard Marian Thomaiyar if (iterator == overrideMap.end()) 2851f65af9e8SRichard Marian Thomaiyar { 2852f65af9e8SRichard Marian Thomaiyar BMCWEB_LOG_INFO << "Unable to find sensor object" 2853f65af9e8SRichard Marian Thomaiyar << item.first << "\n"; 28544f277b54SJayaprakash Mutyala messages::internalError(sensorAsyncResp->asyncResp->res); 2855413961deSRichard Marian Thomaiyar return; 2856413961deSRichard Marian Thomaiyar } 2857413961deSRichard Marian Thomaiyar crow::connections::systemBus->async_method_call( 2858f65af9e8SRichard Marian Thomaiyar [sensorAsyncResp](const boost::system::error_code ec) { 2859413961deSRichard Marian Thomaiyar if (ec) 2860413961deSRichard Marian Thomaiyar { 28614f277b54SJayaprakash Mutyala if (ec.value() == 28624f277b54SJayaprakash Mutyala boost::system::errc::permission_denied) 28634f277b54SJayaprakash Mutyala { 28644f277b54SJayaprakash Mutyala BMCWEB_LOG_WARNING 28654f277b54SJayaprakash Mutyala << "Manufacturing mode is not Enabled...can't " 28664f277b54SJayaprakash Mutyala "Override the sensor value. "; 28674f277b54SJayaprakash Mutyala 28684f277b54SJayaprakash Mutyala messages::insufficientPrivilege( 28698d1b46d7Szhanghch05 sensorAsyncResp->asyncResp->res); 2870413961deSRichard Marian Thomaiyar return; 2871413961deSRichard Marian Thomaiyar } 28724f277b54SJayaprakash Mutyala BMCWEB_LOG_DEBUG 28734f277b54SJayaprakash Mutyala << "setOverrideValueStatus DBUS error: " << ec; 28744f277b54SJayaprakash Mutyala messages::internalError( 28754f277b54SJayaprakash Mutyala sensorAsyncResp->asyncResp->res); 28764f277b54SJayaprakash Mutyala } 2877413961deSRichard Marian Thomaiyar }, 28784f277b54SJayaprakash Mutyala item.second, item.first, "org.freedesktop.DBus.Properties", 28794f277b54SJayaprakash Mutyala "Set", "xyz.openbmc_project.Sensor.Value", "Value", 2880168e20c1SEd Tanous dbus::utility::DbusVariantType(iterator->second.first)); 2881f65af9e8SRichard Marian Thomaiyar } 2882413961deSRichard Marian Thomaiyar }; 2883413961deSRichard Marian Thomaiyar // Get object with connection for the given sensor name 2884413961deSRichard Marian Thomaiyar getObjectsWithConnection(sensorAsyncResp, sensorNames, 2885413961deSRichard Marian Thomaiyar std::move(getObjectsWithConnectionCb)); 2886413961deSRichard Marian Thomaiyar }; 2887413961deSRichard Marian Thomaiyar // get full sensor list for the given chassisId and cross verify the sensor. 2888413961deSRichard Marian Thomaiyar getChassis(sensorAsyncResp, std::move(getChassisSensorListCb)); 2889413961deSRichard Marian Thomaiyar } 2890413961deSRichard Marian Thomaiyar 2891a0ec28b6SAdrian Ambrożewicz /** 2892a0ec28b6SAdrian Ambrożewicz * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus 2893a0ec28b6SAdrian Ambrożewicz * path of the sensor. 2894a0ec28b6SAdrian Ambrożewicz * 2895a0ec28b6SAdrian Ambrożewicz * Function builds valid Redfish response for sensor query of given chassis and 2896a0ec28b6SAdrian Ambrożewicz * node. It then builds metadata about Redfish<->D-Bus correlations and provides 2897a0ec28b6SAdrian Ambrożewicz * it to caller in a callback. 2898a0ec28b6SAdrian Ambrożewicz * 2899a0ec28b6SAdrian Ambrożewicz * @param chassis Chassis for which retrieval should be performed 2900a0ec28b6SAdrian Ambrożewicz * @param node Node (group) of sensors. See sensors::node for supported values 2901a0ec28b6SAdrian Ambrożewicz * @param mapComplete Callback to be called with retrieval result 2902a0ec28b6SAdrian Ambrożewicz */ 2903021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis, 2904021d32cfSKrzysztof Grobelny const std::string& node, 2905a0ec28b6SAdrian Ambrożewicz SensorsAsyncResp::DataCompleteCb&& mapComplete) 2906a0ec28b6SAdrian Ambrożewicz { 2907c2bf7f99SWludzik, Jozef auto pathIt = sensors::dbus::paths.find(node); 2908c2bf7f99SWludzik, Jozef if (pathIt == sensors::dbus::paths.end()) 2909a0ec28b6SAdrian Ambrożewicz { 2910a0ec28b6SAdrian Ambrożewicz BMCWEB_LOG_ERROR << "Wrong node provided : " << node; 2911a0ec28b6SAdrian Ambrożewicz mapComplete(boost::beast::http::status::bad_request, {}); 2912a0ec28b6SAdrian Ambrożewicz return; 2913a0ec28b6SAdrian Ambrożewicz } 2914d51e072fSKrzysztof Grobelny 29159062d478SGunnar Mills auto res = std::make_shared<crow::Response>(); 29169062d478SGunnar Mills auto asyncResp = std::make_shared<bmcweb::AsyncResp>(*res); 2917a0ec28b6SAdrian Ambrożewicz auto callback = 29189062d478SGunnar Mills [res, asyncResp, mapCompleteCb{std::move(mapComplete)}]( 2919a0ec28b6SAdrian Ambrożewicz const boost::beast::http::status status, 2920a0ec28b6SAdrian Ambrożewicz const boost::container::flat_map<std::string, std::string>& 2921a0ec28b6SAdrian Ambrożewicz uriToDbus) { mapCompleteCb(status, uriToDbus); }; 2922a0ec28b6SAdrian Ambrożewicz 2923a0ec28b6SAdrian Ambrożewicz auto resp = std::make_shared<SensorsAsyncResp>( 2924d51e072fSKrzysztof Grobelny asyncResp, chassis, pathIt->second, node, std::move(callback)); 2925a0ec28b6SAdrian Ambrożewicz getChassisData(resp); 2926a0ec28b6SAdrian Ambrożewicz } 2927a0ec28b6SAdrian Ambrożewicz 29287e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app) 292995a3ecadSAnthony Wilson { 29307e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/") 2931ed398213SEd Tanous .privileges(redfish::privileges::getSensorCollection) 29327e860f15SJohn Edward Broadbent .methods( 29337e860f15SJohn Edward Broadbent boost::beast::http::verb::get)([](const crow::Request&, 29347e860f15SJohn Edward Broadbent const std::shared_ptr< 29357e860f15SJohn Edward Broadbent bmcweb::AsyncResp>& aResp, 29367e860f15SJohn Edward Broadbent const std::string& chassisId) { 293795a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "SensorCollection doGet enter"; 29388d1b46d7Szhanghch05 293995a3ecadSAnthony Wilson std::shared_ptr<SensorsAsyncResp> asyncResp = 2940a0ec28b6SAdrian Ambrożewicz std::make_shared<SensorsAsyncResp>( 29418d1b46d7Szhanghch05 aResp, chassisId, 29428d1b46d7Szhanghch05 sensors::dbus::paths.at(sensors::node::sensors), 2943a0ec28b6SAdrian Ambrożewicz sensors::node::sensors); 294495a3ecadSAnthony Wilson 294595a3ecadSAnthony Wilson auto getChassisCb = 2946b5a76932SEd Tanous [asyncResp]( 29477e860f15SJohn Edward Broadbent const std::shared_ptr< 29487e860f15SJohn Edward Broadbent boost::container::flat_set<std::string>>& sensorNames) { 294995a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb enter"; 295095a3ecadSAnthony Wilson 295195a3ecadSAnthony Wilson nlohmann::json& entriesArray = 29528d1b46d7Szhanghch05 asyncResp->asyncResp->res.jsonValue["Members"]; 295395a3ecadSAnthony Wilson for (auto& sensor : *sensorNames) 295495a3ecadSAnthony Wilson { 295595a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor; 295695a3ecadSAnthony Wilson 295728aa8de5SGeorge Liu sdbusplus::message::object_path path(sensor); 295828aa8de5SGeorge Liu std::string sensorName = path.filename(); 295928aa8de5SGeorge Liu if (sensorName.empty()) 296095a3ecadSAnthony Wilson { 29617e860f15SJohn Edward Broadbent BMCWEB_LOG_ERROR << "Invalid sensor path: " 29627e860f15SJohn Edward Broadbent << sensor; 29638d1b46d7Szhanghch05 messages::internalError(asyncResp->asyncResp->res); 296495a3ecadSAnthony Wilson return; 296595a3ecadSAnthony Wilson } 296695a3ecadSAnthony Wilson entriesArray.push_back( 29677e860f15SJohn Edward Broadbent {{"@odata.id", "/redfish/v1/Chassis/" + 29687e860f15SJohn Edward Broadbent asyncResp->chassisId + "/" + 29697e860f15SJohn Edward Broadbent asyncResp->chassisSubNode + "/" + 29707e860f15SJohn Edward Broadbent sensorName}}); 297195a3ecadSAnthony Wilson } 297295a3ecadSAnthony Wilson 29738d1b46d7Szhanghch05 asyncResp->asyncResp->res.jsonValue["Members@odata.count"] = 297495a3ecadSAnthony Wilson entriesArray.size(); 297595a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb exit"; 297695a3ecadSAnthony Wilson }; 297795a3ecadSAnthony Wilson 297895a3ecadSAnthony Wilson // Get set of sensors in chassis 297995a3ecadSAnthony Wilson getChassis(asyncResp, std::move(getChassisCb)); 298095a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "SensorCollection doGet exit"; 29817e860f15SJohn Edward Broadbent }); 298295a3ecadSAnthony Wilson } 298395a3ecadSAnthony Wilson 29847e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app) 298595a3ecadSAnthony Wilson { 29867e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/") 2987ed398213SEd Tanous .privileges(redfish::privileges::getSensor) 29887e860f15SJohn Edward Broadbent .methods( 29897e860f15SJohn Edward Broadbent boost::beast::http::verb::get)([](const crow::Request&, 29907e860f15SJohn Edward Broadbent const std::shared_ptr< 29917e860f15SJohn Edward Broadbent bmcweb::AsyncResp>& aResp, 29927e860f15SJohn Edward Broadbent const std::string& chassisId, 29937e860f15SJohn Edward Broadbent const std::string& sensorName) { 299495a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "Sensor doGet enter"; 299595a3ecadSAnthony Wilson std::shared_ptr<SensorsAsyncResp> asyncResp = 29968d1b46d7Szhanghch05 std::make_shared<SensorsAsyncResp>(aResp, chassisId, 2997a0ec28b6SAdrian Ambrożewicz std::vector<const char*>(), 2998a0ec28b6SAdrian Ambrożewicz sensors::node::sensors); 299995a3ecadSAnthony Wilson 300095a3ecadSAnthony Wilson const std::array<const char*, 1> interfaces = { 300195a3ecadSAnthony Wilson "xyz.openbmc_project.Sensor.Value"}; 300295a3ecadSAnthony Wilson 300395a3ecadSAnthony Wilson // Get a list of all of the sensors that implement Sensor.Value 300495a3ecadSAnthony Wilson // and get the path and service name associated with the sensor 300595a3ecadSAnthony Wilson crow::connections::systemBus->async_method_call( 300695a3ecadSAnthony Wilson [asyncResp, sensorName](const boost::system::error_code ec, 300795a3ecadSAnthony Wilson const GetSubTreeType& subtree) { 300895a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "respHandler1 enter"; 300995a3ecadSAnthony Wilson if (ec) 301095a3ecadSAnthony Wilson { 30118d1b46d7Szhanghch05 messages::internalError(asyncResp->asyncResp->res); 30127e860f15SJohn Edward Broadbent BMCWEB_LOG_ERROR 30137e860f15SJohn Edward Broadbent << "Sensor getSensorPaths resp_handler: " 301495a3ecadSAnthony Wilson << "Dbus error " << ec; 301595a3ecadSAnthony Wilson return; 301695a3ecadSAnthony Wilson } 301795a3ecadSAnthony Wilson 301895a3ecadSAnthony Wilson GetSubTreeType::const_iterator it = std::find_if( 301995a3ecadSAnthony Wilson subtree.begin(), subtree.end(), 302095a3ecadSAnthony Wilson [sensorName]( 302195a3ecadSAnthony Wilson const std::pair< 302295a3ecadSAnthony Wilson std::string, 30237e860f15SJohn Edward Broadbent std::vector<std::pair< 30247e860f15SJohn Edward Broadbent std::string, std::vector<std::string>>>>& 302595a3ecadSAnthony Wilson object) { 302628aa8de5SGeorge Liu sdbusplus::message::object_path path(object.first); 302728aa8de5SGeorge Liu std::string name = path.filename(); 302828aa8de5SGeorge Liu if (name.empty()) 302995a3ecadSAnthony Wilson { 303095a3ecadSAnthony Wilson BMCWEB_LOG_ERROR << "Invalid sensor path: " 303128aa8de5SGeorge Liu << object.first; 303295a3ecadSAnthony Wilson return false; 303395a3ecadSAnthony Wilson } 303495a3ecadSAnthony Wilson 303595a3ecadSAnthony Wilson return name == sensorName; 303695a3ecadSAnthony Wilson }); 303795a3ecadSAnthony Wilson 303895a3ecadSAnthony Wilson if (it == subtree.end()) 303995a3ecadSAnthony Wilson { 304095a3ecadSAnthony Wilson BMCWEB_LOG_ERROR << "Could not find path for sensor: " 304195a3ecadSAnthony Wilson << sensorName; 30428d1b46d7Szhanghch05 messages::resourceNotFound(asyncResp->asyncResp->res, 30438d1b46d7Szhanghch05 "Sensor", sensorName); 304495a3ecadSAnthony Wilson return; 304595a3ecadSAnthony Wilson } 304695a3ecadSAnthony Wilson std::string_view sensorPath = (*it).first; 304795a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" 304895a3ecadSAnthony Wilson << sensorName << "': " << sensorPath; 304995a3ecadSAnthony Wilson 30507e860f15SJohn Edward Broadbent const std::shared_ptr< 30517e860f15SJohn Edward Broadbent boost::container::flat_set<std::string>> 305295a3ecadSAnthony Wilson sensorList = std::make_shared< 305395a3ecadSAnthony Wilson boost::container::flat_set<std::string>>(); 305495a3ecadSAnthony Wilson 305595a3ecadSAnthony Wilson sensorList->emplace(sensorPath); 305695a3ecadSAnthony Wilson processSensorList(asyncResp, sensorList); 305795a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "respHandler1 exit"; 305895a3ecadSAnthony Wilson }, 305995a3ecadSAnthony Wilson "xyz.openbmc_project.ObjectMapper", 306095a3ecadSAnthony Wilson "/xyz/openbmc_project/object_mapper", 306195a3ecadSAnthony Wilson "xyz.openbmc_project.ObjectMapper", "GetSubTree", 306295a3ecadSAnthony Wilson "/xyz/openbmc_project/sensors", 2, interfaces); 30637e860f15SJohn Edward Broadbent }); 306495a3ecadSAnthony Wilson } 306595a3ecadSAnthony Wilson 306608777fb0SLewanczyk, Dawid } // namespace redfish 3067