140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0 240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors 3eee0013eSWilly Tu #pragma once 4d7857201SEd Tanous #include "bmcweb_config.h" 5d7857201SEd Tanous 63ccb3adbSEd Tanous #include "async_resp.hpp" 7*177612aaSEd Tanous #include "boost_formatters.hpp" 8d7857201SEd Tanous #include "dbus_singleton.hpp" 93ccb3adbSEd Tanous #include "dbus_utility.hpp" 103ccb3adbSEd Tanous #include "error_messages.hpp" 113ccb3adbSEd Tanous #include "generated/enums/resource.hpp" 12d7857201SEd Tanous #include "logging.hpp" 133ccb3adbSEd Tanous #include "utils/dbus_utils.hpp" 143ccb3adbSEd Tanous 15d7857201SEd Tanous #include <asm-generic/errno.h> 16d7857201SEd Tanous 17e99073f5SGeorge Liu #include <boost/system/error_code.hpp> 18ef4c65b7SEd Tanous #include <boost/url/format.hpp> 19d7857201SEd Tanous #include <nlohmann/json.hpp> 20d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp> 21d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp> 22eee0013eSWilly Tu 23eee0013eSWilly Tu #include <algorithm> 24e99073f5SGeorge Liu #include <array> 25d7857201SEd Tanous #include <functional> 26d7857201SEd Tanous #include <memory> 277f23576aSJagpal Singh Gill #include <optional> 283544d2a7SEd Tanous #include <ranges> 29eee0013eSWilly Tu #include <string> 30e99073f5SGeorge Liu #include <string_view> 31d7857201SEd Tanous #include <utility> 32eee0013eSWilly Tu #include <vector> 33eee0013eSWilly Tu 34eee0013eSWilly Tu namespace redfish 35eee0013eSWilly Tu { 36eee0013eSWilly Tu namespace sw_util 37eee0013eSWilly Tu { 38eee0013eSWilly Tu /* @brief String that indicates a bios software instance */ 39eee0013eSWilly Tu constexpr const char* biosPurpose = 40eee0013eSWilly Tu "xyz.openbmc_project.Software.Version.VersionPurpose.Host"; 41eee0013eSWilly Tu 42eee0013eSWilly Tu /* @brief String that indicates a BMC software instance */ 43eee0013eSWilly Tu constexpr const char* bmcPurpose = 44eee0013eSWilly Tu "xyz.openbmc_project.Software.Version.VersionPurpose.BMC"; 45eee0013eSWilly Tu 46504af5a0SPatrick Williams inline std::optional<sdbusplus::message::object_path> getFunctionalSoftwarePath( 47504af5a0SPatrick Williams const std::string& swType) 487f23576aSJagpal Singh Gill { 497f23576aSJagpal Singh Gill if (swType == bmcPurpose) 507f23576aSJagpal Singh Gill { 517f23576aSJagpal Singh Gill if constexpr (BMCWEB_REDFISH_UPDATESERVICE_USE_DBUS) 527f23576aSJagpal Singh Gill { 537f23576aSJagpal Singh Gill return sdbusplus::message::object_path( 547f23576aSJagpal Singh Gill "/xyz/openbmc_project/software/bmc/functional"); 557f23576aSJagpal Singh Gill } 567f23576aSJagpal Singh Gill else 577f23576aSJagpal Singh Gill { 587f23576aSJagpal Singh Gill return sdbusplus::message::object_path( 597f23576aSJagpal Singh Gill "/xyz/openbmc_project/software/functional"); 607f23576aSJagpal Singh Gill } 617f23576aSJagpal Singh Gill } 627f23576aSJagpal Singh Gill else if (swType == biosPurpose) 637f23576aSJagpal Singh Gill { 647f23576aSJagpal Singh Gill if constexpr (BMCWEB_REDFISH_UPDATESERVICE_USE_DBUS) 657f23576aSJagpal Singh Gill { 667f23576aSJagpal Singh Gill return sdbusplus::message::object_path( 677f23576aSJagpal Singh Gill "/xyz/openbmc_project/software/bios/functional"); 687f23576aSJagpal Singh Gill } 697f23576aSJagpal Singh Gill else 707f23576aSJagpal Singh Gill { 717f23576aSJagpal Singh Gill return sdbusplus::message::object_path( 727f23576aSJagpal Singh Gill "/xyz/openbmc_project/software/functional"); 737f23576aSJagpal Singh Gill } 747f23576aSJagpal Singh Gill } 757f23576aSJagpal Singh Gill else 767f23576aSJagpal Singh Gill { 777f23576aSJagpal Singh Gill BMCWEB_LOG_ERROR("No valid software path"); 787f23576aSJagpal Singh Gill return std::nullopt; 797f23576aSJagpal Singh Gill } 807f23576aSJagpal Singh Gill } 817f23576aSJagpal Singh Gill 82f8010a3dSEd Tanous inline void afterGetProperties( 83f8010a3dSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 84f8010a3dSEd Tanous const std::string& swId, bool runningImage, 85f8010a3dSEd Tanous const std::string& swVersionPurpose, 86f8010a3dSEd Tanous const std::string& activeVersionPropName, bool populateLinkToImages, 87f8010a3dSEd Tanous const boost::system::error_code& ec, 88f8010a3dSEd Tanous const dbus::utility::DBusPropertiesMap& propertiesList) 89f8010a3dSEd Tanous { 90f8010a3dSEd Tanous if (ec) 91f8010a3dSEd Tanous { 92f8010a3dSEd Tanous BMCWEB_LOG_ERROR("error_code = {}", ec); 93f8010a3dSEd Tanous BMCWEB_LOG_ERROR("error msg = {}", ec.message()); 94f8010a3dSEd Tanous // Have seen the code update app delete the 95f8010a3dSEd Tanous // D-Bus object, during code update, between 96f8010a3dSEd Tanous // the call to mapper and here. Just leave 97f8010a3dSEd Tanous // these properties off if resource not 98f8010a3dSEd Tanous // found. 99f8010a3dSEd Tanous if (ec.value() == EBADR) 100f8010a3dSEd Tanous { 101f8010a3dSEd Tanous return; 102f8010a3dSEd Tanous } 103f8010a3dSEd Tanous messages::internalError(asyncResp->res); 104f8010a3dSEd Tanous return; 105f8010a3dSEd Tanous } 106f8010a3dSEd Tanous // example propertiesList 107f8010a3dSEd Tanous // a{sv} 2 "Version" s 108f8010a3dSEd Tanous // "IBM-witherspoon-OP9-v2.0.10-2.22" "Purpose" 109f8010a3dSEd Tanous // s 110f8010a3dSEd Tanous // "xyz.openbmc_project.Software.Version.VersionPurpose.Host" 111f8010a3dSEd Tanous const std::string* version = nullptr; 112f8010a3dSEd Tanous const std::string* swInvPurpose = nullptr; 113f8010a3dSEd Tanous 114f8010a3dSEd Tanous const bool success = sdbusplus::unpackPropertiesNoThrow( 115f8010a3dSEd Tanous dbus_utils::UnpackErrorPrinter(), propertiesList, "Purpose", 116f8010a3dSEd Tanous swInvPurpose, "Version", version); 117f8010a3dSEd Tanous 118f8010a3dSEd Tanous if (!success) 119f8010a3dSEd Tanous { 120f8010a3dSEd Tanous messages::internalError(asyncResp->res); 121f8010a3dSEd Tanous return; 122f8010a3dSEd Tanous } 123f8010a3dSEd Tanous 124f8010a3dSEd Tanous if (version == nullptr || version->empty()) 125f8010a3dSEd Tanous { 126f8010a3dSEd Tanous messages::internalError(asyncResp->res); 127f8010a3dSEd Tanous return; 128f8010a3dSEd Tanous } 129f8010a3dSEd Tanous if (swInvPurpose == nullptr || *swInvPurpose != swVersionPurpose) 130f8010a3dSEd Tanous { 131f8010a3dSEd Tanous // Not purpose we're looking for 132f8010a3dSEd Tanous return; 133f8010a3dSEd Tanous } 134f8010a3dSEd Tanous 135f8010a3dSEd Tanous BMCWEB_LOG_DEBUG("Image ID: {}", swId); 136f8010a3dSEd Tanous BMCWEB_LOG_DEBUG("Running image: {}", runningImage); 137f8010a3dSEd Tanous BMCWEB_LOG_DEBUG("Image purpose: {}", *swInvPurpose); 138f8010a3dSEd Tanous 139f8010a3dSEd Tanous if (populateLinkToImages) 140f8010a3dSEd Tanous { 141f8010a3dSEd Tanous nlohmann::json& softwareImageMembers = 142f8010a3dSEd Tanous asyncResp->res.jsonValue["Links"]["SoftwareImages"]; 143f8010a3dSEd Tanous // Firmware images are at 144f8010a3dSEd Tanous // /redfish/v1/UpdateService/FirmwareInventory/<Id> 145f8010a3dSEd Tanous // e.g. .../FirmwareInventory/82d3ec86 146f8010a3dSEd Tanous nlohmann::json::object_t member; 147f8010a3dSEd Tanous member["@odata.id"] = boost::urls::format( 148f8010a3dSEd Tanous "/redfish/v1/UpdateService/FirmwareInventory/{}", swId); 149f8010a3dSEd Tanous softwareImageMembers.emplace_back(std::move(member)); 150f8010a3dSEd Tanous asyncResp->res.jsonValue["Links"]["SoftwareImages@odata.count"] = 151f8010a3dSEd Tanous softwareImageMembers.size(); 152f8010a3dSEd Tanous 153f8010a3dSEd Tanous if (runningImage) 154f8010a3dSEd Tanous { 155f8010a3dSEd Tanous nlohmann::json::object_t runningMember; 156f8010a3dSEd Tanous runningMember["@odata.id"] = boost::urls::format( 157f8010a3dSEd Tanous "/redfish/v1/UpdateService/FirmwareInventory/{}", swId); 158f8010a3dSEd Tanous // Create the link to the running image 159f8010a3dSEd Tanous asyncResp->res.jsonValue["Links"]["ActiveSoftwareImage"] = 160f8010a3dSEd Tanous std::move(runningMember); 161f8010a3dSEd Tanous } 162f8010a3dSEd Tanous } 163f8010a3dSEd Tanous if (!activeVersionPropName.empty() && runningImage) 164f8010a3dSEd Tanous { 165f8010a3dSEd Tanous asyncResp->res.jsonValue[activeVersionPropName] = *version; 166f8010a3dSEd Tanous } 167f8010a3dSEd Tanous } 168f8010a3dSEd Tanous 169f8010a3dSEd Tanous inline void afterGetSubtree( 170f8010a3dSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 171f8010a3dSEd Tanous const std::string& swVersionPurpose, 172f8010a3dSEd Tanous const std::string& activeVersionPropName, bool populateLinkToImages, 173f8010a3dSEd Tanous const std::vector<std::string>& functionalSwIds, 174f8010a3dSEd Tanous const boost::system::error_code& ec, 175f8010a3dSEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) 176f8010a3dSEd Tanous { 177f8010a3dSEd Tanous if (ec) 178f8010a3dSEd Tanous { 179f8010a3dSEd Tanous BMCWEB_LOG_ERROR("error_code = {}", ec); 180f8010a3dSEd Tanous BMCWEB_LOG_ERROR("error msg = {}", ec.message()); 181f8010a3dSEd Tanous messages::internalError(asyncResp->res); 182f8010a3dSEd Tanous return; 183f8010a3dSEd Tanous } 184f8010a3dSEd Tanous 185f8010a3dSEd Tanous BMCWEB_LOG_DEBUG("Found {} images", subtree.size()); 186f8010a3dSEd Tanous 187f8010a3dSEd Tanous for (const std::pair< 188f8010a3dSEd Tanous std::string, 189f8010a3dSEd Tanous std::vector<std::pair<std::string, std::vector<std::string>>>>& 190f8010a3dSEd Tanous obj : subtree) 191f8010a3dSEd Tanous { 192f8010a3dSEd Tanous sdbusplus::message::object_path path(obj.first); 193f8010a3dSEd Tanous std::string swId = path.filename(); 194f8010a3dSEd Tanous if (swId.empty()) 195f8010a3dSEd Tanous { 196f8010a3dSEd Tanous messages::internalError(asyncResp->res); 197f8010a3dSEd Tanous BMCWEB_LOG_ERROR("Invalid software ID"); 198f8010a3dSEd Tanous 199f8010a3dSEd Tanous return; 200f8010a3dSEd Tanous } 201f8010a3dSEd Tanous 202f8010a3dSEd Tanous bool runningImage = false; 203f8010a3dSEd Tanous // Look at Ids from 204f8010a3dSEd Tanous // /xyz/openbmc_project/software/functional 205f8010a3dSEd Tanous // to determine if this is 206f8010a3dSEd Tanous // a running image 207f8010a3dSEd Tanous if (std::ranges::find(functionalSwIds, swId) != functionalSwIds.end()) 208f8010a3dSEd Tanous { 209f8010a3dSEd Tanous runningImage = true; 210f8010a3dSEd Tanous } 211f8010a3dSEd Tanous 212f8010a3dSEd Tanous // Now grab its version 213f8010a3dSEd Tanous // info 214deae6a78SEd Tanous dbus::utility::getAllProperties( 215f8010a3dSEd Tanous *crow::connections::systemBus, obj.second[0].first, obj.first, 216f8010a3dSEd Tanous "xyz.openbmc_project.Software.Version", 217f8010a3dSEd Tanous [asyncResp, swId, runningImage, swVersionPurpose, 218f8010a3dSEd Tanous activeVersionPropName, populateLinkToImages]( 219f8010a3dSEd Tanous const boost::system::error_code& ec3, 220f8010a3dSEd Tanous const dbus::utility::DBusPropertiesMap& propertiesList) { 221f8010a3dSEd Tanous afterGetProperties(asyncResp, swId, runningImage, 222f8010a3dSEd Tanous swVersionPurpose, activeVersionPropName, 223f8010a3dSEd Tanous populateLinkToImages, ec3, propertiesList); 224f8010a3dSEd Tanous }); 225f8010a3dSEd Tanous } 226f8010a3dSEd Tanous } 227f8010a3dSEd Tanous 228f8010a3dSEd Tanous inline void afterAssociatedEndpoints( 229f8010a3dSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 230f8010a3dSEd Tanous const std::string& swVersionPurpose, 231f8010a3dSEd Tanous const std::string& activeVersionPropName, bool populateLinkToImages, 232f8010a3dSEd Tanous const boost::system::error_code& ec, 233f8010a3dSEd Tanous const dbus::utility::MapperEndPoints& functionalSw) 234f8010a3dSEd Tanous { 235f8010a3dSEd Tanous BMCWEB_LOG_DEBUG("populateSoftwareInformation enter"); 236f8010a3dSEd Tanous if (ec) 237f8010a3dSEd Tanous { 238a5f87d48SGunnar Mills BMCWEB_LOG_DEBUG("error_code = {}", ec); 239a5f87d48SGunnar Mills BMCWEB_LOG_DEBUG("error msg = {}", ec.message()); 240f8010a3dSEd Tanous // No functional software for this swVersionPurpose, so just 241f8010a3dSEd Tanous return; 242f8010a3dSEd Tanous } 243f8010a3dSEd Tanous 244f8010a3dSEd Tanous if (functionalSw.empty()) 245f8010a3dSEd Tanous { 246f8010a3dSEd Tanous // Could keep going and try to populate SoftwareImages 247f8010a3dSEd Tanous // but something is seriously wrong, so just fail 248f8010a3dSEd Tanous BMCWEB_LOG_ERROR("Zero functional software in system"); 249f8010a3dSEd Tanous messages::internalError(asyncResp->res); 250f8010a3dSEd Tanous return; 251f8010a3dSEd Tanous } 252f8010a3dSEd Tanous 253f8010a3dSEd Tanous std::vector<std::string> functionalSwIds; 254f8010a3dSEd Tanous // example functionalSw: 255f8010a3dSEd Tanous // v as 2 "/xyz/openbmc_project/software/ace821ef" 256f8010a3dSEd Tanous // "/xyz/openbmc_project/software/230fb078" 257f8010a3dSEd Tanous for (const auto& sw : functionalSw) 258f8010a3dSEd Tanous { 259f8010a3dSEd Tanous sdbusplus::message::object_path path(sw); 260f8010a3dSEd Tanous std::string leaf = path.filename(); 261f8010a3dSEd Tanous if (leaf.empty()) 262f8010a3dSEd Tanous { 263f8010a3dSEd Tanous continue; 264f8010a3dSEd Tanous } 265f8010a3dSEd Tanous 266f8010a3dSEd Tanous functionalSwIds.push_back(leaf); 267f8010a3dSEd Tanous } 268f8010a3dSEd Tanous 269f8010a3dSEd Tanous constexpr std::array<std::string_view, 1> interfaces = { 270f8010a3dSEd Tanous "xyz.openbmc_project.Software.Version"}; 271f8010a3dSEd Tanous dbus::utility::getSubTree( 272f8010a3dSEd Tanous "/xyz/openbmc_project/software", 0, interfaces, 273f8010a3dSEd Tanous [asyncResp, swVersionPurpose, activeVersionPropName, 274f8010a3dSEd Tanous populateLinkToImages, functionalSwIds]( 275f8010a3dSEd Tanous const boost::system::error_code& ec2, 276f8010a3dSEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 277f8010a3dSEd Tanous afterGetSubtree(asyncResp, swVersionPurpose, activeVersionPropName, 278f8010a3dSEd Tanous populateLinkToImages, functionalSwIds, ec2, 279f8010a3dSEd Tanous subtree); 280f8010a3dSEd Tanous }); 281f8010a3dSEd Tanous } 282f8010a3dSEd Tanous 283eee0013eSWilly Tu /** 284eee0013eSWilly Tu * @brief Populate the running software version and image links 285eee0013eSWilly Tu * 286ac106bf6SEd Tanous * @param[i,o] asyncResp Async response object 287eee0013eSWilly Tu * @param[i] swVersionPurpose Indicates what target to look for 288ac106bf6SEd Tanous * @param[i] activeVersionPropName Index in asyncResp->res.jsonValue to write 289eee0013eSWilly Tu * the running software version to 290ac106bf6SEd Tanous * @param[i] populateLinkToImages Populate asyncResp->res "Links" 291eee0013eSWilly Tu * "ActiveSoftwareImage" with a link to the running software image and 292eee0013eSWilly Tu * "SoftwareImages" with a link to the all its software images 293eee0013eSWilly Tu * 294eee0013eSWilly Tu * @return void 295eee0013eSWilly Tu */ 296ac106bf6SEd Tanous inline void populateSoftwareInformation( 297ac106bf6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 298eee0013eSWilly Tu const std::string& swVersionPurpose, 299ac106bf6SEd Tanous const std::string& activeVersionPropName, const bool populateLinkToImages) 300eee0013eSWilly Tu { 3017f23576aSJagpal Singh Gill auto swPath = getFunctionalSoftwarePath(swVersionPurpose); 3027f23576aSJagpal Singh Gill if (!swPath) 3037f23576aSJagpal Singh Gill { 3047f23576aSJagpal Singh Gill BMCWEB_LOG_ERROR("Invalid software type"); 3057f23576aSJagpal Singh Gill messages::internalError(asyncResp->res); 3067f23576aSJagpal Singh Gill return; 3077f23576aSJagpal Singh Gill } 308eee0013eSWilly Tu // Used later to determine running (known on Redfish as active) Sw images 309a4eb761aSGeorge Liu dbus::utility::getAssociationEndPoints( 3107f23576aSJagpal Singh Gill swPath.value().str, 311ac106bf6SEd Tanous [asyncResp, swVersionPurpose, activeVersionPropName, 312ac106bf6SEd Tanous populateLinkToImages]( 313a4eb761aSGeorge Liu const boost::system::error_code& ec, 314a4eb761aSGeorge Liu const dbus::utility::MapperEndPoints& functionalSw) { 315f8010a3dSEd Tanous afterAssociatedEndpoints(asyncResp, swVersionPurpose, 316f8010a3dSEd Tanous activeVersionPropName, 317f8010a3dSEd Tanous populateLinkToImages, ec, functionalSw); 318eee0013eSWilly Tu }); 319eee0013eSWilly Tu } 320eee0013eSWilly Tu 321eee0013eSWilly Tu /** 322eee0013eSWilly Tu * @brief Translate input swState to Redfish state 323eee0013eSWilly Tu * 324eee0013eSWilly Tu * This function will return the corresponding Redfish state 325eee0013eSWilly Tu * 326eee0013eSWilly Tu * @param[i] swState The OpenBMC software state 327eee0013eSWilly Tu * 328eee0013eSWilly Tu * @return The corresponding Redfish state 329eee0013eSWilly Tu */ 3300ec8b83dSEd Tanous inline resource::State getRedfishSwState(const std::string& swState) 331eee0013eSWilly Tu { 332eee0013eSWilly Tu if (swState == "xyz.openbmc_project.Software.Activation.Activations.Active") 333eee0013eSWilly Tu { 3340ec8b83dSEd Tanous return resource::State::Enabled; 335eee0013eSWilly Tu } 336eee0013eSWilly Tu if (swState == "xyz.openbmc_project.Software.Activation." 337eee0013eSWilly Tu "Activations.Activating") 338eee0013eSWilly Tu { 3390ec8b83dSEd Tanous return resource::State::Updating; 340eee0013eSWilly Tu } 341eee0013eSWilly Tu if (swState == "xyz.openbmc_project.Software.Activation." 342eee0013eSWilly Tu "Activations.StandbySpare") 343eee0013eSWilly Tu { 3440ec8b83dSEd Tanous return resource::State::StandbySpare; 345eee0013eSWilly Tu } 34662598e31SEd Tanous BMCWEB_LOG_DEBUG("Default sw state {} to Disabled", swState); 3470ec8b83dSEd Tanous return resource::State::Disabled; 348eee0013eSWilly Tu } 349eee0013eSWilly Tu 350eee0013eSWilly Tu /** 351eee0013eSWilly Tu * @brief Translate input swState to Redfish health state 352eee0013eSWilly Tu * 353eee0013eSWilly Tu * This function will return the corresponding Redfish health state 354eee0013eSWilly Tu * 355eee0013eSWilly Tu * @param[i] swState The OpenBMC software state 356eee0013eSWilly Tu * 357eee0013eSWilly Tu * @return The corresponding Redfish health state 358eee0013eSWilly Tu */ 359eee0013eSWilly Tu inline std::string getRedfishSwHealth(const std::string& swState) 360eee0013eSWilly Tu { 361eee0013eSWilly Tu if ((swState == 362eee0013eSWilly Tu "xyz.openbmc_project.Software.Activation.Activations.Active") || 363eee0013eSWilly Tu (swState == "xyz.openbmc_project.Software.Activation.Activations." 364eee0013eSWilly Tu "Activating") || 365eee0013eSWilly Tu (swState == 366eee0013eSWilly Tu "xyz.openbmc_project.Software.Activation.Activations.Ready")) 367eee0013eSWilly Tu { 368eee0013eSWilly Tu return "OK"; 369eee0013eSWilly Tu } 37062598e31SEd Tanous BMCWEB_LOG_DEBUG("Sw state {} to Warning", swState); 371eee0013eSWilly Tu return "Warning"; 372eee0013eSWilly Tu } 373eee0013eSWilly Tu 374eee0013eSWilly Tu /** 375eee0013eSWilly Tu * @brief Put status of input swId into json response 376eee0013eSWilly Tu * 377eee0013eSWilly Tu * This function will put the appropriate Redfish state of the input 378eee0013eSWilly Tu * software id to ["Status"]["State"] within the json response 379eee0013eSWilly Tu * 380ac106bf6SEd Tanous * @param[i,o] asyncResp Async response object 381eee0013eSWilly Tu * @param[i] swId The software ID to get status for 382eee0013eSWilly Tu * @param[i] dbusSvc The dbus service implementing the software object 383eee0013eSWilly Tu * 384eee0013eSWilly Tu * @return void 385eee0013eSWilly Tu */ 386eee0013eSWilly Tu inline void getSwStatus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 387eee0013eSWilly Tu const std::shared_ptr<std::string>& swId, 388eee0013eSWilly Tu const std::string& dbusSvc) 389eee0013eSWilly Tu { 39062598e31SEd Tanous BMCWEB_LOG_DEBUG("getSwStatus: swId {} svc {}", *swId, dbusSvc); 391eee0013eSWilly Tu 392deae6a78SEd Tanous dbus::utility::getAllProperties( 393deae6a78SEd Tanous dbusSvc, "/xyz/openbmc_project/software/" + *swId, 394d1bde9e5SKrzysztof Grobelny "xyz.openbmc_project.Software.Activation", 395eee0013eSWilly Tu [asyncResp, 3968b24275dSEd Tanous swId](const boost::system::error_code& ec, 397eee0013eSWilly Tu const dbus::utility::DBusPropertiesMap& propertiesList) { 3988b24275dSEd Tanous if (ec) 399eee0013eSWilly Tu { 400eee0013eSWilly Tu // not all swtypes are updateable, this is ok 401539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] = 402539d8c6bSEd Tanous resource::State::Enabled; 403eee0013eSWilly Tu return; 404eee0013eSWilly Tu } 405d1bde9e5SKrzysztof Grobelny 406eee0013eSWilly Tu const std::string* swInvActivation = nullptr; 407d1bde9e5SKrzysztof Grobelny 408d1bde9e5SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 409d1bde9e5SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), propertiesList, "Activation", 410d1bde9e5SKrzysztof Grobelny swInvActivation); 411d1bde9e5SKrzysztof Grobelny 412d1bde9e5SKrzysztof Grobelny if (!success) 413eee0013eSWilly Tu { 414d1bde9e5SKrzysztof Grobelny messages::internalError(asyncResp->res); 415d1bde9e5SKrzysztof Grobelny return; 416eee0013eSWilly Tu } 417eee0013eSWilly Tu 418eee0013eSWilly Tu if (swInvActivation == nullptr) 419eee0013eSWilly Tu { 420eee0013eSWilly Tu messages::internalError(asyncResp->res); 421eee0013eSWilly Tu return; 422eee0013eSWilly Tu } 423d1bde9e5SKrzysztof Grobelny 42462598e31SEd Tanous BMCWEB_LOG_DEBUG("getSwStatus: Activation {}", *swInvActivation); 425eee0013eSWilly Tu asyncResp->res.jsonValue["Status"]["State"] = 426eee0013eSWilly Tu getRedfishSwState(*swInvActivation); 427eee0013eSWilly Tu asyncResp->res.jsonValue["Status"]["Health"] = 428eee0013eSWilly Tu getRedfishSwHealth(*swInvActivation); 429d1bde9e5SKrzysztof Grobelny }); 430eee0013eSWilly Tu } 431eee0013eSWilly Tu 432c018f705SJagpal Singh Gill inline void handleUpdateableEndpoints( 433c018f705SJagpal Singh Gill const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 434c018f705SJagpal Singh Gill const std::shared_ptr<std::string>& swId, 435c018f705SJagpal Singh Gill const boost::system::error_code& ec, 436c018f705SJagpal Singh Gill const dbus::utility::MapperEndPoints& objPaths) 437c018f705SJagpal Singh Gill { 438c018f705SJagpal Singh Gill if (ec) 439c018f705SJagpal Singh Gill { 440c018f705SJagpal Singh Gill BMCWEB_LOG_DEBUG(" error_code = {} error msg = {}", ec, ec.message()); 441c018f705SJagpal Singh Gill // System can exist with no updateable software, 442c018f705SJagpal Singh Gill // so don't throw error here. 443c018f705SJagpal Singh Gill return; 444c018f705SJagpal Singh Gill } 445c018f705SJagpal Singh Gill sdbusplus::message::object_path reqSwObjPath( 446c018f705SJagpal Singh Gill "/xyz/openbmc_project/software"); 447c018f705SJagpal Singh Gill reqSwObjPath = reqSwObjPath / *swId; 448c018f705SJagpal Singh Gill 449c018f705SJagpal Singh Gill if (std::ranges::find(objPaths, reqSwObjPath.str) != objPaths.end()) 450c018f705SJagpal Singh Gill { 451c018f705SJagpal Singh Gill asyncResp->res.jsonValue["Updateable"] = true; 452c018f705SJagpal Singh Gill return; 453c018f705SJagpal Singh Gill } 454c018f705SJagpal Singh Gill } 455c018f705SJagpal Singh Gill 456504af5a0SPatrick Williams inline void handleUpdateableObject( 457504af5a0SPatrick Williams const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 458c018f705SJagpal Singh Gill const boost::system::error_code& ec, 459c018f705SJagpal Singh Gill const dbus::utility::MapperGetObject& objectInfo) 460c018f705SJagpal Singh Gill { 461c018f705SJagpal Singh Gill if (ec) 462c018f705SJagpal Singh Gill { 463c018f705SJagpal Singh Gill BMCWEB_LOG_DEBUG(" error_code = {} error msg = {}", ec, ec.message()); 464c018f705SJagpal Singh Gill // System can exist with no updateable software, 465c018f705SJagpal Singh Gill // so don't throw error here. 466c018f705SJagpal Singh Gill return; 467c018f705SJagpal Singh Gill } 468c018f705SJagpal Singh Gill if (objectInfo.empty()) 469c018f705SJagpal Singh Gill { 470c018f705SJagpal Singh Gill BMCWEB_LOG_DEBUG("No updateable software found"); 471c018f705SJagpal Singh Gill // System can exist with no updateable software, 472c018f705SJagpal Singh Gill // so don't throw error here. 473c018f705SJagpal Singh Gill return; 474c018f705SJagpal Singh Gill } 475c018f705SJagpal Singh Gill asyncResp->res.jsonValue["Updateable"] = true; 476c018f705SJagpal Singh Gill } 477c018f705SJagpal Singh Gill 478eee0013eSWilly Tu /** 479eee0013eSWilly Tu * @brief Updates programmable status of input swId into json response 480eee0013eSWilly Tu * 481eee0013eSWilly Tu * This function checks whether software inventory component 482eee0013eSWilly Tu * can be programmable or not and fill's the "Updatable" 483eee0013eSWilly Tu * Property. 484eee0013eSWilly Tu * 485eee0013eSWilly Tu * @param[i,o] asyncResp Async response object 486eee0013eSWilly Tu * @param[i] swId The software ID 487eee0013eSWilly Tu */ 488504af5a0SPatrick Williams inline void getSwUpdatableStatus( 489504af5a0SPatrick Williams const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 490eee0013eSWilly Tu const std::shared_ptr<std::string>& swId) 491eee0013eSWilly Tu { 492c018f705SJagpal Singh Gill if constexpr (BMCWEB_REDFISH_UPDATESERVICE_USE_DBUS) 493c018f705SJagpal Singh Gill { 494c018f705SJagpal Singh Gill sdbusplus::message::object_path swObjectPath( 495c018f705SJagpal Singh Gill "/xyz/openbmc_project/software"); 496c018f705SJagpal Singh Gill swObjectPath = swObjectPath / *swId; 497c018f705SJagpal Singh Gill constexpr std::array<std::string_view, 1> interfaces = { 498c018f705SJagpal Singh Gill "xyz.openbmc_project.Software.Update"}; 499c018f705SJagpal Singh Gill dbus::utility::getDbusObject( 500c018f705SJagpal Singh Gill swObjectPath.str, interfaces, 501c018f705SJagpal Singh Gill std::bind_front(handleUpdateableObject, asyncResp)); 502c018f705SJagpal Singh Gill } 503c018f705SJagpal Singh Gill else 504c018f705SJagpal Singh Gill { 5056c3e9451SGeorge Liu dbus::utility::getAssociationEndPoints( 506eee0013eSWilly Tu "/xyz/openbmc_project/software/updateable", 507c018f705SJagpal Singh Gill std::bind_front(handleUpdateableEndpoints, asyncResp, swId)); 508eee0013eSWilly Tu } 509eee0013eSWilly Tu } 510eee0013eSWilly Tu 511eee0013eSWilly Tu } // namespace sw_util 512eee0013eSWilly Tu } // namespace redfish 513