update_service: Refactor getSoftwareVersion into a seperate function
Moved the dbus call to SoftwareVersion into a function called
getSoftwareVersion to help organize the code for future supports/
This help thin out the code a bit and will be a bit easier to navigate
the code.
Tested:
Passed Redfish Validation test for UpdateService.
No change to the redfish tree.
Change-Id: I3cbc8a9c79272fc86a453e528820b2b3b95476d3
Signed-off-by: Willy Tu <wltu@google.com>
diff --git a/redfish-core/lib/update_service.hpp b/redfish-core/lib/update_service.hpp
index aa8b0e5..33769a0 100644
--- a/redfish-core/lib/update_service.hpp
+++ b/redfish-core/lib/update_service.hpp
@@ -816,6 +816,78 @@
}
}
+inline void
+ getSoftwareVersion(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+ const std::string& service, const std::string& path,
+ const std::string& swId)
+{
+ crow::connections::systemBus->async_method_call(
+ [asyncResp,
+ swId](const boost::system::error_code errorCode,
+ const dbus::utility::DBusPropertiesMap& propertiesList) {
+ if (errorCode)
+ {
+ messages::internalError(asyncResp->res);
+ return;
+ }
+ const std::string* swInvPurpose = nullptr;
+ const std::string* version = nullptr;
+ for (const auto& property : propertiesList)
+ {
+ if (property.first == "Purpose")
+ {
+ swInvPurpose = std::get_if<std::string>(&property.second);
+ }
+ else if (property.first == "Version")
+ {
+ version = std::get_if<std::string>(&property.second);
+ }
+ }
+
+ if (swInvPurpose == nullptr)
+ {
+ BMCWEB_LOG_DEBUG << "Can't find property \"Purpose\"!";
+ messages::internalError(asyncResp->res);
+ return;
+ }
+
+ BMCWEB_LOG_DEBUG << "swInvPurpose = " << *swInvPurpose;
+
+ if (version == nullptr)
+ {
+ BMCWEB_LOG_DEBUG << "Can't find property \"Version\"!";
+
+ messages::internalError(asyncResp->res);
+
+ return;
+ }
+ asyncResp->res.jsonValue["Version"] = *version;
+ asyncResp->res.jsonValue["Id"] = swId;
+
+ // swInvPurpose is of format:
+ // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
+ // Translate this to "ABC image"
+ size_t endDesc = swInvPurpose->rfind('.');
+ if (endDesc == std::string::npos)
+ {
+ messages::internalError(asyncResp->res);
+ return;
+ }
+ endDesc++;
+ if (endDesc >= swInvPurpose->size())
+ {
+ messages::internalError(asyncResp->res);
+ return;
+ }
+
+ std::string formatDesc = swInvPurpose->substr(endDesc);
+ asyncResp->res.jsonValue["Description"] = formatDesc + " image";
+ getRelatedItems(asyncResp, *swInvPurpose);
+ },
+ service, path, "org.freedesktop.DBus.Properties", "GetAll",
+ "xyz.openbmc_project.Software.Version");
+}
+
inline void requestRoutesSoftwareInventory(App& app)
{
BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
@@ -864,76 +936,8 @@
found = true;
sw_util::getSwStatus(asyncResp, swId, obj.second[0].first);
-
- crow::connections::systemBus->async_method_call(
- [asyncResp, swId](const boost::system::error_code errorCode,
- const dbus::utility::DBusPropertiesMap&
- propertiesList) {
- if (errorCode)
- {
- messages::internalError(asyncResp->res);
- return;
- }
- const std::string* swInvPurpose = nullptr;
- const std::string* version = nullptr;
- for (const auto& property : propertiesList)
- {
- if (property.first == "Purpose")
- {
- swInvPurpose =
- std::get_if<std::string>(&property.second);
- }
- if (property.first == "Version")
- {
- version =
- std::get_if<std::string>(&property.second);
- }
- }
-
- if (swInvPurpose == nullptr)
- {
- BMCWEB_LOG_DEBUG << "Can't find property \"Purpose\"!";
- messages::internalError(asyncResp->res);
- return;
- }
-
- BMCWEB_LOG_DEBUG << "swInvPurpose = " << *swInvPurpose;
-
- if (version == nullptr)
- {
- BMCWEB_LOG_DEBUG << "Can't find property \"Version\"!";
-
- messages::internalError(asyncResp->res);
-
- return;
- }
- asyncResp->res.jsonValue["Version"] = *version;
- asyncResp->res.jsonValue["Id"] = *swId;
-
- // swInvPurpose is of format:
- // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
- // Translate this to "ABC image"
- size_t endDesc = swInvPurpose->rfind('.');
- if (endDesc == std::string::npos)
- {
- messages::internalError(asyncResp->res);
- return;
- }
- endDesc++;
- if (endDesc >= swInvPurpose->size())
- {
- messages::internalError(asyncResp->res);
- return;
- }
-
- std::string formatDesc = swInvPurpose->substr(endDesc);
- asyncResp->res.jsonValue["Description"] =
- formatDesc + " image";
- getRelatedItems(asyncResp, *swInvPurpose);
- },
- obj.second[0].first, obj.first,
- "org.freedesktop.DBus.Properties", "GetAll",
- "xyz.openbmc_project.Software.Version");
+ getSoftwareVersion(asyncResp, obj.second[0].first, obj.first,
+ *swId);
}
if (!found)
{