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 valueKey; 196a0ec28b6SAdrian Ambrożewicz const std::string dbusPath; 197a0ec28b6SAdrian Ambrożewicz }; 198a0ec28b6SAdrian Ambrożewicz 1998a592810SEd Tanous SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 2008d1b46d7Szhanghch05 const std::string& chassisIdIn, 20102da7c5aSEd Tanous std::span<std::string_view> typesIn, 20202da7c5aSEd Tanous std::string_view subNode) : 2038a592810SEd Tanous asyncResp(asyncRespIn), 204928fefb9SNan Zhou chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode), 205928fefb9SNan Zhou efficientExpand(false) 2061214b7e7SGunnar Mills {} 20708777fb0SLewanczyk, Dawid 208a0ec28b6SAdrian Ambrożewicz // Store extra data about sensor mapping and return it in callback 2098a592810SEd Tanous SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 2108d1b46d7Szhanghch05 const std::string& chassisIdIn, 21102da7c5aSEd Tanous std::span<std::string_view> typesIn, 21202da7c5aSEd Tanous std::string_view subNode, 213a0ec28b6SAdrian Ambrożewicz DataCompleteCb&& creationComplete) : 2148a592810SEd Tanous asyncResp(asyncRespIn), 215928fefb9SNan Zhou chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode), 216928fefb9SNan Zhou efficientExpand(false), metadata{std::vector<SensorData>()}, 217a0ec28b6SAdrian Ambrożewicz dataComplete{std::move(creationComplete)} 218a0ec28b6SAdrian Ambrożewicz {} 219a0ec28b6SAdrian Ambrożewicz 220928fefb9SNan Zhou // sensor collections expand 2218a592810SEd Tanous SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 222928fefb9SNan Zhou const std::string& chassisIdIn, 22302da7c5aSEd Tanous const std::span<std::string_view> typesIn, 2248a592810SEd Tanous const std::string_view& subNode, bool efficientExpandIn) : 2258a592810SEd Tanous asyncResp(asyncRespIn), 226928fefb9SNan Zhou chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode), 2278a592810SEd Tanous efficientExpand(efficientExpandIn) 228928fefb9SNan Zhou {} 229928fefb9SNan Zhou 2301abe55efSEd Tanous ~SensorsAsyncResp() 2311abe55efSEd Tanous { 2328d1b46d7Szhanghch05 if (asyncResp->res.result() == 2338d1b46d7Szhanghch05 boost::beast::http::status::internal_server_error) 2341abe55efSEd Tanous { 2351abe55efSEd Tanous // Reset the json object to clear out any data that made it in 2361abe55efSEd Tanous // before the error happened todo(ed) handle error condition with 2371abe55efSEd Tanous // proper code 2388d1b46d7Szhanghch05 asyncResp->res.jsonValue = nlohmann::json::object(); 23908777fb0SLewanczyk, Dawid } 240a0ec28b6SAdrian Ambrożewicz 241a0ec28b6SAdrian Ambrożewicz if (dataComplete && metadata) 242a0ec28b6SAdrian Ambrożewicz { 243fe04d49cSNan Zhou std::map<std::string, std::string> map; 2448d1b46d7Szhanghch05 if (asyncResp->res.result() == boost::beast::http::status::ok) 245a0ec28b6SAdrian Ambrożewicz { 246a0ec28b6SAdrian Ambrożewicz for (auto& sensor : *metadata) 247a0ec28b6SAdrian Ambrożewicz { 248a0ec28b6SAdrian Ambrożewicz map.insert(std::make_pair(sensor.uri + sensor.valueKey, 249a0ec28b6SAdrian Ambrożewicz sensor.dbusPath)); 250a0ec28b6SAdrian Ambrożewicz } 251a0ec28b6SAdrian Ambrożewicz } 2528d1b46d7Szhanghch05 dataComplete(asyncResp->res.result(), map); 253a0ec28b6SAdrian Ambrożewicz } 25408777fb0SLewanczyk, Dawid } 255588c3f0dSKowalski, Kamil 256ecd6a3a2SEd Tanous SensorsAsyncResp(const SensorsAsyncResp&) = delete; 257ecd6a3a2SEd Tanous SensorsAsyncResp(SensorsAsyncResp&&) = delete; 258ecd6a3a2SEd Tanous SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete; 259ecd6a3a2SEd Tanous SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete; 260ecd6a3a2SEd Tanous 261a0ec28b6SAdrian Ambrożewicz void addMetadata(const nlohmann::json& sensorObject, 262a0ec28b6SAdrian Ambrożewicz const std::string& valueKey, const std::string& dbusPath) 263a0ec28b6SAdrian Ambrożewicz { 264a0ec28b6SAdrian Ambrożewicz if (metadata) 265a0ec28b6SAdrian Ambrożewicz { 266a0ec28b6SAdrian Ambrożewicz metadata->emplace_back(SensorData{sensorObject["Name"], 267a0ec28b6SAdrian Ambrożewicz sensorObject["@odata.id"], 268a0ec28b6SAdrian Ambrożewicz valueKey, dbusPath}); 269a0ec28b6SAdrian Ambrożewicz } 270a0ec28b6SAdrian Ambrożewicz } 271a0ec28b6SAdrian Ambrożewicz 272a0ec28b6SAdrian Ambrożewicz void updateUri(const std::string& name, const std::string& uri) 273a0ec28b6SAdrian Ambrożewicz { 274a0ec28b6SAdrian Ambrożewicz if (metadata) 275a0ec28b6SAdrian Ambrożewicz { 276a0ec28b6SAdrian Ambrożewicz for (auto& sensor : *metadata) 277a0ec28b6SAdrian Ambrożewicz { 278a0ec28b6SAdrian Ambrożewicz if (sensor.name == name) 279a0ec28b6SAdrian Ambrożewicz { 280a0ec28b6SAdrian Ambrożewicz sensor.uri = uri; 281a0ec28b6SAdrian Ambrożewicz } 282a0ec28b6SAdrian Ambrożewicz } 283a0ec28b6SAdrian Ambrożewicz } 284a0ec28b6SAdrian Ambrożewicz } 285a0ec28b6SAdrian Ambrożewicz 2868d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp> asyncResp; 287a0ec28b6SAdrian Ambrożewicz const std::string chassisId; 28802da7c5aSEd Tanous const std::span<std::string_view> types; 289a0ec28b6SAdrian Ambrożewicz const std::string chassisSubNode; 290928fefb9SNan Zhou const bool efficientExpand; 291a0ec28b6SAdrian Ambrożewicz 292a0ec28b6SAdrian Ambrożewicz private: 293a0ec28b6SAdrian Ambrożewicz std::optional<std::vector<SensorData>> metadata; 294a0ec28b6SAdrian Ambrożewicz DataCompleteCb dataComplete; 29508777fb0SLewanczyk, Dawid }; 29608777fb0SLewanczyk, Dawid 29708777fb0SLewanczyk, Dawid /** 298d500549bSAnthony Wilson * Possible states for physical inventory leds 299d500549bSAnthony Wilson */ 300d500549bSAnthony Wilson enum class LedState 301d500549bSAnthony Wilson { 302d500549bSAnthony Wilson OFF, 303d500549bSAnthony Wilson ON, 304d500549bSAnthony Wilson BLINK, 305d500549bSAnthony Wilson UNKNOWN 306d500549bSAnthony Wilson }; 307d500549bSAnthony Wilson 308d500549bSAnthony Wilson /** 309adc4f0dbSShawn McCarney * D-Bus inventory item associated with one or more sensors. 310adc4f0dbSShawn McCarney */ 311adc4f0dbSShawn McCarney class InventoryItem 312adc4f0dbSShawn McCarney { 313adc4f0dbSShawn McCarney public: 3144e23a444SEd Tanous explicit InventoryItem(const std::string& objPath) : objectPath(objPath) 315adc4f0dbSShawn McCarney { 316adc4f0dbSShawn McCarney // Set inventory item name to last node of object path 31728aa8de5SGeorge Liu sdbusplus::message::object_path path(objectPath); 31828aa8de5SGeorge Liu name = path.filename(); 31928aa8de5SGeorge Liu if (name.empty()) 320adc4f0dbSShawn McCarney { 32128aa8de5SGeorge Liu BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath; 322adc4f0dbSShawn McCarney } 323adc4f0dbSShawn McCarney } 324adc4f0dbSShawn McCarney 325adc4f0dbSShawn McCarney std::string objectPath; 326adc4f0dbSShawn McCarney std::string name; 327e05aec50SEd Tanous bool isPresent = true; 328e05aec50SEd Tanous bool isFunctional = true; 329e05aec50SEd Tanous bool isPowerSupply = false; 330e05aec50SEd Tanous int powerSupplyEfficiencyPercent = -1; 331adc4f0dbSShawn McCarney std::string manufacturer; 332adc4f0dbSShawn McCarney std::string model; 333adc4f0dbSShawn McCarney std::string partNumber; 334adc4f0dbSShawn McCarney std::string serialNumber; 335adc4f0dbSShawn McCarney std::set<std::string> sensors; 336d500549bSAnthony Wilson std::string ledObjectPath; 337e05aec50SEd Tanous LedState ledState = LedState::UNKNOWN; 338adc4f0dbSShawn McCarney }; 339adc4f0dbSShawn McCarney 340adc4f0dbSShawn McCarney /** 341413961deSRichard Marian Thomaiyar * @brief Get objects with connection necessary for sensors 342588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 34308777fb0SLewanczyk, Dawid * @param sensorNames Sensors retrieved from chassis 34408777fb0SLewanczyk, Dawid * @param callback Callback for processing gathered connections 34508777fb0SLewanczyk, Dawid */ 34608777fb0SLewanczyk, Dawid template <typename Callback> 347413961deSRichard Marian Thomaiyar void getObjectsWithConnection( 34881ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 349fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames, 3501abe55efSEd Tanous Callback&& callback) 3511abe55efSEd Tanous { 352413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter"; 35303b5bae3SJames Feist const std::string path = "/xyz/openbmc_project/sensors"; 35408777fb0SLewanczyk, Dawid const std::array<std::string, 1> interfaces = { 35508777fb0SLewanczyk, Dawid "xyz.openbmc_project.Sensor.Value"}; 35608777fb0SLewanczyk, Dawid 35708777fb0SLewanczyk, Dawid // Response handler for parsing objects subtree 358002d39b4SEd Tanous auto respHandler = 359002d39b4SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 360002d39b4SEd Tanous sensorNames](const boost::system::error_code ec, 361002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 362413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter"; 3631abe55efSEd Tanous if (ec) 3641abe55efSEd Tanous { 3658d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 366413961deSRichard Marian Thomaiyar BMCWEB_LOG_ERROR 367413961deSRichard Marian Thomaiyar << "getObjectsWithConnection resp_handler: Dbus error " << ec; 36808777fb0SLewanczyk, Dawid return; 36908777fb0SLewanczyk, Dawid } 37008777fb0SLewanczyk, Dawid 37155c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees"; 37208777fb0SLewanczyk, Dawid 37308777fb0SLewanczyk, Dawid // Make unique list of connections only for requested sensor types and 37408777fb0SLewanczyk, Dawid // found in the chassis 375fe04d49cSNan Zhou std::set<std::string> connections; 376413961deSRichard Marian Thomaiyar std::set<std::pair<std::string, std::string>> objectsWithConnection; 37708777fb0SLewanczyk, Dawid 37849c53ac9SJohnathan Mantey BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size(); 37949c53ac9SJohnathan Mantey for (const std::string& tsensor : *sensorNames) 3801abe55efSEd Tanous { 38155c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor; 38208777fb0SLewanczyk, Dawid } 38308777fb0SLewanczyk, Dawid 38408777fb0SLewanczyk, Dawid for (const std::pair< 38508777fb0SLewanczyk, Dawid std::string, 38608777fb0SLewanczyk, Dawid std::vector<std::pair<std::string, std::vector<std::string>>>>& 3871abe55efSEd Tanous object : subtree) 3881abe55efSEd Tanous { 38949c53ac9SJohnathan Mantey if (sensorNames->find(object.first) != sensorNames->end()) 3901abe55efSEd Tanous { 39149c53ac9SJohnathan Mantey for (const std::pair<std::string, std::vector<std::string>>& 3921abe55efSEd Tanous objData : object.second) 3931abe55efSEd Tanous { 39449c53ac9SJohnathan Mantey BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first; 39508777fb0SLewanczyk, Dawid connections.insert(objData.first); 396de629b6eSShawn McCarney objectsWithConnection.insert( 397de629b6eSShawn McCarney std::make_pair(object.first, objData.first)); 39808777fb0SLewanczyk, Dawid } 39908777fb0SLewanczyk, Dawid } 40008777fb0SLewanczyk, Dawid } 40155c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections"; 402413961deSRichard Marian Thomaiyar callback(std::move(connections), std::move(objectsWithConnection)); 403413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit"; 40408777fb0SLewanczyk, Dawid }; 40508777fb0SLewanczyk, Dawid // Make call to ObjectMapper to find all sensors objects 40655c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 40755c7b7a2SEd Tanous std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 4081abe55efSEd Tanous "/xyz/openbmc_project/object_mapper", 4091abe55efSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces); 410413961deSRichard Marian Thomaiyar BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit"; 411413961deSRichard Marian Thomaiyar } 412413961deSRichard Marian Thomaiyar 413413961deSRichard Marian Thomaiyar /** 414413961deSRichard Marian Thomaiyar * @brief Create connections necessary for sensors 415413961deSRichard Marian Thomaiyar * @param SensorsAsyncResp Pointer to object holding response data 416413961deSRichard Marian Thomaiyar * @param sensorNames Sensors retrieved from chassis 417413961deSRichard Marian Thomaiyar * @param callback Callback for processing gathered connections 418413961deSRichard Marian Thomaiyar */ 419413961deSRichard Marian Thomaiyar template <typename Callback> 420fe04d49cSNan Zhou void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 421fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> sensorNames, 422413961deSRichard Marian Thomaiyar Callback&& callback) 423413961deSRichard Marian Thomaiyar { 424413961deSRichard Marian Thomaiyar auto objectsWithConnectionCb = 425fe04d49cSNan Zhou [callback](const std::set<std::string>& connections, 426413961deSRichard Marian Thomaiyar const std::set<std::pair<std::string, std::string>>& 4273174e4dfSEd Tanous /*objectsWithConnection*/) { callback(connections); }; 42881ce609eSEd Tanous getObjectsWithConnection(sensorsAsyncResp, sensorNames, 429413961deSRichard Marian Thomaiyar std::move(objectsWithConnectionCb)); 43008777fb0SLewanczyk, Dawid } 43108777fb0SLewanczyk, Dawid 43208777fb0SLewanczyk, Dawid /** 43349c53ac9SJohnathan Mantey * @brief Shrinks the list of sensors for processing 43449c53ac9SJohnathan Mantey * @param SensorsAysncResp The class holding the Redfish response 43549c53ac9SJohnathan Mantey * @param allSensors A list of all the sensors associated to the 43649c53ac9SJohnathan Mantey * chassis element (i.e. baseboard, front panel, etc...) 43749c53ac9SJohnathan Mantey * @param activeSensors A list that is a reduction of the incoming 43849c53ac9SJohnathan Mantey * allSensors list. Eliminate Thermal sensors when a Power request is 43949c53ac9SJohnathan Mantey * made, and eliminate Power sensors when a Thermal request is made. 44049c53ac9SJohnathan Mantey */ 44123a21a1cSEd Tanous inline void reduceSensorList( 442*7f1cc26dSEd Tanous crow::Response& res, std::string_view chassisSubNode, 443*7f1cc26dSEd Tanous std::span<std::string_view> sensorTypes, 44449c53ac9SJohnathan Mantey const std::vector<std::string>* allSensors, 445fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& activeSensors) 44649c53ac9SJohnathan Mantey { 44749c53ac9SJohnathan Mantey if ((allSensors == nullptr) || (activeSensors == nullptr)) 44849c53ac9SJohnathan Mantey { 449*7f1cc26dSEd Tanous messages::resourceNotFound(res, chassisSubNode, 450*7f1cc26dSEd Tanous chassisSubNode == sensors::node::thermal 451a0ec28b6SAdrian Ambrożewicz ? "Temperatures" 45249c53ac9SJohnathan Mantey : "Voltages"); 45349c53ac9SJohnathan Mantey 45449c53ac9SJohnathan Mantey return; 45549c53ac9SJohnathan Mantey } 45649c53ac9SJohnathan Mantey if (allSensors->empty()) 45749c53ac9SJohnathan Mantey { 45849c53ac9SJohnathan Mantey // Nothing to do, the activeSensors object is also empty 45949c53ac9SJohnathan Mantey return; 46049c53ac9SJohnathan Mantey } 46149c53ac9SJohnathan Mantey 462*7f1cc26dSEd Tanous for (std::string_view type : sensorTypes) 46349c53ac9SJohnathan Mantey { 46449c53ac9SJohnathan Mantey for (const std::string& sensor : *allSensors) 46549c53ac9SJohnathan Mantey { 46611ba3979SEd Tanous if (sensor.starts_with(type)) 46749c53ac9SJohnathan Mantey { 46849c53ac9SJohnathan Mantey activeSensors->emplace(sensor); 46949c53ac9SJohnathan Mantey } 47049c53ac9SJohnathan Mantey } 47149c53ac9SJohnathan Mantey } 47249c53ac9SJohnathan Mantey } 47349c53ac9SJohnathan Mantey 474*7f1cc26dSEd Tanous /* 475*7f1cc26dSEd Tanous *Populates the top level collection for a given subnode. Populates 476*7f1cc26dSEd Tanous *SensorCollection, Power, or Thermal schemas. 477*7f1cc26dSEd Tanous * 478*7f1cc26dSEd Tanous * */ 479*7f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue, 480*7f1cc26dSEd Tanous std::string_view chassisSubNode) 481*7f1cc26dSEd Tanous { 482*7f1cc26dSEd Tanous if (chassisSubNode == sensors::node::power) 483*7f1cc26dSEd Tanous { 484*7f1cc26dSEd Tanous jsonValue["@odata.type"] = "#Power.v1_5_2.Power"; 485*7f1cc26dSEd Tanous } 486*7f1cc26dSEd Tanous else if (chassisSubNode == sensors::node::thermal) 487*7f1cc26dSEd Tanous { 488*7f1cc26dSEd Tanous jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal"; 489*7f1cc26dSEd Tanous jsonValue["Fans"] = nlohmann::json::array(); 490*7f1cc26dSEd Tanous jsonValue["Temperatures"] = nlohmann::json::array(); 491*7f1cc26dSEd Tanous } 492*7f1cc26dSEd Tanous else if (chassisSubNode == sensors::node::sensors) 493*7f1cc26dSEd Tanous { 494*7f1cc26dSEd Tanous jsonValue["@odata.type"] = "#SensorCollection.SensorCollection"; 495*7f1cc26dSEd Tanous jsonValue["Description"] = "Collection of Sensors for this Chassis"; 496*7f1cc26dSEd Tanous jsonValue["Members"] = nlohmann::json::array(); 497*7f1cc26dSEd Tanous jsonValue["Members@odata.count"] = 0; 498*7f1cc26dSEd Tanous } 499*7f1cc26dSEd Tanous 500*7f1cc26dSEd Tanous if (chassisSubNode != sensors::node::sensors) 501*7f1cc26dSEd Tanous { 502*7f1cc26dSEd Tanous jsonValue["Id"] = chassisSubNode; 503*7f1cc26dSEd Tanous } 504*7f1cc26dSEd Tanous jsonValue["Name"] = chassisSubNode; 505*7f1cc26dSEd Tanous } 506*7f1cc26dSEd Tanous 50749c53ac9SJohnathan Mantey /** 50808777fb0SLewanczyk, Dawid * @brief Retrieves requested chassis sensors and redundancy data from DBus . 509588c3f0dSKowalski, Kamil * @param SensorsAsyncResp Pointer to object holding response data 51008777fb0SLewanczyk, Dawid * @param callback Callback for next step in gathered sensor processing 51108777fb0SLewanczyk, Dawid */ 51208777fb0SLewanczyk, Dawid template <typename Callback> 513*7f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 514*7f1cc26dSEd Tanous std::string_view chassisId, std::string_view chassisSubNode, 515*7f1cc26dSEd Tanous std::span<std::string_view> sensorTypes, Callback&& callback) 5161abe55efSEd Tanous { 51755c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis enter"; 518adc4f0dbSShawn McCarney const std::array<const char*, 2> interfaces = { 51949c53ac9SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Board", 520adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.Chassis"}; 521002d39b4SEd Tanous auto respHandler = 522*7f1cc26dSEd Tanous [callback{std::forward<Callback>(callback)}, asyncResp, 523*7f1cc26dSEd Tanous chassisIdStr{std::string(chassisId)}, 524*7f1cc26dSEd Tanous chassisSubNode{std::string(chassisSubNode)}, sensorTypes]( 52549c53ac9SJohnathan Mantey const boost::system::error_code ec, 526002d39b4SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) { 52755c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis respHandler enter"; 5281abe55efSEd Tanous if (ec) 5291abe55efSEd Tanous { 53055c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec; 531*7f1cc26dSEd Tanous messages::internalError(asyncResp->res); 53208777fb0SLewanczyk, Dawid return; 53308777fb0SLewanczyk, Dawid } 53449c53ac9SJohnathan Mantey const std::string* chassisPath = nullptr; 53549c53ac9SJohnathan Mantey std::string chassisName; 53649c53ac9SJohnathan Mantey for (const std::string& chassis : chassisPaths) 5371abe55efSEd Tanous { 53828aa8de5SGeorge Liu sdbusplus::message::object_path path(chassis); 53928aa8de5SGeorge Liu chassisName = path.filename(); 54028aa8de5SGeorge Liu if (chassisName.empty()) 5411abe55efSEd Tanous { 54249c53ac9SJohnathan Mantey BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis; 543daf36e2eSEd Tanous continue; 544daf36e2eSEd Tanous } 545*7f1cc26dSEd Tanous if (chassisName == chassisIdStr) 5461abe55efSEd Tanous { 54749c53ac9SJohnathan Mantey chassisPath = &chassis; 54849c53ac9SJohnathan Mantey break; 549daf36e2eSEd Tanous } 55049c53ac9SJohnathan Mantey } 55149c53ac9SJohnathan Mantey if (chassisPath == nullptr) 5521abe55efSEd Tanous { 553*7f1cc26dSEd Tanous messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr); 55449c53ac9SJohnathan Mantey return; 5551abe55efSEd Tanous } 556*7f1cc26dSEd Tanous populateChassisNode(asyncResp->res.jsonValue, chassisSubNode); 55708777fb0SLewanczyk, Dawid 558*7f1cc26dSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 559*7f1cc26dSEd Tanous "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode; 56095a3ecadSAnthony Wilson 5618fb49dd6SShawn McCarney // Get the list of all sensors for this Chassis element 5628fb49dd6SShawn McCarney std::string sensorPath = *chassisPath + "/all_sensors"; 5631e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::vector<std::string>>( 5641e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper", 5651e1e598dSJonathan Doman sensorPath, "xyz.openbmc_project.Association", "endpoints", 566*7f1cc26dSEd Tanous [asyncResp, chassisSubNode, sensorTypes, 567f94c4ecfSEd Tanous callback{std::forward<const Callback>(callback)}]( 568271584abSEd Tanous const boost::system::error_code& e, 5691e1e598dSJonathan Doman const std::vector<std::string>& nodeSensorList) { 570271584abSEd Tanous if (e) 57149c53ac9SJohnathan Mantey { 572271584abSEd Tanous if (e.value() != EBADR) 57349c53ac9SJohnathan Mantey { 574*7f1cc26dSEd Tanous messages::internalError(asyncResp->res); 57549c53ac9SJohnathan Mantey return; 57649c53ac9SJohnathan Mantey } 57749c53ac9SJohnathan Mantey } 578fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> culledSensorList = 579fe04d49cSNan Zhou std::make_shared<std::set<std::string>>(); 580*7f1cc26dSEd Tanous reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes, 581*7f1cc26dSEd Tanous &nodeSensorList, culledSensorList); 582*7f1cc26dSEd Tanous BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size(); 58349c53ac9SJohnathan Mantey callback(culledSensorList); 5841e1e598dSJonathan Doman }); 58549c53ac9SJohnathan Mantey }; 58649c53ac9SJohnathan Mantey 58749c53ac9SJohnathan Mantey // Get the Chassis Collection 58849c53ac9SJohnathan Mantey crow::connections::systemBus->async_method_call( 58949c53ac9SJohnathan Mantey respHandler, "xyz.openbmc_project.ObjectMapper", 59049c53ac9SJohnathan Mantey "/xyz/openbmc_project/object_mapper", 59149c53ac9SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 592271584abSEd Tanous "/xyz/openbmc_project/inventory", 0, interfaces); 59355c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassis exit"; 59408777fb0SLewanczyk, Dawid } 59508777fb0SLewanczyk, Dawid 59608777fb0SLewanczyk, Dawid /** 597de629b6eSShawn McCarney * @brief Finds all DBus object paths that implement ObjectManager. 598de629b6eSShawn McCarney * 599de629b6eSShawn McCarney * Creates a mapping from the associated connection name to the object path. 600de629b6eSShawn McCarney * 601de629b6eSShawn McCarney * Finds the object paths asynchronously. Invokes callback when information has 602de629b6eSShawn McCarney * been obtained. 603de629b6eSShawn McCarney * 604de629b6eSShawn McCarney * The callback must have the following signature: 605de629b6eSShawn McCarney * @code 606fe04d49cSNan Zhou * callback(std::shared_ptr<std::map<std::string,std::string>> objectMgrPaths) 607de629b6eSShawn McCarney * @endcode 608de629b6eSShawn McCarney * 60949c53ac9SJohnathan Mantey * @param sensorsAsyncResp Pointer to object holding response data. 610de629b6eSShawn McCarney * @param callback Callback to invoke when object paths obtained. 611de629b6eSShawn McCarney */ 612de629b6eSShawn McCarney template <typename Callback> 613b5a76932SEd Tanous void getObjectManagerPaths( 61481ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 615de629b6eSShawn McCarney Callback&& callback) 616de629b6eSShawn McCarney { 617de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter"; 618de629b6eSShawn McCarney const std::array<std::string, 1> interfaces = { 619de629b6eSShawn McCarney "org.freedesktop.DBus.ObjectManager"}; 620de629b6eSShawn McCarney 621de629b6eSShawn McCarney // Response handler for GetSubTree DBus method 622002d39b4SEd Tanous auto respHandler = 623002d39b4SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp]( 624b9d36b47SEd Tanous const boost::system::error_code ec, 625002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 626de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter"; 627de629b6eSShawn McCarney if (ec) 628de629b6eSShawn McCarney { 6298d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 630de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error " 631de629b6eSShawn McCarney << ec; 632de629b6eSShawn McCarney return; 633de629b6eSShawn McCarney } 634de629b6eSShawn McCarney 635de629b6eSShawn McCarney // Loop over returned object paths 636fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths = 637fe04d49cSNan Zhou std::make_shared<std::map<std::string, std::string>>(); 638de629b6eSShawn McCarney for (const std::pair< 639de629b6eSShawn McCarney std::string, 640de629b6eSShawn McCarney std::vector<std::pair<std::string, std::vector<std::string>>>>& 641de629b6eSShawn McCarney object : subtree) 642de629b6eSShawn McCarney { 643de629b6eSShawn McCarney // Loop over connections for current object path 644de629b6eSShawn McCarney const std::string& objectPath = object.first; 645de629b6eSShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 646de629b6eSShawn McCarney objData : object.second) 647de629b6eSShawn McCarney { 648de629b6eSShawn McCarney // Add mapping from connection to object path 649de629b6eSShawn McCarney const std::string& connection = objData.first; 6508fb49dd6SShawn McCarney (*objectMgrPaths)[connection] = objectPath; 651de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> " 652de629b6eSShawn McCarney << objectPath; 653de629b6eSShawn McCarney } 654de629b6eSShawn McCarney } 6558fb49dd6SShawn McCarney callback(objectMgrPaths); 656de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit"; 657de629b6eSShawn McCarney }; 658de629b6eSShawn McCarney 659de629b6eSShawn McCarney // Query mapper for all DBus object paths that implement ObjectManager 660de629b6eSShawn McCarney crow::connections::systemBus->async_method_call( 661de629b6eSShawn McCarney std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 662de629b6eSShawn McCarney "/xyz/openbmc_project/object_mapper", 663271584abSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces); 664de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit"; 665de629b6eSShawn McCarney } 666de629b6eSShawn McCarney 667de629b6eSShawn McCarney /** 668adc4f0dbSShawn McCarney * @brief Returns the Redfish State value for the specified inventory item. 669adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with a sensor. 670adc4f0dbSShawn McCarney * @return State value for inventory item. 67134dd179eSJames Feist */ 67223a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem) 673adc4f0dbSShawn McCarney { 674adc4f0dbSShawn McCarney if ((inventoryItem != nullptr) && !(inventoryItem->isPresent)) 675adc4f0dbSShawn McCarney { 676adc4f0dbSShawn McCarney return "Absent"; 677adc4f0dbSShawn McCarney } 67834dd179eSJames Feist 679adc4f0dbSShawn McCarney return "Enabled"; 680adc4f0dbSShawn McCarney } 681adc4f0dbSShawn McCarney 682adc4f0dbSShawn McCarney /** 683adc4f0dbSShawn McCarney * @brief Returns the Redfish Health value for the specified sensor. 684adc4f0dbSShawn McCarney * @param sensorJson Sensor JSON object. 6851d7c0054SEd Tanous * @param valuesDict Map of all sensor DBus values. 686adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with the sensor. Will 687adc4f0dbSShawn McCarney * be nullptr if no associated inventory item was found. 688adc4f0dbSShawn McCarney * @return Health value for sensor. 689adc4f0dbSShawn McCarney */ 6901d7c0054SEd Tanous inline std::string getHealth(nlohmann::json& sensorJson, 6911d7c0054SEd Tanous const dbus::utility::DBusPropertiesMap& valuesDict, 692adc4f0dbSShawn McCarney const InventoryItem* inventoryItem) 69334dd179eSJames Feist { 694adc4f0dbSShawn McCarney // Get current health value (if any) in the sensor JSON object. Some JSON 695adc4f0dbSShawn McCarney // objects contain multiple sensors (such as PowerSupplies). We want to set 696adc4f0dbSShawn McCarney // the overall health to be the most severe of any of the sensors. 697adc4f0dbSShawn McCarney std::string currentHealth; 698adc4f0dbSShawn McCarney auto statusIt = sensorJson.find("Status"); 699adc4f0dbSShawn McCarney if (statusIt != sensorJson.end()) 700adc4f0dbSShawn McCarney { 701adc4f0dbSShawn McCarney auto healthIt = statusIt->find("Health"); 702adc4f0dbSShawn McCarney if (healthIt != statusIt->end()) 703adc4f0dbSShawn McCarney { 704adc4f0dbSShawn McCarney std::string* health = healthIt->get_ptr<std::string*>(); 705adc4f0dbSShawn McCarney if (health != nullptr) 706adc4f0dbSShawn McCarney { 707adc4f0dbSShawn McCarney currentHealth = *health; 708adc4f0dbSShawn McCarney } 709adc4f0dbSShawn McCarney } 710adc4f0dbSShawn McCarney } 711adc4f0dbSShawn McCarney 712adc4f0dbSShawn McCarney // If current health in JSON object is already Critical, return that. This 713adc4f0dbSShawn McCarney // should override the sensor health, which might be less severe. 714adc4f0dbSShawn McCarney if (currentHealth == "Critical") 715adc4f0dbSShawn McCarney { 716adc4f0dbSShawn McCarney return "Critical"; 717adc4f0dbSShawn McCarney } 718adc4f0dbSShawn McCarney 719adc4f0dbSShawn McCarney // Check if sensor has critical threshold alarm 720711ac7a9SEd Tanous 7211d7c0054SEd Tanous for (const auto& [valueName, value] : valuesDict) 72234dd179eSJames Feist { 7231d7c0054SEd Tanous if (valueName == "CriticalAlarmHigh" || valueName == "CriticalAlarmLow") 724711ac7a9SEd Tanous { 725711ac7a9SEd Tanous const bool* asserted = std::get_if<bool>(&value); 72634dd179eSJames Feist if (asserted == nullptr) 72734dd179eSJames Feist { 72834dd179eSJames Feist BMCWEB_LOG_ERROR << "Illegal sensor threshold"; 72934dd179eSJames Feist } 73034dd179eSJames Feist else if (*asserted) 73134dd179eSJames Feist { 73234dd179eSJames Feist return "Critical"; 73334dd179eSJames Feist } 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 750adc4f0dbSShawn McCarney // Check if sensor has warning threshold alarm 7511d7c0054SEd Tanous for (const auto& [valueName, value] : valuesDict) 75234dd179eSJames Feist { 7531d7c0054SEd Tanous if (valueName == "WarningAlarmHigh" || valueName == "WarningAlarmLow") 754711ac7a9SEd Tanous { 755711ac7a9SEd Tanous const bool* asserted = std::get_if<bool>(&value); 75634dd179eSJames Feist if (asserted == nullptr) 75734dd179eSJames Feist { 75834dd179eSJames Feist BMCWEB_LOG_ERROR << "Illegal sensor threshold"; 75934dd179eSJames Feist } 76034dd179eSJames Feist else if (*asserted) 76134dd179eSJames Feist { 762ebe4d91eSEd Tanous return "Warning"; 76334dd179eSJames Feist } 76434dd179eSJames Feist } 76534dd179eSJames Feist } 766adc4f0dbSShawn McCarney 76734dd179eSJames Feist return "OK"; 76834dd179eSJames Feist } 76934dd179eSJames Feist 77023a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson, 771d500549bSAnthony Wilson const InventoryItem* inventoryItem) 772d500549bSAnthony Wilson { 773d500549bSAnthony Wilson if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty()) 774d500549bSAnthony Wilson { 775d500549bSAnthony Wilson switch (inventoryItem->ledState) 776d500549bSAnthony Wilson { 777d500549bSAnthony Wilson case LedState::OFF: 778d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Off"; 779d500549bSAnthony Wilson break; 780d500549bSAnthony Wilson case LedState::ON: 781d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Lit"; 782d500549bSAnthony Wilson break; 783d500549bSAnthony Wilson case LedState::BLINK: 784d500549bSAnthony Wilson sensorJson["IndicatorLED"] = "Blinking"; 785d500549bSAnthony Wilson break; 78623a21a1cSEd Tanous case LedState::UNKNOWN: 787d500549bSAnthony Wilson break; 788d500549bSAnthony Wilson } 789d500549bSAnthony Wilson } 790d500549bSAnthony Wilson } 791d500549bSAnthony Wilson 79234dd179eSJames Feist /** 79308777fb0SLewanczyk, Dawid * @brief Builds a json sensor representation of a sensor. 79408777fb0SLewanczyk, Dawid * @param sensorName The name of the sensor to be built 795274fad5aSGunnar Mills * @param sensorType The type (temperature, fan_tach, etc) of the sensor to 79608777fb0SLewanczyk, Dawid * build 7971d7c0054SEd Tanous * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor 7981d7c0054SEd Tanous * @param propertiesDict A dictionary of the properties to build the sensor 7991d7c0054SEd Tanous * from. 8001d7c0054SEd Tanous * @param sensorJson The json object to fill 801adc4f0dbSShawn McCarney * @param inventoryItem D-Bus inventory item associated with the sensor. Will 802adc4f0dbSShawn McCarney * be nullptr if no associated inventory item was found. 80308777fb0SLewanczyk, Dawid */ 8041d7c0054SEd Tanous inline void objectPropertiesToJson( 8051d7c0054SEd Tanous std::string_view sensorName, std::string_view sensorType, 8061d7c0054SEd Tanous std::string_view chassisSubNode, 8071d7c0054SEd Tanous const dbus::utility::DBusPropertiesMap& propertiesDict, 80881ce609eSEd Tanous nlohmann::json& sensorJson, InventoryItem* inventoryItem) 8091abe55efSEd Tanous { 81008777fb0SLewanczyk, Dawid // Assume values exist as is (10^0 == 1) if no scale exists 81108777fb0SLewanczyk, Dawid int64_t scaleMultiplier = 0; 8121d7c0054SEd Tanous for (const auto& [valueName, value] : propertiesDict) 813711ac7a9SEd Tanous { 814711ac7a9SEd Tanous if (valueName == "Scale") 815711ac7a9SEd Tanous { 816711ac7a9SEd Tanous const int64_t* int64Value = std::get_if<int64_t>(&value); 8171abe55efSEd Tanous if (int64Value != nullptr) 8181abe55efSEd Tanous { 81908777fb0SLewanczyk, Dawid scaleMultiplier = *int64Value; 82008777fb0SLewanczyk, Dawid } 82108777fb0SLewanczyk, Dawid } 822711ac7a9SEd Tanous } 82308777fb0SLewanczyk, Dawid 8241d7c0054SEd Tanous if (chassisSubNode == sensors::node::sensors) 825adc4f0dbSShawn McCarney { 82681ce609eSEd Tanous sensorJson["Id"] = sensorName; 8271d7c0054SEd Tanous std::string sensorNameEs(sensorName); 8281d7c0054SEd Tanous std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' '); 8291d7c0054SEd Tanous sensorJson["Name"] = std::move(sensorNameEs); 83095a3ecadSAnthony Wilson } 83195a3ecadSAnthony Wilson else if (sensorType != "power") 83295a3ecadSAnthony Wilson { 83395a3ecadSAnthony Wilson // Set MemberId and Name for non-power sensors. For PowerSupplies and 83495a3ecadSAnthony Wilson // PowerControl, those properties have more general values because 83595a3ecadSAnthony Wilson // multiple sensors can be stored in the same JSON object. 83681ce609eSEd Tanous sensorJson["MemberId"] = sensorName; 8371d7c0054SEd Tanous std::string sensorNameEs(sensorName); 8381d7c0054SEd Tanous std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' '); 8391d7c0054SEd Tanous sensorJson["Name"] = std::move(sensorNameEs); 840adc4f0dbSShawn McCarney } 841e742b6ccSEd Tanous 84281ce609eSEd Tanous sensorJson["Status"]["State"] = getState(inventoryItem); 84381ce609eSEd Tanous sensorJson["Status"]["Health"] = 8441d7c0054SEd Tanous getHealth(sensorJson, propertiesDict, inventoryItem); 84508777fb0SLewanczyk, Dawid 84608777fb0SLewanczyk, Dawid // Parameter to set to override the type we get from dbus, and force it to 84708777fb0SLewanczyk, Dawid // int, regardless of what is available. This is used for schemas like fan, 84808777fb0SLewanczyk, Dawid // that require integers, not floats. 84908777fb0SLewanczyk, Dawid bool forceToInt = false; 85008777fb0SLewanczyk, Dawid 8513929aca1SAnthony Wilson nlohmann::json::json_pointer unit("/Reading"); 8521d7c0054SEd Tanous if (chassisSubNode == sensors::node::sensors) 85395a3ecadSAnthony Wilson { 8542a4ba195SShounak Mitra sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor"; 855c2bf7f99SWludzik, Jozef 8561d7c0054SEd Tanous std::string_view readingType = sensors::toReadingType(sensorType); 857c2bf7f99SWludzik, Jozef if (readingType.empty()) 85895a3ecadSAnthony Wilson { 859c2bf7f99SWludzik, Jozef BMCWEB_LOG_ERROR << "Redfish cannot map reading type for " 860c2bf7f99SWludzik, Jozef << sensorType; 86195a3ecadSAnthony Wilson } 862c2bf7f99SWludzik, Jozef else 86395a3ecadSAnthony Wilson { 864c2bf7f99SWludzik, Jozef sensorJson["ReadingType"] = readingType; 86595a3ecadSAnthony Wilson } 866c2bf7f99SWludzik, Jozef 8671d7c0054SEd Tanous std::string_view readingUnits = sensors::toReadingUnits(sensorType); 868c2bf7f99SWludzik, Jozef if (readingUnits.empty()) 869f8ede15eSAdrian Ambrożewicz { 870c2bf7f99SWludzik, Jozef BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for " 871c2bf7f99SWludzik, Jozef << sensorType; 872c2bf7f99SWludzik, Jozef } 873c2bf7f99SWludzik, Jozef else 874c2bf7f99SWludzik, Jozef { 875c2bf7f99SWludzik, Jozef sensorJson["ReadingUnits"] = readingUnits; 876f8ede15eSAdrian Ambrożewicz } 87795a3ecadSAnthony Wilson } 87895a3ecadSAnthony Wilson else if (sensorType == "temperature") 8791abe55efSEd Tanous { 8803929aca1SAnthony Wilson unit = "/ReadingCelsius"_json_pointer; 88181ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature"; 88208777fb0SLewanczyk, Dawid // TODO(ed) Documentation says that path should be type fan_tach, 88308777fb0SLewanczyk, Dawid // implementation seems to implement fan 8841abe55efSEd Tanous } 8851abe55efSEd Tanous else if (sensorType == "fan" || sensorType == "fan_tach") 8861abe55efSEd Tanous { 8873929aca1SAnthony Wilson unit = "/Reading"_json_pointer; 88881ce609eSEd Tanous sensorJson["ReadingUnits"] = "RPM"; 88981ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan"; 89081ce609eSEd Tanous setLedState(sensorJson, inventoryItem); 89108777fb0SLewanczyk, Dawid forceToInt = true; 8921abe55efSEd Tanous } 8936f6d0d32SEd Tanous else if (sensorType == "fan_pwm") 8946f6d0d32SEd Tanous { 8953929aca1SAnthony Wilson unit = "/Reading"_json_pointer; 89681ce609eSEd Tanous sensorJson["ReadingUnits"] = "Percent"; 89781ce609eSEd Tanous sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan"; 89881ce609eSEd Tanous setLedState(sensorJson, inventoryItem); 8996f6d0d32SEd Tanous forceToInt = true; 9006f6d0d32SEd Tanous } 9011abe55efSEd Tanous else if (sensorType == "voltage") 9021abe55efSEd Tanous { 9033929aca1SAnthony Wilson unit = "/ReadingVolts"_json_pointer; 90481ce609eSEd Tanous sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage"; 9051abe55efSEd Tanous } 9062474adfaSEd Tanous else if (sensorType == "power") 9072474adfaSEd Tanous { 9081d7c0054SEd Tanous if (boost::iequals(sensorName, "total_power")) 909028f7ebcSEddie James { 91081ce609eSEd Tanous sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl"; 9117ab06f49SGunnar Mills // Put multiple "sensors" into a single PowerControl, so have 9127ab06f49SGunnar Mills // generic names for MemberId and Name. Follows Redfish mockup. 91381ce609eSEd Tanous sensorJson["MemberId"] = "0"; 91481ce609eSEd Tanous sensorJson["Name"] = "Chassis Power Control"; 9153929aca1SAnthony Wilson unit = "/PowerConsumedWatts"_json_pointer; 916028f7ebcSEddie James } 9171d7c0054SEd Tanous else if (boost::ifind_first(sensorName, "input").empty()) 91849c53ac9SJohnathan Mantey { 9193929aca1SAnthony Wilson unit = "/PowerInputWatts"_json_pointer; 92049c53ac9SJohnathan Mantey } 92149c53ac9SJohnathan Mantey else 92249c53ac9SJohnathan Mantey { 9233929aca1SAnthony Wilson unit = "/PowerOutputWatts"_json_pointer; 92449c53ac9SJohnathan Mantey } 9252474adfaSEd Tanous } 9261abe55efSEd Tanous else 9271abe55efSEd Tanous { 92855c7b7a2SEd Tanous BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName; 92908777fb0SLewanczyk, Dawid return; 93008777fb0SLewanczyk, Dawid } 93108777fb0SLewanczyk, Dawid // Map of dbus interface name, dbus property name and redfish property_name 9323929aca1SAnthony Wilson std::vector< 9333929aca1SAnthony Wilson std::tuple<const char*, const char*, nlohmann::json::json_pointer>> 9343929aca1SAnthony Wilson properties; 93508777fb0SLewanczyk, Dawid properties.reserve(7); 93608777fb0SLewanczyk, Dawid 93708777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit); 938de629b6eSShawn McCarney 9391d7c0054SEd Tanous if (chassisSubNode == sensors::node::sensors) 9403929aca1SAnthony Wilson { 9413929aca1SAnthony Wilson properties.emplace_back( 9423929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh", 9433929aca1SAnthony Wilson "/Thresholds/UpperCaution/Reading"_json_pointer); 9443929aca1SAnthony Wilson properties.emplace_back( 9453929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow", 9463929aca1SAnthony Wilson "/Thresholds/LowerCaution/Reading"_json_pointer); 9473929aca1SAnthony Wilson properties.emplace_back( 9483929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh", 9493929aca1SAnthony Wilson "/Thresholds/UpperCritical/Reading"_json_pointer); 9503929aca1SAnthony Wilson properties.emplace_back( 9513929aca1SAnthony Wilson "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow", 9523929aca1SAnthony Wilson "/Thresholds/LowerCritical/Reading"_json_pointer); 9533929aca1SAnthony Wilson } 9543929aca1SAnthony Wilson else if (sensorType != "power") 955de629b6eSShawn McCarney { 95608777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 9573929aca1SAnthony Wilson "WarningHigh", 9583929aca1SAnthony Wilson "/UpperThresholdNonCritical"_json_pointer); 95908777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", 9603929aca1SAnthony Wilson "WarningLow", 9613929aca1SAnthony Wilson "/LowerThresholdNonCritical"_json_pointer); 96208777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 9633929aca1SAnthony Wilson "CriticalHigh", 9643929aca1SAnthony Wilson "/UpperThresholdCritical"_json_pointer); 96508777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", 9663929aca1SAnthony Wilson "CriticalLow", 9673929aca1SAnthony Wilson "/LowerThresholdCritical"_json_pointer); 968de629b6eSShawn McCarney } 96908777fb0SLewanczyk, Dawid 9702474adfaSEd Tanous // TODO Need to get UpperThresholdFatal and LowerThresholdFatal 9712474adfaSEd Tanous 9721d7c0054SEd Tanous if (chassisSubNode == sensors::node::sensors) 97395a3ecadSAnthony Wilson { 97495a3ecadSAnthony Wilson properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 9753929aca1SAnthony Wilson "/ReadingRangeMin"_json_pointer); 97695a3ecadSAnthony Wilson properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 9773929aca1SAnthony Wilson "/ReadingRangeMax"_json_pointer); 97895a3ecadSAnthony Wilson } 97995a3ecadSAnthony Wilson else if (sensorType == "temperature") 9801abe55efSEd Tanous { 98108777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 9823929aca1SAnthony Wilson "/MinReadingRangeTemp"_json_pointer); 98308777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 9843929aca1SAnthony Wilson "/MaxReadingRangeTemp"_json_pointer); 9851abe55efSEd Tanous } 986adc4f0dbSShawn McCarney else if (sensorType != "power") 9871abe55efSEd Tanous { 98808777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", 9893929aca1SAnthony Wilson "/MinReadingRange"_json_pointer); 99008777fb0SLewanczyk, Dawid properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", 9913929aca1SAnthony Wilson "/MaxReadingRange"_json_pointer); 99208777fb0SLewanczyk, Dawid } 99308777fb0SLewanczyk, Dawid 9943929aca1SAnthony Wilson for (const std::tuple<const char*, const char*, 9953929aca1SAnthony Wilson nlohmann::json::json_pointer>& p : properties) 9961abe55efSEd Tanous { 9971d7c0054SEd Tanous for (const auto& [valueName, valueVariant] : propertiesDict) 998711ac7a9SEd Tanous { 999711ac7a9SEd Tanous if (valueName != std::get<1>(p)) 1000711ac7a9SEd Tanous { 1001711ac7a9SEd Tanous continue; 1002711ac7a9SEd Tanous } 10033929aca1SAnthony Wilson 10043929aca1SAnthony Wilson // The property we want to set may be nested json, so use 10053929aca1SAnthony Wilson // a json_pointer for easy indexing into the json structure. 10063929aca1SAnthony Wilson const nlohmann::json::json_pointer& key = std::get<2>(p); 10073929aca1SAnthony Wilson 100808777fb0SLewanczyk, Dawid // Attempt to pull the int64 directly 1009abf2add6SEd Tanous const int64_t* int64Value = std::get_if<int64_t>(&valueVariant); 101008777fb0SLewanczyk, Dawid 1011abf2add6SEd Tanous const double* doubleValue = std::get_if<double>(&valueVariant); 1012028f7ebcSEddie James const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant); 10136f6d0d32SEd Tanous double temp = 0.0; 10146f6d0d32SEd Tanous if (int64Value != nullptr) 10151abe55efSEd Tanous { 1016271584abSEd Tanous temp = static_cast<double>(*int64Value); 10176f6d0d32SEd Tanous } 10186f6d0d32SEd Tanous else if (doubleValue != nullptr) 10191abe55efSEd Tanous { 10206f6d0d32SEd Tanous temp = *doubleValue; 10211abe55efSEd Tanous } 1022028f7ebcSEddie James else if (uValue != nullptr) 1023028f7ebcSEddie James { 1024028f7ebcSEddie James temp = *uValue; 1025028f7ebcSEddie James } 10261abe55efSEd Tanous else 10271abe55efSEd Tanous { 10286f6d0d32SEd Tanous BMCWEB_LOG_ERROR 10296f6d0d32SEd Tanous << "Got value interface that wasn't int or double"; 10306f6d0d32SEd Tanous continue; 103108777fb0SLewanczyk, Dawid } 10326f6d0d32SEd Tanous temp = temp * std::pow(10, scaleMultiplier); 10336f6d0d32SEd Tanous if (forceToInt) 10346f6d0d32SEd Tanous { 103581ce609eSEd Tanous sensorJson[key] = static_cast<int64_t>(temp); 10366f6d0d32SEd Tanous } 10376f6d0d32SEd Tanous else 10386f6d0d32SEd Tanous { 103981ce609eSEd Tanous sensorJson[key] = temp; 104008777fb0SLewanczyk, Dawid } 104108777fb0SLewanczyk, Dawid } 104208777fb0SLewanczyk, Dawid } 1043a0ec28b6SAdrian Ambrożewicz 104455c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "Added sensor " << sensorName; 104508777fb0SLewanczyk, Dawid } 104608777fb0SLewanczyk, Dawid 10471d7c0054SEd Tanous /** 10481d7c0054SEd Tanous * @brief Builds a json sensor representation of a sensor. 10491d7c0054SEd Tanous * @param sensorName The name of the sensor to be built 10501d7c0054SEd Tanous * @param sensorType The type (temperature, fan_tach, etc) of the sensor to 10511d7c0054SEd Tanous * build 10521d7c0054SEd Tanous * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor 10531d7c0054SEd Tanous * @param interfacesDict A dictionary of the interfaces and properties of said 10541d7c0054SEd Tanous * interfaces to be built from 10551d7c0054SEd Tanous * @param sensorJson The json object to fill 10561d7c0054SEd Tanous * @param inventoryItem D-Bus inventory item associated with the sensor. Will 10571d7c0054SEd Tanous * be nullptr if no associated inventory item was found. 10581d7c0054SEd Tanous */ 10591d7c0054SEd Tanous inline void objectInterfacesToJson( 10601d7c0054SEd Tanous const std::string& sensorName, const std::string& sensorType, 10611d7c0054SEd Tanous const std::string& chassisSubNode, 10621d7c0054SEd Tanous const dbus::utility::DBusInteracesMap& interfacesDict, 10631d7c0054SEd Tanous nlohmann::json& sensorJson, InventoryItem* inventoryItem) 10641d7c0054SEd Tanous { 10651d7c0054SEd Tanous 10661d7c0054SEd Tanous for (const auto& [interface, valuesDict] : interfacesDict) 10671d7c0054SEd Tanous { 10681d7c0054SEd Tanous objectPropertiesToJson(sensorName, sensorType, chassisSubNode, 10691d7c0054SEd Tanous valuesDict, sensorJson, inventoryItem); 10701d7c0054SEd Tanous } 10711d7c0054SEd Tanous } 10721d7c0054SEd Tanous 1073b5a76932SEd Tanous inline void populateFanRedundancy( 1074b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 10758bd25ccdSJames Feist { 10768bd25ccdSJames Feist crow::connections::systemBus->async_method_call( 1077b9d36b47SEd Tanous [sensorsAsyncResp]( 1078b9d36b47SEd Tanous const boost::system::error_code ec, 1079b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& resp) { 10808bd25ccdSJames Feist if (ec) 10818bd25ccdSJames Feist { 10828bd25ccdSJames Feist return; // don't have to have this interface 10838bd25ccdSJames Feist } 1084002d39b4SEd Tanous for (const std::pair< 1085002d39b4SEd Tanous std::string, 1086002d39b4SEd Tanous std::vector<std::pair<std::string, std::vector<std::string>>>>& 1087e278c18fSEd Tanous pathPair : resp) 10888bd25ccdSJames Feist { 1089e278c18fSEd Tanous const std::string& path = pathPair.first; 1090002d39b4SEd Tanous const std::vector<std::pair<std::string, std::vector<std::string>>>& 1091002d39b4SEd Tanous objDict = pathPair.second; 10928bd25ccdSJames Feist if (objDict.empty()) 10938bd25ccdSJames Feist { 10948bd25ccdSJames Feist continue; // this should be impossible 10958bd25ccdSJames Feist } 10968bd25ccdSJames Feist 10978bd25ccdSJames Feist const std::string& owner = objDict.begin()->first; 10981e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::vector<std::string>>( 10991e1e598dSJonathan Doman *crow::connections::systemBus, 11001e1e598dSJonathan Doman "xyz.openbmc_project.ObjectMapper", path + "/chassis", 11011e1e598dSJonathan Doman "xyz.openbmc_project.Association", "endpoints", 1102002d39b4SEd Tanous [path, owner, 1103002d39b4SEd Tanous sensorsAsyncResp](const boost::system::error_code e, 11041e1e598dSJonathan Doman const std::vector<std::string>& endpoints) { 1105271584abSEd Tanous if (e) 11068bd25ccdSJames Feist { 11078bd25ccdSJames Feist return; // if they don't have an association we 11088bd25ccdSJames Feist // can't tell what chassis is 11098bd25ccdSJames Feist } 1110002d39b4SEd Tanous auto found = 1111002d39b4SEd Tanous std::find_if(endpoints.begin(), endpoints.end(), 11128bd25ccdSJames Feist [sensorsAsyncResp](const std::string& entry) { 1113002d39b4SEd Tanous return entry.find(sensorsAsyncResp->chassisId) != 11148bd25ccdSJames Feist std::string::npos; 11158bd25ccdSJames Feist }); 11168bd25ccdSJames Feist 11171e1e598dSJonathan Doman if (found == endpoints.end()) 11188bd25ccdSJames Feist { 11198bd25ccdSJames Feist return; 11208bd25ccdSJames Feist } 112186d89ed7SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 112286d89ed7SKrzysztof Grobelny *crow::connections::systemBus, owner, path, 112386d89ed7SKrzysztof Grobelny "xyz.openbmc_project.Control.FanRedundancy", 11248bd25ccdSJames Feist [path, sensorsAsyncResp]( 1125271584abSEd Tanous const boost::system::error_code& err, 112686d89ed7SKrzysztof Grobelny const dbus::utility::DBusPropertiesMap& ret) { 1127271584abSEd Tanous if (err) 11288bd25ccdSJames Feist { 11298bd25ccdSJames Feist return; // don't have to have this 11308bd25ccdSJames Feist // interface 11318bd25ccdSJames Feist } 11328bd25ccdSJames Feist 113386d89ed7SKrzysztof Grobelny const uint8_t* allowedFailures = nullptr; 113486d89ed7SKrzysztof Grobelny const std::vector<std::string>* collection = nullptr; 113586d89ed7SKrzysztof Grobelny const std::string* status = nullptr; 113686d89ed7SKrzysztof Grobelny 113786d89ed7SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 113886d89ed7SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), ret, 113986d89ed7SKrzysztof Grobelny "AllowedFailures", allowedFailures, "Collection", 114086d89ed7SKrzysztof Grobelny collection, "Status", status); 114186d89ed7SKrzysztof Grobelny 114286d89ed7SKrzysztof Grobelny if (!success) 114386d89ed7SKrzysztof Grobelny { 114486d89ed7SKrzysztof Grobelny messages::internalError( 114586d89ed7SKrzysztof Grobelny sensorsAsyncResp->asyncResp->res); 114686d89ed7SKrzysztof Grobelny return; 114786d89ed7SKrzysztof Grobelny } 114886d89ed7SKrzysztof Grobelny 114986d89ed7SKrzysztof Grobelny if (allowedFailures == nullptr || collection == nullptr || 115086d89ed7SKrzysztof Grobelny status == nullptr) 11518bd25ccdSJames Feist { 1152002d39b4SEd Tanous BMCWEB_LOG_ERROR << "Invalid redundancy interface"; 11538bd25ccdSJames Feist messages::internalError( 11548d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 11558bd25ccdSJames Feist return; 11568bd25ccdSJames Feist } 11578bd25ccdSJames Feist 1158002d39b4SEd Tanous sdbusplus::message::object_path objectPath(path); 115928aa8de5SGeorge Liu std::string name = objectPath.filename(); 116028aa8de5SGeorge Liu if (name.empty()) 11618bd25ccdSJames Feist { 11628bd25ccdSJames Feist // this should be impossible 11638bd25ccdSJames Feist messages::internalError( 11648d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 11658bd25ccdSJames Feist return; 11668bd25ccdSJames Feist } 1167002d39b4SEd Tanous std::replace(name.begin(), name.end(), '_', ' '); 11688bd25ccdSJames Feist 11698bd25ccdSJames Feist std::string health; 11708bd25ccdSJames Feist 117111ba3979SEd Tanous if (status->ends_with("Full")) 11728bd25ccdSJames Feist { 11738bd25ccdSJames Feist health = "OK"; 11748bd25ccdSJames Feist } 117511ba3979SEd Tanous else if (status->ends_with("Degraded")) 11768bd25ccdSJames Feist { 11778bd25ccdSJames Feist health = "Warning"; 11788bd25ccdSJames Feist } 11798bd25ccdSJames Feist else 11808bd25ccdSJames Feist { 11818bd25ccdSJames Feist health = "Critical"; 11828bd25ccdSJames Feist } 11831476687dSEd Tanous nlohmann::json::array_t redfishCollection; 11848bd25ccdSJames Feist const auto& fanRedfish = 1185002d39b4SEd Tanous sensorsAsyncResp->asyncResp->res.jsonValue["Fans"]; 11868bd25ccdSJames Feist for (const std::string& item : *collection) 11878bd25ccdSJames Feist { 11888a592810SEd Tanous sdbusplus::message::object_path itemPath(item); 11898a592810SEd Tanous std::string itemName = itemPath.filename(); 119028aa8de5SGeorge Liu if (itemName.empty()) 119128aa8de5SGeorge Liu { 119228aa8de5SGeorge Liu continue; 119328aa8de5SGeorge Liu } 11948bd25ccdSJames Feist /* 11958bd25ccdSJames Feist todo(ed): merge patch that fixes the names 11968bd25ccdSJames Feist std::replace(itemName.begin(), 11978bd25ccdSJames Feist itemName.end(), '_', ' ');*/ 1198002d39b4SEd Tanous auto schemaItem = 1199002d39b4SEd Tanous std::find_if(fanRedfish.begin(), fanRedfish.end(), 12008bd25ccdSJames Feist [itemName](const nlohmann::json& fan) { 12018bd25ccdSJames Feist return fan["MemberId"] == itemName; 12028bd25ccdSJames Feist }); 12038bd25ccdSJames Feist if (schemaItem != fanRedfish.end()) 12048bd25ccdSJames Feist { 12058a592810SEd Tanous nlohmann::json::object_t collectionId; 12068a592810SEd Tanous collectionId["@odata.id"] = 12071476687dSEd Tanous (*schemaItem)["@odata.id"]; 12081476687dSEd Tanous redfishCollection.emplace_back( 12098a592810SEd Tanous std::move(collectionId)); 12108bd25ccdSJames Feist } 12118bd25ccdSJames Feist else 12128bd25ccdSJames Feist { 1213002d39b4SEd Tanous BMCWEB_LOG_ERROR << "failed to find fan in schema"; 12148bd25ccdSJames Feist messages::internalError( 12158d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res); 12168bd25ccdSJames Feist return; 12178bd25ccdSJames Feist } 12188bd25ccdSJames Feist } 12198bd25ccdSJames Feist 12203e9e72ebSKuiying Wang size_t minNumNeeded = 122126f6976fSEd Tanous collection->empty() 122226f6976fSEd Tanous ? 0 122326f6976fSEd Tanous : collection->size() - *allowedFailures; 1224002d39b4SEd Tanous nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res 12258bd25ccdSJames Feist .jsonValue["Redundancy"]; 12261476687dSEd Tanous 12271476687dSEd Tanous nlohmann::json::object_t redundancy; 12281476687dSEd Tanous redundancy["@odata.id"] = 1229002d39b4SEd Tanous "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + 1230002d39b4SEd Tanous "/" + sensorsAsyncResp->chassisSubNode + 1231002d39b4SEd Tanous "#/Redundancy/" + std::to_string(jResp.size()); 1232002d39b4SEd Tanous redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy"; 12331476687dSEd Tanous redundancy["MinNumNeeded"] = minNumNeeded; 12341476687dSEd Tanous redundancy["MemberId"] = name; 12351476687dSEd Tanous redundancy["Mode"] = "N+m"; 12361476687dSEd Tanous redundancy["Name"] = name; 12371476687dSEd Tanous redundancy["RedundancySet"] = redfishCollection; 12381476687dSEd Tanous redundancy["Status"]["Health"] = health; 12391476687dSEd Tanous redundancy["Status"]["State"] = "Enabled"; 12401476687dSEd Tanous 12411476687dSEd Tanous jResp.push_back(std::move(redundancy)); 124286d89ed7SKrzysztof Grobelny }); 12431e1e598dSJonathan Doman }); 12448bd25ccdSJames Feist } 12458bd25ccdSJames Feist }, 12468bd25ccdSJames Feist "xyz.openbmc_project.ObjectMapper", 12478bd25ccdSJames Feist "/xyz/openbmc_project/object_mapper", 12488bd25ccdSJames Feist "xyz.openbmc_project.ObjectMapper", "GetSubTree", 12498bd25ccdSJames Feist "/xyz/openbmc_project/control", 2, 12508bd25ccdSJames Feist std::array<const char*, 1>{ 12518bd25ccdSJames Feist "xyz.openbmc_project.Control.FanRedundancy"}); 12528bd25ccdSJames Feist } 12538bd25ccdSJames Feist 1254b5a76932SEd Tanous inline void 125581ce609eSEd Tanous sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 125649c53ac9SJohnathan Mantey { 12578d1b46d7Szhanghch05 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue; 125849c53ac9SJohnathan Mantey std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"}; 125981ce609eSEd Tanous if (sensorsAsyncResp->chassisSubNode == sensors::node::power) 126049c53ac9SJohnathan Mantey { 126149c53ac9SJohnathan Mantey sensorHeaders = {"Voltages", "PowerSupplies"}; 126249c53ac9SJohnathan Mantey } 126349c53ac9SJohnathan Mantey for (const std::string& sensorGroup : sensorHeaders) 126449c53ac9SJohnathan Mantey { 126549c53ac9SJohnathan Mantey nlohmann::json::iterator entry = response.find(sensorGroup); 126649c53ac9SJohnathan Mantey if (entry != response.end()) 126749c53ac9SJohnathan Mantey { 126849c53ac9SJohnathan Mantey std::sort(entry->begin(), entry->end(), 126902cad96eSEd Tanous [](const nlohmann::json& c1, const nlohmann::json& c2) { 127049c53ac9SJohnathan Mantey return c1["Name"] < c2["Name"]; 127149c53ac9SJohnathan Mantey }); 127249c53ac9SJohnathan Mantey 127349c53ac9SJohnathan Mantey // add the index counts to the end of each entry 127449c53ac9SJohnathan Mantey size_t count = 0; 127549c53ac9SJohnathan Mantey for (nlohmann::json& sensorJson : *entry) 127649c53ac9SJohnathan Mantey { 127749c53ac9SJohnathan Mantey nlohmann::json::iterator odata = sensorJson.find("@odata.id"); 127849c53ac9SJohnathan Mantey if (odata == sensorJson.end()) 127949c53ac9SJohnathan Mantey { 128049c53ac9SJohnathan Mantey continue; 128149c53ac9SJohnathan Mantey } 128249c53ac9SJohnathan Mantey std::string* value = odata->get_ptr<std::string*>(); 128349c53ac9SJohnathan Mantey if (value != nullptr) 128449c53ac9SJohnathan Mantey { 128549c53ac9SJohnathan Mantey *value += std::to_string(count); 128649c53ac9SJohnathan Mantey count++; 128781ce609eSEd Tanous sensorsAsyncResp->updateUri(sensorJson["Name"], *value); 128849c53ac9SJohnathan Mantey } 128949c53ac9SJohnathan Mantey } 129049c53ac9SJohnathan Mantey } 129149c53ac9SJohnathan Mantey } 129249c53ac9SJohnathan Mantey } 129349c53ac9SJohnathan Mantey 129408777fb0SLewanczyk, Dawid /** 1295adc4f0dbSShawn McCarney * @brief Finds the inventory item with the specified object path. 1296adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1297adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 1298adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 12998fb49dd6SShawn McCarney */ 130023a21a1cSEd Tanous inline InventoryItem* findInventoryItem( 1301b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1302adc4f0dbSShawn McCarney const std::string& invItemObjPath) 13038fb49dd6SShawn McCarney { 1304adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 13058fb49dd6SShawn McCarney { 1306adc4f0dbSShawn McCarney if (inventoryItem.objectPath == invItemObjPath) 13078fb49dd6SShawn McCarney { 1308adc4f0dbSShawn McCarney return &inventoryItem; 13098fb49dd6SShawn McCarney } 13108fb49dd6SShawn McCarney } 13118fb49dd6SShawn McCarney return nullptr; 13128fb49dd6SShawn McCarney } 13138fb49dd6SShawn McCarney 13148fb49dd6SShawn McCarney /** 1315adc4f0dbSShawn McCarney * @brief Finds the inventory item associated with the specified sensor. 1316adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1317adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor. 1318adc4f0dbSShawn McCarney * @return Inventory item within vector, or nullptr if no match found. 13198fb49dd6SShawn McCarney */ 132023a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor( 1321b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1322adc4f0dbSShawn McCarney const std::string& sensorObjPath) 1323adc4f0dbSShawn McCarney { 1324adc4f0dbSShawn McCarney for (InventoryItem& inventoryItem : *inventoryItems) 1325adc4f0dbSShawn McCarney { 1326adc4f0dbSShawn McCarney if (inventoryItem.sensors.count(sensorObjPath) > 0) 1327adc4f0dbSShawn McCarney { 1328adc4f0dbSShawn McCarney return &inventoryItem; 1329adc4f0dbSShawn McCarney } 1330adc4f0dbSShawn McCarney } 1331adc4f0dbSShawn McCarney return nullptr; 1332adc4f0dbSShawn McCarney } 1333adc4f0dbSShawn McCarney 1334adc4f0dbSShawn McCarney /** 1335d500549bSAnthony Wilson * @brief Finds the inventory item associated with the specified led path. 1336d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1337d500549bSAnthony Wilson * @param ledObjPath D-Bus object path of led. 1338d500549bSAnthony Wilson * @return Inventory item within vector, or nullptr if no match found. 1339d500549bSAnthony Wilson */ 1340d500549bSAnthony Wilson inline InventoryItem* 1341d500549bSAnthony Wilson findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems, 1342d500549bSAnthony Wilson const std::string& ledObjPath) 1343d500549bSAnthony Wilson { 1344d500549bSAnthony Wilson for (InventoryItem& inventoryItem : inventoryItems) 1345d500549bSAnthony Wilson { 1346d500549bSAnthony Wilson if (inventoryItem.ledObjectPath == ledObjPath) 1347d500549bSAnthony Wilson { 1348d500549bSAnthony Wilson return &inventoryItem; 1349d500549bSAnthony Wilson } 1350d500549bSAnthony Wilson } 1351d500549bSAnthony Wilson return nullptr; 1352d500549bSAnthony Wilson } 1353d500549bSAnthony Wilson 1354d500549bSAnthony Wilson /** 1355adc4f0dbSShawn McCarney * @brief Adds inventory item and associated sensor to specified vector. 1356adc4f0dbSShawn McCarney * 1357adc4f0dbSShawn McCarney * Adds a new InventoryItem to the vector if necessary. Searches for an 1358adc4f0dbSShawn McCarney * existing InventoryItem with the specified object path. If not found, one is 1359adc4f0dbSShawn McCarney * added to the vector. 1360adc4f0dbSShawn McCarney * 1361adc4f0dbSShawn McCarney * Next, the specified sensor is added to the set of sensors associated with the 1362adc4f0dbSShawn McCarney * InventoryItem. 1363adc4f0dbSShawn McCarney * 1364adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1365adc4f0dbSShawn McCarney * @param invItemObjPath D-Bus object path of inventory item. 1366adc4f0dbSShawn McCarney * @param sensorObjPath D-Bus object path of sensor 1367adc4f0dbSShawn McCarney */ 1368b5a76932SEd Tanous inline void addInventoryItem( 1369b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 1370b5a76932SEd Tanous const std::string& invItemObjPath, const std::string& sensorObjPath) 1371adc4f0dbSShawn McCarney { 1372adc4f0dbSShawn McCarney // Look for inventory item in vector 1373adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1374adc4f0dbSShawn McCarney findInventoryItem(inventoryItems, invItemObjPath); 1375adc4f0dbSShawn McCarney 1376adc4f0dbSShawn McCarney // If inventory item doesn't exist in vector, add it 1377adc4f0dbSShawn McCarney if (inventoryItem == nullptr) 1378adc4f0dbSShawn McCarney { 1379adc4f0dbSShawn McCarney inventoryItems->emplace_back(invItemObjPath); 1380adc4f0dbSShawn McCarney inventoryItem = &(inventoryItems->back()); 1381adc4f0dbSShawn McCarney } 1382adc4f0dbSShawn McCarney 1383adc4f0dbSShawn McCarney // Add sensor to set of sensors associated with inventory item 1384adc4f0dbSShawn McCarney inventoryItem->sensors.emplace(sensorObjPath); 1385adc4f0dbSShawn McCarney } 1386adc4f0dbSShawn McCarney 1387adc4f0dbSShawn McCarney /** 1388adc4f0dbSShawn McCarney * @brief Stores D-Bus data in the specified inventory item. 1389adc4f0dbSShawn McCarney * 1390adc4f0dbSShawn McCarney * Finds D-Bus data in the specified map of interfaces. Stores the data in the 1391adc4f0dbSShawn McCarney * specified InventoryItem. 1392adc4f0dbSShawn McCarney * 1393adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1394adc4f0dbSShawn McCarney * response. 1395adc4f0dbSShawn McCarney * 1396adc4f0dbSShawn McCarney * @param inventoryItem Inventory item where data will be stored. 1397adc4f0dbSShawn McCarney * @param interfacesDict Map containing D-Bus interfaces and their properties 1398adc4f0dbSShawn McCarney * for the specified inventory item. 1399adc4f0dbSShawn McCarney */ 140023a21a1cSEd Tanous inline void storeInventoryItemData( 1401adc4f0dbSShawn McCarney InventoryItem& inventoryItem, 1402711ac7a9SEd Tanous const dbus::utility::DBusInteracesMap& interfacesDict) 14038fb49dd6SShawn McCarney { 1404adc4f0dbSShawn McCarney // Get properties from Inventory.Item interface 1405711ac7a9SEd Tanous 14069eb808c1SEd Tanous for (const auto& [interface, values] : interfacesDict) 14078fb49dd6SShawn McCarney { 1408711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Item") 14098fb49dd6SShawn McCarney { 14109eb808c1SEd Tanous for (const auto& [name, dbusValue] : values) 1411711ac7a9SEd Tanous { 1412711ac7a9SEd Tanous if (name == "Present") 1413711ac7a9SEd Tanous { 1414711ac7a9SEd Tanous const bool* value = std::get_if<bool>(&dbusValue); 1415adc4f0dbSShawn McCarney if (value != nullptr) 14168fb49dd6SShawn McCarney { 1417adc4f0dbSShawn McCarney inventoryItem.isPresent = *value; 14188fb49dd6SShawn McCarney } 14198fb49dd6SShawn McCarney } 14208fb49dd6SShawn McCarney } 1421711ac7a9SEd Tanous } 1422adc4f0dbSShawn McCarney // Check if Inventory.Item.PowerSupply interface is present 1423711ac7a9SEd Tanous 1424711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply") 14258fb49dd6SShawn McCarney { 1426adc4f0dbSShawn McCarney inventoryItem.isPowerSupply = true; 14278fb49dd6SShawn McCarney } 1428adc4f0dbSShawn McCarney 1429adc4f0dbSShawn McCarney // Get properties from Inventory.Decorator.Asset interface 1430711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset") 1431adc4f0dbSShawn McCarney { 14329eb808c1SEd Tanous for (const auto& [name, dbusValue] : values) 1433711ac7a9SEd Tanous { 1434711ac7a9SEd Tanous if (name == "Manufacturer") 1435adc4f0dbSShawn McCarney { 1436adc4f0dbSShawn McCarney const std::string* value = 1437711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1438adc4f0dbSShawn McCarney if (value != nullptr) 1439adc4f0dbSShawn McCarney { 1440adc4f0dbSShawn McCarney inventoryItem.manufacturer = *value; 1441adc4f0dbSShawn McCarney } 1442adc4f0dbSShawn McCarney } 1443711ac7a9SEd Tanous if (name == "Model") 1444adc4f0dbSShawn McCarney { 1445adc4f0dbSShawn McCarney const std::string* value = 1446711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1447adc4f0dbSShawn McCarney if (value != nullptr) 1448adc4f0dbSShawn McCarney { 1449adc4f0dbSShawn McCarney inventoryItem.model = *value; 1450adc4f0dbSShawn McCarney } 1451adc4f0dbSShawn McCarney } 1452711ac7a9SEd Tanous if (name == "SerialNumber") 1453adc4f0dbSShawn McCarney { 1454adc4f0dbSShawn McCarney const std::string* value = 1455711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1456adc4f0dbSShawn McCarney if (value != nullptr) 1457adc4f0dbSShawn McCarney { 1458adc4f0dbSShawn McCarney inventoryItem.serialNumber = *value; 1459adc4f0dbSShawn McCarney } 1460adc4f0dbSShawn McCarney } 1461711ac7a9SEd Tanous if (name == "PartNumber") 1462711ac7a9SEd Tanous { 1463711ac7a9SEd Tanous const std::string* value = 1464711ac7a9SEd Tanous std::get_if<std::string>(&dbusValue); 1465711ac7a9SEd Tanous if (value != nullptr) 1466711ac7a9SEd Tanous { 1467711ac7a9SEd Tanous inventoryItem.partNumber = *value; 1468711ac7a9SEd Tanous } 1469711ac7a9SEd Tanous } 1470711ac7a9SEd Tanous } 1471adc4f0dbSShawn McCarney } 1472adc4f0dbSShawn McCarney 1473711ac7a9SEd Tanous if (interface == 1474711ac7a9SEd Tanous "xyz.openbmc_project.State.Decorator.OperationalStatus") 1475adc4f0dbSShawn McCarney { 14769eb808c1SEd Tanous for (const auto& [name, dbusValue] : values) 1477adc4f0dbSShawn McCarney { 1478711ac7a9SEd Tanous if (name == "Functional") 1479711ac7a9SEd Tanous { 1480711ac7a9SEd Tanous const bool* value = std::get_if<bool>(&dbusValue); 1481adc4f0dbSShawn McCarney if (value != nullptr) 1482adc4f0dbSShawn McCarney { 1483adc4f0dbSShawn McCarney inventoryItem.isFunctional = *value; 14848fb49dd6SShawn McCarney } 14858fb49dd6SShawn McCarney } 14868fb49dd6SShawn McCarney } 14878fb49dd6SShawn McCarney } 1488711ac7a9SEd Tanous } 1489711ac7a9SEd Tanous } 14908fb49dd6SShawn McCarney 14918fb49dd6SShawn McCarney /** 1492adc4f0dbSShawn McCarney * @brief Gets D-Bus data for inventory items associated with sensors. 14938fb49dd6SShawn McCarney * 1494adc4f0dbSShawn McCarney * Uses the specified connections (services) to obtain D-Bus data for inventory 1495adc4f0dbSShawn McCarney * items associated with sensors. Stores the resulting data in the 1496adc4f0dbSShawn McCarney * inventoryItems vector. 14978fb49dd6SShawn McCarney * 1498adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 1499adc4f0dbSShawn McCarney * response. 1500adc4f0dbSShawn McCarney * 1501adc4f0dbSShawn McCarney * Finds the inventory item data asynchronously. Invokes callback when data has 1502adc4f0dbSShawn McCarney * been obtained. 1503adc4f0dbSShawn McCarney * 1504adc4f0dbSShawn McCarney * The callback must have the following signature: 1505adc4f0dbSShawn McCarney * @code 1506d500549bSAnthony Wilson * callback(void) 1507adc4f0dbSShawn McCarney * @endcode 1508adc4f0dbSShawn McCarney * 1509adc4f0dbSShawn McCarney * This function is called recursively, obtaining data asynchronously from one 1510adc4f0dbSShawn McCarney * connection in each call. This ensures the callback is not invoked until the 1511adc4f0dbSShawn McCarney * last asynchronous function has completed. 15128fb49dd6SShawn McCarney * 15138fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1514adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 1515adc4f0dbSShawn McCarney * @param invConnections Connections that provide data for the inventory items. 15168fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 15178fb49dd6SShawn McCarney * implements ObjectManager. 1518adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory data has been obtained. 1519adc4f0dbSShawn McCarney * @param invConnectionsIndex Current index in invConnections. Only specified 1520adc4f0dbSShawn McCarney * in recursive calls to this function. 15218fb49dd6SShawn McCarney */ 1522adc4f0dbSShawn McCarney template <typename Callback> 1523adc4f0dbSShawn McCarney static void getInventoryItemsData( 15248fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1525adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1526fe04d49cSNan Zhou std::shared_ptr<std::set<std::string>> invConnections, 1527fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths, 1528271584abSEd Tanous Callback&& callback, size_t invConnectionsIndex = 0) 15298fb49dd6SShawn McCarney { 1530adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData enter"; 15318fb49dd6SShawn McCarney 1532adc4f0dbSShawn McCarney // If no more connections left, call callback 1533adc4f0dbSShawn McCarney if (invConnectionsIndex >= invConnections->size()) 15348fb49dd6SShawn McCarney { 1535d500549bSAnthony Wilson callback(); 1536adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData exit"; 1537adc4f0dbSShawn McCarney return; 1538adc4f0dbSShawn McCarney } 1539adc4f0dbSShawn McCarney 1540adc4f0dbSShawn McCarney // Get inventory item data from current connection 1541fe04d49cSNan Zhou auto it = invConnections->begin(); 1542fe04d49cSNan Zhou std::advance(it, invConnectionsIndex); 1543adc4f0dbSShawn McCarney if (it != invConnections->end()) 1544adc4f0dbSShawn McCarney { 1545adc4f0dbSShawn McCarney const std::string& invConnection = *it; 1546adc4f0dbSShawn McCarney 15478fb49dd6SShawn McCarney // Response handler for GetManagedObjects 1548002d39b4SEd Tanous auto respHandler = 1549002d39b4SEd Tanous [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths, 155002cad96eSEd Tanous callback{std::forward<Callback>(callback)}, invConnectionsIndex]( 155102cad96eSEd Tanous const boost::system::error_code ec, 155202cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 1553adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter"; 15548fb49dd6SShawn McCarney if (ec) 15558fb49dd6SShawn McCarney { 15568fb49dd6SShawn McCarney BMCWEB_LOG_ERROR 1557adc4f0dbSShawn McCarney << "getInventoryItemsData respHandler DBus error " << ec; 15588d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 15598fb49dd6SShawn McCarney return; 15608fb49dd6SShawn McCarney } 15618fb49dd6SShawn McCarney 15628fb49dd6SShawn McCarney // Loop through returned object paths 15638fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 15648fb49dd6SShawn McCarney { 15658fb49dd6SShawn McCarney const std::string& objPath = 15668fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 15678fb49dd6SShawn McCarney 1568adc4f0dbSShawn McCarney // If this object path is one of the specified inventory items 1569adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 1570adc4f0dbSShawn McCarney findInventoryItem(inventoryItems, objPath); 1571adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 15728fb49dd6SShawn McCarney { 1573adc4f0dbSShawn McCarney // Store inventory data in InventoryItem 1574adc4f0dbSShawn McCarney storeInventoryItemData(*inventoryItem, objDictEntry.second); 15758fb49dd6SShawn McCarney } 15768fb49dd6SShawn McCarney } 15778fb49dd6SShawn McCarney 1578adc4f0dbSShawn McCarney // Recurse to get inventory item data from next connection 1579adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 1580adc4f0dbSShawn McCarney invConnections, objectMgrPaths, 1581adc4f0dbSShawn McCarney std::move(callback), invConnectionsIndex + 1); 1582adc4f0dbSShawn McCarney 1583adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit"; 15848fb49dd6SShawn McCarney }; 15858fb49dd6SShawn McCarney 15868fb49dd6SShawn McCarney // Find DBus object path that implements ObjectManager for the current 15878fb49dd6SShawn McCarney // connection. If no mapping found, default to "/". 15888fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(invConnection); 15898fb49dd6SShawn McCarney const std::string& objectMgrPath = 15908fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 15918fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is " 15928fb49dd6SShawn McCarney << objectMgrPath; 15938fb49dd6SShawn McCarney 15948fb49dd6SShawn McCarney // Get all object paths and their interfaces for current connection 15958fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 15968fb49dd6SShawn McCarney std::move(respHandler), invConnection, objectMgrPath, 15978fb49dd6SShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 15988fb49dd6SShawn McCarney } 15998fb49dd6SShawn McCarney 1600adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsData exit"; 16018fb49dd6SShawn McCarney } 16028fb49dd6SShawn McCarney 16038fb49dd6SShawn McCarney /** 1604adc4f0dbSShawn McCarney * @brief Gets connections that provide D-Bus data for inventory items. 16058fb49dd6SShawn McCarney * 1606adc4f0dbSShawn McCarney * Gets the D-Bus connections (services) that provide data for the inventory 1607adc4f0dbSShawn McCarney * items that are associated with sensors. 16088fb49dd6SShawn McCarney * 16098fb49dd6SShawn McCarney * Finds the connections asynchronously. Invokes callback when information has 16108fb49dd6SShawn McCarney * been obtained. 16118fb49dd6SShawn McCarney * 16128fb49dd6SShawn McCarney * The callback must have the following signature: 16138fb49dd6SShawn McCarney * @code 1614fe04d49cSNan Zhou * callback(std::shared_ptr<std::set<std::string>> invConnections) 16158fb49dd6SShawn McCarney * @endcode 16168fb49dd6SShawn McCarney * 16178fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 1618adc4f0dbSShawn McCarney * @param inventoryItems D-Bus inventory items associated with sensors. 16198fb49dd6SShawn McCarney * @param callback Callback to invoke when connections have been obtained. 16208fb49dd6SShawn McCarney */ 16218fb49dd6SShawn McCarney template <typename Callback> 16228fb49dd6SShawn McCarney static void getInventoryItemsConnections( 1623b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1624b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, 16258fb49dd6SShawn McCarney Callback&& callback) 16268fb49dd6SShawn McCarney { 16278fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter"; 16288fb49dd6SShawn McCarney 16298fb49dd6SShawn McCarney const std::string path = "/xyz/openbmc_project/inventory"; 1630adc4f0dbSShawn McCarney const std::array<std::string, 4> interfaces = { 16318fb49dd6SShawn McCarney "xyz.openbmc_project.Inventory.Item", 1632adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Item.PowerSupply", 1633adc4f0dbSShawn McCarney "xyz.openbmc_project.Inventory.Decorator.Asset", 16348fb49dd6SShawn McCarney "xyz.openbmc_project.State.Decorator.OperationalStatus"}; 16358fb49dd6SShawn McCarney 16368fb49dd6SShawn McCarney // Response handler for parsing output from GetSubTree 1637002d39b4SEd Tanous auto respHandler = 1638002d39b4SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 1639002d39b4SEd Tanous inventoryItems]( 1640b9d36b47SEd Tanous const boost::system::error_code ec, 1641002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 16428fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter"; 16438fb49dd6SShawn McCarney if (ec) 16448fb49dd6SShawn McCarney { 16458d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 16468fb49dd6SShawn McCarney BMCWEB_LOG_ERROR 16478fb49dd6SShawn McCarney << "getInventoryItemsConnections respHandler DBus error " << ec; 16488fb49dd6SShawn McCarney return; 16498fb49dd6SShawn McCarney } 16508fb49dd6SShawn McCarney 16518fb49dd6SShawn McCarney // Make unique list of connections for desired inventory items 1652fe04d49cSNan Zhou std::shared_ptr<std::set<std::string>> invConnections = 1653fe04d49cSNan Zhou std::make_shared<std::set<std::string>>(); 16548fb49dd6SShawn McCarney 16558fb49dd6SShawn McCarney // Loop through objects from GetSubTree 16568fb49dd6SShawn McCarney for (const std::pair< 16578fb49dd6SShawn McCarney std::string, 16588fb49dd6SShawn McCarney std::vector<std::pair<std::string, std::vector<std::string>>>>& 16598fb49dd6SShawn McCarney object : subtree) 16608fb49dd6SShawn McCarney { 1661adc4f0dbSShawn McCarney // Check if object path is one of the specified inventory items 16628fb49dd6SShawn McCarney const std::string& objPath = object.first; 1663adc4f0dbSShawn McCarney if (findInventoryItem(inventoryItems, objPath) != nullptr) 16648fb49dd6SShawn McCarney { 16658fb49dd6SShawn McCarney // Store all connections to inventory item 16668fb49dd6SShawn McCarney for (const std::pair<std::string, std::vector<std::string>>& 16678fb49dd6SShawn McCarney objData : object.second) 16688fb49dd6SShawn McCarney { 16698fb49dd6SShawn McCarney const std::string& invConnection = objData.first; 16708fb49dd6SShawn McCarney invConnections->insert(invConnection); 16718fb49dd6SShawn McCarney } 16728fb49dd6SShawn McCarney } 16738fb49dd6SShawn McCarney } 1674d500549bSAnthony Wilson 16758fb49dd6SShawn McCarney callback(invConnections); 16768fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit"; 16778fb49dd6SShawn McCarney }; 16788fb49dd6SShawn McCarney 16798fb49dd6SShawn McCarney // Make call to ObjectMapper to find all inventory items 16808fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 16818fb49dd6SShawn McCarney std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 16828fb49dd6SShawn McCarney "/xyz/openbmc_project/object_mapper", 16838fb49dd6SShawn McCarney "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); 16848fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit"; 16858fb49dd6SShawn McCarney } 16868fb49dd6SShawn McCarney 16878fb49dd6SShawn McCarney /** 1688adc4f0dbSShawn McCarney * @brief Gets associations from sensors to inventory items. 16898fb49dd6SShawn McCarney * 16908fb49dd6SShawn McCarney * Looks for ObjectMapper associations from the specified sensors to related 1691d500549bSAnthony Wilson * inventory items. Then finds the associations from those inventory items to 1692d500549bSAnthony Wilson * their LEDs, if any. 16938fb49dd6SShawn McCarney * 16948fb49dd6SShawn McCarney * Finds the inventory items asynchronously. Invokes callback when information 16958fb49dd6SShawn McCarney * has been obtained. 16968fb49dd6SShawn McCarney * 16978fb49dd6SShawn McCarney * The callback must have the following signature: 16988fb49dd6SShawn McCarney * @code 1699adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 17008fb49dd6SShawn McCarney * @endcode 17018fb49dd6SShawn McCarney * 17028fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 17038fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 17048fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 17058fb49dd6SShawn McCarney * implements ObjectManager. 17068fb49dd6SShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 17078fb49dd6SShawn McCarney */ 17088fb49dd6SShawn McCarney template <typename Callback> 1709adc4f0dbSShawn McCarney static void getInventoryItemAssociations( 1710b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 1711fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames, 1712fe04d49cSNan Zhou const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths, 17138fb49dd6SShawn McCarney Callback&& callback) 17148fb49dd6SShawn McCarney { 1715adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter"; 17168fb49dd6SShawn McCarney 17178fb49dd6SShawn McCarney // Response handler for GetManagedObjects 171802cad96eSEd Tanous auto respHandler = 171902cad96eSEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 17208fb49dd6SShawn McCarney sensorNames](const boost::system::error_code ec, 172102cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 1722adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter"; 17238fb49dd6SShawn McCarney if (ec) 17248fb49dd6SShawn McCarney { 1725adc4f0dbSShawn McCarney BMCWEB_LOG_ERROR 1726adc4f0dbSShawn McCarney << "getInventoryItemAssociations respHandler DBus error " << ec; 17278d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 17288fb49dd6SShawn McCarney return; 17298fb49dd6SShawn McCarney } 17308fb49dd6SShawn McCarney 1731adc4f0dbSShawn McCarney // Create vector to hold list of inventory items 1732adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems = 1733adc4f0dbSShawn McCarney std::make_shared<std::vector<InventoryItem>>(); 1734adc4f0dbSShawn McCarney 17358fb49dd6SShawn McCarney // Loop through returned object paths 17368fb49dd6SShawn McCarney std::string sensorAssocPath; 17378fb49dd6SShawn McCarney sensorAssocPath.reserve(128); // avoid memory allocations 17388fb49dd6SShawn McCarney for (const auto& objDictEntry : resp) 17398fb49dd6SShawn McCarney { 17408fb49dd6SShawn McCarney const std::string& objPath = 17418fb49dd6SShawn McCarney static_cast<const std::string&>(objDictEntry.first); 17428fb49dd6SShawn McCarney 17438fb49dd6SShawn McCarney // If path is inventory association for one of the specified sensors 17448fb49dd6SShawn McCarney for (const std::string& sensorName : *sensorNames) 17458fb49dd6SShawn McCarney { 17468fb49dd6SShawn McCarney sensorAssocPath = sensorName; 17478fb49dd6SShawn McCarney sensorAssocPath += "/inventory"; 17488fb49dd6SShawn McCarney if (objPath == sensorAssocPath) 17498fb49dd6SShawn McCarney { 17508fb49dd6SShawn McCarney // Get Association interface for object path 1751711ac7a9SEd Tanous for (const auto& [interface, values] : objDictEntry.second) 17528fb49dd6SShawn McCarney { 1753711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Association") 1754711ac7a9SEd Tanous { 1755711ac7a9SEd Tanous for (const auto& [valueName, value] : values) 1756711ac7a9SEd Tanous { 1757711ac7a9SEd Tanous if (valueName == "endpoints") 17588fb49dd6SShawn McCarney { 17598fb49dd6SShawn McCarney const std::vector<std::string>* endpoints = 17608fb49dd6SShawn McCarney std::get_if<std::vector<std::string>>( 1761711ac7a9SEd Tanous &value); 1762711ac7a9SEd Tanous if ((endpoints != nullptr) && 1763711ac7a9SEd Tanous !endpoints->empty()) 17648fb49dd6SShawn McCarney { 1765adc4f0dbSShawn McCarney // Add inventory item to vector 1766adc4f0dbSShawn McCarney const std::string& invItemPath = 1767adc4f0dbSShawn McCarney endpoints->front(); 1768711ac7a9SEd Tanous addInventoryItem(inventoryItems, 1769711ac7a9SEd Tanous invItemPath, 1770adc4f0dbSShawn McCarney sensorName); 17718fb49dd6SShawn McCarney } 17728fb49dd6SShawn McCarney } 17738fb49dd6SShawn McCarney } 1774711ac7a9SEd Tanous } 1775711ac7a9SEd Tanous } 17768fb49dd6SShawn McCarney break; 17778fb49dd6SShawn McCarney } 17788fb49dd6SShawn McCarney } 17798fb49dd6SShawn McCarney } 17808fb49dd6SShawn McCarney 1781d500549bSAnthony Wilson // Now loop through the returned object paths again, this time to 1782d500549bSAnthony Wilson // find the leds associated with the inventory items we just found 1783d500549bSAnthony Wilson std::string inventoryAssocPath; 1784d500549bSAnthony Wilson inventoryAssocPath.reserve(128); // avoid memory allocations 1785d500549bSAnthony Wilson for (const auto& objDictEntry : resp) 1786d500549bSAnthony Wilson { 1787d500549bSAnthony Wilson const std::string& objPath = 1788d500549bSAnthony Wilson static_cast<const std::string&>(objDictEntry.first); 1789d500549bSAnthony Wilson 1790d500549bSAnthony Wilson for (InventoryItem& inventoryItem : *inventoryItems) 1791d500549bSAnthony Wilson { 1792d500549bSAnthony Wilson inventoryAssocPath = inventoryItem.objectPath; 1793d500549bSAnthony Wilson inventoryAssocPath += "/leds"; 1794d500549bSAnthony Wilson if (objPath == inventoryAssocPath) 1795d500549bSAnthony Wilson { 1796711ac7a9SEd Tanous for (const auto& [interface, values] : objDictEntry.second) 1797d500549bSAnthony Wilson { 1798711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.Association") 1799711ac7a9SEd Tanous { 1800711ac7a9SEd Tanous for (const auto& [valueName, value] : values) 1801711ac7a9SEd Tanous { 1802711ac7a9SEd Tanous if (valueName == "endpoints") 1803d500549bSAnthony Wilson { 1804d500549bSAnthony Wilson const std::vector<std::string>* endpoints = 1805d500549bSAnthony Wilson std::get_if<std::vector<std::string>>( 1806711ac7a9SEd Tanous &value); 1807711ac7a9SEd Tanous if ((endpoints != nullptr) && 1808711ac7a9SEd Tanous !endpoints->empty()) 1809d500549bSAnthony Wilson { 1810711ac7a9SEd Tanous // Add inventory item to vector 1811d500549bSAnthony Wilson // Store LED path in inventory item 1812711ac7a9SEd Tanous const std::string& ledPath = 1813711ac7a9SEd Tanous endpoints->front(); 1814d500549bSAnthony Wilson inventoryItem.ledObjectPath = ledPath; 1815d500549bSAnthony Wilson } 1816d500549bSAnthony Wilson } 1817d500549bSAnthony Wilson } 1818711ac7a9SEd Tanous } 1819711ac7a9SEd Tanous } 1820711ac7a9SEd Tanous 1821d500549bSAnthony Wilson break; 1822d500549bSAnthony Wilson } 1823d500549bSAnthony Wilson } 1824d500549bSAnthony Wilson } 1825adc4f0dbSShawn McCarney callback(inventoryItems); 1826adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit"; 18278fb49dd6SShawn McCarney }; 18288fb49dd6SShawn McCarney 18298fb49dd6SShawn McCarney // Find DBus object path that implements ObjectManager for ObjectMapper 18308fb49dd6SShawn McCarney std::string connection = "xyz.openbmc_project.ObjectMapper"; 18318fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(connection); 18328fb49dd6SShawn McCarney const std::string& objectMgrPath = 18338fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 18348fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is " 18358fb49dd6SShawn McCarney << objectMgrPath; 18368fb49dd6SShawn McCarney 18378fb49dd6SShawn McCarney // Call GetManagedObjects on the ObjectMapper to get all associations 18388fb49dd6SShawn McCarney crow::connections::systemBus->async_method_call( 18398fb49dd6SShawn McCarney std::move(respHandler), connection, objectMgrPath, 18408fb49dd6SShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 18418fb49dd6SShawn McCarney 1842adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit"; 18438fb49dd6SShawn McCarney } 18448fb49dd6SShawn McCarney 18458fb49dd6SShawn McCarney /** 1846d500549bSAnthony Wilson * @brief Gets D-Bus data for inventory item leds associated with sensors. 1847d500549bSAnthony Wilson * 1848d500549bSAnthony Wilson * Uses the specified connections (services) to obtain D-Bus data for inventory 1849d500549bSAnthony Wilson * item leds associated with sensors. Stores the resulting data in the 1850d500549bSAnthony Wilson * inventoryItems vector. 1851d500549bSAnthony Wilson * 1852d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 1853d500549bSAnthony Wilson * response. 1854d500549bSAnthony Wilson * 1855d500549bSAnthony Wilson * Finds the inventory item led data asynchronously. Invokes callback when data 1856d500549bSAnthony Wilson * has been obtained. 1857d500549bSAnthony Wilson * 1858d500549bSAnthony Wilson * The callback must have the following signature: 1859d500549bSAnthony Wilson * @code 186042cbe538SGunnar Mills * callback() 1861d500549bSAnthony Wilson * @endcode 1862d500549bSAnthony Wilson * 1863d500549bSAnthony Wilson * This function is called recursively, obtaining data asynchronously from one 1864d500549bSAnthony Wilson * connection in each call. This ensures the callback is not invoked until the 1865d500549bSAnthony Wilson * last asynchronous function has completed. 1866d500549bSAnthony Wilson * 1867d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 1868d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1869d500549bSAnthony Wilson * @param ledConnections Connections that provide data for the inventory leds. 1870d500549bSAnthony Wilson * @param callback Callback to invoke when inventory data has been obtained. 1871d500549bSAnthony Wilson * @param ledConnectionsIndex Current index in ledConnections. Only specified 1872d500549bSAnthony Wilson * in recursive calls to this function. 1873d500549bSAnthony Wilson */ 1874d500549bSAnthony Wilson template <typename Callback> 1875d500549bSAnthony Wilson void getInventoryLedData( 1876d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1877d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1878fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> ledConnections, 1879d500549bSAnthony Wilson Callback&& callback, size_t ledConnectionsIndex = 0) 1880d500549bSAnthony Wilson { 1881d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData enter"; 1882d500549bSAnthony Wilson 1883d500549bSAnthony Wilson // If no more connections left, call callback 1884d500549bSAnthony Wilson if (ledConnectionsIndex >= ledConnections->size()) 1885d500549bSAnthony Wilson { 188642cbe538SGunnar Mills callback(); 1887d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData exit"; 1888d500549bSAnthony Wilson return; 1889d500549bSAnthony Wilson } 1890d500549bSAnthony Wilson 1891d500549bSAnthony Wilson // Get inventory item data from current connection 1892fe04d49cSNan Zhou auto it = ledConnections->begin(); 1893fe04d49cSNan Zhou std::advance(it, ledConnectionsIndex); 1894d500549bSAnthony Wilson if (it != ledConnections->end()) 1895d500549bSAnthony Wilson { 1896d500549bSAnthony Wilson const std::string& ledPath = (*it).first; 1897d500549bSAnthony Wilson const std::string& ledConnection = (*it).second; 1898d500549bSAnthony Wilson // Response handler for Get State property 18991e1e598dSJonathan Doman auto respHandler = 19001e1e598dSJonathan Doman [sensorsAsyncResp, inventoryItems, ledConnections, ledPath, 1901f94c4ecfSEd Tanous callback{std::forward<Callback>(callback)}, ledConnectionsIndex]( 19021e1e598dSJonathan Doman const boost::system::error_code ec, const std::string& state) { 1903d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter"; 1904d500549bSAnthony Wilson if (ec) 1905d500549bSAnthony Wilson { 1906d500549bSAnthony Wilson BMCWEB_LOG_ERROR 1907d500549bSAnthony Wilson << "getInventoryLedData respHandler DBus error " << ec; 19088d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 1909d500549bSAnthony Wilson return; 1910d500549bSAnthony Wilson } 1911d500549bSAnthony Wilson 19121e1e598dSJonathan Doman BMCWEB_LOG_DEBUG << "Led state: " << state; 1913d500549bSAnthony Wilson // Find inventory item with this LED object path 1914d500549bSAnthony Wilson InventoryItem* inventoryItem = 1915d500549bSAnthony Wilson findInventoryItemForLed(*inventoryItems, ledPath); 1916d500549bSAnthony Wilson if (inventoryItem != nullptr) 1917d500549bSAnthony Wilson { 1918d500549bSAnthony Wilson // Store LED state in InventoryItem 191911ba3979SEd Tanous if (state.ends_with("On")) 1920d500549bSAnthony Wilson { 1921d500549bSAnthony Wilson inventoryItem->ledState = LedState::ON; 1922d500549bSAnthony Wilson } 192311ba3979SEd Tanous else if (state.ends_with("Blink")) 1924d500549bSAnthony Wilson { 1925d500549bSAnthony Wilson inventoryItem->ledState = LedState::BLINK; 1926d500549bSAnthony Wilson } 192711ba3979SEd Tanous else if (state.ends_with("Off")) 1928d500549bSAnthony Wilson { 1929d500549bSAnthony Wilson inventoryItem->ledState = LedState::OFF; 1930d500549bSAnthony Wilson } 1931d500549bSAnthony Wilson else 1932d500549bSAnthony Wilson { 1933d500549bSAnthony Wilson inventoryItem->ledState = LedState::UNKNOWN; 1934d500549bSAnthony Wilson } 1935d500549bSAnthony Wilson } 1936d500549bSAnthony Wilson 1937d500549bSAnthony Wilson // Recurse to get LED data from next connection 1938d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, 1939d500549bSAnthony Wilson ledConnections, std::move(callback), 1940d500549bSAnthony Wilson ledConnectionsIndex + 1); 1941d500549bSAnthony Wilson 1942d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit"; 1943d500549bSAnthony Wilson }; 1944d500549bSAnthony Wilson 1945d500549bSAnthony Wilson // Get the State property for the current LED 19461e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 19471e1e598dSJonathan Doman *crow::connections::systemBus, ledConnection, ledPath, 19481e1e598dSJonathan Doman "xyz.openbmc_project.Led.Physical", "State", 19491e1e598dSJonathan Doman std::move(respHandler)); 1950d500549bSAnthony Wilson } 1951d500549bSAnthony Wilson 1952d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLedData exit"; 1953d500549bSAnthony Wilson } 1954d500549bSAnthony Wilson 1955d500549bSAnthony Wilson /** 1956d500549bSAnthony Wilson * @brief Gets LED data for LEDs associated with given inventory items. 1957d500549bSAnthony Wilson * 1958d500549bSAnthony Wilson * Gets the D-Bus connections (services) that provide LED data for the LEDs 1959d500549bSAnthony Wilson * associated with the specified inventory items. Then gets the LED data from 1960d500549bSAnthony Wilson * each connection and stores it in the inventory item. 1961d500549bSAnthony Wilson * 1962d500549bSAnthony Wilson * This data is later used to provide sensor property values in the JSON 1963d500549bSAnthony Wilson * response. 1964d500549bSAnthony Wilson * 1965d500549bSAnthony Wilson * Finds the LED data asynchronously. Invokes callback when information has 1966d500549bSAnthony Wilson * been obtained. 1967d500549bSAnthony Wilson * 1968d500549bSAnthony Wilson * The callback must have the following signature: 1969d500549bSAnthony Wilson * @code 197042cbe538SGunnar Mills * callback() 1971d500549bSAnthony Wilson * @endcode 1972d500549bSAnthony Wilson * 1973d500549bSAnthony Wilson * @param sensorsAsyncResp Pointer to object holding response data. 1974d500549bSAnthony Wilson * @param inventoryItems D-Bus inventory items associated with sensors. 1975d500549bSAnthony Wilson * @param callback Callback to invoke when inventory items have been obtained. 1976d500549bSAnthony Wilson */ 1977d500549bSAnthony Wilson template <typename Callback> 1978d500549bSAnthony Wilson void getInventoryLeds( 1979d500549bSAnthony Wilson std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 1980d500549bSAnthony Wilson std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 1981d500549bSAnthony Wilson Callback&& callback) 1982d500549bSAnthony Wilson { 1983d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds enter"; 1984d500549bSAnthony Wilson 1985d500549bSAnthony Wilson const std::string path = "/xyz/openbmc_project"; 1986d500549bSAnthony Wilson const std::array<std::string, 1> interfaces = { 1987d500549bSAnthony Wilson "xyz.openbmc_project.Led.Physical"}; 1988d500549bSAnthony Wilson 1989d500549bSAnthony Wilson // Response handler for parsing output from GetSubTree 1990002d39b4SEd Tanous auto respHandler = 1991002d39b4SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 1992002d39b4SEd Tanous inventoryItems]( 1993b9d36b47SEd Tanous const boost::system::error_code ec, 1994002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 1995d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter"; 1996d500549bSAnthony Wilson if (ec) 1997d500549bSAnthony Wilson { 19988d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 1999d500549bSAnthony Wilson BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error " 2000d500549bSAnthony Wilson << ec; 2001d500549bSAnthony Wilson return; 2002d500549bSAnthony Wilson } 2003d500549bSAnthony Wilson 2004d500549bSAnthony Wilson // Build map of LED object paths to connections 2005fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> ledConnections = 2006fe04d49cSNan Zhou std::make_shared<std::map<std::string, std::string>>(); 2007d500549bSAnthony Wilson 2008d500549bSAnthony Wilson // Loop through objects from GetSubTree 2009d500549bSAnthony Wilson for (const std::pair< 2010d500549bSAnthony Wilson std::string, 2011d500549bSAnthony Wilson std::vector<std::pair<std::string, std::vector<std::string>>>>& 2012d500549bSAnthony Wilson object : subtree) 2013d500549bSAnthony Wilson { 2014d500549bSAnthony Wilson // Check if object path is LED for one of the specified inventory 2015d500549bSAnthony Wilson // items 2016d500549bSAnthony Wilson const std::string& ledPath = object.first; 2017d500549bSAnthony Wilson if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr) 2018d500549bSAnthony Wilson { 2019d500549bSAnthony Wilson // Add mapping from ledPath to connection 2020d500549bSAnthony Wilson const std::string& connection = object.second.begin()->first; 2021d500549bSAnthony Wilson (*ledConnections)[ledPath] = connection; 2022d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> " 2023d500549bSAnthony Wilson << connection; 2024d500549bSAnthony Wilson } 2025d500549bSAnthony Wilson } 2026d500549bSAnthony Wilson 2027d500549bSAnthony Wilson getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections, 2028d500549bSAnthony Wilson std::move(callback)); 2029d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit"; 2030d500549bSAnthony Wilson }; 2031d500549bSAnthony Wilson // Make call to ObjectMapper to find all inventory items 2032d500549bSAnthony Wilson crow::connections::systemBus->async_method_call( 2033d500549bSAnthony Wilson std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 2034d500549bSAnthony Wilson "/xyz/openbmc_project/object_mapper", 2035d500549bSAnthony Wilson "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); 2036d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryLeds exit"; 2037d500549bSAnthony Wilson } 2038d500549bSAnthony Wilson 2039d500549bSAnthony Wilson /** 204042cbe538SGunnar Mills * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent 204142cbe538SGunnar Mills * 204242cbe538SGunnar Mills * Uses the specified connections (services) (currently assumes just one) to 204342cbe538SGunnar Mills * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in 204442cbe538SGunnar Mills * the inventoryItems vector. Only stores data in Power Supply inventoryItems. 204542cbe538SGunnar Mills * 204642cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 204742cbe538SGunnar Mills * response. 204842cbe538SGunnar Mills * 204942cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 205042cbe538SGunnar Mills * when data has been obtained. 205142cbe538SGunnar Mills * 205242cbe538SGunnar Mills * The callback must have the following signature: 205342cbe538SGunnar Mills * @code 205442cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 205542cbe538SGunnar Mills * @endcode 205642cbe538SGunnar Mills * 205742cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 205842cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 205942cbe538SGunnar Mills * @param psAttributesConnections Connections that provide data for the Power 206042cbe538SGunnar Mills * Supply Attributes 206142cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 206242cbe538SGunnar Mills */ 206342cbe538SGunnar Mills template <typename Callback> 206442cbe538SGunnar Mills void getPowerSupplyAttributesData( 2065b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 206642cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 2067fe04d49cSNan Zhou const std::map<std::string, std::string>& psAttributesConnections, 206842cbe538SGunnar Mills Callback&& callback) 206942cbe538SGunnar Mills { 207042cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter"; 207142cbe538SGunnar Mills 207242cbe538SGunnar Mills if (psAttributesConnections.empty()) 207342cbe538SGunnar Mills { 207442cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!"; 207542cbe538SGunnar Mills callback(inventoryItems); 207642cbe538SGunnar Mills return; 207742cbe538SGunnar Mills } 207842cbe538SGunnar Mills 207942cbe538SGunnar Mills // Assuming just one connection (service) for now 2080fe04d49cSNan Zhou auto it = psAttributesConnections.begin(); 208142cbe538SGunnar Mills 208242cbe538SGunnar Mills const std::string& psAttributesPath = (*it).first; 208342cbe538SGunnar Mills const std::string& psAttributesConnection = (*it).second; 208442cbe538SGunnar Mills 208542cbe538SGunnar Mills // Response handler for Get DeratingFactor property 2086002d39b4SEd Tanous auto respHandler = 2087002d39b4SEd Tanous [sensorsAsyncResp, inventoryItems, 2088f94c4ecfSEd Tanous callback{std::forward<Callback>(callback)}]( 2089002d39b4SEd Tanous const boost::system::error_code ec, const uint32_t value) { 209042cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter"; 209142cbe538SGunnar Mills if (ec) 209242cbe538SGunnar Mills { 209342cbe538SGunnar Mills BMCWEB_LOG_ERROR 209442cbe538SGunnar Mills << "getPowerSupplyAttributesData respHandler DBus error " << ec; 20958d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 209642cbe538SGunnar Mills return; 209742cbe538SGunnar Mills } 209842cbe538SGunnar Mills 20991e1e598dSJonathan Doman BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value; 210042cbe538SGunnar Mills // Store value in Power Supply Inventory Items 210142cbe538SGunnar Mills for (InventoryItem& inventoryItem : *inventoryItems) 210242cbe538SGunnar Mills { 210355f79e6fSEd Tanous if (inventoryItem.isPowerSupply) 210442cbe538SGunnar Mills { 210542cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent = 21061e1e598dSJonathan Doman static_cast<int>(value); 210742cbe538SGunnar Mills } 210842cbe538SGunnar Mills } 210942cbe538SGunnar Mills 211042cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit"; 211142cbe538SGunnar Mills callback(inventoryItems); 211242cbe538SGunnar Mills }; 211342cbe538SGunnar Mills 211442cbe538SGunnar Mills // Get the DeratingFactor property for the PowerSupplyAttributes 211542cbe538SGunnar Mills // Currently only property on the interface/only one we care about 21161e1e598dSJonathan Doman sdbusplus::asio::getProperty<uint32_t>( 21171e1e598dSJonathan Doman *crow::connections::systemBus, psAttributesConnection, psAttributesPath, 21181e1e598dSJonathan Doman "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor", 21191e1e598dSJonathan Doman std::move(respHandler)); 212042cbe538SGunnar Mills 212142cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit"; 212242cbe538SGunnar Mills } 212342cbe538SGunnar Mills 212442cbe538SGunnar Mills /** 212542cbe538SGunnar Mills * @brief Gets the Power Supply Attributes such as EfficiencyPercent 212642cbe538SGunnar Mills * 212742cbe538SGunnar Mills * Gets the D-Bus connection (service) that provides Power Supply Attributes 212842cbe538SGunnar Mills * data. Then gets the Power Supply Attributes data from the connection 212942cbe538SGunnar Mills * (currently just assumes 1 connection) and stores the data in the inventory 213042cbe538SGunnar Mills * item. 213142cbe538SGunnar Mills * 213242cbe538SGunnar Mills * This data is later used to provide sensor property values in the JSON 213342cbe538SGunnar Mills * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish. 213442cbe538SGunnar Mills * 213542cbe538SGunnar Mills * Finds the Power Supply Attributes data asynchronously. Invokes callback 213642cbe538SGunnar Mills * when information has been obtained. 213742cbe538SGunnar Mills * 213842cbe538SGunnar Mills * The callback must have the following signature: 213942cbe538SGunnar Mills * @code 214042cbe538SGunnar Mills * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 214142cbe538SGunnar Mills * @endcode 214242cbe538SGunnar Mills * 214342cbe538SGunnar Mills * @param sensorsAsyncResp Pointer to object holding response data. 214442cbe538SGunnar Mills * @param inventoryItems D-Bus inventory items associated with sensors. 214542cbe538SGunnar Mills * @param callback Callback to invoke when data has been obtained. 214642cbe538SGunnar Mills */ 214742cbe538SGunnar Mills template <typename Callback> 214842cbe538SGunnar Mills void getPowerSupplyAttributes( 214942cbe538SGunnar Mills std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 215042cbe538SGunnar Mills std::shared_ptr<std::vector<InventoryItem>> inventoryItems, 215142cbe538SGunnar Mills Callback&& callback) 215242cbe538SGunnar Mills { 215342cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter"; 215442cbe538SGunnar Mills 215542cbe538SGunnar Mills // Only need the power supply attributes when the Power Schema 2156a0ec28b6SAdrian Ambrożewicz if (sensorsAsyncResp->chassisSubNode != sensors::node::power) 215742cbe538SGunnar Mills { 215842cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power"; 215942cbe538SGunnar Mills callback(inventoryItems); 216042cbe538SGunnar Mills return; 216142cbe538SGunnar Mills } 216242cbe538SGunnar Mills 216342cbe538SGunnar Mills const std::array<std::string, 1> interfaces = { 216442cbe538SGunnar Mills "xyz.openbmc_project.Control.PowerSupplyAttributes"}; 216542cbe538SGunnar Mills 216642cbe538SGunnar Mills // Response handler for parsing output from GetSubTree 2167b9d36b47SEd Tanous auto respHandler = 2168b9d36b47SEd Tanous [callback{std::forward<Callback>(callback)}, sensorsAsyncResp, 2169b9d36b47SEd Tanous inventoryItems]( 2170b9d36b47SEd Tanous const boost::system::error_code ec, 2171b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 217242cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter"; 217342cbe538SGunnar Mills if (ec) 217442cbe538SGunnar Mills { 21758d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 217642cbe538SGunnar Mills BMCWEB_LOG_ERROR 217742cbe538SGunnar Mills << "getPowerSupplyAttributes respHandler DBus error " << ec; 217842cbe538SGunnar Mills return; 217942cbe538SGunnar Mills } 218026f6976fSEd Tanous if (subtree.empty()) 218142cbe538SGunnar Mills { 218242cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!"; 218342cbe538SGunnar Mills callback(inventoryItems); 218442cbe538SGunnar Mills return; 218542cbe538SGunnar Mills } 218642cbe538SGunnar Mills 218742cbe538SGunnar Mills // Currently we only support 1 power supply attribute, use this for 218842cbe538SGunnar Mills // all the power supplies. Build map of object path to connection. 218942cbe538SGunnar Mills // Assume just 1 connection and 1 path for now. 2190fe04d49cSNan Zhou std::map<std::string, std::string> psAttributesConnections; 219142cbe538SGunnar Mills 219242cbe538SGunnar Mills if (subtree[0].first.empty() || subtree[0].second.empty()) 219342cbe538SGunnar Mills { 219442cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!"; 219542cbe538SGunnar Mills callback(inventoryItems); 219642cbe538SGunnar Mills return; 219742cbe538SGunnar Mills } 219842cbe538SGunnar Mills 219942cbe538SGunnar Mills const std::string& psAttributesPath = subtree[0].first; 220042cbe538SGunnar Mills const std::string& connection = subtree[0].second.begin()->first; 220142cbe538SGunnar Mills 220242cbe538SGunnar Mills if (connection.empty()) 220342cbe538SGunnar Mills { 220442cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!"; 220542cbe538SGunnar Mills callback(inventoryItems); 220642cbe538SGunnar Mills return; 220742cbe538SGunnar Mills } 220842cbe538SGunnar Mills 220942cbe538SGunnar Mills psAttributesConnections[psAttributesPath] = connection; 221042cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> " 221142cbe538SGunnar Mills << connection; 221242cbe538SGunnar Mills 221342cbe538SGunnar Mills getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems, 221442cbe538SGunnar Mills psAttributesConnections, 221542cbe538SGunnar Mills std::move(callback)); 221642cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit"; 221742cbe538SGunnar Mills }; 221842cbe538SGunnar Mills // Make call to ObjectMapper to find the PowerSupplyAttributes service 221942cbe538SGunnar Mills crow::connections::systemBus->async_method_call( 222042cbe538SGunnar Mills std::move(respHandler), "xyz.openbmc_project.ObjectMapper", 222142cbe538SGunnar Mills "/xyz/openbmc_project/object_mapper", 222242cbe538SGunnar Mills "xyz.openbmc_project.ObjectMapper", "GetSubTree", 222342cbe538SGunnar Mills "/xyz/openbmc_project", 0, interfaces); 222442cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit"; 222542cbe538SGunnar Mills } 222642cbe538SGunnar Mills 222742cbe538SGunnar Mills /** 2228adc4f0dbSShawn McCarney * @brief Gets inventory items associated with sensors. 22298fb49dd6SShawn McCarney * 22308fb49dd6SShawn McCarney * Finds the inventory items that are associated with the specified sensors. 2231adc4f0dbSShawn McCarney * Then gets D-Bus data for the inventory items, such as presence and VPD. 22328fb49dd6SShawn McCarney * 2233adc4f0dbSShawn McCarney * This data is later used to provide sensor property values in the JSON 2234adc4f0dbSShawn McCarney * response. 22358fb49dd6SShawn McCarney * 2236adc4f0dbSShawn McCarney * Finds the inventory items asynchronously. Invokes callback when the 2237adc4f0dbSShawn McCarney * inventory items have been obtained. 2238adc4f0dbSShawn McCarney * 2239adc4f0dbSShawn McCarney * The callback must have the following signature: 2240adc4f0dbSShawn McCarney * @code 2241adc4f0dbSShawn McCarney * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) 2242adc4f0dbSShawn McCarney * @endcode 22438fb49dd6SShawn McCarney * 22448fb49dd6SShawn McCarney * @param sensorsAsyncResp Pointer to object holding response data. 22458fb49dd6SShawn McCarney * @param sensorNames All sensors within the current chassis. 22468fb49dd6SShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 22478fb49dd6SShawn McCarney * implements ObjectManager. 2248adc4f0dbSShawn McCarney * @param callback Callback to invoke when inventory items have been obtained. 22498fb49dd6SShawn McCarney */ 2250adc4f0dbSShawn McCarney template <typename Callback> 2251adc4f0dbSShawn McCarney static void getInventoryItems( 22528fb49dd6SShawn McCarney std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp, 2253fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> sensorNames, 2254fe04d49cSNan Zhou std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths, 2255adc4f0dbSShawn McCarney Callback&& callback) 22568fb49dd6SShawn McCarney { 2257adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItems enter"; 2258adc4f0dbSShawn McCarney auto getInventoryItemAssociationsCb = 2259f94c4ecfSEd Tanous [sensorsAsyncResp, objectMgrPaths, 2260f94c4ecfSEd Tanous callback{std::forward<Callback>(callback)}]( 2261adc4f0dbSShawn McCarney std::shared_ptr<std::vector<InventoryItem>> inventoryItems) { 2262adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter"; 22638fb49dd6SShawn McCarney auto getInventoryItemsConnectionsCb = 2264adc4f0dbSShawn McCarney [sensorsAsyncResp, inventoryItems, objectMgrPaths, 2265f94c4ecfSEd Tanous callback{std::forward<const Callback>(callback)}]( 2266fe04d49cSNan Zhou std::shared_ptr<std::set<std::string>> invConnections) { 22678fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter"; 2268002d39b4SEd Tanous auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems, 2269d500549bSAnthony Wilson callback{std::move(callback)}]() { 2270d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter"; 227142cbe538SGunnar Mills 2272002d39b4SEd Tanous auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems, 2273002d39b4SEd Tanous callback{std::move(callback)}]() { 227442cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter"; 227542cbe538SGunnar Mills // Find Power Supply Attributes and get the data 2276002d39b4SEd Tanous getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems, 227742cbe538SGunnar Mills std::move(callback)); 227842cbe538SGunnar Mills BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit"; 227942cbe538SGunnar Mills }; 228042cbe538SGunnar Mills 2281d500549bSAnthony Wilson // Find led connections and get the data 2282d500549bSAnthony Wilson getInventoryLeds(sensorsAsyncResp, inventoryItems, 228342cbe538SGunnar Mills std::move(getInventoryLedsCb)); 2284d500549bSAnthony Wilson BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit"; 2285d500549bSAnthony Wilson }; 22868fb49dd6SShawn McCarney 2287adc4f0dbSShawn McCarney // Get inventory item data from connections 2288adc4f0dbSShawn McCarney getInventoryItemsData(sensorsAsyncResp, inventoryItems, 2289adc4f0dbSShawn McCarney invConnections, objectMgrPaths, 2290d500549bSAnthony Wilson std::move(getInventoryItemsDataCb)); 22918fb49dd6SShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit"; 22928fb49dd6SShawn McCarney }; 22938fb49dd6SShawn McCarney 2294adc4f0dbSShawn McCarney // Get connections that provide inventory item data 2295002d39b4SEd Tanous getInventoryItemsConnections(sensorsAsyncResp, inventoryItems, 22968fb49dd6SShawn McCarney std::move(getInventoryItemsConnectionsCb)); 2297adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit"; 22988fb49dd6SShawn McCarney }; 22998fb49dd6SShawn McCarney 2300adc4f0dbSShawn McCarney // Get associations from sensors to inventory items 2301adc4f0dbSShawn McCarney getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths, 2302adc4f0dbSShawn McCarney std::move(getInventoryItemAssociationsCb)); 2303adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItems exit"; 2304adc4f0dbSShawn McCarney } 2305adc4f0dbSShawn McCarney 2306adc4f0dbSShawn McCarney /** 2307adc4f0dbSShawn McCarney * @brief Returns JSON PowerSupply object for the specified inventory item. 2308adc4f0dbSShawn McCarney * 2309adc4f0dbSShawn McCarney * Searches for a JSON PowerSupply object that matches the specified inventory 2310adc4f0dbSShawn McCarney * item. If one is not found, a new PowerSupply object is added to the JSON 2311adc4f0dbSShawn McCarney * array. 2312adc4f0dbSShawn McCarney * 2313adc4f0dbSShawn McCarney * Multiple sensors are often associated with one power supply inventory item. 2314adc4f0dbSShawn McCarney * As a result, multiple sensor values are stored in one JSON PowerSupply 2315adc4f0dbSShawn McCarney * object. 2316adc4f0dbSShawn McCarney * 2317adc4f0dbSShawn McCarney * @param powerSupplyArray JSON array containing Redfish PowerSupply objects. 2318adc4f0dbSShawn McCarney * @param inventoryItem Inventory item for the power supply. 2319adc4f0dbSShawn McCarney * @param chassisId Chassis that contains the power supply. 2320adc4f0dbSShawn McCarney * @return JSON PowerSupply object for the specified inventory item. 2321adc4f0dbSShawn McCarney */ 232223a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray, 2323adc4f0dbSShawn McCarney const InventoryItem& inventoryItem, 2324adc4f0dbSShawn McCarney const std::string& chassisId) 2325adc4f0dbSShawn McCarney { 2326adc4f0dbSShawn McCarney // Check if matching PowerSupply object already exists in JSON array 2327adc4f0dbSShawn McCarney for (nlohmann::json& powerSupply : powerSupplyArray) 2328adc4f0dbSShawn McCarney { 2329adc4f0dbSShawn McCarney if (powerSupply["MemberId"] == inventoryItem.name) 2330adc4f0dbSShawn McCarney { 2331adc4f0dbSShawn McCarney return powerSupply; 2332adc4f0dbSShawn McCarney } 2333adc4f0dbSShawn McCarney } 2334adc4f0dbSShawn McCarney 2335adc4f0dbSShawn McCarney // Add new PowerSupply object to JSON array 2336adc4f0dbSShawn McCarney powerSupplyArray.push_back({}); 2337adc4f0dbSShawn McCarney nlohmann::json& powerSupply = powerSupplyArray.back(); 2338adc4f0dbSShawn McCarney powerSupply["@odata.id"] = 2339adc4f0dbSShawn McCarney "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/"; 2340adc4f0dbSShawn McCarney powerSupply["MemberId"] = inventoryItem.name; 2341adc4f0dbSShawn McCarney powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " "); 2342adc4f0dbSShawn McCarney powerSupply["Manufacturer"] = inventoryItem.manufacturer; 2343adc4f0dbSShawn McCarney powerSupply["Model"] = inventoryItem.model; 2344adc4f0dbSShawn McCarney powerSupply["PartNumber"] = inventoryItem.partNumber; 2345adc4f0dbSShawn McCarney powerSupply["SerialNumber"] = inventoryItem.serialNumber; 2346d500549bSAnthony Wilson setLedState(powerSupply, &inventoryItem); 2347adc4f0dbSShawn McCarney 234842cbe538SGunnar Mills if (inventoryItem.powerSupplyEfficiencyPercent >= 0) 234942cbe538SGunnar Mills { 235042cbe538SGunnar Mills powerSupply["EfficiencyPercent"] = 235142cbe538SGunnar Mills inventoryItem.powerSupplyEfficiencyPercent; 235242cbe538SGunnar Mills } 235342cbe538SGunnar Mills 235442cbe538SGunnar Mills powerSupply["Status"]["State"] = getState(&inventoryItem); 2355adc4f0dbSShawn McCarney const char* health = inventoryItem.isFunctional ? "OK" : "Critical"; 2356adc4f0dbSShawn McCarney powerSupply["Status"]["Health"] = health; 2357adc4f0dbSShawn McCarney 2358adc4f0dbSShawn McCarney return powerSupply; 23598fb49dd6SShawn McCarney } 23608fb49dd6SShawn McCarney 23618fb49dd6SShawn McCarney /** 2362de629b6eSShawn McCarney * @brief Gets the values of the specified sensors. 2363de629b6eSShawn McCarney * 2364de629b6eSShawn McCarney * Stores the results as JSON in the SensorsAsyncResp. 2365de629b6eSShawn McCarney * 2366de629b6eSShawn McCarney * Gets the sensor values asynchronously. Stores the results later when the 2367de629b6eSShawn McCarney * information has been obtained. 2368de629b6eSShawn McCarney * 2369adc4f0dbSShawn McCarney * The sensorNames set contains all requested sensors for the current chassis. 2370de629b6eSShawn McCarney * 2371de629b6eSShawn McCarney * To minimize the number of DBus calls, the DBus method 2372de629b6eSShawn McCarney * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the 2373de629b6eSShawn McCarney * values of all sensors provided by a connection (service). 2374de629b6eSShawn McCarney * 2375de629b6eSShawn McCarney * The connections set contains all the connections that provide sensor values. 2376de629b6eSShawn McCarney * 2377de629b6eSShawn McCarney * The objectMgrPaths map contains mappings from a connection name to the 2378de629b6eSShawn McCarney * corresponding DBus object path that implements ObjectManager. 2379de629b6eSShawn McCarney * 2380adc4f0dbSShawn McCarney * The InventoryItem vector contains D-Bus inventory items associated with the 2381adc4f0dbSShawn McCarney * sensors. Inventory item data is needed for some Redfish sensor properties. 2382adc4f0dbSShawn McCarney * 2383de629b6eSShawn McCarney * @param SensorsAsyncResp Pointer to object holding response data. 2384adc4f0dbSShawn McCarney * @param sensorNames All requested sensors within the current chassis. 2385de629b6eSShawn McCarney * @param connections Connections that provide sensor values. 2386de629b6eSShawn McCarney * @param objectMgrPaths Mappings from connection name to DBus object path that 2387de629b6eSShawn McCarney * implements ObjectManager. 2388adc4f0dbSShawn McCarney * @param inventoryItems Inventory items associated with the sensors. 2389de629b6eSShawn McCarney */ 239023a21a1cSEd Tanous inline void getSensorData( 239181ce609eSEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 2392fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames, 2393fe04d49cSNan Zhou const std::set<std::string>& connections, 2394fe04d49cSNan Zhou const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths, 2395b5a76932SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems) 2396de629b6eSShawn McCarney { 2397de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getSensorData enter"; 2398de629b6eSShawn McCarney // Get managed objects from all services exposing sensors 2399de629b6eSShawn McCarney for (const std::string& connection : connections) 2400de629b6eSShawn McCarney { 2401de629b6eSShawn McCarney // Response handler to process managed objects 2402002d39b4SEd Tanous auto getManagedObjectsCb = 2403002d39b4SEd Tanous [sensorsAsyncResp, sensorNames, 2404002d39b4SEd Tanous inventoryItems](const boost::system::error_code ec, 240502cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 2406de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter"; 2407de629b6eSShawn McCarney if (ec) 2408de629b6eSShawn McCarney { 2409de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec; 24108d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 2411de629b6eSShawn McCarney return; 2412de629b6eSShawn McCarney } 2413de629b6eSShawn McCarney // Go through all objects and update response with sensor data 2414de629b6eSShawn McCarney for (const auto& objDictEntry : resp) 2415de629b6eSShawn McCarney { 2416de629b6eSShawn McCarney const std::string& objPath = 2417de629b6eSShawn McCarney static_cast<const std::string&>(objDictEntry.first); 2418de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object " 2419de629b6eSShawn McCarney << objPath; 2420de629b6eSShawn McCarney 2421de629b6eSShawn McCarney std::vector<std::string> split; 2422de629b6eSShawn McCarney // Reserve space for 2423de629b6eSShawn McCarney // /xyz/openbmc_project/sensors/<name>/<subname> 2424de629b6eSShawn McCarney split.reserve(6); 2425de629b6eSShawn McCarney boost::algorithm::split(split, objPath, boost::is_any_of("/")); 2426de629b6eSShawn McCarney if (split.size() < 6) 2427de629b6eSShawn McCarney { 2428de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "Got path that isn't long enough " 2429de629b6eSShawn McCarney << objPath; 2430de629b6eSShawn McCarney continue; 2431de629b6eSShawn McCarney } 2432de629b6eSShawn McCarney // These indexes aren't intuitive, as boost::split puts an empty 2433de629b6eSShawn McCarney // string at the beginning 2434de629b6eSShawn McCarney const std::string& sensorType = split[4]; 2435de629b6eSShawn McCarney const std::string& sensorName = split[5]; 2436de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "sensorName " << sensorName 2437de629b6eSShawn McCarney << " sensorType " << sensorType; 243849c53ac9SJohnathan Mantey if (sensorNames->find(objPath) == sensorNames->end()) 2439de629b6eSShawn McCarney { 2440accdbb2cSAndrew Geissler BMCWEB_LOG_DEBUG << sensorName << " not in sensor list "; 2441de629b6eSShawn McCarney continue; 2442de629b6eSShawn McCarney } 2443de629b6eSShawn McCarney 2444adc4f0dbSShawn McCarney // Find inventory item (if any) associated with sensor 2445adc4f0dbSShawn McCarney InventoryItem* inventoryItem = 2446adc4f0dbSShawn McCarney findInventoryItemForSensor(inventoryItems, objPath); 2447adc4f0dbSShawn McCarney 244895a3ecadSAnthony Wilson const std::string& sensorSchema = 244981ce609eSEd Tanous sensorsAsyncResp->chassisSubNode; 245095a3ecadSAnthony Wilson 245195a3ecadSAnthony Wilson nlohmann::json* sensorJson = nullptr; 245295a3ecadSAnthony Wilson 2453928fefb9SNan Zhou if (sensorSchema == sensors::node::sensors && 2454928fefb9SNan Zhou !sensorsAsyncResp->efficientExpand) 245595a3ecadSAnthony Wilson { 24568d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] = 245781ce609eSEd Tanous "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + 245881ce609eSEd Tanous "/" + sensorsAsyncResp->chassisSubNode + "/" + 245995a3ecadSAnthony Wilson sensorName; 24608d1b46d7Szhanghch05 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue); 246195a3ecadSAnthony Wilson } 246295a3ecadSAnthony Wilson else 246395a3ecadSAnthony Wilson { 2464271584abSEd Tanous std::string fieldName; 2465928fefb9SNan Zhou if (sensorsAsyncResp->efficientExpand) 2466928fefb9SNan Zhou { 2467928fefb9SNan Zhou fieldName = "Members"; 2468928fefb9SNan Zhou } 2469928fefb9SNan Zhou else if (sensorType == "temperature") 2470de629b6eSShawn McCarney { 2471de629b6eSShawn McCarney fieldName = "Temperatures"; 2472de629b6eSShawn McCarney } 2473de629b6eSShawn McCarney else if (sensorType == "fan" || sensorType == "fan_tach" || 2474de629b6eSShawn McCarney sensorType == "fan_pwm") 2475de629b6eSShawn McCarney { 2476de629b6eSShawn McCarney fieldName = "Fans"; 2477de629b6eSShawn McCarney } 2478de629b6eSShawn McCarney else if (sensorType == "voltage") 2479de629b6eSShawn McCarney { 2480de629b6eSShawn McCarney fieldName = "Voltages"; 2481de629b6eSShawn McCarney } 2482de629b6eSShawn McCarney else if (sensorType == "power") 2483de629b6eSShawn McCarney { 248455f79e6fSEd Tanous if (sensorName == "total_power") 2485028f7ebcSEddie James { 2486028f7ebcSEddie James fieldName = "PowerControl"; 2487028f7ebcSEddie James } 2488adc4f0dbSShawn McCarney else if ((inventoryItem != nullptr) && 2489adc4f0dbSShawn McCarney (inventoryItem->isPowerSupply)) 2490028f7ebcSEddie James { 2491de629b6eSShawn McCarney fieldName = "PowerSupplies"; 2492de629b6eSShawn McCarney } 2493adc4f0dbSShawn McCarney else 2494adc4f0dbSShawn McCarney { 2495adc4f0dbSShawn McCarney // Other power sensors are in SensorCollection 2496adc4f0dbSShawn McCarney continue; 2497adc4f0dbSShawn McCarney } 2498028f7ebcSEddie James } 2499de629b6eSShawn McCarney else 2500de629b6eSShawn McCarney { 2501de629b6eSShawn McCarney BMCWEB_LOG_ERROR << "Unsure how to handle sensorType " 2502de629b6eSShawn McCarney << sensorType; 2503de629b6eSShawn McCarney continue; 2504de629b6eSShawn McCarney } 2505de629b6eSShawn McCarney 2506de629b6eSShawn McCarney nlohmann::json& tempArray = 25078d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName]; 2508adc4f0dbSShawn McCarney if (fieldName == "PowerControl") 250949c53ac9SJohnathan Mantey { 2510adc4f0dbSShawn McCarney if (tempArray.empty()) 25117ab06f49SGunnar Mills { 251295a3ecadSAnthony Wilson // Put multiple "sensors" into a single 251395a3ecadSAnthony Wilson // PowerControl. Follows MemberId naming and 251495a3ecadSAnthony Wilson // naming in power.hpp. 25151476687dSEd Tanous nlohmann::json::object_t power; 25161476687dSEd Tanous power["@odata.id"] = 2517adc4f0dbSShawn McCarney "/redfish/v1/Chassis/" + 251881ce609eSEd Tanous sensorsAsyncResp->chassisId + "/" + 251981ce609eSEd Tanous sensorsAsyncResp->chassisSubNode + "#/" + 25201476687dSEd Tanous fieldName + "/0"; 25211476687dSEd Tanous tempArray.push_back(std::move(power)); 2522adc4f0dbSShawn McCarney } 2523adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 2524adc4f0dbSShawn McCarney } 2525adc4f0dbSShawn McCarney else if (fieldName == "PowerSupplies") 2526adc4f0dbSShawn McCarney { 2527adc4f0dbSShawn McCarney if (inventoryItem != nullptr) 2528adc4f0dbSShawn McCarney { 2529adc4f0dbSShawn McCarney sensorJson = 2530adc4f0dbSShawn McCarney &(getPowerSupply(tempArray, *inventoryItem, 253181ce609eSEd Tanous sensorsAsyncResp->chassisId)); 2532adc4f0dbSShawn McCarney } 253349c53ac9SJohnathan Mantey } 2534928fefb9SNan Zhou else if (fieldName == "Members") 2535928fefb9SNan Zhou { 25361476687dSEd Tanous nlohmann::json::object_t member; 25371476687dSEd Tanous member["@odata.id"] = 2538928fefb9SNan Zhou "/redfish/v1/Chassis/" + 2539928fefb9SNan Zhou sensorsAsyncResp->chassisId + "/" + 25401476687dSEd Tanous sensorsAsyncResp->chassisSubNode + "/" + sensorName; 25411476687dSEd Tanous tempArray.push_back(std::move(member)); 2542928fefb9SNan Zhou sensorJson = &(tempArray.back()); 2543928fefb9SNan Zhou } 254449c53ac9SJohnathan Mantey else 254549c53ac9SJohnathan Mantey { 25461476687dSEd Tanous nlohmann::json::object_t member; 25471476687dSEd Tanous member["@odata.id"] = "/redfish/v1/Chassis/" + 25481476687dSEd Tanous sensorsAsyncResp->chassisId + 25491476687dSEd Tanous "/" + 25501476687dSEd Tanous sensorsAsyncResp->chassisSubNode + 25511476687dSEd Tanous "#/" + fieldName + "/"; 25521476687dSEd Tanous tempArray.push_back(std::move(member)); 2553adc4f0dbSShawn McCarney sensorJson = &(tempArray.back()); 255449c53ac9SJohnathan Mantey } 255595a3ecadSAnthony Wilson } 2556de629b6eSShawn McCarney 2557adc4f0dbSShawn McCarney if (sensorJson != nullptr) 2558adc4f0dbSShawn McCarney { 25591d7c0054SEd Tanous objectInterfacesToJson(sensorName, sensorType, 25601d7c0054SEd Tanous sensorsAsyncResp->chassisSubNode, 25611d7c0054SEd Tanous objDictEntry.second, *sensorJson, 25621d7c0054SEd Tanous inventoryItem); 25631d7c0054SEd Tanous 25641d7c0054SEd Tanous std::string path = "/xyz/openbmc_project/sensors/"; 25651d7c0054SEd Tanous path += sensorType; 25661d7c0054SEd Tanous path += "/"; 25671d7c0054SEd Tanous path += sensorName; 25681d7c0054SEd Tanous sensorsAsyncResp->addMetadata(*sensorJson, sensorType, 25691d7c0054SEd Tanous path); 2570adc4f0dbSShawn McCarney } 2571de629b6eSShawn McCarney } 257281ce609eSEd Tanous if (sensorsAsyncResp.use_count() == 1) 257349c53ac9SJohnathan Mantey { 257481ce609eSEd Tanous sortJSONResponse(sensorsAsyncResp); 2575928fefb9SNan Zhou if (sensorsAsyncResp->chassisSubNode == 2576928fefb9SNan Zhou sensors::node::sensors && 2577928fefb9SNan Zhou sensorsAsyncResp->efficientExpand) 2578928fefb9SNan Zhou { 2579928fefb9SNan Zhou sensorsAsyncResp->asyncResp->res 2580928fefb9SNan Zhou .jsonValue["Members@odata.count"] = 2581928fefb9SNan Zhou sensorsAsyncResp->asyncResp->res.jsonValue["Members"] 2582928fefb9SNan Zhou .size(); 2583928fefb9SNan Zhou } 2584928fefb9SNan Zhou else if (sensorsAsyncResp->chassisSubNode == 2585928fefb9SNan Zhou sensors::node::thermal) 25868bd25ccdSJames Feist { 258781ce609eSEd Tanous populateFanRedundancy(sensorsAsyncResp); 25888bd25ccdSJames Feist } 258949c53ac9SJohnathan Mantey } 2590de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit"; 2591de629b6eSShawn McCarney }; 2592de629b6eSShawn McCarney 2593de629b6eSShawn McCarney // Find DBus object path that implements ObjectManager for the current 2594de629b6eSShawn McCarney // connection. If no mapping found, default to "/". 25958fb49dd6SShawn McCarney auto iter = objectMgrPaths->find(connection); 2596de629b6eSShawn McCarney const std::string& objectMgrPath = 25978fb49dd6SShawn McCarney (iter != objectMgrPaths->end()) ? iter->second : "/"; 2598de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is " 2599de629b6eSShawn McCarney << objectMgrPath; 2600de629b6eSShawn McCarney 2601de629b6eSShawn McCarney crow::connections::systemBus->async_method_call( 2602de629b6eSShawn McCarney getManagedObjectsCb, connection, objectMgrPath, 2603de629b6eSShawn McCarney "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 260423a21a1cSEd Tanous } 2605de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getSensorData exit"; 2606de629b6eSShawn McCarney } 2607de629b6eSShawn McCarney 2608fe04d49cSNan Zhou inline void 2609fe04d49cSNan Zhou processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 2610fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames) 26111abe55efSEd Tanous { 2612fe04d49cSNan Zhou auto getConnectionCb = [sensorsAsyncResp, sensorNames]( 2613fe04d49cSNan Zhou const std::set<std::string>& connections) { 261455c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getConnectionCb enter"; 2615de629b6eSShawn McCarney auto getObjectManagerPathsCb = 2616fe04d49cSNan Zhou [sensorsAsyncResp, sensorNames, connections]( 2617fe04d49cSNan Zhou const std::shared_ptr<std::map<std::string, std::string>>& 2618fe04d49cSNan Zhou objectMgrPaths) { 2619de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter"; 2620adc4f0dbSShawn McCarney auto getInventoryItemsCb = 2621002d39b4SEd Tanous [sensorsAsyncResp, sensorNames, connections, objectMgrPaths]( 2622f23b7296SEd Tanous const std::shared_ptr<std::vector<InventoryItem>>& 2623adc4f0dbSShawn McCarney inventoryItems) { 2624adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter"; 262549c53ac9SJohnathan Mantey // Get sensor data and store results in JSON 2626002d39b4SEd Tanous getSensorData(sensorsAsyncResp, sensorNames, connections, 2627002d39b4SEd Tanous objectMgrPaths, inventoryItems); 2628adc4f0dbSShawn McCarney BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit"; 2629adc4f0dbSShawn McCarney }; 2630adc4f0dbSShawn McCarney 2631adc4f0dbSShawn McCarney // Get inventory items associated with sensors 2632002d39b4SEd Tanous getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths, 2633adc4f0dbSShawn McCarney std::move(getInventoryItemsCb)); 2634adc4f0dbSShawn McCarney 2635de629b6eSShawn McCarney BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit"; 263608777fb0SLewanczyk, Dawid }; 2637de629b6eSShawn McCarney 263849c53ac9SJohnathan Mantey // Get mapping from connection names to the DBus object 263949c53ac9SJohnathan Mantey // paths that implement the ObjectManager interface 264081ce609eSEd Tanous getObjectManagerPaths(sensorsAsyncResp, 2641de629b6eSShawn McCarney std::move(getObjectManagerPathsCb)); 264255c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getConnectionCb exit"; 264308777fb0SLewanczyk, Dawid }; 2644de629b6eSShawn McCarney 2645de629b6eSShawn McCarney // Get set of connections that provide sensor values 264681ce609eSEd Tanous getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb)); 264795a3ecadSAnthony Wilson } 264895a3ecadSAnthony Wilson 264995a3ecadSAnthony Wilson /** 265095a3ecadSAnthony Wilson * @brief Entry point for retrieving sensors data related to requested 265195a3ecadSAnthony Wilson * chassis. 265295a3ecadSAnthony Wilson * @param SensorsAsyncResp Pointer to object holding response data 265395a3ecadSAnthony Wilson */ 2654b5a76932SEd Tanous inline void 265581ce609eSEd Tanous getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) 265695a3ecadSAnthony Wilson { 265795a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisData enter"; 265895a3ecadSAnthony Wilson auto getChassisCb = 265981ce609eSEd Tanous [sensorsAsyncResp]( 2660fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames) { 266195a3ecadSAnthony Wilson BMCWEB_LOG_DEBUG << "getChassisCb enter"; 266281ce609eSEd Tanous processSensorList(sensorsAsyncResp, sensorNames); 266355c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassisCb exit"; 266408777fb0SLewanczyk, Dawid }; 2665928fefb9SNan Zhou // SensorCollection doesn't contain the Redundancy property 2666928fefb9SNan Zhou if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors) 2667928fefb9SNan Zhou { 26688d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] = 26698d1b46d7Szhanghch05 nlohmann::json::array(); 2670928fefb9SNan Zhou } 267126f03899SShawn McCarney // Get set of sensors in chassis 2672*7f1cc26dSEd Tanous getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId, 2673*7f1cc26dSEd Tanous sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types, 2674*7f1cc26dSEd Tanous std::move(getChassisCb)); 267555c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "getChassisData exit"; 2676271584abSEd Tanous } 267708777fb0SLewanczyk, Dawid 2678413961deSRichard Marian Thomaiyar /** 267949c53ac9SJohnathan Mantey * @brief Find the requested sensorName in the list of all sensors supplied by 268049c53ac9SJohnathan Mantey * the chassis node 268149c53ac9SJohnathan Mantey * 268249c53ac9SJohnathan Mantey * @param sensorName The sensor name supplied in the PATCH request 268349c53ac9SJohnathan Mantey * @param sensorsList The list of sensors managed by the chassis node 268449c53ac9SJohnathan Mantey * @param sensorsModified The list of sensors that were found as a result of 268549c53ac9SJohnathan Mantey * repeated calls to this function 268649c53ac9SJohnathan Mantey */ 2687fe04d49cSNan Zhou inline bool 2688fe04d49cSNan Zhou findSensorNameUsingSensorPath(std::string_view sensorName, 268902cad96eSEd Tanous const std::set<std::string>& sensorsList, 2690fe04d49cSNan Zhou std::set<std::string>& sensorsModified) 269149c53ac9SJohnathan Mantey { 2692fe04d49cSNan Zhou for (const auto& chassisSensor : sensorsList) 269349c53ac9SJohnathan Mantey { 269428aa8de5SGeorge Liu sdbusplus::message::object_path path(chassisSensor); 2695b00dcc27SEd Tanous std::string thisSensorName = path.filename(); 269628aa8de5SGeorge Liu if (thisSensorName.empty()) 269749c53ac9SJohnathan Mantey { 269849c53ac9SJohnathan Mantey continue; 269949c53ac9SJohnathan Mantey } 270049c53ac9SJohnathan Mantey if (thisSensorName == sensorName) 270149c53ac9SJohnathan Mantey { 270249c53ac9SJohnathan Mantey sensorsModified.emplace(chassisSensor); 270349c53ac9SJohnathan Mantey return true; 270449c53ac9SJohnathan Mantey } 270549c53ac9SJohnathan Mantey } 270649c53ac9SJohnathan Mantey return false; 270749c53ac9SJohnathan Mantey } 270849c53ac9SJohnathan Mantey 270949c53ac9SJohnathan Mantey /** 2710413961deSRichard Marian Thomaiyar * @brief Entry point for overriding sensor values of given sensor 2711413961deSRichard Marian Thomaiyar * 27128d1b46d7Szhanghch05 * @param sensorAsyncResp response object 27134bb3dc34SCarol Wang * @param allCollections Collections extract from sensors' request patch info 2714413961deSRichard Marian Thomaiyar * @param chassisSubNode Chassis Node for which the query has to happen 2715413961deSRichard Marian Thomaiyar */ 271623a21a1cSEd Tanous inline void setSensorsOverride( 2717b5a76932SEd Tanous const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp, 27184bb3dc34SCarol Wang std::unordered_map<std::string, std::vector<nlohmann::json>>& 2719397fd61fSjayaprakash Mutyala allCollections) 2720413961deSRichard Marian Thomaiyar { 272170d1d0aaSjayaprakash Mutyala BMCWEB_LOG_INFO << "setSensorsOverride for subNode" 27224bb3dc34SCarol Wang << sensorAsyncResp->chassisSubNode << "\n"; 2723413961deSRichard Marian Thomaiyar 2724543f4400SEd Tanous const char* propertyValueName = nullptr; 2725f65af9e8SRichard Marian Thomaiyar std::unordered_map<std::string, std::pair<double, std::string>> overrideMap; 2726413961deSRichard Marian Thomaiyar std::string memberId; 2727543f4400SEd Tanous double value = 0.0; 2728f65af9e8SRichard Marian Thomaiyar for (auto& collectionItems : allCollections) 2729f65af9e8SRichard Marian Thomaiyar { 2730f65af9e8SRichard Marian Thomaiyar if (collectionItems.first == "Temperatures") 2731f65af9e8SRichard Marian Thomaiyar { 2732f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingCelsius"; 2733f65af9e8SRichard Marian Thomaiyar } 2734f65af9e8SRichard Marian Thomaiyar else if (collectionItems.first == "Fans") 2735f65af9e8SRichard Marian Thomaiyar { 2736f65af9e8SRichard Marian Thomaiyar propertyValueName = "Reading"; 2737f65af9e8SRichard Marian Thomaiyar } 2738f65af9e8SRichard Marian Thomaiyar else 2739f65af9e8SRichard Marian Thomaiyar { 2740f65af9e8SRichard Marian Thomaiyar propertyValueName = "ReadingVolts"; 2741f65af9e8SRichard Marian Thomaiyar } 2742f65af9e8SRichard Marian Thomaiyar for (auto& item : collectionItems.second) 2743f65af9e8SRichard Marian Thomaiyar { 27448d1b46d7Szhanghch05 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res, 27458d1b46d7Szhanghch05 "MemberId", memberId, propertyValueName, 27468d1b46d7Szhanghch05 value)) 2747413961deSRichard Marian Thomaiyar { 2748413961deSRichard Marian Thomaiyar return; 2749413961deSRichard Marian Thomaiyar } 2750f65af9e8SRichard Marian Thomaiyar overrideMap.emplace(memberId, 2751f65af9e8SRichard Marian Thomaiyar std::make_pair(value, collectionItems.first)); 2752f65af9e8SRichard Marian Thomaiyar } 2753f65af9e8SRichard Marian Thomaiyar } 27544bb3dc34SCarol Wang 2755002d39b4SEd Tanous auto getChassisSensorListCb = 2756002d39b4SEd Tanous [sensorAsyncResp, overrideMap]( 2757fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorsList) { 275849c53ac9SJohnathan Mantey // Match sensor names in the PATCH request to those managed by the 275949c53ac9SJohnathan Mantey // chassis node 2760fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> sensorNames = 2761fe04d49cSNan Zhou std::make_shared<std::set<std::string>>(); 2762f65af9e8SRichard Marian Thomaiyar for (const auto& item : overrideMap) 2763413961deSRichard Marian Thomaiyar { 2764f65af9e8SRichard Marian Thomaiyar const auto& sensor = item.first; 276549c53ac9SJohnathan Mantey if (!findSensorNameUsingSensorPath(sensor, *sensorsList, 276649c53ac9SJohnathan Mantey *sensorNames)) 2767f65af9e8SRichard Marian Thomaiyar { 2768f65af9e8SRichard Marian Thomaiyar BMCWEB_LOG_INFO << "Unable to find memberId " << item.first; 27698d1b46d7Szhanghch05 messages::resourceNotFound(sensorAsyncResp->asyncResp->res, 2770f65af9e8SRichard Marian Thomaiyar item.second.second, item.first); 2771413961deSRichard Marian Thomaiyar return; 2772413961deSRichard Marian Thomaiyar } 2773f65af9e8SRichard Marian Thomaiyar } 2774413961deSRichard Marian Thomaiyar // Get the connection to which the memberId belongs 2775002d39b4SEd Tanous auto getObjectsWithConnectionCb = 2776fe04d49cSNan Zhou [sensorAsyncResp, 2777fe04d49cSNan Zhou overrideMap](const std::set<std::string>& /*connections*/, 2778002d39b4SEd Tanous const std::set<std::pair<std::string, std::string>>& 2779413961deSRichard Marian Thomaiyar objectsWithConnection) { 2780f65af9e8SRichard Marian Thomaiyar if (objectsWithConnection.size() != overrideMap.size()) 2781413961deSRichard Marian Thomaiyar { 2782413961deSRichard Marian Thomaiyar BMCWEB_LOG_INFO 2783f65af9e8SRichard Marian Thomaiyar << "Unable to find all objects with proper connection " 2784f65af9e8SRichard Marian Thomaiyar << objectsWithConnection.size() << " requested " 2785f65af9e8SRichard Marian Thomaiyar << overrideMap.size() << "\n"; 27864f277b54SJayaprakash Mutyala messages::resourceNotFound(sensorAsyncResp->asyncResp->res, 2787a0ec28b6SAdrian Ambrożewicz sensorAsyncResp->chassisSubNode == 2788a0ec28b6SAdrian Ambrożewicz sensors::node::thermal 2789413961deSRichard Marian Thomaiyar ? "Temperatures" 2790413961deSRichard Marian Thomaiyar : "Voltages", 2791f65af9e8SRichard Marian Thomaiyar "Count"); 2792f65af9e8SRichard Marian Thomaiyar return; 2793f65af9e8SRichard Marian Thomaiyar } 2794f65af9e8SRichard Marian Thomaiyar for (const auto& item : objectsWithConnection) 2795f65af9e8SRichard Marian Thomaiyar { 279628aa8de5SGeorge Liu sdbusplus::message::object_path path(item.first); 279728aa8de5SGeorge Liu std::string sensorName = path.filename(); 279828aa8de5SGeorge Liu if (sensorName.empty()) 2799f65af9e8SRichard Marian Thomaiyar { 28004f277b54SJayaprakash Mutyala messages::internalError(sensorAsyncResp->asyncResp->res); 2801f65af9e8SRichard Marian Thomaiyar return; 2802f65af9e8SRichard Marian Thomaiyar } 2803f65af9e8SRichard Marian Thomaiyar 2804f65af9e8SRichard Marian Thomaiyar const auto& iterator = overrideMap.find(sensorName); 2805f65af9e8SRichard Marian Thomaiyar if (iterator == overrideMap.end()) 2806f65af9e8SRichard Marian Thomaiyar { 2807f65af9e8SRichard Marian Thomaiyar BMCWEB_LOG_INFO << "Unable to find sensor object" 2808f65af9e8SRichard Marian Thomaiyar << item.first << "\n"; 28094f277b54SJayaprakash Mutyala messages::internalError(sensorAsyncResp->asyncResp->res); 2810413961deSRichard Marian Thomaiyar return; 2811413961deSRichard Marian Thomaiyar } 2812413961deSRichard Marian Thomaiyar crow::connections::systemBus->async_method_call( 2813f65af9e8SRichard Marian Thomaiyar [sensorAsyncResp](const boost::system::error_code ec) { 2814413961deSRichard Marian Thomaiyar if (ec) 2815413961deSRichard Marian Thomaiyar { 28164f277b54SJayaprakash Mutyala if (ec.value() == 28174f277b54SJayaprakash Mutyala boost::system::errc::permission_denied) 28184f277b54SJayaprakash Mutyala { 28194f277b54SJayaprakash Mutyala BMCWEB_LOG_WARNING 28204f277b54SJayaprakash Mutyala << "Manufacturing mode is not Enabled...can't " 28214f277b54SJayaprakash Mutyala "Override the sensor value. "; 28224f277b54SJayaprakash Mutyala 28234f277b54SJayaprakash Mutyala messages::insufficientPrivilege( 28248d1b46d7Szhanghch05 sensorAsyncResp->asyncResp->res); 2825413961deSRichard Marian Thomaiyar return; 2826413961deSRichard Marian Thomaiyar } 28274f277b54SJayaprakash Mutyala BMCWEB_LOG_DEBUG 28284f277b54SJayaprakash Mutyala << "setOverrideValueStatus DBUS error: " << ec; 28294f277b54SJayaprakash Mutyala messages::internalError( 28304f277b54SJayaprakash Mutyala sensorAsyncResp->asyncResp->res); 28314f277b54SJayaprakash Mutyala } 2832413961deSRichard Marian Thomaiyar }, 28334f277b54SJayaprakash Mutyala item.second, item.first, "org.freedesktop.DBus.Properties", 28344f277b54SJayaprakash Mutyala "Set", "xyz.openbmc_project.Sensor.Value", "Value", 2835168e20c1SEd Tanous dbus::utility::DbusVariantType(iterator->second.first)); 2836f65af9e8SRichard Marian Thomaiyar } 2837413961deSRichard Marian Thomaiyar }; 2838413961deSRichard Marian Thomaiyar // Get object with connection for the given sensor name 2839413961deSRichard Marian Thomaiyar getObjectsWithConnection(sensorAsyncResp, sensorNames, 2840413961deSRichard Marian Thomaiyar std::move(getObjectsWithConnectionCb)); 2841413961deSRichard Marian Thomaiyar }; 2842413961deSRichard Marian Thomaiyar // get full sensor list for the given chassisId and cross verify the sensor. 2843*7f1cc26dSEd Tanous getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId, 2844*7f1cc26dSEd Tanous sensorAsyncResp->chassisSubNode, sensorAsyncResp->types, 2845*7f1cc26dSEd Tanous std::move(getChassisSensorListCb)); 2846413961deSRichard Marian Thomaiyar } 2847413961deSRichard Marian Thomaiyar 2848a0ec28b6SAdrian Ambrożewicz /** 2849a0ec28b6SAdrian Ambrożewicz * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus 2850a0ec28b6SAdrian Ambrożewicz * path of the sensor. 2851a0ec28b6SAdrian Ambrożewicz * 2852a0ec28b6SAdrian Ambrożewicz * Function builds valid Redfish response for sensor query of given chassis and 2853a0ec28b6SAdrian Ambrożewicz * node. It then builds metadata about Redfish<->D-Bus correlations and provides 2854a0ec28b6SAdrian Ambrożewicz * it to caller in a callback. 2855a0ec28b6SAdrian Ambrożewicz * 2856a0ec28b6SAdrian Ambrożewicz * @param chassis Chassis for which retrieval should be performed 2857a0ec28b6SAdrian Ambrożewicz * @param node Node (group) of sensors. See sensors::node for supported values 2858a0ec28b6SAdrian Ambrożewicz * @param mapComplete Callback to be called with retrieval result 2859a0ec28b6SAdrian Ambrożewicz */ 2860021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis, 2861021d32cfSKrzysztof Grobelny const std::string& node, 2862a0ec28b6SAdrian Ambrożewicz SensorsAsyncResp::DataCompleteCb&& mapComplete) 2863a0ec28b6SAdrian Ambrożewicz { 286402da7c5aSEd Tanous decltype(sensors::paths)::const_iterator pathIt = 286502da7c5aSEd Tanous std::find_if(sensors::paths.cbegin(), sensors::paths.cend(), 286602da7c5aSEd Tanous [&node](auto&& val) { return val.first == node; }); 286702da7c5aSEd Tanous if (pathIt == sensors::paths.cend()) 2868a0ec28b6SAdrian Ambrożewicz { 2869a0ec28b6SAdrian Ambrożewicz BMCWEB_LOG_ERROR << "Wrong node provided : " << node; 2870a0ec28b6SAdrian Ambrożewicz mapComplete(boost::beast::http::status::bad_request, {}); 2871a0ec28b6SAdrian Ambrożewicz return; 2872a0ec28b6SAdrian Ambrożewicz } 2873d51e072fSKrzysztof Grobelny 287472374eb7SNan Zhou auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); 2875fe04d49cSNan Zhou auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}]( 2876a0ec28b6SAdrian Ambrożewicz const boost::beast::http::status status, 2877fe04d49cSNan Zhou const std::map<std::string, std::string>& uriToDbus) { 2878fe04d49cSNan Zhou mapCompleteCb(status, uriToDbus); 2879fe04d49cSNan Zhou }; 2880a0ec28b6SAdrian Ambrożewicz 2881a0ec28b6SAdrian Ambrożewicz auto resp = std::make_shared<SensorsAsyncResp>( 2882d51e072fSKrzysztof Grobelny asyncResp, chassis, pathIt->second, node, std::move(callback)); 2883a0ec28b6SAdrian Ambrożewicz getChassisData(resp); 2884a0ec28b6SAdrian Ambrożewicz } 2885a0ec28b6SAdrian Ambrożewicz 2886bacb2162SNan Zhou namespace sensors 2887bacb2162SNan Zhou { 2888928fefb9SNan Zhou 2889bacb2162SNan Zhou inline void getChassisCallback( 2890bacb2162SNan Zhou const std::shared_ptr<SensorsAsyncResp>& asyncResp, 2891fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>>& sensorNames) 2892bacb2162SNan Zhou { 2893bacb2162SNan Zhou BMCWEB_LOG_DEBUG << "getChassisCallback enter"; 2894bacb2162SNan Zhou 2895bacb2162SNan Zhou nlohmann::json& entriesArray = 2896bacb2162SNan Zhou asyncResp->asyncResp->res.jsonValue["Members"]; 2897fe04d49cSNan Zhou for (const auto& sensor : *sensorNames) 2898bacb2162SNan Zhou { 2899bacb2162SNan Zhou BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor; 2900bacb2162SNan Zhou 2901bacb2162SNan Zhou sdbusplus::message::object_path path(sensor); 2902bacb2162SNan Zhou std::string sensorName = path.filename(); 2903bacb2162SNan Zhou if (sensorName.empty()) 2904bacb2162SNan Zhou { 2905bacb2162SNan Zhou BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor; 2906bacb2162SNan Zhou messages::internalError(asyncResp->asyncResp->res); 2907bacb2162SNan Zhou return; 2908bacb2162SNan Zhou } 29091476687dSEd Tanous nlohmann::json::object_t member; 29101476687dSEd Tanous member["@odata.id"] = "/redfish/v1/Chassis/" + asyncResp->chassisId + 29111476687dSEd Tanous "/" + asyncResp->chassisSubNode + "/" + 29121476687dSEd Tanous sensorName; 29131476687dSEd Tanous entriesArray.push_back(std::move(member)); 2914bacb2162SNan Zhou } 2915bacb2162SNan Zhou 2916bacb2162SNan Zhou asyncResp->asyncResp->res.jsonValue["Members@odata.count"] = 2917bacb2162SNan Zhou entriesArray.size(); 2918bacb2162SNan Zhou BMCWEB_LOG_DEBUG << "getChassisCallback exit"; 2919bacb2162SNan Zhou } 2920e6bd846dSNan Zhou 2921de167a6fSNan Zhou inline void 2922de167a6fSNan Zhou handleSensorCollectionGet(App& app, const crow::Request& req, 2923de167a6fSNan Zhou const std::shared_ptr<bmcweb::AsyncResp>& aResp, 2924de167a6fSNan Zhou const std::string& chassisId) 2925de167a6fSNan Zhou { 2926de167a6fSNan Zhou query_param::QueryCapabilities capabilities = { 2927de167a6fSNan Zhou .canDelegateExpandLevel = 1, 2928de167a6fSNan Zhou }; 2929de167a6fSNan Zhou query_param::Query delegatedQuery; 29303ba00073SCarson Labrado if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp, 2931de167a6fSNan Zhou delegatedQuery, capabilities)) 2932de167a6fSNan Zhou { 2933de167a6fSNan Zhou return; 2934de167a6fSNan Zhou } 2935de167a6fSNan Zhou 2936de167a6fSNan Zhou if (delegatedQuery.expandType != query_param::ExpandType::None) 2937de167a6fSNan Zhou { 2938de167a6fSNan Zhou // we perform efficient expand. 2939de167a6fSNan Zhou auto asyncResp = std::make_shared<SensorsAsyncResp>( 2940de167a6fSNan Zhou aResp, chassisId, sensors::dbus::sensorPaths, 2941de167a6fSNan Zhou sensors::node::sensors, 2942de167a6fSNan Zhou /*efficientExpand=*/true); 2943de167a6fSNan Zhou getChassisData(asyncResp); 2944de167a6fSNan Zhou 2945de167a6fSNan Zhou BMCWEB_LOG_DEBUG 2946de167a6fSNan Zhou << "SensorCollection doGet exit via efficient expand handler"; 2947de167a6fSNan Zhou return; 29480bad320cSEd Tanous } 2949de167a6fSNan Zhou 2950de167a6fSNan Zhou // if there's no efficient expand available, we use the default 2951de167a6fSNan Zhou // Query Parameters route 2952de167a6fSNan Zhou auto asyncResp = std::make_shared<SensorsAsyncResp>( 2953de167a6fSNan Zhou aResp, chassisId, sensors::dbus::sensorPaths, sensors::node::sensors); 2954de167a6fSNan Zhou 2955de167a6fSNan Zhou // We get all sensors as hyperlinkes in the chassis (this 2956de167a6fSNan Zhou // implies we reply on the default query parameters handler) 2957*7f1cc26dSEd Tanous getChassis(asyncResp->asyncResp, asyncResp->chassisId, 2958*7f1cc26dSEd Tanous asyncResp->chassisSubNode, asyncResp->types, 2959*7f1cc26dSEd Tanous 2960de167a6fSNan Zhou std::bind_front(sensors::getChassisCallback, asyncResp)); 2961de167a6fSNan Zhou BMCWEB_LOG_DEBUG << "SensorCollection doGet exit"; 2962de167a6fSNan Zhou } 2963de167a6fSNan Zhou 2964e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req, 2965e6bd846dSNan Zhou const std::shared_ptr<bmcweb::AsyncResp>& aResp, 2966e6bd846dSNan Zhou const std::string& chassisId, 2967e6bd846dSNan Zhou const std::string& sensorName) 2968e6bd846dSNan Zhou { 29693ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, aResp)) 2970e6bd846dSNan Zhou { 2971e6bd846dSNan Zhou return; 2972e6bd846dSNan Zhou } 2973e6bd846dSNan Zhou BMCWEB_LOG_DEBUG << "Sensor doGet enter"; 2974e6bd846dSNan Zhou std::shared_ptr<SensorsAsyncResp> asyncResp = 2975e6bd846dSNan Zhou std::make_shared<SensorsAsyncResp>(aResp, chassisId, 2976e6bd846dSNan Zhou std::span<std::string_view>(), 2977e6bd846dSNan Zhou sensors::node::sensors); 2978e6bd846dSNan Zhou 2979e6bd846dSNan Zhou const std::array<const char*, 1> interfaces = { 2980e6bd846dSNan Zhou "xyz.openbmc_project.Sensor.Value"}; 2981e6bd846dSNan Zhou 2982e6bd846dSNan Zhou // Get a list of all of the sensors that implement Sensor.Value 2983e6bd846dSNan Zhou // and get the path and service name associated with the sensor 2984e6bd846dSNan Zhou crow::connections::systemBus->async_method_call( 2985e6bd846dSNan Zhou [asyncResp, 2986e6bd846dSNan Zhou sensorName](const boost::system::error_code ec, 2987e6bd846dSNan Zhou const ::dbus::utility::MapperGetSubTreeResponse& subtree) { 2988e6bd846dSNan Zhou BMCWEB_LOG_DEBUG << "respHandler1 enter"; 2989e6bd846dSNan Zhou if (ec) 2990e6bd846dSNan Zhou { 2991e6bd846dSNan Zhou messages::internalError(asyncResp->asyncResp->res); 2992e6bd846dSNan Zhou BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: " 2993e6bd846dSNan Zhou << "Dbus error " << ec; 2994e6bd846dSNan Zhou return; 2995e6bd846dSNan Zhou } 2996e6bd846dSNan Zhou 2997e6bd846dSNan Zhou ::dbus::utility::MapperGetSubTreeResponse::const_iterator it = 2998e6bd846dSNan Zhou std::find_if( 2999e6bd846dSNan Zhou subtree.begin(), subtree.end(), 3000e6bd846dSNan Zhou [sensorName]( 3001e6bd846dSNan Zhou const std::pair< 3002e6bd846dSNan Zhou std::string, 3003e6bd846dSNan Zhou std::vector<std::pair< 3004e6bd846dSNan Zhou std::string, std::vector<std::string>>>>& object) { 3005e6bd846dSNan Zhou sdbusplus::message::object_path path(object.first); 3006e6bd846dSNan Zhou std::string name = path.filename(); 3007e6bd846dSNan Zhou if (name.empty()) 3008e6bd846dSNan Zhou { 3009e6bd846dSNan Zhou BMCWEB_LOG_ERROR << "Invalid sensor path: " << object.first; 3010e6bd846dSNan Zhou return false; 3011e6bd846dSNan Zhou } 3012e6bd846dSNan Zhou 3013e6bd846dSNan Zhou return name == sensorName; 3014e6bd846dSNan Zhou }); 3015e6bd846dSNan Zhou 3016e6bd846dSNan Zhou if (it == subtree.end()) 3017e6bd846dSNan Zhou { 3018e6bd846dSNan Zhou BMCWEB_LOG_ERROR << "Could not find path for sensor: " 3019e6bd846dSNan Zhou << sensorName; 3020e6bd846dSNan Zhou messages::resourceNotFound(asyncResp->asyncResp->res, "Sensor", 3021e6bd846dSNan Zhou sensorName); 3022e6bd846dSNan Zhou return; 3023e6bd846dSNan Zhou } 3024e6bd846dSNan Zhou std::string_view sensorPath = (*it).first; 3025e6bd846dSNan Zhou BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" << sensorName 3026e6bd846dSNan Zhou << "': " << sensorPath; 3027e6bd846dSNan Zhou 3028fe04d49cSNan Zhou const std::shared_ptr<std::set<std::string>> sensorList = 3029fe04d49cSNan Zhou std::make_shared<std::set<std::string>>(); 3030e6bd846dSNan Zhou 3031e6bd846dSNan Zhou sensorList->emplace(sensorPath); 3032e6bd846dSNan Zhou processSensorList(asyncResp, sensorList); 3033e6bd846dSNan Zhou BMCWEB_LOG_DEBUG << "respHandler1 exit"; 3034e6bd846dSNan Zhou }, 3035e6bd846dSNan Zhou "xyz.openbmc_project.ObjectMapper", 3036e6bd846dSNan Zhou "/xyz/openbmc_project/object_mapper", 3037e6bd846dSNan Zhou "xyz.openbmc_project.ObjectMapper", "GetSubTree", 3038e6bd846dSNan Zhou "/xyz/openbmc_project/sensors", 2, interfaces); 3039e6bd846dSNan Zhou } 3040e6bd846dSNan Zhou 3041bacb2162SNan Zhou } // namespace sensors 3042bacb2162SNan Zhou 30437e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app) 304495a3ecadSAnthony Wilson { 30457e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/") 3046ed398213SEd Tanous .privileges(redfish::privileges::getSensorCollection) 3047002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 3048de167a6fSNan Zhou std::bind_front(sensors::handleSensorCollectionGet, std::ref(app))); 304995a3ecadSAnthony Wilson } 305095a3ecadSAnthony Wilson 30517e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app) 305295a3ecadSAnthony Wilson { 30537e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/") 3054ed398213SEd Tanous .privileges(redfish::privileges::getSensor) 3055002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 3056e6bd846dSNan Zhou std::bind_front(sensors::handleSensorGet, std::ref(app))); 305795a3ecadSAnthony Wilson } 305895a3ecadSAnthony Wilson 305908777fb0SLewanczyk, Dawid } // namespace redfish 3060