diff options
author | Nan Zhou <nanzhoumails@gmail.com> | 2022-06-01 07:35:35 +0300 |
---|---|---|
committer | Nan Zhou <nanzhoumails@gmail.com> | 2022-06-01 22:03:09 +0300 |
commit | e6bd846d38bf7bb565b3f6a9aa8236543feb59f4 (patch) | |
tree | c0155e7c09b36eb145906bdd0716c58a9544f66c /redfish-core/lib | |
parent | f65fca6a44ded566fa6e993b1b23b2e189a48703 (diff) | |
download | bmcweb-e6bd846d38bf7bb565b3f6a9aa8236543feb59f4.tar.xz |
sensors: use inline functions + bind_front
This commit changes the `/redfish/v1/Chassis/<str>/Sensors/<str>/` route
to take std::bind_front instead of lambdas. We can clearly see the
indent levels decrease. It increases the readability.
Tested:
1. trivial change; code compiles.
2. tested on my local mock environment;
URL: /redfish/v1/Chassis/fake_chassis/Sensors/sensor0
Response:
{
"@odata.id": "/redfish/v1/Chassis/fake_chassis/Sensors/sensor0",
"@odata.type": "#Sensor.v1_0_0.Sensor",
"Id": "sensor0",
"Name": "sensor0",
"Reading": 0.0,
"ReadingRangeMax": null,
"ReadingRangeMin": null,
"ReadingType": "Current",
"ReadingUnits": "A",
"Status": {
"Health": "OK",
"State": "Enabled"
}
}
3. Service Validator Pass
*** /redfish/v1/Chassis/fake_chassis/Sensors/sensor0
Type (Sensor.v1_0_0.Sensor), GET SUCCESS (time: 0:00:00.007105)
PASS
Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Change-Id: Ic60521a937a8b18d317390fc75d792c58f56e3e6
Diffstat (limited to 'redfish-core/lib')
-rw-r--r-- | redfish-core/lib/sensors.hpp | 157 |
1 files changed, 80 insertions, 77 deletions
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp index 4c93b140b5..e1e25d576e 100644 --- a/redfish-core/lib/sensors.hpp +++ b/redfish-core/lib/sensors.hpp @@ -2991,6 +2991,85 @@ inline void getChassisCallback( entriesArray.size(); BMCWEB_LOG_DEBUG << "getChassisCallback exit"; } + +inline void handleSensorGet(App& app, const crow::Request& req, + const std::shared_ptr<bmcweb::AsyncResp>& aResp, + const std::string& chassisId, + const std::string& sensorName) +{ + if (!redfish::setUpRedfishRoute(app, req, aResp->res)) + { + return; + } + BMCWEB_LOG_DEBUG << "Sensor doGet enter"; + std::shared_ptr<SensorsAsyncResp> asyncResp = + std::make_shared<SensorsAsyncResp>(aResp, chassisId, + std::span<std::string_view>(), + sensors::node::sensors); + + const std::array<const char*, 1> interfaces = { + "xyz.openbmc_project.Sensor.Value"}; + + // Get a list of all of the sensors that implement Sensor.Value + // and get the path and service name associated with the sensor + crow::connections::systemBus->async_method_call( + [asyncResp, + sensorName](const boost::system::error_code ec, + const ::dbus::utility::MapperGetSubTreeResponse& subtree) { + BMCWEB_LOG_DEBUG << "respHandler1 enter"; + if (ec) + { + messages::internalError(asyncResp->asyncResp->res); + BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: " + << "Dbus error " << ec; + return; + } + + ::dbus::utility::MapperGetSubTreeResponse::const_iterator it = + std::find_if( + subtree.begin(), subtree.end(), + [sensorName]( + const std::pair< + std::string, + std::vector<std::pair< + std::string, std::vector<std::string>>>>& object) { + sdbusplus::message::object_path path(object.first); + std::string name = path.filename(); + if (name.empty()) + { + BMCWEB_LOG_ERROR << "Invalid sensor path: " << object.first; + return false; + } + + return name == sensorName; + }); + + if (it == subtree.end()) + { + BMCWEB_LOG_ERROR << "Could not find path for sensor: " + << sensorName; + messages::resourceNotFound(asyncResp->asyncResp->res, "Sensor", + sensorName); + return; + } + std::string_view sensorPath = (*it).first; + BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" << sensorName + << "': " << sensorPath; + + const std::shared_ptr<boost::container::flat_set<std::string>> + sensorList = + std::make_shared<boost::container::flat_set<std::string>>(); + + sensorList->emplace(sensorPath); + processSensorList(asyncResp, sensorList); + BMCWEB_LOG_DEBUG << "respHandler1 exit"; + }, + "xyz.openbmc_project.ObjectMapper", + "/xyz/openbmc_project/object_mapper", + "xyz.openbmc_project.ObjectMapper", "GetSubTree", + "/xyz/openbmc_project/sensors", 2, interfaces); +} + } // namespace sensors inline void requestRoutesSensorCollection(App& app) @@ -3044,83 +3123,7 @@ inline void requestRoutesSensor(App& app) BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/") .privileges(redfish::privileges::getSensor) .methods(boost::beast::http::verb::get)( - [&app](const crow::Request& req, - const std::shared_ptr<bmcweb::AsyncResp>& aResp, - const std::string& chassisId, - const std::string& sensorName) { - if (!redfish::setUpRedfishRoute(app, req, aResp->res)) - { - return; - } - BMCWEB_LOG_DEBUG << "Sensor doGet enter"; - std::shared_ptr<SensorsAsyncResp> asyncResp = - std::make_shared<SensorsAsyncResp>(aResp, chassisId, - std::span<std::string_view>(), - sensors::node::sensors); - - const std::array<const char*, 1> interfaces = { - "xyz.openbmc_project.Sensor.Value"}; - - // Get a list of all of the sensors that implement Sensor.Value - // and get the path and service name associated with the sensor - crow::connections::systemBus->async_method_call( - [asyncResp, sensorName]( - const boost::system::error_code ec, - const dbus::utility::MapperGetSubTreeResponse& subtree) { - BMCWEB_LOG_DEBUG << "respHandler1 enter"; - if (ec) - { - messages::internalError(asyncResp->asyncResp->res); - BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: " - << "Dbus error " << ec; - return; - } - - dbus::utility::MapperGetSubTreeResponse::const_iterator it = - std::find_if( - subtree.begin(), subtree.end(), - [sensorName]( - const std::pair< - std::string, - std::vector<std::pair<std::string, - std::vector<std::string>>>>& - object) { - sdbusplus::message::object_path path(object.first); - std::string name = path.filename(); - if (name.empty()) - { - BMCWEB_LOG_ERROR << "Invalid sensor path: " << object.first; - return false; - } - - return name == sensorName; - }); - - if (it == subtree.end()) - { - BMCWEB_LOG_ERROR << "Could not find path for sensor: " - << sensorName; - messages::resourceNotFound(asyncResp->asyncResp->res, "Sensor", - sensorName); - return; - } - std::string_view sensorPath = (*it).first; - BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" << sensorName - << "': " << sensorPath; - - const std::shared_ptr<boost::container::flat_set<std::string>> - sensorList = - std::make_shared<boost::container::flat_set<std::string>>(); - - sensorList->emplace(sensorPath); - processSensorList(asyncResp, sensorList); - BMCWEB_LOG_DEBUG << "respHandler1 exit"; - }, - "xyz.openbmc_project.ObjectMapper", - "/xyz/openbmc_project/object_mapper", - "xyz.openbmc_project.ObjectMapper", "GetSubTree", - "/xyz/openbmc_project/sensors", 2, interfaces); - }); + std::bind_front(sensors::handleSensorGet, std::ref(app))); } } // namespace redfish |