xref: /openbmc/bmcweb/features/redfish/lib/systems.hpp (revision fc5ae94d7037b35b28b8ee3c1eda1a07ff62d857)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
340e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
4c5b2abe0SLewanczyk, Dawid #pragma once
5c5b2abe0SLewanczyk, Dawid 
613451e39SWilly Tu #include "bmcweb_config.h"
713451e39SWilly Tu 
83ccb3adbSEd Tanous #include "app.hpp"
9d7857201SEd Tanous #include "async_resp.hpp"
101e1e598dSJonathan Doman #include "dbus_singleton.hpp"
117a1dbc48SGeorge Liu #include "dbus_utility.hpp"
12d7857201SEd Tanous #include "error_messages.hpp"
13539d8c6bSEd Tanous #include "generated/enums/action_info.hpp"
148d69c668SEd Tanous #include "generated/enums/computer_system.hpp"
15539d8c6bSEd Tanous #include "generated/enums/open_bmc_computer_system.hpp"
1633e1f122SAndrew Geissler #include "generated/enums/resource.hpp"
17d7857201SEd Tanous #include "http_request.hpp"
18746b56f3SAsmitha Karunanithi #include "hypervisor_system.hpp"
191c8fba97SJames Feist #include "led.hpp"
20d7857201SEd Tanous #include "logging.hpp"
21f4c99e70SEd Tanous #include "query.hpp"
22c5d03ff4SJennifer Lee #include "redfish_util.hpp"
233ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
243ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
253ccb3adbSEd Tanous #include "utils/json_utils.hpp"
26472bd202SLakshmi Yadlapati #include "utils/pcie_util.hpp"
273ccb3adbSEd Tanous #include "utils/sw_utils.hpp"
28*fc5ae94dSOliver Brewka #include "utils/systems_utils.hpp"
292b82937eSEd Tanous #include "utils/time_utils.hpp"
30c5d03ff4SJennifer Lee 
31d7857201SEd Tanous #include <asm-generic/errno.h>
32d7857201SEd Tanous 
33fc903b3dSAndrew Geissler #include <boost/asio/error.hpp>
34d7857201SEd Tanous #include <boost/beast/http/field.hpp>
35d7857201SEd Tanous #include <boost/beast/http/status.hpp>
36d7857201SEd Tanous #include <boost/beast/http/verb.hpp>
37e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
3833e1f122SAndrew Geissler #include <boost/system/linux_error.hpp>
39ef4c65b7SEd Tanous #include <boost/url/format.hpp>
40d7857201SEd Tanous #include <nlohmann/json.hpp>
41d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
42bc1d29deSKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
431214b7e7SGunnar Mills 
447a1dbc48SGeorge Liu #include <array>
45d7857201SEd Tanous #include <chrono>
46d7857201SEd Tanous #include <cstddef>
47d7857201SEd Tanous #include <cstdint>
48d7857201SEd Tanous #include <functional>
4933e1f122SAndrew Geissler #include <memory>
50d7857201SEd Tanous #include <optional>
51d7857201SEd Tanous #include <ratio>
526b9ac4f2SChris Cain #include <string>
537a1dbc48SGeorge Liu #include <string_view>
54d7857201SEd Tanous #include <tuple>
5520fa6a2cSEd Tanous #include <utility>
566b9ac4f2SChris Cain #include <vector>
57c5b2abe0SLewanczyk, Dawid 
581abe55efSEd Tanous namespace redfish
591abe55efSEd Tanous {
60c5b2abe0SLewanczyk, Dawid 
615c3e9272SAbhishek Patel const static std::array<std::pair<std::string_view, std::string_view>, 2>
625c3e9272SAbhishek Patel     protocolToDBusForSystems{
635c3e9272SAbhishek Patel         {{"SSH", "obmc-console-ssh"}, {"IPMI", "phosphor-ipmi-net"}}};
645c3e9272SAbhishek Patel 
659d3ae10eSAlpana Kumari /**
669d3ae10eSAlpana Kumari  * @brief Updates the Functional State of DIMMs
679d3ae10eSAlpana Kumari  *
68ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
699d3ae10eSAlpana Kumari  * @param[in] dimmState Dimm's Functional state, true/false
709d3ae10eSAlpana Kumari  *
719d3ae10eSAlpana Kumari  * @return None.
729d3ae10eSAlpana Kumari  */
73bd79bce8SPatrick Williams inline void updateDimmProperties(
74bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, bool isDimmFunctional)
759d3ae10eSAlpana Kumari {
7662598e31SEd Tanous     BMCWEB_LOG_DEBUG("Dimm Functional: {}", isDimmFunctional);
779d3ae10eSAlpana Kumari 
789d3ae10eSAlpana Kumari     // Set it as Enabled if at least one DIMM is functional
799d3ae10eSAlpana Kumari     // Update STATE only if previous State was DISABLED and current Dimm is
809d3ae10eSAlpana Kumari     // ENABLED.
8102cad96eSEd Tanous     const nlohmann::json& prevMemSummary =
82ac106bf6SEd Tanous         asyncResp->res.jsonValue["MemorySummary"]["Status"]["State"];
839d3ae10eSAlpana Kumari     if (prevMemSummary == "Disabled")
849d3ae10eSAlpana Kumari     {
85e05aec50SEd Tanous         if (isDimmFunctional)
869d3ae10eSAlpana Kumari         {
87ac106bf6SEd Tanous             asyncResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
889d3ae10eSAlpana Kumari                 "Enabled";
899d3ae10eSAlpana Kumari         }
909d3ae10eSAlpana Kumari     }
919d3ae10eSAlpana Kumari }
929d3ae10eSAlpana Kumari 
9357e8c9beSAlpana Kumari /*
9457e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Status" "State" based on
9557e8c9beSAlpana Kumari  *        CPU Functional State
9657e8c9beSAlpana Kumari  *
97ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
9857e8c9beSAlpana Kumari  * @param[in] cpuFunctionalState is CPU functional true/false
9957e8c9beSAlpana Kumari  *
10057e8c9beSAlpana Kumari  * @return None.
10157e8c9beSAlpana Kumari  */
102ac106bf6SEd Tanous inline void modifyCpuFunctionalState(
103ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, bool isCpuFunctional)
10457e8c9beSAlpana Kumari {
10562598e31SEd Tanous     BMCWEB_LOG_DEBUG("Cpu Functional: {}", isCpuFunctional);
10657e8c9beSAlpana Kumari 
10702cad96eSEd Tanous     const nlohmann::json& prevProcState =
108ac106bf6SEd Tanous         asyncResp->res.jsonValue["ProcessorSummary"]["Status"]["State"];
10957e8c9beSAlpana Kumari 
11057e8c9beSAlpana Kumari     // Set it as Enabled if at least one CPU is functional
11157e8c9beSAlpana Kumari     // Update STATE only if previous State was Non_Functional and current CPU is
11257e8c9beSAlpana Kumari     // Functional.
11357e8c9beSAlpana Kumari     if (prevProcState == "Disabled")
11457e8c9beSAlpana Kumari     {
115e05aec50SEd Tanous         if (isCpuFunctional)
11657e8c9beSAlpana Kumari         {
117ac106bf6SEd Tanous             asyncResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
11857e8c9beSAlpana Kumari                 "Enabled";
11957e8c9beSAlpana Kumari         }
12057e8c9beSAlpana Kumari     }
12157e8c9beSAlpana Kumari }
12257e8c9beSAlpana Kumari 
123cf0e004cSNinad Palsule /*
124cf0e004cSNinad Palsule  * @brief Update "ProcessorSummary" "Count" based on Cpu PresenceState
125cf0e004cSNinad Palsule  *
126ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
127cf0e004cSNinad Palsule  * @param[in] cpuPresenceState CPU present or not
128cf0e004cSNinad Palsule  *
129cf0e004cSNinad Palsule  * @return None.
130cf0e004cSNinad Palsule  */
131bd79bce8SPatrick Williams inline void modifyCpuPresenceState(
132bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, bool isCpuPresent)
133cf0e004cSNinad Palsule {
13462598e31SEd Tanous     BMCWEB_LOG_DEBUG("Cpu Present: {}", isCpuPresent);
135cf0e004cSNinad Palsule 
136cf0e004cSNinad Palsule     if (isCpuPresent)
137cf0e004cSNinad Palsule     {
138cf0e004cSNinad Palsule         nlohmann::json& procCount =
139ac106bf6SEd Tanous             asyncResp->res.jsonValue["ProcessorSummary"]["Count"];
140cf0e004cSNinad Palsule         auto* procCountPtr =
141cf0e004cSNinad Palsule             procCount.get_ptr<nlohmann::json::number_integer_t*>();
142cf0e004cSNinad Palsule         if (procCountPtr != nullptr)
143cf0e004cSNinad Palsule         {
144cf0e004cSNinad Palsule             // shouldn't be possible to be nullptr
145cf0e004cSNinad Palsule             *procCountPtr += 1;
146cf0e004cSNinad Palsule         }
147cf0e004cSNinad Palsule     }
148cf0e004cSNinad Palsule }
149cf0e004cSNinad Palsule 
150382d6475SAli Ahmed inline void getProcessorProperties(
151ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
152382d6475SAli Ahmed     const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
153382d6475SAli Ahmed         properties)
15403fbed92SAli Ahmed {
15562598e31SEd Tanous     BMCWEB_LOG_DEBUG("Got {} Cpu properties.", properties.size());
15603fbed92SAli Ahmed 
15703fbed92SAli Ahmed     // TODO: Get Model
15803fbed92SAli Ahmed 
159bc1d29deSKrzysztof Grobelny     const uint16_t* coreCount = nullptr;
16003fbed92SAli Ahmed 
161bc1d29deSKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
162bc1d29deSKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), properties, "CoreCount", coreCount);
16303fbed92SAli Ahmed 
164bc1d29deSKrzysztof Grobelny     if (!success)
16503fbed92SAli Ahmed     {
166ac106bf6SEd Tanous         messages::internalError(asyncResp->res);
16703fbed92SAli Ahmed         return;
16803fbed92SAli Ahmed     }
16903fbed92SAli Ahmed 
170bc1d29deSKrzysztof Grobelny     if (coreCount != nullptr)
17103fbed92SAli Ahmed     {
172bc1d29deSKrzysztof Grobelny         nlohmann::json& coreCountJson =
173ac106bf6SEd Tanous             asyncResp->res.jsonValue["ProcessorSummary"]["CoreCount"];
174bc1d29deSKrzysztof Grobelny         uint64_t* coreCountJsonPtr = coreCountJson.get_ptr<uint64_t*>();
175bc1d29deSKrzysztof Grobelny 
176bc1d29deSKrzysztof Grobelny         if (coreCountJsonPtr == nullptr)
177bc1d29deSKrzysztof Grobelny         {
178bc1d29deSKrzysztof Grobelny             coreCountJson = *coreCount;
17903fbed92SAli Ahmed         }
18003fbed92SAli Ahmed         else
18103fbed92SAli Ahmed         {
182bc1d29deSKrzysztof Grobelny             *coreCountJsonPtr += *coreCount;
18303fbed92SAli Ahmed         }
18403fbed92SAli Ahmed     }
18503fbed92SAli Ahmed }
18603fbed92SAli Ahmed 
18703fbed92SAli Ahmed /*
18803fbed92SAli Ahmed  * @brief Get ProcessorSummary fields
18903fbed92SAli Ahmed  *
190ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
19103fbed92SAli Ahmed  * @param[in] service dbus service for Cpu Information
19203fbed92SAli Ahmed  * @param[in] path dbus path for Cpu
19303fbed92SAli Ahmed  *
19403fbed92SAli Ahmed  * @return None.
19503fbed92SAli Ahmed  */
196504af5a0SPatrick Williams inline void getProcessorSummary(
197504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
198ac106bf6SEd Tanous     const std::string& service, const std::string& path)
19903fbed92SAli Ahmed {
200ac106bf6SEd Tanous     auto getCpuPresenceState = [asyncResp](const boost::system::error_code& ec3,
201382d6475SAli Ahmed                                            const bool cpuPresenceCheck) {
202382d6475SAli Ahmed         if (ec3)
203382d6475SAli Ahmed         {
20462598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error {}", ec3);
205382d6475SAli Ahmed             return;
206382d6475SAli Ahmed         }
207ac106bf6SEd Tanous         modifyCpuPresenceState(asyncResp, cpuPresenceCheck);
208382d6475SAli Ahmed     };
209382d6475SAli Ahmed 
210cf0e004cSNinad Palsule     // Get the Presence of CPU
211deae6a78SEd Tanous     dbus::utility::getProperty<bool>(*crow::connections::systemBus, service,
212deae6a78SEd Tanous                                      path, "xyz.openbmc_project.Inventory.Item",
213deae6a78SEd Tanous                                      "Present", std::move(getCpuPresenceState));
214cf0e004cSNinad Palsule 
215deae6a78SEd Tanous     dbus::utility::getAllProperties(
216deae6a78SEd Tanous         service, path, "xyz.openbmc_project.Inventory.Item.Cpu",
217ac106bf6SEd Tanous         [asyncResp, service,
2185e7e2dc5SEd Tanous          path](const boost::system::error_code& ec2,
219b9d36b47SEd Tanous                const dbus::utility::DBusPropertiesMap& properties) {
22003fbed92SAli Ahmed             if (ec2)
22103fbed92SAli Ahmed             {
22262598e31SEd Tanous                 BMCWEB_LOG_ERROR("DBUS response error {}", ec2);
223ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
22403fbed92SAli Ahmed                 return;
22503fbed92SAli Ahmed             }
226ac106bf6SEd Tanous             getProcessorProperties(asyncResp, properties);
227bc1d29deSKrzysztof Grobelny         });
22803fbed92SAli Ahmed }
22903fbed92SAli Ahmed 
23057e8c9beSAlpana Kumari /*
231cf0e004cSNinad Palsule  * @brief processMemoryProperties fields
232cf0e004cSNinad Palsule  *
233ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
234cf0e004cSNinad Palsule  * @param[in] DBUS properties for memory
235cf0e004cSNinad Palsule  *
236cf0e004cSNinad Palsule  * @return None.
237cf0e004cSNinad Palsule  */
238504af5a0SPatrick Williams inline void processMemoryProperties(
239504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
240cf0e004cSNinad Palsule     const dbus::utility::DBusPropertiesMap& properties)
241cf0e004cSNinad Palsule {
24262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Got {} Dimm properties.", properties.size());
243cf0e004cSNinad Palsule 
244cf0e004cSNinad Palsule     if (properties.empty())
245cf0e004cSNinad Palsule     {
246cf0e004cSNinad Palsule         return;
247cf0e004cSNinad Palsule     }
248cf0e004cSNinad Palsule 
249cf0e004cSNinad Palsule     const size_t* memorySizeInKB = nullptr;
250cf0e004cSNinad Palsule 
251cf0e004cSNinad Palsule     const bool success = sdbusplus::unpackPropertiesNoThrow(
252cf0e004cSNinad Palsule         dbus_utils::UnpackErrorPrinter(), properties, "MemorySizeInKB",
253cf0e004cSNinad Palsule         memorySizeInKB);
254cf0e004cSNinad Palsule 
255cf0e004cSNinad Palsule     if (!success)
256cf0e004cSNinad Palsule     {
257ac106bf6SEd Tanous         messages::internalError(asyncResp->res);
258cf0e004cSNinad Palsule         return;
259cf0e004cSNinad Palsule     }
260cf0e004cSNinad Palsule 
261cf0e004cSNinad Palsule     if (memorySizeInKB != nullptr)
262cf0e004cSNinad Palsule     {
263cf0e004cSNinad Palsule         nlohmann::json& totalMemory =
264ac106bf6SEd Tanous             asyncResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"];
265dfb2b408SPriyanga Ramasamy         const double* preValue = totalMemory.get_ptr<const double*>();
266cf0e004cSNinad Palsule         if (preValue == nullptr)
267cf0e004cSNinad Palsule         {
268ac106bf6SEd Tanous             asyncResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] =
269dfb2b408SPriyanga Ramasamy                 static_cast<double>(*memorySizeInKB) / (1024 * 1024);
270cf0e004cSNinad Palsule         }
271cf0e004cSNinad Palsule         else
272cf0e004cSNinad Palsule         {
273ac106bf6SEd Tanous             asyncResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] =
274dfb2b408SPriyanga Ramasamy                 static_cast<double>(*memorySizeInKB) / (1024 * 1024) +
275dfb2b408SPriyanga Ramasamy                 *preValue;
276cf0e004cSNinad Palsule         }
277cf0e004cSNinad Palsule     }
278cf0e004cSNinad Palsule }
279cf0e004cSNinad Palsule 
280cf0e004cSNinad Palsule /*
281cf0e004cSNinad Palsule  * @brief Get getMemorySummary fields
282cf0e004cSNinad Palsule  *
283ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
284cf0e004cSNinad Palsule  * @param[in] service dbus service for memory Information
285cf0e004cSNinad Palsule  * @param[in] path dbus path for memory
286cf0e004cSNinad Palsule  *
287cf0e004cSNinad Palsule  * @return None.
288cf0e004cSNinad Palsule  */
289504af5a0SPatrick Williams inline void getMemorySummary(
290504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
291ac106bf6SEd Tanous     const std::string& service, const std::string& path)
292cf0e004cSNinad Palsule {
293deae6a78SEd Tanous     dbus::utility::getAllProperties(
294deae6a78SEd Tanous         service, path, "xyz.openbmc_project.Inventory.Item.Dimm",
295ac106bf6SEd Tanous         [asyncResp, service,
296cf0e004cSNinad Palsule          path](const boost::system::error_code& ec2,
297cf0e004cSNinad Palsule                const dbus::utility::DBusPropertiesMap& properties) {
298cf0e004cSNinad Palsule             if (ec2)
299cf0e004cSNinad Palsule             {
30062598e31SEd Tanous                 BMCWEB_LOG_ERROR("DBUS response error {}", ec2);
301ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
302cf0e004cSNinad Palsule                 return;
303cf0e004cSNinad Palsule             }
30451bd2d8aSGunnar Mills             processMemoryProperties(asyncResp, properties);
305cf0e004cSNinad Palsule         });
306cf0e004cSNinad Palsule }
307cf0e004cSNinad Palsule 
308a974c132SLakshmi Yadlapati inline void afterGetUUID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
309a974c132SLakshmi Yadlapati                          const boost::system::error_code& ec,
310a974c132SLakshmi Yadlapati                          const dbus::utility::DBusPropertiesMap& properties)
3111abe55efSEd Tanous {
312a974c132SLakshmi Yadlapati     if (ec)
313a974c132SLakshmi Yadlapati     {
314a974c132SLakshmi Yadlapati         BMCWEB_LOG_ERROR("DBUS response error {}", ec);
315a974c132SLakshmi Yadlapati         messages::internalError(asyncResp->res);
316a974c132SLakshmi Yadlapati         return;
317a974c132SLakshmi Yadlapati     }
318a974c132SLakshmi Yadlapati     BMCWEB_LOG_DEBUG("Got {} UUID properties.", properties.size());
319a974c132SLakshmi Yadlapati 
320a974c132SLakshmi Yadlapati     const std::string* uUID = nullptr;
321a974c132SLakshmi Yadlapati 
322a974c132SLakshmi Yadlapati     const bool success = sdbusplus::unpackPropertiesNoThrow(
323a974c132SLakshmi Yadlapati         dbus_utils::UnpackErrorPrinter(), properties, "UUID", uUID);
324a974c132SLakshmi Yadlapati 
325a974c132SLakshmi Yadlapati     if (!success)
326a974c132SLakshmi Yadlapati     {
327a974c132SLakshmi Yadlapati         messages::internalError(asyncResp->res);
328a974c132SLakshmi Yadlapati         return;
329a974c132SLakshmi Yadlapati     }
330a974c132SLakshmi Yadlapati 
331a974c132SLakshmi Yadlapati     if (uUID != nullptr)
332a974c132SLakshmi Yadlapati     {
333a974c132SLakshmi Yadlapati         std::string valueStr = *uUID;
334a974c132SLakshmi Yadlapati         if (valueStr.size() == 32)
335a974c132SLakshmi Yadlapati         {
336a974c132SLakshmi Yadlapati             valueStr.insert(8, 1, '-');
337a974c132SLakshmi Yadlapati             valueStr.insert(13, 1, '-');
338a974c132SLakshmi Yadlapati             valueStr.insert(18, 1, '-');
339a974c132SLakshmi Yadlapati             valueStr.insert(23, 1, '-');
340a974c132SLakshmi Yadlapati         }
341a974c132SLakshmi Yadlapati         BMCWEB_LOG_DEBUG("UUID = {}", valueStr);
342a974c132SLakshmi Yadlapati         asyncResp->res.jsonValue["UUID"] = valueStr;
343a974c132SLakshmi Yadlapati     }
344a974c132SLakshmi Yadlapati }
345a974c132SLakshmi Yadlapati 
346504af5a0SPatrick Williams inline void afterGetInventory(
347504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
348a974c132SLakshmi Yadlapati     const boost::system::error_code& ec,
349a974c132SLakshmi Yadlapati     const dbus::utility::DBusPropertiesMap& propertiesList)
350a974c132SLakshmi Yadlapati {
351a974c132SLakshmi Yadlapati     if (ec)
352a974c132SLakshmi Yadlapati     {
353a974c132SLakshmi Yadlapati         // doesn't have to include this
354a974c132SLakshmi Yadlapati         // interface
355a974c132SLakshmi Yadlapati         return;
356a974c132SLakshmi Yadlapati     }
357a974c132SLakshmi Yadlapati     BMCWEB_LOG_DEBUG("Got {} properties for system", propertiesList.size());
358a974c132SLakshmi Yadlapati 
359a974c132SLakshmi Yadlapati     const std::string* partNumber = nullptr;
360a974c132SLakshmi Yadlapati     const std::string* serialNumber = nullptr;
361a974c132SLakshmi Yadlapati     const std::string* manufacturer = nullptr;
362a974c132SLakshmi Yadlapati     const std::string* model = nullptr;
363a974c132SLakshmi Yadlapati     const std::string* subModel = nullptr;
364a974c132SLakshmi Yadlapati 
365a974c132SLakshmi Yadlapati     const bool success = sdbusplus::unpackPropertiesNoThrow(
366a974c132SLakshmi Yadlapati         dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
367a974c132SLakshmi Yadlapati         partNumber, "SerialNumber", serialNumber, "Manufacturer", manufacturer,
368a974c132SLakshmi Yadlapati         "Model", model, "SubModel", subModel);
369a974c132SLakshmi Yadlapati 
370a974c132SLakshmi Yadlapati     if (!success)
371a974c132SLakshmi Yadlapati     {
372a974c132SLakshmi Yadlapati         messages::internalError(asyncResp->res);
373a974c132SLakshmi Yadlapati         return;
374a974c132SLakshmi Yadlapati     }
375a974c132SLakshmi Yadlapati 
376a974c132SLakshmi Yadlapati     if (partNumber != nullptr)
377a974c132SLakshmi Yadlapati     {
378a974c132SLakshmi Yadlapati         asyncResp->res.jsonValue["PartNumber"] = *partNumber;
379a974c132SLakshmi Yadlapati     }
380a974c132SLakshmi Yadlapati 
381a974c132SLakshmi Yadlapati     if (serialNumber != nullptr)
382a974c132SLakshmi Yadlapati     {
383a974c132SLakshmi Yadlapati         asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
384a974c132SLakshmi Yadlapati     }
385a974c132SLakshmi Yadlapati 
386a974c132SLakshmi Yadlapati     if (manufacturer != nullptr)
387a974c132SLakshmi Yadlapati     {
388a974c132SLakshmi Yadlapati         asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
389a974c132SLakshmi Yadlapati     }
390a974c132SLakshmi Yadlapati 
391a974c132SLakshmi Yadlapati     if (model != nullptr)
392a974c132SLakshmi Yadlapati     {
393a974c132SLakshmi Yadlapati         asyncResp->res.jsonValue["Model"] = *model;
394a974c132SLakshmi Yadlapati     }
395a974c132SLakshmi Yadlapati 
396a974c132SLakshmi Yadlapati     if (subModel != nullptr)
397a974c132SLakshmi Yadlapati     {
398a974c132SLakshmi Yadlapati         asyncResp->res.jsonValue["SubModel"] = *subModel;
399a974c132SLakshmi Yadlapati     }
400a974c132SLakshmi Yadlapati 
401a974c132SLakshmi Yadlapati     // Grab the bios version
402a974c132SLakshmi Yadlapati     sw_util::populateSoftwareInformation(asyncResp, sw_util::biosPurpose,
403a974c132SLakshmi Yadlapati                                          "BiosVersion", false);
404a974c132SLakshmi Yadlapati }
405a974c132SLakshmi Yadlapati 
406bd79bce8SPatrick Williams inline void afterGetAssetTag(
407bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
408bd79bce8SPatrick Williams     const boost::system::error_code& ec, const std::string& value)
409a974c132SLakshmi Yadlapati {
410a974c132SLakshmi Yadlapati     if (ec)
411a974c132SLakshmi Yadlapati     {
412a974c132SLakshmi Yadlapati         // doesn't have to include this
413a974c132SLakshmi Yadlapati         // interface
414a974c132SLakshmi Yadlapati         return;
415a974c132SLakshmi Yadlapati     }
416a974c132SLakshmi Yadlapati 
417a974c132SLakshmi Yadlapati     asyncResp->res.jsonValue["AssetTag"] = value;
418a974c132SLakshmi Yadlapati }
419a974c132SLakshmi Yadlapati 
420a974c132SLakshmi Yadlapati inline void afterSystemGetSubTree(
421a974c132SLakshmi Yadlapati     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
422a974c132SLakshmi Yadlapati     const boost::system::error_code& ec,
423a974c132SLakshmi Yadlapati     const dbus::utility::MapperGetSubTreeResponse& subtree)
424a974c132SLakshmi Yadlapati {
4251abe55efSEd Tanous     if (ec)
4261abe55efSEd Tanous     {
427b3e86cb0SGunnar Mills         BMCWEB_LOG_ERROR("DBUS response error {}", ec);
428ac106bf6SEd Tanous         messages::internalError(asyncResp->res);
429c5b2abe0SLewanczyk, Dawid         return;
430c5b2abe0SLewanczyk, Dawid     }
431c5b2abe0SLewanczyk, Dawid     // Iterate over all retrieved ObjectPaths.
432002d39b4SEd Tanous     for (const std::pair<
433002d39b4SEd Tanous              std::string,
434002d39b4SEd Tanous              std::vector<std::pair<std::string, std::vector<std::string>>>>&
4351214b7e7SGunnar Mills              object : subtree)
4361abe55efSEd Tanous     {
437c5b2abe0SLewanczyk, Dawid         const std::string& path = object.first;
43862598e31SEd Tanous         BMCWEB_LOG_DEBUG("Got path: {}", path);
439002d39b4SEd Tanous         const std::vector<std::pair<std::string, std::vector<std::string>>>&
4401214b7e7SGunnar Mills             connectionNames = object.second;
44126f6976fSEd Tanous         if (connectionNames.empty())
4421abe55efSEd Tanous         {
443c5b2abe0SLewanczyk, Dawid             continue;
444c5b2abe0SLewanczyk, Dawid         }
445029573d4SEd Tanous 
4466c34de48SEd Tanous         // This is not system, so check if it's cpu, dimm, UUID or
4476c34de48SEd Tanous         // BiosVer
44804a258f4SEd Tanous         for (const auto& connection : connectionNames)
4491abe55efSEd Tanous         {
45004a258f4SEd Tanous             for (const auto& interfaceName : connection.second)
4511abe55efSEd Tanous             {
452a974c132SLakshmi Yadlapati                 if (interfaceName == "xyz.openbmc_project.Inventory.Item.Dimm")
4531abe55efSEd Tanous                 {
45462598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Found Dimm, now get its properties.");
4559d3ae10eSAlpana Kumari 
456ac106bf6SEd Tanous                     getMemorySummary(asyncResp, connection.first, path);
4575fd0aafbSNinad Palsule                 }
45804a258f4SEd Tanous                 else if (interfaceName ==
45904a258f4SEd Tanous                          "xyz.openbmc_project.Inventory.Item.Cpu")
4601abe55efSEd Tanous                 {
46162598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Found Cpu, now get its properties.");
46257e8c9beSAlpana Kumari 
463ac106bf6SEd Tanous                     getProcessorSummary(asyncResp, connection.first, path);
4645fd0aafbSNinad Palsule                 }
465002d39b4SEd Tanous                 else if (interfaceName == "xyz.openbmc_project.Common.UUID")
4661abe55efSEd Tanous                 {
46762598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Found UUID, now get its properties.");
468bc1d29deSKrzysztof Grobelny 
469deae6a78SEd Tanous                     dbus::utility::getAllProperties(
470a974c132SLakshmi Yadlapati                         *crow::connections::systemBus, connection.first, path,
471a974c132SLakshmi Yadlapati                         "xyz.openbmc_project.Common.UUID",
472ac106bf6SEd Tanous                         [asyncResp](const boost::system::error_code& ec3,
473b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
4741214b7e7SGunnar Mills                                         properties) {
475a974c132SLakshmi Yadlapati                             afterGetUUID(asyncResp, ec3, properties);
476bc1d29deSKrzysztof Grobelny                         });
477c5b2abe0SLewanczyk, Dawid                 }
478029573d4SEd Tanous                 else if (interfaceName ==
479029573d4SEd Tanous                          "xyz.openbmc_project.Inventory.Item.System")
4801abe55efSEd Tanous                 {
481deae6a78SEd Tanous                     dbus::utility::getAllProperties(
482a974c132SLakshmi Yadlapati                         *crow::connections::systemBus, connection.first, path,
483bc1d29deSKrzysztof Grobelny                         "xyz.openbmc_project.Inventory.Decorator.Asset",
484a974c132SLakshmi Yadlapati                         [asyncResp](const boost::system::error_code& ec3,
485b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
486a974c132SLakshmi Yadlapati                                         properties) {
487a974c132SLakshmi Yadlapati                             afterGetInventory(asyncResp, ec3, properties);
488bc1d29deSKrzysztof Grobelny                         });
489e4a4b9a9SJames Feist 
490deae6a78SEd Tanous                     dbus::utility::getProperty<std::string>(
491deae6a78SEd Tanous                         connection.first, path,
4921e1e598dSJonathan Doman                         "xyz.openbmc_project.Inventory.Decorator."
4931e1e598dSJonathan Doman                         "AssetTag",
4941e1e598dSJonathan Doman                         "AssetTag",
495a974c132SLakshmi Yadlapati                         std::bind_front(afterGetAssetTag, asyncResp));
496a974c132SLakshmi Yadlapati                 }
497a974c132SLakshmi Yadlapati             }
498a974c132SLakshmi Yadlapati         }
499a974c132SLakshmi Yadlapati     }
500a974c132SLakshmi Yadlapati }
501a974c132SLakshmi Yadlapati 
502a974c132SLakshmi Yadlapati /*
503a974c132SLakshmi Yadlapati  * @brief Retrieves computer system properties over dbus
504a974c132SLakshmi Yadlapati  *
505a974c132SLakshmi Yadlapati  * @param[in] asyncResp Shared pointer for completing asynchronous calls
506a974c132SLakshmi Yadlapati  *
507a974c132SLakshmi Yadlapati  * @return None.
508a974c132SLakshmi Yadlapati  */
509504af5a0SPatrick Williams inline void getComputerSystem(
510504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
511e4a4b9a9SJames Feist {
512a974c132SLakshmi Yadlapati     BMCWEB_LOG_DEBUG("Get available system components.");
513a974c132SLakshmi Yadlapati     constexpr std::array<std::string_view, 5> interfaces = {
514a974c132SLakshmi Yadlapati         "xyz.openbmc_project.Inventory.Decorator.Asset",
515a974c132SLakshmi Yadlapati         "xyz.openbmc_project.Inventory.Item.Cpu",
516a974c132SLakshmi Yadlapati         "xyz.openbmc_project.Inventory.Item.Dimm",
517a974c132SLakshmi Yadlapati         "xyz.openbmc_project.Inventory.Item.System",
518a974c132SLakshmi Yadlapati         "xyz.openbmc_project.Common.UUID",
519a974c132SLakshmi Yadlapati     };
520a974c132SLakshmi Yadlapati     dbus::utility::getSubTree(
521a974c132SLakshmi Yadlapati         "/xyz/openbmc_project/inventory", 0, interfaces,
52251bd2d8aSGunnar Mills         std::bind_front(afterSystemGetSubTree, asyncResp));
523c5b2abe0SLewanczyk, Dawid }
524c5b2abe0SLewanczyk, Dawid 
525c5b2abe0SLewanczyk, Dawid /**
526c5b2abe0SLewanczyk, Dawid  * @brief Retrieves host state properties over dbus
527c5b2abe0SLewanczyk, Dawid  *
528ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
529c5b2abe0SLewanczyk, Dawid  *
530c5b2abe0SLewanczyk, Dawid  * @return None.
531c5b2abe0SLewanczyk, Dawid  */
532ac106bf6SEd Tanous inline void getHostState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
5331abe55efSEd Tanous {
53462598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get host information.");
535deae6a78SEd Tanous     dbus::utility::getProperty<std::string>(
536deae6a78SEd Tanous         "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
537deae6a78SEd Tanous         "xyz.openbmc_project.State.Host", "CurrentHostState",
538ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
5391e1e598dSJonathan Doman                     const std::string& hostState) {
5401abe55efSEd Tanous             if (ec)
5411abe55efSEd Tanous             {
54222228c28SAndrew Geissler                 if (ec == boost::system::errc::host_unreachable)
54322228c28SAndrew Geissler                 {
54422228c28SAndrew Geissler                     // Service not available, no error, just don't return
54522228c28SAndrew Geissler                     // host state info
54662598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Service not available {}", ec);
54722228c28SAndrew Geissler                     return;
54822228c28SAndrew Geissler                 }
54962598e31SEd Tanous                 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
550ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
551c5b2abe0SLewanczyk, Dawid                 return;
552c5b2abe0SLewanczyk, Dawid             }
5536617338dSEd Tanous 
55462598e31SEd Tanous             BMCWEB_LOG_DEBUG("Host state: {}", hostState);
555c5b2abe0SLewanczyk, Dawid             // Verify Host State
5561e1e598dSJonathan Doman             if (hostState == "xyz.openbmc_project.State.Host.HostState.Running")
5571abe55efSEd Tanous             {
558bd79bce8SPatrick Williams                 asyncResp->res.jsonValue["PowerState"] =
559bd79bce8SPatrick Williams                     resource::PowerState::On;
560539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
561539d8c6bSEd Tanous                     resource::State::Enabled;
5621abe55efSEd Tanous             }
5631e1e598dSJonathan Doman             else if (hostState ==
5640fda0f12SGeorge Liu                      "xyz.openbmc_project.State.Host.HostState.Quiesced")
5658c888608SGunnar Mills             {
566bd79bce8SPatrick Williams                 asyncResp->res.jsonValue["PowerState"] =
567bd79bce8SPatrick Williams                     resource::PowerState::On;
568539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
569539d8c6bSEd Tanous                     resource::State::Quiesced;
5708c888608SGunnar Mills             }
5711e1e598dSJonathan Doman             else if (hostState ==
5720fda0f12SGeorge Liu                      "xyz.openbmc_project.State.Host.HostState.DiagnosticMode")
57383935af9SAndrew Geissler             {
574bd79bce8SPatrick Williams                 asyncResp->res.jsonValue["PowerState"] =
575bd79bce8SPatrick Williams                     resource::PowerState::On;
576539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
577539d8c6bSEd Tanous                     resource::State::InTest;
57883935af9SAndrew Geissler             }
5790fda0f12SGeorge Liu             else if (
5801e1e598dSJonathan Doman                 hostState ==
5810fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Host.HostState.TransitioningToRunning")
5821a2a1437SAndrew Geissler             {
583539d8c6bSEd Tanous                 asyncResp->res.jsonValue["PowerState"] =
584539d8c6bSEd Tanous                     resource::PowerState::PoweringOn;
585539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
586539d8c6bSEd Tanous                     resource::State::Starting;
5871a2a1437SAndrew Geissler             }
588bd79bce8SPatrick Williams             else if (
589bd79bce8SPatrick Williams                 hostState ==
5900fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Host.HostState.TransitioningToOff")
5911a2a1437SAndrew Geissler             {
592539d8c6bSEd Tanous                 asyncResp->res.jsonValue["PowerState"] =
593539d8c6bSEd Tanous                     resource::PowerState::PoweringOff;
594539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
595539d8c6bSEd Tanous                     resource::State::Disabled;
5961a2a1437SAndrew Geissler             }
5971abe55efSEd Tanous             else
5981abe55efSEd Tanous             {
599bd79bce8SPatrick Williams                 asyncResp->res.jsonValue["PowerState"] =
600bd79bce8SPatrick Williams                     resource::PowerState::Off;
601539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
602539d8c6bSEd Tanous                     resource::State::Disabled;
603c5b2abe0SLewanczyk, Dawid             }
6041e1e598dSJonathan Doman         });
605c5b2abe0SLewanczyk, Dawid }
606c5b2abe0SLewanczyk, Dawid 
607c5b2abe0SLewanczyk, Dawid /**
608786d0f60SGunnar Mills  * @brief Translates boot source DBUS property value to redfish.
609491d8ee7SSantosh Puranik  *
610491d8ee7SSantosh Puranik  * @param[in] dbusSource    The boot source in DBUS speak.
611491d8ee7SSantosh Puranik  *
612491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source in Redfish terms. If translation
613491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
614491d8ee7SSantosh Puranik  */
61523a21a1cSEd Tanous inline std::string dbusToRfBootSource(const std::string& dbusSource)
616491d8ee7SSantosh Puranik {
617491d8ee7SSantosh Puranik     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
618491d8ee7SSantosh Puranik     {
619491d8ee7SSantosh Puranik         return "None";
620491d8ee7SSantosh Puranik     }
6213174e4dfSEd Tanous     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Disk")
622491d8ee7SSantosh Puranik     {
623491d8ee7SSantosh Puranik         return "Hdd";
624491d8ee7SSantosh Puranik     }
6253174e4dfSEd Tanous     if (dbusSource ==
626a71dc0b7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia")
627491d8ee7SSantosh Puranik     {
628491d8ee7SSantosh Puranik         return "Cd";
629491d8ee7SSantosh Puranik     }
6303174e4dfSEd Tanous     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Network")
631491d8ee7SSantosh Puranik     {
632491d8ee7SSantosh Puranik         return "Pxe";
633491d8ee7SSantosh Puranik     }
6343174e4dfSEd Tanous     if (dbusSource ==
635944ffaf9SJohnathan Mantey         "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia")
6369f16b2c1SJennifer Lee     {
6379f16b2c1SJennifer Lee         return "Usb";
6389f16b2c1SJennifer Lee     }
639491d8ee7SSantosh Puranik     return "";
640491d8ee7SSantosh Puranik }
641491d8ee7SSantosh Puranik 
642491d8ee7SSantosh Puranik /**
643cd9a4666SKonstantin Aladyshev  * @brief Translates boot type DBUS property value to redfish.
644cd9a4666SKonstantin Aladyshev  *
645cd9a4666SKonstantin Aladyshev  * @param[in] dbusType    The boot type in DBUS speak.
646cd9a4666SKonstantin Aladyshev  *
647cd9a4666SKonstantin Aladyshev  * @return Returns as a string, the boot type in Redfish terms. If translation
648cd9a4666SKonstantin Aladyshev  * cannot be done, returns an empty string.
649cd9a4666SKonstantin Aladyshev  */
650cd9a4666SKonstantin Aladyshev inline std::string dbusToRfBootType(const std::string& dbusType)
651cd9a4666SKonstantin Aladyshev {
652cd9a4666SKonstantin Aladyshev     if (dbusType == "xyz.openbmc_project.Control.Boot.Type.Types.Legacy")
653cd9a4666SKonstantin Aladyshev     {
654cd9a4666SKonstantin Aladyshev         return "Legacy";
655cd9a4666SKonstantin Aladyshev     }
656cd9a4666SKonstantin Aladyshev     if (dbusType == "xyz.openbmc_project.Control.Boot.Type.Types.EFI")
657cd9a4666SKonstantin Aladyshev     {
658cd9a4666SKonstantin Aladyshev         return "UEFI";
659cd9a4666SKonstantin Aladyshev     }
660cd9a4666SKonstantin Aladyshev     return "";
661cd9a4666SKonstantin Aladyshev }
662cd9a4666SKonstantin Aladyshev 
663cd9a4666SKonstantin Aladyshev /**
664786d0f60SGunnar Mills  * @brief Translates boot mode DBUS property value to redfish.
665491d8ee7SSantosh Puranik  *
666491d8ee7SSantosh Puranik  * @param[in] dbusMode    The boot mode in DBUS speak.
667491d8ee7SSantosh Puranik  *
668491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode in Redfish terms. If translation
669491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
670491d8ee7SSantosh Puranik  */
67123a21a1cSEd Tanous inline std::string dbusToRfBootMode(const std::string& dbusMode)
672491d8ee7SSantosh Puranik {
673491d8ee7SSantosh Puranik     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
674491d8ee7SSantosh Puranik     {
675491d8ee7SSantosh Puranik         return "None";
676491d8ee7SSantosh Puranik     }
6773174e4dfSEd Tanous     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe")
678491d8ee7SSantosh Puranik     {
679491d8ee7SSantosh Puranik         return "Diags";
680491d8ee7SSantosh Puranik     }
6813174e4dfSEd Tanous     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup")
682491d8ee7SSantosh Puranik     {
683491d8ee7SSantosh Puranik         return "BiosSetup";
684491d8ee7SSantosh Puranik     }
685491d8ee7SSantosh Puranik     return "";
686491d8ee7SSantosh Puranik }
687491d8ee7SSantosh Puranik 
688491d8ee7SSantosh Puranik /**
689e43914b3SAndrew Geissler  * @brief Translates boot progress DBUS property value to redfish.
690e43914b3SAndrew Geissler  *
691e43914b3SAndrew Geissler  * @param[in] dbusBootProgress    The boot progress in DBUS speak.
692e43914b3SAndrew Geissler  *
693e43914b3SAndrew Geissler  * @return Returns as a string, the boot progress in Redfish terms. If
694e43914b3SAndrew Geissler  *         translation cannot be done, returns "None".
695e43914b3SAndrew Geissler  */
696e43914b3SAndrew Geissler inline std::string dbusToRfBootProgress(const std::string& dbusBootProgress)
697e43914b3SAndrew Geissler {
698e43914b3SAndrew Geissler     // Now convert the D-Bus BootProgress to the appropriate Redfish
699e43914b3SAndrew Geissler     // enum
700e43914b3SAndrew Geissler     std::string rfBpLastState = "None";
701e43914b3SAndrew Geissler     if (dbusBootProgress == "xyz.openbmc_project.State.Boot.Progress."
702e43914b3SAndrew Geissler                             "ProgressStages.Unspecified")
703e43914b3SAndrew Geissler     {
704e43914b3SAndrew Geissler         rfBpLastState = "None";
705e43914b3SAndrew Geissler     }
706e43914b3SAndrew Geissler     else if (dbusBootProgress ==
707e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
708e43914b3SAndrew Geissler              "PrimaryProcInit")
709e43914b3SAndrew Geissler     {
710e43914b3SAndrew Geissler         rfBpLastState = "PrimaryProcessorInitializationStarted";
711e43914b3SAndrew Geissler     }
712e43914b3SAndrew Geissler     else if (dbusBootProgress ==
713e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
714e43914b3SAndrew Geissler              "BusInit")
715e43914b3SAndrew Geissler     {
716e43914b3SAndrew Geissler         rfBpLastState = "BusInitializationStarted";
717e43914b3SAndrew Geissler     }
718e43914b3SAndrew Geissler     else if (dbusBootProgress ==
719e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
720e43914b3SAndrew Geissler              "MemoryInit")
721e43914b3SAndrew Geissler     {
722e43914b3SAndrew Geissler         rfBpLastState = "MemoryInitializationStarted";
723e43914b3SAndrew Geissler     }
724e43914b3SAndrew Geissler     else if (dbusBootProgress ==
725e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
726e43914b3SAndrew Geissler              "SecondaryProcInit")
727e43914b3SAndrew Geissler     {
728e43914b3SAndrew Geissler         rfBpLastState = "SecondaryProcessorInitializationStarted";
729e43914b3SAndrew Geissler     }
730e43914b3SAndrew Geissler     else if (dbusBootProgress ==
731e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
732e43914b3SAndrew Geissler              "PCIInit")
733e43914b3SAndrew Geissler     {
734e43914b3SAndrew Geissler         rfBpLastState = "PCIResourceConfigStarted";
735e43914b3SAndrew Geissler     }
736e43914b3SAndrew Geissler     else if (dbusBootProgress ==
737e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
738e43914b3SAndrew Geissler              "SystemSetup")
739e43914b3SAndrew Geissler     {
740e43914b3SAndrew Geissler         rfBpLastState = "SetupEntered";
741e43914b3SAndrew Geissler     }
742e43914b3SAndrew Geissler     else if (dbusBootProgress ==
743e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
744e43914b3SAndrew Geissler              "SystemInitComplete")
745e43914b3SAndrew Geissler     {
746e43914b3SAndrew Geissler         rfBpLastState = "SystemHardwareInitializationComplete";
747e43914b3SAndrew Geissler     }
748e43914b3SAndrew Geissler     else if (dbusBootProgress ==
749e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
750e43914b3SAndrew Geissler              "OSStart")
751e43914b3SAndrew Geissler     {
752e43914b3SAndrew Geissler         rfBpLastState = "OSBootStarted";
753e43914b3SAndrew Geissler     }
754e43914b3SAndrew Geissler     else if (dbusBootProgress ==
755e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
756e43914b3SAndrew Geissler              "OSRunning")
757e43914b3SAndrew Geissler     {
758e43914b3SAndrew Geissler         rfBpLastState = "OSRunning";
759e43914b3SAndrew Geissler     }
760e43914b3SAndrew Geissler     else
761e43914b3SAndrew Geissler     {
76262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Unsupported D-Bus BootProgress {}", dbusBootProgress);
763e43914b3SAndrew Geissler         // Just return the default
764e43914b3SAndrew Geissler     }
765e43914b3SAndrew Geissler     return rfBpLastState;
766e43914b3SAndrew Geissler }
767e43914b3SAndrew Geissler 
768e43914b3SAndrew Geissler /**
769786d0f60SGunnar Mills  * @brief Translates boot source from Redfish to the DBus boot paths.
770491d8ee7SSantosh Puranik  *
771491d8ee7SSantosh Puranik  * @param[in] rfSource    The boot source in Redfish.
772944ffaf9SJohnathan Mantey  * @param[out] bootSource The DBus source
773944ffaf9SJohnathan Mantey  * @param[out] bootMode   the DBus boot mode
774491d8ee7SSantosh Puranik  *
775944ffaf9SJohnathan Mantey  * @return Integer error code.
776491d8ee7SSantosh Puranik  */
777bd79bce8SPatrick Williams inline int assignBootParameters(
778bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
779bd79bce8SPatrick Williams     const std::string& rfSource, std::string& bootSource, std::string& bootMode)
780491d8ee7SSantosh Puranik {
781c21865c4SKonstantin Aladyshev     bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
782c21865c4SKonstantin Aladyshev     bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
783944ffaf9SJohnathan Mantey 
784491d8ee7SSantosh Puranik     if (rfSource == "None")
785491d8ee7SSantosh Puranik     {
786944ffaf9SJohnathan Mantey         return 0;
787491d8ee7SSantosh Puranik     }
7883174e4dfSEd Tanous     if (rfSource == "Pxe")
789491d8ee7SSantosh Puranik     {
790944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Network";
791944ffaf9SJohnathan Mantey     }
792944ffaf9SJohnathan Mantey     else if (rfSource == "Hdd")
793944ffaf9SJohnathan Mantey     {
794944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Disk";
795944ffaf9SJohnathan Mantey     }
796944ffaf9SJohnathan Mantey     else if (rfSource == "Diags")
797944ffaf9SJohnathan Mantey     {
798944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe";
799944ffaf9SJohnathan Mantey     }
800944ffaf9SJohnathan Mantey     else if (rfSource == "Cd")
801944ffaf9SJohnathan Mantey     {
802944ffaf9SJohnathan Mantey         bootSource =
803944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia";
804944ffaf9SJohnathan Mantey     }
805944ffaf9SJohnathan Mantey     else if (rfSource == "BiosSetup")
806944ffaf9SJohnathan Mantey     {
807944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup";
808491d8ee7SSantosh Puranik     }
8099f16b2c1SJennifer Lee     else if (rfSource == "Usb")
8109f16b2c1SJennifer Lee     {
811944ffaf9SJohnathan Mantey         bootSource =
812944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia";
8139f16b2c1SJennifer Lee     }
814491d8ee7SSantosh Puranik     else
815491d8ee7SSantosh Puranik     {
81662598e31SEd Tanous         BMCWEB_LOG_DEBUG(
81762598e31SEd Tanous             "Invalid property value for BootSourceOverrideTarget: {}",
81862598e31SEd Tanous             bootSource);
819ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, rfSource,
820944ffaf9SJohnathan Mantey                                          "BootSourceTargetOverride");
821944ffaf9SJohnathan Mantey         return -1;
822491d8ee7SSantosh Puranik     }
823944ffaf9SJohnathan Mantey     return 0;
824491d8ee7SSantosh Puranik }
8251981771bSAli Ahmed 
826978b8803SAndrew Geissler /**
827978b8803SAndrew Geissler  * @brief Retrieves boot progress of the system
828978b8803SAndrew Geissler  *
829ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
830978b8803SAndrew Geissler  *
831978b8803SAndrew Geissler  * @return None.
832978b8803SAndrew Geissler  */
833ac106bf6SEd Tanous inline void getBootProgress(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
834978b8803SAndrew Geissler {
835deae6a78SEd Tanous     dbus::utility::getProperty<std::string>(
836deae6a78SEd Tanous         "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
8371e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.Progress", "BootProgress",
838ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
8391e1e598dSJonathan Doman                     const std::string& bootProgressStr) {
840978b8803SAndrew Geissler             if (ec)
841978b8803SAndrew Geissler             {
842978b8803SAndrew Geissler                 // BootProgress is an optional object so just do nothing if
843978b8803SAndrew Geissler                 // not found
844978b8803SAndrew Geissler                 return;
845978b8803SAndrew Geissler             }
846978b8803SAndrew Geissler 
84762598e31SEd Tanous             BMCWEB_LOG_DEBUG("Boot Progress: {}", bootProgressStr);
848978b8803SAndrew Geissler 
849ac106bf6SEd Tanous             asyncResp->res.jsonValue["BootProgress"]["LastState"] =
850e43914b3SAndrew Geissler                 dbusToRfBootProgress(bootProgressStr);
8511e1e598dSJonathan Doman         });
852978b8803SAndrew Geissler }
853491d8ee7SSantosh Puranik 
854491d8ee7SSantosh Puranik /**
855b6d5d45cSHieu Huynh  * @brief Retrieves boot progress Last Update of the system
856b6d5d45cSHieu Huynh  *
857ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
858b6d5d45cSHieu Huynh  *
859b6d5d45cSHieu Huynh  * @return None.
860b6d5d45cSHieu Huynh  */
861b6d5d45cSHieu Huynh inline void getBootProgressLastStateTime(
862ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
863b6d5d45cSHieu Huynh {
864deae6a78SEd Tanous     dbus::utility::getProperty<uint64_t>(
865deae6a78SEd Tanous         "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
866b6d5d45cSHieu Huynh         "xyz.openbmc_project.State.Boot.Progress", "BootProgressLastUpdate",
867ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
868b6d5d45cSHieu Huynh                     const uint64_t lastStateTime) {
869b6d5d45cSHieu Huynh             if (ec)
870b6d5d45cSHieu Huynh             {
87162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
872b6d5d45cSHieu Huynh                 return;
873b6d5d45cSHieu Huynh             }
874b6d5d45cSHieu Huynh 
875b6d5d45cSHieu Huynh             // BootProgressLastUpdate is the last time the BootProgress property
876b6d5d45cSHieu Huynh             // was updated. The time is the Epoch time, number of microseconds
877b6d5d45cSHieu Huynh             // since 1 Jan 1970 00::00::00 UTC."
878b6d5d45cSHieu Huynh             // https://github.com/openbmc/phosphor-dbus-interfaces/blob/master/
879b6d5d45cSHieu Huynh             // yaml/xyz/openbmc_project/State/Boot/Progress.interface.yaml#L11
880b6d5d45cSHieu Huynh 
881b6d5d45cSHieu Huynh             // Convert to ISO 8601 standard
882ac106bf6SEd Tanous             asyncResp->res.jsonValue["BootProgress"]["LastStateTime"] =
883b6d5d45cSHieu Huynh                 redfish::time_utils::getDateTimeUintUs(lastStateTime);
884b6d5d45cSHieu Huynh         });
885b6d5d45cSHieu Huynh }
886b6d5d45cSHieu Huynh 
887b6d5d45cSHieu Huynh /**
888c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override type over DBUS and fills out the response
889cd9a4666SKonstantin Aladyshev  *
890ac106bf6SEd Tanous  * @param[in] asyncResp         Shared pointer for generating response message.
891cd9a4666SKonstantin Aladyshev  *
892cd9a4666SKonstantin Aladyshev  * @return None.
893cd9a4666SKonstantin Aladyshev  */
894cd9a4666SKonstantin Aladyshev 
895504af5a0SPatrick Williams inline void getBootOverrideType(
896504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
897cd9a4666SKonstantin Aladyshev {
898deae6a78SEd Tanous     dbus::utility::getProperty<std::string>(
899deae6a78SEd Tanous         "xyz.openbmc_project.Settings",
9001e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
9011e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Type", "BootType",
902ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
9031e1e598dSJonathan Doman                     const std::string& bootType) {
904cd9a4666SKonstantin Aladyshev             if (ec)
905cd9a4666SKonstantin Aladyshev             {
906cd9a4666SKonstantin Aladyshev                 // not an error, don't have to have the interface
907cd9a4666SKonstantin Aladyshev                 return;
908cd9a4666SKonstantin Aladyshev             }
909cd9a4666SKonstantin Aladyshev 
91062598e31SEd Tanous             BMCWEB_LOG_DEBUG("Boot type: {}", bootType);
911cd9a4666SKonstantin Aladyshev 
912ac106bf6SEd Tanous             asyncResp->res
913ac106bf6SEd Tanous                 .jsonValue["Boot"]
914002d39b4SEd Tanous                           ["BootSourceOverrideMode@Redfish.AllowableValues"] =
915613dabeaSEd Tanous                 nlohmann::json::array_t({"Legacy", "UEFI"});
916cd9a4666SKonstantin Aladyshev 
9171e1e598dSJonathan Doman             auto rfType = dbusToRfBootType(bootType);
918cd9a4666SKonstantin Aladyshev             if (rfType.empty())
919cd9a4666SKonstantin Aladyshev             {
920ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
921cd9a4666SKonstantin Aladyshev                 return;
922cd9a4666SKonstantin Aladyshev             }
923cd9a4666SKonstantin Aladyshev 
924ac106bf6SEd Tanous             asyncResp->res.jsonValue["Boot"]["BootSourceOverrideMode"] = rfType;
9251e1e598dSJonathan Doman         });
926cd9a4666SKonstantin Aladyshev }
927cd9a4666SKonstantin Aladyshev 
928cd9a4666SKonstantin Aladyshev /**
929c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override mode over DBUS and fills out the response
930491d8ee7SSantosh Puranik  *
931ac106bf6SEd Tanous  * @param[in] asyncResp         Shared pointer for generating response message.
932491d8ee7SSantosh Puranik  *
933491d8ee7SSantosh Puranik  * @return None.
934491d8ee7SSantosh Puranik  */
935c21865c4SKonstantin Aladyshev 
936504af5a0SPatrick Williams inline void getBootOverrideMode(
937504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
938491d8ee7SSantosh Puranik {
939deae6a78SEd Tanous     dbus::utility::getProperty<std::string>(
940deae6a78SEd Tanous         "xyz.openbmc_project.Settings",
9411e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
9421e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
943ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
9441e1e598dSJonathan Doman                     const std::string& bootModeStr) {
945491d8ee7SSantosh Puranik             if (ec)
946491d8ee7SSantosh Puranik             {
947b3e86cb0SGunnar Mills                 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
948ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
949491d8ee7SSantosh Puranik                 return;
950491d8ee7SSantosh Puranik             }
951491d8ee7SSantosh Puranik 
95262598e31SEd Tanous             BMCWEB_LOG_DEBUG("Boot mode: {}", bootModeStr);
953491d8ee7SSantosh Puranik 
95420fa6a2cSEd Tanous             nlohmann::json::array_t allowed;
95520fa6a2cSEd Tanous             allowed.emplace_back("None");
95620fa6a2cSEd Tanous             allowed.emplace_back("Pxe");
95720fa6a2cSEd Tanous             allowed.emplace_back("Hdd");
95820fa6a2cSEd Tanous             allowed.emplace_back("Cd");
95920fa6a2cSEd Tanous             allowed.emplace_back("Diags");
96020fa6a2cSEd Tanous             allowed.emplace_back("BiosSetup");
96120fa6a2cSEd Tanous             allowed.emplace_back("Usb");
96220fa6a2cSEd Tanous 
963ac106bf6SEd Tanous             asyncResp->res
9640fda0f12SGeorge Liu                 .jsonValue["Boot"]
96520fa6a2cSEd Tanous                           ["BootSourceOverrideTarget@Redfish.AllowableValues"] =
96620fa6a2cSEd Tanous                 std::move(allowed);
9671e1e598dSJonathan Doman             if (bootModeStr !=
968491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
969491d8ee7SSantosh Puranik             {
9701e1e598dSJonathan Doman                 auto rfMode = dbusToRfBootMode(bootModeStr);
971491d8ee7SSantosh Puranik                 if (!rfMode.empty())
972491d8ee7SSantosh Puranik                 {
973bd79bce8SPatrick Williams                     asyncResp->res
974bd79bce8SPatrick Williams                         .jsonValue["Boot"]["BootSourceOverrideTarget"] = rfMode;
975491d8ee7SSantosh Puranik                 }
976491d8ee7SSantosh Puranik             }
9771e1e598dSJonathan Doman         });
978491d8ee7SSantosh Puranik }
979491d8ee7SSantosh Puranik 
980491d8ee7SSantosh Puranik /**
981c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override source over DBUS
982491d8ee7SSantosh Puranik  *
983ac106bf6SEd Tanous  * @param[in] asyncResp         Shared pointer for generating response message.
984491d8ee7SSantosh Puranik  *
985491d8ee7SSantosh Puranik  * @return None.
986491d8ee7SSantosh Puranik  */
987c21865c4SKonstantin Aladyshev 
988504af5a0SPatrick Williams inline void getBootOverrideSource(
989504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
990491d8ee7SSantosh Puranik {
991deae6a78SEd Tanous     dbus::utility::getProperty<std::string>(
992deae6a78SEd Tanous         "xyz.openbmc_project.Settings",
9931e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
9941e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Source", "BootSource",
995ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
9961e1e598dSJonathan Doman                     const std::string& bootSourceStr) {
997491d8ee7SSantosh Puranik             if (ec)
998491d8ee7SSantosh Puranik             {
9995ef735c8SNan Zhou                 if (ec.value() == boost::asio::error::host_unreachable)
10005ef735c8SNan Zhou                 {
10015ef735c8SNan Zhou                     return;
10025ef735c8SNan Zhou                 }
1003b3e86cb0SGunnar Mills                 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
1004ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
1005491d8ee7SSantosh Puranik                 return;
1006491d8ee7SSantosh Puranik             }
1007491d8ee7SSantosh Puranik 
100862598e31SEd Tanous             BMCWEB_LOG_DEBUG("Boot source: {}", bootSourceStr);
1009491d8ee7SSantosh Puranik 
10101e1e598dSJonathan Doman             auto rfSource = dbusToRfBootSource(bootSourceStr);
1011491d8ee7SSantosh Puranik             if (!rfSource.empty())
1012491d8ee7SSantosh Puranik             {
1013ac106bf6SEd Tanous                 asyncResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
1014ac106bf6SEd Tanous                     rfSource;
1015491d8ee7SSantosh Puranik             }
1016cd9a4666SKonstantin Aladyshev 
1017cd9a4666SKonstantin Aladyshev             // Get BootMode as BootSourceOverrideTarget is constructed
1018cd9a4666SKonstantin Aladyshev             // from both BootSource and BootMode
1019ac106bf6SEd Tanous             getBootOverrideMode(asyncResp);
10201e1e598dSJonathan Doman         });
1021491d8ee7SSantosh Puranik }
1022491d8ee7SSantosh Puranik 
1023491d8ee7SSantosh Puranik /**
1024c21865c4SKonstantin Aladyshev  * @brief This functions abstracts all the logic behind getting a
1025c21865c4SKonstantin Aladyshev  * "BootSourceOverrideEnabled" property from an overall boot override enable
1026c21865c4SKonstantin Aladyshev  * state
1027491d8ee7SSantosh Puranik  *
1028ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
1029491d8ee7SSantosh Puranik  *
1030491d8ee7SSantosh Puranik  * @return None.
1031491d8ee7SSantosh Puranik  */
1032491d8ee7SSantosh Puranik 
1033ac106bf6SEd Tanous inline void processBootOverrideEnable(
1034ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1035c21865c4SKonstantin Aladyshev     const bool bootOverrideEnableSetting)
1036c21865c4SKonstantin Aladyshev {
1037c21865c4SKonstantin Aladyshev     if (!bootOverrideEnableSetting)
1038c21865c4SKonstantin Aladyshev     {
1039ac106bf6SEd Tanous         asyncResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
1040ac106bf6SEd Tanous             "Disabled";
1041c21865c4SKonstantin Aladyshev         return;
1042c21865c4SKonstantin Aladyshev     }
1043c21865c4SKonstantin Aladyshev 
1044c21865c4SKonstantin Aladyshev     // If boot source override is enabled, we need to check 'one_time'
1045c21865c4SKonstantin Aladyshev     // property to set a correct value for the "BootSourceOverrideEnabled"
1046deae6a78SEd Tanous     dbus::utility::getProperty<bool>(
1047deae6a78SEd Tanous         "xyz.openbmc_project.Settings",
10481e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot/one_time",
10491e1e598dSJonathan Doman         "xyz.openbmc_project.Object.Enable", "Enabled",
1050ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec, bool oneTimeSetting) {
1051491d8ee7SSantosh Puranik             if (ec)
1052491d8ee7SSantosh Puranik             {
1053b3e86cb0SGunnar Mills                 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
1054ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
1055491d8ee7SSantosh Puranik                 return;
1056491d8ee7SSantosh Puranik             }
1057491d8ee7SSantosh Puranik 
1058c21865c4SKonstantin Aladyshev             if (oneTimeSetting)
1059c21865c4SKonstantin Aladyshev             {
1060ac106bf6SEd Tanous                 asyncResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
1061ac106bf6SEd Tanous                     "Once";
1062c21865c4SKonstantin Aladyshev             }
1063c21865c4SKonstantin Aladyshev             else
1064c21865c4SKonstantin Aladyshev             {
1065ac106bf6SEd Tanous                 asyncResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
1066c21865c4SKonstantin Aladyshev                     "Continuous";
1067c21865c4SKonstantin Aladyshev             }
10681e1e598dSJonathan Doman         });
1069491d8ee7SSantosh Puranik }
1070491d8ee7SSantosh Puranik 
1071491d8ee7SSantosh Puranik /**
1072c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override enable over DBUS
1073c21865c4SKonstantin Aladyshev  *
1074ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
1075c21865c4SKonstantin Aladyshev  *
1076c21865c4SKonstantin Aladyshev  * @return None.
1077c21865c4SKonstantin Aladyshev  */
1078c21865c4SKonstantin Aladyshev 
1079504af5a0SPatrick Williams inline void getBootOverrideEnable(
1080504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1081c21865c4SKonstantin Aladyshev {
1082deae6a78SEd Tanous     dbus::utility::getProperty<bool>(
1083deae6a78SEd Tanous         "xyz.openbmc_project.Settings",
10841e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
10851e1e598dSJonathan Doman         "xyz.openbmc_project.Object.Enable", "Enabled",
1086ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
10871e1e598dSJonathan Doman                     const bool bootOverrideEnable) {
1088c21865c4SKonstantin Aladyshev             if (ec)
1089c21865c4SKonstantin Aladyshev             {
10905ef735c8SNan Zhou                 if (ec.value() == boost::asio::error::host_unreachable)
10915ef735c8SNan Zhou                 {
10925ef735c8SNan Zhou                     return;
10935ef735c8SNan Zhou                 }
1094b3e86cb0SGunnar Mills                 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
1095ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
1096c21865c4SKonstantin Aladyshev                 return;
1097c21865c4SKonstantin Aladyshev             }
1098c21865c4SKonstantin Aladyshev 
1099ac106bf6SEd Tanous             processBootOverrideEnable(asyncResp, bootOverrideEnable);
11001e1e598dSJonathan Doman         });
1101c21865c4SKonstantin Aladyshev }
1102c21865c4SKonstantin Aladyshev 
1103c21865c4SKonstantin Aladyshev /**
1104c21865c4SKonstantin Aladyshev  * @brief Retrieves boot source override properties
1105c21865c4SKonstantin Aladyshev  *
1106ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
1107c21865c4SKonstantin Aladyshev  *
1108c21865c4SKonstantin Aladyshev  * @return None.
1109c21865c4SKonstantin Aladyshev  */
1110504af5a0SPatrick Williams inline void getBootProperties(
1111504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1112c21865c4SKonstantin Aladyshev {
111362598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get boot information.");
1114c21865c4SKonstantin Aladyshev 
1115ac106bf6SEd Tanous     getBootOverrideSource(asyncResp);
1116ac106bf6SEd Tanous     getBootOverrideType(asyncResp);
1117ac106bf6SEd Tanous     getBootOverrideEnable(asyncResp);
1118c21865c4SKonstantin Aladyshev }
1119c21865c4SKonstantin Aladyshev 
1120c21865c4SKonstantin Aladyshev /**
1121c0557e1aSGunnar Mills  * @brief Retrieves the Last Reset Time
1122c0557e1aSGunnar Mills  *
1123c0557e1aSGunnar Mills  * "Reset" is an overloaded term in Redfish, "Reset" includes power on
1124c0557e1aSGunnar Mills  * and power off. Even though this is the "system" Redfish object look at the
1125c0557e1aSGunnar Mills  * chassis D-Bus interface for the LastStateChangeTime since this has the
1126c0557e1aSGunnar Mills  * last power operation time.
1127c0557e1aSGunnar Mills  *
1128ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
1129c0557e1aSGunnar Mills  *
1130c0557e1aSGunnar Mills  * @return None.
1131c0557e1aSGunnar Mills  */
1132504af5a0SPatrick Williams inline void getLastResetTime(
1133504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1134c0557e1aSGunnar Mills {
113562598e31SEd Tanous     BMCWEB_LOG_DEBUG("Getting System Last Reset Time");
1136c0557e1aSGunnar Mills 
1137deae6a78SEd Tanous     dbus::utility::getProperty<uint64_t>(
1138deae6a78SEd Tanous         "xyz.openbmc_project.State.Chassis",
11391e1e598dSJonathan Doman         "/xyz/openbmc_project/state/chassis0",
11401e1e598dSJonathan Doman         "xyz.openbmc_project.State.Chassis", "LastStateChangeTime",
1141ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
1142ac106bf6SEd Tanous                     uint64_t lastResetTime) {
1143c0557e1aSGunnar Mills             if (ec)
1144c0557e1aSGunnar Mills             {
114562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
1146c0557e1aSGunnar Mills                 return;
1147c0557e1aSGunnar Mills             }
1148c0557e1aSGunnar Mills 
1149c0557e1aSGunnar Mills             // LastStateChangeTime is epoch time, in milliseconds
1150c0557e1aSGunnar Mills             // https://github.com/openbmc/phosphor-dbus-interfaces/blob/33e8e1dd64da53a66e888d33dc82001305cd0bf9/xyz/openbmc_project/State/Chassis.interface.yaml#L19
11511e1e598dSJonathan Doman             uint64_t lastResetTimeStamp = lastResetTime / 1000;
1152c0557e1aSGunnar Mills 
1153c0557e1aSGunnar Mills             // Convert to ISO 8601 standard
1154ac106bf6SEd Tanous             asyncResp->res.jsonValue["LastResetTime"] =
11552b82937eSEd Tanous                 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
11561e1e598dSJonathan Doman         });
1157c0557e1aSGunnar Mills }
1158c0557e1aSGunnar Mills 
1159c0557e1aSGunnar Mills /**
1160797d5daeSCorey Hardesty  * @brief Retrieves the number of automatic boot Retry attempts allowed/left.
1161797d5daeSCorey Hardesty  *
1162797d5daeSCorey Hardesty  * The total number of automatic reboot retries allowed "RetryAttempts" and its
1163797d5daeSCorey Hardesty  * corresponding property "AttemptsLeft" that keeps track of the amount of
1164797d5daeSCorey Hardesty  * automatic retry attempts left are hosted in phosphor-state-manager through
1165797d5daeSCorey Hardesty  * dbus.
1166797d5daeSCorey Hardesty  *
1167ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
1168797d5daeSCorey Hardesty  *
1169797d5daeSCorey Hardesty  * @return None.
1170797d5daeSCorey Hardesty  */
1171ac106bf6SEd Tanous inline void getAutomaticRebootAttempts(
1172ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1173797d5daeSCorey Hardesty {
117462598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get Automatic Retry policy");
1175797d5daeSCorey Hardesty 
1176deae6a78SEd Tanous     dbus::utility::getAllProperties(
1177deae6a78SEd Tanous         "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
1178797d5daeSCorey Hardesty         "xyz.openbmc_project.Control.Boot.RebootAttempts",
1179ac106bf6SEd Tanous         [asyncResp{asyncResp}](
1180ac106bf6SEd Tanous             const boost::system::error_code& ec,
1181797d5daeSCorey Hardesty             const dbus::utility::DBusPropertiesMap& propertiesList) {
1182797d5daeSCorey Hardesty             if (ec)
1183797d5daeSCorey Hardesty             {
1184797d5daeSCorey Hardesty                 if (ec.value() != EBADR)
1185797d5daeSCorey Hardesty                 {
118662598e31SEd Tanous                     BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
1187ac106bf6SEd Tanous                     messages::internalError(asyncResp->res);
1188797d5daeSCorey Hardesty                 }
1189797d5daeSCorey Hardesty                 return;
1190797d5daeSCorey Hardesty             }
1191797d5daeSCorey Hardesty 
1192797d5daeSCorey Hardesty             const uint32_t* attemptsLeft = nullptr;
1193797d5daeSCorey Hardesty             const uint32_t* retryAttempts = nullptr;
1194797d5daeSCorey Hardesty 
1195797d5daeSCorey Hardesty             const bool success = sdbusplus::unpackPropertiesNoThrow(
1196bd79bce8SPatrick Williams                 dbus_utils::UnpackErrorPrinter(), propertiesList,
1197bd79bce8SPatrick Williams                 "AttemptsLeft", attemptsLeft, "RetryAttempts", retryAttempts);
1198797d5daeSCorey Hardesty 
1199797d5daeSCorey Hardesty             if (!success)
1200797d5daeSCorey Hardesty             {
1201ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
1202797d5daeSCorey Hardesty                 return;
1203797d5daeSCorey Hardesty             }
1204797d5daeSCorey Hardesty 
1205797d5daeSCorey Hardesty             if (attemptsLeft != nullptr)
1206797d5daeSCorey Hardesty             {
1207ac106bf6SEd Tanous                 asyncResp->res
1208ac106bf6SEd Tanous                     .jsonValue["Boot"]["RemainingAutomaticRetryAttempts"] =
1209797d5daeSCorey Hardesty                     *attemptsLeft;
1210797d5daeSCorey Hardesty             }
1211797d5daeSCorey Hardesty 
1212797d5daeSCorey Hardesty             if (retryAttempts != nullptr)
1213797d5daeSCorey Hardesty             {
1214ac106bf6SEd Tanous                 asyncResp->res.jsonValue["Boot"]["AutomaticRetryAttempts"] =
1215797d5daeSCorey Hardesty                     *retryAttempts;
1216797d5daeSCorey Hardesty             }
1217797d5daeSCorey Hardesty         });
1218797d5daeSCorey Hardesty }
1219797d5daeSCorey Hardesty 
1220797d5daeSCorey Hardesty /**
12216bd5a8d2SGunnar Mills  * @brief Retrieves Automatic Retry properties. Known on D-Bus as AutoReboot.
12226bd5a8d2SGunnar Mills  *
1223ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
12246bd5a8d2SGunnar Mills  *
12256bd5a8d2SGunnar Mills  * @return None.
12266bd5a8d2SGunnar Mills  */
1227504af5a0SPatrick Williams inline void getAutomaticRetryPolicy(
1228504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12296bd5a8d2SGunnar Mills {
123062598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get Automatic Retry policy");
12316bd5a8d2SGunnar Mills 
1232deae6a78SEd Tanous     dbus::utility::getProperty<bool>(
1233deae6a78SEd Tanous         "xyz.openbmc_project.Settings",
12341e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/auto_reboot",
12351e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
1236ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
1237ac106bf6SEd Tanous                     bool autoRebootEnabled) {
12386bd5a8d2SGunnar Mills             if (ec)
12396bd5a8d2SGunnar Mills             {
1240797d5daeSCorey Hardesty                 if (ec.value() != EBADR)
1241797d5daeSCorey Hardesty                 {
124262598e31SEd Tanous                     BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
1243ac106bf6SEd Tanous                     messages::internalError(asyncResp->res);
1244797d5daeSCorey Hardesty                 }
12456bd5a8d2SGunnar Mills                 return;
12466bd5a8d2SGunnar Mills             }
12476bd5a8d2SGunnar Mills 
124862598e31SEd Tanous             BMCWEB_LOG_DEBUG("Auto Reboot: {}", autoRebootEnabled);
1249e05aec50SEd Tanous             if (autoRebootEnabled)
12506bd5a8d2SGunnar Mills             {
1251ac106bf6SEd Tanous                 asyncResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
12526bd5a8d2SGunnar Mills                     "RetryAttempts";
12536bd5a8d2SGunnar Mills             }
12546bd5a8d2SGunnar Mills             else
12556bd5a8d2SGunnar Mills             {
1256ac106bf6SEd Tanous                 asyncResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
1257ac106bf6SEd Tanous                     "Disabled";
12586bd5a8d2SGunnar Mills             }
1259ac106bf6SEd Tanous             getAutomaticRebootAttempts(asyncResp);
126069f35306SGunnar Mills 
126169f35306SGunnar Mills             // "AutomaticRetryConfig" can be 3 values, Disabled, RetryAlways,
126269f35306SGunnar Mills             // and RetryAttempts. OpenBMC only supports Disabled and
126369f35306SGunnar Mills             // RetryAttempts.
126420fa6a2cSEd Tanous             nlohmann::json::array_t allowed;
126520fa6a2cSEd Tanous             allowed.emplace_back("Disabled");
126620fa6a2cSEd Tanous             allowed.emplace_back("RetryAttempts");
1267ac106bf6SEd Tanous             asyncResp->res
1268bd79bce8SPatrick Williams                 .jsonValue["Boot"]
1269bd79bce8SPatrick Williams                           ["AutomaticRetryConfig@Redfish.AllowableValues"] =
127020fa6a2cSEd Tanous                 std::move(allowed);
12711e1e598dSJonathan Doman         });
12726bd5a8d2SGunnar Mills }
12736bd5a8d2SGunnar Mills 
12746bd5a8d2SGunnar Mills /**
1275797d5daeSCorey Hardesty  * @brief Sets RetryAttempts
1276797d5daeSCorey Hardesty  *
1277ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
1278797d5daeSCorey Hardesty  * @param[in] retryAttempts  "AutomaticRetryAttempts" from request.
1279797d5daeSCorey Hardesty  *
1280797d5daeSCorey Hardesty  *@return None.
1281797d5daeSCorey Hardesty  */
1282797d5daeSCorey Hardesty 
1283ac106bf6SEd Tanous inline void setAutomaticRetryAttempts(
1284ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1285797d5daeSCorey Hardesty     const uint32_t retryAttempts)
1286797d5daeSCorey Hardesty {
128762598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set Automatic Retry Attempts.");
128887c44966SAsmitha Karunanithi     setDbusProperty(
1289e93abac6SGinu George         asyncResp, "Boot/AutomaticRetryAttempts",
1290e93abac6SGinu George         "xyz.openbmc_project.State.Host",
129187c44966SAsmitha Karunanithi         sdbusplus::message::object_path("/xyz/openbmc_project/state/host0"),
12929ae226faSGeorge Liu         "xyz.openbmc_project.Control.Boot.RebootAttempts", "RetryAttempts",
1293e93abac6SGinu George         retryAttempts);
1294797d5daeSCorey Hardesty }
1295797d5daeSCorey Hardesty 
12968d69c668SEd Tanous inline computer_system::PowerRestorePolicyTypes
12978d69c668SEd Tanous     redfishPowerRestorePolicyFromDbus(std::string_view value)
12988d69c668SEd Tanous {
12998d69c668SEd Tanous     if (value ==
13008d69c668SEd Tanous         "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn")
13018d69c668SEd Tanous     {
13028d69c668SEd Tanous         return computer_system::PowerRestorePolicyTypes::AlwaysOn;
13038d69c668SEd Tanous     }
13048d69c668SEd Tanous     if (value ==
13058d69c668SEd Tanous         "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff")
13068d69c668SEd Tanous     {
13078d69c668SEd Tanous         return computer_system::PowerRestorePolicyTypes::AlwaysOff;
13088d69c668SEd Tanous     }
13098d69c668SEd Tanous     if (value ==
13103a34b742SGunnar Mills         "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore")
13118d69c668SEd Tanous     {
13128d69c668SEd Tanous         return computer_system::PowerRestorePolicyTypes::LastState;
13138d69c668SEd Tanous     }
13148d69c668SEd Tanous     if (value == "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.None")
13158d69c668SEd Tanous     {
13168d69c668SEd Tanous         return computer_system::PowerRestorePolicyTypes::AlwaysOff;
13178d69c668SEd Tanous     }
13188d69c668SEd Tanous     return computer_system::PowerRestorePolicyTypes::Invalid;
13198d69c668SEd Tanous }
1320797d5daeSCorey Hardesty /**
1321c6a620f2SGeorge Liu  * @brief Retrieves power restore policy over DBUS.
1322c6a620f2SGeorge Liu  *
1323ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
1324c6a620f2SGeorge Liu  *
1325c6a620f2SGeorge Liu  * @return None.
1326c6a620f2SGeorge Liu  */
1327504af5a0SPatrick Williams inline void getPowerRestorePolicy(
1328504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1329c6a620f2SGeorge Liu {
133062598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get power restore policy");
1331c6a620f2SGeorge Liu 
1332deae6a78SEd Tanous     dbus::utility::getProperty<std::string>(
1333deae6a78SEd Tanous         "xyz.openbmc_project.Settings",
13341e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/power_restore_policy",
13351e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
1336ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
13375e7e2dc5SEd Tanous                     const std::string& policy) {
1338c6a620f2SGeorge Liu             if (ec)
1339c6a620f2SGeorge Liu             {
134062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
1341c6a620f2SGeorge Liu                 return;
1342c6a620f2SGeorge Liu             }
13438d69c668SEd Tanous             computer_system::PowerRestorePolicyTypes restore =
13448d69c668SEd Tanous                 redfishPowerRestorePolicyFromDbus(policy);
13458d69c668SEd Tanous             if (restore == computer_system::PowerRestorePolicyTypes::Invalid)
1346c6a620f2SGeorge Liu             {
1347ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
1348c6a620f2SGeorge Liu                 return;
1349c6a620f2SGeorge Liu             }
1350c6a620f2SGeorge Liu 
13518d69c668SEd Tanous             asyncResp->res.jsonValue["PowerRestorePolicy"] = restore;
13521e1e598dSJonathan Doman         });
1353c6a620f2SGeorge Liu }
1354c6a620f2SGeorge Liu 
1355c6a620f2SGeorge Liu /**
13569dcfe8c1SAlbert Zhang  * @brief Stop Boot On Fault over DBUS.
13579dcfe8c1SAlbert Zhang  *
13589dcfe8c1SAlbert Zhang  * @param[in] asyncResp     Shared pointer for generating response message.
13599dcfe8c1SAlbert Zhang  *
13609dcfe8c1SAlbert Zhang  * @return None.
13619dcfe8c1SAlbert Zhang  */
1362504af5a0SPatrick Williams inline void getStopBootOnFault(
1363504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13649dcfe8c1SAlbert Zhang {
136562598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get Stop Boot On Fault");
13669dcfe8c1SAlbert Zhang 
1367deae6a78SEd Tanous     dbus::utility::getProperty<bool>(
1368deae6a78SEd Tanous         "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings",
13699dcfe8c1SAlbert Zhang         "xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError",
13709dcfe8c1SAlbert Zhang         [asyncResp](const boost::system::error_code& ec, bool value) {
13719dcfe8c1SAlbert Zhang             if (ec)
13729dcfe8c1SAlbert Zhang             {
13739dcfe8c1SAlbert Zhang                 if (ec.value() != EBADR)
13749dcfe8c1SAlbert Zhang                 {
1375b3e86cb0SGunnar Mills                     BMCWEB_LOG_ERROR("DBUS response error {}", ec);
13769dcfe8c1SAlbert Zhang                     messages::internalError(asyncResp->res);
13779dcfe8c1SAlbert Zhang                 }
13789dcfe8c1SAlbert Zhang                 return;
13799dcfe8c1SAlbert Zhang             }
13809dcfe8c1SAlbert Zhang 
13819dcfe8c1SAlbert Zhang             if (value)
13829dcfe8c1SAlbert Zhang             {
1383539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Boot"]["StopBootOnFault"] =
1384539d8c6bSEd Tanous                     computer_system::StopBootOnFault::AnyFault;
13859dcfe8c1SAlbert Zhang             }
13869dcfe8c1SAlbert Zhang             else
13879dcfe8c1SAlbert Zhang             {
1388539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Boot"]["StopBootOnFault"] =
1389539d8c6bSEd Tanous                     computer_system::StopBootOnFault::Never;
13909dcfe8c1SAlbert Zhang             }
13919dcfe8c1SAlbert Zhang         });
13929dcfe8c1SAlbert Zhang }
13939dcfe8c1SAlbert Zhang 
13949dcfe8c1SAlbert Zhang /**
13951981771bSAli Ahmed  * @brief Get TrustedModuleRequiredToBoot property. Determines whether or not
13961981771bSAli Ahmed  * TPM is required for booting the host.
13971981771bSAli Ahmed  *
1398ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
13991981771bSAli Ahmed  *
14001981771bSAli Ahmed  * @return None.
14011981771bSAli Ahmed  */
14021981771bSAli Ahmed inline void getTrustedModuleRequiredToBoot(
1403ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14041981771bSAli Ahmed {
140562598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get TPM required to boot.");
1406e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1407e99073f5SGeorge Liu         "xyz.openbmc_project.Control.TPM.Policy"};
1408e99073f5SGeorge Liu     dbus::utility::getSubTree(
1409e99073f5SGeorge Liu         "/", 0, interfaces,
1410ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
1411b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
14121981771bSAli Ahmed             if (ec)
14131981771bSAli Ahmed             {
1414bd79bce8SPatrick Williams                 BMCWEB_LOG_DEBUG(
1415bd79bce8SPatrick Williams                     "DBUS response error on TPM.Policy GetSubTree{}", ec);
14161981771bSAli Ahmed                 // This is an optional D-Bus object so just return if
14171981771bSAli Ahmed                 // error occurs
14181981771bSAli Ahmed                 return;
14191981771bSAli Ahmed             }
142026f6976fSEd Tanous             if (subtree.empty())
14211981771bSAli Ahmed             {
14221981771bSAli Ahmed                 // As noted above, this is an optional interface so just return
14231981771bSAli Ahmed                 // if there is no instance found
14241981771bSAli Ahmed                 return;
14251981771bSAli Ahmed             }
14261981771bSAli Ahmed 
14271981771bSAli Ahmed             /* When there is more than one TPMEnable object... */
14281981771bSAli Ahmed             if (subtree.size() > 1)
14291981771bSAli Ahmed             {
143062598e31SEd Tanous                 BMCWEB_LOG_DEBUG(
143162598e31SEd Tanous                     "DBUS response has more than 1 TPM Enable object:{}",
143262598e31SEd Tanous                     subtree.size());
14331981771bSAli Ahmed                 // Throw an internal Error and return
1434ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
14351981771bSAli Ahmed                 return;
14361981771bSAli Ahmed             }
14371981771bSAli Ahmed 
14381981771bSAli Ahmed             // Make sure the Dbus response map has a service and objectPath
14391981771bSAli Ahmed             // field
14401981771bSAli Ahmed             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
14411981771bSAli Ahmed             {
144262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("TPM.Policy mapper error!");
1443ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
14441981771bSAli Ahmed                 return;
14451981771bSAli Ahmed             }
14461981771bSAli Ahmed 
14471981771bSAli Ahmed             const std::string& path = subtree[0].first;
14481981771bSAli Ahmed             const std::string& serv = subtree[0].second.begin()->first;
14491981771bSAli Ahmed 
14501981771bSAli Ahmed             // Valid TPM Enable object found, now reading the current value
1451deae6a78SEd Tanous             dbus::utility::getProperty<bool>(
1452deae6a78SEd Tanous                 serv, path, "xyz.openbmc_project.Control.TPM.Policy",
1453deae6a78SEd Tanous                 "TPMEnable",
1454ac106bf6SEd Tanous                 [asyncResp](const boost::system::error_code& ec2,
1455ac106bf6SEd Tanous                             bool tpmRequired) {
14568a592810SEd Tanous                     if (ec2)
14571981771bSAli Ahmed                     {
1458bd79bce8SPatrick Williams                         BMCWEB_LOG_ERROR(
1459bd79bce8SPatrick Williams                             "D-BUS response error on TPM.Policy Get{}", ec2);
1460ac106bf6SEd Tanous                         messages::internalError(asyncResp->res);
14611981771bSAli Ahmed                         return;
14621981771bSAli Ahmed                     }
14631981771bSAli Ahmed 
14641e1e598dSJonathan Doman                     if (tpmRequired)
14651981771bSAli Ahmed                     {
1466ac106bf6SEd Tanous                         asyncResp->res
1467ac106bf6SEd Tanous                             .jsonValue["Boot"]["TrustedModuleRequiredToBoot"] =
14681981771bSAli Ahmed                             "Required";
14691981771bSAli Ahmed                     }
14701981771bSAli Ahmed                     else
14711981771bSAli Ahmed                     {
1472ac106bf6SEd Tanous                         asyncResp->res
1473ac106bf6SEd Tanous                             .jsonValue["Boot"]["TrustedModuleRequiredToBoot"] =
14741981771bSAli Ahmed                             "Disabled";
14751981771bSAli Ahmed                     }
14761e1e598dSJonathan Doman                 });
1477e99073f5SGeorge Liu         });
14781981771bSAli Ahmed }
14791981771bSAli Ahmed 
14801981771bSAli Ahmed /**
14811c05dae3SAli Ahmed  * @brief Set TrustedModuleRequiredToBoot property. Determines whether or not
14821c05dae3SAli Ahmed  * TPM is required for booting the host.
14831c05dae3SAli Ahmed  *
1484ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
14851c05dae3SAli Ahmed  * @param[in] tpmRequired   Value to set TPM Required To Boot property to.
14861c05dae3SAli Ahmed  *
14871c05dae3SAli Ahmed  * @return None.
14881c05dae3SAli Ahmed  */
14891c05dae3SAli Ahmed inline void setTrustedModuleRequiredToBoot(
1490ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const bool tpmRequired)
14911c05dae3SAli Ahmed {
149262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set TrustedModuleRequiredToBoot.");
1493e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1494e99073f5SGeorge Liu         "xyz.openbmc_project.Control.TPM.Policy"};
1495e99073f5SGeorge Liu     dbus::utility::getSubTree(
1496e99073f5SGeorge Liu         "/", 0, interfaces,
1497ac106bf6SEd Tanous         [asyncResp,
1498e99073f5SGeorge Liu          tpmRequired](const boost::system::error_code& ec,
1499e99073f5SGeorge Liu                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
15001c05dae3SAli Ahmed             if (ec)
15011c05dae3SAli Ahmed             {
1502bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR(
1503bd79bce8SPatrick Williams                     "DBUS response error on TPM.Policy GetSubTree{}", ec);
1504ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
15051c05dae3SAli Ahmed                 return;
15061c05dae3SAli Ahmed             }
150726f6976fSEd Tanous             if (subtree.empty())
15081c05dae3SAli Ahmed             {
1509bd79bce8SPatrick Williams                 messages::propertyValueNotInList(asyncResp->res,
1510bd79bce8SPatrick Williams                                                  "ComputerSystem",
15111c05dae3SAli Ahmed                                                  "TrustedModuleRequiredToBoot");
15121c05dae3SAli Ahmed                 return;
15131c05dae3SAli Ahmed             }
15141c05dae3SAli Ahmed 
15151c05dae3SAli Ahmed             /* When there is more than one TPMEnable object... */
15161c05dae3SAli Ahmed             if (subtree.size() > 1)
15171c05dae3SAli Ahmed             {
151862598e31SEd Tanous                 BMCWEB_LOG_DEBUG(
151962598e31SEd Tanous                     "DBUS response has more than 1 TPM Enable object:{}",
152062598e31SEd Tanous                     subtree.size());
15211c05dae3SAli Ahmed                 // Throw an internal Error and return
1522ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
15231c05dae3SAli Ahmed                 return;
15241c05dae3SAli Ahmed             }
15251c05dae3SAli Ahmed 
15261c05dae3SAli Ahmed             // Make sure the Dbus response map has a service and objectPath
15271c05dae3SAli Ahmed             // field
15281c05dae3SAli Ahmed             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
15291c05dae3SAli Ahmed             {
153062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("TPM.Policy mapper error!");
1531ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
15321c05dae3SAli Ahmed                 return;
15331c05dae3SAli Ahmed             }
15341c05dae3SAli Ahmed 
15351c05dae3SAli Ahmed             const std::string& path = subtree[0].first;
15361c05dae3SAli Ahmed             const std::string& serv = subtree[0].second.begin()->first;
15371c05dae3SAli Ahmed 
15381c05dae3SAli Ahmed             if (serv.empty())
15391c05dae3SAli Ahmed             {
154062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("TPM.Policy service mapper error!");
1541ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
15421c05dae3SAli Ahmed                 return;
15431c05dae3SAli Ahmed             }
15441c05dae3SAli Ahmed 
15451c05dae3SAli Ahmed             // Valid TPM Enable object found, now setting the value
1546e93abac6SGinu George             setDbusProperty(asyncResp, "Boot/TrustedModuleRequiredToBoot", serv,
1547e93abac6SGinu George                             path, "xyz.openbmc_project.Control.TPM.Policy",
1548e93abac6SGinu George                             "TPMEnable", tpmRequired);
1549e99073f5SGeorge Liu         });
15501c05dae3SAli Ahmed }
15511c05dae3SAli Ahmed 
15521c05dae3SAli Ahmed /**
1553491d8ee7SSantosh Puranik  * @brief Sets boot properties into DBUS object(s).
1554491d8ee7SSantosh Puranik  *
1555ac106bf6SEd Tanous  * @param[in] asyncResp       Shared pointer for generating response message.
1556cd9a4666SKonstantin Aladyshev  * @param[in] bootType        The boot type to set.
1557cd9a4666SKonstantin Aladyshev  * @return Integer error code.
1558cd9a4666SKonstantin Aladyshev  */
1559ac106bf6SEd Tanous inline void setBootType(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1560cd9a4666SKonstantin Aladyshev                         const std::optional<std::string>& bootType)
1561cd9a4666SKonstantin Aladyshev {
1562c21865c4SKonstantin Aladyshev     std::string bootTypeStr;
1563cd9a4666SKonstantin Aladyshev 
1564c21865c4SKonstantin Aladyshev     if (!bootType)
1565cd9a4666SKonstantin Aladyshev     {
1566c21865c4SKonstantin Aladyshev         return;
1567c21865c4SKonstantin Aladyshev     }
1568c21865c4SKonstantin Aladyshev 
1569cd9a4666SKonstantin Aladyshev     // Source target specified
157062598e31SEd Tanous     BMCWEB_LOG_DEBUG("Boot type: {}", *bootType);
1571cd9a4666SKonstantin Aladyshev     // Figure out which DBUS interface and property to use
1572cd9a4666SKonstantin Aladyshev     if (*bootType == "Legacy")
1573cd9a4666SKonstantin Aladyshev     {
1574cd9a4666SKonstantin Aladyshev         bootTypeStr = "xyz.openbmc_project.Control.Boot.Type.Types.Legacy";
1575cd9a4666SKonstantin Aladyshev     }
1576cd9a4666SKonstantin Aladyshev     else if (*bootType == "UEFI")
1577cd9a4666SKonstantin Aladyshev     {
1578cd9a4666SKonstantin Aladyshev         bootTypeStr = "xyz.openbmc_project.Control.Boot.Type.Types.EFI";
1579cd9a4666SKonstantin Aladyshev     }
1580cd9a4666SKonstantin Aladyshev     else
1581cd9a4666SKonstantin Aladyshev     {
158262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid property value for "
158362598e31SEd Tanous                          "BootSourceOverrideMode: {}",
158462598e31SEd Tanous                          *bootType);
1585ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *bootType,
1586cd9a4666SKonstantin Aladyshev                                          "BootSourceOverrideMode");
1587cd9a4666SKonstantin Aladyshev         return;
1588cd9a4666SKonstantin Aladyshev     }
1589cd9a4666SKonstantin Aladyshev 
1590cd9a4666SKonstantin Aladyshev     // Act on validated parameters
159162598e31SEd Tanous     BMCWEB_LOG_DEBUG("DBUS boot type: {}", bootTypeStr);
1592cd9a4666SKonstantin Aladyshev 
1593e93abac6SGinu George     setDbusProperty(asyncResp, "Boot/BootSourceOverrideMode",
1594e93abac6SGinu George                     "xyz.openbmc_project.Settings",
159587c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
159687c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/boot"),
159787c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Control.Boot.Type", "BootType",
1598e93abac6SGinu George                     bootTypeStr);
1599cd9a4666SKonstantin Aladyshev }
1600cd9a4666SKonstantin Aladyshev 
1601cd9a4666SKonstantin Aladyshev /**
1602cd9a4666SKonstantin Aladyshev  * @brief Sets boot properties into DBUS object(s).
1603cd9a4666SKonstantin Aladyshev  *
1604ac106bf6SEd Tanous  * @param[in] asyncResp           Shared pointer for generating response
1605ac106bf6SEd Tanous  * message.
1606c21865c4SKonstantin Aladyshev  * @param[in] bootType        The boot type to set.
1607c21865c4SKonstantin Aladyshev  * @return Integer error code.
1608c21865c4SKonstantin Aladyshev  */
1609ac106bf6SEd Tanous inline void setBootEnable(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1610c21865c4SKonstantin Aladyshev                           const std::optional<std::string>& bootEnable)
1611c21865c4SKonstantin Aladyshev {
1612c21865c4SKonstantin Aladyshev     if (!bootEnable)
1613c21865c4SKonstantin Aladyshev     {
1614c21865c4SKonstantin Aladyshev         return;
1615c21865c4SKonstantin Aladyshev     }
1616c21865c4SKonstantin Aladyshev     // Source target specified
161762598e31SEd Tanous     BMCWEB_LOG_DEBUG("Boot enable: {}", *bootEnable);
1618c21865c4SKonstantin Aladyshev 
1619c21865c4SKonstantin Aladyshev     bool bootOverrideEnable = false;
1620c21865c4SKonstantin Aladyshev     bool bootOverridePersistent = false;
1621c21865c4SKonstantin Aladyshev     // Figure out which DBUS interface and property to use
1622c21865c4SKonstantin Aladyshev     if (*bootEnable == "Disabled")
1623c21865c4SKonstantin Aladyshev     {
1624c21865c4SKonstantin Aladyshev         bootOverrideEnable = false;
1625c21865c4SKonstantin Aladyshev     }
1626c21865c4SKonstantin Aladyshev     else if (*bootEnable == "Once")
1627c21865c4SKonstantin Aladyshev     {
1628c21865c4SKonstantin Aladyshev         bootOverrideEnable = true;
1629c21865c4SKonstantin Aladyshev         bootOverridePersistent = false;
1630c21865c4SKonstantin Aladyshev     }
1631c21865c4SKonstantin Aladyshev     else if (*bootEnable == "Continuous")
1632c21865c4SKonstantin Aladyshev     {
1633c21865c4SKonstantin Aladyshev         bootOverrideEnable = true;
1634c21865c4SKonstantin Aladyshev         bootOverridePersistent = true;
1635c21865c4SKonstantin Aladyshev     }
1636c21865c4SKonstantin Aladyshev     else
1637c21865c4SKonstantin Aladyshev     {
163862598e31SEd Tanous         BMCWEB_LOG_DEBUG(
163962598e31SEd Tanous             "Invalid property value for BootSourceOverrideEnabled: {}",
164062598e31SEd Tanous             *bootEnable);
1641ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *bootEnable,
1642c21865c4SKonstantin Aladyshev                                          "BootSourceOverrideEnabled");
1643c21865c4SKonstantin Aladyshev         return;
1644c21865c4SKonstantin Aladyshev     }
1645c21865c4SKonstantin Aladyshev 
1646c21865c4SKonstantin Aladyshev     // Act on validated parameters
164762598e31SEd Tanous     BMCWEB_LOG_DEBUG("DBUS boot override enable: {}", bootOverrideEnable);
1648c21865c4SKonstantin Aladyshev 
1649e93abac6SGinu George     setDbusProperty(asyncResp, "Boot/BootSourceOverrideEnabled",
1650e93abac6SGinu George                     "xyz.openbmc_project.Settings",
165187c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
165287c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/boot"),
165387c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Object.Enable", "Enabled",
1654e93abac6SGinu George                     bootOverrideEnable);
1655c21865c4SKonstantin Aladyshev 
1656c21865c4SKonstantin Aladyshev     if (!bootOverrideEnable)
1657c21865c4SKonstantin Aladyshev     {
1658c21865c4SKonstantin Aladyshev         return;
1659c21865c4SKonstantin Aladyshev     }
1660c21865c4SKonstantin Aladyshev 
1661c21865c4SKonstantin Aladyshev     // In case boot override is enabled we need to set correct value for the
1662c21865c4SKonstantin Aladyshev     // 'one_time' enable DBus interface
166362598e31SEd Tanous     BMCWEB_LOG_DEBUG("DBUS boot override persistent: {}",
166462598e31SEd Tanous                      bootOverridePersistent);
1665c21865c4SKonstantin Aladyshev 
1666e93abac6SGinu George     setDbusProperty(asyncResp, "Boot/BootSourceOverrideEnabled",
1667e93abac6SGinu George                     "xyz.openbmc_project.Settings",
166887c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
166987c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/boot/one_time"),
167087c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Object.Enable", "Enabled",
1671e93abac6SGinu George                     !bootOverridePersistent);
1672c21865c4SKonstantin Aladyshev }
1673c21865c4SKonstantin Aladyshev 
1674c21865c4SKonstantin Aladyshev /**
1675c21865c4SKonstantin Aladyshev  * @brief Sets boot properties into DBUS object(s).
1676c21865c4SKonstantin Aladyshev  *
1677ac106bf6SEd Tanous  * @param[in] asyncResp       Shared pointer for generating response message.
1678491d8ee7SSantosh Puranik  * @param[in] bootSource      The boot source to set.
1679491d8ee7SSantosh Puranik  *
1680265c1602SJohnathan Mantey  * @return Integer error code.
1681491d8ee7SSantosh Puranik  */
1682504af5a0SPatrick Williams inline void setBootModeOrSource(
1683504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1684cd9a4666SKonstantin Aladyshev     const std::optional<std::string>& bootSource)
1685491d8ee7SSantosh Puranik {
1686c21865c4SKonstantin Aladyshev     std::string bootSourceStr;
1687c21865c4SKonstantin Aladyshev     std::string bootModeStr;
1688944ffaf9SJohnathan Mantey 
1689c21865c4SKonstantin Aladyshev     if (!bootSource)
1690491d8ee7SSantosh Puranik     {
1691c21865c4SKonstantin Aladyshev         return;
1692c21865c4SKonstantin Aladyshev     }
1693c21865c4SKonstantin Aladyshev 
1694491d8ee7SSantosh Puranik     // Source target specified
169562598e31SEd Tanous     BMCWEB_LOG_DEBUG("Boot source: {}", *bootSource);
1696491d8ee7SSantosh Puranik     // Figure out which DBUS interface and property to use
1697ac106bf6SEd Tanous     if (assignBootParameters(asyncResp, *bootSource, bootSourceStr,
1698ac106bf6SEd Tanous                              bootModeStr) != 0)
1699491d8ee7SSantosh Puranik     {
170062598e31SEd Tanous         BMCWEB_LOG_DEBUG(
170162598e31SEd Tanous             "Invalid property value for BootSourceOverrideTarget: {}",
170262598e31SEd Tanous             *bootSource);
1703ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *bootSource,
1704491d8ee7SSantosh Puranik                                          "BootSourceTargetOverride");
1705491d8ee7SSantosh Puranik         return;
1706491d8ee7SSantosh Puranik     }
1707491d8ee7SSantosh Puranik 
1708944ffaf9SJohnathan Mantey     // Act on validated parameters
170962598e31SEd Tanous     BMCWEB_LOG_DEBUG("DBUS boot source: {}", bootSourceStr);
171062598e31SEd Tanous     BMCWEB_LOG_DEBUG("DBUS boot mode: {}", bootModeStr);
1711944ffaf9SJohnathan Mantey 
1712e93abac6SGinu George     setDbusProperty(asyncResp, "Boot/BootSourceOverrideTarget",
1713e93abac6SGinu George                     "xyz.openbmc_project.Settings",
171487c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
171587c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/boot"),
171687c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Control.Boot.Source", "BootSource",
1717e93abac6SGinu George                     bootSourceStr);
1718e93abac6SGinu George     setDbusProperty(asyncResp, "Boot/BootSourceOverrideTarget",
1719e93abac6SGinu George                     "xyz.openbmc_project.Settings",
172087c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
172187c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/boot"),
172287c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
1723e93abac6SGinu George                     bootModeStr);
1724cd9a4666SKonstantin Aladyshev }
1725944ffaf9SJohnathan Mantey 
1726cd9a4666SKonstantin Aladyshev /**
1727c21865c4SKonstantin Aladyshev  * @brief Sets Boot source override properties.
1728491d8ee7SSantosh Puranik  *
1729ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
1730491d8ee7SSantosh Puranik  * @param[in] bootSource The boot source from incoming RF request.
1731cd9a4666SKonstantin Aladyshev  * @param[in] bootType   The boot type from incoming RF request.
1732491d8ee7SSantosh Puranik  * @param[in] bootEnable The boot override enable from incoming RF request.
1733491d8ee7SSantosh Puranik  *
1734265c1602SJohnathan Mantey  * @return Integer error code.
1735491d8ee7SSantosh Puranik  */
1736c21865c4SKonstantin Aladyshev 
1737504af5a0SPatrick Williams inline void setBootProperties(
1738504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1739c21865c4SKonstantin Aladyshev     const std::optional<std::string>& bootSource,
1740c21865c4SKonstantin Aladyshev     const std::optional<std::string>& bootType,
1741c21865c4SKonstantin Aladyshev     const std::optional<std::string>& bootEnable)
1742491d8ee7SSantosh Puranik {
174362598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set boot information.");
1744491d8ee7SSantosh Puranik 
1745ac106bf6SEd Tanous     setBootModeOrSource(asyncResp, bootSource);
1746ac106bf6SEd Tanous     setBootType(asyncResp, bootType);
1747ac106bf6SEd Tanous     setBootEnable(asyncResp, bootEnable);
1748491d8ee7SSantosh Puranik }
1749491d8ee7SSantosh Puranik 
1750c6a620f2SGeorge Liu /**
175198e386ecSGunnar Mills  * @brief Sets AssetTag
175298e386ecSGunnar Mills  *
1753ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for generating response message.
175498e386ecSGunnar Mills  * @param[in] assetTag  "AssetTag" from request.
175598e386ecSGunnar Mills  *
175698e386ecSGunnar Mills  * @return None.
175798e386ecSGunnar Mills  */
1758ac106bf6SEd Tanous inline void setAssetTag(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
175998e386ecSGunnar Mills                         const std::string& assetTag)
176098e386ecSGunnar Mills {
1761e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1762e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.System"};
1763e99073f5SGeorge Liu     dbus::utility::getSubTree(
1764e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
1765ac106bf6SEd Tanous         [asyncResp,
1766e99073f5SGeorge Liu          assetTag](const boost::system::error_code& ec,
1767b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
176898e386ecSGunnar Mills             if (ec)
176998e386ecSGunnar Mills             {
177062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("D-Bus response error on GetSubTree {}", ec);
1771ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
177298e386ecSGunnar Mills                 return;
177398e386ecSGunnar Mills             }
177426f6976fSEd Tanous             if (subtree.empty())
177598e386ecSGunnar Mills             {
177662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't find system D-Bus object!");
1777ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
177898e386ecSGunnar Mills                 return;
177998e386ecSGunnar Mills             }
178098e386ecSGunnar Mills             // Assume only 1 system D-Bus object
178198e386ecSGunnar Mills             // Throw an error if there is more than 1
178298e386ecSGunnar Mills             if (subtree.size() > 1)
178398e386ecSGunnar Mills             {
178462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Found more than 1 system D-Bus object!");
1785ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
178698e386ecSGunnar Mills                 return;
178798e386ecSGunnar Mills             }
178898e386ecSGunnar Mills             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
178998e386ecSGunnar Mills             {
179062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Asset Tag Set mapper error!");
1791ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
179298e386ecSGunnar Mills                 return;
179398e386ecSGunnar Mills             }
179498e386ecSGunnar Mills 
179598e386ecSGunnar Mills             const std::string& path = subtree[0].first;
179698e386ecSGunnar Mills             const std::string& service = subtree[0].second.begin()->first;
179798e386ecSGunnar Mills 
179898e386ecSGunnar Mills             if (service.empty())
179998e386ecSGunnar Mills             {
180062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Asset Tag Set service mapper error!");
1801ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
180298e386ecSGunnar Mills                 return;
180398e386ecSGunnar Mills             }
180498e386ecSGunnar Mills 
1805e93abac6SGinu George             setDbusProperty(asyncResp, "AssetTag", service, path,
180687c44966SAsmitha Karunanithi                             "xyz.openbmc_project.Inventory.Decorator.AssetTag",
1807e93abac6SGinu George                             "AssetTag", assetTag);
1808e99073f5SGeorge Liu         });
180998e386ecSGunnar Mills }
181098e386ecSGunnar Mills 
181198e386ecSGunnar Mills /**
18129dcfe8c1SAlbert Zhang  * @brief Validate the specified stopBootOnFault is valid and return the
18139dcfe8c1SAlbert Zhang  * stopBootOnFault name associated with that string
18149dcfe8c1SAlbert Zhang  *
18159dcfe8c1SAlbert Zhang  * @param[in] stopBootOnFaultString  String representing the desired
18169dcfe8c1SAlbert Zhang  * stopBootOnFault
18179dcfe8c1SAlbert Zhang  *
18189dcfe8c1SAlbert Zhang  * @return stopBootOnFault value or empty  if incoming value is not valid
18199dcfe8c1SAlbert Zhang  */
1820504af5a0SPatrick Williams inline std::optional<bool> validstopBootOnFault(
1821504af5a0SPatrick Williams     const std::string& stopBootOnFaultString)
18229dcfe8c1SAlbert Zhang {
18239dcfe8c1SAlbert Zhang     if (stopBootOnFaultString == "AnyFault")
18249dcfe8c1SAlbert Zhang     {
18259dcfe8c1SAlbert Zhang         return true;
18269dcfe8c1SAlbert Zhang     }
18279dcfe8c1SAlbert Zhang 
18289dcfe8c1SAlbert Zhang     if (stopBootOnFaultString == "Never")
18299dcfe8c1SAlbert Zhang     {
18309dcfe8c1SAlbert Zhang         return false;
18319dcfe8c1SAlbert Zhang     }
18329dcfe8c1SAlbert Zhang 
18339dcfe8c1SAlbert Zhang     return std::nullopt;
18349dcfe8c1SAlbert Zhang }
18359dcfe8c1SAlbert Zhang 
18369dcfe8c1SAlbert Zhang /**
18379dcfe8c1SAlbert Zhang  * @brief Sets stopBootOnFault
18389dcfe8c1SAlbert Zhang  *
1839fc3edfddSEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
18409dcfe8c1SAlbert Zhang  * @param[in] stopBootOnFault  "StopBootOnFault" from request.
18419dcfe8c1SAlbert Zhang  *
18429dcfe8c1SAlbert Zhang  * @return None.
18439dcfe8c1SAlbert Zhang  */
1844504af5a0SPatrick Williams inline void setStopBootOnFault(
1845504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18469dcfe8c1SAlbert Zhang     const std::string& stopBootOnFault)
18479dcfe8c1SAlbert Zhang {
184862598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set Stop Boot On Fault.");
18499dcfe8c1SAlbert Zhang 
18509dcfe8c1SAlbert Zhang     std::optional<bool> stopBootEnabled = validstopBootOnFault(stopBootOnFault);
18519dcfe8c1SAlbert Zhang     if (!stopBootEnabled)
18529dcfe8c1SAlbert Zhang     {
185362598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid property value for StopBootOnFault: {}",
185462598e31SEd Tanous                          stopBootOnFault);
1855fc3edfddSEd Tanous         messages::propertyValueNotInList(asyncResp->res, stopBootOnFault,
18569dcfe8c1SAlbert Zhang                                          "StopBootOnFault");
18579dcfe8c1SAlbert Zhang         return;
18589dcfe8c1SAlbert Zhang     }
18599dcfe8c1SAlbert Zhang 
1860e93abac6SGinu George     setDbusProperty(asyncResp, "Boot/StopBootOnFault",
1861e93abac6SGinu George                     "xyz.openbmc_project.Settings",
186287c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
186387c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/logging/settings"),
1864fc3edfddSEd Tanous                     "xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError",
1865e93abac6SGinu George                     *stopBootEnabled);
18669dcfe8c1SAlbert Zhang }
18679dcfe8c1SAlbert Zhang 
18689dcfe8c1SAlbert Zhang /**
186969f35306SGunnar Mills  * @brief Sets automaticRetry (Auto Reboot)
187069f35306SGunnar Mills  *
1871ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
187269f35306SGunnar Mills  * @param[in] automaticRetryConfig  "AutomaticRetryConfig" from request.
187369f35306SGunnar Mills  *
187469f35306SGunnar Mills  * @return None.
187569f35306SGunnar Mills  */
1876504af5a0SPatrick Williams inline void setAutomaticRetry(
1877504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1878f23b7296SEd Tanous     const std::string& automaticRetryConfig)
187969f35306SGunnar Mills {
188062598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set Automatic Retry.");
188169f35306SGunnar Mills 
188269f35306SGunnar Mills     // OpenBMC only supports "Disabled" and "RetryAttempts".
1883543f4400SEd Tanous     bool autoRebootEnabled = false;
188469f35306SGunnar Mills 
188569f35306SGunnar Mills     if (automaticRetryConfig == "Disabled")
188669f35306SGunnar Mills     {
188769f35306SGunnar Mills         autoRebootEnabled = false;
188869f35306SGunnar Mills     }
188969f35306SGunnar Mills     else if (automaticRetryConfig == "RetryAttempts")
189069f35306SGunnar Mills     {
189169f35306SGunnar Mills         autoRebootEnabled = true;
189269f35306SGunnar Mills     }
189369f35306SGunnar Mills     else
189469f35306SGunnar Mills     {
189562598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid property value for AutomaticRetryConfig: {}",
189662598e31SEd Tanous                          automaticRetryConfig);
1897ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, automaticRetryConfig,
189869f35306SGunnar Mills                                          "AutomaticRetryConfig");
189969f35306SGunnar Mills         return;
190069f35306SGunnar Mills     }
190169f35306SGunnar Mills 
1902e93abac6SGinu George     setDbusProperty(asyncResp, "Boot/AutomaticRetryConfig",
1903e93abac6SGinu George                     "xyz.openbmc_project.Settings",
190487c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
190587c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/auto_reboot"),
190687c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Control.Boot.RebootPolicy",
1907e93abac6SGinu George                     "AutoReboot", autoRebootEnabled);
190869f35306SGunnar Mills }
190969f35306SGunnar Mills 
19108d69c668SEd Tanous inline std::string dbusPowerRestorePolicyFromRedfish(std::string_view policy)
19118d69c668SEd Tanous {
19128d69c668SEd Tanous     if (policy == "AlwaysOn")
19138d69c668SEd Tanous     {
19148d69c668SEd Tanous         return "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn";
19158d69c668SEd Tanous     }
19168d69c668SEd Tanous     if (policy == "AlwaysOff")
19178d69c668SEd Tanous     {
19188d69c668SEd Tanous         return "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff";
19198d69c668SEd Tanous     }
19208d69c668SEd Tanous     if (policy == "LastState")
19218d69c668SEd Tanous     {
19228d69c668SEd Tanous         return "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore";
19238d69c668SEd Tanous     }
19248d69c668SEd Tanous     return "";
19258d69c668SEd Tanous }
19268d69c668SEd Tanous 
192769f35306SGunnar Mills /**
1928c6a620f2SGeorge Liu  * @brief Sets power restore policy properties.
1929c6a620f2SGeorge Liu  *
1930ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
1931c6a620f2SGeorge Liu  * @param[in] policy  power restore policy properties from request.
1932c6a620f2SGeorge Liu  *
1933c6a620f2SGeorge Liu  * @return None.
1934c6a620f2SGeorge Liu  */
1935504af5a0SPatrick Williams inline void setPowerRestorePolicy(
1936504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19378d69c668SEd Tanous     std::string_view policy)
1938c6a620f2SGeorge Liu {
193962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set power restore policy.");
1940c6a620f2SGeorge Liu 
19418d69c668SEd Tanous     std::string powerRestorePolicy = dbusPowerRestorePolicyFromRedfish(policy);
1942c6a620f2SGeorge Liu 
19438d69c668SEd Tanous     if (powerRestorePolicy.empty())
1944c6a620f2SGeorge Liu     {
1945ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, policy,
19464e69c904SGunnar Mills                                          "PowerRestorePolicy");
1947c6a620f2SGeorge Liu         return;
1948c6a620f2SGeorge Liu     }
1949c6a620f2SGeorge Liu 
195087c44966SAsmitha Karunanithi     setDbusProperty(
1951e93abac6SGinu George         asyncResp, "PowerRestorePolicy", "xyz.openbmc_project.Settings",
195287c44966SAsmitha Karunanithi         sdbusplus::message::object_path(
195387c44966SAsmitha Karunanithi             "/xyz/openbmc_project/control/host0/power_restore_policy"),
19549ae226faSGeorge Liu         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
1955e93abac6SGinu George         powerRestorePolicy);
1956c6a620f2SGeorge Liu }
1957c6a620f2SGeorge Liu 
1958a6349918SAppaRao Puli /**
1959a6349918SAppaRao Puli  * @brief Retrieves provisioning status
1960a6349918SAppaRao Puli  *
196125b54dbaSEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous
196225b54dbaSEd Tanous  * calls.
1963a6349918SAppaRao Puli  *
1964a6349918SAppaRao Puli  * @return None.
1965a6349918SAppaRao Puli  */
1966504af5a0SPatrick Williams inline void getProvisioningStatus(
1967504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1968a6349918SAppaRao Puli {
196962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get OEM information.");
1970deae6a78SEd Tanous     dbus::utility::getAllProperties(
1971deae6a78SEd Tanous         "xyz.openbmc_project.PFR.Manager", "/xyz/openbmc_project/pfr",
1972deae6a78SEd Tanous         "xyz.openbmc_project.PFR.Attributes",
1973ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
1974b9d36b47SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
1975b99fb1a9SAppaRao Puli             nlohmann::json& oemPFR =
1976bd79bce8SPatrick Williams                 asyncResp->res
1977bd79bce8SPatrick Williams                     .jsonValue["Oem"]["OpenBmc"]["FirmwareProvisioning"];
1978ac106bf6SEd Tanous             asyncResp->res.jsonValue["Oem"]["OpenBmc"]["@odata.type"] =
19791d834d49SEd Tanous                 "#OpenBMCComputerSystem.v1_0_0.OpenBmc";
1980bd79bce8SPatrick Williams             oemPFR["@odata.type"] =
1981bd79bce8SPatrick Williams                 "#OpenBMCComputerSystem.FirmwareProvisioning";
198250626f4fSJames Feist 
1983a6349918SAppaRao Puli             if (ec)
1984a6349918SAppaRao Puli             {
198562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
1986b99fb1a9SAppaRao Puli                 // not an error, don't have to have the interface
1987539d8c6bSEd Tanous                 oemPFR["ProvisioningStatus"] = open_bmc_computer_system::
1988539d8c6bSEd Tanous                     FirmwareProvisioningStatus::NotProvisioned;
1989a6349918SAppaRao Puli                 return;
1990a6349918SAppaRao Puli             }
1991a6349918SAppaRao Puli 
1992a6349918SAppaRao Puli             const bool* provState = nullptr;
1993a6349918SAppaRao Puli             const bool* lockState = nullptr;
1994bc1d29deSKrzysztof Grobelny 
1995bc1d29deSKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
1996bd79bce8SPatrick Williams                 dbus_utils::UnpackErrorPrinter(), propertiesList,
1997bd79bce8SPatrick Williams                 "UfmProvisioned", provState, "UfmLocked", lockState);
1998bc1d29deSKrzysztof Grobelny 
1999bc1d29deSKrzysztof Grobelny             if (!success)
2000a6349918SAppaRao Puli             {
2001ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
2002bc1d29deSKrzysztof Grobelny                 return;
2003a6349918SAppaRao Puli             }
2004a6349918SAppaRao Puli 
2005a6349918SAppaRao Puli             if ((provState == nullptr) || (lockState == nullptr))
2006a6349918SAppaRao Puli             {
200762598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Unable to get PFR attributes.");
2008ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
2009a6349918SAppaRao Puli                 return;
2010a6349918SAppaRao Puli             }
2011a6349918SAppaRao Puli 
201225b54dbaSEd Tanous             if (*provState)
2013a6349918SAppaRao Puli             {
201425b54dbaSEd Tanous                 if (*lockState)
2015a6349918SAppaRao Puli                 {
2016539d8c6bSEd Tanous                     oemPFR["ProvisioningStatus"] = open_bmc_computer_system::
2017539d8c6bSEd Tanous                         FirmwareProvisioningStatus::ProvisionedAndLocked;
2018a6349918SAppaRao Puli                 }
2019a6349918SAppaRao Puli                 else
2020a6349918SAppaRao Puli                 {
2021539d8c6bSEd Tanous                     oemPFR["ProvisioningStatus"] = open_bmc_computer_system::
2022539d8c6bSEd Tanous                         FirmwareProvisioningStatus::ProvisionedButNotLocked;
2023a6349918SAppaRao Puli                 }
2024a6349918SAppaRao Puli             }
2025a6349918SAppaRao Puli             else
2026a6349918SAppaRao Puli             {
2027539d8c6bSEd Tanous                 oemPFR["ProvisioningStatus"] = open_bmc_computer_system::
2028539d8c6bSEd Tanous                     FirmwareProvisioningStatus::NotProvisioned;
2029a6349918SAppaRao Puli             }
2030bc1d29deSKrzysztof Grobelny         });
2031a6349918SAppaRao Puli }
2032a6349918SAppaRao Puli 
2033491d8ee7SSantosh Puranik /**
20346b9ac4f2SChris Cain  * @brief Translate the PowerMode string to enum value
20353a2d0424SChris Cain  *
20366b9ac4f2SChris Cain  * @param[in]  modeString PowerMode string to be translated
20373a2d0424SChris Cain  *
20386b9ac4f2SChris Cain  * @return PowerMode enum
20393a2d0424SChris Cain  */
2040504af5a0SPatrick Williams inline computer_system::PowerMode translatePowerModeString(
2041504af5a0SPatrick Williams     const std::string& modeString)
20423a2d0424SChris Cain {
2043b6655101SChris Cain     using PowerMode = computer_system::PowerMode;
2044b6655101SChris Cain 
20456b9ac4f2SChris Cain     if (modeString == "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static")
20463a2d0424SChris Cain     {
20476b9ac4f2SChris Cain         return PowerMode::Static;
20483a2d0424SChris Cain     }
20496b9ac4f2SChris Cain     if (modeString ==
20500fda0f12SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance")
20513a2d0424SChris Cain     {
20526b9ac4f2SChris Cain         return PowerMode::MaximumPerformance;
20533a2d0424SChris Cain     }
20546b9ac4f2SChris Cain     if (modeString ==
20550fda0f12SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving")
20563a2d0424SChris Cain     {
20576b9ac4f2SChris Cain         return PowerMode::PowerSaving;
2058b6655101SChris Cain     }
20596b9ac4f2SChris Cain     if (modeString ==
2060b6655101SChris Cain         "xyz.openbmc_project.Control.Power.Mode.PowerMode.BalancedPerformance")
2061b6655101SChris Cain     {
20626b9ac4f2SChris Cain         return PowerMode::BalancedPerformance;
2063b6655101SChris Cain     }
20646b9ac4f2SChris Cain     if (modeString ==
2065b6655101SChris Cain         "xyz.openbmc_project.Control.Power.Mode.PowerMode.EfficiencyFavorPerformance")
2066b6655101SChris Cain     {
20676b9ac4f2SChris Cain         return PowerMode::EfficiencyFavorPerformance;
2068b6655101SChris Cain     }
20696b9ac4f2SChris Cain     if (modeString ==
2070b6655101SChris Cain         "xyz.openbmc_project.Control.Power.Mode.PowerMode.EfficiencyFavorPower")
2071b6655101SChris Cain     {
20726b9ac4f2SChris Cain         return PowerMode::EfficiencyFavorPower;
20733a2d0424SChris Cain     }
20746b9ac4f2SChris Cain     if (modeString == "xyz.openbmc_project.Control.Power.Mode.PowerMode.OEM")
20753a2d0424SChris Cain     {
20766b9ac4f2SChris Cain         return PowerMode::OEM;
20776b9ac4f2SChris Cain     }
20786b9ac4f2SChris Cain     // Any other values would be invalid
20796b9ac4f2SChris Cain     BMCWEB_LOG_ERROR("PowerMode value was not valid: {}", modeString);
20806b9ac4f2SChris Cain     return PowerMode::Invalid;
20816b9ac4f2SChris Cain }
20826b9ac4f2SChris Cain 
2083504af5a0SPatrick Williams inline void afterGetPowerMode(
2084504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
20856b9ac4f2SChris Cain     const boost::system::error_code& ec,
20866b9ac4f2SChris Cain     const dbus::utility::DBusPropertiesMap& properties)
20876b9ac4f2SChris Cain {
20886b9ac4f2SChris Cain     if (ec)
20896b9ac4f2SChris Cain     {
20906b9ac4f2SChris Cain         BMCWEB_LOG_ERROR("DBUS response error on PowerMode GetAll: {}", ec);
20916b9ac4f2SChris Cain         messages::internalError(asyncResp->res);
20926b9ac4f2SChris Cain         return;
20936b9ac4f2SChris Cain     }
20946b9ac4f2SChris Cain 
20956b9ac4f2SChris Cain     std::string powerMode;
20966b9ac4f2SChris Cain     const std::vector<std::string>* allowedModes = nullptr;
20976b9ac4f2SChris Cain     const bool success = sdbusplus::unpackPropertiesNoThrow(
20986b9ac4f2SChris Cain         dbus_utils::UnpackErrorPrinter(), properties, "PowerMode", powerMode,
20996b9ac4f2SChris Cain         "AllowedPowerModes", allowedModes);
21006b9ac4f2SChris Cain 
21016b9ac4f2SChris Cain     if (!success)
21026b9ac4f2SChris Cain     {
21036b9ac4f2SChris Cain         messages::internalError(asyncResp->res);
21046b9ac4f2SChris Cain         return;
21056b9ac4f2SChris Cain     }
21066b9ac4f2SChris Cain 
21076b9ac4f2SChris Cain     nlohmann::json::array_t modeList;
21086b9ac4f2SChris Cain     if (allowedModes == nullptr)
21096b9ac4f2SChris Cain     {
21106b9ac4f2SChris Cain         modeList.emplace_back("Static");
21116b9ac4f2SChris Cain         modeList.emplace_back("MaximumPerformance");
21126b9ac4f2SChris Cain         modeList.emplace_back("PowerSaving");
21133a2d0424SChris Cain     }
21143a2d0424SChris Cain     else
21153a2d0424SChris Cain     {
21166b9ac4f2SChris Cain         for (const auto& aMode : *allowedModes)
21176b9ac4f2SChris Cain         {
21186b9ac4f2SChris Cain             computer_system::PowerMode modeValue =
21196b9ac4f2SChris Cain                 translatePowerModeString(aMode);
21206b9ac4f2SChris Cain             if (modeValue == computer_system::PowerMode::Invalid)
21216b9ac4f2SChris Cain             {
2122ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
21236b9ac4f2SChris Cain                 continue;
21246b9ac4f2SChris Cain             }
21256b9ac4f2SChris Cain             modeList.emplace_back(modeValue);
21263a2d0424SChris Cain         }
21273a2d0424SChris Cain     }
21286b9ac4f2SChris Cain     asyncResp->res.jsonValue["PowerMode@Redfish.AllowableValues"] = modeList;
21293a2d0424SChris Cain 
21306b9ac4f2SChris Cain     BMCWEB_LOG_DEBUG("Current power mode: {}", powerMode);
21316b9ac4f2SChris Cain     const computer_system::PowerMode modeValue =
21326b9ac4f2SChris Cain         translatePowerModeString(powerMode);
21336b9ac4f2SChris Cain     if (modeValue == computer_system::PowerMode::Invalid)
21346b9ac4f2SChris Cain     {
21356b9ac4f2SChris Cain         messages::internalError(asyncResp->res);
21366b9ac4f2SChris Cain         return;
21376b9ac4f2SChris Cain     }
21386b9ac4f2SChris Cain     asyncResp->res.jsonValue["PowerMode"] = modeValue;
21396b9ac4f2SChris Cain }
21403a2d0424SChris Cain /**
21413a2d0424SChris Cain  * @brief Retrieves system power mode
21423a2d0424SChris Cain  *
2143ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
21443a2d0424SChris Cain  *
21453a2d0424SChris Cain  * @return None.
21463a2d0424SChris Cain  */
2147ac106bf6SEd Tanous inline void getPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
21483a2d0424SChris Cain {
214962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get power mode.");
21503a2d0424SChris Cain 
21513a2d0424SChris Cain     // Get Power Mode object path:
2152e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2153e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode"};
2154e99073f5SGeorge Liu     dbus::utility::getSubTree(
2155e99073f5SGeorge Liu         "/", 0, interfaces,
2156ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
2157b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
21583a2d0424SChris Cain             if (ec)
21593a2d0424SChris Cain             {
2160bd79bce8SPatrick Williams                 BMCWEB_LOG_DEBUG(
2161bd79bce8SPatrick Williams                     "DBUS response error on Power.Mode GetSubTree {}", ec);
21623a2d0424SChris Cain                 // This is an optional D-Bus object so just return if
21633a2d0424SChris Cain                 // error occurs
21643a2d0424SChris Cain                 return;
21653a2d0424SChris Cain             }
21663a2d0424SChris Cain             if (subtree.empty())
21673a2d0424SChris Cain             {
21683a2d0424SChris Cain                 // As noted above, this is an optional interface so just return
21693a2d0424SChris Cain                 // if there is no instance found
21703a2d0424SChris Cain                 return;
21713a2d0424SChris Cain             }
21723a2d0424SChris Cain             if (subtree.size() > 1)
21733a2d0424SChris Cain             {
21743a2d0424SChris Cain                 // More then one PowerMode object is not supported and is an
21753a2d0424SChris Cain                 // error
217662598e31SEd Tanous                 BMCWEB_LOG_DEBUG(
217762598e31SEd Tanous                     "Found more than 1 system D-Bus Power.Mode objects: {}",
217862598e31SEd Tanous                     subtree.size());
2179ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
21803a2d0424SChris Cain                 return;
21813a2d0424SChris Cain             }
21823a2d0424SChris Cain             if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
21833a2d0424SChris Cain             {
218462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.Mode mapper error!");
2185ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
21863a2d0424SChris Cain                 return;
21873a2d0424SChris Cain             }
21883a2d0424SChris Cain             const std::string& path = subtree[0].first;
21893a2d0424SChris Cain             const std::string& service = subtree[0].second.begin()->first;
21903a2d0424SChris Cain             if (service.empty())
21913a2d0424SChris Cain             {
219262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.Mode service mapper error!");
2193ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
21943a2d0424SChris Cain                 return;
21953a2d0424SChris Cain             }
21966b9ac4f2SChris Cain 
21976b9ac4f2SChris Cain             // Valid Power Mode object found, now read the mode properties
2198deae6a78SEd Tanous             dbus::utility::getAllProperties(
21991e1e598dSJonathan Doman                 *crow::connections::systemBus, service, path,
22006b9ac4f2SChris Cain                 "xyz.openbmc_project.Control.Power.Mode",
2201bd79bce8SPatrick Williams                 [asyncResp](
2202bd79bce8SPatrick Williams                     const boost::system::error_code& ec2,
22036b9ac4f2SChris Cain                     const dbus::utility::DBusPropertiesMap& properties) {
22046b9ac4f2SChris Cain                     afterGetPowerMode(asyncResp, ec2, properties);
22051e1e598dSJonathan Doman                 });
2206e99073f5SGeorge Liu         });
22073a2d0424SChris Cain }
22083a2d0424SChris Cain 
22093a2d0424SChris Cain /**
22103a2d0424SChris Cain  * @brief Validate the specified mode is valid and return the PowerMode
22113a2d0424SChris Cain  * name associated with that string
22123a2d0424SChris Cain  *
2213ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
2214b6655101SChris Cain  * @param[in] modeValue   String representing the desired PowerMode
22153a2d0424SChris Cain  *
22163a2d0424SChris Cain  * @return PowerMode value or empty string if mode is not valid
22173a2d0424SChris Cain  */
2218504af5a0SPatrick Williams inline std::string validatePowerMode(
2219504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2220b6655101SChris Cain     const nlohmann::json& modeValue)
22213a2d0424SChris Cain {
2222b6655101SChris Cain     using PowerMode = computer_system::PowerMode;
22233a2d0424SChris Cain     std::string mode;
22243a2d0424SChris Cain 
2225b6655101SChris Cain     if (modeValue == PowerMode::Static)
22263a2d0424SChris Cain     {
22273a2d0424SChris Cain         mode = "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static";
22283a2d0424SChris Cain     }
2229b6655101SChris Cain     else if (modeValue == PowerMode::MaximumPerformance)
22303a2d0424SChris Cain     {
22310fda0f12SGeorge Liu         mode =
22320fda0f12SGeorge Liu             "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance";
22333a2d0424SChris Cain     }
2234b6655101SChris Cain     else if (modeValue == PowerMode::PowerSaving)
22353a2d0424SChris Cain     {
22363a2d0424SChris Cain         mode = "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving";
22373a2d0424SChris Cain     }
2238b6655101SChris Cain     else if (modeValue == PowerMode::BalancedPerformance)
2239b6655101SChris Cain     {
2240b6655101SChris Cain         mode =
2241b6655101SChris Cain             "xyz.openbmc_project.Control.Power.Mode.PowerMode.BalancedPerformance";
2242b6655101SChris Cain     }
2243b6655101SChris Cain     else if (modeValue == PowerMode::EfficiencyFavorPerformance)
2244b6655101SChris Cain     {
2245b6655101SChris Cain         mode =
2246b6655101SChris Cain             "xyz.openbmc_project.Control.Power.Mode.PowerMode.EfficiencyFavorPerformance";
2247b6655101SChris Cain     }
2248b6655101SChris Cain     else if (modeValue == PowerMode::EfficiencyFavorPower)
2249b6655101SChris Cain     {
2250b6655101SChris Cain         mode =
2251b6655101SChris Cain             "xyz.openbmc_project.Control.Power.Mode.PowerMode.EfficiencyFavorPower";
2252b6655101SChris Cain     }
22533a2d0424SChris Cain     else
22543a2d0424SChris Cain     {
2255b6655101SChris Cain         messages::propertyValueNotInList(asyncResp->res, modeValue.dump(),
2256ac106bf6SEd Tanous                                          "PowerMode");
22573a2d0424SChris Cain     }
22583a2d0424SChris Cain     return mode;
22593a2d0424SChris Cain }
22603a2d0424SChris Cain 
22613a2d0424SChris Cain /**
22623a2d0424SChris Cain  * @brief Sets system power mode.
22633a2d0424SChris Cain  *
2264ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
22653a2d0424SChris Cain  * @param[in] pmode   System power mode from request.
22663a2d0424SChris Cain  *
22673a2d0424SChris Cain  * @return None.
22683a2d0424SChris Cain  */
2269ac106bf6SEd Tanous inline void setPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22703a2d0424SChris Cain                          const std::string& pmode)
22713a2d0424SChris Cain {
227262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set power mode.");
22733a2d0424SChris Cain 
2274ac106bf6SEd Tanous     std::string powerMode = validatePowerMode(asyncResp, pmode);
22753a2d0424SChris Cain     if (powerMode.empty())
22763a2d0424SChris Cain     {
22773a2d0424SChris Cain         return;
22783a2d0424SChris Cain     }
22793a2d0424SChris Cain 
22803a2d0424SChris Cain     // Get Power Mode object path:
2281e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2282e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode"};
2283e99073f5SGeorge Liu     dbus::utility::getSubTree(
2284e99073f5SGeorge Liu         "/", 0, interfaces,
2285ac106bf6SEd Tanous         [asyncResp,
2286e99073f5SGeorge Liu          powerMode](const boost::system::error_code& ec,
2287b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
22883a2d0424SChris Cain             if (ec)
22893a2d0424SChris Cain             {
2290bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR(
2291bd79bce8SPatrick Williams                     "DBUS response error on Power.Mode GetSubTree {}", ec);
22923a2d0424SChris Cain                 // This is an optional D-Bus object, but user attempted to patch
2293ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
22943a2d0424SChris Cain                 return;
22953a2d0424SChris Cain             }
22963a2d0424SChris Cain             if (subtree.empty())
22973a2d0424SChris Cain             {
22983a2d0424SChris Cain                 // This is an optional D-Bus object, but user attempted to patch
2299ac106bf6SEd Tanous                 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
23003a2d0424SChris Cain                                            "PowerMode");
23013a2d0424SChris Cain                 return;
23023a2d0424SChris Cain             }
23033a2d0424SChris Cain             if (subtree.size() > 1)
23043a2d0424SChris Cain             {
23053a2d0424SChris Cain                 // More then one PowerMode object is not supported and is an
23063a2d0424SChris Cain                 // error
230762598e31SEd Tanous                 BMCWEB_LOG_DEBUG(
230862598e31SEd Tanous                     "Found more than 1 system D-Bus Power.Mode objects: {}",
230962598e31SEd Tanous                     subtree.size());
2310ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
23113a2d0424SChris Cain                 return;
23123a2d0424SChris Cain             }
23133a2d0424SChris Cain             if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
23143a2d0424SChris Cain             {
231562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.Mode mapper error!");
2316ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
23173a2d0424SChris Cain                 return;
23183a2d0424SChris Cain             }
23193a2d0424SChris Cain             const std::string& path = subtree[0].first;
23203a2d0424SChris Cain             const std::string& service = subtree[0].second.begin()->first;
23213a2d0424SChris Cain             if (service.empty())
23223a2d0424SChris Cain             {
232362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.Mode service mapper error!");
2324ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
23253a2d0424SChris Cain                 return;
23263a2d0424SChris Cain             }
23273a2d0424SChris Cain 
232862598e31SEd Tanous             BMCWEB_LOG_DEBUG("Setting power mode({}) -> {}", powerMode, path);
23293a2d0424SChris Cain 
23303a2d0424SChris Cain             // Set the Power Mode property
2331e93abac6SGinu George             setDbusProperty(asyncResp, "PowerMode", service, path,
2332bd79bce8SPatrick Williams                             "xyz.openbmc_project.Control.Power.Mode",
2333bd79bce8SPatrick Williams                             "PowerMode", powerMode);
2334e99073f5SGeorge Liu         });
23353a2d0424SChris Cain }
23363a2d0424SChris Cain 
23373a2d0424SChris Cain /**
233851709ffdSYong Li  * @brief Translates watchdog timeout action DBUS property value to redfish.
233951709ffdSYong Li  *
234051709ffdSYong Li  * @param[in] dbusAction    The watchdog timeout action in D-BUS.
234151709ffdSYong Li  *
234251709ffdSYong Li  * @return Returns as a string, the timeout action in Redfish terms. If
234351709ffdSYong Li  * translation cannot be done, returns an empty string.
234451709ffdSYong Li  */
234523a21a1cSEd Tanous inline std::string dbusToRfWatchdogAction(const std::string& dbusAction)
234651709ffdSYong Li {
234751709ffdSYong Li     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.None")
234851709ffdSYong Li     {
234951709ffdSYong Li         return "None";
235051709ffdSYong Li     }
23513174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.HardReset")
235251709ffdSYong Li     {
235351709ffdSYong Li         return "ResetSystem";
235451709ffdSYong Li     }
23553174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerOff")
235651709ffdSYong Li     {
235751709ffdSYong Li         return "PowerDown";
235851709ffdSYong Li     }
23593174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerCycle")
236051709ffdSYong Li     {
236151709ffdSYong Li         return "PowerCycle";
236251709ffdSYong Li     }
236351709ffdSYong Li 
236451709ffdSYong Li     return "";
236551709ffdSYong Li }
236651709ffdSYong Li 
236751709ffdSYong Li /**
2368c45f0082SYong Li  *@brief Translates timeout action from Redfish to DBUS property value.
2369c45f0082SYong Li  *
2370c45f0082SYong Li  *@param[in] rfAction The timeout action in Redfish.
2371c45f0082SYong Li  *
2372c45f0082SYong Li  *@return Returns as a string, the time_out action as expected by DBUS.
2373c45f0082SYong Li  *If translation cannot be done, returns an empty string.
2374c45f0082SYong Li  */
2375c45f0082SYong Li 
237623a21a1cSEd Tanous inline std::string rfToDbusWDTTimeOutAct(const std::string& rfAction)
2377c45f0082SYong Li {
2378c45f0082SYong Li     if (rfAction == "None")
2379c45f0082SYong Li     {
2380c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.None";
2381c45f0082SYong Li     }
23823174e4dfSEd Tanous     if (rfAction == "PowerCycle")
2383c45f0082SYong Li     {
2384c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerCycle";
2385c45f0082SYong Li     }
23863174e4dfSEd Tanous     if (rfAction == "PowerDown")
2387c45f0082SYong Li     {
2388c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerOff";
2389c45f0082SYong Li     }
23903174e4dfSEd Tanous     if (rfAction == "ResetSystem")
2391c45f0082SYong Li     {
2392c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.HardReset";
2393c45f0082SYong Li     }
2394c45f0082SYong Li 
2395c45f0082SYong Li     return "";
2396c45f0082SYong Li }
2397c45f0082SYong Li 
2398c45f0082SYong Li /**
239951709ffdSYong Li  * @brief Retrieves host watchdog timer properties over DBUS
240051709ffdSYong Li  *
2401ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
240251709ffdSYong Li  *
240351709ffdSYong Li  * @return None.
240451709ffdSYong Li  */
2405504af5a0SPatrick Williams inline void getHostWatchdogTimer(
2406504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
240751709ffdSYong Li {
240862598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get host watchodg");
2409deae6a78SEd Tanous     dbus::utility::getAllProperties(
2410deae6a78SEd Tanous         "xyz.openbmc_project.Watchdog", "/xyz/openbmc_project/watchdog/host0",
2411bc1d29deSKrzysztof Grobelny         "xyz.openbmc_project.State.Watchdog",
2412ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
2413b9d36b47SEd Tanous                     const dbus::utility::DBusPropertiesMap& properties) {
241451709ffdSYong Li             if (ec)
241551709ffdSYong Li             {
241651709ffdSYong Li                 // watchdog service is stopped
241762598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
241851709ffdSYong Li                 return;
241951709ffdSYong Li             }
242051709ffdSYong Li 
242162598e31SEd Tanous             BMCWEB_LOG_DEBUG("Got {} wdt prop.", properties.size());
242251709ffdSYong Li 
242351709ffdSYong Li             nlohmann::json& hostWatchdogTimer =
2424ac106bf6SEd Tanous                 asyncResp->res.jsonValue["HostWatchdogTimer"];
242551709ffdSYong Li 
242651709ffdSYong Li             // watchdog service is running/enabled
2427539d8c6bSEd Tanous             hostWatchdogTimer["Status"]["State"] = resource::State::Enabled;
242851709ffdSYong Li 
2429bc1d29deSKrzysztof Grobelny             const bool* enabled = nullptr;
2430bc1d29deSKrzysztof Grobelny             const std::string* expireAction = nullptr;
243151709ffdSYong Li 
2432bc1d29deSKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
2433bd79bce8SPatrick Williams                 dbus_utils::UnpackErrorPrinter(), properties, "Enabled",
2434bd79bce8SPatrick Williams                 enabled, "ExpireAction", expireAction);
2435bc1d29deSKrzysztof Grobelny 
2436bc1d29deSKrzysztof Grobelny             if (!success)
243751709ffdSYong Li             {
2438ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
2439601af5edSChicago Duan                 return;
244051709ffdSYong Li             }
244151709ffdSYong Li 
2442bc1d29deSKrzysztof Grobelny             if (enabled != nullptr)
244351709ffdSYong Li             {
2444bc1d29deSKrzysztof Grobelny                 hostWatchdogTimer["FunctionEnabled"] = *enabled;
244551709ffdSYong Li             }
244651709ffdSYong Li 
2447bc1d29deSKrzysztof Grobelny             if (expireAction != nullptr)
2448bc1d29deSKrzysztof Grobelny             {
2449bc1d29deSKrzysztof Grobelny                 std::string action = dbusToRfWatchdogAction(*expireAction);
245051709ffdSYong Li                 if (action.empty())
245151709ffdSYong Li                 {
2452ac106bf6SEd Tanous                     messages::internalError(asyncResp->res);
2453601af5edSChicago Duan                     return;
245451709ffdSYong Li                 }
245551709ffdSYong Li                 hostWatchdogTimer["TimeoutAction"] = action;
245651709ffdSYong Li             }
2457bc1d29deSKrzysztof Grobelny         });
245851709ffdSYong Li }
245951709ffdSYong Li 
246051709ffdSYong Li /**
2461c45f0082SYong Li  * @brief Sets Host WatchDog Timer properties.
2462c45f0082SYong Li  *
2463ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
2464c45f0082SYong Li  * @param[in] wdtEnable  The WDTimer Enable value (true/false) from incoming
2465c45f0082SYong Li  *                       RF request.
2466c45f0082SYong Li  * @param[in] wdtTimeOutAction The WDT Timeout action, from incoming RF request.
2467c45f0082SYong Li  *
2468c45f0082SYong Li  * @return None.
2469c45f0082SYong Li  */
2470504af5a0SPatrick Williams inline void setWDTProperties(
2471504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2472c45f0082SYong Li     const std::optional<bool> wdtEnable,
2473c45f0082SYong Li     const std::optional<std::string>& wdtTimeOutAction)
2474c45f0082SYong Li {
247562598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set host watchdog");
2476c45f0082SYong Li 
2477c45f0082SYong Li     if (wdtTimeOutAction)
2478c45f0082SYong Li     {
2479c45f0082SYong Li         std::string wdtTimeOutActStr = rfToDbusWDTTimeOutAct(*wdtTimeOutAction);
2480c45f0082SYong Li         // check if TimeOut Action is Valid
2481c45f0082SYong Li         if (wdtTimeOutActStr.empty())
2482c45f0082SYong Li         {
248362598e31SEd Tanous             BMCWEB_LOG_DEBUG("Unsupported value for TimeoutAction: {}",
248462598e31SEd Tanous                              *wdtTimeOutAction);
2485ac106bf6SEd Tanous             messages::propertyValueNotInList(asyncResp->res, *wdtTimeOutAction,
2486c45f0082SYong Li                                              "TimeoutAction");
2487c45f0082SYong Li             return;
2488c45f0082SYong Li         }
2489c45f0082SYong Li 
2490e93abac6SGinu George         setDbusProperty(asyncResp, "HostWatchdogTimer/TimeoutAction",
2491e93abac6SGinu George                         "xyz.openbmc_project.Watchdog",
249287c44966SAsmitha Karunanithi                         sdbusplus::message::object_path(
249387c44966SAsmitha Karunanithi                             "/xyz/openbmc_project/watchdog/host0"),
24949ae226faSGeorge Liu                         "xyz.openbmc_project.State.Watchdog", "ExpireAction",
2495e93abac6SGinu George                         wdtTimeOutActStr);
2496c45f0082SYong Li     }
2497c45f0082SYong Li 
2498c45f0082SYong Li     if (wdtEnable)
2499c45f0082SYong Li     {
2500e93abac6SGinu George         setDbusProperty(asyncResp, "HostWatchdogTimer/FunctionEnabled",
2501e93abac6SGinu George                         "xyz.openbmc_project.Watchdog",
250287c44966SAsmitha Karunanithi                         sdbusplus::message::object_path(
250387c44966SAsmitha Karunanithi                             "/xyz/openbmc_project/watchdog/host0"),
250487c44966SAsmitha Karunanithi                         "xyz.openbmc_project.State.Watchdog", "Enabled",
2505e93abac6SGinu George                         *wdtEnable);
2506c45f0082SYong Li     }
2507c45f0082SYong Li }
2508c45f0082SYong Li 
250937bbf98cSChris Cain /**
251037bbf98cSChris Cain  * @brief Parse the Idle Power Saver properties into json
251137bbf98cSChris Cain  *
2512ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for completing asynchronous calls.
251337bbf98cSChris Cain  * @param[in] properties  IPS property data from DBus.
251437bbf98cSChris Cain  *
251537bbf98cSChris Cain  * @return true if successful
251637bbf98cSChris Cain  */
2517504af5a0SPatrick Williams inline bool parseIpsProperties(
2518504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25191e5b7c88SJiaqing Zhao     const dbus::utility::DBusPropertiesMap& properties)
252037bbf98cSChris Cain {
2521bc1d29deSKrzysztof Grobelny     const bool* enabled = nullptr;
2522bc1d29deSKrzysztof Grobelny     const uint8_t* enterUtilizationPercent = nullptr;
2523bc1d29deSKrzysztof Grobelny     const uint64_t* enterDwellTime = nullptr;
2524bc1d29deSKrzysztof Grobelny     const uint8_t* exitUtilizationPercent = nullptr;
2525bc1d29deSKrzysztof Grobelny     const uint64_t* exitDwellTime = nullptr;
2526bc1d29deSKrzysztof Grobelny 
2527bc1d29deSKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
2528bc1d29deSKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), properties, "Enabled", enabled,
25292661b72cSChris Cain         "EnterUtilizationPercent", enterUtilizationPercent, "EnterDwellTime",
25302661b72cSChris Cain         enterDwellTime, "ExitUtilizationPercent", exitUtilizationPercent,
25312661b72cSChris Cain         "ExitDwellTime", exitDwellTime);
2532bc1d29deSKrzysztof Grobelny 
2533bc1d29deSKrzysztof Grobelny     if (!success)
253437bbf98cSChris Cain     {
253537bbf98cSChris Cain         return false;
253637bbf98cSChris Cain     }
2537bc1d29deSKrzysztof Grobelny 
2538bc1d29deSKrzysztof Grobelny     if (enabled != nullptr)
253937bbf98cSChris Cain     {
2540ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["Enabled"] = *enabled;
254137bbf98cSChris Cain     }
2542bc1d29deSKrzysztof Grobelny 
2543bc1d29deSKrzysztof Grobelny     if (enterUtilizationPercent != nullptr)
254437bbf98cSChris Cain     {
2545ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["EnterUtilizationPercent"] =
2546bc1d29deSKrzysztof Grobelny             *enterUtilizationPercent;
254737bbf98cSChris Cain     }
2548bc1d29deSKrzysztof Grobelny 
2549bc1d29deSKrzysztof Grobelny     if (enterDwellTime != nullptr)
2550bc1d29deSKrzysztof Grobelny     {
2551bc1d29deSKrzysztof Grobelny         const std::chrono::duration<uint64_t, std::milli> ms(*enterDwellTime);
2552ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["EnterDwellTimeSeconds"] =
255337bbf98cSChris Cain             std::chrono::duration_cast<std::chrono::duration<uint64_t>>(ms)
255437bbf98cSChris Cain                 .count();
255537bbf98cSChris Cain     }
2556bc1d29deSKrzysztof Grobelny 
2557bc1d29deSKrzysztof Grobelny     if (exitUtilizationPercent != nullptr)
255837bbf98cSChris Cain     {
2559ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["ExitUtilizationPercent"] =
2560bc1d29deSKrzysztof Grobelny             *exitUtilizationPercent;
256137bbf98cSChris Cain     }
2562bc1d29deSKrzysztof Grobelny 
2563bc1d29deSKrzysztof Grobelny     if (exitDwellTime != nullptr)
256437bbf98cSChris Cain     {
2565bc1d29deSKrzysztof Grobelny         const std::chrono::duration<uint64_t, std::milli> ms(*exitDwellTime);
2566ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["ExitDwellTimeSeconds"] =
256737bbf98cSChris Cain             std::chrono::duration_cast<std::chrono::duration<uint64_t>>(ms)
256837bbf98cSChris Cain                 .count();
256937bbf98cSChris Cain     }
257037bbf98cSChris Cain 
257137bbf98cSChris Cain     return true;
257237bbf98cSChris Cain }
257337bbf98cSChris Cain 
257437bbf98cSChris Cain /**
257537bbf98cSChris Cain  * @brief Retrieves host watchdog timer properties over DBUS
257637bbf98cSChris Cain  *
2577ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
257837bbf98cSChris Cain  *
257937bbf98cSChris Cain  * @return None.
258037bbf98cSChris Cain  */
2581504af5a0SPatrick Williams inline void getIdlePowerSaver(
2582504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
258337bbf98cSChris Cain {
258462598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get idle power saver parameters");
258537bbf98cSChris Cain 
258637bbf98cSChris Cain     // Get IdlePowerSaver object path:
2587e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2588e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.IdlePowerSaver"};
2589e99073f5SGeorge Liu     dbus::utility::getSubTree(
2590e99073f5SGeorge Liu         "/", 0, interfaces,
2591ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
2592b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
259337bbf98cSChris Cain             if (ec)
259437bbf98cSChris Cain             {
2595b3e86cb0SGunnar Mills                 BMCWEB_LOG_ERROR(
259662598e31SEd Tanous                     "DBUS response error on Power.IdlePowerSaver GetSubTree {}",
259762598e31SEd Tanous                     ec);
2598ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
259937bbf98cSChris Cain                 return;
260037bbf98cSChris Cain             }
260137bbf98cSChris Cain             if (subtree.empty())
260237bbf98cSChris Cain             {
260337bbf98cSChris Cain                 // This is an optional interface so just return
260437bbf98cSChris Cain                 // if there is no instance found
260562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("No instances found");
260637bbf98cSChris Cain                 return;
260737bbf98cSChris Cain             }
260837bbf98cSChris Cain             if (subtree.size() > 1)
260937bbf98cSChris Cain             {
261037bbf98cSChris Cain                 // More then one PowerIdlePowerSaver object is not supported and
261137bbf98cSChris Cain                 // is an error
261262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Found more than 1 system D-Bus "
261362598e31SEd Tanous                                  "Power.IdlePowerSaver objects: {}",
261462598e31SEd Tanous                                  subtree.size());
2615ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
261637bbf98cSChris Cain                 return;
261737bbf98cSChris Cain             }
261837bbf98cSChris Cain             if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
261937bbf98cSChris Cain             {
262062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.IdlePowerSaver mapper error!");
2621ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
262237bbf98cSChris Cain                 return;
262337bbf98cSChris Cain             }
262437bbf98cSChris Cain             const std::string& path = subtree[0].first;
262537bbf98cSChris Cain             const std::string& service = subtree[0].second.begin()->first;
262637bbf98cSChris Cain             if (service.empty())
262737bbf98cSChris Cain             {
262862598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.IdlePowerSaver service mapper error!");
2629ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
263037bbf98cSChris Cain                 return;
263137bbf98cSChris Cain             }
263237bbf98cSChris Cain 
263337bbf98cSChris Cain             // Valid IdlePowerSaver object found, now read the current values
2634deae6a78SEd Tanous             dbus::utility::getAllProperties(
2635bc1d29deSKrzysztof Grobelny                 *crow::connections::systemBus, service, path,
2636bc1d29deSKrzysztof Grobelny                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2637bd79bce8SPatrick Williams                 [asyncResp](
2638bd79bce8SPatrick Williams                     const boost::system::error_code& ec2,
26391e5b7c88SJiaqing Zhao                     const dbus::utility::DBusPropertiesMap& properties) {
26408a592810SEd Tanous                     if (ec2)
264137bbf98cSChris Cain                     {
264262598e31SEd Tanous                         BMCWEB_LOG_ERROR(
2643bd79bce8SPatrick Williams                             "DBUS response error on IdlePowerSaver GetAll: {}",
2644bd79bce8SPatrick Williams                             ec2);
2645ac106bf6SEd Tanous                         messages::internalError(asyncResp->res);
264637bbf98cSChris Cain                         return;
264737bbf98cSChris Cain                     }
264837bbf98cSChris Cain 
2649ac106bf6SEd Tanous                     if (!parseIpsProperties(asyncResp, properties))
265037bbf98cSChris Cain                     {
2651ac106bf6SEd Tanous                         messages::internalError(asyncResp->res);
265237bbf98cSChris Cain                         return;
265337bbf98cSChris Cain                     }
2654bc1d29deSKrzysztof Grobelny                 });
2655e99073f5SGeorge Liu         });
265637bbf98cSChris Cain 
265762598e31SEd Tanous     BMCWEB_LOG_DEBUG("EXIT: Get idle power saver parameters");
265837bbf98cSChris Cain }
265937bbf98cSChris Cain 
266037bbf98cSChris Cain /**
266137bbf98cSChris Cain  * @brief Sets Idle Power Saver properties.
266237bbf98cSChris Cain  *
2663ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
266437bbf98cSChris Cain  * @param[in] ipsEnable  The IPS Enable value (true/false) from incoming
266537bbf98cSChris Cain  *                       RF request.
266637bbf98cSChris Cain  * @param[in] ipsEnterUtil The utilization limit to enter idle state.
266737bbf98cSChris Cain  * @param[in] ipsEnterTime The time the utilization must be below ipsEnterUtil
266837bbf98cSChris Cain  * before entering idle state.
266937bbf98cSChris Cain  * @param[in] ipsExitUtil The utilization limit when exiting idle state.
267037bbf98cSChris Cain  * @param[in] ipsExitTime The time the utilization must be above ipsExutUtil
267137bbf98cSChris Cain  * before exiting idle state
267237bbf98cSChris Cain  *
267337bbf98cSChris Cain  * @return None.
267437bbf98cSChris Cain  */
2675bd79bce8SPatrick Williams inline void setIdlePowerSaver(
2676bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
267737bbf98cSChris Cain     const std::optional<bool> ipsEnable,
267837bbf98cSChris Cain     const std::optional<uint8_t> ipsEnterUtil,
267937bbf98cSChris Cain     const std::optional<uint64_t> ipsEnterTime,
268037bbf98cSChris Cain     const std::optional<uint8_t> ipsExitUtil,
268137bbf98cSChris Cain     const std::optional<uint64_t> ipsExitTime)
268237bbf98cSChris Cain {
268362598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set idle power saver properties");
268437bbf98cSChris Cain 
268537bbf98cSChris Cain     // Get IdlePowerSaver object path:
2686e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2687e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.IdlePowerSaver"};
2688e99073f5SGeorge Liu     dbus::utility::getSubTree(
2689e99073f5SGeorge Liu         "/", 0, interfaces,
2690ac106bf6SEd Tanous         [asyncResp, ipsEnable, ipsEnterUtil, ipsEnterTime, ipsExitUtil,
2691e99073f5SGeorge Liu          ipsExitTime](const boost::system::error_code& ec,
2692b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
269337bbf98cSChris Cain             if (ec)
269437bbf98cSChris Cain             {
2695b3e86cb0SGunnar Mills                 BMCWEB_LOG_ERROR(
269662598e31SEd Tanous                     "DBUS response error on Power.IdlePowerSaver GetSubTree {}",
269762598e31SEd Tanous                     ec);
2698ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
269937bbf98cSChris Cain                 return;
270037bbf98cSChris Cain             }
270137bbf98cSChris Cain             if (subtree.empty())
270237bbf98cSChris Cain             {
270337bbf98cSChris Cain                 // This is an optional D-Bus object, but user attempted to patch
2704ac106bf6SEd Tanous                 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
270537bbf98cSChris Cain                                            "IdlePowerSaver");
270637bbf98cSChris Cain                 return;
270737bbf98cSChris Cain             }
270837bbf98cSChris Cain             if (subtree.size() > 1)
270937bbf98cSChris Cain             {
271037bbf98cSChris Cain                 // More then one PowerIdlePowerSaver object is not supported and
271137bbf98cSChris Cain                 // is an error
271262598e31SEd Tanous                 BMCWEB_LOG_DEBUG(
271362598e31SEd Tanous                     "Found more than 1 system D-Bus Power.IdlePowerSaver objects: {}",
271462598e31SEd Tanous                     subtree.size());
2715ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
271637bbf98cSChris Cain                 return;
271737bbf98cSChris Cain             }
271837bbf98cSChris Cain             if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
271937bbf98cSChris Cain             {
272062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.IdlePowerSaver mapper error!");
2721ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
272237bbf98cSChris Cain                 return;
272337bbf98cSChris Cain             }
272437bbf98cSChris Cain             const std::string& path = subtree[0].first;
272537bbf98cSChris Cain             const std::string& service = subtree[0].second.begin()->first;
272637bbf98cSChris Cain             if (service.empty())
272737bbf98cSChris Cain             {
272862598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.IdlePowerSaver service mapper error!");
2729ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
273037bbf98cSChris Cain                 return;
273137bbf98cSChris Cain             }
273237bbf98cSChris Cain 
273337bbf98cSChris Cain             // Valid Power IdlePowerSaver object found, now set any values that
273437bbf98cSChris Cain             // need to be updated
273537bbf98cSChris Cain 
273637bbf98cSChris Cain             if (ipsEnable)
273737bbf98cSChris Cain             {
2738bd79bce8SPatrick Williams                 setDbusProperty(
2739bd79bce8SPatrick Williams                     asyncResp, "IdlePowerSaver/Enabled", service, path,
274087c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2741e93abac6SGinu George                     "Enabled", *ipsEnable);
274237bbf98cSChris Cain             }
274337bbf98cSChris Cain             if (ipsEnterUtil)
274437bbf98cSChris Cain             {
2745bd79bce8SPatrick Williams                 setDbusProperty(
2746bd79bce8SPatrick Williams                     asyncResp, "IdlePowerSaver/EnterUtilizationPercent",
2747e93abac6SGinu George                     service, path,
27489ae226faSGeorge Liu                     "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2749e93abac6SGinu George                     "EnterUtilizationPercent", *ipsEnterUtil);
275037bbf98cSChris Cain             }
275137bbf98cSChris Cain             if (ipsEnterTime)
275237bbf98cSChris Cain             {
275337bbf98cSChris Cain                 // Convert from seconds into milliseconds for DBus
275437bbf98cSChris Cain                 const uint64_t timeMilliseconds = *ipsEnterTime * 1000;
2755bd79bce8SPatrick Williams                 setDbusProperty(
2756bd79bce8SPatrick Williams                     asyncResp, "IdlePowerSaver/EnterDwellTimeSeconds", service,
2757bd79bce8SPatrick Williams                     path, "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2758e93abac6SGinu George                     "EnterDwellTime", timeMilliseconds);
275937bbf98cSChris Cain             }
276037bbf98cSChris Cain             if (ipsExitUtil)
276137bbf98cSChris Cain             {
2762bd79bce8SPatrick Williams                 setDbusProperty(
2763bd79bce8SPatrick Williams                     asyncResp, "IdlePowerSaver/ExitUtilizationPercent", service,
2764bd79bce8SPatrick Williams                     path, "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2765e93abac6SGinu George                     "ExitUtilizationPercent", *ipsExitUtil);
276637bbf98cSChris Cain             }
276737bbf98cSChris Cain             if (ipsExitTime)
276837bbf98cSChris Cain             {
276937bbf98cSChris Cain                 // Convert from seconds into milliseconds for DBus
277037bbf98cSChris Cain                 const uint64_t timeMilliseconds = *ipsExitTime * 1000;
2771bd79bce8SPatrick Williams                 setDbusProperty(
2772bd79bce8SPatrick Williams                     asyncResp, "IdlePowerSaver/ExitDwellTimeSeconds", service,
2773bd79bce8SPatrick Williams                     path, "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2774e93abac6SGinu George                     "ExitDwellTime", timeMilliseconds);
277537bbf98cSChris Cain             }
2776e99073f5SGeorge Liu         });
277737bbf98cSChris Cain 
277862598e31SEd Tanous     BMCWEB_LOG_DEBUG("EXIT: Set idle power saver parameters");
277937bbf98cSChris Cain }
278037bbf98cSChris Cain 
2781c1e219d5SEd Tanous inline void handleComputerSystemCollectionHead(
2782dd60b9edSEd Tanous     crow::App& app, const crow::Request& req,
2783dd60b9edSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2784dd60b9edSEd Tanous {
2785dd60b9edSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2786dd60b9edSEd Tanous     {
2787dd60b9edSEd Tanous         return;
2788dd60b9edSEd Tanous     }
2789dd60b9edSEd Tanous     asyncResp->res.addHeader(
2790dd60b9edSEd Tanous         boost::beast::http::field::link,
2791dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystemCollection/ComputerSystemCollection.json>; rel=describedby");
2792dd60b9edSEd Tanous }
2793dd60b9edSEd Tanous 
2794c1e219d5SEd Tanous inline void handleComputerSystemCollectionGet(
2795c1e219d5SEd Tanous     crow::App& app, const crow::Request& req,
2796c1e219d5SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
27971abe55efSEd Tanous {
27983ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2799f4c99e70SEd Tanous     {
2800f4c99e70SEd Tanous         return;
2801f4c99e70SEd Tanous     }
2802dd60b9edSEd Tanous 
2803dd60b9edSEd Tanous     asyncResp->res.addHeader(
2804dd60b9edSEd Tanous         boost::beast::http::field::link,
2805dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystemCollection.json>; rel=describedby");
28068d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
28070f74e643SEd Tanous         "#ComputerSystemCollection.ComputerSystemCollection";
28088d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Systems";
28098d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Computer System Collection";
2810462023adSSunitha Harish 
2811*fc5ae94dSOliver Brewka     getSystemCollectionMembers(asyncResp);
2812c1e219d5SEd Tanous }
2813c1e219d5SEd Tanous 
2814c1e219d5SEd Tanous /**
28157e860f15SJohn Edward Broadbent  * Function transceives data with dbus directly.
28167e860f15SJohn Edward Broadbent  */
28174f48d5f6SEd Tanous inline void doNMI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
28187e860f15SJohn Edward Broadbent {
281989492a15SPatrick Williams     constexpr const char* serviceName = "xyz.openbmc_project.Control.Host.NMI";
282089492a15SPatrick Williams     constexpr const char* objectPath = "/xyz/openbmc_project/control/host0/nmi";
282189492a15SPatrick Williams     constexpr const char* interfaceName =
28227e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Control.Host.NMI";
282389492a15SPatrick Williams     constexpr const char* method = "NMI";
28247e860f15SJohn Edward Broadbent 
2825177612aaSEd Tanous     dbus::utility::async_method_call(
2826177612aaSEd Tanous         asyncResp,
28275e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
28287e860f15SJohn Edward Broadbent             if (ec)
28297e860f15SJohn Edward Broadbent             {
283062598e31SEd Tanous                 BMCWEB_LOG_ERROR(" Bad D-Bus request error: {}", ec);
28317e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
28327e860f15SJohn Edward Broadbent                 return;
28337e860f15SJohn Edward Broadbent             }
28347e860f15SJohn Edward Broadbent             messages::success(asyncResp->res);
28357e860f15SJohn Edward Broadbent         },
28367e860f15SJohn Edward Broadbent         serviceName, objectPath, interfaceName, method);
28377e860f15SJohn Edward Broadbent }
2838c5b2abe0SLewanczyk, Dawid 
2839c1e219d5SEd Tanous inline void handleComputerSystemResetActionPost(
2840c1e219d5SEd Tanous     crow::App& app, const crow::Request& req,
28417f3e84a1SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2842c1e219d5SEd Tanous     const std::string& systemName)
2843c1e219d5SEd Tanous {
28443ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
284545ca1b86SEd Tanous     {
284645ca1b86SEd Tanous         return;
284745ca1b86SEd Tanous     }
2848dd7090e6SGunnar Mills 
2849dd7090e6SGunnar Mills     if constexpr (BMCWEB_HYPERVISOR_COMPUTER_SYSTEM)
2850dd7090e6SGunnar Mills     {
2851dd7090e6SGunnar Mills         if (systemName == "hypervisor")
2852dd7090e6SGunnar Mills         {
2853dd7090e6SGunnar Mills             handleHypervisorSystemResetPost(req, asyncResp);
2854dd7090e6SGunnar Mills             return;
2855dd7090e6SGunnar Mills         }
2856dd7090e6SGunnar Mills     }
2857dd7090e6SGunnar Mills 
2858253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
2859c1e219d5SEd Tanous     {
2860c1e219d5SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2861c1e219d5SEd Tanous                                    systemName);
2862c1e219d5SEd Tanous         return;
2863c1e219d5SEd Tanous     }
286425b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
28657f3e84a1SEd Tanous     {
28667f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
28677f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2868c1e219d5SEd Tanous                                    systemName);
28697f3e84a1SEd Tanous         return;
28707f3e84a1SEd Tanous     }
28719712f8acSEd Tanous     std::string resetType;
2872c1e219d5SEd Tanous     if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
2873cc340dd9SEd Tanous     {
2874cc340dd9SEd Tanous         return;
2875cc340dd9SEd Tanous     }
2876cc340dd9SEd Tanous 
2877d22c8396SJason M. Bills     // Get the command and host vs. chassis
2878cc340dd9SEd Tanous     std::string command;
2879543f4400SEd Tanous     bool hostCommand = true;
2880d4d25793SEd Tanous     if ((resetType == "On") || (resetType == "ForceOn"))
2881cc340dd9SEd Tanous     {
2882cc340dd9SEd Tanous         command = "xyz.openbmc_project.State.Host.Transition.On";
2883d22c8396SJason M. Bills         hostCommand = true;
2884d22c8396SJason M. Bills     }
2885d22c8396SJason M. Bills     else if (resetType == "ForceOff")
2886d22c8396SJason M. Bills     {
2887d22c8396SJason M. Bills         command = "xyz.openbmc_project.State.Chassis.Transition.Off";
2888d22c8396SJason M. Bills         hostCommand = false;
2889d22c8396SJason M. Bills     }
2890d22c8396SJason M. Bills     else if (resetType == "ForceRestart")
2891d22c8396SJason M. Bills     {
2892c1e219d5SEd Tanous         command = "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot";
289386a0851aSJason M. Bills         hostCommand = true;
2894cc340dd9SEd Tanous     }
28959712f8acSEd Tanous     else if (resetType == "GracefulShutdown")
2896cc340dd9SEd Tanous     {
2897cc340dd9SEd Tanous         command = "xyz.openbmc_project.State.Host.Transition.Off";
2898d22c8396SJason M. Bills         hostCommand = true;
2899cc340dd9SEd Tanous     }
29009712f8acSEd Tanous     else if (resetType == "GracefulRestart")
2901cc340dd9SEd Tanous     {
29020fda0f12SGeorge Liu         command =
29030fda0f12SGeorge Liu             "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot";
2904d22c8396SJason M. Bills         hostCommand = true;
2905d22c8396SJason M. Bills     }
2906d22c8396SJason M. Bills     else if (resetType == "PowerCycle")
2907d22c8396SJason M. Bills     {
290886a0851aSJason M. Bills         command = "xyz.openbmc_project.State.Host.Transition.Reboot";
290986a0851aSJason M. Bills         hostCommand = true;
2910cc340dd9SEd Tanous     }
2911bfd5b826SLakshminarayana R. Kammath     else if (resetType == "Nmi")
2912bfd5b826SLakshminarayana R. Kammath     {
2913bfd5b826SLakshminarayana R. Kammath         doNMI(asyncResp);
2914bfd5b826SLakshminarayana R. Kammath         return;
2915bfd5b826SLakshminarayana R. Kammath     }
2916cc340dd9SEd Tanous     else
2917cc340dd9SEd Tanous     {
2918c1e219d5SEd Tanous         messages::actionParameterUnknown(asyncResp->res, "Reset", resetType);
2919cc340dd9SEd Tanous         return;
2920cc340dd9SEd Tanous     }
2921d02aad39SEd Tanous     sdbusplus::message::object_path statePath("/xyz/openbmc_project/state");
2922cc340dd9SEd Tanous 
2923d22c8396SJason M. Bills     if (hostCommand)
2924d22c8396SJason M. Bills     {
2925e93abac6SGinu George         setDbusProperty(asyncResp, "Reset", "xyz.openbmc_project.State.Host",
2926d02aad39SEd Tanous                         statePath / "host0", "xyz.openbmc_project.State.Host",
2927e93abac6SGinu George                         "RequestedHostTransition", command);
2928cc340dd9SEd Tanous     }
2929d22c8396SJason M. Bills     else
2930d22c8396SJason M. Bills     {
2931e93abac6SGinu George         setDbusProperty(asyncResp, "Reset", "xyz.openbmc_project.State.Chassis",
2932d02aad39SEd Tanous                         statePath / "chassis0",
2933d02aad39SEd Tanous                         "xyz.openbmc_project.State.Chassis",
2934e93abac6SGinu George                         "RequestedPowerTransition", command);
2935d22c8396SJason M. Bills     }
2936d22c8396SJason M. Bills }
2937cc340dd9SEd Tanous 
2938c1e219d5SEd Tanous inline void handleComputerSystemHead(
2939dd60b9edSEd Tanous     App& app, const crow::Request& req,
29407f3e84a1SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29417f3e84a1SEd Tanous     const std::string& /*systemName*/)
2942dd60b9edSEd Tanous {
2943dd60b9edSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2944dd60b9edSEd Tanous     {
2945dd60b9edSEd Tanous         return;
2946dd60b9edSEd Tanous     }
2947dd60b9edSEd Tanous 
2948dd60b9edSEd Tanous     asyncResp->res.addHeader(
2949dd60b9edSEd Tanous         boost::beast::http::field::link,
2950dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystem/ComputerSystem.json>; rel=describedby");
2951dd60b9edSEd Tanous }
2952dd60b9edSEd Tanous 
29535c3e9272SAbhishek Patel inline void afterPortRequest(
29545c3e9272SAbhishek Patel     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29555c3e9272SAbhishek Patel     const boost::system::error_code& ec,
29565c3e9272SAbhishek Patel     const std::vector<std::tuple<std::string, std::string, bool>>& socketData)
29575c3e9272SAbhishek Patel {
29585c3e9272SAbhishek Patel     if (ec)
29595c3e9272SAbhishek Patel     {
2960b3e86cb0SGunnar Mills         BMCWEB_LOG_ERROR("DBUS response error {}", ec);
29615c3e9272SAbhishek Patel         messages::internalError(asyncResp->res);
29625c3e9272SAbhishek Patel         return;
29635c3e9272SAbhishek Patel     }
29645c3e9272SAbhishek Patel     for (const auto& data : socketData)
29655c3e9272SAbhishek Patel     {
29665c3e9272SAbhishek Patel         const std::string& socketPath = get<0>(data);
29675c3e9272SAbhishek Patel         const std::string& protocolName = get<1>(data);
29685c3e9272SAbhishek Patel         bool isProtocolEnabled = get<2>(data);
29695c3e9272SAbhishek Patel         nlohmann::json& dataJson = asyncResp->res.jsonValue["SerialConsole"];
29705c3e9272SAbhishek Patel         dataJson[protocolName]["ServiceEnabled"] = isProtocolEnabled;
29715c3e9272SAbhishek Patel         // need to retrieve port number for
29725c3e9272SAbhishek Patel         // obmc-console-ssh service
29735c3e9272SAbhishek Patel         if (protocolName == "SSH")
29745c3e9272SAbhishek Patel         {
29755c3e9272SAbhishek Patel             getPortNumber(socketPath, [asyncResp, protocolName](
297681c4e330SEd Tanous                                           const boost::system::error_code& ec1,
29775c3e9272SAbhishek Patel                                           int portNumber) {
29785c3e9272SAbhishek Patel                 if (ec1)
29795c3e9272SAbhishek Patel                 {
2980b3e86cb0SGunnar Mills                     BMCWEB_LOG_ERROR("DBUS response error {}", ec1);
29815c3e9272SAbhishek Patel                     messages::internalError(asyncResp->res);
29825c3e9272SAbhishek Patel                     return;
29835c3e9272SAbhishek Patel                 }
29845c3e9272SAbhishek Patel                 nlohmann::json& dataJson1 =
29855c3e9272SAbhishek Patel                     asyncResp->res.jsonValue["SerialConsole"];
29865c3e9272SAbhishek Patel                 dataJson1[protocolName]["Port"] = portNumber;
29875c3e9272SAbhishek Patel             });
29885c3e9272SAbhishek Patel         }
29895c3e9272SAbhishek Patel     }
29905c3e9272SAbhishek Patel }
2991c1e219d5SEd Tanous 
2992504af5a0SPatrick Williams inline void handleComputerSystemGet(
2993504af5a0SPatrick Williams     crow::App& app, const crow::Request& req,
299422d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2995c1e219d5SEd Tanous     const std::string& systemName)
2996c1e219d5SEd Tanous {
29973ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
299845ca1b86SEd Tanous     {
299945ca1b86SEd Tanous         return;
300045ca1b86SEd Tanous     }
3001746b56f3SAsmitha Karunanithi 
300225b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
30037f3e84a1SEd Tanous     {
30047f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
30057f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
30067f3e84a1SEd Tanous                                    systemName);
30077f3e84a1SEd Tanous         return;
30087f3e84a1SEd Tanous     }
30097f3e84a1SEd Tanous 
301068896206SGunnar Mills     if constexpr (BMCWEB_HYPERVISOR_COMPUTER_SYSTEM)
301168896206SGunnar Mills     {
3012746b56f3SAsmitha Karunanithi         if (systemName == "hypervisor")
3013746b56f3SAsmitha Karunanithi         {
3014746b56f3SAsmitha Karunanithi             handleHypervisorSystemGet(asyncResp);
3015746b56f3SAsmitha Karunanithi             return;
3016746b56f3SAsmitha Karunanithi         }
301768896206SGunnar Mills     }
3018746b56f3SAsmitha Karunanithi 
3019253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
302022d268cbSEd Tanous     {
302122d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
302222d268cbSEd Tanous                                    systemName);
302322d268cbSEd Tanous         return;
302422d268cbSEd Tanous     }
3025dd60b9edSEd Tanous     asyncResp->res.addHeader(
3026dd60b9edSEd Tanous         boost::beast::http::field::link,
3027dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystem/ComputerSystem.json>; rel=describedby");
30288d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
3029b6655101SChris Cain         "#ComputerSystem.v1_22_0.ComputerSystem";
3030253f11b8SEd Tanous     asyncResp->res.jsonValue["Name"] = BMCWEB_REDFISH_SYSTEM_URI_NAME;
3031253f11b8SEd Tanous     asyncResp->res.jsonValue["Id"] = BMCWEB_REDFISH_SYSTEM_URI_NAME;
3032539d8c6bSEd Tanous     asyncResp->res.jsonValue["SystemType"] =
3033539d8c6bSEd Tanous         computer_system::SystemType::Physical;
30348d1b46d7Szhanghch05     asyncResp->res.jsonValue["Description"] = "Computer System";
30358d1b46d7Szhanghch05     asyncResp->res.jsonValue["ProcessorSummary"]["Count"] = 0;
3036cf0e004cSNinad Palsule     asyncResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] =
3037dfb2b408SPriyanga Ramasamy         double(0);
3038253f11b8SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
3039253f11b8SEd Tanous         "/redfish/v1/Systems/{}", BMCWEB_REDFISH_SYSTEM_URI_NAME);
304004a258f4SEd Tanous 
3041253f11b8SEd Tanous     asyncResp->res.jsonValue["Processors"]["@odata.id"] = boost::urls::format(
3042253f11b8SEd Tanous         "/redfish/v1/Systems/{}/Processors", BMCWEB_REDFISH_SYSTEM_URI_NAME);
3043253f11b8SEd Tanous     asyncResp->res.jsonValue["Memory"]["@odata.id"] = boost::urls::format(
3044253f11b8SEd Tanous         "/redfish/v1/Systems/{}/Memory", BMCWEB_REDFISH_SYSTEM_URI_NAME);
3045253f11b8SEd Tanous     asyncResp->res.jsonValue["Storage"]["@odata.id"] = boost::urls::format(
3046253f11b8SEd Tanous         "/redfish/v1/Systems/{}/Storage", BMCWEB_REDFISH_SYSTEM_URI_NAME);
30473179105bSSunny Srivastava     asyncResp->res.jsonValue["FabricAdapters"]["@odata.id"] =
3048253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Systems/{}/FabricAdapters",
3049253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME);
3050029573d4SEd Tanous 
3051002d39b4SEd Tanous     asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"]["target"] =
3052253f11b8SEd Tanous         boost::urls::format(
3053253f11b8SEd Tanous             "/redfish/v1/Systems/{}/Actions/ComputerSystem.Reset",
3054253f11b8SEd Tanous             BMCWEB_REDFISH_SYSTEM_URI_NAME);
3055c1e219d5SEd Tanous     asyncResp->res
3056c1e219d5SEd Tanous         .jsonValue["Actions"]["#ComputerSystem.Reset"]["@Redfish.ActionInfo"] =
3057253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Systems/{}/ResetActionInfo",
3058253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME);
3059c5b2abe0SLewanczyk, Dawid 
3060253f11b8SEd Tanous     asyncResp->res.jsonValue["LogServices"]["@odata.id"] = boost::urls::format(
3061253f11b8SEd Tanous         "/redfish/v1/Systems/{}/LogServices", BMCWEB_REDFISH_SYSTEM_URI_NAME);
3062253f11b8SEd Tanous     asyncResp->res.jsonValue["Bios"]["@odata.id"] = boost::urls::format(
3063253f11b8SEd Tanous         "/redfish/v1/Systems/{}/Bios", BMCWEB_REDFISH_SYSTEM_URI_NAME);
3064c4bf6374SJason M. Bills 
30651476687dSEd Tanous     nlohmann::json::array_t managedBy;
30661476687dSEd Tanous     nlohmann::json& manager = managedBy.emplace_back();
3067253f11b8SEd Tanous     manager["@odata.id"] = boost::urls::format("/redfish/v1/Managers/{}",
3068253f11b8SEd Tanous                                                BMCWEB_REDFISH_MANAGER_URI_NAME);
3069002d39b4SEd Tanous     asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy);
3070539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
3071539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
30720e8ac5e7SGunnar Mills 
30730e8ac5e7SGunnar Mills     // Fill in SerialConsole info
3074002d39b4SEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
3075c1e219d5SEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["IPMI"]["ServiceEnabled"] = true;
30761476687dSEd Tanous 
3077c1e219d5SEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["SSH"]["ServiceEnabled"] = true;
30781476687dSEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["SSH"]["Port"] = 2200;
3079c1e219d5SEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["SSH"]["HotKeySequenceDisplay"] =
30801476687dSEd Tanous         "Press ~. to exit console";
30815c3e9272SAbhishek Patel     getPortStatusAndPath(std::span{protocolToDBusForSystems},
30825c3e9272SAbhishek Patel                          std::bind_front(afterPortRequest, asyncResp));
30830e8ac5e7SGunnar Mills 
308425b54dbaSEd Tanous     if constexpr (BMCWEB_KVM)
308525b54dbaSEd Tanous     {
30860e8ac5e7SGunnar Mills         // Fill in GraphicalConsole info
3087002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
308825b54dbaSEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
308925b54dbaSEd Tanous             4;
3090613dabeaSEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
3091613dabeaSEd Tanous             nlohmann::json::array_t({"KVMIP"});
309225b54dbaSEd Tanous     }
309313451e39SWilly Tu 
3094bd79bce8SPatrick Williams     getMainChassisId(
3095bd79bce8SPatrick Williams         asyncResp, [](const std::string& chassisId,
30968d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
3097b2c7e208SEd Tanous             nlohmann::json::array_t chassisArray;
3098b2c7e208SEd Tanous             nlohmann::json& chassis = chassisArray.emplace_back();
3099bd79bce8SPatrick Williams             chassis["@odata.id"] =
3100bd79bce8SPatrick Williams                 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
3101002d39b4SEd Tanous             aRsp->res.jsonValue["Links"]["Chassis"] = std::move(chassisArray);
3102c5d03ff4SJennifer Lee         });
3103a3002228SAppaRao Puli 
310459a17e4fSGeorge Liu     getSystemLocationIndicatorActive(asyncResp);
31059f8bfa7cSGunnar Mills     // TODO (Gunnar): Remove IndicatorLED after enough time has passed
3106a3002228SAppaRao Puli     getIndicatorLedState(asyncResp);
310751bd2d8aSGunnar Mills     getComputerSystem(asyncResp);
31086c34de48SEd Tanous     getHostState(asyncResp);
3109491d8ee7SSantosh Puranik     getBootProperties(asyncResp);
3110978b8803SAndrew Geissler     getBootProgress(asyncResp);
3111b6d5d45cSHieu Huynh     getBootProgressLastStateTime(asyncResp);
311270c4d545SLakshmi Yadlapati     pcie_util::getPCIeDeviceList(asyncResp,
311370c4d545SLakshmi Yadlapati                                  nlohmann::json::json_pointer("/PCIeDevices"));
311451709ffdSYong Li     getHostWatchdogTimer(asyncResp);
3115c6a620f2SGeorge Liu     getPowerRestorePolicy(asyncResp);
31169dcfe8c1SAlbert Zhang     getStopBootOnFault(asyncResp);
3117797d5daeSCorey Hardesty     getAutomaticRetryPolicy(asyncResp);
3118c0557e1aSGunnar Mills     getLastResetTime(asyncResp);
311925b54dbaSEd Tanous     if constexpr (BMCWEB_REDFISH_PROVISIONING_FEATURE)
312025b54dbaSEd Tanous     {
3121a6349918SAppaRao Puli         getProvisioningStatus(asyncResp);
312225b54dbaSEd Tanous     }
31231981771bSAli Ahmed     getTrustedModuleRequiredToBoot(asyncResp);
31243a2d0424SChris Cain     getPowerMode(asyncResp);
312537bbf98cSChris Cain     getIdlePowerSaver(asyncResp);
3126c1e219d5SEd Tanous }
3127550a6bf8SJiaqing Zhao 
3128c1e219d5SEd Tanous inline void handleComputerSystemPatch(
3129c1e219d5SEd Tanous     crow::App& app, const crow::Request& req,
313022d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3131c1e219d5SEd Tanous     const std::string& systemName)
3132c1e219d5SEd Tanous {
31333ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
313445ca1b86SEd Tanous     {
313545ca1b86SEd Tanous         return;
313645ca1b86SEd Tanous     }
313725b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
31387f3e84a1SEd Tanous     {
31397f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
31407f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
31417f3e84a1SEd Tanous                                    systemName);
31427f3e84a1SEd Tanous         return;
31437f3e84a1SEd Tanous     }
3144253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
314522d268cbSEd Tanous     {
314622d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
314722d268cbSEd Tanous                                    systemName);
314822d268cbSEd Tanous         return;
314922d268cbSEd Tanous     }
315022d268cbSEd Tanous 
3151dd60b9edSEd Tanous     asyncResp->res.addHeader(
3152dd60b9edSEd Tanous         boost::beast::http::field::link,
3153dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystem/ComputerSystem.json>; rel=describedby");
3154dd60b9edSEd Tanous 
31559f8bfa7cSGunnar Mills     std::optional<bool> locationIndicatorActive;
3156cde19e5fSSantosh Puranik     std::optional<std::string> indicatorLed;
315798e386ecSGunnar Mills     std::optional<std::string> assetTag;
3158c6a620f2SGeorge Liu     std::optional<std::string> powerRestorePolicy;
31593a2d0424SChris Cain     std::optional<std::string> powerMode;
3160550a6bf8SJiaqing Zhao     std::optional<bool> wdtEnable;
3161550a6bf8SJiaqing Zhao     std::optional<std::string> wdtTimeOutAction;
3162550a6bf8SJiaqing Zhao     std::optional<std::string> bootSource;
3163550a6bf8SJiaqing Zhao     std::optional<std::string> bootType;
3164550a6bf8SJiaqing Zhao     std::optional<std::string> bootEnable;
3165550a6bf8SJiaqing Zhao     std::optional<std::string> bootAutomaticRetry;
3166797d5daeSCorey Hardesty     std::optional<uint32_t> bootAutomaticRetryAttempts;
3167550a6bf8SJiaqing Zhao     std::optional<bool> bootTrustedModuleRequired;
31689dcfe8c1SAlbert Zhang     std::optional<std::string> stopBootOnFault;
3169550a6bf8SJiaqing Zhao     std::optional<bool> ipsEnable;
3170550a6bf8SJiaqing Zhao     std::optional<uint8_t> ipsEnterUtil;
3171550a6bf8SJiaqing Zhao     std::optional<uint64_t> ipsEnterTime;
3172550a6bf8SJiaqing Zhao     std::optional<uint8_t> ipsExitUtil;
3173550a6bf8SJiaqing Zhao     std::optional<uint64_t> ipsExitTime;
3174550a6bf8SJiaqing Zhao 
3175afc474aeSMyung Bae     if (!json_util::readJsonPatch(                                         //
3176afc474aeSMyung Bae             req, asyncResp->res,                                           //
3177afc474aeSMyung Bae             "AssetTag", assetTag,                                          //
3178afc474aeSMyung Bae             "Boot/AutomaticRetryAttempts", bootAutomaticRetryAttempts,     //
3179afc474aeSMyung Bae             "Boot/AutomaticRetryConfig", bootAutomaticRetry,               //
3180afc474aeSMyung Bae             "Boot/BootSourceOverrideEnabled", bootEnable,                  //
3181afc474aeSMyung Bae             "Boot/BootSourceOverrideMode", bootType,                       //
3182afc474aeSMyung Bae             "Boot/BootSourceOverrideTarget", bootSource,                   //
3183afc474aeSMyung Bae             "Boot/StopBootOnFault", stopBootOnFault,                       //
3184afc474aeSMyung Bae             "Boot/TrustedModuleRequiredToBoot", bootTrustedModuleRequired, //
3185afc474aeSMyung Bae             "HostWatchdogTimer/FunctionEnabled", wdtEnable,                //
3186afc474aeSMyung Bae             "HostWatchdogTimer/TimeoutAction", wdtTimeOutAction,           //
3187afc474aeSMyung Bae             "IdlePowerSaver/Enabled", ipsEnable,                           //
3188afc474aeSMyung Bae             "IdlePowerSaver/EnterDwellTimeSeconds", ipsEnterTime,          //
3189afc474aeSMyung Bae             "IdlePowerSaver/EnterUtilizationPercent", ipsEnterUtil,        //
3190afc474aeSMyung Bae             "IdlePowerSaver/ExitDwellTimeSeconds", ipsExitTime,            //
3191afc474aeSMyung Bae             "IdlePowerSaver/ExitUtilizationPercent", ipsExitUtil,          //
3192afc474aeSMyung Bae             "IndicatorLED", indicatorLed,                                  //
3193afc474aeSMyung Bae             "LocationIndicatorActive", locationIndicatorActive,            //
3194afc474aeSMyung Bae             "PowerMode", powerMode,                                        //
3195afc474aeSMyung Bae             "PowerRestorePolicy", powerRestorePolicy                       //
3196afc474aeSMyung Bae             ))
31976617338dSEd Tanous     {
31986617338dSEd Tanous         return;
31996617338dSEd Tanous     }
3200491d8ee7SSantosh Puranik 
32018d1b46d7Szhanghch05     asyncResp->res.result(boost::beast::http::status::no_content);
3202c45f0082SYong Li 
320398e386ecSGunnar Mills     if (assetTag)
320498e386ecSGunnar Mills     {
320598e386ecSGunnar Mills         setAssetTag(asyncResp, *assetTag);
320698e386ecSGunnar Mills     }
320798e386ecSGunnar Mills 
3208550a6bf8SJiaqing Zhao     if (wdtEnable || wdtTimeOutAction)
3209c45f0082SYong Li     {
3210f23b7296SEd Tanous         setWDTProperties(asyncResp, wdtEnable, wdtTimeOutAction);
3211c45f0082SYong Li     }
3212c45f0082SYong Li 
3213cd9a4666SKonstantin Aladyshev     if (bootSource || bootType || bootEnable)
321469f35306SGunnar Mills     {
3215002d39b4SEd Tanous         setBootProperties(asyncResp, bootSource, bootType, bootEnable);
3216491d8ee7SSantosh Puranik     }
3217550a6bf8SJiaqing Zhao     if (bootAutomaticRetry)
321869f35306SGunnar Mills     {
3219550a6bf8SJiaqing Zhao         setAutomaticRetry(asyncResp, *bootAutomaticRetry);
322069f35306SGunnar Mills     }
3221ac7e1e0bSAli Ahmed 
3222797d5daeSCorey Hardesty     if (bootAutomaticRetryAttempts)
3223797d5daeSCorey Hardesty     {
3224797d5daeSCorey Hardesty         setAutomaticRetryAttempts(asyncResp,
3225797d5daeSCorey Hardesty                                   bootAutomaticRetryAttempts.value());
3226797d5daeSCorey Hardesty     }
3227797d5daeSCorey Hardesty 
3228550a6bf8SJiaqing Zhao     if (bootTrustedModuleRequired)
3229ac7e1e0bSAli Ahmed     {
3230c1e219d5SEd Tanous         setTrustedModuleRequiredToBoot(asyncResp, *bootTrustedModuleRequired);
323169f35306SGunnar Mills     }
3232265c1602SJohnathan Mantey 
32339dcfe8c1SAlbert Zhang     if (stopBootOnFault)
32349dcfe8c1SAlbert Zhang     {
32359dcfe8c1SAlbert Zhang         setStopBootOnFault(asyncResp, *stopBootOnFault);
32369dcfe8c1SAlbert Zhang     }
32379dcfe8c1SAlbert Zhang 
32389f8bfa7cSGunnar Mills     if (locationIndicatorActive)
32399f8bfa7cSGunnar Mills     {
324059a17e4fSGeorge Liu         setSystemLocationIndicatorActive(asyncResp, *locationIndicatorActive);
32419f8bfa7cSGunnar Mills     }
32429f8bfa7cSGunnar Mills 
32437e860f15SJohn Edward Broadbent     // TODO (Gunnar): Remove IndicatorLED after enough time has
32447e860f15SJohn Edward Broadbent     // passed
32459712f8acSEd Tanous     if (indicatorLed)
32466617338dSEd Tanous     {
3247f23b7296SEd Tanous         setIndicatorLedState(asyncResp, *indicatorLed);
3248002d39b4SEd Tanous         asyncResp->res.addHeader(boost::beast::http::field::warning,
3249d6aa0093SGunnar Mills                                  "299 - \"IndicatorLED is deprecated. Use "
3250d6aa0093SGunnar Mills                                  "LocationIndicatorActive instead.\"");
32516617338dSEd Tanous     }
3252c6a620f2SGeorge Liu 
3253c6a620f2SGeorge Liu     if (powerRestorePolicy)
3254c6a620f2SGeorge Liu     {
32554e69c904SGunnar Mills         setPowerRestorePolicy(asyncResp, *powerRestorePolicy);
3256c6a620f2SGeorge Liu     }
32573a2d0424SChris Cain 
32583a2d0424SChris Cain     if (powerMode)
32593a2d0424SChris Cain     {
32603a2d0424SChris Cain         setPowerMode(asyncResp, *powerMode);
32613a2d0424SChris Cain     }
326237bbf98cSChris Cain 
3263c1e219d5SEd Tanous     if (ipsEnable || ipsEnterUtil || ipsEnterTime || ipsExitUtil || ipsExitTime)
326437bbf98cSChris Cain     {
3265002d39b4SEd Tanous         setIdlePowerSaver(asyncResp, ipsEnable, ipsEnterUtil, ipsEnterTime,
3266002d39b4SEd Tanous                           ipsExitUtil, ipsExitTime);
326737bbf98cSChris Cain     }
3268c1e219d5SEd Tanous }
32691cb1a9e6SAppaRao Puli 
327038c8a6f2SEd Tanous inline void handleSystemCollectionResetActionHead(
3271dd60b9edSEd Tanous     crow::App& app, const crow::Request& req,
32727f3e84a1SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3273c1e219d5SEd Tanous     const std::string& /*systemName*/)
3274dd60b9edSEd Tanous {
3275dd60b9edSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3276dd60b9edSEd Tanous     {
3277dd60b9edSEd Tanous         return;
3278dd60b9edSEd Tanous     }
3279dd60b9edSEd Tanous     asyncResp->res.addHeader(
3280dd60b9edSEd Tanous         boost::beast::http::field::link,
3281dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ActionInfo/ActionInfo.json>; rel=describedby");
3282dd60b9edSEd Tanous }
328333e1f122SAndrew Geissler 
328433e1f122SAndrew Geissler /**
328533e1f122SAndrew Geissler  * @brief Translates allowed host transitions to redfish string
328633e1f122SAndrew Geissler  *
328733e1f122SAndrew Geissler  * @param[in]  dbusAllowedHostTran The allowed host transition on dbus
328833e1f122SAndrew Geissler  * @param[out] allowableValues     The translated host transition(s)
328933e1f122SAndrew Geissler  *
3290efff2b5dSManojkiran Eda  * @return Emplaces corresponding Redfish translated value(s) in
329133e1f122SAndrew Geissler  * allowableValues. If translation not possible, does nothing to
329233e1f122SAndrew Geissler  * allowableValues.
329333e1f122SAndrew Geissler  */
3294504af5a0SPatrick Williams inline void dbusToRfAllowedHostTransitions(
3295504af5a0SPatrick Williams     const std::string& dbusAllowedHostTran,
329633e1f122SAndrew Geissler     nlohmann::json::array_t& allowableValues)
329733e1f122SAndrew Geissler {
329833e1f122SAndrew Geissler     if (dbusAllowedHostTran == "xyz.openbmc_project.State.Host.Transition.On")
329933e1f122SAndrew Geissler     {
330033e1f122SAndrew Geissler         allowableValues.emplace_back(resource::ResetType::On);
330133e1f122SAndrew Geissler         allowableValues.emplace_back(resource::ResetType::ForceOn);
330233e1f122SAndrew Geissler     }
330333e1f122SAndrew Geissler     else if (dbusAllowedHostTran ==
330433e1f122SAndrew Geissler              "xyz.openbmc_project.State.Host.Transition.Off")
330533e1f122SAndrew Geissler     {
330633e1f122SAndrew Geissler         allowableValues.emplace_back(resource::ResetType::GracefulShutdown);
330733e1f122SAndrew Geissler     }
330833e1f122SAndrew Geissler     else if (dbusAllowedHostTran ==
330933e1f122SAndrew Geissler              "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot")
331033e1f122SAndrew Geissler     {
331133e1f122SAndrew Geissler         allowableValues.emplace_back(resource::ResetType::GracefulRestart);
331233e1f122SAndrew Geissler     }
331333e1f122SAndrew Geissler     else if (dbusAllowedHostTran ==
331433e1f122SAndrew Geissler              "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot")
331533e1f122SAndrew Geissler     {
331633e1f122SAndrew Geissler         allowableValues.emplace_back(resource::ResetType::ForceRestart);
331733e1f122SAndrew Geissler     }
331833e1f122SAndrew Geissler     else
331933e1f122SAndrew Geissler     {
332033e1f122SAndrew Geissler         BMCWEB_LOG_WARNING("Unsupported host tran {}", dbusAllowedHostTran);
332133e1f122SAndrew Geissler     }
332233e1f122SAndrew Geissler }
332333e1f122SAndrew Geissler 
332433e1f122SAndrew Geissler inline void afterGetAllowedHostTransitions(
332533e1f122SAndrew Geissler     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
332633e1f122SAndrew Geissler     const boost::system::error_code& ec,
332733e1f122SAndrew Geissler     const std::vector<std::string>& allowedHostTransitions)
332833e1f122SAndrew Geissler {
332933e1f122SAndrew Geissler     nlohmann::json::array_t allowableValues;
333033e1f122SAndrew Geissler 
333133e1f122SAndrew Geissler     // Supported on all systems currently
333233e1f122SAndrew Geissler     allowableValues.emplace_back(resource::ResetType::ForceOff);
333333e1f122SAndrew Geissler     allowableValues.emplace_back(resource::ResetType::PowerCycle);
333433e1f122SAndrew Geissler     allowableValues.emplace_back(resource::ResetType::Nmi);
333533e1f122SAndrew Geissler 
333633e1f122SAndrew Geissler     if (ec)
333733e1f122SAndrew Geissler     {
3338e715d14bSEd Tanous         if ((ec.value() ==
3339e715d14bSEd Tanous              boost::system::linux_error::bad_request_descriptor) ||
3340e715d14bSEd Tanous             (ec.value() == boost::asio::error::basic_errors::host_unreachable))
334133e1f122SAndrew Geissler         {
334233e1f122SAndrew Geissler             // Property not implemented so just return defaults
334333e1f122SAndrew Geissler             BMCWEB_LOG_DEBUG("Property not available {}", ec);
334433e1f122SAndrew Geissler             allowableValues.emplace_back(resource::ResetType::On);
334533e1f122SAndrew Geissler             allowableValues.emplace_back(resource::ResetType::ForceOn);
334633e1f122SAndrew Geissler             allowableValues.emplace_back(resource::ResetType::ForceRestart);
334733e1f122SAndrew Geissler             allowableValues.emplace_back(resource::ResetType::GracefulRestart);
334833e1f122SAndrew Geissler             allowableValues.emplace_back(resource::ResetType::GracefulShutdown);
334933e1f122SAndrew Geissler         }
335033e1f122SAndrew Geissler         else
335133e1f122SAndrew Geissler         {
335233e1f122SAndrew Geissler             BMCWEB_LOG_ERROR("DBUS response error {}", ec);
335333e1f122SAndrew Geissler             messages::internalError(asyncResp->res);
335433e1f122SAndrew Geissler             return;
335533e1f122SAndrew Geissler         }
335633e1f122SAndrew Geissler     }
335733e1f122SAndrew Geissler     else
335833e1f122SAndrew Geissler     {
335933e1f122SAndrew Geissler         for (const std::string& transition : allowedHostTransitions)
336033e1f122SAndrew Geissler         {
336133e1f122SAndrew Geissler             BMCWEB_LOG_DEBUG("Found allowed host tran {}", transition);
336233e1f122SAndrew Geissler             dbusToRfAllowedHostTransitions(transition, allowableValues);
336333e1f122SAndrew Geissler         }
336433e1f122SAndrew Geissler     }
336533e1f122SAndrew Geissler 
336633e1f122SAndrew Geissler     nlohmann::json::object_t parameter;
336733e1f122SAndrew Geissler     parameter["Name"] = "ResetType";
336833e1f122SAndrew Geissler     parameter["Required"] = true;
3369539d8c6bSEd Tanous     parameter["DataType"] = action_info::ParameterTypes::String;
337033e1f122SAndrew Geissler     parameter["AllowableValues"] = std::move(allowableValues);
337133e1f122SAndrew Geissler     nlohmann::json::array_t parameters;
337233e1f122SAndrew Geissler     parameters.emplace_back(std::move(parameter));
337333e1f122SAndrew Geissler     asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
337433e1f122SAndrew Geissler }
337533e1f122SAndrew Geissler 
3376c1e219d5SEd Tanous inline void handleSystemCollectionResetActionGet(
3377c1e219d5SEd Tanous     crow::App& app, const crow::Request& req,
337822d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3379c1e219d5SEd Tanous     const std::string& systemName)
3380c1e219d5SEd Tanous {
33813ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
338245ca1b86SEd Tanous     {
338345ca1b86SEd Tanous         return;
338445ca1b86SEd Tanous     }
338525b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
33867f3e84a1SEd Tanous     {
33877f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
33887f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
33897f3e84a1SEd Tanous                                    systemName);
33907f3e84a1SEd Tanous         return;
33917f3e84a1SEd Tanous     }
3392746b56f3SAsmitha Karunanithi 
339368896206SGunnar Mills     if constexpr (BMCWEB_HYPERVISOR_COMPUTER_SYSTEM)
339468896206SGunnar Mills     {
3395746b56f3SAsmitha Karunanithi         if (systemName == "hypervisor")
3396746b56f3SAsmitha Karunanithi         {
3397746b56f3SAsmitha Karunanithi             handleHypervisorResetActionGet(asyncResp);
3398746b56f3SAsmitha Karunanithi             return;
3399746b56f3SAsmitha Karunanithi         }
340068896206SGunnar Mills     }
3401746b56f3SAsmitha Karunanithi 
3402253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
340322d268cbSEd Tanous     {
340422d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
340522d268cbSEd Tanous                                    systemName);
340622d268cbSEd Tanous         return;
340722d268cbSEd Tanous     }
340822d268cbSEd Tanous 
3409dd60b9edSEd Tanous     asyncResp->res.addHeader(
3410dd60b9edSEd Tanous         boost::beast::http::field::link,
3411dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ActionInfo/ActionInfo.json>; rel=describedby");
34121476687dSEd Tanous 
34131476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
3414253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Systems/{}/ResetActionInfo",
3415253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME);
3416c1e219d5SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ActionInfo.v1_1_2.ActionInfo";
34171476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Reset Action Info";
34181476687dSEd Tanous     asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
34193215e700SNan Zhou 
342033e1f122SAndrew Geissler     // Look to see if system defines AllowedHostTransitions
3421deae6a78SEd Tanous     dbus::utility::getProperty<std::vector<std::string>>(
3422deae6a78SEd Tanous         "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
3423deae6a78SEd Tanous         "xyz.openbmc_project.State.Host", "AllowedHostTransitions",
342433e1f122SAndrew Geissler         [asyncResp](const boost::system::error_code& ec,
342533e1f122SAndrew Geissler                     const std::vector<std::string>& allowedHostTransitions) {
3426bd79bce8SPatrick Williams             afterGetAllowedHostTransitions(asyncResp, ec,
3427bd79bce8SPatrick Williams                                            allowedHostTransitions);
342833e1f122SAndrew Geissler         });
3429c1e219d5SEd Tanous }
3430c1e219d5SEd Tanous /**
3431c1e219d5SEd Tanous  * SystemResetActionInfo derived class for delivering Computer Systems
3432c1e219d5SEd Tanous  * ResetType AllowableValues using ResetInfo schema.
3433c1e219d5SEd Tanous  */
3434100afe56SEd Tanous inline void requestRoutesSystems(App& app)
3435c1e219d5SEd Tanous {
3436100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/")
3437100afe56SEd Tanous         .privileges(redfish::privileges::headComputerSystemCollection)
3438100afe56SEd Tanous         .methods(boost::beast::http::verb::head)(
3439100afe56SEd Tanous             std::bind_front(handleComputerSystemCollectionHead, std::ref(app)));
3440100afe56SEd Tanous 
3441100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/")
3442100afe56SEd Tanous         .privileges(redfish::privileges::getComputerSystemCollection)
3443100afe56SEd Tanous         .methods(boost::beast::http::verb::get)(
3444100afe56SEd Tanous             std::bind_front(handleComputerSystemCollectionGet, std::ref(app)));
3445100afe56SEd Tanous 
3446100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/")
3447100afe56SEd Tanous         .privileges(redfish::privileges::headComputerSystem)
3448100afe56SEd Tanous         .methods(boost::beast::http::verb::head)(
3449100afe56SEd Tanous             std::bind_front(handleComputerSystemHead, std::ref(app)));
3450100afe56SEd Tanous 
3451100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/")
3452100afe56SEd Tanous         .privileges(redfish::privileges::getComputerSystem)
3453100afe56SEd Tanous         .methods(boost::beast::http::verb::get)(
3454100afe56SEd Tanous             std::bind_front(handleComputerSystemGet, std::ref(app)));
3455100afe56SEd Tanous 
3456100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/")
3457100afe56SEd Tanous         .privileges(redfish::privileges::patchComputerSystem)
3458100afe56SEd Tanous         .methods(boost::beast::http::verb::patch)(
3459100afe56SEd Tanous             std::bind_front(handleComputerSystemPatch, std::ref(app)));
3460100afe56SEd Tanous 
3461100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Actions/ComputerSystem.Reset/")
3462100afe56SEd Tanous         .privileges(redfish::privileges::postComputerSystem)
3463100afe56SEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
3464100afe56SEd Tanous             handleComputerSystemResetActionPost, std::ref(app)));
3465100afe56SEd Tanous 
3466c1e219d5SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/ResetActionInfo/")
3467c1e219d5SEd Tanous         .privileges(redfish::privileges::headActionInfo)
3468c1e219d5SEd Tanous         .methods(boost::beast::http::verb::head)(std::bind_front(
3469c1e219d5SEd Tanous             handleSystemCollectionResetActionHead, std::ref(app)));
3470c1e219d5SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/ResetActionInfo/")
3471c1e219d5SEd Tanous         .privileges(redfish::privileges::getActionInfo)
3472c1e219d5SEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
3473c1e219d5SEd Tanous             handleSystemCollectionResetActionGet, std::ref(app)));
34741cb1a9e6SAppaRao Puli }
3475c5b2abe0SLewanczyk, Dawid } // namespace redfish
3476