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 18*0ec8b83dSEd Tanous #include "generated/enums/sensor.hpp" 19*0ec8b83dSEd Tanous 207e860f15SJohn Edward Broadbent #include <app.hpp> 2111ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp> 221d7c0054SEd Tanous #include <boost/algorithm/string/find.hpp> 231d7c0054SEd Tanous #include <boost/algorithm/string/predicate.hpp> 2408777fb0SLewanczyk, Dawid #include <boost/algorithm/string/split.hpp> 2508777fb0SLewanczyk, Dawid #include <boost/range/algorithm/replace_copy_if.hpp> 261abe55efSEd Tanous #include <dbus_singleton.hpp> 27168e20c1SEd Tanous #include <dbus_utility.hpp> 2845ca1b86SEd Tanous #include <query.hpp> 29ed398213SEd Tanous #include <registries/privilege_registry.hpp> 301e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 3186d89ed7SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp> 3286d89ed7SKrzysztof Grobelny #include <utils/dbus_utils.hpp> 33413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp> 34928fefb9SNan Zhou #include <utils/query_param.hpp> 351214b7e7SGunnar Mills 361214b7e7SGunnar Mills #include <cmath> 37fe04d49cSNan Zhou #include <iterator> 38fe04d49cSNan Zhou #include <map> 39fe04d49cSNan Zhou #include <set> 40b5a76932SEd Tanous #include <utility> 41abf2add6SEd Tanous #include <variant> 4208777fb0SLewanczyk, Dawid 431abe55efSEd Tanous namespace redfish 441abe55efSEd Tanous { 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 5502da7c5aSEd Tanous // clang-format off 56a0ec28b6SAdrian Ambrożewicz namespace dbus 57a0ec28b6SAdrian Ambrożewicz { 584ee8e211SEd Tanous static auto powerPaths = std::to_array<std::string_view>({ 5902da7c5aSEd Tanous "/xyz/openbmc_project/sensors/voltage", 6002da7c5aSEd Tanous "/xyz/openbmc_project/sensors/power" 6102da7c5aSEd Tanous }); 62c2bf7f99SWludzik, Jozef 634ee8e211SEd Tanous static auto sensorPaths = std::to_array<std::string_view>({ 6402da7c5aSEd Tanous "/xyz/openbmc_project/sensors/power", 65a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/current", 667088690cSBasheer Ahmed Muddebihal "/xyz/openbmc_project/sensors/airflow", 675deabed9SGunnar Mills "/xyz/openbmc_project/sensors/humidity", 68e8204933SGeorge Liu #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM 69e8204933SGeorge Liu "/xyz/openbmc_project/sensors/voltage", 70e8204933SGeorge Liu "/xyz/openbmc_project/sensors/fan_tach", 71e8204933SGeorge Liu "/xyz/openbmc_project/sensors/temperature", 72e8204933SGeorge Liu "/xyz/openbmc_project/sensors/fan_pwm", 73e8204933SGeorge Liu "/xyz/openbmc_project/sensors/altitude", 74e8204933SGeorge Liu "/xyz/openbmc_project/sensors/energy", 75e8204933SGeorge Liu #endif 7602da7c5aSEd Tanous "/xyz/openbmc_project/sensors/utilization" 7702da7c5aSEd Tanous }); 7802da7c5aSEd Tanous 794ee8e211SEd Tanous static auto thermalPaths = std::to_array<std::string_view>({ 8002da7c5aSEd Tanous "/xyz/openbmc_project/sensors/fan_tach", 81a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/temperature", 8202da7c5aSEd Tanous "/xyz/openbmc_project/sensors/fan_pwm" 8302da7c5aSEd Tanous }); 8402da7c5aSEd Tanous 85c2bf7f99SWludzik, Jozef } // namespace dbus 8602da7c5aSEd Tanous // clang-format on 8702da7c5aSEd Tanous 8802da7c5aSEd Tanous using sensorPair = std::pair<std::string_view, std::span<std::string_view>>; 8902da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = { 9002da7c5aSEd Tanous {{node::power, std::span<std::string_view>(dbus::powerPaths)}, 9102da7c5aSEd Tanous {node::sensors, std::span<std::string_view>(dbus::sensorPaths)}, 9202da7c5aSEd Tanous {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}}; 93c2bf7f99SWludzik, Jozef 94*0ec8b83dSEd Tanous inline sensor::ReadingType toReadingType(std::string_view sensorType) 95c2bf7f99SWludzik, Jozef { 96c2bf7f99SWludzik, Jozef if (sensorType == "voltage") 97c2bf7f99SWludzik, Jozef { 98*0ec8b83dSEd Tanous return sensor::ReadingType::Voltage; 99c2bf7f99SWludzik, Jozef } 100c2bf7f99SWludzik, Jozef if (sensorType == "power") 101c2bf7f99SWludzik, Jozef { 102*0ec8b83dSEd Tanous return sensor::ReadingType::Power; 103c2bf7f99SWludzik, Jozef } 104c2bf7f99SWludzik, Jozef if (sensorType == "current") 105c2bf7f99SWludzik, Jozef { 106*0ec8b83dSEd Tanous return sensor::ReadingType::Current; 107c2bf7f99SWludzik, Jozef } 108c2bf7f99SWludzik, Jozef if (sensorType == "fan_tach") 109c2bf7f99SWludzik, Jozef { 110*0ec8b83dSEd Tanous return sensor::ReadingType::Rotational; 111c2bf7f99SWludzik, Jozef } 112c2bf7f99SWludzik, Jozef if (sensorType == "temperature") 113c2bf7f99SWludzik, Jozef { 114*0ec8b83dSEd Tanous return sensor::ReadingType::Temperature; 115c2bf7f99SWludzik, Jozef } 116c2bf7f99SWludzik, Jozef if (sensorType == "fan_pwm" || sensorType == "utilization") 117c2bf7f99SWludzik, Jozef { 118*0ec8b83dSEd Tanous return sensor::ReadingType::Percent; 119c2bf7f99SWludzik, Jozef } 1205deabed9SGunnar Mills if (sensorType == "humidity") 1215deabed9SGunnar Mills { 122*0ec8b83dSEd Tanous return sensor::ReadingType::Humidity; 1235deabed9SGunnar Mills } 124c2bf7f99SWludzik, Jozef if (sensorType == "altitude") 125c2bf7f99SWludzik, Jozef { 126*0ec8b83dSEd Tanous return sensor::ReadingType::Altitude; 127c2bf7f99SWludzik, Jozef } 128c2bf7f99SWludzik, Jozef if (sensorType == "airflow") 129c2bf7f99SWludzik, Jozef { 130*0ec8b83dSEd Tanous return sensor::ReadingType::AirFlow; 131c2bf7f99SWludzik, Jozef } 132c2bf7f99SWludzik, Jozef if (sensorType == "energy") 133c2bf7f99SWludzik, Jozef { 134*0ec8b83dSEd Tanous return sensor::ReadingType::EnergyJoules; 135c2bf7f99SWludzik, Jozef } 136*0ec8b83dSEd Tanous return sensor::ReadingType::Invalid; 137c2bf7f99SWludzik, Jozef } 138c2bf7f99SWludzik, Jozef 1391d7c0054SEd Tanous inline std::string_view toReadingUnits(std::string_view sensorType) 140c2bf7f99SWludzik, Jozef { 141c2bf7f99SWludzik, Jozef if (sensorType == "voltage") 142c2bf7f99SWludzik, Jozef { 143c2bf7f99SWludzik, Jozef return "V"; 144c2bf7f99SWludzik, Jozef } 145c2bf7f99SWludzik, Jozef if (sensorType == "power") 146c2bf7f99SWludzik, Jozef { 147c2bf7f99SWludzik, Jozef return "W"; 148c2bf7f99SWludzik, Jozef } 149c2bf7f99SWludzik, Jozef if (sensorType == "current") 150c2bf7f99SWludzik, Jozef { 151c2bf7f99SWludzik, Jozef return "A"; 152c2bf7f99SWludzik, Jozef } 153c2bf7f99SWludzik, Jozef if (sensorType == "fan_tach") 154c2bf7f99SWludzik, Jozef { 155c2bf7f99SWludzik, Jozef return "RPM"; 156c2bf7f99SWludzik, Jozef } 157c2bf7f99SWludzik, Jozef if (sensorType == "temperature") 158c2bf7f99SWludzik, Jozef { 159c2bf7f99SWludzik, Jozef return "Cel"; 160c2bf7f99SWludzik, Jozef } 1615deabed9SGunnar Mills if (sensorType == "fan_pwm" || sensorType == "utilization" || 1625deabed9SGunnar Mills sensorType == "humidity") 163c2bf7f99SWludzik, Jozef { 164c2bf7f99SWludzik, Jozef return "%"; 165c2bf7f99SWludzik, Jozef } 166c2bf7f99SWludzik, Jozef if (sensorType == "altitude") 167c2bf7f99SWludzik, Jozef { 168c2bf7f99SWludzik, Jozef return "m"; 169c2bf7f99SWludzik, Jozef } 170c2bf7f99SWludzik, Jozef if (sensorType == "airflow") 171c2bf7f99SWludzik, Jozef { 172c2bf7f99SWludzik, Jozef return "cft_i/min"; 173c2bf7f99SWludzik, Jozef } 174c2bf7f99SWludzik, Jozef if (sensorType == "energy") 175c2bf7f99SWludzik, Jozef { 176c2bf7f99SWludzik, Jozef return "J"; 177c2bf7f99SWludzik, Jozef } 178c2bf7f99SWludzik, Jozef return ""; 179a0ec28b6SAdrian Ambrożewicz } 180a0ec28b6SAdrian Ambrożewicz } // namespace sensors 181a0ec28b6SAdrian Ambrożewicz 18208777fb0SLewanczyk, Dawid /** 183588c3f0dSKowalski, Kamil * SensorsAsyncResp 18408777fb0SLewanczyk, Dawid * Gathers data needed for response processing after async calls are done 18508777fb0SLewanczyk, Dawid */ 1861abe55efSEd Tanous class SensorsAsyncResp 1871abe55efSEd Tanous { 18808777fb0SLewanczyk, Dawid public: 189a0ec28b6SAdrian Ambrożewicz using DataCompleteCb = std::function<void( 190a0ec28b6SAdrian Ambrożewicz const boost::beast::http::status status, 191fe04d49cSNan Zhou const std::map<std::string, std::string>& uriToDbus)>; 192a0ec28b6SAdrian Ambrożewicz 193a0ec28b6SAdrian Ambrożewicz struct SensorData 194a0ec28b6SAdrian Ambrożewicz { 195a0ec28b6SAdrian Ambrożewicz const std::string name; 196a0ec28b6SAdrian Ambrożewicz std::string uri; 197a0ec28b6SAdrian Ambrożewicz const std::string dbusPath; 198a0ec28b6SAdrian Ambrożewicz }; 199a0ec28b6SAdrian Ambrożewicz 2008a592810SEd Tanous SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 2018d1b46d7Szhanghch05 const std::string& chassisIdIn, 20202da7c5aSEd Tanous std::span<std::string_view> typesIn, 20302da7c5aSEd Tanous std::string_view subNode) : 2048a592810SEd Tanous asyncResp(asyncRespIn), 205928fefb9SNan Zhou chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode), 206928fefb9SNan Zhou efficientExpand(false) 2071214b7e7SGunnar Mills {} 20808777fb0SLewanczyk, Dawid 209a0ec28b6SAdrian Ambrożewicz // Store extra data about sensor mapping and return it in callback 2108a592810SEd Tanous SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 2118d1b46d7Szhanghch05 const std::string& chassisIdIn, 21202da7c5aSEd Tanous std::span<std::string_view> typesIn, 21302da7c5aSEd Tanous std::string_view subNode, 214a0ec28b6SAdrian Ambrożewicz DataCompleteCb&& creationComplete) : 2158a592810SEd Tanous asyncResp(asyncRespIn), 216928fefb9SNan Zhou chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode), 217928fefb9SNan Zhou efficientExpand(false), metadata{std::vector<SensorData>()}, 218a0ec28b6SAdrian Ambrożewicz dataComplete{std::move(creationComplete)} 219a0ec28b6SAdrian Ambrożewicz {} 220a0ec28b6SAdrian Ambrożewicz 221928fefb9SNan Zhou // sensor collections expand 2228a592810SEd Tanous SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 223928fefb9SNan Zhou const std::string& chassisIdIn, 22402da7c5aSEd Tanous const std::span<std::string_view> typesIn, 2258a592810SEd Tanous const std::string_view& subNode, bool efficientExpandIn) : 2268a592810SEd Tanous asyncResp(asyncRespIn), 227928fefb9SNan Zhou chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode), 2288a592810SEd Tanous efficientExpand(efficientExpandIn) 229928fefb9SNan Zhou {} 230928fefb9SNan Zhou 2311abe55efSEd Tanous ~SensorsAsyncResp() 2321abe55efSEd Tanous { 2338d1b46d7Szhanghch05 if (asyncResp->res.result() == 2348d1b46d7Szhanghch05 boost::beast::http::status::internal_server_error) 2351abe55efSEd Tanous { 2361abe55efSEd Tanous // Reset the json object to clear out any data that made it in 2371abe55efSEd Tanous // before the error happened todo(ed) handle error condition with 2381abe55efSEd Tanous // proper code 2398d1b46d7Szhanghch05 asyncResp->res.jsonValue = nlohmann::json::object(); 24008777fb0SLewanczyk, Dawid } 241a0ec28b6SAdrian Ambrożewicz 242a0ec28b6SAdrian Ambrożewicz if (dataComplete && metadata) 243a0ec28b6SAdrian Ambrożewicz { 244fe04d49cSNan Zhou std::map<std::string, std::string> map; 2458d1b46d7Szhanghch05 if (asyncResp->res.result() == boost::beast::http::status::ok) 246a0ec28b6SAdrian Ambrożewicz { 247a0ec28b6SAdrian Ambrożewicz for (auto& sensor : *metadata) 248a0ec28b6SAdrian Ambrożewicz { 249c1d019a6SEd Tanous map.emplace(sensor.uri, sensor.dbusPath); 250a0ec28b6SAdrian Ambrożewicz } 251a0ec28b6SAdrian Ambrożewicz } 2528d1b46d7Szhanghch05 dataComplete(asyncResp->res.result(), map); 253a0ec28b6SAdrian Ambrożewicz } 25408777fb0SLewanczyk, Dawid } 255588c3f0dSKowalski, Kamil 256ecd6a3a2SEd Tanous SensorsAsyncResp(const SensorsAsyncResp&) = delete; 257ecd6a3a2SEd Tanous SensorsAsyncResp(SensorsAsyncResp&&) = delete; 258ecd6a3a2SEd Tanous SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete; 259ecd6a3a2SEd Tanous SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete; 260ecd6a3a2SEd Tanous 261a0ec28b6SAdrian Ambrożewicz void addMetadata(const nlohmann::json& sensorObject, 262c1d019a6SEd Tanous const std::string& dbusPath) 263a0ec28b6SAdrian Ambrożewicz { 264a0ec28b6SAdrian Ambrożewicz if (metadata) 265a0ec28b6SAdrian Ambrożewicz { 266c1d019a6SEd Tanous metadata->emplace_back(SensorData{ 267c1d019a6SEd Tanous sensorObject["Name"], sensorObject["@odata.id"], dbusPath}); 268a0ec28b6SAdrian Ambrożewicz } 269a0ec28b6SAdrian Ambrożewicz } 270a0ec28b6SAdrian Ambrożewicz 271a0ec28b6SAdrian Ambrożewicz void updateUri(const std::string& name, const std::string& uri) 272a0ec28b6SAdrian Ambrożewicz { 273a0ec28b6SAdrian Ambrożewicz if (metadata) 274a0ec28b6SAdrian Ambrożewicz { 275a0ec28b6SAdrian Ambrożewicz for (auto& sensor : *metadata) 276a0ec28b6SAdrian Ambrożewicz { 277a0ec28b6SAdrian Ambrożewicz if (sensor.name == name) 278a0ec28b6SAdrian Ambrożewicz { 279a0ec28b6SAdrian Ambrożewicz sensor.uri = uri; 280a0ec28b6SAdrian Ambrożewicz } 281a0ec28b6SAdrian Ambrożewicz } 282a0ec28b6SAdrian Ambrożewicz } 283a0ec28b6SAdrian Ambrożewicz } 284a0ec28b6SAdrian Ambrożewicz 2858d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp> asyncResp; 286a0ec28b6SAdrian Ambrożewicz const std::string chassisId; 28702da7c5aSEd Tanous const std::span<std::string_view> types; 288a0ec28b6SAdrian Ambrożewicz const std::string chassisSubNode; 289928fefb9SNan Zhou const bool efficientExpand; 290a0ec28b6SAdrian Ambrożewicz 291a0ec28b6SAdrian Ambrożewicz private: 292a0ec28b6SAdrian Ambrożewicz std::optional<std::vector<SensorData>> metadata; 293a0ec28b6SAdrian Ambrożewicz DataCompleteCb dataComplete; 29408777fb0SLewanczyk, Dawid }; 29508777fb0SLewanczyk, Dawid 29608777fb0SLewanczyk, Dawid /** 297d500549bSAnthony Wilson * Possible states for physical inventory leds 298d500549bSAnthony Wilson */ 299d500549bSAnthony Wilson enum class LedState 300d500549bSAnthony Wilson { 301d500549bSAnthony Wilson OFF, 302d500549bSAnthony Wilson ON, 303d500549bSAnthony Wilson BLINK, 304d500549bSAnthony Wilson UNKNOWN 305d500549bSAnthony Wilson }; 306d500549bSAnthony Wilson 307d500549bSAnthony Wilson /** 308adc4f0dbSShawn McCarney * D-Bus inventory item associated with one or more sensors. 309adc4f0dbSShawn McCarney */ 310adc4f0dbSShawn McCarney class InventoryItem 311adc4f0dbSShawn McCarney { 312adc4f0dbSShawn McCarney public: 3134e23a444SEd Tanous explicit InventoryItem(const std::string& objPath) : objectPath(objPath) 314adc4f0dbSShawn McCarney { 315adc4f0dbSShawn McCarney // Set inventory item name to last node of object path 31628aa8de5SGeorge Liu sdbusplus::message::object_path path(objectPath); 31728aa8de5SGeorge Liu name = path.filename(); 31828aa8de5SGeorge Liu if (name.empty()) 319adc4f0dbSShawn McCarney { 32028aa8de5SGeorge Liu BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath; 321adc4f0dbSShawn McCarney } 322adc4f0dbSShawn McCarney } 323adc4f0dbSShawn McCarney 324adc4f0dbSShawn McCarney std::string objectPath; 325adc4f0dbSShawn McCarney std::string name; 326e05aec50SEd Tanous bool isPresent = true; 327e05aec50SEd Tanous bool isFunctional = true; 328e05aec50SEd Tanous bool isPowerSupply = false; 329e05aec50SEd Tanous int powerSupplyEfficiencyPercent = -1; 330adc4f0dbSShawn McCarney std::string manufacturer; 331adc4f0dbSShawn McCarney std::string model; 332adc4f0dbSShawn McCarney std::string partNumber; 333adc4f0dbSShawn McCarney std::string serialNumber; 334adc4f0dbSShawn McCarney std::set<std::string> sensors; 335d500549bSAnthony Wilson std::string ledObjectPath; 336e05aec50SEd Tanous LedState ledState = LedState::UNKNOWN; 337adc4f0dbSShawn McCarney }; 338adc4f0dbSShawn McCarney 339adc4f0dbSShawn McCarney /** 340413961deSRichard Marian Thomaiyar * @brief Get objects with connection necessary for sensors 341588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 34208777fb0SLewanczyk, Dawid * @param sensorNames Sensors retrieved from chassis 34308777fb0SLewanczyk, Dawid * @param callback Callback for processing gathered connections 34408777fb0SLewanczyk, Dawid */ 34508777fb0SLewanczyk, Dawid template <typename Callback> 346413961deSRichard Marian Thomaiyar void getObjectsWithConnection( 34781ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 348fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames, 3491abe55efSEd Tanous Callback&& callback) 3501abe55efSEd Tanous { 351413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter"; 35203b5bae3SJames Feist const std::string path = "/xyz/openbmc_project/sensors"; 35308777fb0SLewanczyk, Dawid const std::array<std::string, 1> interfaces = { 35408777fb0SLewanczyk, Dawid "xyz.openbmc_project.Sensor.Value"}; 35508777fb0SLewanczyk, Dawid 35608777fb0SLewanczyk, Dawid // Response handler for parsing objects subtree 357002d39b4SEd Tanous auto respHandler = 358002d39b4SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 359002d39b4SEd Tanous sensorNames](const boost::system::error_code ec, 360002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 361413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter"; 3621abe55efSEd Tanous if (ec) 3631abe55efSEd Tanous { 3648d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 365413961deSRichard Marian Thomaiyar BMCWEB_LOG_ERROR 366413961deSRichard Marian Thomaiyar << "getObjectsWithConnection resp_handler: Dbus error " << ec; 36708777fb0SLewanczyk, Dawid return; 36808777fb0SLewanczyk, Dawid } 36908777fb0SLewanczyk, Dawid 37055c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees"; 37108777fb0SLewanczyk, Dawid 37208777fb0SLewanczyk, Dawid // Make unique list of connections only for requested sensor types and 37308777fb0SLewanczyk, Dawid // found in the chassis 374fe04d49cSNan Zhou std::set<std::string> connections; 375413961deSRichard Marian Thomaiyar std::set<std::pair<std::string, std::string>> objectsWithConnection; 37608777fb0SLewanczyk, Dawid 37749c53ac9SJohnathan Mantey BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size(); 37849c53ac9SJohnathan Mantey for (const std::string& tsensor : *sensorNames) 3791abe55efSEd Tanous { 38055c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor; 38108777fb0SLewanczyk, Dawid } 38208777fb0SLewanczyk, Dawid 38308777fb0SLewanczyk, Dawid for (const std::pair< 38408777fb0SLewanczyk, Dawid std::string, 38508777fb0SLewanczyk, Dawid std::vector<std::pair<std::string, std::vector<std::string>>>>& 3861abe55efSEd Tanous object : subtree) 3871abe55efSEd Tanous { 38849c53ac9SJohnathan Mantey if (sensorNames->find(object.first) != sensorNames->end()) 3891abe55efSEd Tanous { 39049c53ac9SJohnathan Mantey for (const std::pair<std::string, std::vector<std::string>>& 3911abe55efSEd Tanous objData : object.second) 3921abe55efSEd Tanous { 39349c53ac9SJohnathan Mantey BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first; 39408777fb0SLewanczyk, Dawid connections.insert(objData.first); 395de629b6eSShawn McCarney objectsWithConnection.insert( 396de629b6eSShawn McCarney std::make_pair(object.first, objData.first)); 39708777fb0SLewanczyk, Dawid } 39808777fb0SLewanczyk, Dawid } 39908777fb0SLewanczyk, Dawid } 40055c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections"; 401413961deSRichard Marian Thomaiyar callback(std::move(connections), std::move(objectsWithConnection)); 402413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit"; 40308777fb0SLewanczyk, Dawid }; 40408777fb0SLewanczyk, Dawid // Make call to ObjectMapper to find all sensors objects 40555c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 40655c7b7a2SEd Tanous std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 4071abe55efSEd Tanous "/xyz/openbmc_project/object_mapper", 4081abe55efSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces); 409413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit"; 410413961deSRichard Marian Thomaiyar } 411413961deSRichard Marian Thomaiyar 412413961deSRichard Marian Thomaiyar /** 413413961deSRichard Marian Thomaiyar * @brief Create connections necessary for sensors 414413961deSRichard Marian Thomaiyar * @param SensorsAsyncResp Pointer to object holding response data 415413961deSRichard Marian Thomaiyar * @param sensorNames Sensors retrieved from chassis 416413961deSRichard Marian Thomaiyar * @param callback Callback for processing gathered connections 417413961deSRichard Marian Thomaiyar */ 418413961deSRichard Marian Thomaiyar template <typename Callback> 419fe04d49cSNan Zhou void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 420fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> sensorNames, 421413961deSRichard Marian Thomaiyar Callback&& callback) 422413961deSRichard Marian Thomaiyar { 423413961deSRichard Marian Thomaiyar auto objectsWithConnectionCb = 424fe04d49cSNan Zhou [callback](const std::set<std::string>& connections, 425413961deSRichard Marian Thomaiyar const std::set<std::pair<std::string, std::string>>& 4263174e4dfSEd Tanous /*objectsWithConnection*/) { callback(connections); }; 42781ce609eSEd Tanous getObjectsWithConnection(sensorsAsyncResp, sensorNames, 428413961deSRichard Marian Thomaiyar std::move(objectsWithConnectionCb)); 42908777fb0SLewanczyk, Dawid } 43008777fb0SLewanczyk, Dawid 43108777fb0SLewanczyk, Dawid /** 43249c53ac9SJohnathan Mantey * @brief Shrinks the list of sensors for processing 43349c53ac9SJohnathan Mantey * @param SensorsAysncResp The class holding the Redfish response 43449c53ac9SJohnathan Mantey * @param allSensors A list of all the sensors associated to the 43549c53ac9SJohnathan Mantey * chassis element (i.e. baseboard, front panel, etc...) 43649c53ac9SJohnathan Mantey * @param activeSensors A list that is a reduction of the incoming 43749c53ac9SJohnathan Mantey * allSensors list. Eliminate Thermal sensors when a Power request is 43849c53ac9SJohnathan Mantey * made, and eliminate Power sensors when a Thermal request is made. 43949c53ac9SJohnathan Mantey */ 44023a21a1cSEd Tanous inline void reduceSensorList( 4417f1cc26dSEd Tanous crow::Response& res, std::string_view chassisSubNode, 4427f1cc26dSEd Tanous std::span<std::string_view> sensorTypes, 44349c53ac9SJohnathan Mantey const std::vector<std::string>* allSensors, 444fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& activeSensors) 44549c53ac9SJohnathan Mantey { 44649c53ac9SJohnathan Mantey if ((allSensors == nullptr) || (activeSensors == nullptr)) 44749c53ac9SJohnathan Mantey { 4487f1cc26dSEd Tanous messages::resourceNotFound(res, chassisSubNode, 4497f1cc26dSEd Tanous chassisSubNode == sensors::node::thermal 450a0ec28b6SAdrian Ambrożewicz ? "Temperatures" 45149c53ac9SJohnathan Mantey : "Voltages"); 45249c53ac9SJohnathan Mantey 45349c53ac9SJohnathan Mantey return; 45449c53ac9SJohnathan Mantey } 45549c53ac9SJohnathan Mantey if (allSensors->empty()) 45649c53ac9SJohnathan Mantey { 45749c53ac9SJohnathan Mantey // Nothing to do, the activeSensors object is also empty 45849c53ac9SJohnathan Mantey return; 45949c53ac9SJohnathan Mantey } 46049c53ac9SJohnathan Mantey 4617f1cc26dSEd Tanous for (std::string_view type : sensorTypes) 46249c53ac9SJohnathan Mantey { 46349c53ac9SJohnathan Mantey for (const std::string& sensor : *allSensors) 46449c53ac9SJohnathan Mantey { 46511ba3979SEd Tanous if (sensor.starts_with(type)) 46649c53ac9SJohnathan Mantey { 46749c53ac9SJohnathan Mantey activeSensors->emplace(sensor); 46849c53ac9SJohnathan Mantey } 46949c53ac9SJohnathan Mantey } 47049c53ac9SJohnathan Mantey } 47149c53ac9SJohnathan Mantey } 47249c53ac9SJohnathan Mantey 4737f1cc26dSEd Tanous /* 4747f1cc26dSEd Tanous *Populates the top level collection for a given subnode. Populates 4757f1cc26dSEd Tanous *SensorCollection, Power, or Thermal schemas. 4767f1cc26dSEd Tanous * 4777f1cc26dSEd Tanous * */ 4787f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue, 4797f1cc26dSEd Tanous std::string_view chassisSubNode) 4807f1cc26dSEd Tanous { 4817f1cc26dSEd Tanous if (chassisSubNode == sensors::node::power) 4827f1cc26dSEd Tanous { 4837f1cc26dSEd Tanous jsonValue["@odata.type"] = "#Power.v1_5_2.Power"; 4847f1cc26dSEd Tanous } 4857f1cc26dSEd Tanous else if (chassisSubNode == sensors::node::thermal) 4867f1cc26dSEd Tanous { 4877f1cc26dSEd Tanous jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal"; 4887f1cc26dSEd Tanous jsonValue["Fans"] = nlohmann::json::array(); 4897f1cc26dSEd Tanous jsonValue["Temperatures"] = nlohmann::json::array(); 4907f1cc26dSEd Tanous } 4917f1cc26dSEd Tanous else if (chassisSubNode == sensors::node::sensors) 4927f1cc26dSEd Tanous { 4937f1cc26dSEd Tanous jsonValue["@odata.type"] = "#SensorCollection.SensorCollection"; 4947f1cc26dSEd Tanous jsonValue["Description"] = "Collection of Sensors for this Chassis"; 4957f1cc26dSEd Tanous jsonValue["Members"] = nlohmann::json::array(); 4967f1cc26dSEd Tanous jsonValue["Members@odata.count"] = 0; 4977f1cc26dSEd Tanous } 4987f1cc26dSEd Tanous 4997f1cc26dSEd Tanous if (chassisSubNode != sensors::node::sensors) 5007f1cc26dSEd Tanous { 5017f1cc26dSEd Tanous jsonValue["Id"] = chassisSubNode; 5027f1cc26dSEd Tanous } 5037f1cc26dSEd Tanous jsonValue["Name"] = chassisSubNode; 5047f1cc26dSEd Tanous } 5057f1cc26dSEd Tanous 50649c53ac9SJohnathan Mantey /** 50708777fb0SLewanczyk, Dawid * @brief Retrieves requested chassis sensors and redundancy data from DBus . 508588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 50908777fb0SLewanczyk, Dawid * @param callback Callback for next step in gathered sensor processing 51008777fb0SLewanczyk, Dawid */ 51108777fb0SLewanczyk, Dawid template <typename Callback> 5127f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 5137f1cc26dSEd Tanous std::string_view chassisId, std::string_view chassisSubNode, 5147f1cc26dSEd Tanous std::span<std::string_view> sensorTypes, Callback&& callback) 5151abe55efSEd Tanous { 51655c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis enter"; 517adc4f0dbSShawn McCarney const std::array<const char*, 2> interfaces = { 51849c53ac9SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Board", 519adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.Chassis"}; 520002d39b4SEd Tanous auto respHandler = 5217f1cc26dSEd Tanous [callback{std::forward<Callback>(callback)}, asyncResp, 5227f1cc26dSEd Tanous chassisIdStr{std::string(chassisId)}, 5237f1cc26dSEd Tanous chassisSubNode{std::string(chassisSubNode)}, sensorTypes]( 52449c53ac9SJohnathan Mantey const boost::system::error_code ec, 525002d39b4SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) { 52655c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis respHandler enter"; 5271abe55efSEd Tanous if (ec) 5281abe55efSEd Tanous { 52955c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec; 5307f1cc26dSEd Tanous messages::internalError(asyncResp->res); 53108777fb0SLewanczyk, Dawid return; 53208777fb0SLewanczyk, Dawid } 53349c53ac9SJohnathan Mantey const std::string* chassisPath = nullptr; 53449c53ac9SJohnathan Mantey std::string chassisName; 53549c53ac9SJohnathan Mantey for (const std::string& chassis : chassisPaths) 5361abe55efSEd Tanous { 53728aa8de5SGeorge Liu sdbusplus::message::object_path path(chassis); 53828aa8de5SGeorge Liu chassisName = path.filename(); 53928aa8de5SGeorge Liu if (chassisName.empty()) 5401abe55efSEd Tanous { 54149c53ac9SJohnathan Mantey BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis; 542daf36e2eSEd Tanous continue; 543daf36e2eSEd Tanous } 5447f1cc26dSEd Tanous if (chassisName == chassisIdStr) 5451abe55efSEd Tanous { 54649c53ac9SJohnathan Mantey chassisPath = &chassis; 54749c53ac9SJohnathan Mantey break; 548daf36e2eSEd Tanous } 54949c53ac9SJohnathan Mantey } 55049c53ac9SJohnathan Mantey if (chassisPath == nullptr) 5511abe55efSEd Tanous { 5527f1cc26dSEd Tanous messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr); 55349c53ac9SJohnathan Mantey return; 5541abe55efSEd Tanous } 5557f1cc26dSEd Tanous populateChassisNode(asyncResp->res.jsonValue, chassisSubNode); 55608777fb0SLewanczyk, Dawid 5577f1cc26dSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 5587f1cc26dSEd Tanous "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode; 55995a3ecadSAnthony Wilson 5608fb49dd6SShawn McCarney // Get the list of all sensors for this Chassis element 5618fb49dd6SShawn McCarney std::string sensorPath = *chassisPath + "/all_sensors"; 5621e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::vector<std::string>>( 5631e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper", 5641e1e598dSJonathan Doman sensorPath, "xyz.openbmc_project.Association", "endpoints", 5657f1cc26dSEd Tanous [asyncResp, chassisSubNode, sensorTypes, 566f94c4ecfSEd Tanous callback{std::forward<const Callback>(callback)}]( 567271584abSEd Tanous const boost::system::error_code& e, 5681e1e598dSJonathan Doman const std::vector<std::string>& nodeSensorList) { 569271584abSEd Tanous if (e) 57049c53ac9SJohnathan Mantey { 571271584abSEd Tanous if (e.value() != EBADR) 57249c53ac9SJohnathan Mantey { 5737f1cc26dSEd Tanous messages::internalError(asyncResp->res); 57449c53ac9SJohnathan Mantey return; 57549c53ac9SJohnathan Mantey } 57649c53ac9SJohnathan Mantey } 577fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> culledSensorList = 578fe04d49cSNan Zhou std::make_shared<std::set<std::string>>(); 5797f1cc26dSEd Tanous reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes, 5807f1cc26dSEd Tanous &nodeSensorList, culledSensorList); 5817f1cc26dSEd Tanous BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size(); 58249c53ac9SJohnathan Mantey callback(culledSensorList); 5831e1e598dSJonathan Doman }); 58449c53ac9SJohnathan Mantey }; 58549c53ac9SJohnathan Mantey 58649c53ac9SJohnathan Mantey // Get the Chassis Collection 58749c53ac9SJohnathan Mantey crow::connections::systemBus->async_method_call( 58849c53ac9SJohnathan Mantey respHandler, "xyz.openbmc_project.ObjectMapper", 58949c53ac9SJohnathan Mantey "/xyz/openbmc_project/object_mapper", 59049c53ac9SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 591271584abSEd Tanous "/xyz/openbmc_project/inventory", 0, interfaces); 59255c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis exit"; 59308777fb0SLewanczyk, Dawid } 59408777fb0SLewanczyk, Dawid 59508777fb0SLewanczyk, Dawid /** 596de629b6eSShawn McCarney * @brief Finds all DBus object paths that implement ObjectManager. 597de629b6eSShawn McCarney * 598de629b6eSShawn McCarney * Creates a mapping from the associated connection name to the object path. 599de629b6eSShawn McCarney * 600de629b6eSShawn McCarney * Finds the object paths asynchronously. Invokes callback when information has 601de629b6eSShawn McCarney * been obtained. 602de629b6eSShawn McCarney * 603de629b6eSShawn McCarney * The callback must have the following signature: 604de629b6eSShawn McCarney * @code 605fe04d49cSNan Zhou * callback(std::shared_ptr<std::map<std::string,std::string>> objectMgrPaths) 606de629b6eSShawn McCarney * @endcode 607de629b6eSShawn McCarney * 60849c53ac9SJohnathan Mantey * @param sensorsAsyncResp Pointer to object holding response data. 609de629b6eSShawn McCarney * @param callback Callback to invoke when object paths obtained. 610de629b6eSShawn McCarney */ 611de629b6eSShawn McCarney template <typename Callback> 612b5a76932SEd Tanous void getObjectManagerPaths( 61381ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 614de629b6eSShawn McCarney Callback&& callback) 615de629b6eSShawn McCarney { 616de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter"; 617de629b6eSShawn McCarney const std::array<std::string, 1> interfaces = { 618de629b6eSShawn McCarney "org.freedesktop.DBus.ObjectManager"}; 619de629b6eSShawn McCarney 620de629b6eSShawn McCarney // Response handler for GetSubTree DBus method 621002d39b4SEd Tanous auto respHandler = 622002d39b4SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp]( 623b9d36b47SEd Tanous const boost::system::error_code ec, 624002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 625de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter"; 626de629b6eSShawn McCarney if (ec) 627de629b6eSShawn McCarney { 6288d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 629de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error " 630de629b6eSShawn McCarney << ec; 631de629b6eSShawn McCarney return; 632de629b6eSShawn McCarney } 633de629b6eSShawn McCarney 634de629b6eSShawn McCarney // Loop over returned object paths 635fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths = 636fe04d49cSNan Zhou std::make_shared<std::map<std::string, std::string>>(); 637de629b6eSShawn McCarney for (const std::pair< 638de629b6eSShawn McCarney std::string, 639de629b6eSShawn McCarney std::vector<std::pair<std::string, std::vector<std::string>>>>& 640de629b6eSShawn McCarney object : subtree) 641de629b6eSShawn McCarney { 642de629b6eSShawn McCarney // Loop over connections for current object path 643de629b6eSShawn McCarney const std::string& objectPath = object.first; 644de629b6eSShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 645de629b6eSShawn McCarney objData : object.second) 646de629b6eSShawn McCarney { 647de629b6eSShawn McCarney // Add mapping from connection to object path 648de629b6eSShawn McCarney const std::string& connection = objData.first; 6498fb49dd6SShawn McCarney (*objectMgrPaths)[connection] = objectPath; 650de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> " 651de629b6eSShawn McCarney << objectPath; 652de629b6eSShawn McCarney } 653de629b6eSShawn McCarney } 6548fb49dd6SShawn McCarney callback(objectMgrPaths); 655de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit"; 656de629b6eSShawn McCarney }; 657de629b6eSShawn McCarney 658de629b6eSShawn McCarney // Query mapper for all DBus object paths that implement ObjectManager 659de629b6eSShawn McCarney crow::connections::systemBus->async_method_call( 660de629b6eSShawn McCarney std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 661de629b6eSShawn McCarney "/xyz/openbmc_project/object_mapper", 662271584abSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces); 663de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit"; 664de629b6eSShawn McCarney } 665de629b6eSShawn McCarney 666de629b6eSShawn McCarney /** 667adc4f0dbSShawn McCarney * @brief Returns the Redfish State value for the specified inventory item. 668adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with a sensor. 669adc4f0dbSShawn McCarney * @return State value for inventory item. 67034dd179eSJames Feist */ 67123a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem) 672adc4f0dbSShawn McCarney { 673adc4f0dbSShawn McCarney if ((inventoryItem != nullptr) && !(inventoryItem->isPresent)) 674adc4f0dbSShawn McCarney { 675adc4f0dbSShawn McCarney return "Absent"; 676adc4f0dbSShawn McCarney } 67734dd179eSJames Feist 678adc4f0dbSShawn McCarney return "Enabled"; 679adc4f0dbSShawn McCarney } 680adc4f0dbSShawn McCarney 681adc4f0dbSShawn McCarney /** 682adc4f0dbSShawn McCarney * @brief Returns the Redfish Health value for the specified sensor. 683adc4f0dbSShawn McCarney * @param sensorJson Sensor JSON object. 6841d7c0054SEd Tanous * @param valuesDict Map of all sensor DBus values. 685adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with the sensor. Will 686adc4f0dbSShawn McCarney * be nullptr if no associated inventory item was found. 687adc4f0dbSShawn McCarney * @return Health value for sensor. 688adc4f0dbSShawn McCarney */ 6891d7c0054SEd Tanous inline std::string getHealth(nlohmann::json& sensorJson, 6901d7c0054SEd Tanous const dbus::utility::DBusPropertiesMap& valuesDict, 691adc4f0dbSShawn McCarney const InventoryItem* inventoryItem) 69234dd179eSJames Feist { 693adc4f0dbSShawn McCarney // Get current health value (if any) in the sensor JSON object. Some JSON 694adc4f0dbSShawn McCarney // objects contain multiple sensors (such as PowerSupplies). We want to set 695adc4f0dbSShawn McCarney // the overall health to be the most severe of any of the sensors. 696adc4f0dbSShawn McCarney std::string currentHealth; 697adc4f0dbSShawn McCarney auto statusIt = sensorJson.find("Status"); 698adc4f0dbSShawn McCarney if (statusIt != sensorJson.end()) 699adc4f0dbSShawn McCarney { 700adc4f0dbSShawn McCarney auto healthIt = statusIt->find("Health"); 701adc4f0dbSShawn McCarney if (healthIt != statusIt->end()) 702adc4f0dbSShawn McCarney { 703adc4f0dbSShawn McCarney std::string* health = healthIt->get_ptr<std::string*>(); 704adc4f0dbSShawn McCarney if (health != nullptr) 705adc4f0dbSShawn McCarney { 706adc4f0dbSShawn McCarney currentHealth = *health; 707adc4f0dbSShawn McCarney } 708adc4f0dbSShawn McCarney } 709adc4f0dbSShawn McCarney } 710adc4f0dbSShawn McCarney 711adc4f0dbSShawn McCarney // If current health in JSON object is already Critical, return that. This 712adc4f0dbSShawn McCarney // should override the sensor health, which might be less severe. 713adc4f0dbSShawn McCarney if (currentHealth == "Critical") 714adc4f0dbSShawn McCarney { 715adc4f0dbSShawn McCarney return "Critical"; 716adc4f0dbSShawn McCarney } 717adc4f0dbSShawn McCarney 718c1343bf6SKrzysztof Grobelny const bool* criticalAlarmHigh = nullptr; 719c1343bf6SKrzysztof Grobelny const bool* criticalAlarmLow = nullptr; 720c1343bf6SKrzysztof Grobelny const bool* warningAlarmHigh = nullptr; 721c1343bf6SKrzysztof Grobelny const bool* warningAlarmLow = nullptr; 722711ac7a9SEd Tanous 723c1343bf6SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 724c1343bf6SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh", 725c1343bf6SKrzysztof Grobelny criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow, 726c1343bf6SKrzysztof Grobelny "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow", 727c1343bf6SKrzysztof Grobelny warningAlarmLow); 728c1343bf6SKrzysztof Grobelny 729c1343bf6SKrzysztof Grobelny if (success) 73034dd179eSJames Feist { 731c1343bf6SKrzysztof Grobelny // Check if sensor has critical threshold alarm 732c1343bf6SKrzysztof Grobelny if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) || 733c1343bf6SKrzysztof Grobelny (criticalAlarmLow != nullptr && *criticalAlarmLow)) 73434dd179eSJames Feist { 73534dd179eSJames Feist return "Critical"; 73634dd179eSJames Feist } 73734dd179eSJames Feist } 73834dd179eSJames Feist 739adc4f0dbSShawn McCarney // Check if associated inventory item is not functional 740adc4f0dbSShawn McCarney if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional)) 741adc4f0dbSShawn McCarney { 742adc4f0dbSShawn McCarney return "Critical"; 743adc4f0dbSShawn McCarney } 744adc4f0dbSShawn McCarney 745adc4f0dbSShawn McCarney // If current health in JSON object is already Warning, return that. This 746adc4f0dbSShawn McCarney // should override the sensor status, which might be less severe. 747adc4f0dbSShawn McCarney if (currentHealth == "Warning") 748adc4f0dbSShawn McCarney { 749adc4f0dbSShawn McCarney return "Warning"; 750adc4f0dbSShawn McCarney } 751adc4f0dbSShawn McCarney 752c1343bf6SKrzysztof Grobelny if (success) 753c1343bf6SKrzysztof Grobelny { 754adc4f0dbSShawn McCarney // Check if sensor has warning threshold alarm 755c1343bf6SKrzysztof Grobelny if ((warningAlarmHigh != nullptr && *warningAlarmHigh) || 756c1343bf6SKrzysztof Grobelny (warningAlarmLow != nullptr && *warningAlarmLow)) 75734dd179eSJames Feist { 758ebe4d91eSEd Tanous return "Warning"; 75934dd179eSJames Feist } 76034dd179eSJames Feist } 761adc4f0dbSShawn McCarney 76234dd179eSJames Feist return "OK"; 76334dd179eSJames Feist } 76434dd179eSJames Feist 76523a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson, 766d500549bSAnthony Wilson const InventoryItem* inventoryItem) 767d500549bSAnthony Wilson { 768d500549bSAnthony Wilson if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty()) 769d500549bSAnthony Wilson { 770d500549bSAnthony Wilson switch (inventoryItem->ledState) 771d500549bSAnthony Wilson { 772d500549bSAnthony Wilson case LedState::OFF: 773d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Off"; 774d500549bSAnthony Wilson break; 775d500549bSAnthony Wilson case LedState::ON: 776d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Lit"; 777d500549bSAnthony Wilson break; 778d500549bSAnthony Wilson case LedState::BLINK: 779d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Blinking"; 780d500549bSAnthony Wilson break; 78123a21a1cSEd Tanous case LedState::UNKNOWN: 782d500549bSAnthony Wilson break; 783d500549bSAnthony Wilson } 784d500549bSAnthony Wilson } 785d500549bSAnthony Wilson } 786d500549bSAnthony Wilson 78734dd179eSJames Feist /** 78808777fb0SLewanczyk, Dawid * @brief Builds a json sensor representation of a sensor. 78908777fb0SLewanczyk, Dawid * @param sensorName The name of the sensor to be built 790274fad5aSGunnar Mills * @param sensorType The type (temperature, fan_tach, etc) of the sensor to 79108777fb0SLewanczyk, Dawid * build 7921d7c0054SEd Tanous * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor 7931d7c0054SEd Tanous * @param propertiesDict A dictionary of the properties to build the sensor 7941d7c0054SEd Tanous * from. 7951d7c0054SEd Tanous * @param sensorJson The json object to fill 796adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with the sensor. Will 797adc4f0dbSShawn McCarney * be nullptr if no associated inventory item was found. 79808777fb0SLewanczyk, Dawid */ 7991d7c0054SEd Tanous inline void objectPropertiesToJson( 8001d7c0054SEd Tanous std::string_view sensorName, std::string_view sensorType, 8011d7c0054SEd Tanous std::string_view chassisSubNode, 8021d7c0054SEd Tanous const dbus::utility::DBusPropertiesMap& propertiesDict, 80381ce609eSEd Tanous nlohmann::json& sensorJson, InventoryItem* inventoryItem) 8041abe55efSEd Tanous { 8051d7c0054SEd Tanous if (chassisSubNode == sensors::node::sensors) 806adc4f0dbSShawn McCarney { 807c1d019a6SEd Tanous std::string subNodeEscaped(chassisSubNode); 808c1d019a6SEd Tanous subNodeEscaped.erase( 809c1d019a6SEd Tanous std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'), 810c1d019a6SEd Tanous subNodeEscaped.end()); 811c1d019a6SEd Tanous 812c1d019a6SEd Tanous // For sensors in SensorCollection we set Id instead of MemberId, 813c1d019a6SEd Tanous // including power sensors. 814c1d019a6SEd Tanous subNodeEscaped += '_'; 815c1d019a6SEd Tanous subNodeEscaped += sensorName; 816c1d019a6SEd Tanous sensorJson["Id"] = std::move(subNodeEscaped); 817c1d019a6SEd Tanous 8181d7c0054SEd Tanous std::string sensorNameEs(sensorName); 8191d7c0054SEd Tanous std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' '); 8201d7c0054SEd Tanous sensorJson["Name"] = std::move(sensorNameEs); 82195a3ecadSAnthony Wilson } 82295a3ecadSAnthony Wilson else if (sensorType != "power") 82395a3ecadSAnthony Wilson { 82495a3ecadSAnthony Wilson // Set MemberId and Name for non-power sensors. For PowerSupplies and 82595a3ecadSAnthony Wilson // PowerControl, those properties have more general values because 82695a3ecadSAnthony Wilson // multiple sensors can be stored in the same JSON object. 82781ce609eSEd Tanous sensorJson["MemberId"] = sensorName; 8281d7c0054SEd Tanous std::string sensorNameEs(sensorName); 8291d7c0054SEd Tanous std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' '); 8301d7c0054SEd Tanous sensorJson["Name"] = std::move(sensorNameEs); 831adc4f0dbSShawn McCarney } 832e742b6ccSEd Tanous 83381ce609eSEd Tanous sensorJson["Status"]["State"] = getState(inventoryItem); 83481ce609eSEd Tanous sensorJson["Status"]["Health"] = 8351d7c0054SEd Tanous getHealth(sensorJson, propertiesDict, inventoryItem); 83608777fb0SLewanczyk, Dawid 83708777fb0SLewanczyk, Dawid // Parameter to set to override the type we get from dbus, and force it to 83808777fb0SLewanczyk, Dawid // int, regardless of what is available. This is used for schemas like fan, 83908777fb0SLewanczyk, Dawid // that require integers, not floats. 84008777fb0SLewanczyk, Dawid bool forceToInt = false; 84108777fb0SLewanczyk, Dawid 8423929aca1SAnthony Wilson nlohmann::json::json_pointer unit("/Reading"); 8431d7c0054SEd Tanous if (chassisSubNode == sensors::node::sensors) 84495a3ecadSAnthony Wilson { 8452a4ba195SShounak Mitra sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor"; 846c2bf7f99SWludzik, Jozef 847*0ec8b83dSEd Tanous sensor::ReadingType readingType = sensors::toReadingType(sensorType); 848*0ec8b83dSEd Tanous if (readingType == sensor::ReadingType::Invalid) 84995a3ecadSAnthony Wilson { 850c2bf7f99SWludzik, Jozef BMCWEB_LOG_ERROR << "Redfish cannot map reading type for " 851c2bf7f99SWludzik, Jozef << sensorType; 85295a3ecadSAnthony Wilson } 853c2bf7f99SWludzik, Jozef else 85495a3ecadSAnthony Wilson { 855c2bf7f99SWludzik, Jozef sensorJson["ReadingType"] = readingType; 85695a3ecadSAnthony Wilson } 857c2bf7f99SWludzik, Jozef 8581d7c0054SEd Tanous std::string_view readingUnits = sensors::toReadingUnits(sensorType); 859c2bf7f99SWludzik, Jozef if (readingUnits.empty()) 860f8ede15eSAdrian Ambrożewicz { 861c2bf7f99SWludzik, Jozef BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for " 862c2bf7f99SWludzik, Jozef << sensorType; 863c2bf7f99SWludzik, Jozef } 864c2bf7f99SWludzik, Jozef else 865c2bf7f99SWludzik, Jozef { 866c2bf7f99SWludzik, Jozef sensorJson["ReadingUnits"] = readingUnits; 867f8ede15eSAdrian Ambrożewicz } 86895a3ecadSAnthony Wilson } 86995a3ecadSAnthony Wilson else if (sensorType == "temperature") 8701abe55efSEd Tanous { 8713929aca1SAnthony Wilson unit = "/ReadingCelsius"_json_pointer; 87281ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature"; 87308777fb0SLewanczyk, Dawid // TODO(ed) Documentation says that path should be type fan_tach, 87408777fb0SLewanczyk, Dawid // implementation seems to implement fan 8751abe55efSEd Tanous } 8761abe55efSEd Tanous else if (sensorType == "fan" || sensorType == "fan_tach") 8771abe55efSEd Tanous { 8783929aca1SAnthony Wilson unit = "/Reading"_json_pointer; 87981ce609eSEd Tanous sensorJson["ReadingUnits"] = "RPM"; 88081ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan"; 88181ce609eSEd Tanous setLedState(sensorJson, inventoryItem); 88208777fb0SLewanczyk, Dawid forceToInt = true; 8831abe55efSEd Tanous } 8846f6d0d32SEd Tanous else if (sensorType == "fan_pwm") 8856f6d0d32SEd Tanous { 8863929aca1SAnthony Wilson unit = "/Reading"_json_pointer; 88781ce609eSEd Tanous sensorJson["ReadingUnits"] = "Percent"; 88881ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan"; 88981ce609eSEd Tanous setLedState(sensorJson, inventoryItem); 8906f6d0d32SEd Tanous forceToInt = true; 8916f6d0d32SEd Tanous } 8921abe55efSEd Tanous else if (sensorType == "voltage") 8931abe55efSEd Tanous { 8943929aca1SAnthony Wilson unit = "/ReadingVolts"_json_pointer; 89581ce609eSEd Tanous sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage"; 8961abe55efSEd Tanous } 8972474adfaSEd Tanous else if (sensorType == "power") 8982474adfaSEd Tanous { 8991d7c0054SEd Tanous if (boost::iequals(sensorName, "total_power")) 900028f7ebcSEddie James { 90181ce609eSEd Tanous sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl"; 9027ab06f49SGunnar Mills // Put multiple "sensors" into a single PowerControl, so have 9037ab06f49SGunnar Mills // generic names for MemberId and Name. Follows Redfish mockup. 90481ce609eSEd Tanous sensorJson["MemberId"] = "0"; 90581ce609eSEd Tanous sensorJson["Name"] = "Chassis Power Control"; 9063929aca1SAnthony Wilson unit = "/PowerConsumedWatts"_json_pointer; 907028f7ebcSEddie James } 9081d7c0054SEd Tanous else if (boost::ifind_first(sensorName, "input").empty()) 90949c53ac9SJohnathan Mantey { 9103929aca1SAnthony Wilson unit = "/PowerInputWatts"_json_pointer; 91149c53ac9SJohnathan Mantey } 91249c53ac9SJohnathan Mantey else 91349c53ac9SJohnathan Mantey { 9143929aca1SAnthony Wilson unit = "/PowerOutputWatts"_json_pointer; 91549c53ac9SJohnathan Mantey } 9162474adfaSEd Tanous } 9171abe55efSEd Tanous else 9181abe55efSEd Tanous { 91955c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName; 92008777fb0SLewanczyk, Dawid return; 92108777fb0SLewanczyk, Dawid } 92208777fb0SLewanczyk, Dawid // Map of dbus interface name, dbus property name and redfish property_name 9233929aca1SAnthony Wilson std::vector< 9243929aca1SAnthony Wilson std::tuple<const char*, const char*, nlohmann::json::json_pointer>> 9253929aca1SAnthony Wilson properties; 92608777fb0SLewanczyk, Dawid properties.reserve(7); 92708777fb0SLewanczyk, Dawid 92808777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit); 929de629b6eSShawn McCarney 9301d7c0054SEd Tanous if (chassisSubNode == sensors::node::sensors) 9313929aca1SAnthony Wilson { 9323929aca1SAnthony Wilson properties.emplace_back( 9333929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh", 9343929aca1SAnthony Wilson "/Thresholds/UpperCaution/Reading"_json_pointer); 9353929aca1SAnthony Wilson properties.emplace_back( 9363929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow", 9373929aca1SAnthony Wilson "/Thresholds/LowerCaution/Reading"_json_pointer); 9383929aca1SAnthony Wilson properties.emplace_back( 9393929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh", 9403929aca1SAnthony Wilson "/Thresholds/UpperCritical/Reading"_json_pointer); 9413929aca1SAnthony Wilson properties.emplace_back( 9423929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow", 9433929aca1SAnthony Wilson "/Thresholds/LowerCritical/Reading"_json_pointer); 9443929aca1SAnthony Wilson } 9453929aca1SAnthony Wilson else if (sensorType != "power") 946de629b6eSShawn McCarney { 94708777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 9483929aca1SAnthony Wilson "WarningHigh", 9493929aca1SAnthony Wilson "/UpperThresholdNonCritical"_json_pointer); 95008777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 9513929aca1SAnthony Wilson "WarningLow", 9523929aca1SAnthony Wilson "/LowerThresholdNonCritical"_json_pointer); 95308777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 9543929aca1SAnthony Wilson "CriticalHigh", 9553929aca1SAnthony Wilson "/UpperThresholdCritical"_json_pointer); 95608777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 9573929aca1SAnthony Wilson "CriticalLow", 9583929aca1SAnthony Wilson "/LowerThresholdCritical"_json_pointer); 959de629b6eSShawn McCarney } 96008777fb0SLewanczyk, Dawid 9612474adfaSEd Tanous // TODO Need to get UpperThresholdFatal and LowerThresholdFatal 9622474adfaSEd Tanous 9631d7c0054SEd Tanous if (chassisSubNode == sensors::node::sensors) 96495a3ecadSAnthony Wilson { 96595a3ecadSAnthony Wilson properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 9663929aca1SAnthony Wilson "/ReadingRangeMin"_json_pointer); 96795a3ecadSAnthony Wilson properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 9683929aca1SAnthony Wilson "/ReadingRangeMax"_json_pointer); 96995a3ecadSAnthony Wilson } 97095a3ecadSAnthony Wilson else if (sensorType == "temperature") 9711abe55efSEd Tanous { 97208777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 9733929aca1SAnthony Wilson "/MinReadingRangeTemp"_json_pointer); 97408777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 9753929aca1SAnthony Wilson "/MaxReadingRangeTemp"_json_pointer); 9761abe55efSEd Tanous } 977adc4f0dbSShawn McCarney else if (sensorType != "power") 9781abe55efSEd Tanous { 97908777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 9803929aca1SAnthony Wilson "/MinReadingRange"_json_pointer); 98108777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 9823929aca1SAnthony Wilson "/MaxReadingRange"_json_pointer); 98308777fb0SLewanczyk, Dawid } 98408777fb0SLewanczyk, Dawid 9853929aca1SAnthony Wilson for (const std::tuple<const char*, const char*, 9863929aca1SAnthony Wilson nlohmann::json::json_pointer>& p : properties) 9871abe55efSEd Tanous { 9881d7c0054SEd Tanous for (const auto& [valueName, valueVariant] : propertiesDict) 989711ac7a9SEd Tanous { 990711ac7a9SEd Tanous if (valueName != std::get<1>(p)) 991711ac7a9SEd Tanous { 992711ac7a9SEd Tanous continue; 993711ac7a9SEd Tanous } 9943929aca1SAnthony Wilson 9953929aca1SAnthony Wilson // The property we want to set may be nested json, so use 9963929aca1SAnthony Wilson // a json_pointer for easy indexing into the json structure. 9973929aca1SAnthony Wilson const nlohmann::json::json_pointer& key = std::get<2>(p); 9983929aca1SAnthony Wilson 999abf2add6SEd Tanous const double* doubleValue = std::get_if<double>(&valueVariant); 100040e4f380SEd Tanous if (doubleValue == nullptr) 10011abe55efSEd Tanous { 100240e4f380SEd Tanous BMCWEB_LOG_ERROR << "Got value interface that wasn't double"; 10036f6d0d32SEd Tanous continue; 100408777fb0SLewanczyk, Dawid } 10056f6d0d32SEd Tanous if (forceToInt) 10066f6d0d32SEd Tanous { 100740e4f380SEd Tanous sensorJson[key] = static_cast<int64_t>(*doubleValue); 10086f6d0d32SEd Tanous } 10096f6d0d32SEd Tanous else 10106f6d0d32SEd Tanous { 101140e4f380SEd Tanous sensorJson[key] = *doubleValue; 101208777fb0SLewanczyk, Dawid } 101308777fb0SLewanczyk, Dawid } 101408777fb0SLewanczyk, Dawid } 101508777fb0SLewanczyk, Dawid } 101608777fb0SLewanczyk, Dawid 10171d7c0054SEd Tanous /** 10181d7c0054SEd Tanous * @brief Builds a json sensor representation of a sensor. 10191d7c0054SEd Tanous * @param sensorName The name of the sensor to be built 10201d7c0054SEd Tanous * @param sensorType The type (temperature, fan_tach, etc) of the sensor to 10211d7c0054SEd Tanous * build 10221d7c0054SEd Tanous * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor 10231d7c0054SEd Tanous * @param interfacesDict A dictionary of the interfaces and properties of said 10241d7c0054SEd Tanous * interfaces to be built from 10251d7c0054SEd Tanous * @param sensorJson The json object to fill 10261d7c0054SEd Tanous * @param inventoryItem D-Bus inventory item associated with the sensor. Will 10271d7c0054SEd Tanous * be nullptr if no associated inventory item was found. 10281d7c0054SEd Tanous */ 10291d7c0054SEd Tanous inline void objectInterfacesToJson( 10301d7c0054SEd Tanous const std::string& sensorName, const std::string& sensorType, 10311d7c0054SEd Tanous const std::string& chassisSubNode, 10321d7c0054SEd Tanous const dbus::utility::DBusInteracesMap& interfacesDict, 10331d7c0054SEd Tanous nlohmann::json& sensorJson, InventoryItem* inventoryItem) 10341d7c0054SEd Tanous { 10351d7c0054SEd Tanous 10361d7c0054SEd Tanous for (const auto& [interface, valuesDict] : interfacesDict) 10371d7c0054SEd Tanous { 10381d7c0054SEd Tanous objectPropertiesToJson(sensorName, sensorType, chassisSubNode, 10391d7c0054SEd Tanous valuesDict, sensorJson, inventoryItem); 10401d7c0054SEd Tanous } 1041c1d019a6SEd Tanous BMCWEB_LOG_DEBUG << "Added sensor " << sensorName; 10421d7c0054SEd Tanous } 10431d7c0054SEd Tanous 1044b5a76932SEd Tanous inline void populateFanRedundancy( 1045b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 10468bd25ccdSJames Feist { 10478bd25ccdSJames Feist crow::connections::systemBus->async_method_call( 1048b9d36b47SEd Tanous [sensorsAsyncResp]( 1049b9d36b47SEd Tanous const boost::system::error_code ec, 1050b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& resp) { 10518bd25ccdSJames Feist if (ec) 10528bd25ccdSJames Feist { 10538bd25ccdSJames Feist return; // don't have to have this interface 10548bd25ccdSJames Feist } 1055002d39b4SEd Tanous for (const std::pair< 1056002d39b4SEd Tanous std::string, 1057002d39b4SEd Tanous std::vector<std::pair<std::string, std::vector<std::string>>>>& 1058e278c18fSEd Tanous pathPair : resp) 10598bd25ccdSJames Feist { 1060e278c18fSEd Tanous const std::string& path = pathPair.first; 1061002d39b4SEd Tanous const std::vector<std::pair<std::string, std::vector<std::string>>>& 1062002d39b4SEd Tanous objDict = pathPair.second; 10638bd25ccdSJames Feist if (objDict.empty()) 10648bd25ccdSJames Feist { 10658bd25ccdSJames Feist continue; // this should be impossible 10668bd25ccdSJames Feist } 10678bd25ccdSJames Feist 10688bd25ccdSJames Feist const std::string& owner = objDict.begin()->first; 10691e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::vector<std::string>>( 10701e1e598dSJonathan Doman *crow::connections::systemBus, 10711e1e598dSJonathan Doman "xyz.openbmc_project.ObjectMapper", path + "/chassis", 10721e1e598dSJonathan Doman "xyz.openbmc_project.Association", "endpoints", 1073002d39b4SEd Tanous [path, owner, 1074002d39b4SEd Tanous sensorsAsyncResp](const boost::system::error_code e, 10751e1e598dSJonathan Doman const std::vector<std::string>& endpoints) { 1076271584abSEd Tanous if (e) 10778bd25ccdSJames Feist { 10788bd25ccdSJames Feist return; // if they don't have an association we 10798bd25ccdSJames Feist // can't tell what chassis is 10808bd25ccdSJames Feist } 1081002d39b4SEd Tanous auto found = 1082002d39b4SEd Tanous std::find_if(endpoints.begin(), endpoints.end(), 10838bd25ccdSJames Feist [sensorsAsyncResp](const std::string& entry) { 1084002d39b4SEd Tanous return entry.find(sensorsAsyncResp->chassisId) != 10858bd25ccdSJames Feist std::string::npos; 10868bd25ccdSJames Feist }); 10878bd25ccdSJames Feist 10881e1e598dSJonathan Doman if (found == endpoints.end()) 10898bd25ccdSJames Feist { 10908bd25ccdSJames Feist return; 10918bd25ccdSJames Feist } 109286d89ed7SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 109386d89ed7SKrzysztof Grobelny *crow::connections::systemBus, owner, path, 109486d89ed7SKrzysztof Grobelny "xyz.openbmc_project.Control.FanRedundancy", 10958bd25ccdSJames Feist [path, sensorsAsyncResp]( 1096271584abSEd Tanous const boost::system::error_code& err, 109786d89ed7SKrzysztof Grobelny const dbus::utility::DBusPropertiesMap& ret) { 1098271584abSEd Tanous if (err) 10998bd25ccdSJames Feist { 11008bd25ccdSJames Feist return; // don't have to have this 11018bd25ccdSJames Feist // interface 11028bd25ccdSJames Feist } 11038bd25ccdSJames Feist 110486d89ed7SKrzysztof Grobelny const uint8_t* allowedFailures = nullptr; 110586d89ed7SKrzysztof Grobelny const std::vector<std::string>* collection = nullptr; 110686d89ed7SKrzysztof Grobelny const std::string* status = nullptr; 110786d89ed7SKrzysztof Grobelny 110886d89ed7SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 110986d89ed7SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), ret, 111086d89ed7SKrzysztof Grobelny "AllowedFailures", allowedFailures, "Collection", 111186d89ed7SKrzysztof Grobelny collection, "Status", status); 111286d89ed7SKrzysztof Grobelny 111386d89ed7SKrzysztof Grobelny if (!success) 111486d89ed7SKrzysztof Grobelny { 111586d89ed7SKrzysztof Grobelny messages::internalError( 111686d89ed7SKrzysztof Grobelny sensorsAsyncResp->asyncResp->res); 111786d89ed7SKrzysztof Grobelny return; 111886d89ed7SKrzysztof Grobelny } 111986d89ed7SKrzysztof Grobelny 112086d89ed7SKrzysztof Grobelny if (allowedFailures == nullptr || collection == nullptr || 112186d89ed7SKrzysztof Grobelny status == nullptr) 11228bd25ccdSJames Feist { 1123002d39b4SEd Tanous BMCWEB_LOG_ERROR << "Invalid redundancy interface"; 11248bd25ccdSJames Feist messages::internalError( 11258d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 11268bd25ccdSJames Feist return; 11278bd25ccdSJames Feist } 11288bd25ccdSJames Feist 1129002d39b4SEd Tanous sdbusplus::message::object_path objectPath(path); 113028aa8de5SGeorge Liu std::string name = objectPath.filename(); 113128aa8de5SGeorge Liu if (name.empty()) 11328bd25ccdSJames Feist { 11338bd25ccdSJames Feist // this should be impossible 11348bd25ccdSJames Feist messages::internalError( 11358d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 11368bd25ccdSJames Feist return; 11378bd25ccdSJames Feist } 1138002d39b4SEd Tanous std::replace(name.begin(), name.end(), '_', ' '); 11398bd25ccdSJames Feist 11408bd25ccdSJames Feist std::string health; 11418bd25ccdSJames Feist 114211ba3979SEd Tanous if (status->ends_with("Full")) 11438bd25ccdSJames Feist { 11448bd25ccdSJames Feist health = "OK"; 11458bd25ccdSJames Feist } 114611ba3979SEd Tanous else if (status->ends_with("Degraded")) 11478bd25ccdSJames Feist { 11488bd25ccdSJames Feist health = "Warning"; 11498bd25ccdSJames Feist } 11508bd25ccdSJames Feist else 11518bd25ccdSJames Feist { 11528bd25ccdSJames Feist health = "Critical"; 11538bd25ccdSJames Feist } 11541476687dSEd Tanous nlohmann::json::array_t redfishCollection; 11558bd25ccdSJames Feist const auto& fanRedfish = 1156002d39b4SEd Tanous sensorsAsyncResp->asyncResp->res.jsonValue["Fans"]; 11578bd25ccdSJames Feist for (const std::string& item : *collection) 11588bd25ccdSJames Feist { 11598a592810SEd Tanous sdbusplus::message::object_path itemPath(item); 11608a592810SEd Tanous std::string itemName = itemPath.filename(); 116128aa8de5SGeorge Liu if (itemName.empty()) 116228aa8de5SGeorge Liu { 116328aa8de5SGeorge Liu continue; 116428aa8de5SGeorge Liu } 11658bd25ccdSJames Feist /* 11668bd25ccdSJames Feist todo(ed): merge patch that fixes the names 11678bd25ccdSJames Feist std::replace(itemName.begin(), 11688bd25ccdSJames Feist itemName.end(), '_', ' ');*/ 1169002d39b4SEd Tanous auto schemaItem = 1170002d39b4SEd Tanous std::find_if(fanRedfish.begin(), fanRedfish.end(), 11718bd25ccdSJames Feist [itemName](const nlohmann::json& fan) { 11728bd25ccdSJames Feist return fan["MemberId"] == itemName; 11738bd25ccdSJames Feist }); 11748bd25ccdSJames Feist if (schemaItem != fanRedfish.end()) 11758bd25ccdSJames Feist { 11768a592810SEd Tanous nlohmann::json::object_t collectionId; 11778a592810SEd Tanous collectionId["@odata.id"] = 11781476687dSEd Tanous (*schemaItem)["@odata.id"]; 11791476687dSEd Tanous redfishCollection.emplace_back( 11808a592810SEd Tanous std::move(collectionId)); 11818bd25ccdSJames Feist } 11828bd25ccdSJames Feist else 11838bd25ccdSJames Feist { 1184002d39b4SEd Tanous BMCWEB_LOG_ERROR << "failed to find fan in schema"; 11858bd25ccdSJames Feist messages::internalError( 11868d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 11878bd25ccdSJames Feist return; 11888bd25ccdSJames Feist } 11898bd25ccdSJames Feist } 11908bd25ccdSJames Feist 11913e9e72ebSKuiying Wang size_t minNumNeeded = 119226f6976fSEd Tanous collection->empty() 119326f6976fSEd Tanous ? 0 119426f6976fSEd Tanous : collection->size() - *allowedFailures; 1195002d39b4SEd Tanous nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res 11968bd25ccdSJames Feist .jsonValue["Redundancy"]; 11971476687dSEd Tanous 11981476687dSEd Tanous nlohmann::json::object_t redundancy; 11991476687dSEd Tanous redundancy["@odata.id"] = 1200002d39b4SEd Tanous "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + 1201002d39b4SEd Tanous "/" + sensorsAsyncResp->chassisSubNode + 1202002d39b4SEd Tanous "#/Redundancy/" + std::to_string(jResp.size()); 1203002d39b4SEd Tanous redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy"; 12041476687dSEd Tanous redundancy["MinNumNeeded"] = minNumNeeded; 12051476687dSEd Tanous redundancy["MemberId"] = name; 12061476687dSEd Tanous redundancy["Mode"] = "N+m"; 12071476687dSEd Tanous redundancy["Name"] = name; 12081476687dSEd Tanous redundancy["RedundancySet"] = redfishCollection; 12091476687dSEd Tanous redundancy["Status"]["Health"] = health; 12101476687dSEd Tanous redundancy["Status"]["State"] = "Enabled"; 12111476687dSEd Tanous 12121476687dSEd Tanous jResp.push_back(std::move(redundancy)); 121386d89ed7SKrzysztof Grobelny }); 12141e1e598dSJonathan Doman }); 12158bd25ccdSJames Feist } 12168bd25ccdSJames Feist }, 12178bd25ccdSJames Feist "xyz.openbmc_project.ObjectMapper", 12188bd25ccdSJames Feist "/xyz/openbmc_project/object_mapper", 12198bd25ccdSJames Feist "xyz.openbmc_project.ObjectMapper", "GetSubTree", 12208bd25ccdSJames Feist "/xyz/openbmc_project/control", 2, 12218bd25ccdSJames Feist std::array<const char*, 1>{ 12228bd25ccdSJames Feist "xyz.openbmc_project.Control.FanRedundancy"}); 12238bd25ccdSJames Feist } 12248bd25ccdSJames Feist 1225b5a76932SEd Tanous inline void 122681ce609eSEd Tanous sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 122749c53ac9SJohnathan Mantey { 12288d1b46d7Szhanghch05 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue; 122949c53ac9SJohnathan Mantey std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"}; 123081ce609eSEd Tanous if (sensorsAsyncResp->chassisSubNode == sensors::node::power) 123149c53ac9SJohnathan Mantey { 123249c53ac9SJohnathan Mantey sensorHeaders = {"Voltages", "PowerSupplies"}; 123349c53ac9SJohnathan Mantey } 123449c53ac9SJohnathan Mantey for (const std::string& sensorGroup : sensorHeaders) 123549c53ac9SJohnathan Mantey { 123649c53ac9SJohnathan Mantey nlohmann::json::iterator entry = response.find(sensorGroup); 123749c53ac9SJohnathan Mantey if (entry != response.end()) 123849c53ac9SJohnathan Mantey { 123949c53ac9SJohnathan Mantey std::sort(entry->begin(), entry->end(), 124002cad96eSEd Tanous [](const nlohmann::json& c1, const nlohmann::json& c2) { 124149c53ac9SJohnathan Mantey return c1["Name"] < c2["Name"]; 124249c53ac9SJohnathan Mantey }); 124349c53ac9SJohnathan Mantey 124449c53ac9SJohnathan Mantey // add the index counts to the end of each entry 124549c53ac9SJohnathan Mantey size_t count = 0; 124649c53ac9SJohnathan Mantey for (nlohmann::json& sensorJson : *entry) 124749c53ac9SJohnathan Mantey { 124849c53ac9SJohnathan Mantey nlohmann::json::iterator odata = sensorJson.find("@odata.id"); 124949c53ac9SJohnathan Mantey if (odata == sensorJson.end()) 125049c53ac9SJohnathan Mantey { 125149c53ac9SJohnathan Mantey continue; 125249c53ac9SJohnathan Mantey } 125349c53ac9SJohnathan Mantey std::string* value = odata->get_ptr<std::string*>(); 125449c53ac9SJohnathan Mantey if (value != nullptr) 125549c53ac9SJohnathan Mantey { 125649c53ac9SJohnathan Mantey *value += std::to_string(count); 125749c53ac9SJohnathan Mantey count++; 125881ce609eSEd Tanous sensorsAsyncResp->updateUri(sensorJson["Name"], *value); 125949c53ac9SJohnathan Mantey } 126049c53ac9SJohnathan Mantey } 126149c53ac9SJohnathan Mantey } 126249c53ac9SJohnathan Mantey } 126349c53ac9SJohnathan Mantey } 126449c53ac9SJohnathan Mantey 126508777fb0SLewanczyk, Dawid /** 1266adc4f0dbSShawn McCarney * @brief Finds the inventory item with the specified object path. 1267adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1268adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 1269adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 12708fb49dd6SShawn McCarney */ 127123a21a1cSEd Tanous inline InventoryItem* findInventoryItem( 1272b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1273adc4f0dbSShawn McCarney const std::string& invItemObjPath) 12748fb49dd6SShawn McCarney { 1275adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 12768fb49dd6SShawn McCarney { 1277adc4f0dbSShawn McCarney if (inventoryItem.objectPath == invItemObjPath) 12788fb49dd6SShawn McCarney { 1279adc4f0dbSShawn McCarney return &inventoryItem; 12808fb49dd6SShawn McCarney } 12818fb49dd6SShawn McCarney } 12828fb49dd6SShawn McCarney return nullptr; 12838fb49dd6SShawn McCarney } 12848fb49dd6SShawn McCarney 12858fb49dd6SShawn McCarney /** 1286adc4f0dbSShawn McCarney * @brief Finds the inventory item associated with the specified sensor. 1287adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1288adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor. 1289adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 12908fb49dd6SShawn McCarney */ 129123a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor( 1292b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1293adc4f0dbSShawn McCarney const std::string& sensorObjPath) 1294adc4f0dbSShawn McCarney { 1295adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 1296adc4f0dbSShawn McCarney { 1297adc4f0dbSShawn McCarney if (inventoryItem.sensors.count(sensorObjPath) > 0) 1298adc4f0dbSShawn McCarney { 1299adc4f0dbSShawn McCarney return &inventoryItem; 1300adc4f0dbSShawn McCarney } 1301adc4f0dbSShawn McCarney } 1302adc4f0dbSShawn McCarney return nullptr; 1303adc4f0dbSShawn McCarney } 1304adc4f0dbSShawn McCarney 1305adc4f0dbSShawn McCarney /** 1306d500549bSAnthony Wilson * @brief Finds the inventory item associated with the specified led path. 1307d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1308d500549bSAnthony Wilson * @param ledObjPath D-Bus object path of led. 1309d500549bSAnthony Wilson * @return Inventory item within vector, or nullptr if no match found. 1310d500549bSAnthony Wilson */ 1311d500549bSAnthony Wilson inline InventoryItem* 1312d500549bSAnthony Wilson findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems, 1313d500549bSAnthony Wilson const std::string& ledObjPath) 1314d500549bSAnthony Wilson { 1315d500549bSAnthony Wilson for (InventoryItem& inventoryItem : inventoryItems) 1316d500549bSAnthony Wilson { 1317d500549bSAnthony Wilson if (inventoryItem.ledObjectPath == ledObjPath) 1318d500549bSAnthony Wilson { 1319d500549bSAnthony Wilson return &inventoryItem; 1320d500549bSAnthony Wilson } 1321d500549bSAnthony Wilson } 1322d500549bSAnthony Wilson return nullptr; 1323d500549bSAnthony Wilson } 1324d500549bSAnthony Wilson 1325d500549bSAnthony Wilson /** 1326adc4f0dbSShawn McCarney * @brief Adds inventory item and associated sensor to specified vector. 1327adc4f0dbSShawn McCarney * 1328adc4f0dbSShawn McCarney * Adds a new InventoryItem to the vector if necessary. Searches for an 1329adc4f0dbSShawn McCarney * existing InventoryItem with the specified object path. If not found, one is 1330adc4f0dbSShawn McCarney * added to the vector. 1331adc4f0dbSShawn McCarney * 1332adc4f0dbSShawn McCarney * Next, the specified sensor is added to the set of sensors associated with the 1333adc4f0dbSShawn McCarney * InventoryItem. 1334adc4f0dbSShawn McCarney * 1335adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1336adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 1337adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor 1338adc4f0dbSShawn McCarney */ 1339b5a76932SEd Tanous inline void addInventoryItem( 1340b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1341b5a76932SEd Tanous const std::string& invItemObjPath, const std::string& sensorObjPath) 1342adc4f0dbSShawn McCarney { 1343adc4f0dbSShawn McCarney // Look for inventory item in vector 1344adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1345adc4f0dbSShawn McCarney findInventoryItem(inventoryItems, invItemObjPath); 1346adc4f0dbSShawn McCarney 1347adc4f0dbSShawn McCarney // If inventory item doesn't exist in vector, add it 1348adc4f0dbSShawn McCarney if (inventoryItem == nullptr) 1349adc4f0dbSShawn McCarney { 1350adc4f0dbSShawn McCarney inventoryItems->emplace_back(invItemObjPath); 1351adc4f0dbSShawn McCarney inventoryItem = &(inventoryItems->back()); 1352adc4f0dbSShawn McCarney } 1353adc4f0dbSShawn McCarney 1354adc4f0dbSShawn McCarney // Add sensor to set of sensors associated with inventory item 1355adc4f0dbSShawn McCarney inventoryItem->sensors.emplace(sensorObjPath); 1356adc4f0dbSShawn McCarney } 1357adc4f0dbSShawn McCarney 1358adc4f0dbSShawn McCarney /** 1359adc4f0dbSShawn McCarney * @brief Stores D-Bus data in the specified inventory item. 1360adc4f0dbSShawn McCarney * 1361adc4f0dbSShawn McCarney * Finds D-Bus data in the specified map of interfaces. Stores the data in the 1362adc4f0dbSShawn McCarney * specified InventoryItem. 1363adc4f0dbSShawn McCarney * 1364adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1365adc4f0dbSShawn McCarney * response. 1366adc4f0dbSShawn McCarney * 1367adc4f0dbSShawn McCarney * @param inventoryItem Inventory item where data will be stored. 1368adc4f0dbSShawn McCarney * @param interfacesDict Map containing D-Bus interfaces and their properties 1369adc4f0dbSShawn McCarney * for the specified inventory item. 1370adc4f0dbSShawn McCarney */ 137123a21a1cSEd Tanous inline void storeInventoryItemData( 1372adc4f0dbSShawn McCarney InventoryItem& inventoryItem, 1373711ac7a9SEd Tanous const dbus::utility::DBusInteracesMap& interfacesDict) 13748fb49dd6SShawn McCarney { 1375adc4f0dbSShawn McCarney // Get properties from Inventory.Item interface 1376711ac7a9SEd Tanous 13779eb808c1SEd Tanous for (const auto& [interface, values] : interfacesDict) 13788fb49dd6SShawn McCarney { 1379711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Item") 13808fb49dd6SShawn McCarney { 13819eb808c1SEd Tanous for (const auto& [name, dbusValue] : values) 1382711ac7a9SEd Tanous { 1383711ac7a9SEd Tanous if (name == "Present") 1384711ac7a9SEd Tanous { 1385711ac7a9SEd Tanous const bool* value = std::get_if<bool>(&dbusValue); 1386adc4f0dbSShawn McCarney if (value != nullptr) 13878fb49dd6SShawn McCarney { 1388adc4f0dbSShawn McCarney inventoryItem.isPresent = *value; 13898fb49dd6SShawn McCarney } 13908fb49dd6SShawn McCarney } 13918fb49dd6SShawn McCarney } 1392711ac7a9SEd Tanous } 1393adc4f0dbSShawn McCarney // Check if Inventory.Item.PowerSupply interface is present 1394711ac7a9SEd Tanous 1395711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply") 13968fb49dd6SShawn McCarney { 1397adc4f0dbSShawn McCarney inventoryItem.isPowerSupply = true; 13988fb49dd6SShawn McCarney } 1399adc4f0dbSShawn McCarney 1400adc4f0dbSShawn McCarney // Get properties from Inventory.Decorator.Asset interface 1401711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset") 1402adc4f0dbSShawn McCarney { 14039eb808c1SEd Tanous for (const auto& [name, dbusValue] : values) 1404711ac7a9SEd Tanous { 1405711ac7a9SEd Tanous if (name == "Manufacturer") 1406adc4f0dbSShawn McCarney { 1407adc4f0dbSShawn McCarney const std::string* value = 1408711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1409adc4f0dbSShawn McCarney if (value != nullptr) 1410adc4f0dbSShawn McCarney { 1411adc4f0dbSShawn McCarney inventoryItem.manufacturer = *value; 1412adc4f0dbSShawn McCarney } 1413adc4f0dbSShawn McCarney } 1414711ac7a9SEd Tanous if (name == "Model") 1415adc4f0dbSShawn McCarney { 1416adc4f0dbSShawn McCarney const std::string* value = 1417711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1418adc4f0dbSShawn McCarney if (value != nullptr) 1419adc4f0dbSShawn McCarney { 1420adc4f0dbSShawn McCarney inventoryItem.model = *value; 1421adc4f0dbSShawn McCarney } 1422adc4f0dbSShawn McCarney } 1423711ac7a9SEd Tanous if (name == "SerialNumber") 1424adc4f0dbSShawn McCarney { 1425adc4f0dbSShawn McCarney const std::string* value = 1426711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1427adc4f0dbSShawn McCarney if (value != nullptr) 1428adc4f0dbSShawn McCarney { 1429adc4f0dbSShawn McCarney inventoryItem.serialNumber = *value; 1430adc4f0dbSShawn McCarney } 1431adc4f0dbSShawn McCarney } 1432711ac7a9SEd Tanous if (name == "PartNumber") 1433711ac7a9SEd Tanous { 1434711ac7a9SEd Tanous const std::string* value = 1435711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1436711ac7a9SEd Tanous if (value != nullptr) 1437711ac7a9SEd Tanous { 1438711ac7a9SEd Tanous inventoryItem.partNumber = *value; 1439711ac7a9SEd Tanous } 1440711ac7a9SEd Tanous } 1441711ac7a9SEd Tanous } 1442adc4f0dbSShawn McCarney } 1443adc4f0dbSShawn McCarney 1444711ac7a9SEd Tanous if (interface == 1445711ac7a9SEd Tanous "xyz.openbmc_project.State.Decorator.OperationalStatus") 1446adc4f0dbSShawn McCarney { 14479eb808c1SEd Tanous for (const auto& [name, dbusValue] : values) 1448adc4f0dbSShawn McCarney { 1449711ac7a9SEd Tanous if (name == "Functional") 1450711ac7a9SEd Tanous { 1451711ac7a9SEd Tanous const bool* value = std::get_if<bool>(&dbusValue); 1452adc4f0dbSShawn McCarney if (value != nullptr) 1453adc4f0dbSShawn McCarney { 1454adc4f0dbSShawn McCarney inventoryItem.isFunctional = *value; 14558fb49dd6SShawn McCarney } 14568fb49dd6SShawn McCarney } 14578fb49dd6SShawn McCarney } 14588fb49dd6SShawn McCarney } 1459711ac7a9SEd Tanous } 1460711ac7a9SEd Tanous } 14618fb49dd6SShawn McCarney 14628fb49dd6SShawn McCarney /** 1463adc4f0dbSShawn McCarney * @brief Gets D-Bus data for inventory items associated with sensors. 14648fb49dd6SShawn McCarney * 1465adc4f0dbSShawn McCarney * Uses the specified connections (services) to obtain D-Bus data for inventory 1466adc4f0dbSShawn McCarney * items associated with sensors. Stores the resulting data in the 1467adc4f0dbSShawn McCarney * inventoryItems vector. 14688fb49dd6SShawn McCarney * 1469adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1470adc4f0dbSShawn McCarney * response. 1471adc4f0dbSShawn McCarney * 1472adc4f0dbSShawn McCarney * Finds the inventory item data asynchronously. Invokes callback when data has 1473adc4f0dbSShawn McCarney * been obtained. 1474adc4f0dbSShawn McCarney * 1475adc4f0dbSShawn McCarney * The callback must have the following signature: 1476adc4f0dbSShawn McCarney * @code 1477d500549bSAnthony Wilson * callback(void) 1478adc4f0dbSShawn McCarney * @endcode 1479adc4f0dbSShawn McCarney * 1480adc4f0dbSShawn McCarney * This function is called recursively, obtaining data asynchronously from one 1481adc4f0dbSShawn McCarney * connection in each call. This ensures the callback is not invoked until the 1482adc4f0dbSShawn McCarney * last asynchronous function has completed. 14838fb49dd6SShawn McCarney * 14848fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1485adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1486adc4f0dbSShawn McCarney * @param invConnections Connections that provide data for the inventory items. 14878fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 14888fb49dd6SShawn McCarney * implements ObjectManager. 1489adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory data has been obtained. 1490adc4f0dbSShawn McCarney * @param invConnectionsIndex Current index in invConnections. Only specified 1491adc4f0dbSShawn McCarney * in recursive calls to this function. 14928fb49dd6SShawn McCarney */ 1493adc4f0dbSShawn McCarney template <typename Callback> 1494adc4f0dbSShawn McCarney static void getInventoryItemsData( 14958fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1496adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1497fe04d49cSNan Zhou std::shared_ptr<std::set<std::string>> invConnections, 1498fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths, 1499271584abSEd Tanous Callback&& callback, size_t invConnectionsIndex = 0) 15008fb49dd6SShawn McCarney { 1501adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData enter"; 15028fb49dd6SShawn McCarney 1503adc4f0dbSShawn McCarney // If no more connections left, call callback 1504adc4f0dbSShawn McCarney if (invConnectionsIndex >= invConnections->size()) 15058fb49dd6SShawn McCarney { 1506d500549bSAnthony Wilson callback(); 1507adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData exit"; 1508adc4f0dbSShawn McCarney return; 1509adc4f0dbSShawn McCarney } 1510adc4f0dbSShawn McCarney 1511adc4f0dbSShawn McCarney // Get inventory item data from current connection 1512fe04d49cSNan Zhou auto it = invConnections->begin(); 1513fe04d49cSNan Zhou std::advance(it, invConnectionsIndex); 1514adc4f0dbSShawn McCarney if (it != invConnections->end()) 1515adc4f0dbSShawn McCarney { 1516adc4f0dbSShawn McCarney const std::string& invConnection = *it; 1517adc4f0dbSShawn McCarney 15188fb49dd6SShawn McCarney // Response handler for GetManagedObjects 1519002d39b4SEd Tanous auto respHandler = 1520002d39b4SEd Tanous [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths, 152102cad96eSEd Tanous callback{std::forward<Callback>(callback)}, invConnectionsIndex]( 152202cad96eSEd Tanous const boost::system::error_code ec, 152302cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 1524adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter"; 15258fb49dd6SShawn McCarney if (ec) 15268fb49dd6SShawn McCarney { 15278fb49dd6SShawn McCarney BMCWEB_LOG_ERROR 1528adc4f0dbSShawn McCarney << "getInventoryItemsData respHandler DBus error " << ec; 15298d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 15308fb49dd6SShawn McCarney return; 15318fb49dd6SShawn McCarney } 15328fb49dd6SShawn McCarney 15338fb49dd6SShawn McCarney // Loop through returned object paths 15348fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 15358fb49dd6SShawn McCarney { 15368fb49dd6SShawn McCarney const std::string& objPath = 15378fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 15388fb49dd6SShawn McCarney 1539adc4f0dbSShawn McCarney // If this object path is one of the specified inventory items 1540adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1541adc4f0dbSShawn McCarney findInventoryItem(inventoryItems, objPath); 1542adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 15438fb49dd6SShawn McCarney { 1544adc4f0dbSShawn McCarney // Store inventory data in InventoryItem 1545adc4f0dbSShawn McCarney storeInventoryItemData(*inventoryItem, objDictEntry.second); 15468fb49dd6SShawn McCarney } 15478fb49dd6SShawn McCarney } 15488fb49dd6SShawn McCarney 1549adc4f0dbSShawn McCarney // Recurse to get inventory item data from next connection 1550adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 1551adc4f0dbSShawn McCarney invConnections, objectMgrPaths, 1552adc4f0dbSShawn McCarney std::move(callback), invConnectionsIndex + 1); 1553adc4f0dbSShawn McCarney 1554adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit"; 15558fb49dd6SShawn McCarney }; 15568fb49dd6SShawn McCarney 15578fb49dd6SShawn McCarney // Find DBus object path that implements ObjectManager for the current 15588fb49dd6SShawn McCarney // connection. If no mapping found, default to "/". 15598fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(invConnection); 15608fb49dd6SShawn McCarney const std::string& objectMgrPath = 15618fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 15628fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is " 15638fb49dd6SShawn McCarney << objectMgrPath; 15648fb49dd6SShawn McCarney 15658fb49dd6SShawn McCarney // Get all object paths and their interfaces for current connection 15668fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 15678fb49dd6SShawn McCarney std::move(respHandler), invConnection, objectMgrPath, 15688fb49dd6SShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 15698fb49dd6SShawn McCarney } 15708fb49dd6SShawn McCarney 1571adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData exit"; 15728fb49dd6SShawn McCarney } 15738fb49dd6SShawn McCarney 15748fb49dd6SShawn McCarney /** 1575adc4f0dbSShawn McCarney * @brief Gets connections that provide D-Bus data for inventory items. 15768fb49dd6SShawn McCarney * 1577adc4f0dbSShawn McCarney * Gets the D-Bus connections (services) that provide data for the inventory 1578adc4f0dbSShawn McCarney * items that are associated with sensors. 15798fb49dd6SShawn McCarney * 15808fb49dd6SShawn McCarney * Finds the connections asynchronously. Invokes callback when information has 15818fb49dd6SShawn McCarney * been obtained. 15828fb49dd6SShawn McCarney * 15838fb49dd6SShawn McCarney * The callback must have the following signature: 15848fb49dd6SShawn McCarney * @code 1585fe04d49cSNan Zhou * callback(std::shared_ptr<std::set<std::string>> invConnections) 15868fb49dd6SShawn McCarney * @endcode 15878fb49dd6SShawn McCarney * 15888fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1589adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 15908fb49dd6SShawn McCarney * @param callback Callback to invoke when connections have been obtained. 15918fb49dd6SShawn McCarney */ 15928fb49dd6SShawn McCarney template <typename Callback> 15938fb49dd6SShawn McCarney static void getInventoryItemsConnections( 1594b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1595b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 15968fb49dd6SShawn McCarney Callback&& callback) 15978fb49dd6SShawn McCarney { 15988fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter"; 15998fb49dd6SShawn McCarney 16008fb49dd6SShawn McCarney const std::string path = "/xyz/openbmc_project/inventory"; 1601adc4f0dbSShawn McCarney const std::array<std::string, 4> interfaces = { 16028fb49dd6SShawn McCarney "xyz.openbmc_project.Inventory.Item", 1603adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.PowerSupply", 1604adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Decorator.Asset", 16058fb49dd6SShawn McCarney "xyz.openbmc_project.State.Decorator.OperationalStatus"}; 16068fb49dd6SShawn McCarney 16078fb49dd6SShawn McCarney // Response handler for parsing output from GetSubTree 1608002d39b4SEd Tanous auto respHandler = 1609002d39b4SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 1610002d39b4SEd Tanous inventoryItems]( 1611b9d36b47SEd Tanous const boost::system::error_code ec, 1612002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 16138fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter"; 16148fb49dd6SShawn McCarney if (ec) 16158fb49dd6SShawn McCarney { 16168d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 16178fb49dd6SShawn McCarney BMCWEB_LOG_ERROR 16188fb49dd6SShawn McCarney << "getInventoryItemsConnections respHandler DBus error " << ec; 16198fb49dd6SShawn McCarney return; 16208fb49dd6SShawn McCarney } 16218fb49dd6SShawn McCarney 16228fb49dd6SShawn McCarney // Make unique list of connections for desired inventory items 1623fe04d49cSNan Zhou std::shared_ptr<std::set<std::string>> invConnections = 1624fe04d49cSNan Zhou std::make_shared<std::set<std::string>>(); 16258fb49dd6SShawn McCarney 16268fb49dd6SShawn McCarney // Loop through objects from GetSubTree 16278fb49dd6SShawn McCarney for (const std::pair< 16288fb49dd6SShawn McCarney std::string, 16298fb49dd6SShawn McCarney std::vector<std::pair<std::string, std::vector<std::string>>>>& 16308fb49dd6SShawn McCarney object : subtree) 16318fb49dd6SShawn McCarney { 1632adc4f0dbSShawn McCarney // Check if object path is one of the specified inventory items 16338fb49dd6SShawn McCarney const std::string& objPath = object.first; 1634adc4f0dbSShawn McCarney if (findInventoryItem(inventoryItems, objPath) != nullptr) 16358fb49dd6SShawn McCarney { 16368fb49dd6SShawn McCarney // Store all connections to inventory item 16378fb49dd6SShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 16388fb49dd6SShawn McCarney objData : object.second) 16398fb49dd6SShawn McCarney { 16408fb49dd6SShawn McCarney const std::string& invConnection = objData.first; 16418fb49dd6SShawn McCarney invConnections->insert(invConnection); 16428fb49dd6SShawn McCarney } 16438fb49dd6SShawn McCarney } 16448fb49dd6SShawn McCarney } 1645d500549bSAnthony Wilson 16468fb49dd6SShawn McCarney callback(invConnections); 16478fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit"; 16488fb49dd6SShawn McCarney }; 16498fb49dd6SShawn McCarney 16508fb49dd6SShawn McCarney // Make call to ObjectMapper to find all inventory items 16518fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 16528fb49dd6SShawn McCarney std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 16538fb49dd6SShawn McCarney "/xyz/openbmc_project/object_mapper", 16548fb49dd6SShawn McCarney "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); 16558fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit"; 16568fb49dd6SShawn McCarney } 16578fb49dd6SShawn McCarney 16588fb49dd6SShawn McCarney /** 1659adc4f0dbSShawn McCarney * @brief Gets associations from sensors to inventory items. 16608fb49dd6SShawn McCarney * 16618fb49dd6SShawn McCarney * Looks for ObjectMapper associations from the specified sensors to related 1662d500549bSAnthony Wilson * inventory items. Then finds the associations from those inventory items to 1663d500549bSAnthony Wilson * their LEDs, if any. 16648fb49dd6SShawn McCarney * 16658fb49dd6SShawn McCarney * Finds the inventory items asynchronously. Invokes callback when information 16668fb49dd6SShawn McCarney * has been obtained. 16678fb49dd6SShawn McCarney * 16688fb49dd6SShawn McCarney * The callback must have the following signature: 16698fb49dd6SShawn McCarney * @code 1670adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 16718fb49dd6SShawn McCarney * @endcode 16728fb49dd6SShawn McCarney * 16738fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 16748fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 16758fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 16768fb49dd6SShawn McCarney * implements ObjectManager. 16778fb49dd6SShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 16788fb49dd6SShawn McCarney */ 16798fb49dd6SShawn McCarney template <typename Callback> 1680adc4f0dbSShawn McCarney static void getInventoryItemAssociations( 1681b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1682fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames, 1683fe04d49cSNan Zhou const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths, 16848fb49dd6SShawn McCarney Callback&& callback) 16858fb49dd6SShawn McCarney { 1686adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter"; 16878fb49dd6SShawn McCarney 16888fb49dd6SShawn McCarney // Response handler for GetManagedObjects 168902cad96eSEd Tanous auto respHandler = 169002cad96eSEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 16918fb49dd6SShawn McCarney sensorNames](const boost::system::error_code ec, 169202cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 1693adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter"; 16948fb49dd6SShawn McCarney if (ec) 16958fb49dd6SShawn McCarney { 1696adc4f0dbSShawn McCarney BMCWEB_LOG_ERROR 1697adc4f0dbSShawn McCarney << "getInventoryItemAssociations respHandler DBus error " << ec; 16988d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 16998fb49dd6SShawn McCarney return; 17008fb49dd6SShawn McCarney } 17018fb49dd6SShawn McCarney 1702adc4f0dbSShawn McCarney // Create vector to hold list of inventory items 1703adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems = 1704adc4f0dbSShawn McCarney std::make_shared<std::vector<InventoryItem>>(); 1705adc4f0dbSShawn McCarney 17068fb49dd6SShawn McCarney // Loop through returned object paths 17078fb49dd6SShawn McCarney std::string sensorAssocPath; 17088fb49dd6SShawn McCarney sensorAssocPath.reserve(128); // avoid memory allocations 17098fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 17108fb49dd6SShawn McCarney { 17118fb49dd6SShawn McCarney const std::string& objPath = 17128fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 17138fb49dd6SShawn McCarney 17148fb49dd6SShawn McCarney // If path is inventory association for one of the specified sensors 17158fb49dd6SShawn McCarney for (const std::string& sensorName : *sensorNames) 17168fb49dd6SShawn McCarney { 17178fb49dd6SShawn McCarney sensorAssocPath = sensorName; 17188fb49dd6SShawn McCarney sensorAssocPath += "/inventory"; 17198fb49dd6SShawn McCarney if (objPath == sensorAssocPath) 17208fb49dd6SShawn McCarney { 17218fb49dd6SShawn McCarney // Get Association interface for object path 1722711ac7a9SEd Tanous for (const auto& [interface, values] : objDictEntry.second) 17238fb49dd6SShawn McCarney { 1724711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Association") 1725711ac7a9SEd Tanous { 1726711ac7a9SEd Tanous for (const auto& [valueName, value] : values) 1727711ac7a9SEd Tanous { 1728711ac7a9SEd Tanous if (valueName == "endpoints") 17298fb49dd6SShawn McCarney { 17308fb49dd6SShawn McCarney const std::vector<std::string>* endpoints = 17318fb49dd6SShawn McCarney std::get_if<std::vector<std::string>>( 1732711ac7a9SEd Tanous &value); 1733711ac7a9SEd Tanous if ((endpoints != nullptr) && 1734711ac7a9SEd Tanous !endpoints->empty()) 17358fb49dd6SShawn McCarney { 1736adc4f0dbSShawn McCarney // Add inventory item to vector 1737adc4f0dbSShawn McCarney const std::string& invItemPath = 1738adc4f0dbSShawn McCarney endpoints->front(); 1739711ac7a9SEd Tanous addInventoryItem(inventoryItems, 1740711ac7a9SEd Tanous invItemPath, 1741adc4f0dbSShawn McCarney sensorName); 17428fb49dd6SShawn McCarney } 17438fb49dd6SShawn McCarney } 17448fb49dd6SShawn McCarney } 1745711ac7a9SEd Tanous } 1746711ac7a9SEd Tanous } 17478fb49dd6SShawn McCarney break; 17488fb49dd6SShawn McCarney } 17498fb49dd6SShawn McCarney } 17508fb49dd6SShawn McCarney } 17518fb49dd6SShawn McCarney 1752d500549bSAnthony Wilson // Now loop through the returned object paths again, this time to 1753d500549bSAnthony Wilson // find the leds associated with the inventory items we just found 1754d500549bSAnthony Wilson std::string inventoryAssocPath; 1755d500549bSAnthony Wilson inventoryAssocPath.reserve(128); // avoid memory allocations 1756d500549bSAnthony Wilson for (const auto& objDictEntry : resp) 1757d500549bSAnthony Wilson { 1758d500549bSAnthony Wilson const std::string& objPath = 1759d500549bSAnthony Wilson static_cast<const std::string&>(objDictEntry.first); 1760d500549bSAnthony Wilson 1761d500549bSAnthony Wilson for (InventoryItem& inventoryItem : *inventoryItems) 1762d500549bSAnthony Wilson { 1763d500549bSAnthony Wilson inventoryAssocPath = inventoryItem.objectPath; 1764d500549bSAnthony Wilson inventoryAssocPath += "/leds"; 1765d500549bSAnthony Wilson if (objPath == inventoryAssocPath) 1766d500549bSAnthony Wilson { 1767711ac7a9SEd Tanous for (const auto& [interface, values] : objDictEntry.second) 1768d500549bSAnthony Wilson { 1769711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Association") 1770711ac7a9SEd Tanous { 1771711ac7a9SEd Tanous for (const auto& [valueName, value] : values) 1772711ac7a9SEd Tanous { 1773711ac7a9SEd Tanous if (valueName == "endpoints") 1774d500549bSAnthony Wilson { 1775d500549bSAnthony Wilson const std::vector<std::string>* endpoints = 1776d500549bSAnthony Wilson std::get_if<std::vector<std::string>>( 1777711ac7a9SEd Tanous &value); 1778711ac7a9SEd Tanous if ((endpoints != nullptr) && 1779711ac7a9SEd Tanous !endpoints->empty()) 1780d500549bSAnthony Wilson { 1781711ac7a9SEd Tanous // Add inventory item to vector 1782d500549bSAnthony Wilson // Store LED path in inventory item 1783711ac7a9SEd Tanous const std::string& ledPath = 1784711ac7a9SEd Tanous endpoints->front(); 1785d500549bSAnthony Wilson inventoryItem.ledObjectPath = ledPath; 1786d500549bSAnthony Wilson } 1787d500549bSAnthony Wilson } 1788d500549bSAnthony Wilson } 1789711ac7a9SEd Tanous } 1790711ac7a9SEd Tanous } 1791711ac7a9SEd Tanous 1792d500549bSAnthony Wilson break; 1793d500549bSAnthony Wilson } 1794d500549bSAnthony Wilson } 1795d500549bSAnthony Wilson } 1796adc4f0dbSShawn McCarney callback(inventoryItems); 1797adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit"; 17988fb49dd6SShawn McCarney }; 17998fb49dd6SShawn McCarney 18008fb49dd6SShawn McCarney // Find DBus object path that implements ObjectManager for ObjectMapper 18018fb49dd6SShawn McCarney std::string connection = "xyz.openbmc_project.ObjectMapper"; 18028fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(connection); 18038fb49dd6SShawn McCarney const std::string& objectMgrPath = 18048fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 18058fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is " 18068fb49dd6SShawn McCarney << objectMgrPath; 18078fb49dd6SShawn McCarney 18088fb49dd6SShawn McCarney // Call GetManagedObjects on the ObjectMapper to get all associations 18098fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 18108fb49dd6SShawn McCarney std::move(respHandler), connection, objectMgrPath, 18118fb49dd6SShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 18128fb49dd6SShawn McCarney 1813adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit"; 18148fb49dd6SShawn McCarney } 18158fb49dd6SShawn McCarney 18168fb49dd6SShawn McCarney /** 1817d500549bSAnthony Wilson * @brief Gets D-Bus data for inventory item leds associated with sensors. 1818d500549bSAnthony Wilson * 1819d500549bSAnthony Wilson * Uses the specified connections (services) to obtain D-Bus data for inventory 1820d500549bSAnthony Wilson * item leds associated with sensors. Stores the resulting data in the 1821d500549bSAnthony Wilson * inventoryItems vector. 1822d500549bSAnthony Wilson * 1823d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 1824d500549bSAnthony Wilson * response. 1825d500549bSAnthony Wilson * 1826d500549bSAnthony Wilson * Finds the inventory item led data asynchronously. Invokes callback when data 1827d500549bSAnthony Wilson * has been obtained. 1828d500549bSAnthony Wilson * 1829d500549bSAnthony Wilson * The callback must have the following signature: 1830d500549bSAnthony Wilson * @code 183142cbe538SGunnar Mills * callback() 1832d500549bSAnthony Wilson * @endcode 1833d500549bSAnthony Wilson * 1834d500549bSAnthony Wilson * This function is called recursively, obtaining data asynchronously from one 1835d500549bSAnthony Wilson * connection in each call. This ensures the callback is not invoked until the 1836d500549bSAnthony Wilson * last asynchronous function has completed. 1837d500549bSAnthony Wilson * 1838d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 1839d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1840d500549bSAnthony Wilson * @param ledConnections Connections that provide data for the inventory leds. 1841d500549bSAnthony Wilson * @param callback Callback to invoke when inventory data has been obtained. 1842d500549bSAnthony Wilson * @param ledConnectionsIndex Current index in ledConnections. Only specified 1843d500549bSAnthony Wilson * in recursive calls to this function. 1844d500549bSAnthony Wilson */ 1845d500549bSAnthony Wilson template <typename Callback> 1846d500549bSAnthony Wilson void getInventoryLedData( 1847d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1848d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1849fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> ledConnections, 1850d500549bSAnthony Wilson Callback&& callback, size_t ledConnectionsIndex = 0) 1851d500549bSAnthony Wilson { 1852d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData enter"; 1853d500549bSAnthony Wilson 1854d500549bSAnthony Wilson // If no more connections left, call callback 1855d500549bSAnthony Wilson if (ledConnectionsIndex >= ledConnections->size()) 1856d500549bSAnthony Wilson { 185742cbe538SGunnar Mills callback(); 1858d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData exit"; 1859d500549bSAnthony Wilson return; 1860d500549bSAnthony Wilson } 1861d500549bSAnthony Wilson 1862d500549bSAnthony Wilson // Get inventory item data from current connection 1863fe04d49cSNan Zhou auto it = ledConnections->begin(); 1864fe04d49cSNan Zhou std::advance(it, ledConnectionsIndex); 1865d500549bSAnthony Wilson if (it != ledConnections->end()) 1866d500549bSAnthony Wilson { 1867d500549bSAnthony Wilson const std::string& ledPath = (*it).first; 1868d500549bSAnthony Wilson const std::string& ledConnection = (*it).second; 1869d500549bSAnthony Wilson // Response handler for Get State property 18701e1e598dSJonathan Doman auto respHandler = 18711e1e598dSJonathan Doman [sensorsAsyncResp, inventoryItems, ledConnections, ledPath, 1872f94c4ecfSEd Tanous callback{std::forward<Callback>(callback)}, ledConnectionsIndex]( 18731e1e598dSJonathan Doman const boost::system::error_code ec, const std::string& state) { 1874d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter"; 1875d500549bSAnthony Wilson if (ec) 1876d500549bSAnthony Wilson { 1877d500549bSAnthony Wilson BMCWEB_LOG_ERROR 1878d500549bSAnthony Wilson << "getInventoryLedData respHandler DBus error " << ec; 18798d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 1880d500549bSAnthony Wilson return; 1881d500549bSAnthony Wilson } 1882d500549bSAnthony Wilson 18831e1e598dSJonathan Doman BMCWEB_LOG_DEBUG << "Led state: " << state; 1884d500549bSAnthony Wilson // Find inventory item with this LED object path 1885d500549bSAnthony Wilson InventoryItem* inventoryItem = 1886d500549bSAnthony Wilson findInventoryItemForLed(*inventoryItems, ledPath); 1887d500549bSAnthony Wilson if (inventoryItem != nullptr) 1888d500549bSAnthony Wilson { 1889d500549bSAnthony Wilson // Store LED state in InventoryItem 189011ba3979SEd Tanous if (state.ends_with("On")) 1891d500549bSAnthony Wilson { 1892d500549bSAnthony Wilson inventoryItem->ledState = LedState::ON; 1893d500549bSAnthony Wilson } 189411ba3979SEd Tanous else if (state.ends_with("Blink")) 1895d500549bSAnthony Wilson { 1896d500549bSAnthony Wilson inventoryItem->ledState = LedState::BLINK; 1897d500549bSAnthony Wilson } 189811ba3979SEd Tanous else if (state.ends_with("Off")) 1899d500549bSAnthony Wilson { 1900d500549bSAnthony Wilson inventoryItem->ledState = LedState::OFF; 1901d500549bSAnthony Wilson } 1902d500549bSAnthony Wilson else 1903d500549bSAnthony Wilson { 1904d500549bSAnthony Wilson inventoryItem->ledState = LedState::UNKNOWN; 1905d500549bSAnthony Wilson } 1906d500549bSAnthony Wilson } 1907d500549bSAnthony Wilson 1908d500549bSAnthony Wilson // Recurse to get LED data from next connection 1909d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, 1910d500549bSAnthony Wilson ledConnections, std::move(callback), 1911d500549bSAnthony Wilson ledConnectionsIndex + 1); 1912d500549bSAnthony Wilson 1913d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit"; 1914d500549bSAnthony Wilson }; 1915d500549bSAnthony Wilson 1916d500549bSAnthony Wilson // Get the State property for the current LED 19171e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 19181e1e598dSJonathan Doman *crow::connections::systemBus, ledConnection, ledPath, 19191e1e598dSJonathan Doman "xyz.openbmc_project.Led.Physical", "State", 19201e1e598dSJonathan Doman std::move(respHandler)); 1921d500549bSAnthony Wilson } 1922d500549bSAnthony Wilson 1923d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData exit"; 1924d500549bSAnthony Wilson } 1925d500549bSAnthony Wilson 1926d500549bSAnthony Wilson /** 1927d500549bSAnthony Wilson * @brief Gets LED data for LEDs associated with given inventory items. 1928d500549bSAnthony Wilson * 1929d500549bSAnthony Wilson * Gets the D-Bus connections (services) that provide LED data for the LEDs 1930d500549bSAnthony Wilson * associated with the specified inventory items. Then gets the LED data from 1931d500549bSAnthony Wilson * each connection and stores it in the inventory item. 1932d500549bSAnthony Wilson * 1933d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 1934d500549bSAnthony Wilson * response. 1935d500549bSAnthony Wilson * 1936d500549bSAnthony Wilson * Finds the LED data asynchronously. Invokes callback when information has 1937d500549bSAnthony Wilson * been obtained. 1938d500549bSAnthony Wilson * 1939d500549bSAnthony Wilson * The callback must have the following signature: 1940d500549bSAnthony Wilson * @code 194142cbe538SGunnar Mills * callback() 1942d500549bSAnthony Wilson * @endcode 1943d500549bSAnthony Wilson * 1944d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 1945d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1946d500549bSAnthony Wilson * @param callback Callback to invoke when inventory items have been obtained. 1947d500549bSAnthony Wilson */ 1948d500549bSAnthony Wilson template <typename Callback> 1949d500549bSAnthony Wilson void getInventoryLeds( 1950d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1951d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1952d500549bSAnthony Wilson Callback&& callback) 1953d500549bSAnthony Wilson { 1954d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds enter"; 1955d500549bSAnthony Wilson 1956d500549bSAnthony Wilson const std::string path = "/xyz/openbmc_project"; 1957d500549bSAnthony Wilson const std::array<std::string, 1> interfaces = { 1958d500549bSAnthony Wilson "xyz.openbmc_project.Led.Physical"}; 1959d500549bSAnthony Wilson 1960d500549bSAnthony Wilson // Response handler for parsing output from GetSubTree 1961002d39b4SEd Tanous auto respHandler = 1962002d39b4SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 1963002d39b4SEd Tanous inventoryItems]( 1964b9d36b47SEd Tanous const boost::system::error_code ec, 1965002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 1966d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter"; 1967d500549bSAnthony Wilson if (ec) 1968d500549bSAnthony Wilson { 19698d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 1970d500549bSAnthony Wilson BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error " 1971d500549bSAnthony Wilson << ec; 1972d500549bSAnthony Wilson return; 1973d500549bSAnthony Wilson } 1974d500549bSAnthony Wilson 1975d500549bSAnthony Wilson // Build map of LED object paths to connections 1976fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> ledConnections = 1977fe04d49cSNan Zhou std::make_shared<std::map<std::string, std::string>>(); 1978d500549bSAnthony Wilson 1979d500549bSAnthony Wilson // Loop through objects from GetSubTree 1980d500549bSAnthony Wilson for (const std::pair< 1981d500549bSAnthony Wilson std::string, 1982d500549bSAnthony Wilson std::vector<std::pair<std::string, std::vector<std::string>>>>& 1983d500549bSAnthony Wilson object : subtree) 1984d500549bSAnthony Wilson { 1985d500549bSAnthony Wilson // Check if object path is LED for one of the specified inventory 1986d500549bSAnthony Wilson // items 1987d500549bSAnthony Wilson const std::string& ledPath = object.first; 1988d500549bSAnthony Wilson if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr) 1989d500549bSAnthony Wilson { 1990d500549bSAnthony Wilson // Add mapping from ledPath to connection 1991d500549bSAnthony Wilson const std::string& connection = object.second.begin()->first; 1992d500549bSAnthony Wilson (*ledConnections)[ledPath] = connection; 1993d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> " 1994d500549bSAnthony Wilson << connection; 1995d500549bSAnthony Wilson } 1996d500549bSAnthony Wilson } 1997d500549bSAnthony Wilson 1998d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections, 1999d500549bSAnthony Wilson std::move(callback)); 2000d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit"; 2001d500549bSAnthony Wilson }; 2002d500549bSAnthony Wilson // Make call to ObjectMapper to find all inventory items 2003d500549bSAnthony Wilson crow::connections::systemBus->async_method_call( 2004d500549bSAnthony Wilson std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 2005d500549bSAnthony Wilson "/xyz/openbmc_project/object_mapper", 2006d500549bSAnthony Wilson "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); 2007d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds exit"; 2008d500549bSAnthony Wilson } 2009d500549bSAnthony Wilson 2010d500549bSAnthony Wilson /** 201142cbe538SGunnar Mills * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent 201242cbe538SGunnar Mills * 201342cbe538SGunnar Mills * Uses the specified connections (services) (currently assumes just one) to 201442cbe538SGunnar Mills * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in 201542cbe538SGunnar Mills * the inventoryItems vector. Only stores data in Power Supply inventoryItems. 201642cbe538SGunnar Mills * 201742cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 201842cbe538SGunnar Mills * response. 201942cbe538SGunnar Mills * 202042cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 202142cbe538SGunnar Mills * when data has been obtained. 202242cbe538SGunnar Mills * 202342cbe538SGunnar Mills * The callback must have the following signature: 202442cbe538SGunnar Mills * @code 202542cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 202642cbe538SGunnar Mills * @endcode 202742cbe538SGunnar Mills * 202842cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 202942cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 203042cbe538SGunnar Mills * @param psAttributesConnections Connections that provide data for the Power 203142cbe538SGunnar Mills * Supply Attributes 203242cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 203342cbe538SGunnar Mills */ 203442cbe538SGunnar Mills template <typename Callback> 203542cbe538SGunnar Mills void getPowerSupplyAttributesData( 2036b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 203742cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 2038fe04d49cSNan Zhou const std::map<std::string, std::string>& psAttributesConnections, 203942cbe538SGunnar Mills Callback&& callback) 204042cbe538SGunnar Mills { 204142cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter"; 204242cbe538SGunnar Mills 204342cbe538SGunnar Mills if (psAttributesConnections.empty()) 204442cbe538SGunnar Mills { 204542cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!"; 204642cbe538SGunnar Mills callback(inventoryItems); 204742cbe538SGunnar Mills return; 204842cbe538SGunnar Mills } 204942cbe538SGunnar Mills 205042cbe538SGunnar Mills // Assuming just one connection (service) for now 2051fe04d49cSNan Zhou auto it = psAttributesConnections.begin(); 205242cbe538SGunnar Mills 205342cbe538SGunnar Mills const std::string& psAttributesPath = (*it).first; 205442cbe538SGunnar Mills const std::string& psAttributesConnection = (*it).second; 205542cbe538SGunnar Mills 205642cbe538SGunnar Mills // Response handler for Get DeratingFactor property 2057002d39b4SEd Tanous auto respHandler = 2058002d39b4SEd Tanous [sensorsAsyncResp, inventoryItems, 2059f94c4ecfSEd Tanous callback{std::forward<Callback>(callback)}]( 2060002d39b4SEd Tanous const boost::system::error_code ec, const uint32_t value) { 206142cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter"; 206242cbe538SGunnar Mills if (ec) 206342cbe538SGunnar Mills { 206442cbe538SGunnar Mills BMCWEB_LOG_ERROR 206542cbe538SGunnar Mills << "getPowerSupplyAttributesData respHandler DBus error " << ec; 20668d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 206742cbe538SGunnar Mills return; 206842cbe538SGunnar Mills } 206942cbe538SGunnar Mills 20701e1e598dSJonathan Doman BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value; 207142cbe538SGunnar Mills // Store value in Power Supply Inventory Items 207242cbe538SGunnar Mills for (InventoryItem& inventoryItem : *inventoryItems) 207342cbe538SGunnar Mills { 207455f79e6fSEd Tanous if (inventoryItem.isPowerSupply) 207542cbe538SGunnar Mills { 207642cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent = 20771e1e598dSJonathan Doman static_cast<int>(value); 207842cbe538SGunnar Mills } 207942cbe538SGunnar Mills } 208042cbe538SGunnar Mills 208142cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit"; 208242cbe538SGunnar Mills callback(inventoryItems); 208342cbe538SGunnar Mills }; 208442cbe538SGunnar Mills 208542cbe538SGunnar Mills // Get the DeratingFactor property for the PowerSupplyAttributes 208642cbe538SGunnar Mills // Currently only property on the interface/only one we care about 20871e1e598dSJonathan Doman sdbusplus::asio::getProperty<uint32_t>( 20881e1e598dSJonathan Doman *crow::connections::systemBus, psAttributesConnection, psAttributesPath, 20891e1e598dSJonathan Doman "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor", 20901e1e598dSJonathan Doman std::move(respHandler)); 209142cbe538SGunnar Mills 209242cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit"; 209342cbe538SGunnar Mills } 209442cbe538SGunnar Mills 209542cbe538SGunnar Mills /** 209642cbe538SGunnar Mills * @brief Gets the Power Supply Attributes such as EfficiencyPercent 209742cbe538SGunnar Mills * 209842cbe538SGunnar Mills * Gets the D-Bus connection (service) that provides Power Supply Attributes 209942cbe538SGunnar Mills * data. Then gets the Power Supply Attributes data from the connection 210042cbe538SGunnar Mills * (currently just assumes 1 connection) and stores the data in the inventory 210142cbe538SGunnar Mills * item. 210242cbe538SGunnar Mills * 210342cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 210442cbe538SGunnar Mills * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish. 210542cbe538SGunnar Mills * 210642cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 210742cbe538SGunnar Mills * when information has been obtained. 210842cbe538SGunnar Mills * 210942cbe538SGunnar Mills * The callback must have the following signature: 211042cbe538SGunnar Mills * @code 211142cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 211242cbe538SGunnar Mills * @endcode 211342cbe538SGunnar Mills * 211442cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 211542cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 211642cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 211742cbe538SGunnar Mills */ 211842cbe538SGunnar Mills template <typename Callback> 211942cbe538SGunnar Mills void getPowerSupplyAttributes( 212042cbe538SGunnar Mills std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 212142cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 212242cbe538SGunnar Mills Callback&& callback) 212342cbe538SGunnar Mills { 212442cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter"; 212542cbe538SGunnar Mills 212642cbe538SGunnar Mills // Only need the power supply attributes when the Power Schema 2127a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode != sensors::node::power) 212842cbe538SGunnar Mills { 212942cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power"; 213042cbe538SGunnar Mills callback(inventoryItems); 213142cbe538SGunnar Mills return; 213242cbe538SGunnar Mills } 213342cbe538SGunnar Mills 213442cbe538SGunnar Mills const std::array<std::string, 1> interfaces = { 213542cbe538SGunnar Mills "xyz.openbmc_project.Control.PowerSupplyAttributes"}; 213642cbe538SGunnar Mills 213742cbe538SGunnar Mills // Response handler for parsing output from GetSubTree 2138b9d36b47SEd Tanous auto respHandler = 2139b9d36b47SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 2140b9d36b47SEd Tanous inventoryItems]( 2141b9d36b47SEd Tanous const boost::system::error_code ec, 2142b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 214342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter"; 214442cbe538SGunnar Mills if (ec) 214542cbe538SGunnar Mills { 21468d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 214742cbe538SGunnar Mills BMCWEB_LOG_ERROR 214842cbe538SGunnar Mills << "getPowerSupplyAttributes respHandler DBus error " << ec; 214942cbe538SGunnar Mills return; 215042cbe538SGunnar Mills } 215126f6976fSEd Tanous if (subtree.empty()) 215242cbe538SGunnar Mills { 215342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!"; 215442cbe538SGunnar Mills callback(inventoryItems); 215542cbe538SGunnar Mills return; 215642cbe538SGunnar Mills } 215742cbe538SGunnar Mills 215842cbe538SGunnar Mills // Currently we only support 1 power supply attribute, use this for 215942cbe538SGunnar Mills // all the power supplies. Build map of object path to connection. 216042cbe538SGunnar Mills // Assume just 1 connection and 1 path for now. 2161fe04d49cSNan Zhou std::map<std::string, std::string> psAttributesConnections; 216242cbe538SGunnar Mills 216342cbe538SGunnar Mills if (subtree[0].first.empty() || subtree[0].second.empty()) 216442cbe538SGunnar Mills { 216542cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!"; 216642cbe538SGunnar Mills callback(inventoryItems); 216742cbe538SGunnar Mills return; 216842cbe538SGunnar Mills } 216942cbe538SGunnar Mills 217042cbe538SGunnar Mills const std::string& psAttributesPath = subtree[0].first; 217142cbe538SGunnar Mills const std::string& connection = subtree[0].second.begin()->first; 217242cbe538SGunnar Mills 217342cbe538SGunnar Mills if (connection.empty()) 217442cbe538SGunnar Mills { 217542cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!"; 217642cbe538SGunnar Mills callback(inventoryItems); 217742cbe538SGunnar Mills return; 217842cbe538SGunnar Mills } 217942cbe538SGunnar Mills 218042cbe538SGunnar Mills psAttributesConnections[psAttributesPath] = connection; 218142cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> " 218242cbe538SGunnar Mills << connection; 218342cbe538SGunnar Mills 218442cbe538SGunnar Mills getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems, 218542cbe538SGunnar Mills psAttributesConnections, 218642cbe538SGunnar Mills std::move(callback)); 218742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit"; 218842cbe538SGunnar Mills }; 218942cbe538SGunnar Mills // Make call to ObjectMapper to find the PowerSupplyAttributes service 219042cbe538SGunnar Mills crow::connections::systemBus->async_method_call( 219142cbe538SGunnar Mills std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 219242cbe538SGunnar Mills "/xyz/openbmc_project/object_mapper", 219342cbe538SGunnar Mills "xyz.openbmc_project.ObjectMapper", "GetSubTree", 219442cbe538SGunnar Mills "/xyz/openbmc_project", 0, interfaces); 219542cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit"; 219642cbe538SGunnar Mills } 219742cbe538SGunnar Mills 219842cbe538SGunnar Mills /** 2199adc4f0dbSShawn McCarney * @brief Gets inventory items associated with sensors. 22008fb49dd6SShawn McCarney * 22018fb49dd6SShawn McCarney * Finds the inventory items that are associated with the specified sensors. 2202adc4f0dbSShawn McCarney * Then gets D-Bus data for the inventory items, such as presence and VPD. 22038fb49dd6SShawn McCarney * 2204adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 2205adc4f0dbSShawn McCarney * response. 22068fb49dd6SShawn McCarney * 2207adc4f0dbSShawn McCarney * Finds the inventory items asynchronously. Invokes callback when the 2208adc4f0dbSShawn McCarney * inventory items have been obtained. 2209adc4f0dbSShawn McCarney * 2210adc4f0dbSShawn McCarney * The callback must have the following signature: 2211adc4f0dbSShawn McCarney * @code 2212adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 2213adc4f0dbSShawn McCarney * @endcode 22148fb49dd6SShawn McCarney * 22158fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 22168fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 22178fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 22188fb49dd6SShawn McCarney * implements ObjectManager. 2219adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 22208fb49dd6SShawn McCarney */ 2221adc4f0dbSShawn McCarney template <typename Callback> 2222adc4f0dbSShawn McCarney static void getInventoryItems( 22238fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 2224fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> sensorNames, 2225fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths, 2226adc4f0dbSShawn McCarney Callback&& callback) 22278fb49dd6SShawn McCarney { 2228adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItems enter"; 2229adc4f0dbSShawn McCarney auto getInventoryItemAssociationsCb = 2230f94c4ecfSEd Tanous [sensorsAsyncResp, objectMgrPaths, 2231f94c4ecfSEd Tanous callback{std::forward<Callback>(callback)}]( 2232adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems) { 2233adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter"; 22348fb49dd6SShawn McCarney auto getInventoryItemsConnectionsCb = 2235adc4f0dbSShawn McCarney [sensorsAsyncResp, inventoryItems, objectMgrPaths, 2236f94c4ecfSEd Tanous callback{std::forward<const Callback>(callback)}]( 2237fe04d49cSNan Zhou std::shared_ptr<std::set<std::string>> invConnections) { 22388fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter"; 2239002d39b4SEd Tanous auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems, 2240d500549bSAnthony Wilson callback{std::move(callback)}]() { 2241d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter"; 224242cbe538SGunnar Mills 2243002d39b4SEd Tanous auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems, 2244002d39b4SEd Tanous callback{std::move(callback)}]() { 224542cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter"; 224642cbe538SGunnar Mills // Find Power Supply Attributes and get the data 2247002d39b4SEd Tanous getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems, 224842cbe538SGunnar Mills std::move(callback)); 224942cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit"; 225042cbe538SGunnar Mills }; 225142cbe538SGunnar Mills 2252d500549bSAnthony Wilson // Find led connections and get the data 2253d500549bSAnthony Wilson getInventoryLeds(sensorsAsyncResp, inventoryItems, 225442cbe538SGunnar Mills std::move(getInventoryLedsCb)); 2255d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit"; 2256d500549bSAnthony Wilson }; 22578fb49dd6SShawn McCarney 2258adc4f0dbSShawn McCarney // Get inventory item data from connections 2259adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 2260adc4f0dbSShawn McCarney invConnections, objectMgrPaths, 2261d500549bSAnthony Wilson std::move(getInventoryItemsDataCb)); 22628fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit"; 22638fb49dd6SShawn McCarney }; 22648fb49dd6SShawn McCarney 2265adc4f0dbSShawn McCarney // Get connections that provide inventory item data 2266002d39b4SEd Tanous getInventoryItemsConnections(sensorsAsyncResp, inventoryItems, 22678fb49dd6SShawn McCarney std::move(getInventoryItemsConnectionsCb)); 2268adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit"; 22698fb49dd6SShawn McCarney }; 22708fb49dd6SShawn McCarney 2271adc4f0dbSShawn McCarney // Get associations from sensors to inventory items 2272adc4f0dbSShawn McCarney getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths, 2273adc4f0dbSShawn McCarney std::move(getInventoryItemAssociationsCb)); 2274adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItems exit"; 2275adc4f0dbSShawn McCarney } 2276adc4f0dbSShawn McCarney 2277adc4f0dbSShawn McCarney /** 2278adc4f0dbSShawn McCarney * @brief Returns JSON PowerSupply object for the specified inventory item. 2279adc4f0dbSShawn McCarney * 2280adc4f0dbSShawn McCarney * Searches for a JSON PowerSupply object that matches the specified inventory 2281adc4f0dbSShawn McCarney * item. If one is not found, a new PowerSupply object is added to the JSON 2282adc4f0dbSShawn McCarney * array. 2283adc4f0dbSShawn McCarney * 2284adc4f0dbSShawn McCarney * Multiple sensors are often associated with one power supply inventory item. 2285adc4f0dbSShawn McCarney * As a result, multiple sensor values are stored in one JSON PowerSupply 2286adc4f0dbSShawn McCarney * object. 2287adc4f0dbSShawn McCarney * 2288adc4f0dbSShawn McCarney * @param powerSupplyArray JSON array containing Redfish PowerSupply objects. 2289adc4f0dbSShawn McCarney * @param inventoryItem Inventory item for the power supply. 2290adc4f0dbSShawn McCarney * @param chassisId Chassis that contains the power supply. 2291adc4f0dbSShawn McCarney * @return JSON PowerSupply object for the specified inventory item. 2292adc4f0dbSShawn McCarney */ 229323a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray, 2294adc4f0dbSShawn McCarney const InventoryItem& inventoryItem, 2295adc4f0dbSShawn McCarney const std::string& chassisId) 2296adc4f0dbSShawn McCarney { 2297adc4f0dbSShawn McCarney // Check if matching PowerSupply object already exists in JSON array 2298adc4f0dbSShawn McCarney for (nlohmann::json& powerSupply : powerSupplyArray) 2299adc4f0dbSShawn McCarney { 2300adc4f0dbSShawn McCarney if (powerSupply["MemberId"] == inventoryItem.name) 2301adc4f0dbSShawn McCarney { 2302adc4f0dbSShawn McCarney return powerSupply; 2303adc4f0dbSShawn McCarney } 2304adc4f0dbSShawn McCarney } 2305adc4f0dbSShawn McCarney 2306adc4f0dbSShawn McCarney // Add new PowerSupply object to JSON array 2307adc4f0dbSShawn McCarney powerSupplyArray.push_back({}); 2308adc4f0dbSShawn McCarney nlohmann::json& powerSupply = powerSupplyArray.back(); 2309adc4f0dbSShawn McCarney powerSupply["@odata.id"] = 2310adc4f0dbSShawn McCarney "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/"; 2311adc4f0dbSShawn McCarney powerSupply["MemberId"] = inventoryItem.name; 2312adc4f0dbSShawn McCarney powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " "); 2313adc4f0dbSShawn McCarney powerSupply["Manufacturer"] = inventoryItem.manufacturer; 2314adc4f0dbSShawn McCarney powerSupply["Model"] = inventoryItem.model; 2315adc4f0dbSShawn McCarney powerSupply["PartNumber"] = inventoryItem.partNumber; 2316adc4f0dbSShawn McCarney powerSupply["SerialNumber"] = inventoryItem.serialNumber; 2317d500549bSAnthony Wilson setLedState(powerSupply, &inventoryItem); 2318adc4f0dbSShawn McCarney 231942cbe538SGunnar Mills if (inventoryItem.powerSupplyEfficiencyPercent >= 0) 232042cbe538SGunnar Mills { 232142cbe538SGunnar Mills powerSupply["EfficiencyPercent"] = 232242cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent; 232342cbe538SGunnar Mills } 232442cbe538SGunnar Mills 232542cbe538SGunnar Mills powerSupply["Status"]["State"] = getState(&inventoryItem); 2326adc4f0dbSShawn McCarney const char* health = inventoryItem.isFunctional ? "OK" : "Critical"; 2327adc4f0dbSShawn McCarney powerSupply["Status"]["Health"] = health; 2328adc4f0dbSShawn McCarney 2329adc4f0dbSShawn McCarney return powerSupply; 23308fb49dd6SShawn McCarney } 23318fb49dd6SShawn McCarney 23328fb49dd6SShawn McCarney /** 2333de629b6eSShawn McCarney * @brief Gets the values of the specified sensors. 2334de629b6eSShawn McCarney * 2335de629b6eSShawn McCarney * Stores the results as JSON in the SensorsAsyncResp. 2336de629b6eSShawn McCarney * 2337de629b6eSShawn McCarney * Gets the sensor values asynchronously. Stores the results later when the 2338de629b6eSShawn McCarney * information has been obtained. 2339de629b6eSShawn McCarney * 2340adc4f0dbSShawn McCarney * The sensorNames set contains all requested sensors for the current chassis. 2341de629b6eSShawn McCarney * 2342de629b6eSShawn McCarney * To minimize the number of DBus calls, the DBus method 2343de629b6eSShawn McCarney * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the 2344de629b6eSShawn McCarney * values of all sensors provided by a connection (service). 2345de629b6eSShawn McCarney * 2346de629b6eSShawn McCarney * The connections set contains all the connections that provide sensor values. 2347de629b6eSShawn McCarney * 2348de629b6eSShawn McCarney * The objectMgrPaths map contains mappings from a connection name to the 2349de629b6eSShawn McCarney * corresponding DBus object path that implements ObjectManager. 2350de629b6eSShawn McCarney * 2351adc4f0dbSShawn McCarney * The InventoryItem vector contains D-Bus inventory items associated with the 2352adc4f0dbSShawn McCarney * sensors. Inventory item data is needed for some Redfish sensor properties. 2353adc4f0dbSShawn McCarney * 2354de629b6eSShawn McCarney * @param SensorsAsyncResp Pointer to object holding response data. 2355adc4f0dbSShawn McCarney * @param sensorNames All requested sensors within the current chassis. 2356de629b6eSShawn McCarney * @param connections Connections that provide sensor values. 2357de629b6eSShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 2358de629b6eSShawn McCarney * implements ObjectManager. 2359adc4f0dbSShawn McCarney * @param inventoryItems Inventory items associated with the sensors. 2360de629b6eSShawn McCarney */ 236123a21a1cSEd Tanous inline void getSensorData( 236281ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 2363fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames, 2364fe04d49cSNan Zhou const std::set<std::string>& connections, 2365fe04d49cSNan Zhou const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths, 2366b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems) 2367de629b6eSShawn McCarney { 2368de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getSensorData enter"; 2369de629b6eSShawn McCarney // Get managed objects from all services exposing sensors 2370de629b6eSShawn McCarney for (const std::string& connection : connections) 2371de629b6eSShawn McCarney { 2372de629b6eSShawn McCarney // Response handler to process managed objects 2373002d39b4SEd Tanous auto getManagedObjectsCb = 2374002d39b4SEd Tanous [sensorsAsyncResp, sensorNames, 2375002d39b4SEd Tanous inventoryItems](const boost::system::error_code ec, 237602cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 2377de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter"; 2378de629b6eSShawn McCarney if (ec) 2379de629b6eSShawn McCarney { 2380de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec; 23818d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 2382de629b6eSShawn McCarney return; 2383de629b6eSShawn McCarney } 2384de629b6eSShawn McCarney // Go through all objects and update response with sensor data 2385de629b6eSShawn McCarney for (const auto& objDictEntry : resp) 2386de629b6eSShawn McCarney { 2387de629b6eSShawn McCarney const std::string& objPath = 2388de629b6eSShawn McCarney static_cast<const std::string&>(objDictEntry.first); 2389de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object " 2390de629b6eSShawn McCarney << objPath; 2391de629b6eSShawn McCarney 2392de629b6eSShawn McCarney std::vector<std::string> split; 2393de629b6eSShawn McCarney // Reserve space for 2394de629b6eSShawn McCarney // /xyz/openbmc_project/sensors/<name>/<subname> 2395de629b6eSShawn McCarney split.reserve(6); 2396de629b6eSShawn McCarney boost::algorithm::split(split, objPath, boost::is_any_of("/")); 2397de629b6eSShawn McCarney if (split.size() < 6) 2398de629b6eSShawn McCarney { 2399de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "Got path that isn't long enough " 2400de629b6eSShawn McCarney << objPath; 2401de629b6eSShawn McCarney continue; 2402de629b6eSShawn McCarney } 2403de629b6eSShawn McCarney // These indexes aren't intuitive, as boost::split puts an empty 2404de629b6eSShawn McCarney // string at the beginning 2405de629b6eSShawn McCarney const std::string& sensorType = split[4]; 2406de629b6eSShawn McCarney const std::string& sensorName = split[5]; 2407de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "sensorName " << sensorName 2408de629b6eSShawn McCarney << " sensorType " << sensorType; 240949c53ac9SJohnathan Mantey if (sensorNames->find(objPath) == sensorNames->end()) 2410de629b6eSShawn McCarney { 2411accdbb2cSAndrew Geissler BMCWEB_LOG_DEBUG << sensorName << " not in sensor list "; 2412de629b6eSShawn McCarney continue; 2413de629b6eSShawn McCarney } 2414de629b6eSShawn McCarney 2415adc4f0dbSShawn McCarney // Find inventory item (if any) associated with sensor 2416adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 2417adc4f0dbSShawn McCarney findInventoryItemForSensor(inventoryItems, objPath); 2418adc4f0dbSShawn McCarney 241995a3ecadSAnthony Wilson const std::string& sensorSchema = 242081ce609eSEd Tanous sensorsAsyncResp->chassisSubNode; 242195a3ecadSAnthony Wilson 242295a3ecadSAnthony Wilson nlohmann::json* sensorJson = nullptr; 242395a3ecadSAnthony Wilson 2424928fefb9SNan Zhou if (sensorSchema == sensors::node::sensors && 2425928fefb9SNan Zhou !sensorsAsyncResp->efficientExpand) 242695a3ecadSAnthony Wilson { 2427c1d019a6SEd Tanous std::string sensorTypeEscaped(sensorType); 2428c1d019a6SEd Tanous sensorTypeEscaped.erase( 2429c1d019a6SEd Tanous std::remove(sensorTypeEscaped.begin(), 2430c1d019a6SEd Tanous sensorTypeEscaped.end(), '_'), 2431c1d019a6SEd Tanous sensorTypeEscaped.end()); 2432c1d019a6SEd Tanous std::string sensorId(sensorTypeEscaped); 2433c1d019a6SEd Tanous sensorId += "_"; 2434c1d019a6SEd Tanous sensorId += sensorName; 2435c1d019a6SEd Tanous 24368d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] = 2437c1d019a6SEd Tanous crow::utility::urlFromPieces( 2438c1d019a6SEd Tanous "redfish", "v1", "Chassis", 2439c1d019a6SEd Tanous sensorsAsyncResp->chassisId, 2440c1d019a6SEd Tanous sensorsAsyncResp->chassisSubNode, sensorId); 24418d1b46d7Szhanghch05 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue); 244295a3ecadSAnthony Wilson } 244395a3ecadSAnthony Wilson else 244495a3ecadSAnthony Wilson { 2445271584abSEd Tanous std::string fieldName; 2446928fefb9SNan Zhou if (sensorsAsyncResp->efficientExpand) 2447928fefb9SNan Zhou { 2448928fefb9SNan Zhou fieldName = "Members"; 2449928fefb9SNan Zhou } 2450928fefb9SNan Zhou else if (sensorType == "temperature") 2451de629b6eSShawn McCarney { 2452de629b6eSShawn McCarney fieldName = "Temperatures"; 2453de629b6eSShawn McCarney } 2454de629b6eSShawn McCarney else if (sensorType == "fan" || sensorType == "fan_tach" || 2455de629b6eSShawn McCarney sensorType == "fan_pwm") 2456de629b6eSShawn McCarney { 2457de629b6eSShawn McCarney fieldName = "Fans"; 2458de629b6eSShawn McCarney } 2459de629b6eSShawn McCarney else if (sensorType == "voltage") 2460de629b6eSShawn McCarney { 2461de629b6eSShawn McCarney fieldName = "Voltages"; 2462de629b6eSShawn McCarney } 2463de629b6eSShawn McCarney else if (sensorType == "power") 2464de629b6eSShawn McCarney { 246555f79e6fSEd Tanous if (sensorName == "total_power") 2466028f7ebcSEddie James { 2467028f7ebcSEddie James fieldName = "PowerControl"; 2468028f7ebcSEddie James } 2469adc4f0dbSShawn McCarney else if ((inventoryItem != nullptr) && 2470adc4f0dbSShawn McCarney (inventoryItem->isPowerSupply)) 2471028f7ebcSEddie James { 2472de629b6eSShawn McCarney fieldName = "PowerSupplies"; 2473de629b6eSShawn McCarney } 2474adc4f0dbSShawn McCarney else 2475adc4f0dbSShawn McCarney { 2476adc4f0dbSShawn McCarney // Other power sensors are in SensorCollection 2477adc4f0dbSShawn McCarney continue; 2478adc4f0dbSShawn McCarney } 2479028f7ebcSEddie James } 2480de629b6eSShawn McCarney else 2481de629b6eSShawn McCarney { 2482de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "Unsure how to handle sensorType " 2483de629b6eSShawn McCarney << sensorType; 2484de629b6eSShawn McCarney continue; 2485de629b6eSShawn McCarney } 2486de629b6eSShawn McCarney 2487de629b6eSShawn McCarney nlohmann::json& tempArray = 24888d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName]; 2489adc4f0dbSShawn McCarney if (fieldName == "PowerControl") 249049c53ac9SJohnathan Mantey { 2491adc4f0dbSShawn McCarney if (tempArray.empty()) 24927ab06f49SGunnar Mills { 249395a3ecadSAnthony Wilson // Put multiple "sensors" into a single 249495a3ecadSAnthony Wilson // PowerControl. Follows MemberId naming and 249595a3ecadSAnthony Wilson // naming in power.hpp. 24961476687dSEd Tanous nlohmann::json::object_t power; 24971476687dSEd Tanous power["@odata.id"] = 2498adc4f0dbSShawn McCarney "/redfish/v1/Chassis/" + 249981ce609eSEd Tanous sensorsAsyncResp->chassisId + "/" + 250081ce609eSEd Tanous sensorsAsyncResp->chassisSubNode + "#/" + 25011476687dSEd Tanous fieldName + "/0"; 25021476687dSEd Tanous tempArray.push_back(std::move(power)); 2503adc4f0dbSShawn McCarney } 2504adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 2505adc4f0dbSShawn McCarney } 2506adc4f0dbSShawn McCarney else if (fieldName == "PowerSupplies") 2507adc4f0dbSShawn McCarney { 2508adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 2509adc4f0dbSShawn McCarney { 2510adc4f0dbSShawn McCarney sensorJson = 2511adc4f0dbSShawn McCarney &(getPowerSupply(tempArray, *inventoryItem, 251281ce609eSEd Tanous sensorsAsyncResp->chassisId)); 2513adc4f0dbSShawn McCarney } 251449c53ac9SJohnathan Mantey } 2515928fefb9SNan Zhou else if (fieldName == "Members") 2516928fefb9SNan Zhou { 2517677bb756SEd Tanous std::string sensorTypeEscaped(sensorType); 2518677bb756SEd Tanous sensorTypeEscaped.erase( 2519677bb756SEd Tanous std::remove(sensorTypeEscaped.begin(), 2520677bb756SEd Tanous sensorTypeEscaped.end(), '_'), 2521677bb756SEd Tanous sensorTypeEscaped.end()); 2522677bb756SEd Tanous std::string sensorId(sensorTypeEscaped); 2523677bb756SEd Tanous sensorId += "_"; 2524677bb756SEd Tanous sensorId += sensorName; 2525677bb756SEd Tanous 25261476687dSEd Tanous nlohmann::json::object_t member; 2527677bb756SEd Tanous member["@odata.id"] = crow::utility::urlFromPieces( 2528677bb756SEd Tanous "redfish", "v1", "Chassis", 2529677bb756SEd Tanous sensorsAsyncResp->chassisId, 2530677bb756SEd Tanous sensorsAsyncResp->chassisSubNode, sensorId); 25311476687dSEd Tanous tempArray.push_back(std::move(member)); 2532928fefb9SNan Zhou sensorJson = &(tempArray.back()); 2533928fefb9SNan Zhou } 253449c53ac9SJohnathan Mantey else 253549c53ac9SJohnathan Mantey { 25361476687dSEd Tanous nlohmann::json::object_t member; 25371476687dSEd Tanous member["@odata.id"] = "/redfish/v1/Chassis/" + 25381476687dSEd Tanous sensorsAsyncResp->chassisId + 25391476687dSEd Tanous "/" + 25401476687dSEd Tanous sensorsAsyncResp->chassisSubNode + 25411476687dSEd Tanous "#/" + fieldName + "/"; 25421476687dSEd Tanous tempArray.push_back(std::move(member)); 2543adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 254449c53ac9SJohnathan Mantey } 254595a3ecadSAnthony Wilson } 2546de629b6eSShawn McCarney 2547adc4f0dbSShawn McCarney if (sensorJson != nullptr) 2548adc4f0dbSShawn McCarney { 25491d7c0054SEd Tanous objectInterfacesToJson(sensorName, sensorType, 25501d7c0054SEd Tanous sensorsAsyncResp->chassisSubNode, 25511d7c0054SEd Tanous objDictEntry.second, *sensorJson, 25521d7c0054SEd Tanous inventoryItem); 25531d7c0054SEd Tanous 25541d7c0054SEd Tanous std::string path = "/xyz/openbmc_project/sensors/"; 25551d7c0054SEd Tanous path += sensorType; 25561d7c0054SEd Tanous path += "/"; 25571d7c0054SEd Tanous path += sensorName; 2558c1d019a6SEd Tanous sensorsAsyncResp->addMetadata(*sensorJson, path); 2559adc4f0dbSShawn McCarney } 2560de629b6eSShawn McCarney } 256181ce609eSEd Tanous if (sensorsAsyncResp.use_count() == 1) 256249c53ac9SJohnathan Mantey { 256381ce609eSEd Tanous sortJSONResponse(sensorsAsyncResp); 2564928fefb9SNan Zhou if (sensorsAsyncResp->chassisSubNode == 2565928fefb9SNan Zhou sensors::node::sensors && 2566928fefb9SNan Zhou sensorsAsyncResp->efficientExpand) 2567928fefb9SNan Zhou { 2568928fefb9SNan Zhou sensorsAsyncResp->asyncResp->res 2569928fefb9SNan Zhou .jsonValue["Members@odata.count"] = 2570928fefb9SNan Zhou sensorsAsyncResp->asyncResp->res.jsonValue["Members"] 2571928fefb9SNan Zhou .size(); 2572928fefb9SNan Zhou } 2573928fefb9SNan Zhou else if (sensorsAsyncResp->chassisSubNode == 2574928fefb9SNan Zhou sensors::node::thermal) 25758bd25ccdSJames Feist { 257681ce609eSEd Tanous populateFanRedundancy(sensorsAsyncResp); 25778bd25ccdSJames Feist } 257849c53ac9SJohnathan Mantey } 2579de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit"; 2580de629b6eSShawn McCarney }; 2581de629b6eSShawn McCarney 2582de629b6eSShawn McCarney // Find DBus object path that implements ObjectManager for the current 2583de629b6eSShawn McCarney // connection. If no mapping found, default to "/". 25848fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(connection); 2585de629b6eSShawn McCarney const std::string& objectMgrPath = 25868fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 2587de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is " 2588de629b6eSShawn McCarney << objectMgrPath; 2589de629b6eSShawn McCarney 2590de629b6eSShawn McCarney crow::connections::systemBus->async_method_call( 2591de629b6eSShawn McCarney getManagedObjectsCb, connection, objectMgrPath, 2592de629b6eSShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 259323a21a1cSEd Tanous } 2594de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getSensorData exit"; 2595de629b6eSShawn McCarney } 2596de629b6eSShawn McCarney 2597fe04d49cSNan Zhou inline void 2598fe04d49cSNan Zhou processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 2599fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames) 26001abe55efSEd Tanous { 2601fe04d49cSNan Zhou auto getConnectionCb = [sensorsAsyncResp, sensorNames]( 2602fe04d49cSNan Zhou const std::set<std::string>& connections) { 260355c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getConnectionCb enter"; 2604de629b6eSShawn McCarney auto getObjectManagerPathsCb = 2605fe04d49cSNan Zhou [sensorsAsyncResp, sensorNames, connections]( 2606fe04d49cSNan Zhou const std::shared_ptr<std::map<std::string, std::string>>& 2607fe04d49cSNan Zhou objectMgrPaths) { 2608de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter"; 2609adc4f0dbSShawn McCarney auto getInventoryItemsCb = 2610002d39b4SEd Tanous [sensorsAsyncResp, sensorNames, connections, objectMgrPaths]( 2611f23b7296SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& 2612adc4f0dbSShawn McCarney inventoryItems) { 2613adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter"; 261449c53ac9SJohnathan Mantey // Get sensor data and store results in JSON 2615002d39b4SEd Tanous getSensorData(sensorsAsyncResp, sensorNames, connections, 2616002d39b4SEd Tanous objectMgrPaths, inventoryItems); 2617adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit"; 2618adc4f0dbSShawn McCarney }; 2619adc4f0dbSShawn McCarney 2620adc4f0dbSShawn McCarney // Get inventory items associated with sensors 2621002d39b4SEd Tanous getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths, 2622adc4f0dbSShawn McCarney std::move(getInventoryItemsCb)); 2623adc4f0dbSShawn McCarney 2624de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit"; 262508777fb0SLewanczyk, Dawid }; 2626de629b6eSShawn McCarney 262749c53ac9SJohnathan Mantey // Get mapping from connection names to the DBus object 262849c53ac9SJohnathan Mantey // paths that implement the ObjectManager interface 262981ce609eSEd Tanous getObjectManagerPaths(sensorsAsyncResp, 2630de629b6eSShawn McCarney std::move(getObjectManagerPathsCb)); 263155c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getConnectionCb exit"; 263208777fb0SLewanczyk, Dawid }; 2633de629b6eSShawn McCarney 2634de629b6eSShawn McCarney // Get set of connections that provide sensor values 263581ce609eSEd Tanous getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb)); 263695a3ecadSAnthony Wilson } 263795a3ecadSAnthony Wilson 263895a3ecadSAnthony Wilson /** 263995a3ecadSAnthony Wilson * @brief Entry point for retrieving sensors data related to requested 264095a3ecadSAnthony Wilson * chassis. 264195a3ecadSAnthony Wilson * @param SensorsAsyncResp Pointer to object holding response data 264295a3ecadSAnthony Wilson */ 2643b5a76932SEd Tanous inline void 264481ce609eSEd Tanous getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 264595a3ecadSAnthony Wilson { 264695a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisData enter"; 264795a3ecadSAnthony Wilson auto getChassisCb = 264881ce609eSEd Tanous [sensorsAsyncResp]( 2649fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames) { 265095a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb enter"; 265181ce609eSEd Tanous processSensorList(sensorsAsyncResp, sensorNames); 265255c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassisCb exit"; 265308777fb0SLewanczyk, Dawid }; 2654928fefb9SNan Zhou // SensorCollection doesn't contain the Redundancy property 2655928fefb9SNan Zhou if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors) 2656928fefb9SNan Zhou { 26578d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] = 26588d1b46d7Szhanghch05 nlohmann::json::array(); 2659928fefb9SNan Zhou } 266026f03899SShawn McCarney // Get set of sensors in chassis 26617f1cc26dSEd Tanous getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId, 26627f1cc26dSEd Tanous sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types, 26637f1cc26dSEd Tanous std::move(getChassisCb)); 266455c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassisData exit"; 2665271584abSEd Tanous } 266608777fb0SLewanczyk, Dawid 2667413961deSRichard Marian Thomaiyar /** 266849c53ac9SJohnathan Mantey * @brief Find the requested sensorName in the list of all sensors supplied by 266949c53ac9SJohnathan Mantey * the chassis node 267049c53ac9SJohnathan Mantey * 267149c53ac9SJohnathan Mantey * @param sensorName The sensor name supplied in the PATCH request 267249c53ac9SJohnathan Mantey * @param sensorsList The list of sensors managed by the chassis node 267349c53ac9SJohnathan Mantey * @param sensorsModified The list of sensors that were found as a result of 267449c53ac9SJohnathan Mantey * repeated calls to this function 267549c53ac9SJohnathan Mantey */ 2676fe04d49cSNan Zhou inline bool 2677fe04d49cSNan Zhou findSensorNameUsingSensorPath(std::string_view sensorName, 267802cad96eSEd Tanous const std::set<std::string>& sensorsList, 2679fe04d49cSNan Zhou std::set<std::string>& sensorsModified) 268049c53ac9SJohnathan Mantey { 2681fe04d49cSNan Zhou for (const auto& chassisSensor : sensorsList) 268249c53ac9SJohnathan Mantey { 268328aa8de5SGeorge Liu sdbusplus::message::object_path path(chassisSensor); 2684b00dcc27SEd Tanous std::string thisSensorName = path.filename(); 268528aa8de5SGeorge Liu if (thisSensorName.empty()) 268649c53ac9SJohnathan Mantey { 268749c53ac9SJohnathan Mantey continue; 268849c53ac9SJohnathan Mantey } 268949c53ac9SJohnathan Mantey if (thisSensorName == sensorName) 269049c53ac9SJohnathan Mantey { 269149c53ac9SJohnathan Mantey sensorsModified.emplace(chassisSensor); 269249c53ac9SJohnathan Mantey return true; 269349c53ac9SJohnathan Mantey } 269449c53ac9SJohnathan Mantey } 269549c53ac9SJohnathan Mantey return false; 269649c53ac9SJohnathan Mantey } 269749c53ac9SJohnathan Mantey 269849c53ac9SJohnathan Mantey /** 2699413961deSRichard Marian Thomaiyar * @brief Entry point for overriding sensor values of given sensor 2700413961deSRichard Marian Thomaiyar * 27018d1b46d7Szhanghch05 * @param sensorAsyncResp response object 27024bb3dc34SCarol Wang * @param allCollections Collections extract from sensors' request patch info 2703413961deSRichard Marian Thomaiyar * @param chassisSubNode Chassis Node for which the query has to happen 2704413961deSRichard Marian Thomaiyar */ 270523a21a1cSEd Tanous inline void setSensorsOverride( 2706b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp, 27074bb3dc34SCarol Wang std::unordered_map<std::string, std::vector<nlohmann::json>>& 2708397fd61fSjayaprakash Mutyala allCollections) 2709413961deSRichard Marian Thomaiyar { 271070d1d0aaSjayaprakash Mutyala BMCWEB_LOG_INFO << "setSensorsOverride for subNode" 27114bb3dc34SCarol Wang << sensorAsyncResp->chassisSubNode << "\n"; 2712413961deSRichard Marian Thomaiyar 2713543f4400SEd Tanous const char* propertyValueName = nullptr; 2714f65af9e8SRichard Marian Thomaiyar std::unordered_map<std::string, std::pair<double, std::string>> overrideMap; 2715413961deSRichard Marian Thomaiyar std::string memberId; 2716543f4400SEd Tanous double value = 0.0; 2717f65af9e8SRichard Marian Thomaiyar for (auto& collectionItems : allCollections) 2718f65af9e8SRichard Marian Thomaiyar { 2719f65af9e8SRichard Marian Thomaiyar if (collectionItems.first == "Temperatures") 2720f65af9e8SRichard Marian Thomaiyar { 2721f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingCelsius"; 2722f65af9e8SRichard Marian Thomaiyar } 2723f65af9e8SRichard Marian Thomaiyar else if (collectionItems.first == "Fans") 2724f65af9e8SRichard Marian Thomaiyar { 2725f65af9e8SRichard Marian Thomaiyar propertyValueName = "Reading"; 2726f65af9e8SRichard Marian Thomaiyar } 2727f65af9e8SRichard Marian Thomaiyar else 2728f65af9e8SRichard Marian Thomaiyar { 2729f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingVolts"; 2730f65af9e8SRichard Marian Thomaiyar } 2731f65af9e8SRichard Marian Thomaiyar for (auto& item : collectionItems.second) 2732f65af9e8SRichard Marian Thomaiyar { 27338d1b46d7Szhanghch05 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res, 27348d1b46d7Szhanghch05 "MemberId", memberId, propertyValueName, 27358d1b46d7Szhanghch05 value)) 2736413961deSRichard Marian Thomaiyar { 2737413961deSRichard Marian Thomaiyar return; 2738413961deSRichard Marian Thomaiyar } 2739f65af9e8SRichard Marian Thomaiyar overrideMap.emplace(memberId, 2740f65af9e8SRichard Marian Thomaiyar std::make_pair(value, collectionItems.first)); 2741f65af9e8SRichard Marian Thomaiyar } 2742f65af9e8SRichard Marian Thomaiyar } 27434bb3dc34SCarol Wang 2744002d39b4SEd Tanous auto getChassisSensorListCb = 2745002d39b4SEd Tanous [sensorAsyncResp, overrideMap]( 2746fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorsList) { 274749c53ac9SJohnathan Mantey // Match sensor names in the PATCH request to those managed by the 274849c53ac9SJohnathan Mantey // chassis node 2749fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> sensorNames = 2750fe04d49cSNan Zhou std::make_shared<std::set<std::string>>(); 2751f65af9e8SRichard Marian Thomaiyar for (const auto& item : overrideMap) 2752413961deSRichard Marian Thomaiyar { 2753f65af9e8SRichard Marian Thomaiyar const auto& sensor = item.first; 275449c53ac9SJohnathan Mantey if (!findSensorNameUsingSensorPath(sensor, *sensorsList, 275549c53ac9SJohnathan Mantey *sensorNames)) 2756f65af9e8SRichard Marian Thomaiyar { 2757f65af9e8SRichard Marian Thomaiyar BMCWEB_LOG_INFO << "Unable to find memberId " << item.first; 27588d1b46d7Szhanghch05 messages::resourceNotFound(sensorAsyncResp->asyncResp->res, 2759f65af9e8SRichard Marian Thomaiyar item.second.second, item.first); 2760413961deSRichard Marian Thomaiyar return; 2761413961deSRichard Marian Thomaiyar } 2762f65af9e8SRichard Marian Thomaiyar } 2763413961deSRichard Marian Thomaiyar // Get the connection to which the memberId belongs 2764002d39b4SEd Tanous auto getObjectsWithConnectionCb = 2765fe04d49cSNan Zhou [sensorAsyncResp, 2766fe04d49cSNan Zhou overrideMap](const std::set<std::string>& /*connections*/, 2767002d39b4SEd Tanous const std::set<std::pair<std::string, std::string>>& 2768413961deSRichard Marian Thomaiyar objectsWithConnection) { 2769f65af9e8SRichard Marian Thomaiyar if (objectsWithConnection.size() != overrideMap.size()) 2770413961deSRichard Marian Thomaiyar { 2771413961deSRichard Marian Thomaiyar BMCWEB_LOG_INFO 2772f65af9e8SRichard Marian Thomaiyar << "Unable to find all objects with proper connection " 2773f65af9e8SRichard Marian Thomaiyar << objectsWithConnection.size() << " requested " 2774f65af9e8SRichard Marian Thomaiyar << overrideMap.size() << "\n"; 27754f277b54SJayaprakash Mutyala messages::resourceNotFound(sensorAsyncResp->asyncResp->res, 2776a0ec28b6SAdrian Ambrożewicz sensorAsyncResp->chassisSubNode == 2777a0ec28b6SAdrian Ambrożewicz sensors::node::thermal 2778413961deSRichard Marian Thomaiyar ? "Temperatures" 2779413961deSRichard Marian Thomaiyar : "Voltages", 2780f65af9e8SRichard Marian Thomaiyar "Count"); 2781f65af9e8SRichard Marian Thomaiyar return; 2782f65af9e8SRichard Marian Thomaiyar } 2783f65af9e8SRichard Marian Thomaiyar for (const auto& item : objectsWithConnection) 2784f65af9e8SRichard Marian Thomaiyar { 278528aa8de5SGeorge Liu sdbusplus::message::object_path path(item.first); 278628aa8de5SGeorge Liu std::string sensorName = path.filename(); 278728aa8de5SGeorge Liu if (sensorName.empty()) 2788f65af9e8SRichard Marian Thomaiyar { 27894f277b54SJayaprakash Mutyala messages::internalError(sensorAsyncResp->asyncResp->res); 2790f65af9e8SRichard Marian Thomaiyar return; 2791f65af9e8SRichard Marian Thomaiyar } 2792f65af9e8SRichard Marian Thomaiyar 2793f65af9e8SRichard Marian Thomaiyar const auto& iterator = overrideMap.find(sensorName); 2794f65af9e8SRichard Marian Thomaiyar if (iterator == overrideMap.end()) 2795f65af9e8SRichard Marian Thomaiyar { 2796f65af9e8SRichard Marian Thomaiyar BMCWEB_LOG_INFO << "Unable to find sensor object" 2797f65af9e8SRichard Marian Thomaiyar << item.first << "\n"; 27984f277b54SJayaprakash Mutyala messages::internalError(sensorAsyncResp->asyncResp->res); 2799413961deSRichard Marian Thomaiyar return; 2800413961deSRichard Marian Thomaiyar } 2801413961deSRichard Marian Thomaiyar crow::connections::systemBus->async_method_call( 2802f65af9e8SRichard Marian Thomaiyar [sensorAsyncResp](const boost::system::error_code ec) { 2803413961deSRichard Marian Thomaiyar if (ec) 2804413961deSRichard Marian Thomaiyar { 28054f277b54SJayaprakash Mutyala if (ec.value() == 28064f277b54SJayaprakash Mutyala boost::system::errc::permission_denied) 28074f277b54SJayaprakash Mutyala { 28084f277b54SJayaprakash Mutyala BMCWEB_LOG_WARNING 28094f277b54SJayaprakash Mutyala << "Manufacturing mode is not Enabled...can't " 28104f277b54SJayaprakash Mutyala "Override the sensor value. "; 28114f277b54SJayaprakash Mutyala 28124f277b54SJayaprakash Mutyala messages::insufficientPrivilege( 28138d1b46d7Szhanghch05 sensorAsyncResp->asyncResp->res); 2814413961deSRichard Marian Thomaiyar return; 2815413961deSRichard Marian Thomaiyar } 28164f277b54SJayaprakash Mutyala BMCWEB_LOG_DEBUG 28174f277b54SJayaprakash Mutyala << "setOverrideValueStatus DBUS error: " << ec; 28184f277b54SJayaprakash Mutyala messages::internalError( 28194f277b54SJayaprakash Mutyala sensorAsyncResp->asyncResp->res); 28204f277b54SJayaprakash Mutyala } 2821413961deSRichard Marian Thomaiyar }, 28224f277b54SJayaprakash Mutyala item.second, item.first, "org.freedesktop.DBus.Properties", 28234f277b54SJayaprakash Mutyala "Set", "xyz.openbmc_project.Sensor.Value", "Value", 2824168e20c1SEd Tanous dbus::utility::DbusVariantType(iterator->second.first)); 2825f65af9e8SRichard Marian Thomaiyar } 2826413961deSRichard Marian Thomaiyar }; 2827413961deSRichard Marian Thomaiyar // Get object with connection for the given sensor name 2828413961deSRichard Marian Thomaiyar getObjectsWithConnection(sensorAsyncResp, sensorNames, 2829413961deSRichard Marian Thomaiyar std::move(getObjectsWithConnectionCb)); 2830413961deSRichard Marian Thomaiyar }; 2831413961deSRichard Marian Thomaiyar // get full sensor list for the given chassisId and cross verify the sensor. 28327f1cc26dSEd Tanous getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId, 28337f1cc26dSEd Tanous sensorAsyncResp->chassisSubNode, sensorAsyncResp->types, 28347f1cc26dSEd Tanous std::move(getChassisSensorListCb)); 2835413961deSRichard Marian Thomaiyar } 2836413961deSRichard Marian Thomaiyar 2837a0ec28b6SAdrian Ambrożewicz /** 2838a0ec28b6SAdrian Ambrożewicz * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus 2839a0ec28b6SAdrian Ambrożewicz * path of the sensor. 2840a0ec28b6SAdrian Ambrożewicz * 2841a0ec28b6SAdrian Ambrożewicz * Function builds valid Redfish response for sensor query of given chassis and 2842a0ec28b6SAdrian Ambrożewicz * node. It then builds metadata about Redfish<->D-Bus correlations and provides 2843a0ec28b6SAdrian Ambrożewicz * it to caller in a callback. 2844a0ec28b6SAdrian Ambrożewicz * 2845a0ec28b6SAdrian Ambrożewicz * @param chassis Chassis for which retrieval should be performed 2846a0ec28b6SAdrian Ambrożewicz * @param node Node (group) of sensors. See sensors::node for supported values 2847a0ec28b6SAdrian Ambrożewicz * @param mapComplete Callback to be called with retrieval result 2848a0ec28b6SAdrian Ambrożewicz */ 2849021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis, 2850021d32cfSKrzysztof Grobelny const std::string& node, 2851a0ec28b6SAdrian Ambrożewicz SensorsAsyncResp::DataCompleteCb&& mapComplete) 2852a0ec28b6SAdrian Ambrożewicz { 285302da7c5aSEd Tanous decltype(sensors::paths)::const_iterator pathIt = 285402da7c5aSEd Tanous std::find_if(sensors::paths.cbegin(), sensors::paths.cend(), 285502da7c5aSEd Tanous [&node](auto&& val) { return val.first == node; }); 285602da7c5aSEd Tanous if (pathIt == sensors::paths.cend()) 2857a0ec28b6SAdrian Ambrożewicz { 2858a0ec28b6SAdrian Ambrożewicz BMCWEB_LOG_ERROR << "Wrong node provided : " << node; 2859a0ec28b6SAdrian Ambrożewicz mapComplete(boost::beast::http::status::bad_request, {}); 2860a0ec28b6SAdrian Ambrożewicz return; 2861a0ec28b6SAdrian Ambrożewicz } 2862d51e072fSKrzysztof Grobelny 286372374eb7SNan Zhou auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); 2864fe04d49cSNan Zhou auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}]( 2865a0ec28b6SAdrian Ambrożewicz const boost::beast::http::status status, 2866fe04d49cSNan Zhou const std::map<std::string, std::string>& uriToDbus) { 2867fe04d49cSNan Zhou mapCompleteCb(status, uriToDbus); 2868fe04d49cSNan Zhou }; 2869a0ec28b6SAdrian Ambrożewicz 2870a0ec28b6SAdrian Ambrożewicz auto resp = std::make_shared<SensorsAsyncResp>( 2871d51e072fSKrzysztof Grobelny asyncResp, chassis, pathIt->second, node, std::move(callback)); 2872a0ec28b6SAdrian Ambrożewicz getChassisData(resp); 2873a0ec28b6SAdrian Ambrożewicz } 2874a0ec28b6SAdrian Ambrożewicz 2875bacb2162SNan Zhou namespace sensors 2876bacb2162SNan Zhou { 2877928fefb9SNan Zhou 2878bacb2162SNan Zhou inline void getChassisCallback( 2879c1d019a6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2880c1d019a6SEd Tanous std::string_view chassisId, std::string_view chassisSubNode, 2881fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames) 2882bacb2162SNan Zhou { 2883bacb2162SNan Zhou BMCWEB_LOG_DEBUG << "getChassisCallback enter "; 2884bacb2162SNan Zhou 2885c1d019a6SEd Tanous nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"]; 2886c1d019a6SEd Tanous for (const std::string& sensor : *sensorNames) 2887bacb2162SNan Zhou { 2888bacb2162SNan Zhou BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor; 2889bacb2162SNan Zhou 2890bacb2162SNan Zhou sdbusplus::message::object_path path(sensor); 2891bacb2162SNan Zhou std::string sensorName = path.filename(); 2892bacb2162SNan Zhou if (sensorName.empty()) 2893bacb2162SNan Zhou { 2894bacb2162SNan Zhou BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor; 2895c1d019a6SEd Tanous messages::internalError(asyncResp->res); 2896bacb2162SNan Zhou return; 2897bacb2162SNan Zhou } 2898c1d019a6SEd Tanous std::string type = path.parent_path().filename(); 2899c1d019a6SEd Tanous // fan_tach has an underscore in it, so remove it to "normalize" the 2900c1d019a6SEd Tanous // type in the URI 2901c1d019a6SEd Tanous type.erase(std::remove(type.begin(), type.end(), '_'), type.end()); 2902c1d019a6SEd Tanous 29031476687dSEd Tanous nlohmann::json::object_t member; 2904c1d019a6SEd Tanous std::string id = type; 2905c1d019a6SEd Tanous id += "_"; 2906c1d019a6SEd Tanous id += sensorName; 2907c1d019a6SEd Tanous member["@odata.id"] = crow::utility::urlFromPieces( 2908c1d019a6SEd Tanous "redfish", "v1", "Chassis", chassisId, chassisSubNode, id); 2909c1d019a6SEd Tanous 29101476687dSEd Tanous entriesArray.push_back(std::move(member)); 2911bacb2162SNan Zhou } 2912bacb2162SNan Zhou 2913c1d019a6SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size(); 2914bacb2162SNan Zhou BMCWEB_LOG_DEBUG << "getChassisCallback exit"; 2915bacb2162SNan Zhou } 2916e6bd846dSNan Zhou 2917de167a6fSNan Zhou inline void 2918de167a6fSNan Zhou handleSensorCollectionGet(App& app, const crow::Request& req, 2919de167a6fSNan Zhou const std::shared_ptr<bmcweb::AsyncResp>& aResp, 2920de167a6fSNan Zhou const std::string& chassisId) 2921de167a6fSNan Zhou { 2922de167a6fSNan Zhou query_param::QueryCapabilities capabilities = { 2923de167a6fSNan Zhou .canDelegateExpandLevel = 1, 2924de167a6fSNan Zhou }; 2925de167a6fSNan Zhou query_param::Query delegatedQuery; 29263ba00073SCarson Labrado if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp, 2927de167a6fSNan Zhou delegatedQuery, capabilities)) 2928de167a6fSNan Zhou { 2929de167a6fSNan Zhou return; 2930de167a6fSNan Zhou } 2931de167a6fSNan Zhou 2932de167a6fSNan Zhou if (delegatedQuery.expandType != query_param::ExpandType::None) 2933de167a6fSNan Zhou { 2934de167a6fSNan Zhou // we perform efficient expand. 2935de167a6fSNan Zhou auto asyncResp = std::make_shared<SensorsAsyncResp>( 2936de167a6fSNan Zhou aResp, chassisId, sensors::dbus::sensorPaths, 2937de167a6fSNan Zhou sensors::node::sensors, 2938de167a6fSNan Zhou /*efficientExpand=*/true); 2939de167a6fSNan Zhou getChassisData(asyncResp); 2940de167a6fSNan Zhou 2941de167a6fSNan Zhou BMCWEB_LOG_DEBUG 2942de167a6fSNan Zhou << "SensorCollection doGet exit via efficient expand handler"; 2943de167a6fSNan Zhou return; 29440bad320cSEd Tanous } 2945de167a6fSNan Zhou 2946de167a6fSNan Zhou // We get all sensors as hyperlinkes in the chassis (this 2947de167a6fSNan Zhou // implies we reply on the default query parameters handler) 2948c1d019a6SEd Tanous getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths, 2949c1d019a6SEd Tanous std::bind_front(sensors::getChassisCallback, aResp, chassisId, 2950c1d019a6SEd Tanous sensors::node::sensors)); 2951c1d019a6SEd Tanous } 29527f1cc26dSEd Tanous 2953c1d019a6SEd Tanous inline void 2954c1d019a6SEd Tanous getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2955c1d019a6SEd Tanous const std::string& sensorPath, 2956c1d019a6SEd Tanous const ::dbus::utility::MapperGetObject& mapperResponse) 2957c1d019a6SEd Tanous { 2958c1d019a6SEd Tanous if (mapperResponse.size() != 1) 2959c1d019a6SEd Tanous { 2960c1d019a6SEd Tanous messages::internalError(asyncResp->res); 2961c1d019a6SEd Tanous return; 2962c1d019a6SEd Tanous } 2963c1d019a6SEd Tanous const auto& valueIface = *mapperResponse.begin(); 2964c1d019a6SEd Tanous const std::string& connectionName = valueIface.first; 2965c1d019a6SEd Tanous BMCWEB_LOG_DEBUG << "Looking up " << connectionName; 2966c1d019a6SEd Tanous BMCWEB_LOG_DEBUG << "Path " << sensorPath; 2967c1343bf6SKrzysztof Grobelny 2968c1343bf6SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 2969c1343bf6SKrzysztof Grobelny *crow::connections::systemBus, connectionName, sensorPath, "", 2970c1d019a6SEd Tanous [asyncResp, 2971c1d019a6SEd Tanous sensorPath](const boost::system::error_code ec, 2972c1d019a6SEd Tanous const ::dbus::utility::DBusPropertiesMap& valuesDict) { 2973c1d019a6SEd Tanous if (ec) 2974c1d019a6SEd Tanous { 2975c1d019a6SEd Tanous messages::internalError(asyncResp->res); 2976c1d019a6SEd Tanous return; 2977c1d019a6SEd Tanous } 2978c1d019a6SEd Tanous sdbusplus::message::object_path path(sensorPath); 2979c1d019a6SEd Tanous std::string name = path.filename(); 2980c1d019a6SEd Tanous path = path.parent_path(); 2981c1d019a6SEd Tanous std::string type = path.filename(); 2982c1d019a6SEd Tanous objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict, 2983c1d019a6SEd Tanous asyncResp->res.jsonValue, nullptr); 2984c1343bf6SKrzysztof Grobelny }); 2985de167a6fSNan Zhou } 2986de167a6fSNan Zhou 2987e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req, 2988c1d019a6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2989677bb756SEd Tanous const std::string& chassisId, 2990c1d019a6SEd Tanous const std::string& sensorId) 2991e6bd846dSNan Zhou { 2992c1d019a6SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2993e6bd846dSNan Zhou { 2994e6bd846dSNan Zhou return; 2995e6bd846dSNan Zhou } 2996c1d019a6SEd Tanous size_t index = sensorId.find('_'); 2997c1d019a6SEd Tanous if (index == std::string::npos) 2998c1d019a6SEd Tanous { 2999c1d019a6SEd Tanous messages::resourceNotFound(asyncResp->res, sensorId, "Sensor"); 3000c1d019a6SEd Tanous return; 3001c1d019a6SEd Tanous } 3002677bb756SEd Tanous asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces( 3003677bb756SEd Tanous "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId); 3004c1d019a6SEd Tanous std::string sensorType = sensorId.substr(0, index); 3005c1d019a6SEd Tanous std::string sensorName = sensorId.substr(index + 1); 3006c1d019a6SEd Tanous // fan_pwm and fan_tach need special handling 3007c1d019a6SEd Tanous if (sensorType == "fantach" || sensorType == "fanpwm") 3008c1d019a6SEd Tanous { 3009c1d019a6SEd Tanous sensorType.insert(3, 1, '_'); 3010c1d019a6SEd Tanous } 3011c1d019a6SEd Tanous 3012e6bd846dSNan Zhou BMCWEB_LOG_DEBUG << "Sensor doGet enter"; 3013e6bd846dSNan Zhou 3014e6bd846dSNan Zhou const std::array<const char*, 1> interfaces = { 3015e6bd846dSNan Zhou "xyz.openbmc_project.Sensor.Value"}; 3016c1d019a6SEd Tanous std::string sensorPath = 3017c1d019a6SEd Tanous "/xyz/openbmc_project/sensors/" + sensorType + '/' + sensorName; 3018e6bd846dSNan Zhou // Get a list of all of the sensors that implement Sensor.Value 3019e6bd846dSNan Zhou // and get the path and service name associated with the sensor 3020e6bd846dSNan Zhou crow::connections::systemBus->async_method_call( 3021c1d019a6SEd Tanous [asyncResp, sensorPath, 3022e6bd846dSNan Zhou sensorName](const boost::system::error_code ec, 3023c1d019a6SEd Tanous const ::dbus::utility::MapperGetObject& subtree) { 3024e6bd846dSNan Zhou BMCWEB_LOG_DEBUG << "respHandler1 enter"; 3025e6bd846dSNan Zhou if (ec) 3026e6bd846dSNan Zhou { 3027c1d019a6SEd Tanous messages::internalError(asyncResp->res); 3028e6bd846dSNan Zhou BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: " 3029e6bd846dSNan Zhou << "Dbus error " << ec; 3030e6bd846dSNan Zhou return; 3031e6bd846dSNan Zhou } 3032c1d019a6SEd Tanous getSensorFromDbus(asyncResp, sensorPath, subtree); 3033e6bd846dSNan Zhou BMCWEB_LOG_DEBUG << "respHandler1 exit"; 3034e6bd846dSNan Zhou }, 3035e6bd846dSNan Zhou "xyz.openbmc_project.ObjectMapper", 3036e6bd846dSNan Zhou "/xyz/openbmc_project/object_mapper", 3037c1d019a6SEd Tanous "xyz.openbmc_project.ObjectMapper", "GetObject", sensorPath, 3038c1d019a6SEd Tanous interfaces); 3039e6bd846dSNan Zhou } 3040e6bd846dSNan Zhou 3041bacb2162SNan Zhou } // namespace sensors 3042bacb2162SNan Zhou 30437e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app) 304495a3ecadSAnthony Wilson { 30457e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/") 3046ed398213SEd Tanous .privileges(redfish::privileges::getSensorCollection) 3047002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 3048de167a6fSNan Zhou std::bind_front(sensors::handleSensorCollectionGet, std::ref(app))); 304995a3ecadSAnthony Wilson } 305095a3ecadSAnthony Wilson 30517e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app) 305295a3ecadSAnthony Wilson { 30537e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/") 3054ed398213SEd Tanous .privileges(redfish::privileges::getSensor) 3055002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 3056e6bd846dSNan Zhou std::bind_front(sensors::handleSensorGet, std::ref(app))); 305795a3ecadSAnthony Wilson } 305895a3ecadSAnthony Wilson 305908777fb0SLewanczyk, Dawid } // namespace redfish 3060