From 7f1cc26dc160072059e782a3e2f253e5a0a7fe57 Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Tue, 9 Aug 2022 13:33:57 -0700 Subject: Refactor functions in sensors Refactor more functions out of sensors such that they don't require the use of SensorAsyncResp in all cases. This is to prepare for implementations that might not rely on SensorAsyncResp at all. Tested: Tested at end of series. Merge together. Signed-off-by: Ed Tanous Change-Id: Ie359f449c0f7ace57a09c0db0c4f1b347b5d5030 --- redfish-core/lib/sensors.hpp | 127 ++++++++++++++++++++++--------------------- 1 file changed, 66 insertions(+), 61 deletions(-) diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp index ae68a76cf9..7d09ce1904 100644 --- a/redfish-core/lib/sensors.hpp +++ b/redfish-core/lib/sensors.hpp @@ -439,21 +439,17 @@ void getConnections(std::shared_ptr sensorsAsyncResp, * made, and eliminate Power sensors when a Thermal request is made. */ inline void reduceSensorList( - const std::shared_ptr& sensorsAsyncResp, + crow::Response& res, std::string_view chassisSubNode, + std::span sensorTypes, const std::vector* allSensors, const std::shared_ptr>& activeSensors) { - if (sensorsAsyncResp == nullptr) - { - return; - } if ((allSensors == nullptr) || (activeSensors == nullptr)) { - messages::resourceNotFound( - sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode, - sensorsAsyncResp->chassisSubNode == sensors::node::thermal - ? "Temperatures" - : "Voltages"); + messages::resourceNotFound(res, chassisSubNode, + chassisSubNode == sensors::node::thermal + ? "Temperatures" + : "Voltages"); return; } @@ -463,7 +459,7 @@ inline void reduceSensorList( return; } - for (std::string_view type : sensorsAsyncResp->types) + for (std::string_view type : sensorTypes) { for (const std::string& sensor : *allSensors) { @@ -475,31 +471,66 @@ inline void reduceSensorList( } } +/* + *Populates the top level collection for a given subnode. Populates + *SensorCollection, Power, or Thermal schemas. + * + * */ +inline void populateChassisNode(nlohmann::json& jsonValue, + std::string_view chassisSubNode) +{ + if (chassisSubNode == sensors::node::power) + { + jsonValue["@odata.type"] = "#Power.v1_5_2.Power"; + } + else if (chassisSubNode == sensors::node::thermal) + { + jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal"; + jsonValue["Fans"] = nlohmann::json::array(); + jsonValue["Temperatures"] = nlohmann::json::array(); + } + else if (chassisSubNode == sensors::node::sensors) + { + jsonValue["@odata.type"] = "#SensorCollection.SensorCollection"; + jsonValue["Description"] = "Collection of Sensors for this Chassis"; + jsonValue["Members"] = nlohmann::json::array(); + jsonValue["Members@odata.count"] = 0; + } + + if (chassisSubNode != sensors::node::sensors) + { + jsonValue["Id"] = chassisSubNode; + } + jsonValue["Name"] = chassisSubNode; +} + /** * @brief Retrieves requested chassis sensors and redundancy data from DBus . * @param SensorsAsyncResp Pointer to object holding response data * @param callback Callback for next step in gathered sensor processing */ template -void getChassis(const std::shared_ptr& sensorsAsyncResp, - Callback&& callback) +void getChassis(const std::shared_ptr& asyncResp, + std::string_view chassisId, std::string_view chassisSubNode, + std::span sensorTypes, Callback&& callback) { BMCWEB_LOG_DEBUG << "getChassis enter"; const std::array interfaces = { "xyz.openbmc_project.Inventory.Item.Board", "xyz.openbmc_project.Inventory.Item.Chassis"}; auto respHandler = - [callback{std::forward(callback)}, sensorsAsyncResp]( + [callback{std::forward(callback)}, asyncResp, + chassisIdStr{std::string(chassisId)}, + chassisSubNode{std::string(chassisSubNode)}, sensorTypes]( const boost::system::error_code ec, const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) { BMCWEB_LOG_DEBUG << "getChassis respHandler enter"; if (ec) { BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec; - messages::internalError(sensorsAsyncResp->asyncResp->res); + messages::internalError(asyncResp->res); return; } - const std::string* chassisPath = nullptr; std::string chassisName; for (const std::string& chassis : chassisPaths) @@ -511,7 +542,7 @@ void getChassis(const std::shared_ptr& sensorsAsyncResp, BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis; continue; } - if (chassisName == sensorsAsyncResp->chassisId) + if (chassisName == chassisIdStr) { chassisPath = &chassis; break; @@ -519,53 +550,20 @@ void getChassis(const std::shared_ptr& sensorsAsyncResp, } if (chassisPath == nullptr) { - messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, - "Chassis", sensorsAsyncResp->chassisId); + messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr); return; } + populateChassisNode(asyncResp->res.jsonValue, chassisSubNode); - const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode; - if (chassisSubNode == sensors::node::power) - { - sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] = - "#Power.v1_5_2.Power"; - } - else if (chassisSubNode == sensors::node::thermal) - { - sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] = - "#Thermal.v1_4_0.Thermal"; - sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] = - nlohmann::json::array(); - sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] = - nlohmann::json::array(); - } - else if (chassisSubNode == sensors::node::sensors) - { - sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] = - "#SensorCollection.SensorCollection"; - sensorsAsyncResp->asyncResp->res.jsonValue["Description"] = - "Collection of Sensors for this Chassis"; - sensorsAsyncResp->asyncResp->res.jsonValue["Members"] = - nlohmann::json::array(); - sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] = - 0; - } + asyncResp->res.jsonValue["@odata.id"] = + "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode; - if (chassisSubNode != sensors::node::sensors) - { - sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode; - } - - sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] = - "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" + - chassisSubNode; - sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode; // Get the list of all sensors for this Chassis element std::string sensorPath = *chassisPath + "/all_sensors"; sdbusplus::asio::getProperty>( *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper", sensorPath, "xyz.openbmc_project.Association", "endpoints", - [sensorsAsyncResp, + [asyncResp, chassisSubNode, sensorTypes, callback{std::forward(callback)}]( const boost::system::error_code& e, const std::vector& nodeSensorList) { @@ -573,14 +571,15 @@ void getChassis(const std::shared_ptr& sensorsAsyncResp, { if (e.value() != EBADR) { - messages::internalError(sensorsAsyncResp->asyncResp->res); + messages::internalError(asyncResp->res); return; } } const std::shared_ptr> culledSensorList = std::make_shared>(); - reduceSensorList(sensorsAsyncResp, &nodeSensorList, - culledSensorList); + reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes, + &nodeSensorList, culledSensorList); + BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size(); callback(culledSensorList); }); }; @@ -2670,7 +2669,9 @@ inline void nlohmann::json::array(); } // Get set of sensors in chassis - getChassis(sensorsAsyncResp, std::move(getChassisCb)); + getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId, + sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types, + std::move(getChassisCb)); BMCWEB_LOG_DEBUG << "getChassisData exit"; } @@ -2839,7 +2840,9 @@ inline void setSensorsOverride( std::move(getObjectsWithConnectionCb)); }; // get full sensor list for the given chassisId and cross verify the sensor. - getChassis(sensorAsyncResp, std::move(getChassisSensorListCb)); + getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId, + sensorAsyncResp->chassisSubNode, sensorAsyncResp->types, + std::move(getChassisSensorListCb)); } /** @@ -2951,7 +2954,9 @@ inline void // We get all sensors as hyperlinkes in the chassis (this // implies we reply on the default query parameters handler) - getChassis(asyncResp, + getChassis(asyncResp->asyncResp, asyncResp->chassisId, + asyncResp->chassisSubNode, asyncResp->types, + std::bind_front(sensors::getChassisCallback, asyncResp)); BMCWEB_LOG_DEBUG << "SensorCollection doGet exit"; } -- cgit v1.2.3