108777fb0SLewanczyk, Dawid /* 208777fb0SLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation 308777fb0SLewanczyk, Dawid // 408777fb0SLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License"); 508777fb0SLewanczyk, Dawid // you may not use this file except in compliance with the License. 608777fb0SLewanczyk, Dawid // You may obtain a copy of the License at 708777fb0SLewanczyk, Dawid // 808777fb0SLewanczyk, Dawid // http://www.apache.org/licenses/LICENSE-2.0 908777fb0SLewanczyk, Dawid // 1008777fb0SLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software 1108777fb0SLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS, 1208777fb0SLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1308777fb0SLewanczyk, Dawid // See the License for the specific language governing permissions and 1408777fb0SLewanczyk, Dawid // limitations under the License. 1508777fb0SLewanczyk, Dawid */ 1608777fb0SLewanczyk, Dawid #pragma once 1708777fb0SLewanczyk, Dawid 187e860f15SJohn Edward Broadbent #include <app.hpp> 1911ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp> 201d7c0054SEd Tanous #include <boost/algorithm/string/find.hpp> 211d7c0054SEd Tanous #include <boost/algorithm/string/predicate.hpp> 2208777fb0SLewanczyk, Dawid #include <boost/algorithm/string/split.hpp> 2308777fb0SLewanczyk, Dawid #include <boost/range/algorithm/replace_copy_if.hpp> 241abe55efSEd Tanous #include <dbus_singleton.hpp> 25168e20c1SEd Tanous #include <dbus_utility.hpp> 2645ca1b86SEd Tanous #include <query.hpp> 27ed398213SEd Tanous #include <registries/privilege_registry.hpp> 281e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 2986d89ed7SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp> 3086d89ed7SKrzysztof Grobelny #include <utils/dbus_utils.hpp> 31413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp> 32928fefb9SNan Zhou #include <utils/query_param.hpp> 331214b7e7SGunnar Mills 341214b7e7SGunnar Mills #include <cmath> 35fe04d49cSNan Zhou #include <iterator> 36fe04d49cSNan Zhou #include <map> 37fe04d49cSNan Zhou #include <set> 38b5a76932SEd Tanous #include <utility> 39abf2add6SEd Tanous #include <variant> 4008777fb0SLewanczyk, Dawid 411abe55efSEd Tanous namespace redfish 421abe55efSEd Tanous { 4308777fb0SLewanczyk, Dawid 44a0ec28b6SAdrian Ambrożewicz namespace sensors 45a0ec28b6SAdrian Ambrożewicz { 46a0ec28b6SAdrian Ambrożewicz namespace node 47a0ec28b6SAdrian Ambrożewicz { 48a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power"; 49a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors"; 50a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal"; 51a0ec28b6SAdrian Ambrożewicz } // namespace node 52a0ec28b6SAdrian Ambrożewicz 5302da7c5aSEd Tanous // clang-format off 54a0ec28b6SAdrian Ambrożewicz namespace dbus 55a0ec28b6SAdrian Ambrożewicz { 564ee8e211SEd Tanous static auto powerPaths = std::to_array<std::string_view>({ 5702da7c5aSEd Tanous "/xyz/openbmc_project/sensors/voltage", 5802da7c5aSEd Tanous "/xyz/openbmc_project/sensors/power" 5902da7c5aSEd Tanous }); 60c2bf7f99SWludzik, Jozef 614ee8e211SEd Tanous static auto sensorPaths = std::to_array<std::string_view>({ 6202da7c5aSEd Tanous "/xyz/openbmc_project/sensors/power", 63a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/current", 647088690cSBasheer Ahmed Muddebihal "/xyz/openbmc_project/sensors/airflow", 655deabed9SGunnar Mills "/xyz/openbmc_project/sensors/humidity", 66e8204933SGeorge Liu #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM 67e8204933SGeorge Liu "/xyz/openbmc_project/sensors/voltage", 68e8204933SGeorge Liu "/xyz/openbmc_project/sensors/fan_tach", 69e8204933SGeorge Liu "/xyz/openbmc_project/sensors/temperature", 70e8204933SGeorge Liu "/xyz/openbmc_project/sensors/fan_pwm", 71e8204933SGeorge Liu "/xyz/openbmc_project/sensors/altitude", 72e8204933SGeorge Liu "/xyz/openbmc_project/sensors/energy", 73e8204933SGeorge Liu #endif 7402da7c5aSEd Tanous "/xyz/openbmc_project/sensors/utilization" 7502da7c5aSEd Tanous }); 7602da7c5aSEd Tanous 774ee8e211SEd Tanous static auto thermalPaths = std::to_array<std::string_view>({ 7802da7c5aSEd Tanous "/xyz/openbmc_project/sensors/fan_tach", 79a0ec28b6SAdrian Ambrożewicz "/xyz/openbmc_project/sensors/temperature", 8002da7c5aSEd Tanous "/xyz/openbmc_project/sensors/fan_pwm" 8102da7c5aSEd Tanous }); 8202da7c5aSEd Tanous 83c2bf7f99SWludzik, Jozef } // namespace dbus 8402da7c5aSEd Tanous // clang-format on 8502da7c5aSEd Tanous 8602da7c5aSEd Tanous using sensorPair = std::pair<std::string_view, std::span<std::string_view>>; 8702da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = { 8802da7c5aSEd Tanous {{node::power, std::span<std::string_view>(dbus::powerPaths)}, 8902da7c5aSEd Tanous {node::sensors, std::span<std::string_view>(dbus::sensorPaths)}, 9002da7c5aSEd Tanous {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}}; 91c2bf7f99SWludzik, Jozef 921d7c0054SEd Tanous inline std::string_view toReadingType(std::string_view sensorType) 93c2bf7f99SWludzik, Jozef { 94c2bf7f99SWludzik, Jozef if (sensorType == "voltage") 95c2bf7f99SWludzik, Jozef { 96c2bf7f99SWludzik, Jozef return "Voltage"; 97c2bf7f99SWludzik, Jozef } 98c2bf7f99SWludzik, Jozef if (sensorType == "power") 99c2bf7f99SWludzik, Jozef { 100c2bf7f99SWludzik, Jozef return "Power"; 101c2bf7f99SWludzik, Jozef } 102c2bf7f99SWludzik, Jozef if (sensorType == "current") 103c2bf7f99SWludzik, Jozef { 104c2bf7f99SWludzik, Jozef return "Current"; 105c2bf7f99SWludzik, Jozef } 106c2bf7f99SWludzik, Jozef if (sensorType == "fan_tach") 107c2bf7f99SWludzik, Jozef { 108c2bf7f99SWludzik, Jozef return "Rotational"; 109c2bf7f99SWludzik, Jozef } 110c2bf7f99SWludzik, Jozef if (sensorType == "temperature") 111c2bf7f99SWludzik, Jozef { 112c2bf7f99SWludzik, Jozef return "Temperature"; 113c2bf7f99SWludzik, Jozef } 114c2bf7f99SWludzik, Jozef if (sensorType == "fan_pwm" || sensorType == "utilization") 115c2bf7f99SWludzik, Jozef { 116c2bf7f99SWludzik, Jozef return "Percent"; 117c2bf7f99SWludzik, Jozef } 1185deabed9SGunnar Mills if (sensorType == "humidity") 1195deabed9SGunnar Mills { 1205deabed9SGunnar Mills return "Humidity"; 1215deabed9SGunnar Mills } 122c2bf7f99SWludzik, Jozef if (sensorType == "altitude") 123c2bf7f99SWludzik, Jozef { 124c2bf7f99SWludzik, Jozef return "Altitude"; 125c2bf7f99SWludzik, Jozef } 126c2bf7f99SWludzik, Jozef if (sensorType == "airflow") 127c2bf7f99SWludzik, Jozef { 128c2bf7f99SWludzik, Jozef return "AirFlow"; 129c2bf7f99SWludzik, Jozef } 130c2bf7f99SWludzik, Jozef if (sensorType == "energy") 131c2bf7f99SWludzik, Jozef { 132c2bf7f99SWludzik, Jozef return "EnergyJoules"; 133c2bf7f99SWludzik, Jozef } 134c2bf7f99SWludzik, Jozef return ""; 135c2bf7f99SWludzik, Jozef } 136c2bf7f99SWludzik, Jozef 1371d7c0054SEd Tanous inline std::string_view toReadingUnits(std::string_view sensorType) 138c2bf7f99SWludzik, Jozef { 139c2bf7f99SWludzik, Jozef if (sensorType == "voltage") 140c2bf7f99SWludzik, Jozef { 141c2bf7f99SWludzik, Jozef return "V"; 142c2bf7f99SWludzik, Jozef } 143c2bf7f99SWludzik, Jozef if (sensorType == "power") 144c2bf7f99SWludzik, Jozef { 145c2bf7f99SWludzik, Jozef return "W"; 146c2bf7f99SWludzik, Jozef } 147c2bf7f99SWludzik, Jozef if (sensorType == "current") 148c2bf7f99SWludzik, Jozef { 149c2bf7f99SWludzik, Jozef return "A"; 150c2bf7f99SWludzik, Jozef } 151c2bf7f99SWludzik, Jozef if (sensorType == "fan_tach") 152c2bf7f99SWludzik, Jozef { 153c2bf7f99SWludzik, Jozef return "RPM"; 154c2bf7f99SWludzik, Jozef } 155c2bf7f99SWludzik, Jozef if (sensorType == "temperature") 156c2bf7f99SWludzik, Jozef { 157c2bf7f99SWludzik, Jozef return "Cel"; 158c2bf7f99SWludzik, Jozef } 1595deabed9SGunnar Mills if (sensorType == "fan_pwm" || sensorType == "utilization" || 1605deabed9SGunnar Mills sensorType == "humidity") 161c2bf7f99SWludzik, Jozef { 162c2bf7f99SWludzik, Jozef return "%"; 163c2bf7f99SWludzik, Jozef } 164c2bf7f99SWludzik, Jozef if (sensorType == "altitude") 165c2bf7f99SWludzik, Jozef { 166c2bf7f99SWludzik, Jozef return "m"; 167c2bf7f99SWludzik, Jozef } 168c2bf7f99SWludzik, Jozef if (sensorType == "airflow") 169c2bf7f99SWludzik, Jozef { 170c2bf7f99SWludzik, Jozef return "cft_i/min"; 171c2bf7f99SWludzik, Jozef } 172c2bf7f99SWludzik, Jozef if (sensorType == "energy") 173c2bf7f99SWludzik, Jozef { 174c2bf7f99SWludzik, Jozef return "J"; 175c2bf7f99SWludzik, Jozef } 176c2bf7f99SWludzik, Jozef return ""; 177a0ec28b6SAdrian Ambrożewicz } 178a0ec28b6SAdrian Ambrożewicz } // namespace sensors 179a0ec28b6SAdrian Ambrożewicz 18008777fb0SLewanczyk, Dawid /** 181588c3f0dSKowalski, Kamil * SensorsAsyncResp 18208777fb0SLewanczyk, Dawid * Gathers data needed for response processing after async calls are done 18308777fb0SLewanczyk, Dawid */ 1841abe55efSEd Tanous class SensorsAsyncResp 1851abe55efSEd Tanous { 18608777fb0SLewanczyk, Dawid public: 187a0ec28b6SAdrian Ambrożewicz using DataCompleteCb = std::function<void( 188a0ec28b6SAdrian Ambrożewicz const boost::beast::http::status status, 189fe04d49cSNan Zhou const std::map<std::string, std::string>& uriToDbus)>; 190a0ec28b6SAdrian Ambrożewicz 191a0ec28b6SAdrian Ambrożewicz struct SensorData 192a0ec28b6SAdrian Ambrożewicz { 193a0ec28b6SAdrian Ambrożewicz const std::string name; 194a0ec28b6SAdrian Ambrożewicz std::string uri; 195a0ec28b6SAdrian Ambrożewicz const std::string dbusPath; 196a0ec28b6SAdrian Ambrożewicz }; 197a0ec28b6SAdrian Ambrożewicz 1988a592810SEd Tanous SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 1998d1b46d7Szhanghch05 const std::string& chassisIdIn, 20002da7c5aSEd Tanous std::span<std::string_view> typesIn, 20102da7c5aSEd Tanous std::string_view subNode) : 2028a592810SEd Tanous asyncResp(asyncRespIn), 203928fefb9SNan Zhou chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode), 204928fefb9SNan Zhou efficientExpand(false) 2051214b7e7SGunnar Mills {} 20608777fb0SLewanczyk, Dawid 207a0ec28b6SAdrian Ambrożewicz // Store extra data about sensor mapping and return it in callback 2088a592810SEd Tanous SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 2098d1b46d7Szhanghch05 const std::string& chassisIdIn, 21002da7c5aSEd Tanous std::span<std::string_view> typesIn, 21102da7c5aSEd Tanous std::string_view subNode, 212a0ec28b6SAdrian Ambrożewicz DataCompleteCb&& creationComplete) : 2138a592810SEd Tanous asyncResp(asyncRespIn), 214928fefb9SNan Zhou chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode), 215928fefb9SNan Zhou efficientExpand(false), metadata{std::vector<SensorData>()}, 216a0ec28b6SAdrian Ambrożewicz dataComplete{std::move(creationComplete)} 217a0ec28b6SAdrian Ambrożewicz {} 218a0ec28b6SAdrian Ambrożewicz 219928fefb9SNan Zhou // sensor collections expand 2208a592810SEd Tanous SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 221928fefb9SNan Zhou const std::string& chassisIdIn, 22202da7c5aSEd Tanous const std::span<std::string_view> typesIn, 2238a592810SEd Tanous const std::string_view& subNode, bool efficientExpandIn) : 2248a592810SEd Tanous asyncResp(asyncRespIn), 225928fefb9SNan Zhou chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode), 2268a592810SEd Tanous efficientExpand(efficientExpandIn) 227928fefb9SNan Zhou {} 228928fefb9SNan Zhou 2291abe55efSEd Tanous ~SensorsAsyncResp() 2301abe55efSEd Tanous { 2318d1b46d7Szhanghch05 if (asyncResp->res.result() == 2328d1b46d7Szhanghch05 boost::beast::http::status::internal_server_error) 2331abe55efSEd Tanous { 2341abe55efSEd Tanous // Reset the json object to clear out any data that made it in 2351abe55efSEd Tanous // before the error happened todo(ed) handle error condition with 2361abe55efSEd Tanous // proper code 2378d1b46d7Szhanghch05 asyncResp->res.jsonValue = nlohmann::json::object(); 23808777fb0SLewanczyk, Dawid } 239a0ec28b6SAdrian Ambrożewicz 240a0ec28b6SAdrian Ambrożewicz if (dataComplete && metadata) 241a0ec28b6SAdrian Ambrożewicz { 242fe04d49cSNan Zhou std::map<std::string, std::string> map; 2438d1b46d7Szhanghch05 if (asyncResp->res.result() == boost::beast::http::status::ok) 244a0ec28b6SAdrian Ambrożewicz { 245a0ec28b6SAdrian Ambrożewicz for (auto& sensor : *metadata) 246a0ec28b6SAdrian Ambrożewicz { 247c1d019a6SEd Tanous map.emplace(sensor.uri, sensor.dbusPath); 248a0ec28b6SAdrian Ambrożewicz } 249a0ec28b6SAdrian Ambrożewicz } 2508d1b46d7Szhanghch05 dataComplete(asyncResp->res.result(), map); 251a0ec28b6SAdrian Ambrożewicz } 25208777fb0SLewanczyk, Dawid } 253588c3f0dSKowalski, Kamil 254ecd6a3a2SEd Tanous SensorsAsyncResp(const SensorsAsyncResp&) = delete; 255ecd6a3a2SEd Tanous SensorsAsyncResp(SensorsAsyncResp&&) = delete; 256ecd6a3a2SEd Tanous SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete; 257ecd6a3a2SEd Tanous SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete; 258ecd6a3a2SEd Tanous 259a0ec28b6SAdrian Ambrożewicz void addMetadata(const nlohmann::json& sensorObject, 260c1d019a6SEd Tanous const std::string& dbusPath) 261a0ec28b6SAdrian Ambrożewicz { 262a0ec28b6SAdrian Ambrożewicz if (metadata) 263a0ec28b6SAdrian Ambrożewicz { 264c1d019a6SEd Tanous metadata->emplace_back(SensorData{ 265c1d019a6SEd Tanous sensorObject["Name"], sensorObject["@odata.id"], dbusPath}); 266a0ec28b6SAdrian Ambrożewicz } 267a0ec28b6SAdrian Ambrożewicz } 268a0ec28b6SAdrian Ambrożewicz 269a0ec28b6SAdrian Ambrożewicz void updateUri(const std::string& name, const std::string& uri) 270a0ec28b6SAdrian Ambrożewicz { 271a0ec28b6SAdrian Ambrożewicz if (metadata) 272a0ec28b6SAdrian Ambrożewicz { 273a0ec28b6SAdrian Ambrożewicz for (auto& sensor : *metadata) 274a0ec28b6SAdrian Ambrożewicz { 275a0ec28b6SAdrian Ambrożewicz if (sensor.name == name) 276a0ec28b6SAdrian Ambrożewicz { 277a0ec28b6SAdrian Ambrożewicz sensor.uri = uri; 278a0ec28b6SAdrian Ambrożewicz } 279a0ec28b6SAdrian Ambrożewicz } 280a0ec28b6SAdrian Ambrożewicz } 281a0ec28b6SAdrian Ambrożewicz } 282a0ec28b6SAdrian Ambrożewicz 2838d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp> asyncResp; 284a0ec28b6SAdrian Ambrożewicz const std::string chassisId; 28502da7c5aSEd Tanous const std::span<std::string_view> types; 286a0ec28b6SAdrian Ambrożewicz const std::string chassisSubNode; 287928fefb9SNan Zhou const bool efficientExpand; 288a0ec28b6SAdrian Ambrożewicz 289a0ec28b6SAdrian Ambrożewicz private: 290a0ec28b6SAdrian Ambrożewicz std::optional<std::vector<SensorData>> metadata; 291a0ec28b6SAdrian Ambrożewicz DataCompleteCb dataComplete; 29208777fb0SLewanczyk, Dawid }; 29308777fb0SLewanczyk, Dawid 29408777fb0SLewanczyk, Dawid /** 295d500549bSAnthony Wilson * Possible states for physical inventory leds 296d500549bSAnthony Wilson */ 297d500549bSAnthony Wilson enum class LedState 298d500549bSAnthony Wilson { 299d500549bSAnthony Wilson OFF, 300d500549bSAnthony Wilson ON, 301d500549bSAnthony Wilson BLINK, 302d500549bSAnthony Wilson UNKNOWN 303d500549bSAnthony Wilson }; 304d500549bSAnthony Wilson 305d500549bSAnthony Wilson /** 306adc4f0dbSShawn McCarney * D-Bus inventory item associated with one or more sensors. 307adc4f0dbSShawn McCarney */ 308adc4f0dbSShawn McCarney class InventoryItem 309adc4f0dbSShawn McCarney { 310adc4f0dbSShawn McCarney public: 3114e23a444SEd Tanous explicit InventoryItem(const std::string& objPath) : objectPath(objPath) 312adc4f0dbSShawn McCarney { 313adc4f0dbSShawn McCarney // Set inventory item name to last node of object path 31428aa8de5SGeorge Liu sdbusplus::message::object_path path(objectPath); 31528aa8de5SGeorge Liu name = path.filename(); 31628aa8de5SGeorge Liu if (name.empty()) 317adc4f0dbSShawn McCarney { 31828aa8de5SGeorge Liu BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath; 319adc4f0dbSShawn McCarney } 320adc4f0dbSShawn McCarney } 321adc4f0dbSShawn McCarney 322adc4f0dbSShawn McCarney std::string objectPath; 323adc4f0dbSShawn McCarney std::string name; 324e05aec50SEd Tanous bool isPresent = true; 325e05aec50SEd Tanous bool isFunctional = true; 326e05aec50SEd Tanous bool isPowerSupply = false; 327e05aec50SEd Tanous int powerSupplyEfficiencyPercent = -1; 328adc4f0dbSShawn McCarney std::string manufacturer; 329adc4f0dbSShawn McCarney std::string model; 330adc4f0dbSShawn McCarney std::string partNumber; 331adc4f0dbSShawn McCarney std::string serialNumber; 332adc4f0dbSShawn McCarney std::set<std::string> sensors; 333d500549bSAnthony Wilson std::string ledObjectPath; 334e05aec50SEd Tanous LedState ledState = LedState::UNKNOWN; 335adc4f0dbSShawn McCarney }; 336adc4f0dbSShawn McCarney 337adc4f0dbSShawn McCarney /** 338413961deSRichard Marian Thomaiyar * @brief Get objects with connection necessary for sensors 339588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 34008777fb0SLewanczyk, Dawid * @param sensorNames Sensors retrieved from chassis 34108777fb0SLewanczyk, Dawid * @param callback Callback for processing gathered connections 34208777fb0SLewanczyk, Dawid */ 34308777fb0SLewanczyk, Dawid template <typename Callback> 344413961deSRichard Marian Thomaiyar void getObjectsWithConnection( 34581ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 346fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames, 3471abe55efSEd Tanous Callback&& callback) 3481abe55efSEd Tanous { 349413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter"; 35003b5bae3SJames Feist const std::string path = "/xyz/openbmc_project/sensors"; 35108777fb0SLewanczyk, Dawid const std::array<std::string, 1> interfaces = { 35208777fb0SLewanczyk, Dawid "xyz.openbmc_project.Sensor.Value"}; 35308777fb0SLewanczyk, Dawid 35408777fb0SLewanczyk, Dawid // Response handler for parsing objects subtree 355002d39b4SEd Tanous auto respHandler = 356002d39b4SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 357002d39b4SEd Tanous sensorNames](const boost::system::error_code ec, 358002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 359413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter"; 3601abe55efSEd Tanous if (ec) 3611abe55efSEd Tanous { 3628d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 363413961deSRichard Marian Thomaiyar BMCWEB_LOG_ERROR 364413961deSRichard Marian Thomaiyar << "getObjectsWithConnection resp_handler: Dbus error " << ec; 36508777fb0SLewanczyk, Dawid return; 36608777fb0SLewanczyk, Dawid } 36708777fb0SLewanczyk, Dawid 36855c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees"; 36908777fb0SLewanczyk, Dawid 37008777fb0SLewanczyk, Dawid // Make unique list of connections only for requested sensor types and 37108777fb0SLewanczyk, Dawid // found in the chassis 372fe04d49cSNan Zhou std::set<std::string> connections; 373413961deSRichard Marian Thomaiyar std::set<std::pair<std::string, std::string>> objectsWithConnection; 37408777fb0SLewanczyk, Dawid 37549c53ac9SJohnathan Mantey BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size(); 37649c53ac9SJohnathan Mantey for (const std::string& tsensor : *sensorNames) 3771abe55efSEd Tanous { 37855c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor; 37908777fb0SLewanczyk, Dawid } 38008777fb0SLewanczyk, Dawid 38108777fb0SLewanczyk, Dawid for (const std::pair< 38208777fb0SLewanczyk, Dawid std::string, 38308777fb0SLewanczyk, Dawid std::vector<std::pair<std::string, std::vector<std::string>>>>& 3841abe55efSEd Tanous object : subtree) 3851abe55efSEd Tanous { 38649c53ac9SJohnathan Mantey if (sensorNames->find(object.first) != sensorNames->end()) 3871abe55efSEd Tanous { 38849c53ac9SJohnathan Mantey for (const std::pair<std::string, std::vector<std::string>>& 3891abe55efSEd Tanous objData : object.second) 3901abe55efSEd Tanous { 39149c53ac9SJohnathan Mantey BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first; 39208777fb0SLewanczyk, Dawid connections.insert(objData.first); 393de629b6eSShawn McCarney objectsWithConnection.insert( 394de629b6eSShawn McCarney std::make_pair(object.first, objData.first)); 39508777fb0SLewanczyk, Dawid } 39608777fb0SLewanczyk, Dawid } 39708777fb0SLewanczyk, Dawid } 39855c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections"; 399413961deSRichard Marian Thomaiyar callback(std::move(connections), std::move(objectsWithConnection)); 400413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit"; 40108777fb0SLewanczyk, Dawid }; 40208777fb0SLewanczyk, Dawid // Make call to ObjectMapper to find all sensors objects 40355c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 40455c7b7a2SEd Tanous std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 4051abe55efSEd Tanous "/xyz/openbmc_project/object_mapper", 4061abe55efSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces); 407413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit"; 408413961deSRichard Marian Thomaiyar } 409413961deSRichard Marian Thomaiyar 410413961deSRichard Marian Thomaiyar /** 411413961deSRichard Marian Thomaiyar * @brief Create connections necessary for sensors 412413961deSRichard Marian Thomaiyar * @param SensorsAsyncResp Pointer to object holding response data 413413961deSRichard Marian Thomaiyar * @param sensorNames Sensors retrieved from chassis 414413961deSRichard Marian Thomaiyar * @param callback Callback for processing gathered connections 415413961deSRichard Marian Thomaiyar */ 416413961deSRichard Marian Thomaiyar template <typename Callback> 417fe04d49cSNan Zhou void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 418fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> sensorNames, 419413961deSRichard Marian Thomaiyar Callback&& callback) 420413961deSRichard Marian Thomaiyar { 421413961deSRichard Marian Thomaiyar auto objectsWithConnectionCb = 422fe04d49cSNan Zhou [callback](const std::set<std::string>& connections, 423413961deSRichard Marian Thomaiyar const std::set<std::pair<std::string, std::string>>& 4243174e4dfSEd Tanous /*objectsWithConnection*/) { callback(connections); }; 42581ce609eSEd Tanous getObjectsWithConnection(sensorsAsyncResp, sensorNames, 426413961deSRichard Marian Thomaiyar std::move(objectsWithConnectionCb)); 42708777fb0SLewanczyk, Dawid } 42808777fb0SLewanczyk, Dawid 42908777fb0SLewanczyk, Dawid /** 43049c53ac9SJohnathan Mantey * @brief Shrinks the list of sensors for processing 43149c53ac9SJohnathan Mantey * @param SensorsAysncResp The class holding the Redfish response 43249c53ac9SJohnathan Mantey * @param allSensors A list of all the sensors associated to the 43349c53ac9SJohnathan Mantey * chassis element (i.e. baseboard, front panel, etc...) 43449c53ac9SJohnathan Mantey * @param activeSensors A list that is a reduction of the incoming 43549c53ac9SJohnathan Mantey * allSensors list. Eliminate Thermal sensors when a Power request is 43649c53ac9SJohnathan Mantey * made, and eliminate Power sensors when a Thermal request is made. 43749c53ac9SJohnathan Mantey */ 43823a21a1cSEd Tanous inline void reduceSensorList( 4397f1cc26dSEd Tanous crow::Response& res, std::string_view chassisSubNode, 4407f1cc26dSEd Tanous std::span<std::string_view> sensorTypes, 44149c53ac9SJohnathan Mantey const std::vector<std::string>* allSensors, 442fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& activeSensors) 44349c53ac9SJohnathan Mantey { 44449c53ac9SJohnathan Mantey if ((allSensors == nullptr) || (activeSensors == nullptr)) 44549c53ac9SJohnathan Mantey { 4467f1cc26dSEd Tanous messages::resourceNotFound(res, chassisSubNode, 4477f1cc26dSEd Tanous chassisSubNode == sensors::node::thermal 448a0ec28b6SAdrian Ambrożewicz ? "Temperatures" 44949c53ac9SJohnathan Mantey : "Voltages"); 45049c53ac9SJohnathan Mantey 45149c53ac9SJohnathan Mantey return; 45249c53ac9SJohnathan Mantey } 45349c53ac9SJohnathan Mantey if (allSensors->empty()) 45449c53ac9SJohnathan Mantey { 45549c53ac9SJohnathan Mantey // Nothing to do, the activeSensors object is also empty 45649c53ac9SJohnathan Mantey return; 45749c53ac9SJohnathan Mantey } 45849c53ac9SJohnathan Mantey 4597f1cc26dSEd Tanous for (std::string_view type : sensorTypes) 46049c53ac9SJohnathan Mantey { 46149c53ac9SJohnathan Mantey for (const std::string& sensor : *allSensors) 46249c53ac9SJohnathan Mantey { 46311ba3979SEd Tanous if (sensor.starts_with(type)) 46449c53ac9SJohnathan Mantey { 46549c53ac9SJohnathan Mantey activeSensors->emplace(sensor); 46649c53ac9SJohnathan Mantey } 46749c53ac9SJohnathan Mantey } 46849c53ac9SJohnathan Mantey } 46949c53ac9SJohnathan Mantey } 47049c53ac9SJohnathan Mantey 4717f1cc26dSEd Tanous /* 4727f1cc26dSEd Tanous *Populates the top level collection for a given subnode. Populates 4737f1cc26dSEd Tanous *SensorCollection, Power, or Thermal schemas. 4747f1cc26dSEd Tanous * 4757f1cc26dSEd Tanous * */ 4767f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue, 4777f1cc26dSEd Tanous std::string_view chassisSubNode) 4787f1cc26dSEd Tanous { 4797f1cc26dSEd Tanous if (chassisSubNode == sensors::node::power) 4807f1cc26dSEd Tanous { 4817f1cc26dSEd Tanous jsonValue["@odata.type"] = "#Power.v1_5_2.Power"; 4827f1cc26dSEd Tanous } 4837f1cc26dSEd Tanous else if (chassisSubNode == sensors::node::thermal) 4847f1cc26dSEd Tanous { 4857f1cc26dSEd Tanous jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal"; 4867f1cc26dSEd Tanous jsonValue["Fans"] = nlohmann::json::array(); 4877f1cc26dSEd Tanous jsonValue["Temperatures"] = nlohmann::json::array(); 4887f1cc26dSEd Tanous } 4897f1cc26dSEd Tanous else if (chassisSubNode == sensors::node::sensors) 4907f1cc26dSEd Tanous { 4917f1cc26dSEd Tanous jsonValue["@odata.type"] = "#SensorCollection.SensorCollection"; 4927f1cc26dSEd Tanous jsonValue["Description"] = "Collection of Sensors for this Chassis"; 4937f1cc26dSEd Tanous jsonValue["Members"] = nlohmann::json::array(); 4947f1cc26dSEd Tanous jsonValue["Members@odata.count"] = 0; 4957f1cc26dSEd Tanous } 4967f1cc26dSEd Tanous 4977f1cc26dSEd Tanous if (chassisSubNode != sensors::node::sensors) 4987f1cc26dSEd Tanous { 4997f1cc26dSEd Tanous jsonValue["Id"] = chassisSubNode; 5007f1cc26dSEd Tanous } 5017f1cc26dSEd Tanous jsonValue["Name"] = chassisSubNode; 5027f1cc26dSEd Tanous } 5037f1cc26dSEd Tanous 50449c53ac9SJohnathan Mantey /** 50508777fb0SLewanczyk, Dawid * @brief Retrieves requested chassis sensors and redundancy data from DBus . 506588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 50708777fb0SLewanczyk, Dawid * @param callback Callback for next step in gathered sensor processing 50808777fb0SLewanczyk, Dawid */ 50908777fb0SLewanczyk, Dawid template <typename Callback> 5107f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 5117f1cc26dSEd Tanous std::string_view chassisId, std::string_view chassisSubNode, 5127f1cc26dSEd Tanous std::span<std::string_view> sensorTypes, Callback&& callback) 5131abe55efSEd Tanous { 51455c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis enter"; 515adc4f0dbSShawn McCarney const std::array<const char*, 2> interfaces = { 51649c53ac9SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Board", 517adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.Chassis"}; 518002d39b4SEd Tanous auto respHandler = 5197f1cc26dSEd Tanous [callback{std::forward<Callback>(callback)}, asyncResp, 5207f1cc26dSEd Tanous chassisIdStr{std::string(chassisId)}, 5217f1cc26dSEd Tanous chassisSubNode{std::string(chassisSubNode)}, sensorTypes]( 52249c53ac9SJohnathan Mantey const boost::system::error_code ec, 523002d39b4SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) { 52455c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis respHandler enter"; 5251abe55efSEd Tanous if (ec) 5261abe55efSEd Tanous { 52755c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec; 5287f1cc26dSEd Tanous messages::internalError(asyncResp->res); 52908777fb0SLewanczyk, Dawid return; 53008777fb0SLewanczyk, Dawid } 53149c53ac9SJohnathan Mantey const std::string* chassisPath = nullptr; 53249c53ac9SJohnathan Mantey std::string chassisName; 53349c53ac9SJohnathan Mantey for (const std::string& chassis : chassisPaths) 5341abe55efSEd Tanous { 53528aa8de5SGeorge Liu sdbusplus::message::object_path path(chassis); 53628aa8de5SGeorge Liu chassisName = path.filename(); 53728aa8de5SGeorge Liu if (chassisName.empty()) 5381abe55efSEd Tanous { 53949c53ac9SJohnathan Mantey BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis; 540daf36e2eSEd Tanous continue; 541daf36e2eSEd Tanous } 5427f1cc26dSEd Tanous if (chassisName == chassisIdStr) 5431abe55efSEd Tanous { 54449c53ac9SJohnathan Mantey chassisPath = &chassis; 54549c53ac9SJohnathan Mantey break; 546daf36e2eSEd Tanous } 54749c53ac9SJohnathan Mantey } 54849c53ac9SJohnathan Mantey if (chassisPath == nullptr) 5491abe55efSEd Tanous { 5507f1cc26dSEd Tanous messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr); 55149c53ac9SJohnathan Mantey return; 5521abe55efSEd Tanous } 5537f1cc26dSEd Tanous populateChassisNode(asyncResp->res.jsonValue, chassisSubNode); 55408777fb0SLewanczyk, Dawid 5557f1cc26dSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 5567f1cc26dSEd Tanous "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode; 55795a3ecadSAnthony Wilson 5588fb49dd6SShawn McCarney // Get the list of all sensors for this Chassis element 5598fb49dd6SShawn McCarney std::string sensorPath = *chassisPath + "/all_sensors"; 5601e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::vector<std::string>>( 5611e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper", 5621e1e598dSJonathan Doman sensorPath, "xyz.openbmc_project.Association", "endpoints", 5637f1cc26dSEd Tanous [asyncResp, chassisSubNode, sensorTypes, 564f94c4ecfSEd Tanous callback{std::forward<const Callback>(callback)}]( 565271584abSEd Tanous const boost::system::error_code& e, 5661e1e598dSJonathan Doman const std::vector<std::string>& nodeSensorList) { 567271584abSEd Tanous if (e) 56849c53ac9SJohnathan Mantey { 569271584abSEd Tanous if (e.value() != EBADR) 57049c53ac9SJohnathan Mantey { 5717f1cc26dSEd Tanous messages::internalError(asyncResp->res); 57249c53ac9SJohnathan Mantey return; 57349c53ac9SJohnathan Mantey } 57449c53ac9SJohnathan Mantey } 575fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> culledSensorList = 576fe04d49cSNan Zhou std::make_shared<std::set<std::string>>(); 5777f1cc26dSEd Tanous reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes, 5787f1cc26dSEd Tanous &nodeSensorList, culledSensorList); 5797f1cc26dSEd Tanous BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size(); 58049c53ac9SJohnathan Mantey callback(culledSensorList); 5811e1e598dSJonathan Doman }); 58249c53ac9SJohnathan Mantey }; 58349c53ac9SJohnathan Mantey 58449c53ac9SJohnathan Mantey // Get the Chassis Collection 58549c53ac9SJohnathan Mantey crow::connections::systemBus->async_method_call( 58649c53ac9SJohnathan Mantey respHandler, "xyz.openbmc_project.ObjectMapper", 58749c53ac9SJohnathan Mantey "/xyz/openbmc_project/object_mapper", 58849c53ac9SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 589271584abSEd Tanous "/xyz/openbmc_project/inventory", 0, interfaces); 59055c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis exit"; 59108777fb0SLewanczyk, Dawid } 59208777fb0SLewanczyk, Dawid 59308777fb0SLewanczyk, Dawid /** 594de629b6eSShawn McCarney * @brief Finds all DBus object paths that implement ObjectManager. 595de629b6eSShawn McCarney * 596de629b6eSShawn McCarney * Creates a mapping from the associated connection name to the object path. 597de629b6eSShawn McCarney * 598de629b6eSShawn McCarney * Finds the object paths asynchronously. Invokes callback when information has 599de629b6eSShawn McCarney * been obtained. 600de629b6eSShawn McCarney * 601de629b6eSShawn McCarney * The callback must have the following signature: 602de629b6eSShawn McCarney * @code 603fe04d49cSNan Zhou * callback(std::shared_ptr<std::map<std::string,std::string>> objectMgrPaths) 604de629b6eSShawn McCarney * @endcode 605de629b6eSShawn McCarney * 60649c53ac9SJohnathan Mantey * @param sensorsAsyncResp Pointer to object holding response data. 607de629b6eSShawn McCarney * @param callback Callback to invoke when object paths obtained. 608de629b6eSShawn McCarney */ 609de629b6eSShawn McCarney template <typename Callback> 610b5a76932SEd Tanous void getObjectManagerPaths( 61181ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 612de629b6eSShawn McCarney Callback&& callback) 613de629b6eSShawn McCarney { 614de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter"; 615de629b6eSShawn McCarney const std::array<std::string, 1> interfaces = { 616de629b6eSShawn McCarney "org.freedesktop.DBus.ObjectManager"}; 617de629b6eSShawn McCarney 618de629b6eSShawn McCarney // Response handler for GetSubTree DBus method 619002d39b4SEd Tanous auto respHandler = 620002d39b4SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp]( 621b9d36b47SEd Tanous const boost::system::error_code ec, 622002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 623de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter"; 624de629b6eSShawn McCarney if (ec) 625de629b6eSShawn McCarney { 6268d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 627de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error " 628de629b6eSShawn McCarney << ec; 629de629b6eSShawn McCarney return; 630de629b6eSShawn McCarney } 631de629b6eSShawn McCarney 632de629b6eSShawn McCarney // Loop over returned object paths 633fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths = 634fe04d49cSNan Zhou std::make_shared<std::map<std::string, std::string>>(); 635de629b6eSShawn McCarney for (const std::pair< 636de629b6eSShawn McCarney std::string, 637de629b6eSShawn McCarney std::vector<std::pair<std::string, std::vector<std::string>>>>& 638de629b6eSShawn McCarney object : subtree) 639de629b6eSShawn McCarney { 640de629b6eSShawn McCarney // Loop over connections for current object path 641de629b6eSShawn McCarney const std::string& objectPath = object.first; 642de629b6eSShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 643de629b6eSShawn McCarney objData : object.second) 644de629b6eSShawn McCarney { 645de629b6eSShawn McCarney // Add mapping from connection to object path 646de629b6eSShawn McCarney const std::string& connection = objData.first; 6478fb49dd6SShawn McCarney (*objectMgrPaths)[connection] = objectPath; 648de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> " 649de629b6eSShawn McCarney << objectPath; 650de629b6eSShawn McCarney } 651de629b6eSShawn McCarney } 6528fb49dd6SShawn McCarney callback(objectMgrPaths); 653de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit"; 654de629b6eSShawn McCarney }; 655de629b6eSShawn McCarney 656de629b6eSShawn McCarney // Query mapper for all DBus object paths that implement ObjectManager 657de629b6eSShawn McCarney crow::connections::systemBus->async_method_call( 658de629b6eSShawn McCarney std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 659de629b6eSShawn McCarney "/xyz/openbmc_project/object_mapper", 660271584abSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces); 661de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit"; 662de629b6eSShawn McCarney } 663de629b6eSShawn McCarney 664de629b6eSShawn McCarney /** 665adc4f0dbSShawn McCarney * @brief Returns the Redfish State value for the specified inventory item. 666adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with a sensor. 667adc4f0dbSShawn McCarney * @return State value for inventory item. 66834dd179eSJames Feist */ 66923a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem) 670adc4f0dbSShawn McCarney { 671adc4f0dbSShawn McCarney if ((inventoryItem != nullptr) && !(inventoryItem->isPresent)) 672adc4f0dbSShawn McCarney { 673adc4f0dbSShawn McCarney return "Absent"; 674adc4f0dbSShawn McCarney } 67534dd179eSJames Feist 676adc4f0dbSShawn McCarney return "Enabled"; 677adc4f0dbSShawn McCarney } 678adc4f0dbSShawn McCarney 679adc4f0dbSShawn McCarney /** 680adc4f0dbSShawn McCarney * @brief Returns the Redfish Health value for the specified sensor. 681adc4f0dbSShawn McCarney * @param sensorJson Sensor JSON object. 6821d7c0054SEd Tanous * @param valuesDict Map of all sensor DBus values. 683adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with the sensor. Will 684adc4f0dbSShawn McCarney * be nullptr if no associated inventory item was found. 685adc4f0dbSShawn McCarney * @return Health value for sensor. 686adc4f0dbSShawn McCarney */ 6871d7c0054SEd Tanous inline std::string getHealth(nlohmann::json& sensorJson, 6881d7c0054SEd Tanous const dbus::utility::DBusPropertiesMap& valuesDict, 689adc4f0dbSShawn McCarney const InventoryItem* inventoryItem) 69034dd179eSJames Feist { 691adc4f0dbSShawn McCarney // Get current health value (if any) in the sensor JSON object. Some JSON 692adc4f0dbSShawn McCarney // objects contain multiple sensors (such as PowerSupplies). We want to set 693adc4f0dbSShawn McCarney // the overall health to be the most severe of any of the sensors. 694adc4f0dbSShawn McCarney std::string currentHealth; 695adc4f0dbSShawn McCarney auto statusIt = sensorJson.find("Status"); 696adc4f0dbSShawn McCarney if (statusIt != sensorJson.end()) 697adc4f0dbSShawn McCarney { 698adc4f0dbSShawn McCarney auto healthIt = statusIt->find("Health"); 699adc4f0dbSShawn McCarney if (healthIt != statusIt->end()) 700adc4f0dbSShawn McCarney { 701adc4f0dbSShawn McCarney std::string* health = healthIt->get_ptr<std::string*>(); 702adc4f0dbSShawn McCarney if (health != nullptr) 703adc4f0dbSShawn McCarney { 704adc4f0dbSShawn McCarney currentHealth = *health; 705adc4f0dbSShawn McCarney } 706adc4f0dbSShawn McCarney } 707adc4f0dbSShawn McCarney } 708adc4f0dbSShawn McCarney 709adc4f0dbSShawn McCarney // If current health in JSON object is already Critical, return that. This 710adc4f0dbSShawn McCarney // should override the sensor health, which might be less severe. 711adc4f0dbSShawn McCarney if (currentHealth == "Critical") 712adc4f0dbSShawn McCarney { 713adc4f0dbSShawn McCarney return "Critical"; 714adc4f0dbSShawn McCarney } 715adc4f0dbSShawn McCarney 716c1343bf6SKrzysztof Grobelny const bool* criticalAlarmHigh = nullptr; 717c1343bf6SKrzysztof Grobelny const bool* criticalAlarmLow = nullptr; 718c1343bf6SKrzysztof Grobelny const bool* warningAlarmHigh = nullptr; 719c1343bf6SKrzysztof Grobelny const bool* warningAlarmLow = nullptr; 720711ac7a9SEd Tanous 721c1343bf6SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 722c1343bf6SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh", 723c1343bf6SKrzysztof Grobelny criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow, 724c1343bf6SKrzysztof Grobelny "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow", 725c1343bf6SKrzysztof Grobelny warningAlarmLow); 726c1343bf6SKrzysztof Grobelny 727c1343bf6SKrzysztof Grobelny if (success) 72834dd179eSJames Feist { 729c1343bf6SKrzysztof Grobelny // Check if sensor has critical threshold alarm 730c1343bf6SKrzysztof Grobelny if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) || 731c1343bf6SKrzysztof Grobelny (criticalAlarmLow != nullptr && *criticalAlarmLow)) 73234dd179eSJames Feist { 73334dd179eSJames Feist return "Critical"; 73434dd179eSJames Feist } 73534dd179eSJames Feist } 73634dd179eSJames Feist 737adc4f0dbSShawn McCarney // Check if associated inventory item is not functional 738adc4f0dbSShawn McCarney if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional)) 739adc4f0dbSShawn McCarney { 740adc4f0dbSShawn McCarney return "Critical"; 741adc4f0dbSShawn McCarney } 742adc4f0dbSShawn McCarney 743adc4f0dbSShawn McCarney // If current health in JSON object is already Warning, return that. This 744adc4f0dbSShawn McCarney // should override the sensor status, which might be less severe. 745adc4f0dbSShawn McCarney if (currentHealth == "Warning") 746adc4f0dbSShawn McCarney { 747adc4f0dbSShawn McCarney return "Warning"; 748adc4f0dbSShawn McCarney } 749adc4f0dbSShawn McCarney 750c1343bf6SKrzysztof Grobelny if (success) 751c1343bf6SKrzysztof Grobelny { 752adc4f0dbSShawn McCarney // Check if sensor has warning threshold alarm 753c1343bf6SKrzysztof Grobelny if ((warningAlarmHigh != nullptr && *warningAlarmHigh) || 754c1343bf6SKrzysztof Grobelny (warningAlarmLow != nullptr && *warningAlarmLow)) 75534dd179eSJames Feist { 756ebe4d91eSEd Tanous return "Warning"; 75734dd179eSJames Feist } 75834dd179eSJames Feist } 759adc4f0dbSShawn McCarney 76034dd179eSJames Feist return "OK"; 76134dd179eSJames Feist } 76234dd179eSJames Feist 76323a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson, 764d500549bSAnthony Wilson const InventoryItem* inventoryItem) 765d500549bSAnthony Wilson { 766d500549bSAnthony Wilson if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty()) 767d500549bSAnthony Wilson { 768d500549bSAnthony Wilson switch (inventoryItem->ledState) 769d500549bSAnthony Wilson { 770d500549bSAnthony Wilson case LedState::OFF: 771d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Off"; 772d500549bSAnthony Wilson break; 773d500549bSAnthony Wilson case LedState::ON: 774d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Lit"; 775d500549bSAnthony Wilson break; 776d500549bSAnthony Wilson case LedState::BLINK: 777d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Blinking"; 778d500549bSAnthony Wilson break; 77923a21a1cSEd Tanous case LedState::UNKNOWN: 780d500549bSAnthony Wilson break; 781d500549bSAnthony Wilson } 782d500549bSAnthony Wilson } 783d500549bSAnthony Wilson } 784d500549bSAnthony Wilson 78534dd179eSJames Feist /** 78608777fb0SLewanczyk, Dawid * @brief Builds a json sensor representation of a sensor. 78708777fb0SLewanczyk, Dawid * @param sensorName The name of the sensor to be built 788274fad5aSGunnar Mills * @param sensorType The type (temperature, fan_tach, etc) of the sensor to 78908777fb0SLewanczyk, Dawid * build 7901d7c0054SEd Tanous * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor 7911d7c0054SEd Tanous * @param propertiesDict A dictionary of the properties to build the sensor 7921d7c0054SEd Tanous * from. 7931d7c0054SEd Tanous * @param sensorJson The json object to fill 794adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with the sensor. Will 795adc4f0dbSShawn McCarney * be nullptr if no associated inventory item was found. 79608777fb0SLewanczyk, Dawid */ 7971d7c0054SEd Tanous inline void objectPropertiesToJson( 7981d7c0054SEd Tanous std::string_view sensorName, std::string_view sensorType, 7991d7c0054SEd Tanous std::string_view chassisSubNode, 8001d7c0054SEd Tanous const dbus::utility::DBusPropertiesMap& propertiesDict, 80181ce609eSEd Tanous nlohmann::json& sensorJson, InventoryItem* inventoryItem) 8021abe55efSEd Tanous { 8031d7c0054SEd Tanous if (chassisSubNode == sensors::node::sensors) 804adc4f0dbSShawn McCarney { 805c1d019a6SEd Tanous std::string subNodeEscaped(chassisSubNode); 806c1d019a6SEd Tanous subNodeEscaped.erase( 807c1d019a6SEd Tanous std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'), 808c1d019a6SEd Tanous subNodeEscaped.end()); 809c1d019a6SEd Tanous 810c1d019a6SEd Tanous // For sensors in SensorCollection we set Id instead of MemberId, 811c1d019a6SEd Tanous // including power sensors. 812c1d019a6SEd Tanous subNodeEscaped += '_'; 813c1d019a6SEd Tanous subNodeEscaped += sensorName; 814c1d019a6SEd Tanous sensorJson["Id"] = std::move(subNodeEscaped); 815c1d019a6SEd Tanous 8161d7c0054SEd Tanous std::string sensorNameEs(sensorName); 8171d7c0054SEd Tanous std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' '); 8181d7c0054SEd Tanous sensorJson["Name"] = std::move(sensorNameEs); 81995a3ecadSAnthony Wilson } 82095a3ecadSAnthony Wilson else if (sensorType != "power") 82195a3ecadSAnthony Wilson { 82295a3ecadSAnthony Wilson // Set MemberId and Name for non-power sensors. For PowerSupplies and 82395a3ecadSAnthony Wilson // PowerControl, those properties have more general values because 82495a3ecadSAnthony Wilson // multiple sensors can be stored in the same JSON object. 82581ce609eSEd Tanous sensorJson["MemberId"] = sensorName; 8261d7c0054SEd Tanous std::string sensorNameEs(sensorName); 8271d7c0054SEd Tanous std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' '); 8281d7c0054SEd Tanous sensorJson["Name"] = std::move(sensorNameEs); 829adc4f0dbSShawn McCarney } 830e742b6ccSEd Tanous 83181ce609eSEd Tanous sensorJson["Status"]["State"] = getState(inventoryItem); 83281ce609eSEd Tanous sensorJson["Status"]["Health"] = 8331d7c0054SEd Tanous getHealth(sensorJson, propertiesDict, inventoryItem); 83408777fb0SLewanczyk, Dawid 83508777fb0SLewanczyk, Dawid // Parameter to set to override the type we get from dbus, and force it to 83608777fb0SLewanczyk, Dawid // int, regardless of what is available. This is used for schemas like fan, 83708777fb0SLewanczyk, Dawid // that require integers, not floats. 83808777fb0SLewanczyk, Dawid bool forceToInt = false; 83908777fb0SLewanczyk, Dawid 8403929aca1SAnthony Wilson nlohmann::json::json_pointer unit("/Reading"); 8411d7c0054SEd Tanous if (chassisSubNode == sensors::node::sensors) 84295a3ecadSAnthony Wilson { 8432a4ba195SShounak Mitra sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor"; 844c2bf7f99SWludzik, Jozef 8451d7c0054SEd Tanous std::string_view readingType = sensors::toReadingType(sensorType); 846c2bf7f99SWludzik, Jozef if (readingType.empty()) 84795a3ecadSAnthony Wilson { 848c2bf7f99SWludzik, Jozef BMCWEB_LOG_ERROR << "Redfish cannot map reading type for " 849c2bf7f99SWludzik, Jozef << sensorType; 85095a3ecadSAnthony Wilson } 851c2bf7f99SWludzik, Jozef else 85295a3ecadSAnthony Wilson { 853c2bf7f99SWludzik, Jozef sensorJson["ReadingType"] = readingType; 85495a3ecadSAnthony Wilson } 855c2bf7f99SWludzik, Jozef 8561d7c0054SEd Tanous std::string_view readingUnits = sensors::toReadingUnits(sensorType); 857c2bf7f99SWludzik, Jozef if (readingUnits.empty()) 858f8ede15eSAdrian Ambrożewicz { 859c2bf7f99SWludzik, Jozef BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for " 860c2bf7f99SWludzik, Jozef << sensorType; 861c2bf7f99SWludzik, Jozef } 862c2bf7f99SWludzik, Jozef else 863c2bf7f99SWludzik, Jozef { 864c2bf7f99SWludzik, Jozef sensorJson["ReadingUnits"] = readingUnits; 865f8ede15eSAdrian Ambrożewicz } 86695a3ecadSAnthony Wilson } 86795a3ecadSAnthony Wilson else if (sensorType == "temperature") 8681abe55efSEd Tanous { 8693929aca1SAnthony Wilson unit = "/ReadingCelsius"_json_pointer; 87081ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature"; 87108777fb0SLewanczyk, Dawid // TODO(ed) Documentation says that path should be type fan_tach, 87208777fb0SLewanczyk, Dawid // implementation seems to implement fan 8731abe55efSEd Tanous } 8741abe55efSEd Tanous else if (sensorType == "fan" || sensorType == "fan_tach") 8751abe55efSEd Tanous { 8763929aca1SAnthony Wilson unit = "/Reading"_json_pointer; 87781ce609eSEd Tanous sensorJson["ReadingUnits"] = "RPM"; 87881ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan"; 87981ce609eSEd Tanous setLedState(sensorJson, inventoryItem); 88008777fb0SLewanczyk, Dawid forceToInt = true; 8811abe55efSEd Tanous } 8826f6d0d32SEd Tanous else if (sensorType == "fan_pwm") 8836f6d0d32SEd Tanous { 8843929aca1SAnthony Wilson unit = "/Reading"_json_pointer; 88581ce609eSEd Tanous sensorJson["ReadingUnits"] = "Percent"; 88681ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan"; 88781ce609eSEd Tanous setLedState(sensorJson, inventoryItem); 8886f6d0d32SEd Tanous forceToInt = true; 8896f6d0d32SEd Tanous } 8901abe55efSEd Tanous else if (sensorType == "voltage") 8911abe55efSEd Tanous { 8923929aca1SAnthony Wilson unit = "/ReadingVolts"_json_pointer; 89381ce609eSEd Tanous sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage"; 8941abe55efSEd Tanous } 8952474adfaSEd Tanous else if (sensorType == "power") 8962474adfaSEd Tanous { 8971d7c0054SEd Tanous if (boost::iequals(sensorName, "total_power")) 898028f7ebcSEddie James { 89981ce609eSEd Tanous sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl"; 9007ab06f49SGunnar Mills // Put multiple "sensors" into a single PowerControl, so have 9017ab06f49SGunnar Mills // generic names for MemberId and Name. Follows Redfish mockup. 90281ce609eSEd Tanous sensorJson["MemberId"] = "0"; 90381ce609eSEd Tanous sensorJson["Name"] = "Chassis Power Control"; 9043929aca1SAnthony Wilson unit = "/PowerConsumedWatts"_json_pointer; 905028f7ebcSEddie James } 9061d7c0054SEd Tanous else if (boost::ifind_first(sensorName, "input").empty()) 90749c53ac9SJohnathan Mantey { 9083929aca1SAnthony Wilson unit = "/PowerInputWatts"_json_pointer; 90949c53ac9SJohnathan Mantey } 91049c53ac9SJohnathan Mantey else 91149c53ac9SJohnathan Mantey { 9123929aca1SAnthony Wilson unit = "/PowerOutputWatts"_json_pointer; 91349c53ac9SJohnathan Mantey } 9142474adfaSEd Tanous } 9151abe55efSEd Tanous else 9161abe55efSEd Tanous { 91755c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName; 91808777fb0SLewanczyk, Dawid return; 91908777fb0SLewanczyk, Dawid } 92008777fb0SLewanczyk, Dawid // Map of dbus interface name, dbus property name and redfish property_name 9213929aca1SAnthony Wilson std::vector< 9223929aca1SAnthony Wilson std::tuple<const char*, const char*, nlohmann::json::json_pointer>> 9233929aca1SAnthony Wilson properties; 92408777fb0SLewanczyk, Dawid properties.reserve(7); 92508777fb0SLewanczyk, Dawid 92608777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit); 927de629b6eSShawn McCarney 9281d7c0054SEd Tanous if (chassisSubNode == sensors::node::sensors) 9293929aca1SAnthony Wilson { 9303929aca1SAnthony Wilson properties.emplace_back( 9313929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh", 9323929aca1SAnthony Wilson "/Thresholds/UpperCaution/Reading"_json_pointer); 9333929aca1SAnthony Wilson properties.emplace_back( 9343929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow", 9353929aca1SAnthony Wilson "/Thresholds/LowerCaution/Reading"_json_pointer); 9363929aca1SAnthony Wilson properties.emplace_back( 9373929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh", 9383929aca1SAnthony Wilson "/Thresholds/UpperCritical/Reading"_json_pointer); 9393929aca1SAnthony Wilson properties.emplace_back( 9403929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow", 9413929aca1SAnthony Wilson "/Thresholds/LowerCritical/Reading"_json_pointer); 9423929aca1SAnthony Wilson } 9433929aca1SAnthony Wilson else if (sensorType != "power") 944de629b6eSShawn McCarney { 94508777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 9463929aca1SAnthony Wilson "WarningHigh", 9473929aca1SAnthony Wilson "/UpperThresholdNonCritical"_json_pointer); 94808777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 9493929aca1SAnthony Wilson "WarningLow", 9503929aca1SAnthony Wilson "/LowerThresholdNonCritical"_json_pointer); 95108777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 9523929aca1SAnthony Wilson "CriticalHigh", 9533929aca1SAnthony Wilson "/UpperThresholdCritical"_json_pointer); 95408777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 9553929aca1SAnthony Wilson "CriticalLow", 9563929aca1SAnthony Wilson "/LowerThresholdCritical"_json_pointer); 957de629b6eSShawn McCarney } 95808777fb0SLewanczyk, Dawid 9592474adfaSEd Tanous // TODO Need to get UpperThresholdFatal and LowerThresholdFatal 9602474adfaSEd Tanous 9611d7c0054SEd Tanous if (chassisSubNode == sensors::node::sensors) 96295a3ecadSAnthony Wilson { 96395a3ecadSAnthony Wilson properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 9643929aca1SAnthony Wilson "/ReadingRangeMin"_json_pointer); 96595a3ecadSAnthony Wilson properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 9663929aca1SAnthony Wilson "/ReadingRangeMax"_json_pointer); 96795a3ecadSAnthony Wilson } 96895a3ecadSAnthony Wilson else if (sensorType == "temperature") 9691abe55efSEd Tanous { 97008777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 9713929aca1SAnthony Wilson "/MinReadingRangeTemp"_json_pointer); 97208777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 9733929aca1SAnthony Wilson "/MaxReadingRangeTemp"_json_pointer); 9741abe55efSEd Tanous } 975adc4f0dbSShawn McCarney else if (sensorType != "power") 9761abe55efSEd Tanous { 97708777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 9783929aca1SAnthony Wilson "/MinReadingRange"_json_pointer); 97908777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 9803929aca1SAnthony Wilson "/MaxReadingRange"_json_pointer); 98108777fb0SLewanczyk, Dawid } 98208777fb0SLewanczyk, Dawid 9833929aca1SAnthony Wilson for (const std::tuple<const char*, const char*, 9843929aca1SAnthony Wilson nlohmann::json::json_pointer>& p : properties) 9851abe55efSEd Tanous { 9861d7c0054SEd Tanous for (const auto& [valueName, valueVariant] : propertiesDict) 987711ac7a9SEd Tanous { 988711ac7a9SEd Tanous if (valueName != std::get<1>(p)) 989711ac7a9SEd Tanous { 990711ac7a9SEd Tanous continue; 991711ac7a9SEd Tanous } 9923929aca1SAnthony Wilson 9933929aca1SAnthony Wilson // The property we want to set may be nested json, so use 9943929aca1SAnthony Wilson // a json_pointer for easy indexing into the json structure. 9953929aca1SAnthony Wilson const nlohmann::json::json_pointer& key = std::get<2>(p); 9963929aca1SAnthony Wilson 997abf2add6SEd Tanous const double* doubleValue = std::get_if<double>(&valueVariant); 998*40e4f380SEd Tanous if (doubleValue == nullptr) 9991abe55efSEd Tanous { 1000*40e4f380SEd Tanous BMCWEB_LOG_ERROR << "Got value interface that wasn't double"; 10016f6d0d32SEd Tanous continue; 100208777fb0SLewanczyk, Dawid } 10036f6d0d32SEd Tanous if (forceToInt) 10046f6d0d32SEd Tanous { 1005*40e4f380SEd Tanous sensorJson[key] = static_cast<int64_t>(*doubleValue); 10066f6d0d32SEd Tanous } 10076f6d0d32SEd Tanous else 10086f6d0d32SEd Tanous { 1009*40e4f380SEd Tanous sensorJson[key] = *doubleValue; 101008777fb0SLewanczyk, Dawid } 101108777fb0SLewanczyk, Dawid } 101208777fb0SLewanczyk, Dawid } 101308777fb0SLewanczyk, Dawid } 101408777fb0SLewanczyk, Dawid 10151d7c0054SEd Tanous /** 10161d7c0054SEd Tanous * @brief Builds a json sensor representation of a sensor. 10171d7c0054SEd Tanous * @param sensorName The name of the sensor to be built 10181d7c0054SEd Tanous * @param sensorType The type (temperature, fan_tach, etc) of the sensor to 10191d7c0054SEd Tanous * build 10201d7c0054SEd Tanous * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor 10211d7c0054SEd Tanous * @param interfacesDict A dictionary of the interfaces and properties of said 10221d7c0054SEd Tanous * interfaces to be built from 10231d7c0054SEd Tanous * @param sensorJson The json object to fill 10241d7c0054SEd Tanous * @param inventoryItem D-Bus inventory item associated with the sensor. Will 10251d7c0054SEd Tanous * be nullptr if no associated inventory item was found. 10261d7c0054SEd Tanous */ 10271d7c0054SEd Tanous inline void objectInterfacesToJson( 10281d7c0054SEd Tanous const std::string& sensorName, const std::string& sensorType, 10291d7c0054SEd Tanous const std::string& chassisSubNode, 10301d7c0054SEd Tanous const dbus::utility::DBusInteracesMap& interfacesDict, 10311d7c0054SEd Tanous nlohmann::json& sensorJson, InventoryItem* inventoryItem) 10321d7c0054SEd Tanous { 10331d7c0054SEd Tanous 10341d7c0054SEd Tanous for (const auto& [interface, valuesDict] : interfacesDict) 10351d7c0054SEd Tanous { 10361d7c0054SEd Tanous objectPropertiesToJson(sensorName, sensorType, chassisSubNode, 10371d7c0054SEd Tanous valuesDict, sensorJson, inventoryItem); 10381d7c0054SEd Tanous } 1039c1d019a6SEd Tanous BMCWEB_LOG_DEBUG << "Added sensor " << sensorName; 10401d7c0054SEd Tanous } 10411d7c0054SEd Tanous 1042b5a76932SEd Tanous inline void populateFanRedundancy( 1043b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 10448bd25ccdSJames Feist { 10458bd25ccdSJames Feist crow::connections::systemBus->async_method_call( 1046b9d36b47SEd Tanous [sensorsAsyncResp]( 1047b9d36b47SEd Tanous const boost::system::error_code ec, 1048b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& resp) { 10498bd25ccdSJames Feist if (ec) 10508bd25ccdSJames Feist { 10518bd25ccdSJames Feist return; // don't have to have this interface 10528bd25ccdSJames Feist } 1053002d39b4SEd Tanous for (const std::pair< 1054002d39b4SEd Tanous std::string, 1055002d39b4SEd Tanous std::vector<std::pair<std::string, std::vector<std::string>>>>& 1056e278c18fSEd Tanous pathPair : resp) 10578bd25ccdSJames Feist { 1058e278c18fSEd Tanous const std::string& path = pathPair.first; 1059002d39b4SEd Tanous const std::vector<std::pair<std::string, std::vector<std::string>>>& 1060002d39b4SEd Tanous objDict = pathPair.second; 10618bd25ccdSJames Feist if (objDict.empty()) 10628bd25ccdSJames Feist { 10638bd25ccdSJames Feist continue; // this should be impossible 10648bd25ccdSJames Feist } 10658bd25ccdSJames Feist 10668bd25ccdSJames Feist const std::string& owner = objDict.begin()->first; 10671e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::vector<std::string>>( 10681e1e598dSJonathan Doman *crow::connections::systemBus, 10691e1e598dSJonathan Doman "xyz.openbmc_project.ObjectMapper", path + "/chassis", 10701e1e598dSJonathan Doman "xyz.openbmc_project.Association", "endpoints", 1071002d39b4SEd Tanous [path, owner, 1072002d39b4SEd Tanous sensorsAsyncResp](const boost::system::error_code e, 10731e1e598dSJonathan Doman const std::vector<std::string>& endpoints) { 1074271584abSEd Tanous if (e) 10758bd25ccdSJames Feist { 10768bd25ccdSJames Feist return; // if they don't have an association we 10778bd25ccdSJames Feist // can't tell what chassis is 10788bd25ccdSJames Feist } 1079002d39b4SEd Tanous auto found = 1080002d39b4SEd Tanous std::find_if(endpoints.begin(), endpoints.end(), 10818bd25ccdSJames Feist [sensorsAsyncResp](const std::string& entry) { 1082002d39b4SEd Tanous return entry.find(sensorsAsyncResp->chassisId) != 10838bd25ccdSJames Feist std::string::npos; 10848bd25ccdSJames Feist }); 10858bd25ccdSJames Feist 10861e1e598dSJonathan Doman if (found == endpoints.end()) 10878bd25ccdSJames Feist { 10888bd25ccdSJames Feist return; 10898bd25ccdSJames Feist } 109086d89ed7SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 109186d89ed7SKrzysztof Grobelny *crow::connections::systemBus, owner, path, 109286d89ed7SKrzysztof Grobelny "xyz.openbmc_project.Control.FanRedundancy", 10938bd25ccdSJames Feist [path, sensorsAsyncResp]( 1094271584abSEd Tanous const boost::system::error_code& err, 109586d89ed7SKrzysztof Grobelny const dbus::utility::DBusPropertiesMap& ret) { 1096271584abSEd Tanous if (err) 10978bd25ccdSJames Feist { 10988bd25ccdSJames Feist return; // don't have to have this 10998bd25ccdSJames Feist // interface 11008bd25ccdSJames Feist } 11018bd25ccdSJames Feist 110286d89ed7SKrzysztof Grobelny const uint8_t* allowedFailures = nullptr; 110386d89ed7SKrzysztof Grobelny const std::vector<std::string>* collection = nullptr; 110486d89ed7SKrzysztof Grobelny const std::string* status = nullptr; 110586d89ed7SKrzysztof Grobelny 110686d89ed7SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 110786d89ed7SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), ret, 110886d89ed7SKrzysztof Grobelny "AllowedFailures", allowedFailures, "Collection", 110986d89ed7SKrzysztof Grobelny collection, "Status", status); 111086d89ed7SKrzysztof Grobelny 111186d89ed7SKrzysztof Grobelny if (!success) 111286d89ed7SKrzysztof Grobelny { 111386d89ed7SKrzysztof Grobelny messages::internalError( 111486d89ed7SKrzysztof Grobelny sensorsAsyncResp->asyncResp->res); 111586d89ed7SKrzysztof Grobelny return; 111686d89ed7SKrzysztof Grobelny } 111786d89ed7SKrzysztof Grobelny 111886d89ed7SKrzysztof Grobelny if (allowedFailures == nullptr || collection == nullptr || 111986d89ed7SKrzysztof Grobelny status == nullptr) 11208bd25ccdSJames Feist { 1121002d39b4SEd Tanous BMCWEB_LOG_ERROR << "Invalid redundancy interface"; 11228bd25ccdSJames Feist messages::internalError( 11238d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 11248bd25ccdSJames Feist return; 11258bd25ccdSJames Feist } 11268bd25ccdSJames Feist 1127002d39b4SEd Tanous sdbusplus::message::object_path objectPath(path); 112828aa8de5SGeorge Liu std::string name = objectPath.filename(); 112928aa8de5SGeorge Liu if (name.empty()) 11308bd25ccdSJames Feist { 11318bd25ccdSJames Feist // this should be impossible 11328bd25ccdSJames Feist messages::internalError( 11338d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 11348bd25ccdSJames Feist return; 11358bd25ccdSJames Feist } 1136002d39b4SEd Tanous std::replace(name.begin(), name.end(), '_', ' '); 11378bd25ccdSJames Feist 11388bd25ccdSJames Feist std::string health; 11398bd25ccdSJames Feist 114011ba3979SEd Tanous if (status->ends_with("Full")) 11418bd25ccdSJames Feist { 11428bd25ccdSJames Feist health = "OK"; 11438bd25ccdSJames Feist } 114411ba3979SEd Tanous else if (status->ends_with("Degraded")) 11458bd25ccdSJames Feist { 11468bd25ccdSJames Feist health = "Warning"; 11478bd25ccdSJames Feist } 11488bd25ccdSJames Feist else 11498bd25ccdSJames Feist { 11508bd25ccdSJames Feist health = "Critical"; 11518bd25ccdSJames Feist } 11521476687dSEd Tanous nlohmann::json::array_t redfishCollection; 11538bd25ccdSJames Feist const auto& fanRedfish = 1154002d39b4SEd Tanous sensorsAsyncResp->asyncResp->res.jsonValue["Fans"]; 11558bd25ccdSJames Feist for (const std::string& item : *collection) 11568bd25ccdSJames Feist { 11578a592810SEd Tanous sdbusplus::message::object_path itemPath(item); 11588a592810SEd Tanous std::string itemName = itemPath.filename(); 115928aa8de5SGeorge Liu if (itemName.empty()) 116028aa8de5SGeorge Liu { 116128aa8de5SGeorge Liu continue; 116228aa8de5SGeorge Liu } 11638bd25ccdSJames Feist /* 11648bd25ccdSJames Feist todo(ed): merge patch that fixes the names 11658bd25ccdSJames Feist std::replace(itemName.begin(), 11668bd25ccdSJames Feist itemName.end(), '_', ' ');*/ 1167002d39b4SEd Tanous auto schemaItem = 1168002d39b4SEd Tanous std::find_if(fanRedfish.begin(), fanRedfish.end(), 11698bd25ccdSJames Feist [itemName](const nlohmann::json& fan) { 11708bd25ccdSJames Feist return fan["MemberId"] == itemName; 11718bd25ccdSJames Feist }); 11728bd25ccdSJames Feist if (schemaItem != fanRedfish.end()) 11738bd25ccdSJames Feist { 11748a592810SEd Tanous nlohmann::json::object_t collectionId; 11758a592810SEd Tanous collectionId["@odata.id"] = 11761476687dSEd Tanous (*schemaItem)["@odata.id"]; 11771476687dSEd Tanous redfishCollection.emplace_back( 11788a592810SEd Tanous std::move(collectionId)); 11798bd25ccdSJames Feist } 11808bd25ccdSJames Feist else 11818bd25ccdSJames Feist { 1182002d39b4SEd Tanous BMCWEB_LOG_ERROR << "failed to find fan in schema"; 11838bd25ccdSJames Feist messages::internalError( 11848d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 11858bd25ccdSJames Feist return; 11868bd25ccdSJames Feist } 11878bd25ccdSJames Feist } 11888bd25ccdSJames Feist 11893e9e72ebSKuiying Wang size_t minNumNeeded = 119026f6976fSEd Tanous collection->empty() 119126f6976fSEd Tanous ? 0 119226f6976fSEd Tanous : collection->size() - *allowedFailures; 1193002d39b4SEd Tanous nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res 11948bd25ccdSJames Feist .jsonValue["Redundancy"]; 11951476687dSEd Tanous 11961476687dSEd Tanous nlohmann::json::object_t redundancy; 11971476687dSEd Tanous redundancy["@odata.id"] = 1198002d39b4SEd Tanous "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + 1199002d39b4SEd Tanous "/" + sensorsAsyncResp->chassisSubNode + 1200002d39b4SEd Tanous "#/Redundancy/" + std::to_string(jResp.size()); 1201002d39b4SEd Tanous redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy"; 12021476687dSEd Tanous redundancy["MinNumNeeded"] = minNumNeeded; 12031476687dSEd Tanous redundancy["MemberId"] = name; 12041476687dSEd Tanous redundancy["Mode"] = "N+m"; 12051476687dSEd Tanous redundancy["Name"] = name; 12061476687dSEd Tanous redundancy["RedundancySet"] = redfishCollection; 12071476687dSEd Tanous redundancy["Status"]["Health"] = health; 12081476687dSEd Tanous redundancy["Status"]["State"] = "Enabled"; 12091476687dSEd Tanous 12101476687dSEd Tanous jResp.push_back(std::move(redundancy)); 121186d89ed7SKrzysztof Grobelny }); 12121e1e598dSJonathan Doman }); 12138bd25ccdSJames Feist } 12148bd25ccdSJames Feist }, 12158bd25ccdSJames Feist "xyz.openbmc_project.ObjectMapper", 12168bd25ccdSJames Feist "/xyz/openbmc_project/object_mapper", 12178bd25ccdSJames Feist "xyz.openbmc_project.ObjectMapper", "GetSubTree", 12188bd25ccdSJames Feist "/xyz/openbmc_project/control", 2, 12198bd25ccdSJames Feist std::array<const char*, 1>{ 12208bd25ccdSJames Feist "xyz.openbmc_project.Control.FanRedundancy"}); 12218bd25ccdSJames Feist } 12228bd25ccdSJames Feist 1223b5a76932SEd Tanous inline void 122481ce609eSEd Tanous sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 122549c53ac9SJohnathan Mantey { 12268d1b46d7Szhanghch05 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue; 122749c53ac9SJohnathan Mantey std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"}; 122881ce609eSEd Tanous if (sensorsAsyncResp->chassisSubNode == sensors::node::power) 122949c53ac9SJohnathan Mantey { 123049c53ac9SJohnathan Mantey sensorHeaders = {"Voltages", "PowerSupplies"}; 123149c53ac9SJohnathan Mantey } 123249c53ac9SJohnathan Mantey for (const std::string& sensorGroup : sensorHeaders) 123349c53ac9SJohnathan Mantey { 123449c53ac9SJohnathan Mantey nlohmann::json::iterator entry = response.find(sensorGroup); 123549c53ac9SJohnathan Mantey if (entry != response.end()) 123649c53ac9SJohnathan Mantey { 123749c53ac9SJohnathan Mantey std::sort(entry->begin(), entry->end(), 123802cad96eSEd Tanous [](const nlohmann::json& c1, const nlohmann::json& c2) { 123949c53ac9SJohnathan Mantey return c1["Name"] < c2["Name"]; 124049c53ac9SJohnathan Mantey }); 124149c53ac9SJohnathan Mantey 124249c53ac9SJohnathan Mantey // add the index counts to the end of each entry 124349c53ac9SJohnathan Mantey size_t count = 0; 124449c53ac9SJohnathan Mantey for (nlohmann::json& sensorJson : *entry) 124549c53ac9SJohnathan Mantey { 124649c53ac9SJohnathan Mantey nlohmann::json::iterator odata = sensorJson.find("@odata.id"); 124749c53ac9SJohnathan Mantey if (odata == sensorJson.end()) 124849c53ac9SJohnathan Mantey { 124949c53ac9SJohnathan Mantey continue; 125049c53ac9SJohnathan Mantey } 125149c53ac9SJohnathan Mantey std::string* value = odata->get_ptr<std::string*>(); 125249c53ac9SJohnathan Mantey if (value != nullptr) 125349c53ac9SJohnathan Mantey { 125449c53ac9SJohnathan Mantey *value += std::to_string(count); 125549c53ac9SJohnathan Mantey count++; 125681ce609eSEd Tanous sensorsAsyncResp->updateUri(sensorJson["Name"], *value); 125749c53ac9SJohnathan Mantey } 125849c53ac9SJohnathan Mantey } 125949c53ac9SJohnathan Mantey } 126049c53ac9SJohnathan Mantey } 126149c53ac9SJohnathan Mantey } 126249c53ac9SJohnathan Mantey 126308777fb0SLewanczyk, Dawid /** 1264adc4f0dbSShawn McCarney * @brief Finds the inventory item with the specified object path. 1265adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1266adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 1267adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 12688fb49dd6SShawn McCarney */ 126923a21a1cSEd Tanous inline InventoryItem* findInventoryItem( 1270b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1271adc4f0dbSShawn McCarney const std::string& invItemObjPath) 12728fb49dd6SShawn McCarney { 1273adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 12748fb49dd6SShawn McCarney { 1275adc4f0dbSShawn McCarney if (inventoryItem.objectPath == invItemObjPath) 12768fb49dd6SShawn McCarney { 1277adc4f0dbSShawn McCarney return &inventoryItem; 12788fb49dd6SShawn McCarney } 12798fb49dd6SShawn McCarney } 12808fb49dd6SShawn McCarney return nullptr; 12818fb49dd6SShawn McCarney } 12828fb49dd6SShawn McCarney 12838fb49dd6SShawn McCarney /** 1284adc4f0dbSShawn McCarney * @brief Finds the inventory item associated with the specified sensor. 1285adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1286adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor. 1287adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 12888fb49dd6SShawn McCarney */ 128923a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor( 1290b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1291adc4f0dbSShawn McCarney const std::string& sensorObjPath) 1292adc4f0dbSShawn McCarney { 1293adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 1294adc4f0dbSShawn McCarney { 1295adc4f0dbSShawn McCarney if (inventoryItem.sensors.count(sensorObjPath) > 0) 1296adc4f0dbSShawn McCarney { 1297adc4f0dbSShawn McCarney return &inventoryItem; 1298adc4f0dbSShawn McCarney } 1299adc4f0dbSShawn McCarney } 1300adc4f0dbSShawn McCarney return nullptr; 1301adc4f0dbSShawn McCarney } 1302adc4f0dbSShawn McCarney 1303adc4f0dbSShawn McCarney /** 1304d500549bSAnthony Wilson * @brief Finds the inventory item associated with the specified led path. 1305d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1306d500549bSAnthony Wilson * @param ledObjPath D-Bus object path of led. 1307d500549bSAnthony Wilson * @return Inventory item within vector, or nullptr if no match found. 1308d500549bSAnthony Wilson */ 1309d500549bSAnthony Wilson inline InventoryItem* 1310d500549bSAnthony Wilson findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems, 1311d500549bSAnthony Wilson const std::string& ledObjPath) 1312d500549bSAnthony Wilson { 1313d500549bSAnthony Wilson for (InventoryItem& inventoryItem : inventoryItems) 1314d500549bSAnthony Wilson { 1315d500549bSAnthony Wilson if (inventoryItem.ledObjectPath == ledObjPath) 1316d500549bSAnthony Wilson { 1317d500549bSAnthony Wilson return &inventoryItem; 1318d500549bSAnthony Wilson } 1319d500549bSAnthony Wilson } 1320d500549bSAnthony Wilson return nullptr; 1321d500549bSAnthony Wilson } 1322d500549bSAnthony Wilson 1323d500549bSAnthony Wilson /** 1324adc4f0dbSShawn McCarney * @brief Adds inventory item and associated sensor to specified vector. 1325adc4f0dbSShawn McCarney * 1326adc4f0dbSShawn McCarney * Adds a new InventoryItem to the vector if necessary. Searches for an 1327adc4f0dbSShawn McCarney * existing InventoryItem with the specified object path. If not found, one is 1328adc4f0dbSShawn McCarney * added to the vector. 1329adc4f0dbSShawn McCarney * 1330adc4f0dbSShawn McCarney * Next, the specified sensor is added to the set of sensors associated with the 1331adc4f0dbSShawn McCarney * InventoryItem. 1332adc4f0dbSShawn McCarney * 1333adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1334adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 1335adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor 1336adc4f0dbSShawn McCarney */ 1337b5a76932SEd Tanous inline void addInventoryItem( 1338b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1339b5a76932SEd Tanous const std::string& invItemObjPath, const std::string& sensorObjPath) 1340adc4f0dbSShawn McCarney { 1341adc4f0dbSShawn McCarney // Look for inventory item in vector 1342adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1343adc4f0dbSShawn McCarney findInventoryItem(inventoryItems, invItemObjPath); 1344adc4f0dbSShawn McCarney 1345adc4f0dbSShawn McCarney // If inventory item doesn't exist in vector, add it 1346adc4f0dbSShawn McCarney if (inventoryItem == nullptr) 1347adc4f0dbSShawn McCarney { 1348adc4f0dbSShawn McCarney inventoryItems->emplace_back(invItemObjPath); 1349adc4f0dbSShawn McCarney inventoryItem = &(inventoryItems->back()); 1350adc4f0dbSShawn McCarney } 1351adc4f0dbSShawn McCarney 1352adc4f0dbSShawn McCarney // Add sensor to set of sensors associated with inventory item 1353adc4f0dbSShawn McCarney inventoryItem->sensors.emplace(sensorObjPath); 1354adc4f0dbSShawn McCarney } 1355adc4f0dbSShawn McCarney 1356adc4f0dbSShawn McCarney /** 1357adc4f0dbSShawn McCarney * @brief Stores D-Bus data in the specified inventory item. 1358adc4f0dbSShawn McCarney * 1359adc4f0dbSShawn McCarney * Finds D-Bus data in the specified map of interfaces. Stores the data in the 1360adc4f0dbSShawn McCarney * specified InventoryItem. 1361adc4f0dbSShawn McCarney * 1362adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1363adc4f0dbSShawn McCarney * response. 1364adc4f0dbSShawn McCarney * 1365adc4f0dbSShawn McCarney * @param inventoryItem Inventory item where data will be stored. 1366adc4f0dbSShawn McCarney * @param interfacesDict Map containing D-Bus interfaces and their properties 1367adc4f0dbSShawn McCarney * for the specified inventory item. 1368adc4f0dbSShawn McCarney */ 136923a21a1cSEd Tanous inline void storeInventoryItemData( 1370adc4f0dbSShawn McCarney InventoryItem& inventoryItem, 1371711ac7a9SEd Tanous const dbus::utility::DBusInteracesMap& interfacesDict) 13728fb49dd6SShawn McCarney { 1373adc4f0dbSShawn McCarney // Get properties from Inventory.Item interface 1374711ac7a9SEd Tanous 13759eb808c1SEd Tanous for (const auto& [interface, values] : interfacesDict) 13768fb49dd6SShawn McCarney { 1377711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Item") 13788fb49dd6SShawn McCarney { 13799eb808c1SEd Tanous for (const auto& [name, dbusValue] : values) 1380711ac7a9SEd Tanous { 1381711ac7a9SEd Tanous if (name == "Present") 1382711ac7a9SEd Tanous { 1383711ac7a9SEd Tanous const bool* value = std::get_if<bool>(&dbusValue); 1384adc4f0dbSShawn McCarney if (value != nullptr) 13858fb49dd6SShawn McCarney { 1386adc4f0dbSShawn McCarney inventoryItem.isPresent = *value; 13878fb49dd6SShawn McCarney } 13888fb49dd6SShawn McCarney } 13898fb49dd6SShawn McCarney } 1390711ac7a9SEd Tanous } 1391adc4f0dbSShawn McCarney // Check if Inventory.Item.PowerSupply interface is present 1392711ac7a9SEd Tanous 1393711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply") 13948fb49dd6SShawn McCarney { 1395adc4f0dbSShawn McCarney inventoryItem.isPowerSupply = true; 13968fb49dd6SShawn McCarney } 1397adc4f0dbSShawn McCarney 1398adc4f0dbSShawn McCarney // Get properties from Inventory.Decorator.Asset interface 1399711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset") 1400adc4f0dbSShawn McCarney { 14019eb808c1SEd Tanous for (const auto& [name, dbusValue] : values) 1402711ac7a9SEd Tanous { 1403711ac7a9SEd Tanous if (name == "Manufacturer") 1404adc4f0dbSShawn McCarney { 1405adc4f0dbSShawn McCarney const std::string* value = 1406711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1407adc4f0dbSShawn McCarney if (value != nullptr) 1408adc4f0dbSShawn McCarney { 1409adc4f0dbSShawn McCarney inventoryItem.manufacturer = *value; 1410adc4f0dbSShawn McCarney } 1411adc4f0dbSShawn McCarney } 1412711ac7a9SEd Tanous if (name == "Model") 1413adc4f0dbSShawn McCarney { 1414adc4f0dbSShawn McCarney const std::string* value = 1415711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1416adc4f0dbSShawn McCarney if (value != nullptr) 1417adc4f0dbSShawn McCarney { 1418adc4f0dbSShawn McCarney inventoryItem.model = *value; 1419adc4f0dbSShawn McCarney } 1420adc4f0dbSShawn McCarney } 1421711ac7a9SEd Tanous if (name == "SerialNumber") 1422adc4f0dbSShawn McCarney { 1423adc4f0dbSShawn McCarney const std::string* value = 1424711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1425adc4f0dbSShawn McCarney if (value != nullptr) 1426adc4f0dbSShawn McCarney { 1427adc4f0dbSShawn McCarney inventoryItem.serialNumber = *value; 1428adc4f0dbSShawn McCarney } 1429adc4f0dbSShawn McCarney } 1430711ac7a9SEd Tanous if (name == "PartNumber") 1431711ac7a9SEd Tanous { 1432711ac7a9SEd Tanous const std::string* value = 1433711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1434711ac7a9SEd Tanous if (value != nullptr) 1435711ac7a9SEd Tanous { 1436711ac7a9SEd Tanous inventoryItem.partNumber = *value; 1437711ac7a9SEd Tanous } 1438711ac7a9SEd Tanous } 1439711ac7a9SEd Tanous } 1440adc4f0dbSShawn McCarney } 1441adc4f0dbSShawn McCarney 1442711ac7a9SEd Tanous if (interface == 1443711ac7a9SEd Tanous "xyz.openbmc_project.State.Decorator.OperationalStatus") 1444adc4f0dbSShawn McCarney { 14459eb808c1SEd Tanous for (const auto& [name, dbusValue] : values) 1446adc4f0dbSShawn McCarney { 1447711ac7a9SEd Tanous if (name == "Functional") 1448711ac7a9SEd Tanous { 1449711ac7a9SEd Tanous const bool* value = std::get_if<bool>(&dbusValue); 1450adc4f0dbSShawn McCarney if (value != nullptr) 1451adc4f0dbSShawn McCarney { 1452adc4f0dbSShawn McCarney inventoryItem.isFunctional = *value; 14538fb49dd6SShawn McCarney } 14548fb49dd6SShawn McCarney } 14558fb49dd6SShawn McCarney } 14568fb49dd6SShawn McCarney } 1457711ac7a9SEd Tanous } 1458711ac7a9SEd Tanous } 14598fb49dd6SShawn McCarney 14608fb49dd6SShawn McCarney /** 1461adc4f0dbSShawn McCarney * @brief Gets D-Bus data for inventory items associated with sensors. 14628fb49dd6SShawn McCarney * 1463adc4f0dbSShawn McCarney * Uses the specified connections (services) to obtain D-Bus data for inventory 1464adc4f0dbSShawn McCarney * items associated with sensors. Stores the resulting data in the 1465adc4f0dbSShawn McCarney * inventoryItems vector. 14668fb49dd6SShawn McCarney * 1467adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1468adc4f0dbSShawn McCarney * response. 1469adc4f0dbSShawn McCarney * 1470adc4f0dbSShawn McCarney * Finds the inventory item data asynchronously. Invokes callback when data has 1471adc4f0dbSShawn McCarney * been obtained. 1472adc4f0dbSShawn McCarney * 1473adc4f0dbSShawn McCarney * The callback must have the following signature: 1474adc4f0dbSShawn McCarney * @code 1475d500549bSAnthony Wilson * callback(void) 1476adc4f0dbSShawn McCarney * @endcode 1477adc4f0dbSShawn McCarney * 1478adc4f0dbSShawn McCarney * This function is called recursively, obtaining data asynchronously from one 1479adc4f0dbSShawn McCarney * connection in each call. This ensures the callback is not invoked until the 1480adc4f0dbSShawn McCarney * last asynchronous function has completed. 14818fb49dd6SShawn McCarney * 14828fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1483adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1484adc4f0dbSShawn McCarney * @param invConnections Connections that provide data for the inventory items. 14858fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 14868fb49dd6SShawn McCarney * implements ObjectManager. 1487adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory data has been obtained. 1488adc4f0dbSShawn McCarney * @param invConnectionsIndex Current index in invConnections. Only specified 1489adc4f0dbSShawn McCarney * in recursive calls to this function. 14908fb49dd6SShawn McCarney */ 1491adc4f0dbSShawn McCarney template <typename Callback> 1492adc4f0dbSShawn McCarney static void getInventoryItemsData( 14938fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1494adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1495fe04d49cSNan Zhou std::shared_ptr<std::set<std::string>> invConnections, 1496fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths, 1497271584abSEd Tanous Callback&& callback, size_t invConnectionsIndex = 0) 14988fb49dd6SShawn McCarney { 1499adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData enter"; 15008fb49dd6SShawn McCarney 1501adc4f0dbSShawn McCarney // If no more connections left, call callback 1502adc4f0dbSShawn McCarney if (invConnectionsIndex >= invConnections->size()) 15038fb49dd6SShawn McCarney { 1504d500549bSAnthony Wilson callback(); 1505adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData exit"; 1506adc4f0dbSShawn McCarney return; 1507adc4f0dbSShawn McCarney } 1508adc4f0dbSShawn McCarney 1509adc4f0dbSShawn McCarney // Get inventory item data from current connection 1510fe04d49cSNan Zhou auto it = invConnections->begin(); 1511fe04d49cSNan Zhou std::advance(it, invConnectionsIndex); 1512adc4f0dbSShawn McCarney if (it != invConnections->end()) 1513adc4f0dbSShawn McCarney { 1514adc4f0dbSShawn McCarney const std::string& invConnection = *it; 1515adc4f0dbSShawn McCarney 15168fb49dd6SShawn McCarney // Response handler for GetManagedObjects 1517002d39b4SEd Tanous auto respHandler = 1518002d39b4SEd Tanous [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths, 151902cad96eSEd Tanous callback{std::forward<Callback>(callback)}, invConnectionsIndex]( 152002cad96eSEd Tanous const boost::system::error_code ec, 152102cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 1522adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter"; 15238fb49dd6SShawn McCarney if (ec) 15248fb49dd6SShawn McCarney { 15258fb49dd6SShawn McCarney BMCWEB_LOG_ERROR 1526adc4f0dbSShawn McCarney << "getInventoryItemsData respHandler DBus error " << ec; 15278d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 15288fb49dd6SShawn McCarney return; 15298fb49dd6SShawn McCarney } 15308fb49dd6SShawn McCarney 15318fb49dd6SShawn McCarney // Loop through returned object paths 15328fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 15338fb49dd6SShawn McCarney { 15348fb49dd6SShawn McCarney const std::string& objPath = 15358fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 15368fb49dd6SShawn McCarney 1537adc4f0dbSShawn McCarney // If this object path is one of the specified inventory items 1538adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1539adc4f0dbSShawn McCarney findInventoryItem(inventoryItems, objPath); 1540adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 15418fb49dd6SShawn McCarney { 1542adc4f0dbSShawn McCarney // Store inventory data in InventoryItem 1543adc4f0dbSShawn McCarney storeInventoryItemData(*inventoryItem, objDictEntry.second); 15448fb49dd6SShawn McCarney } 15458fb49dd6SShawn McCarney } 15468fb49dd6SShawn McCarney 1547adc4f0dbSShawn McCarney // Recurse to get inventory item data from next connection 1548adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 1549adc4f0dbSShawn McCarney invConnections, objectMgrPaths, 1550adc4f0dbSShawn McCarney std::move(callback), invConnectionsIndex + 1); 1551adc4f0dbSShawn McCarney 1552adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit"; 15538fb49dd6SShawn McCarney }; 15548fb49dd6SShawn McCarney 15558fb49dd6SShawn McCarney // Find DBus object path that implements ObjectManager for the current 15568fb49dd6SShawn McCarney // connection. If no mapping found, default to "/". 15578fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(invConnection); 15588fb49dd6SShawn McCarney const std::string& objectMgrPath = 15598fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 15608fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is " 15618fb49dd6SShawn McCarney << objectMgrPath; 15628fb49dd6SShawn McCarney 15638fb49dd6SShawn McCarney // Get all object paths and their interfaces for current connection 15648fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 15658fb49dd6SShawn McCarney std::move(respHandler), invConnection, objectMgrPath, 15668fb49dd6SShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 15678fb49dd6SShawn McCarney } 15688fb49dd6SShawn McCarney 1569adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData exit"; 15708fb49dd6SShawn McCarney } 15718fb49dd6SShawn McCarney 15728fb49dd6SShawn McCarney /** 1573adc4f0dbSShawn McCarney * @brief Gets connections that provide D-Bus data for inventory items. 15748fb49dd6SShawn McCarney * 1575adc4f0dbSShawn McCarney * Gets the D-Bus connections (services) that provide data for the inventory 1576adc4f0dbSShawn McCarney * items that are associated with sensors. 15778fb49dd6SShawn McCarney * 15788fb49dd6SShawn McCarney * Finds the connections asynchronously. Invokes callback when information has 15798fb49dd6SShawn McCarney * been obtained. 15808fb49dd6SShawn McCarney * 15818fb49dd6SShawn McCarney * The callback must have the following signature: 15828fb49dd6SShawn McCarney * @code 1583fe04d49cSNan Zhou * callback(std::shared_ptr<std::set<std::string>> invConnections) 15848fb49dd6SShawn McCarney * @endcode 15858fb49dd6SShawn McCarney * 15868fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1587adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 15888fb49dd6SShawn McCarney * @param callback Callback to invoke when connections have been obtained. 15898fb49dd6SShawn McCarney */ 15908fb49dd6SShawn McCarney template <typename Callback> 15918fb49dd6SShawn McCarney static void getInventoryItemsConnections( 1592b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1593b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 15948fb49dd6SShawn McCarney Callback&& callback) 15958fb49dd6SShawn McCarney { 15968fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter"; 15978fb49dd6SShawn McCarney 15988fb49dd6SShawn McCarney const std::string path = "/xyz/openbmc_project/inventory"; 1599adc4f0dbSShawn McCarney const std::array<std::string, 4> interfaces = { 16008fb49dd6SShawn McCarney "xyz.openbmc_project.Inventory.Item", 1601adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.PowerSupply", 1602adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Decorator.Asset", 16038fb49dd6SShawn McCarney "xyz.openbmc_project.State.Decorator.OperationalStatus"}; 16048fb49dd6SShawn McCarney 16058fb49dd6SShawn McCarney // Response handler for parsing output from GetSubTree 1606002d39b4SEd Tanous auto respHandler = 1607002d39b4SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 1608002d39b4SEd Tanous inventoryItems]( 1609b9d36b47SEd Tanous const boost::system::error_code ec, 1610002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 16118fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter"; 16128fb49dd6SShawn McCarney if (ec) 16138fb49dd6SShawn McCarney { 16148d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 16158fb49dd6SShawn McCarney BMCWEB_LOG_ERROR 16168fb49dd6SShawn McCarney << "getInventoryItemsConnections respHandler DBus error " << ec; 16178fb49dd6SShawn McCarney return; 16188fb49dd6SShawn McCarney } 16198fb49dd6SShawn McCarney 16208fb49dd6SShawn McCarney // Make unique list of connections for desired inventory items 1621fe04d49cSNan Zhou std::shared_ptr<std::set<std::string>> invConnections = 1622fe04d49cSNan Zhou std::make_shared<std::set<std::string>>(); 16238fb49dd6SShawn McCarney 16248fb49dd6SShawn McCarney // Loop through objects from GetSubTree 16258fb49dd6SShawn McCarney for (const std::pair< 16268fb49dd6SShawn McCarney std::string, 16278fb49dd6SShawn McCarney std::vector<std::pair<std::string, std::vector<std::string>>>>& 16288fb49dd6SShawn McCarney object : subtree) 16298fb49dd6SShawn McCarney { 1630adc4f0dbSShawn McCarney // Check if object path is one of the specified inventory items 16318fb49dd6SShawn McCarney const std::string& objPath = object.first; 1632adc4f0dbSShawn McCarney if (findInventoryItem(inventoryItems, objPath) != nullptr) 16338fb49dd6SShawn McCarney { 16348fb49dd6SShawn McCarney // Store all connections to inventory item 16358fb49dd6SShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 16368fb49dd6SShawn McCarney objData : object.second) 16378fb49dd6SShawn McCarney { 16388fb49dd6SShawn McCarney const std::string& invConnection = objData.first; 16398fb49dd6SShawn McCarney invConnections->insert(invConnection); 16408fb49dd6SShawn McCarney } 16418fb49dd6SShawn McCarney } 16428fb49dd6SShawn McCarney } 1643d500549bSAnthony Wilson 16448fb49dd6SShawn McCarney callback(invConnections); 16458fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit"; 16468fb49dd6SShawn McCarney }; 16478fb49dd6SShawn McCarney 16488fb49dd6SShawn McCarney // Make call to ObjectMapper to find all inventory items 16498fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 16508fb49dd6SShawn McCarney std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 16518fb49dd6SShawn McCarney "/xyz/openbmc_project/object_mapper", 16528fb49dd6SShawn McCarney "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); 16538fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit"; 16548fb49dd6SShawn McCarney } 16558fb49dd6SShawn McCarney 16568fb49dd6SShawn McCarney /** 1657adc4f0dbSShawn McCarney * @brief Gets associations from sensors to inventory items. 16588fb49dd6SShawn McCarney * 16598fb49dd6SShawn McCarney * Looks for ObjectMapper associations from the specified sensors to related 1660d500549bSAnthony Wilson * inventory items. Then finds the associations from those inventory items to 1661d500549bSAnthony Wilson * their LEDs, if any. 16628fb49dd6SShawn McCarney * 16638fb49dd6SShawn McCarney * Finds the inventory items asynchronously. Invokes callback when information 16648fb49dd6SShawn McCarney * has been obtained. 16658fb49dd6SShawn McCarney * 16668fb49dd6SShawn McCarney * The callback must have the following signature: 16678fb49dd6SShawn McCarney * @code 1668adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 16698fb49dd6SShawn McCarney * @endcode 16708fb49dd6SShawn McCarney * 16718fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 16728fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 16738fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 16748fb49dd6SShawn McCarney * implements ObjectManager. 16758fb49dd6SShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 16768fb49dd6SShawn McCarney */ 16778fb49dd6SShawn McCarney template <typename Callback> 1678adc4f0dbSShawn McCarney static void getInventoryItemAssociations( 1679b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1680fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames, 1681fe04d49cSNan Zhou const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths, 16828fb49dd6SShawn McCarney Callback&& callback) 16838fb49dd6SShawn McCarney { 1684adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter"; 16858fb49dd6SShawn McCarney 16868fb49dd6SShawn McCarney // Response handler for GetManagedObjects 168702cad96eSEd Tanous auto respHandler = 168802cad96eSEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 16898fb49dd6SShawn McCarney sensorNames](const boost::system::error_code ec, 169002cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 1691adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter"; 16928fb49dd6SShawn McCarney if (ec) 16938fb49dd6SShawn McCarney { 1694adc4f0dbSShawn McCarney BMCWEB_LOG_ERROR 1695adc4f0dbSShawn McCarney << "getInventoryItemAssociations respHandler DBus error " << ec; 16968d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 16978fb49dd6SShawn McCarney return; 16988fb49dd6SShawn McCarney } 16998fb49dd6SShawn McCarney 1700adc4f0dbSShawn McCarney // Create vector to hold list of inventory items 1701adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems = 1702adc4f0dbSShawn McCarney std::make_shared<std::vector<InventoryItem>>(); 1703adc4f0dbSShawn McCarney 17048fb49dd6SShawn McCarney // Loop through returned object paths 17058fb49dd6SShawn McCarney std::string sensorAssocPath; 17068fb49dd6SShawn McCarney sensorAssocPath.reserve(128); // avoid memory allocations 17078fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 17088fb49dd6SShawn McCarney { 17098fb49dd6SShawn McCarney const std::string& objPath = 17108fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 17118fb49dd6SShawn McCarney 17128fb49dd6SShawn McCarney // If path is inventory association for one of the specified sensors 17138fb49dd6SShawn McCarney for (const std::string& sensorName : *sensorNames) 17148fb49dd6SShawn McCarney { 17158fb49dd6SShawn McCarney sensorAssocPath = sensorName; 17168fb49dd6SShawn McCarney sensorAssocPath += "/inventory"; 17178fb49dd6SShawn McCarney if (objPath == sensorAssocPath) 17188fb49dd6SShawn McCarney { 17198fb49dd6SShawn McCarney // Get Association interface for object path 1720711ac7a9SEd Tanous for (const auto& [interface, values] : objDictEntry.second) 17218fb49dd6SShawn McCarney { 1722711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Association") 1723711ac7a9SEd Tanous { 1724711ac7a9SEd Tanous for (const auto& [valueName, value] : values) 1725711ac7a9SEd Tanous { 1726711ac7a9SEd Tanous if (valueName == "endpoints") 17278fb49dd6SShawn McCarney { 17288fb49dd6SShawn McCarney const std::vector<std::string>* endpoints = 17298fb49dd6SShawn McCarney std::get_if<std::vector<std::string>>( 1730711ac7a9SEd Tanous &value); 1731711ac7a9SEd Tanous if ((endpoints != nullptr) && 1732711ac7a9SEd Tanous !endpoints->empty()) 17338fb49dd6SShawn McCarney { 1734adc4f0dbSShawn McCarney // Add inventory item to vector 1735adc4f0dbSShawn McCarney const std::string& invItemPath = 1736adc4f0dbSShawn McCarney endpoints->front(); 1737711ac7a9SEd Tanous addInventoryItem(inventoryItems, 1738711ac7a9SEd Tanous invItemPath, 1739adc4f0dbSShawn McCarney sensorName); 17408fb49dd6SShawn McCarney } 17418fb49dd6SShawn McCarney } 17428fb49dd6SShawn McCarney } 1743711ac7a9SEd Tanous } 1744711ac7a9SEd Tanous } 17458fb49dd6SShawn McCarney break; 17468fb49dd6SShawn McCarney } 17478fb49dd6SShawn McCarney } 17488fb49dd6SShawn McCarney } 17498fb49dd6SShawn McCarney 1750d500549bSAnthony Wilson // Now loop through the returned object paths again, this time to 1751d500549bSAnthony Wilson // find the leds associated with the inventory items we just found 1752d500549bSAnthony Wilson std::string inventoryAssocPath; 1753d500549bSAnthony Wilson inventoryAssocPath.reserve(128); // avoid memory allocations 1754d500549bSAnthony Wilson for (const auto& objDictEntry : resp) 1755d500549bSAnthony Wilson { 1756d500549bSAnthony Wilson const std::string& objPath = 1757d500549bSAnthony Wilson static_cast<const std::string&>(objDictEntry.first); 1758d500549bSAnthony Wilson 1759d500549bSAnthony Wilson for (InventoryItem& inventoryItem : *inventoryItems) 1760d500549bSAnthony Wilson { 1761d500549bSAnthony Wilson inventoryAssocPath = inventoryItem.objectPath; 1762d500549bSAnthony Wilson inventoryAssocPath += "/leds"; 1763d500549bSAnthony Wilson if (objPath == inventoryAssocPath) 1764d500549bSAnthony Wilson { 1765711ac7a9SEd Tanous for (const auto& [interface, values] : objDictEntry.second) 1766d500549bSAnthony Wilson { 1767711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Association") 1768711ac7a9SEd Tanous { 1769711ac7a9SEd Tanous for (const auto& [valueName, value] : values) 1770711ac7a9SEd Tanous { 1771711ac7a9SEd Tanous if (valueName == "endpoints") 1772d500549bSAnthony Wilson { 1773d500549bSAnthony Wilson const std::vector<std::string>* endpoints = 1774d500549bSAnthony Wilson std::get_if<std::vector<std::string>>( 1775711ac7a9SEd Tanous &value); 1776711ac7a9SEd Tanous if ((endpoints != nullptr) && 1777711ac7a9SEd Tanous !endpoints->empty()) 1778d500549bSAnthony Wilson { 1779711ac7a9SEd Tanous // Add inventory item to vector 1780d500549bSAnthony Wilson // Store LED path in inventory item 1781711ac7a9SEd Tanous const std::string& ledPath = 1782711ac7a9SEd Tanous endpoints->front(); 1783d500549bSAnthony Wilson inventoryItem.ledObjectPath = ledPath; 1784d500549bSAnthony Wilson } 1785d500549bSAnthony Wilson } 1786d500549bSAnthony Wilson } 1787711ac7a9SEd Tanous } 1788711ac7a9SEd Tanous } 1789711ac7a9SEd Tanous 1790d500549bSAnthony Wilson break; 1791d500549bSAnthony Wilson } 1792d500549bSAnthony Wilson } 1793d500549bSAnthony Wilson } 1794adc4f0dbSShawn McCarney callback(inventoryItems); 1795adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit"; 17968fb49dd6SShawn McCarney }; 17978fb49dd6SShawn McCarney 17988fb49dd6SShawn McCarney // Find DBus object path that implements ObjectManager for ObjectMapper 17998fb49dd6SShawn McCarney std::string connection = "xyz.openbmc_project.ObjectMapper"; 18008fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(connection); 18018fb49dd6SShawn McCarney const std::string& objectMgrPath = 18028fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 18038fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is " 18048fb49dd6SShawn McCarney << objectMgrPath; 18058fb49dd6SShawn McCarney 18068fb49dd6SShawn McCarney // Call GetManagedObjects on the ObjectMapper to get all associations 18078fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 18088fb49dd6SShawn McCarney std::move(respHandler), connection, objectMgrPath, 18098fb49dd6SShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 18108fb49dd6SShawn McCarney 1811adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit"; 18128fb49dd6SShawn McCarney } 18138fb49dd6SShawn McCarney 18148fb49dd6SShawn McCarney /** 1815d500549bSAnthony Wilson * @brief Gets D-Bus data for inventory item leds associated with sensors. 1816d500549bSAnthony Wilson * 1817d500549bSAnthony Wilson * Uses the specified connections (services) to obtain D-Bus data for inventory 1818d500549bSAnthony Wilson * item leds associated with sensors. Stores the resulting data in the 1819d500549bSAnthony Wilson * inventoryItems vector. 1820d500549bSAnthony Wilson * 1821d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 1822d500549bSAnthony Wilson * response. 1823d500549bSAnthony Wilson * 1824d500549bSAnthony Wilson * Finds the inventory item led data asynchronously. Invokes callback when data 1825d500549bSAnthony Wilson * has been obtained. 1826d500549bSAnthony Wilson * 1827d500549bSAnthony Wilson * The callback must have the following signature: 1828d500549bSAnthony Wilson * @code 182942cbe538SGunnar Mills * callback() 1830d500549bSAnthony Wilson * @endcode 1831d500549bSAnthony Wilson * 1832d500549bSAnthony Wilson * This function is called recursively, obtaining data asynchronously from one 1833d500549bSAnthony Wilson * connection in each call. This ensures the callback is not invoked until the 1834d500549bSAnthony Wilson * last asynchronous function has completed. 1835d500549bSAnthony Wilson * 1836d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 1837d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1838d500549bSAnthony Wilson * @param ledConnections Connections that provide data for the inventory leds. 1839d500549bSAnthony Wilson * @param callback Callback to invoke when inventory data has been obtained. 1840d500549bSAnthony Wilson * @param ledConnectionsIndex Current index in ledConnections. Only specified 1841d500549bSAnthony Wilson * in recursive calls to this function. 1842d500549bSAnthony Wilson */ 1843d500549bSAnthony Wilson template <typename Callback> 1844d500549bSAnthony Wilson void getInventoryLedData( 1845d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1846d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1847fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> ledConnections, 1848d500549bSAnthony Wilson Callback&& callback, size_t ledConnectionsIndex = 0) 1849d500549bSAnthony Wilson { 1850d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData enter"; 1851d500549bSAnthony Wilson 1852d500549bSAnthony Wilson // If no more connections left, call callback 1853d500549bSAnthony Wilson if (ledConnectionsIndex >= ledConnections->size()) 1854d500549bSAnthony Wilson { 185542cbe538SGunnar Mills callback(); 1856d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData exit"; 1857d500549bSAnthony Wilson return; 1858d500549bSAnthony Wilson } 1859d500549bSAnthony Wilson 1860d500549bSAnthony Wilson // Get inventory item data from current connection 1861fe04d49cSNan Zhou auto it = ledConnections->begin(); 1862fe04d49cSNan Zhou std::advance(it, ledConnectionsIndex); 1863d500549bSAnthony Wilson if (it != ledConnections->end()) 1864d500549bSAnthony Wilson { 1865d500549bSAnthony Wilson const std::string& ledPath = (*it).first; 1866d500549bSAnthony Wilson const std::string& ledConnection = (*it).second; 1867d500549bSAnthony Wilson // Response handler for Get State property 18681e1e598dSJonathan Doman auto respHandler = 18691e1e598dSJonathan Doman [sensorsAsyncResp, inventoryItems, ledConnections, ledPath, 1870f94c4ecfSEd Tanous callback{std::forward<Callback>(callback)}, ledConnectionsIndex]( 18711e1e598dSJonathan Doman const boost::system::error_code ec, const std::string& state) { 1872d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter"; 1873d500549bSAnthony Wilson if (ec) 1874d500549bSAnthony Wilson { 1875d500549bSAnthony Wilson BMCWEB_LOG_ERROR 1876d500549bSAnthony Wilson << "getInventoryLedData respHandler DBus error " << ec; 18778d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 1878d500549bSAnthony Wilson return; 1879d500549bSAnthony Wilson } 1880d500549bSAnthony Wilson 18811e1e598dSJonathan Doman BMCWEB_LOG_DEBUG << "Led state: " << state; 1882d500549bSAnthony Wilson // Find inventory item with this LED object path 1883d500549bSAnthony Wilson InventoryItem* inventoryItem = 1884d500549bSAnthony Wilson findInventoryItemForLed(*inventoryItems, ledPath); 1885d500549bSAnthony Wilson if (inventoryItem != nullptr) 1886d500549bSAnthony Wilson { 1887d500549bSAnthony Wilson // Store LED state in InventoryItem 188811ba3979SEd Tanous if (state.ends_with("On")) 1889d500549bSAnthony Wilson { 1890d500549bSAnthony Wilson inventoryItem->ledState = LedState::ON; 1891d500549bSAnthony Wilson } 189211ba3979SEd Tanous else if (state.ends_with("Blink")) 1893d500549bSAnthony Wilson { 1894d500549bSAnthony Wilson inventoryItem->ledState = LedState::BLINK; 1895d500549bSAnthony Wilson } 189611ba3979SEd Tanous else if (state.ends_with("Off")) 1897d500549bSAnthony Wilson { 1898d500549bSAnthony Wilson inventoryItem->ledState = LedState::OFF; 1899d500549bSAnthony Wilson } 1900d500549bSAnthony Wilson else 1901d500549bSAnthony Wilson { 1902d500549bSAnthony Wilson inventoryItem->ledState = LedState::UNKNOWN; 1903d500549bSAnthony Wilson } 1904d500549bSAnthony Wilson } 1905d500549bSAnthony Wilson 1906d500549bSAnthony Wilson // Recurse to get LED data from next connection 1907d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, 1908d500549bSAnthony Wilson ledConnections, std::move(callback), 1909d500549bSAnthony Wilson ledConnectionsIndex + 1); 1910d500549bSAnthony Wilson 1911d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit"; 1912d500549bSAnthony Wilson }; 1913d500549bSAnthony Wilson 1914d500549bSAnthony Wilson // Get the State property for the current LED 19151e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 19161e1e598dSJonathan Doman *crow::connections::systemBus, ledConnection, ledPath, 19171e1e598dSJonathan Doman "xyz.openbmc_project.Led.Physical", "State", 19181e1e598dSJonathan Doman std::move(respHandler)); 1919d500549bSAnthony Wilson } 1920d500549bSAnthony Wilson 1921d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData exit"; 1922d500549bSAnthony Wilson } 1923d500549bSAnthony Wilson 1924d500549bSAnthony Wilson /** 1925d500549bSAnthony Wilson * @brief Gets LED data for LEDs associated with given inventory items. 1926d500549bSAnthony Wilson * 1927d500549bSAnthony Wilson * Gets the D-Bus connections (services) that provide LED data for the LEDs 1928d500549bSAnthony Wilson * associated with the specified inventory items. Then gets the LED data from 1929d500549bSAnthony Wilson * each connection and stores it in the inventory item. 1930d500549bSAnthony Wilson * 1931d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 1932d500549bSAnthony Wilson * response. 1933d500549bSAnthony Wilson * 1934d500549bSAnthony Wilson * Finds the LED data asynchronously. Invokes callback when information has 1935d500549bSAnthony Wilson * been obtained. 1936d500549bSAnthony Wilson * 1937d500549bSAnthony Wilson * The callback must have the following signature: 1938d500549bSAnthony Wilson * @code 193942cbe538SGunnar Mills * callback() 1940d500549bSAnthony Wilson * @endcode 1941d500549bSAnthony Wilson * 1942d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 1943d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1944d500549bSAnthony Wilson * @param callback Callback to invoke when inventory items have been obtained. 1945d500549bSAnthony Wilson */ 1946d500549bSAnthony Wilson template <typename Callback> 1947d500549bSAnthony Wilson void getInventoryLeds( 1948d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1949d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1950d500549bSAnthony Wilson Callback&& callback) 1951d500549bSAnthony Wilson { 1952d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds enter"; 1953d500549bSAnthony Wilson 1954d500549bSAnthony Wilson const std::string path = "/xyz/openbmc_project"; 1955d500549bSAnthony Wilson const std::array<std::string, 1> interfaces = { 1956d500549bSAnthony Wilson "xyz.openbmc_project.Led.Physical"}; 1957d500549bSAnthony Wilson 1958d500549bSAnthony Wilson // Response handler for parsing output from GetSubTree 1959002d39b4SEd Tanous auto respHandler = 1960002d39b4SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 1961002d39b4SEd Tanous inventoryItems]( 1962b9d36b47SEd Tanous const boost::system::error_code ec, 1963002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 1964d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter"; 1965d500549bSAnthony Wilson if (ec) 1966d500549bSAnthony Wilson { 19678d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 1968d500549bSAnthony Wilson BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error " 1969d500549bSAnthony Wilson << ec; 1970d500549bSAnthony Wilson return; 1971d500549bSAnthony Wilson } 1972d500549bSAnthony Wilson 1973d500549bSAnthony Wilson // Build map of LED object paths to connections 1974fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> ledConnections = 1975fe04d49cSNan Zhou std::make_shared<std::map<std::string, std::string>>(); 1976d500549bSAnthony Wilson 1977d500549bSAnthony Wilson // Loop through objects from GetSubTree 1978d500549bSAnthony Wilson for (const std::pair< 1979d500549bSAnthony Wilson std::string, 1980d500549bSAnthony Wilson std::vector<std::pair<std::string, std::vector<std::string>>>>& 1981d500549bSAnthony Wilson object : subtree) 1982d500549bSAnthony Wilson { 1983d500549bSAnthony Wilson // Check if object path is LED for one of the specified inventory 1984d500549bSAnthony Wilson // items 1985d500549bSAnthony Wilson const std::string& ledPath = object.first; 1986d500549bSAnthony Wilson if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr) 1987d500549bSAnthony Wilson { 1988d500549bSAnthony Wilson // Add mapping from ledPath to connection 1989d500549bSAnthony Wilson const std::string& connection = object.second.begin()->first; 1990d500549bSAnthony Wilson (*ledConnections)[ledPath] = connection; 1991d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> " 1992d500549bSAnthony Wilson << connection; 1993d500549bSAnthony Wilson } 1994d500549bSAnthony Wilson } 1995d500549bSAnthony Wilson 1996d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections, 1997d500549bSAnthony Wilson std::move(callback)); 1998d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit"; 1999d500549bSAnthony Wilson }; 2000d500549bSAnthony Wilson // Make call to ObjectMapper to find all inventory items 2001d500549bSAnthony Wilson crow::connections::systemBus->async_method_call( 2002d500549bSAnthony Wilson std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 2003d500549bSAnthony Wilson "/xyz/openbmc_project/object_mapper", 2004d500549bSAnthony Wilson "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); 2005d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds exit"; 2006d500549bSAnthony Wilson } 2007d500549bSAnthony Wilson 2008d500549bSAnthony Wilson /** 200942cbe538SGunnar Mills * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent 201042cbe538SGunnar Mills * 201142cbe538SGunnar Mills * Uses the specified connections (services) (currently assumes just one) to 201242cbe538SGunnar Mills * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in 201342cbe538SGunnar Mills * the inventoryItems vector. Only stores data in Power Supply inventoryItems. 201442cbe538SGunnar Mills * 201542cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 201642cbe538SGunnar Mills * response. 201742cbe538SGunnar Mills * 201842cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 201942cbe538SGunnar Mills * when data has been obtained. 202042cbe538SGunnar Mills * 202142cbe538SGunnar Mills * The callback must have the following signature: 202242cbe538SGunnar Mills * @code 202342cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 202442cbe538SGunnar Mills * @endcode 202542cbe538SGunnar Mills * 202642cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 202742cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 202842cbe538SGunnar Mills * @param psAttributesConnections Connections that provide data for the Power 202942cbe538SGunnar Mills * Supply Attributes 203042cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 203142cbe538SGunnar Mills */ 203242cbe538SGunnar Mills template <typename Callback> 203342cbe538SGunnar Mills void getPowerSupplyAttributesData( 2034b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 203542cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 2036fe04d49cSNan Zhou const std::map<std::string, std::string>& psAttributesConnections, 203742cbe538SGunnar Mills Callback&& callback) 203842cbe538SGunnar Mills { 203942cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter"; 204042cbe538SGunnar Mills 204142cbe538SGunnar Mills if (psAttributesConnections.empty()) 204242cbe538SGunnar Mills { 204342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!"; 204442cbe538SGunnar Mills callback(inventoryItems); 204542cbe538SGunnar Mills return; 204642cbe538SGunnar Mills } 204742cbe538SGunnar Mills 204842cbe538SGunnar Mills // Assuming just one connection (service) for now 2049fe04d49cSNan Zhou auto it = psAttributesConnections.begin(); 205042cbe538SGunnar Mills 205142cbe538SGunnar Mills const std::string& psAttributesPath = (*it).first; 205242cbe538SGunnar Mills const std::string& psAttributesConnection = (*it).second; 205342cbe538SGunnar Mills 205442cbe538SGunnar Mills // Response handler for Get DeratingFactor property 2055002d39b4SEd Tanous auto respHandler = 2056002d39b4SEd Tanous [sensorsAsyncResp, inventoryItems, 2057f94c4ecfSEd Tanous callback{std::forward<Callback>(callback)}]( 2058002d39b4SEd Tanous const boost::system::error_code ec, const uint32_t value) { 205942cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter"; 206042cbe538SGunnar Mills if (ec) 206142cbe538SGunnar Mills { 206242cbe538SGunnar Mills BMCWEB_LOG_ERROR 206342cbe538SGunnar Mills << "getPowerSupplyAttributesData respHandler DBus error " << ec; 20648d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 206542cbe538SGunnar Mills return; 206642cbe538SGunnar Mills } 206742cbe538SGunnar Mills 20681e1e598dSJonathan Doman BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value; 206942cbe538SGunnar Mills // Store value in Power Supply Inventory Items 207042cbe538SGunnar Mills for (InventoryItem& inventoryItem : *inventoryItems) 207142cbe538SGunnar Mills { 207255f79e6fSEd Tanous if (inventoryItem.isPowerSupply) 207342cbe538SGunnar Mills { 207442cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent = 20751e1e598dSJonathan Doman static_cast<int>(value); 207642cbe538SGunnar Mills } 207742cbe538SGunnar Mills } 207842cbe538SGunnar Mills 207942cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit"; 208042cbe538SGunnar Mills callback(inventoryItems); 208142cbe538SGunnar Mills }; 208242cbe538SGunnar Mills 208342cbe538SGunnar Mills // Get the DeratingFactor property for the PowerSupplyAttributes 208442cbe538SGunnar Mills // Currently only property on the interface/only one we care about 20851e1e598dSJonathan Doman sdbusplus::asio::getProperty<uint32_t>( 20861e1e598dSJonathan Doman *crow::connections::systemBus, psAttributesConnection, psAttributesPath, 20871e1e598dSJonathan Doman "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor", 20881e1e598dSJonathan Doman std::move(respHandler)); 208942cbe538SGunnar Mills 209042cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit"; 209142cbe538SGunnar Mills } 209242cbe538SGunnar Mills 209342cbe538SGunnar Mills /** 209442cbe538SGunnar Mills * @brief Gets the Power Supply Attributes such as EfficiencyPercent 209542cbe538SGunnar Mills * 209642cbe538SGunnar Mills * Gets the D-Bus connection (service) that provides Power Supply Attributes 209742cbe538SGunnar Mills * data. Then gets the Power Supply Attributes data from the connection 209842cbe538SGunnar Mills * (currently just assumes 1 connection) and stores the data in the inventory 209942cbe538SGunnar Mills * item. 210042cbe538SGunnar Mills * 210142cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 210242cbe538SGunnar Mills * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish. 210342cbe538SGunnar Mills * 210442cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 210542cbe538SGunnar Mills * when information has been obtained. 210642cbe538SGunnar Mills * 210742cbe538SGunnar Mills * The callback must have the following signature: 210842cbe538SGunnar Mills * @code 210942cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 211042cbe538SGunnar Mills * @endcode 211142cbe538SGunnar Mills * 211242cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 211342cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 211442cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 211542cbe538SGunnar Mills */ 211642cbe538SGunnar Mills template <typename Callback> 211742cbe538SGunnar Mills void getPowerSupplyAttributes( 211842cbe538SGunnar Mills std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 211942cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 212042cbe538SGunnar Mills Callback&& callback) 212142cbe538SGunnar Mills { 212242cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter"; 212342cbe538SGunnar Mills 212442cbe538SGunnar Mills // Only need the power supply attributes when the Power Schema 2125a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode != sensors::node::power) 212642cbe538SGunnar Mills { 212742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power"; 212842cbe538SGunnar Mills callback(inventoryItems); 212942cbe538SGunnar Mills return; 213042cbe538SGunnar Mills } 213142cbe538SGunnar Mills 213242cbe538SGunnar Mills const std::array<std::string, 1> interfaces = { 213342cbe538SGunnar Mills "xyz.openbmc_project.Control.PowerSupplyAttributes"}; 213442cbe538SGunnar Mills 213542cbe538SGunnar Mills // Response handler for parsing output from GetSubTree 2136b9d36b47SEd Tanous auto respHandler = 2137b9d36b47SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 2138b9d36b47SEd Tanous inventoryItems]( 2139b9d36b47SEd Tanous const boost::system::error_code ec, 2140b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 214142cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter"; 214242cbe538SGunnar Mills if (ec) 214342cbe538SGunnar Mills { 21448d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 214542cbe538SGunnar Mills BMCWEB_LOG_ERROR 214642cbe538SGunnar Mills << "getPowerSupplyAttributes respHandler DBus error " << ec; 214742cbe538SGunnar Mills return; 214842cbe538SGunnar Mills } 214926f6976fSEd Tanous if (subtree.empty()) 215042cbe538SGunnar Mills { 215142cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!"; 215242cbe538SGunnar Mills callback(inventoryItems); 215342cbe538SGunnar Mills return; 215442cbe538SGunnar Mills } 215542cbe538SGunnar Mills 215642cbe538SGunnar Mills // Currently we only support 1 power supply attribute, use this for 215742cbe538SGunnar Mills // all the power supplies. Build map of object path to connection. 215842cbe538SGunnar Mills // Assume just 1 connection and 1 path for now. 2159fe04d49cSNan Zhou std::map<std::string, std::string> psAttributesConnections; 216042cbe538SGunnar Mills 216142cbe538SGunnar Mills if (subtree[0].first.empty() || subtree[0].second.empty()) 216242cbe538SGunnar Mills { 216342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!"; 216442cbe538SGunnar Mills callback(inventoryItems); 216542cbe538SGunnar Mills return; 216642cbe538SGunnar Mills } 216742cbe538SGunnar Mills 216842cbe538SGunnar Mills const std::string& psAttributesPath = subtree[0].first; 216942cbe538SGunnar Mills const std::string& connection = subtree[0].second.begin()->first; 217042cbe538SGunnar Mills 217142cbe538SGunnar Mills if (connection.empty()) 217242cbe538SGunnar Mills { 217342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!"; 217442cbe538SGunnar Mills callback(inventoryItems); 217542cbe538SGunnar Mills return; 217642cbe538SGunnar Mills } 217742cbe538SGunnar Mills 217842cbe538SGunnar Mills psAttributesConnections[psAttributesPath] = connection; 217942cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> " 218042cbe538SGunnar Mills << connection; 218142cbe538SGunnar Mills 218242cbe538SGunnar Mills getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems, 218342cbe538SGunnar Mills psAttributesConnections, 218442cbe538SGunnar Mills std::move(callback)); 218542cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit"; 218642cbe538SGunnar Mills }; 218742cbe538SGunnar Mills // Make call to ObjectMapper to find the PowerSupplyAttributes service 218842cbe538SGunnar Mills crow::connections::systemBus->async_method_call( 218942cbe538SGunnar Mills std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 219042cbe538SGunnar Mills "/xyz/openbmc_project/object_mapper", 219142cbe538SGunnar Mills "xyz.openbmc_project.ObjectMapper", "GetSubTree", 219242cbe538SGunnar Mills "/xyz/openbmc_project", 0, interfaces); 219342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit"; 219442cbe538SGunnar Mills } 219542cbe538SGunnar Mills 219642cbe538SGunnar Mills /** 2197adc4f0dbSShawn McCarney * @brief Gets inventory items associated with sensors. 21988fb49dd6SShawn McCarney * 21998fb49dd6SShawn McCarney * Finds the inventory items that are associated with the specified sensors. 2200adc4f0dbSShawn McCarney * Then gets D-Bus data for the inventory items, such as presence and VPD. 22018fb49dd6SShawn McCarney * 2202adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 2203adc4f0dbSShawn McCarney * response. 22048fb49dd6SShawn McCarney * 2205adc4f0dbSShawn McCarney * Finds the inventory items asynchronously. Invokes callback when the 2206adc4f0dbSShawn McCarney * inventory items have been obtained. 2207adc4f0dbSShawn McCarney * 2208adc4f0dbSShawn McCarney * The callback must have the following signature: 2209adc4f0dbSShawn McCarney * @code 2210adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 2211adc4f0dbSShawn McCarney * @endcode 22128fb49dd6SShawn McCarney * 22138fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 22148fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 22158fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 22168fb49dd6SShawn McCarney * implements ObjectManager. 2217adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 22188fb49dd6SShawn McCarney */ 2219adc4f0dbSShawn McCarney template <typename Callback> 2220adc4f0dbSShawn McCarney static void getInventoryItems( 22218fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 2222fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> sensorNames, 2223fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths, 2224adc4f0dbSShawn McCarney Callback&& callback) 22258fb49dd6SShawn McCarney { 2226adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItems enter"; 2227adc4f0dbSShawn McCarney auto getInventoryItemAssociationsCb = 2228f94c4ecfSEd Tanous [sensorsAsyncResp, objectMgrPaths, 2229f94c4ecfSEd Tanous callback{std::forward<Callback>(callback)}]( 2230adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems) { 2231adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter"; 22328fb49dd6SShawn McCarney auto getInventoryItemsConnectionsCb = 2233adc4f0dbSShawn McCarney [sensorsAsyncResp, inventoryItems, objectMgrPaths, 2234f94c4ecfSEd Tanous callback{std::forward<const Callback>(callback)}]( 2235fe04d49cSNan Zhou std::shared_ptr<std::set<std::string>> invConnections) { 22368fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter"; 2237002d39b4SEd Tanous auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems, 2238d500549bSAnthony Wilson callback{std::move(callback)}]() { 2239d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter"; 224042cbe538SGunnar Mills 2241002d39b4SEd Tanous auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems, 2242002d39b4SEd Tanous callback{std::move(callback)}]() { 224342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter"; 224442cbe538SGunnar Mills // Find Power Supply Attributes and get the data 2245002d39b4SEd Tanous getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems, 224642cbe538SGunnar Mills std::move(callback)); 224742cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit"; 224842cbe538SGunnar Mills }; 224942cbe538SGunnar Mills 2250d500549bSAnthony Wilson // Find led connections and get the data 2251d500549bSAnthony Wilson getInventoryLeds(sensorsAsyncResp, inventoryItems, 225242cbe538SGunnar Mills std::move(getInventoryLedsCb)); 2253d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit"; 2254d500549bSAnthony Wilson }; 22558fb49dd6SShawn McCarney 2256adc4f0dbSShawn McCarney // Get inventory item data from connections 2257adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 2258adc4f0dbSShawn McCarney invConnections, objectMgrPaths, 2259d500549bSAnthony Wilson std::move(getInventoryItemsDataCb)); 22608fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit"; 22618fb49dd6SShawn McCarney }; 22628fb49dd6SShawn McCarney 2263adc4f0dbSShawn McCarney // Get connections that provide inventory item data 2264002d39b4SEd Tanous getInventoryItemsConnections(sensorsAsyncResp, inventoryItems, 22658fb49dd6SShawn McCarney std::move(getInventoryItemsConnectionsCb)); 2266adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit"; 22678fb49dd6SShawn McCarney }; 22688fb49dd6SShawn McCarney 2269adc4f0dbSShawn McCarney // Get associations from sensors to inventory items 2270adc4f0dbSShawn McCarney getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths, 2271adc4f0dbSShawn McCarney std::move(getInventoryItemAssociationsCb)); 2272adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItems exit"; 2273adc4f0dbSShawn McCarney } 2274adc4f0dbSShawn McCarney 2275adc4f0dbSShawn McCarney /** 2276adc4f0dbSShawn McCarney * @brief Returns JSON PowerSupply object for the specified inventory item. 2277adc4f0dbSShawn McCarney * 2278adc4f0dbSShawn McCarney * Searches for a JSON PowerSupply object that matches the specified inventory 2279adc4f0dbSShawn McCarney * item. If one is not found, a new PowerSupply object is added to the JSON 2280adc4f0dbSShawn McCarney * array. 2281adc4f0dbSShawn McCarney * 2282adc4f0dbSShawn McCarney * Multiple sensors are often associated with one power supply inventory item. 2283adc4f0dbSShawn McCarney * As a result, multiple sensor values are stored in one JSON PowerSupply 2284adc4f0dbSShawn McCarney * object. 2285adc4f0dbSShawn McCarney * 2286adc4f0dbSShawn McCarney * @param powerSupplyArray JSON array containing Redfish PowerSupply objects. 2287adc4f0dbSShawn McCarney * @param inventoryItem Inventory item for the power supply. 2288adc4f0dbSShawn McCarney * @param chassisId Chassis that contains the power supply. 2289adc4f0dbSShawn McCarney * @return JSON PowerSupply object for the specified inventory item. 2290adc4f0dbSShawn McCarney */ 229123a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray, 2292adc4f0dbSShawn McCarney const InventoryItem& inventoryItem, 2293adc4f0dbSShawn McCarney const std::string& chassisId) 2294adc4f0dbSShawn McCarney { 2295adc4f0dbSShawn McCarney // Check if matching PowerSupply object already exists in JSON array 2296adc4f0dbSShawn McCarney for (nlohmann::json& powerSupply : powerSupplyArray) 2297adc4f0dbSShawn McCarney { 2298adc4f0dbSShawn McCarney if (powerSupply["MemberId"] == inventoryItem.name) 2299adc4f0dbSShawn McCarney { 2300adc4f0dbSShawn McCarney return powerSupply; 2301adc4f0dbSShawn McCarney } 2302adc4f0dbSShawn McCarney } 2303adc4f0dbSShawn McCarney 2304adc4f0dbSShawn McCarney // Add new PowerSupply object to JSON array 2305adc4f0dbSShawn McCarney powerSupplyArray.push_back({}); 2306adc4f0dbSShawn McCarney nlohmann::json& powerSupply = powerSupplyArray.back(); 2307adc4f0dbSShawn McCarney powerSupply["@odata.id"] = 2308adc4f0dbSShawn McCarney "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/"; 2309adc4f0dbSShawn McCarney powerSupply["MemberId"] = inventoryItem.name; 2310adc4f0dbSShawn McCarney powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " "); 2311adc4f0dbSShawn McCarney powerSupply["Manufacturer"] = inventoryItem.manufacturer; 2312adc4f0dbSShawn McCarney powerSupply["Model"] = inventoryItem.model; 2313adc4f0dbSShawn McCarney powerSupply["PartNumber"] = inventoryItem.partNumber; 2314adc4f0dbSShawn McCarney powerSupply["SerialNumber"] = inventoryItem.serialNumber; 2315d500549bSAnthony Wilson setLedState(powerSupply, &inventoryItem); 2316adc4f0dbSShawn McCarney 231742cbe538SGunnar Mills if (inventoryItem.powerSupplyEfficiencyPercent >= 0) 231842cbe538SGunnar Mills { 231942cbe538SGunnar Mills powerSupply["EfficiencyPercent"] = 232042cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent; 232142cbe538SGunnar Mills } 232242cbe538SGunnar Mills 232342cbe538SGunnar Mills powerSupply["Status"]["State"] = getState(&inventoryItem); 2324adc4f0dbSShawn McCarney const char* health = inventoryItem.isFunctional ? "OK" : "Critical"; 2325adc4f0dbSShawn McCarney powerSupply["Status"]["Health"] = health; 2326adc4f0dbSShawn McCarney 2327adc4f0dbSShawn McCarney return powerSupply; 23288fb49dd6SShawn McCarney } 23298fb49dd6SShawn McCarney 23308fb49dd6SShawn McCarney /** 2331de629b6eSShawn McCarney * @brief Gets the values of the specified sensors. 2332de629b6eSShawn McCarney * 2333de629b6eSShawn McCarney * Stores the results as JSON in the SensorsAsyncResp. 2334de629b6eSShawn McCarney * 2335de629b6eSShawn McCarney * Gets the sensor values asynchronously. Stores the results later when the 2336de629b6eSShawn McCarney * information has been obtained. 2337de629b6eSShawn McCarney * 2338adc4f0dbSShawn McCarney * The sensorNames set contains all requested sensors for the current chassis. 2339de629b6eSShawn McCarney * 2340de629b6eSShawn McCarney * To minimize the number of DBus calls, the DBus method 2341de629b6eSShawn McCarney * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the 2342de629b6eSShawn McCarney * values of all sensors provided by a connection (service). 2343de629b6eSShawn McCarney * 2344de629b6eSShawn McCarney * The connections set contains all the connections that provide sensor values. 2345de629b6eSShawn McCarney * 2346de629b6eSShawn McCarney * The objectMgrPaths map contains mappings from a connection name to the 2347de629b6eSShawn McCarney * corresponding DBus object path that implements ObjectManager. 2348de629b6eSShawn McCarney * 2349adc4f0dbSShawn McCarney * The InventoryItem vector contains D-Bus inventory items associated with the 2350adc4f0dbSShawn McCarney * sensors. Inventory item data is needed for some Redfish sensor properties. 2351adc4f0dbSShawn McCarney * 2352de629b6eSShawn McCarney * @param SensorsAsyncResp Pointer to object holding response data. 2353adc4f0dbSShawn McCarney * @param sensorNames All requested sensors within the current chassis. 2354de629b6eSShawn McCarney * @param connections Connections that provide sensor values. 2355de629b6eSShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 2356de629b6eSShawn McCarney * implements ObjectManager. 2357adc4f0dbSShawn McCarney * @param inventoryItems Inventory items associated with the sensors. 2358de629b6eSShawn McCarney */ 235923a21a1cSEd Tanous inline void getSensorData( 236081ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 2361fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames, 2362fe04d49cSNan Zhou const std::set<std::string>& connections, 2363fe04d49cSNan Zhou const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths, 2364b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems) 2365de629b6eSShawn McCarney { 2366de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getSensorData enter"; 2367de629b6eSShawn McCarney // Get managed objects from all services exposing sensors 2368de629b6eSShawn McCarney for (const std::string& connection : connections) 2369de629b6eSShawn McCarney { 2370de629b6eSShawn McCarney // Response handler to process managed objects 2371002d39b4SEd Tanous auto getManagedObjectsCb = 2372002d39b4SEd Tanous [sensorsAsyncResp, sensorNames, 2373002d39b4SEd Tanous inventoryItems](const boost::system::error_code ec, 237402cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 2375de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter"; 2376de629b6eSShawn McCarney if (ec) 2377de629b6eSShawn McCarney { 2378de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec; 23798d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 2380de629b6eSShawn McCarney return; 2381de629b6eSShawn McCarney } 2382de629b6eSShawn McCarney // Go through all objects and update response with sensor data 2383de629b6eSShawn McCarney for (const auto& objDictEntry : resp) 2384de629b6eSShawn McCarney { 2385de629b6eSShawn McCarney const std::string& objPath = 2386de629b6eSShawn McCarney static_cast<const std::string&>(objDictEntry.first); 2387de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object " 2388de629b6eSShawn McCarney << objPath; 2389de629b6eSShawn McCarney 2390de629b6eSShawn McCarney std::vector<std::string> split; 2391de629b6eSShawn McCarney // Reserve space for 2392de629b6eSShawn McCarney // /xyz/openbmc_project/sensors/<name>/<subname> 2393de629b6eSShawn McCarney split.reserve(6); 2394de629b6eSShawn McCarney boost::algorithm::split(split, objPath, boost::is_any_of("/")); 2395de629b6eSShawn McCarney if (split.size() < 6) 2396de629b6eSShawn McCarney { 2397de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "Got path that isn't long enough " 2398de629b6eSShawn McCarney << objPath; 2399de629b6eSShawn McCarney continue; 2400de629b6eSShawn McCarney } 2401de629b6eSShawn McCarney // These indexes aren't intuitive, as boost::split puts an empty 2402de629b6eSShawn McCarney // string at the beginning 2403de629b6eSShawn McCarney const std::string& sensorType = split[4]; 2404de629b6eSShawn McCarney const std::string& sensorName = split[5]; 2405de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "sensorName " << sensorName 2406de629b6eSShawn McCarney << " sensorType " << sensorType; 240749c53ac9SJohnathan Mantey if (sensorNames->find(objPath) == sensorNames->end()) 2408de629b6eSShawn McCarney { 2409accdbb2cSAndrew Geissler BMCWEB_LOG_DEBUG << sensorName << " not in sensor list "; 2410de629b6eSShawn McCarney continue; 2411de629b6eSShawn McCarney } 2412de629b6eSShawn McCarney 2413adc4f0dbSShawn McCarney // Find inventory item (if any) associated with sensor 2414adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 2415adc4f0dbSShawn McCarney findInventoryItemForSensor(inventoryItems, objPath); 2416adc4f0dbSShawn McCarney 241795a3ecadSAnthony Wilson const std::string& sensorSchema = 241881ce609eSEd Tanous sensorsAsyncResp->chassisSubNode; 241995a3ecadSAnthony Wilson 242095a3ecadSAnthony Wilson nlohmann::json* sensorJson = nullptr; 242195a3ecadSAnthony Wilson 2422928fefb9SNan Zhou if (sensorSchema == sensors::node::sensors && 2423928fefb9SNan Zhou !sensorsAsyncResp->efficientExpand) 242495a3ecadSAnthony Wilson { 2425c1d019a6SEd Tanous std::string sensorTypeEscaped(sensorType); 2426c1d019a6SEd Tanous sensorTypeEscaped.erase( 2427c1d019a6SEd Tanous std::remove(sensorTypeEscaped.begin(), 2428c1d019a6SEd Tanous sensorTypeEscaped.end(), '_'), 2429c1d019a6SEd Tanous sensorTypeEscaped.end()); 2430c1d019a6SEd Tanous std::string sensorId(sensorTypeEscaped); 2431c1d019a6SEd Tanous sensorId += "_"; 2432c1d019a6SEd Tanous sensorId += sensorName; 2433c1d019a6SEd Tanous 24348d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] = 2435c1d019a6SEd Tanous crow::utility::urlFromPieces( 2436c1d019a6SEd Tanous "redfish", "v1", "Chassis", 2437c1d019a6SEd Tanous sensorsAsyncResp->chassisId, 2438c1d019a6SEd Tanous sensorsAsyncResp->chassisSubNode, sensorId); 24398d1b46d7Szhanghch05 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue); 244095a3ecadSAnthony Wilson } 244195a3ecadSAnthony Wilson else 244295a3ecadSAnthony Wilson { 2443271584abSEd Tanous std::string fieldName; 2444928fefb9SNan Zhou if (sensorsAsyncResp->efficientExpand) 2445928fefb9SNan Zhou { 2446928fefb9SNan Zhou fieldName = "Members"; 2447928fefb9SNan Zhou } 2448928fefb9SNan Zhou else if (sensorType == "temperature") 2449de629b6eSShawn McCarney { 2450de629b6eSShawn McCarney fieldName = "Temperatures"; 2451de629b6eSShawn McCarney } 2452de629b6eSShawn McCarney else if (sensorType == "fan" || sensorType == "fan_tach" || 2453de629b6eSShawn McCarney sensorType == "fan_pwm") 2454de629b6eSShawn McCarney { 2455de629b6eSShawn McCarney fieldName = "Fans"; 2456de629b6eSShawn McCarney } 2457de629b6eSShawn McCarney else if (sensorType == "voltage") 2458de629b6eSShawn McCarney { 2459de629b6eSShawn McCarney fieldName = "Voltages"; 2460de629b6eSShawn McCarney } 2461de629b6eSShawn McCarney else if (sensorType == "power") 2462de629b6eSShawn McCarney { 246355f79e6fSEd Tanous if (sensorName == "total_power") 2464028f7ebcSEddie James { 2465028f7ebcSEddie James fieldName = "PowerControl"; 2466028f7ebcSEddie James } 2467adc4f0dbSShawn McCarney else if ((inventoryItem != nullptr) && 2468adc4f0dbSShawn McCarney (inventoryItem->isPowerSupply)) 2469028f7ebcSEddie James { 2470de629b6eSShawn McCarney fieldName = "PowerSupplies"; 2471de629b6eSShawn McCarney } 2472adc4f0dbSShawn McCarney else 2473adc4f0dbSShawn McCarney { 2474adc4f0dbSShawn McCarney // Other power sensors are in SensorCollection 2475adc4f0dbSShawn McCarney continue; 2476adc4f0dbSShawn McCarney } 2477028f7ebcSEddie James } 2478de629b6eSShawn McCarney else 2479de629b6eSShawn McCarney { 2480de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "Unsure how to handle sensorType " 2481de629b6eSShawn McCarney << sensorType; 2482de629b6eSShawn McCarney continue; 2483de629b6eSShawn McCarney } 2484de629b6eSShawn McCarney 2485de629b6eSShawn McCarney nlohmann::json& tempArray = 24868d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName]; 2487adc4f0dbSShawn McCarney if (fieldName == "PowerControl") 248849c53ac9SJohnathan Mantey { 2489adc4f0dbSShawn McCarney if (tempArray.empty()) 24907ab06f49SGunnar Mills { 249195a3ecadSAnthony Wilson // Put multiple "sensors" into a single 249295a3ecadSAnthony Wilson // PowerControl. Follows MemberId naming and 249395a3ecadSAnthony Wilson // naming in power.hpp. 24941476687dSEd Tanous nlohmann::json::object_t power; 24951476687dSEd Tanous power["@odata.id"] = 2496adc4f0dbSShawn McCarney "/redfish/v1/Chassis/" + 249781ce609eSEd Tanous sensorsAsyncResp->chassisId + "/" + 249881ce609eSEd Tanous sensorsAsyncResp->chassisSubNode + "#/" + 24991476687dSEd Tanous fieldName + "/0"; 25001476687dSEd Tanous tempArray.push_back(std::move(power)); 2501adc4f0dbSShawn McCarney } 2502adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 2503adc4f0dbSShawn McCarney } 2504adc4f0dbSShawn McCarney else if (fieldName == "PowerSupplies") 2505adc4f0dbSShawn McCarney { 2506adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 2507adc4f0dbSShawn McCarney { 2508adc4f0dbSShawn McCarney sensorJson = 2509adc4f0dbSShawn McCarney &(getPowerSupply(tempArray, *inventoryItem, 251081ce609eSEd Tanous sensorsAsyncResp->chassisId)); 2511adc4f0dbSShawn McCarney } 251249c53ac9SJohnathan Mantey } 2513928fefb9SNan Zhou else if (fieldName == "Members") 2514928fefb9SNan Zhou { 2515677bb756SEd Tanous std::string sensorTypeEscaped(sensorType); 2516677bb756SEd Tanous sensorTypeEscaped.erase( 2517677bb756SEd Tanous std::remove(sensorTypeEscaped.begin(), 2518677bb756SEd Tanous sensorTypeEscaped.end(), '_'), 2519677bb756SEd Tanous sensorTypeEscaped.end()); 2520677bb756SEd Tanous std::string sensorId(sensorTypeEscaped); 2521677bb756SEd Tanous sensorId += "_"; 2522677bb756SEd Tanous sensorId += sensorName; 2523677bb756SEd Tanous 25241476687dSEd Tanous nlohmann::json::object_t member; 2525677bb756SEd Tanous member["@odata.id"] = crow::utility::urlFromPieces( 2526677bb756SEd Tanous "redfish", "v1", "Chassis", 2527677bb756SEd Tanous sensorsAsyncResp->chassisId, 2528677bb756SEd Tanous sensorsAsyncResp->chassisSubNode, sensorId); 25291476687dSEd Tanous tempArray.push_back(std::move(member)); 2530928fefb9SNan Zhou sensorJson = &(tempArray.back()); 2531928fefb9SNan Zhou } 253249c53ac9SJohnathan Mantey else 253349c53ac9SJohnathan Mantey { 25341476687dSEd Tanous nlohmann::json::object_t member; 25351476687dSEd Tanous member["@odata.id"] = "/redfish/v1/Chassis/" + 25361476687dSEd Tanous sensorsAsyncResp->chassisId + 25371476687dSEd Tanous "/" + 25381476687dSEd Tanous sensorsAsyncResp->chassisSubNode + 25391476687dSEd Tanous "#/" + fieldName + "/"; 25401476687dSEd Tanous tempArray.push_back(std::move(member)); 2541adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 254249c53ac9SJohnathan Mantey } 254395a3ecadSAnthony Wilson } 2544de629b6eSShawn McCarney 2545adc4f0dbSShawn McCarney if (sensorJson != nullptr) 2546adc4f0dbSShawn McCarney { 25471d7c0054SEd Tanous objectInterfacesToJson(sensorName, sensorType, 25481d7c0054SEd Tanous sensorsAsyncResp->chassisSubNode, 25491d7c0054SEd Tanous objDictEntry.second, *sensorJson, 25501d7c0054SEd Tanous inventoryItem); 25511d7c0054SEd Tanous 25521d7c0054SEd Tanous std::string path = "/xyz/openbmc_project/sensors/"; 25531d7c0054SEd Tanous path += sensorType; 25541d7c0054SEd Tanous path += "/"; 25551d7c0054SEd Tanous path += sensorName; 2556c1d019a6SEd Tanous sensorsAsyncResp->addMetadata(*sensorJson, path); 2557adc4f0dbSShawn McCarney } 2558de629b6eSShawn McCarney } 255981ce609eSEd Tanous if (sensorsAsyncResp.use_count() == 1) 256049c53ac9SJohnathan Mantey { 256181ce609eSEd Tanous sortJSONResponse(sensorsAsyncResp); 2562928fefb9SNan Zhou if (sensorsAsyncResp->chassisSubNode == 2563928fefb9SNan Zhou sensors::node::sensors && 2564928fefb9SNan Zhou sensorsAsyncResp->efficientExpand) 2565928fefb9SNan Zhou { 2566928fefb9SNan Zhou sensorsAsyncResp->asyncResp->res 2567928fefb9SNan Zhou .jsonValue["Members@odata.count"] = 2568928fefb9SNan Zhou sensorsAsyncResp->asyncResp->res.jsonValue["Members"] 2569928fefb9SNan Zhou .size(); 2570928fefb9SNan Zhou } 2571928fefb9SNan Zhou else if (sensorsAsyncResp->chassisSubNode == 2572928fefb9SNan Zhou sensors::node::thermal) 25738bd25ccdSJames Feist { 257481ce609eSEd Tanous populateFanRedundancy(sensorsAsyncResp); 25758bd25ccdSJames Feist } 257649c53ac9SJohnathan Mantey } 2577de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit"; 2578de629b6eSShawn McCarney }; 2579de629b6eSShawn McCarney 2580de629b6eSShawn McCarney // Find DBus object path that implements ObjectManager for the current 2581de629b6eSShawn McCarney // connection. If no mapping found, default to "/". 25828fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(connection); 2583de629b6eSShawn McCarney const std::string& objectMgrPath = 25848fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 2585de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is " 2586de629b6eSShawn McCarney << objectMgrPath; 2587de629b6eSShawn McCarney 2588de629b6eSShawn McCarney crow::connections::systemBus->async_method_call( 2589de629b6eSShawn McCarney getManagedObjectsCb, connection, objectMgrPath, 2590de629b6eSShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 259123a21a1cSEd Tanous } 2592de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getSensorData exit"; 2593de629b6eSShawn McCarney } 2594de629b6eSShawn McCarney 2595fe04d49cSNan Zhou inline void 2596fe04d49cSNan Zhou processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 2597fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames) 25981abe55efSEd Tanous { 2599fe04d49cSNan Zhou auto getConnectionCb = [sensorsAsyncResp, sensorNames]( 2600fe04d49cSNan Zhou const std::set<std::string>& connections) { 260155c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getConnectionCb enter"; 2602de629b6eSShawn McCarney auto getObjectManagerPathsCb = 2603fe04d49cSNan Zhou [sensorsAsyncResp, sensorNames, connections]( 2604fe04d49cSNan Zhou const std::shared_ptr<std::map<std::string, std::string>>& 2605fe04d49cSNan Zhou objectMgrPaths) { 2606de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter"; 2607adc4f0dbSShawn McCarney auto getInventoryItemsCb = 2608002d39b4SEd Tanous [sensorsAsyncResp, sensorNames, connections, objectMgrPaths]( 2609f23b7296SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& 2610adc4f0dbSShawn McCarney inventoryItems) { 2611adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter"; 261249c53ac9SJohnathan Mantey // Get sensor data and store results in JSON 2613002d39b4SEd Tanous getSensorData(sensorsAsyncResp, sensorNames, connections, 2614002d39b4SEd Tanous objectMgrPaths, inventoryItems); 2615adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit"; 2616adc4f0dbSShawn McCarney }; 2617adc4f0dbSShawn McCarney 2618adc4f0dbSShawn McCarney // Get inventory items associated with sensors 2619002d39b4SEd Tanous getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths, 2620adc4f0dbSShawn McCarney std::move(getInventoryItemsCb)); 2621adc4f0dbSShawn McCarney 2622de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit"; 262308777fb0SLewanczyk, Dawid }; 2624de629b6eSShawn McCarney 262549c53ac9SJohnathan Mantey // Get mapping from connection names to the DBus object 262649c53ac9SJohnathan Mantey // paths that implement the ObjectManager interface 262781ce609eSEd Tanous getObjectManagerPaths(sensorsAsyncResp, 2628de629b6eSShawn McCarney std::move(getObjectManagerPathsCb)); 262955c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getConnectionCb exit"; 263008777fb0SLewanczyk, Dawid }; 2631de629b6eSShawn McCarney 2632de629b6eSShawn McCarney // Get set of connections that provide sensor values 263381ce609eSEd Tanous getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb)); 263495a3ecadSAnthony Wilson } 263595a3ecadSAnthony Wilson 263695a3ecadSAnthony Wilson /** 263795a3ecadSAnthony Wilson * @brief Entry point for retrieving sensors data related to requested 263895a3ecadSAnthony Wilson * chassis. 263995a3ecadSAnthony Wilson * @param SensorsAsyncResp Pointer to object holding response data 264095a3ecadSAnthony Wilson */ 2641b5a76932SEd Tanous inline void 264281ce609eSEd Tanous getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 264395a3ecadSAnthony Wilson { 264495a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisData enter"; 264595a3ecadSAnthony Wilson auto getChassisCb = 264681ce609eSEd Tanous [sensorsAsyncResp]( 2647fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames) { 264895a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb enter"; 264981ce609eSEd Tanous processSensorList(sensorsAsyncResp, sensorNames); 265055c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassisCb exit"; 265108777fb0SLewanczyk, Dawid }; 2652928fefb9SNan Zhou // SensorCollection doesn't contain the Redundancy property 2653928fefb9SNan Zhou if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors) 2654928fefb9SNan Zhou { 26558d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] = 26568d1b46d7Szhanghch05 nlohmann::json::array(); 2657928fefb9SNan Zhou } 265826f03899SShawn McCarney // Get set of sensors in chassis 26597f1cc26dSEd Tanous getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId, 26607f1cc26dSEd Tanous sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types, 26617f1cc26dSEd Tanous std::move(getChassisCb)); 266255c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassisData exit"; 2663271584abSEd Tanous } 266408777fb0SLewanczyk, Dawid 2665413961deSRichard Marian Thomaiyar /** 266649c53ac9SJohnathan Mantey * @brief Find the requested sensorName in the list of all sensors supplied by 266749c53ac9SJohnathan Mantey * the chassis node 266849c53ac9SJohnathan Mantey * 266949c53ac9SJohnathan Mantey * @param sensorName The sensor name supplied in the PATCH request 267049c53ac9SJohnathan Mantey * @param sensorsList The list of sensors managed by the chassis node 267149c53ac9SJohnathan Mantey * @param sensorsModified The list of sensors that were found as a result of 267249c53ac9SJohnathan Mantey * repeated calls to this function 267349c53ac9SJohnathan Mantey */ 2674fe04d49cSNan Zhou inline bool 2675fe04d49cSNan Zhou findSensorNameUsingSensorPath(std::string_view sensorName, 267602cad96eSEd Tanous const std::set<std::string>& sensorsList, 2677fe04d49cSNan Zhou std::set<std::string>& sensorsModified) 267849c53ac9SJohnathan Mantey { 2679fe04d49cSNan Zhou for (const auto& chassisSensor : sensorsList) 268049c53ac9SJohnathan Mantey { 268128aa8de5SGeorge Liu sdbusplus::message::object_path path(chassisSensor); 2682b00dcc27SEd Tanous std::string thisSensorName = path.filename(); 268328aa8de5SGeorge Liu if (thisSensorName.empty()) 268449c53ac9SJohnathan Mantey { 268549c53ac9SJohnathan Mantey continue; 268649c53ac9SJohnathan Mantey } 268749c53ac9SJohnathan Mantey if (thisSensorName == sensorName) 268849c53ac9SJohnathan Mantey { 268949c53ac9SJohnathan Mantey sensorsModified.emplace(chassisSensor); 269049c53ac9SJohnathan Mantey return true; 269149c53ac9SJohnathan Mantey } 269249c53ac9SJohnathan Mantey } 269349c53ac9SJohnathan Mantey return false; 269449c53ac9SJohnathan Mantey } 269549c53ac9SJohnathan Mantey 269649c53ac9SJohnathan Mantey /** 2697413961deSRichard Marian Thomaiyar * @brief Entry point for overriding sensor values of given sensor 2698413961deSRichard Marian Thomaiyar * 26998d1b46d7Szhanghch05 * @param sensorAsyncResp response object 27004bb3dc34SCarol Wang * @param allCollections Collections extract from sensors' request patch info 2701413961deSRichard Marian Thomaiyar * @param chassisSubNode Chassis Node for which the query has to happen 2702413961deSRichard Marian Thomaiyar */ 270323a21a1cSEd Tanous inline void setSensorsOverride( 2704b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp, 27054bb3dc34SCarol Wang std::unordered_map<std::string, std::vector<nlohmann::json>>& 2706397fd61fSjayaprakash Mutyala allCollections) 2707413961deSRichard Marian Thomaiyar { 270870d1d0aaSjayaprakash Mutyala BMCWEB_LOG_INFO << "setSensorsOverride for subNode" 27094bb3dc34SCarol Wang << sensorAsyncResp->chassisSubNode << "\n"; 2710413961deSRichard Marian Thomaiyar 2711543f4400SEd Tanous const char* propertyValueName = nullptr; 2712f65af9e8SRichard Marian Thomaiyar std::unordered_map<std::string, std::pair<double, std::string>> overrideMap; 2713413961deSRichard Marian Thomaiyar std::string memberId; 2714543f4400SEd Tanous double value = 0.0; 2715f65af9e8SRichard Marian Thomaiyar for (auto& collectionItems : allCollections) 2716f65af9e8SRichard Marian Thomaiyar { 2717f65af9e8SRichard Marian Thomaiyar if (collectionItems.first == "Temperatures") 2718f65af9e8SRichard Marian Thomaiyar { 2719f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingCelsius"; 2720f65af9e8SRichard Marian Thomaiyar } 2721f65af9e8SRichard Marian Thomaiyar else if (collectionItems.first == "Fans") 2722f65af9e8SRichard Marian Thomaiyar { 2723f65af9e8SRichard Marian Thomaiyar propertyValueName = "Reading"; 2724f65af9e8SRichard Marian Thomaiyar } 2725f65af9e8SRichard Marian Thomaiyar else 2726f65af9e8SRichard Marian Thomaiyar { 2727f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingVolts"; 2728f65af9e8SRichard Marian Thomaiyar } 2729f65af9e8SRichard Marian Thomaiyar for (auto& item : collectionItems.second) 2730f65af9e8SRichard Marian Thomaiyar { 27318d1b46d7Szhanghch05 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res, 27328d1b46d7Szhanghch05 "MemberId", memberId, propertyValueName, 27338d1b46d7Szhanghch05 value)) 2734413961deSRichard Marian Thomaiyar { 2735413961deSRichard Marian Thomaiyar return; 2736413961deSRichard Marian Thomaiyar } 2737f65af9e8SRichard Marian Thomaiyar overrideMap.emplace(memberId, 2738f65af9e8SRichard Marian Thomaiyar std::make_pair(value, collectionItems.first)); 2739f65af9e8SRichard Marian Thomaiyar } 2740f65af9e8SRichard Marian Thomaiyar } 27414bb3dc34SCarol Wang 2742002d39b4SEd Tanous auto getChassisSensorListCb = 2743002d39b4SEd Tanous [sensorAsyncResp, overrideMap]( 2744fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorsList) { 274549c53ac9SJohnathan Mantey // Match sensor names in the PATCH request to those managed by the 274649c53ac9SJohnathan Mantey // chassis node 2747fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> sensorNames = 2748fe04d49cSNan Zhou std::make_shared<std::set<std::string>>(); 2749f65af9e8SRichard Marian Thomaiyar for (const auto& item : overrideMap) 2750413961deSRichard Marian Thomaiyar { 2751f65af9e8SRichard Marian Thomaiyar const auto& sensor = item.first; 275249c53ac9SJohnathan Mantey if (!findSensorNameUsingSensorPath(sensor, *sensorsList, 275349c53ac9SJohnathan Mantey *sensorNames)) 2754f65af9e8SRichard Marian Thomaiyar { 2755f65af9e8SRichard Marian Thomaiyar BMCWEB_LOG_INFO << "Unable to find memberId " << item.first; 27568d1b46d7Szhanghch05 messages::resourceNotFound(sensorAsyncResp->asyncResp->res, 2757f65af9e8SRichard Marian Thomaiyar item.second.second, item.first); 2758413961deSRichard Marian Thomaiyar return; 2759413961deSRichard Marian Thomaiyar } 2760f65af9e8SRichard Marian Thomaiyar } 2761413961deSRichard Marian Thomaiyar // Get the connection to which the memberId belongs 2762002d39b4SEd Tanous auto getObjectsWithConnectionCb = 2763fe04d49cSNan Zhou [sensorAsyncResp, 2764fe04d49cSNan Zhou overrideMap](const std::set<std::string>& /*connections*/, 2765002d39b4SEd Tanous const std::set<std::pair<std::string, std::string>>& 2766413961deSRichard Marian Thomaiyar objectsWithConnection) { 2767f65af9e8SRichard Marian Thomaiyar if (objectsWithConnection.size() != overrideMap.size()) 2768413961deSRichard Marian Thomaiyar { 2769413961deSRichard Marian Thomaiyar BMCWEB_LOG_INFO 2770f65af9e8SRichard Marian Thomaiyar << "Unable to find all objects with proper connection " 2771f65af9e8SRichard Marian Thomaiyar << objectsWithConnection.size() << " requested " 2772f65af9e8SRichard Marian Thomaiyar << overrideMap.size() << "\n"; 27734f277b54SJayaprakash Mutyala messages::resourceNotFound(sensorAsyncResp->asyncResp->res, 2774a0ec28b6SAdrian Ambrożewicz sensorAsyncResp->chassisSubNode == 2775a0ec28b6SAdrian Ambrożewicz sensors::node::thermal 2776413961deSRichard Marian Thomaiyar ? "Temperatures" 2777413961deSRichard Marian Thomaiyar : "Voltages", 2778f65af9e8SRichard Marian Thomaiyar "Count"); 2779f65af9e8SRichard Marian Thomaiyar return; 2780f65af9e8SRichard Marian Thomaiyar } 2781f65af9e8SRichard Marian Thomaiyar for (const auto& item : objectsWithConnection) 2782f65af9e8SRichard Marian Thomaiyar { 278328aa8de5SGeorge Liu sdbusplus::message::object_path path(item.first); 278428aa8de5SGeorge Liu std::string sensorName = path.filename(); 278528aa8de5SGeorge Liu if (sensorName.empty()) 2786f65af9e8SRichard Marian Thomaiyar { 27874f277b54SJayaprakash Mutyala messages::internalError(sensorAsyncResp->asyncResp->res); 2788f65af9e8SRichard Marian Thomaiyar return; 2789f65af9e8SRichard Marian Thomaiyar } 2790f65af9e8SRichard Marian Thomaiyar 2791f65af9e8SRichard Marian Thomaiyar const auto& iterator = overrideMap.find(sensorName); 2792f65af9e8SRichard Marian Thomaiyar if (iterator == overrideMap.end()) 2793f65af9e8SRichard Marian Thomaiyar { 2794f65af9e8SRichard Marian Thomaiyar BMCWEB_LOG_INFO << "Unable to find sensor object" 2795f65af9e8SRichard Marian Thomaiyar << item.first << "\n"; 27964f277b54SJayaprakash Mutyala messages::internalError(sensorAsyncResp->asyncResp->res); 2797413961deSRichard Marian Thomaiyar return; 2798413961deSRichard Marian Thomaiyar } 2799413961deSRichard Marian Thomaiyar crow::connections::systemBus->async_method_call( 2800f65af9e8SRichard Marian Thomaiyar [sensorAsyncResp](const boost::system::error_code ec) { 2801413961deSRichard Marian Thomaiyar if (ec) 2802413961deSRichard Marian Thomaiyar { 28034f277b54SJayaprakash Mutyala if (ec.value() == 28044f277b54SJayaprakash Mutyala boost::system::errc::permission_denied) 28054f277b54SJayaprakash Mutyala { 28064f277b54SJayaprakash Mutyala BMCWEB_LOG_WARNING 28074f277b54SJayaprakash Mutyala << "Manufacturing mode is not Enabled...can't " 28084f277b54SJayaprakash Mutyala "Override the sensor value. "; 28094f277b54SJayaprakash Mutyala 28104f277b54SJayaprakash Mutyala messages::insufficientPrivilege( 28118d1b46d7Szhanghch05 sensorAsyncResp->asyncResp->res); 2812413961deSRichard Marian Thomaiyar return; 2813413961deSRichard Marian Thomaiyar } 28144f277b54SJayaprakash Mutyala BMCWEB_LOG_DEBUG 28154f277b54SJayaprakash Mutyala << "setOverrideValueStatus DBUS error: " << ec; 28164f277b54SJayaprakash Mutyala messages::internalError( 28174f277b54SJayaprakash Mutyala sensorAsyncResp->asyncResp->res); 28184f277b54SJayaprakash Mutyala } 2819413961deSRichard Marian Thomaiyar }, 28204f277b54SJayaprakash Mutyala item.second, item.first, "org.freedesktop.DBus.Properties", 28214f277b54SJayaprakash Mutyala "Set", "xyz.openbmc_project.Sensor.Value", "Value", 2822168e20c1SEd Tanous dbus::utility::DbusVariantType(iterator->second.first)); 2823f65af9e8SRichard Marian Thomaiyar } 2824413961deSRichard Marian Thomaiyar }; 2825413961deSRichard Marian Thomaiyar // Get object with connection for the given sensor name 2826413961deSRichard Marian Thomaiyar getObjectsWithConnection(sensorAsyncResp, sensorNames, 2827413961deSRichard Marian Thomaiyar std::move(getObjectsWithConnectionCb)); 2828413961deSRichard Marian Thomaiyar }; 2829413961deSRichard Marian Thomaiyar // get full sensor list for the given chassisId and cross verify the sensor. 28307f1cc26dSEd Tanous getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId, 28317f1cc26dSEd Tanous sensorAsyncResp->chassisSubNode, sensorAsyncResp->types, 28327f1cc26dSEd Tanous std::move(getChassisSensorListCb)); 2833413961deSRichard Marian Thomaiyar } 2834413961deSRichard Marian Thomaiyar 2835a0ec28b6SAdrian Ambrożewicz /** 2836a0ec28b6SAdrian Ambrożewicz * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus 2837a0ec28b6SAdrian Ambrożewicz * path of the sensor. 2838a0ec28b6SAdrian Ambrożewicz * 2839a0ec28b6SAdrian Ambrożewicz * Function builds valid Redfish response for sensor query of given chassis and 2840a0ec28b6SAdrian Ambrożewicz * node. It then builds metadata about Redfish<->D-Bus correlations and provides 2841a0ec28b6SAdrian Ambrożewicz * it to caller in a callback. 2842a0ec28b6SAdrian Ambrożewicz * 2843a0ec28b6SAdrian Ambrożewicz * @param chassis Chassis for which retrieval should be performed 2844a0ec28b6SAdrian Ambrożewicz * @param node Node (group) of sensors. See sensors::node for supported values 2845a0ec28b6SAdrian Ambrożewicz * @param mapComplete Callback to be called with retrieval result 2846a0ec28b6SAdrian Ambrożewicz */ 2847021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis, 2848021d32cfSKrzysztof Grobelny const std::string& node, 2849a0ec28b6SAdrian Ambrożewicz SensorsAsyncResp::DataCompleteCb&& mapComplete) 2850a0ec28b6SAdrian Ambrożewicz { 285102da7c5aSEd Tanous decltype(sensors::paths)::const_iterator pathIt = 285202da7c5aSEd Tanous std::find_if(sensors::paths.cbegin(), sensors::paths.cend(), 285302da7c5aSEd Tanous [&node](auto&& val) { return val.first == node; }); 285402da7c5aSEd Tanous if (pathIt == sensors::paths.cend()) 2855a0ec28b6SAdrian Ambrożewicz { 2856a0ec28b6SAdrian Ambrożewicz BMCWEB_LOG_ERROR << "Wrong node provided : " << node; 2857a0ec28b6SAdrian Ambrożewicz mapComplete(boost::beast::http::status::bad_request, {}); 2858a0ec28b6SAdrian Ambrożewicz return; 2859a0ec28b6SAdrian Ambrożewicz } 2860d51e072fSKrzysztof Grobelny 286172374eb7SNan Zhou auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); 2862fe04d49cSNan Zhou auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}]( 2863a0ec28b6SAdrian Ambrożewicz const boost::beast::http::status status, 2864fe04d49cSNan Zhou const std::map<std::string, std::string>& uriToDbus) { 2865fe04d49cSNan Zhou mapCompleteCb(status, uriToDbus); 2866fe04d49cSNan Zhou }; 2867a0ec28b6SAdrian Ambrożewicz 2868a0ec28b6SAdrian Ambrożewicz auto resp = std::make_shared<SensorsAsyncResp>( 2869d51e072fSKrzysztof Grobelny asyncResp, chassis, pathIt->second, node, std::move(callback)); 2870a0ec28b6SAdrian Ambrożewicz getChassisData(resp); 2871a0ec28b6SAdrian Ambrożewicz } 2872a0ec28b6SAdrian Ambrożewicz 2873bacb2162SNan Zhou namespace sensors 2874bacb2162SNan Zhou { 2875928fefb9SNan Zhou 2876bacb2162SNan Zhou inline void getChassisCallback( 2877c1d019a6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2878c1d019a6SEd Tanous std::string_view chassisId, std::string_view chassisSubNode, 2879fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames) 2880bacb2162SNan Zhou { 2881bacb2162SNan Zhou BMCWEB_LOG_DEBUG << "getChassisCallback enter "; 2882bacb2162SNan Zhou 2883c1d019a6SEd Tanous nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"]; 2884c1d019a6SEd Tanous for (const std::string& sensor : *sensorNames) 2885bacb2162SNan Zhou { 2886bacb2162SNan Zhou BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor; 2887bacb2162SNan Zhou 2888bacb2162SNan Zhou sdbusplus::message::object_path path(sensor); 2889bacb2162SNan Zhou std::string sensorName = path.filename(); 2890bacb2162SNan Zhou if (sensorName.empty()) 2891bacb2162SNan Zhou { 2892bacb2162SNan Zhou BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor; 2893c1d019a6SEd Tanous messages::internalError(asyncResp->res); 2894bacb2162SNan Zhou return; 2895bacb2162SNan Zhou } 2896c1d019a6SEd Tanous std::string type = path.parent_path().filename(); 2897c1d019a6SEd Tanous // fan_tach has an underscore in it, so remove it to "normalize" the 2898c1d019a6SEd Tanous // type in the URI 2899c1d019a6SEd Tanous type.erase(std::remove(type.begin(), type.end(), '_'), type.end()); 2900c1d019a6SEd Tanous 29011476687dSEd Tanous nlohmann::json::object_t member; 2902c1d019a6SEd Tanous std::string id = type; 2903c1d019a6SEd Tanous id += "_"; 2904c1d019a6SEd Tanous id += sensorName; 2905c1d019a6SEd Tanous member["@odata.id"] = crow::utility::urlFromPieces( 2906c1d019a6SEd Tanous "redfish", "v1", "Chassis", chassisId, chassisSubNode, id); 2907c1d019a6SEd Tanous 29081476687dSEd Tanous entriesArray.push_back(std::move(member)); 2909bacb2162SNan Zhou } 2910bacb2162SNan Zhou 2911c1d019a6SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size(); 2912bacb2162SNan Zhou BMCWEB_LOG_DEBUG << "getChassisCallback exit"; 2913bacb2162SNan Zhou } 2914e6bd846dSNan Zhou 2915de167a6fSNan Zhou inline void 2916de167a6fSNan Zhou handleSensorCollectionGet(App& app, const crow::Request& req, 2917de167a6fSNan Zhou const std::shared_ptr<bmcweb::AsyncResp>& aResp, 2918de167a6fSNan Zhou const std::string& chassisId) 2919de167a6fSNan Zhou { 2920de167a6fSNan Zhou query_param::QueryCapabilities capabilities = { 2921de167a6fSNan Zhou .canDelegateExpandLevel = 1, 2922de167a6fSNan Zhou }; 2923de167a6fSNan Zhou query_param::Query delegatedQuery; 29243ba00073SCarson Labrado if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp, 2925de167a6fSNan Zhou delegatedQuery, capabilities)) 2926de167a6fSNan Zhou { 2927de167a6fSNan Zhou return; 2928de167a6fSNan Zhou } 2929de167a6fSNan Zhou 2930de167a6fSNan Zhou if (delegatedQuery.expandType != query_param::ExpandType::None) 2931de167a6fSNan Zhou { 2932de167a6fSNan Zhou // we perform efficient expand. 2933de167a6fSNan Zhou auto asyncResp = std::make_shared<SensorsAsyncResp>( 2934de167a6fSNan Zhou aResp, chassisId, sensors::dbus::sensorPaths, 2935de167a6fSNan Zhou sensors::node::sensors, 2936de167a6fSNan Zhou /*efficientExpand=*/true); 2937de167a6fSNan Zhou getChassisData(asyncResp); 2938de167a6fSNan Zhou 2939de167a6fSNan Zhou BMCWEB_LOG_DEBUG 2940de167a6fSNan Zhou << "SensorCollection doGet exit via efficient expand handler"; 2941de167a6fSNan Zhou return; 29420bad320cSEd Tanous } 2943de167a6fSNan Zhou 2944de167a6fSNan Zhou // We get all sensors as hyperlinkes in the chassis (this 2945de167a6fSNan Zhou // implies we reply on the default query parameters handler) 2946c1d019a6SEd Tanous getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths, 2947c1d019a6SEd Tanous std::bind_front(sensors::getChassisCallback, aResp, chassisId, 2948c1d019a6SEd Tanous sensors::node::sensors)); 2949c1d019a6SEd Tanous } 29507f1cc26dSEd Tanous 2951c1d019a6SEd Tanous inline void 2952c1d019a6SEd Tanous getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2953c1d019a6SEd Tanous const std::string& sensorPath, 2954c1d019a6SEd Tanous const ::dbus::utility::MapperGetObject& mapperResponse) 2955c1d019a6SEd Tanous { 2956c1d019a6SEd Tanous if (mapperResponse.size() != 1) 2957c1d019a6SEd Tanous { 2958c1d019a6SEd Tanous messages::internalError(asyncResp->res); 2959c1d019a6SEd Tanous return; 2960c1d019a6SEd Tanous } 2961c1d019a6SEd Tanous const auto& valueIface = *mapperResponse.begin(); 2962c1d019a6SEd Tanous const std::string& connectionName = valueIface.first; 2963c1d019a6SEd Tanous BMCWEB_LOG_DEBUG << "Looking up " << connectionName; 2964c1d019a6SEd Tanous BMCWEB_LOG_DEBUG << "Path " << sensorPath; 2965c1343bf6SKrzysztof Grobelny 2966c1343bf6SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 2967c1343bf6SKrzysztof Grobelny *crow::connections::systemBus, connectionName, sensorPath, "", 2968c1d019a6SEd Tanous [asyncResp, 2969c1d019a6SEd Tanous sensorPath](const boost::system::error_code ec, 2970c1d019a6SEd Tanous const ::dbus::utility::DBusPropertiesMap& valuesDict) { 2971c1d019a6SEd Tanous if (ec) 2972c1d019a6SEd Tanous { 2973c1d019a6SEd Tanous messages::internalError(asyncResp->res); 2974c1d019a6SEd Tanous return; 2975c1d019a6SEd Tanous } 2976c1d019a6SEd Tanous sdbusplus::message::object_path path(sensorPath); 2977c1d019a6SEd Tanous std::string name = path.filename(); 2978c1d019a6SEd Tanous path = path.parent_path(); 2979c1d019a6SEd Tanous std::string type = path.filename(); 2980c1d019a6SEd Tanous objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict, 2981c1d019a6SEd Tanous asyncResp->res.jsonValue, nullptr); 2982c1343bf6SKrzysztof Grobelny }); 2983de167a6fSNan Zhou } 2984de167a6fSNan Zhou 2985e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req, 2986c1d019a6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2987677bb756SEd Tanous const std::string& chassisId, 2988c1d019a6SEd Tanous const std::string& sensorId) 2989e6bd846dSNan Zhou { 2990c1d019a6SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2991e6bd846dSNan Zhou { 2992e6bd846dSNan Zhou return; 2993e6bd846dSNan Zhou } 2994c1d019a6SEd Tanous size_t index = sensorId.find('_'); 2995c1d019a6SEd Tanous if (index == std::string::npos) 2996c1d019a6SEd Tanous { 2997c1d019a6SEd Tanous messages::resourceNotFound(asyncResp->res, sensorId, "Sensor"); 2998c1d019a6SEd Tanous return; 2999c1d019a6SEd Tanous } 3000677bb756SEd Tanous asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces( 3001677bb756SEd Tanous "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId); 3002c1d019a6SEd Tanous std::string sensorType = sensorId.substr(0, index); 3003c1d019a6SEd Tanous std::string sensorName = sensorId.substr(index + 1); 3004c1d019a6SEd Tanous // fan_pwm and fan_tach need special handling 3005c1d019a6SEd Tanous if (sensorType == "fantach" || sensorType == "fanpwm") 3006c1d019a6SEd Tanous { 3007c1d019a6SEd Tanous sensorType.insert(3, 1, '_'); 3008c1d019a6SEd Tanous } 3009c1d019a6SEd Tanous 3010e6bd846dSNan Zhou BMCWEB_LOG_DEBUG << "Sensor doGet enter"; 3011e6bd846dSNan Zhou 3012e6bd846dSNan Zhou const std::array<const char*, 1> interfaces = { 3013e6bd846dSNan Zhou "xyz.openbmc_project.Sensor.Value"}; 3014c1d019a6SEd Tanous std::string sensorPath = 3015c1d019a6SEd Tanous "/xyz/openbmc_project/sensors/" + sensorType + '/' + sensorName; 3016e6bd846dSNan Zhou // Get a list of all of the sensors that implement Sensor.Value 3017e6bd846dSNan Zhou // and get the path and service name associated with the sensor 3018e6bd846dSNan Zhou crow::connections::systemBus->async_method_call( 3019c1d019a6SEd Tanous [asyncResp, sensorPath, 3020e6bd846dSNan Zhou sensorName](const boost::system::error_code ec, 3021c1d019a6SEd Tanous const ::dbus::utility::MapperGetObject& subtree) { 3022e6bd846dSNan Zhou BMCWEB_LOG_DEBUG << "respHandler1 enter"; 3023e6bd846dSNan Zhou if (ec) 3024e6bd846dSNan Zhou { 3025c1d019a6SEd Tanous messages::internalError(asyncResp->res); 3026e6bd846dSNan Zhou BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: " 3027e6bd846dSNan Zhou << "Dbus error " << ec; 3028e6bd846dSNan Zhou return; 3029e6bd846dSNan Zhou } 3030c1d019a6SEd Tanous getSensorFromDbus(asyncResp, sensorPath, subtree); 3031e6bd846dSNan Zhou BMCWEB_LOG_DEBUG << "respHandler1 exit"; 3032e6bd846dSNan Zhou }, 3033e6bd846dSNan Zhou "xyz.openbmc_project.ObjectMapper", 3034e6bd846dSNan Zhou "/xyz/openbmc_project/object_mapper", 3035c1d019a6SEd Tanous "xyz.openbmc_project.ObjectMapper", "GetObject", sensorPath, 3036c1d019a6SEd Tanous interfaces); 3037e6bd846dSNan Zhou } 3038e6bd846dSNan Zhou 3039bacb2162SNan Zhou } // namespace sensors 3040bacb2162SNan Zhou 30417e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app) 304295a3ecadSAnthony Wilson { 30437e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/") 3044ed398213SEd Tanous .privileges(redfish::privileges::getSensorCollection) 3045002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 3046de167a6fSNan Zhou std::bind_front(sensors::handleSensorCollectionGet, std::ref(app))); 304795a3ecadSAnthony Wilson } 304895a3ecadSAnthony Wilson 30497e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app) 305095a3ecadSAnthony Wilson { 30517e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/") 3052ed398213SEd Tanous .privileges(redfish::privileges::getSensor) 3053002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 3054e6bd846dSNan Zhou std::bind_front(sensors::handleSensorGet, std::ref(app))); 305595a3ecadSAnthony Wilson } 305695a3ecadSAnthony Wilson 305708777fb0SLewanczyk, Dawid } // namespace redfish 3058