xref: /openbmc/bmcweb/features/redfish/lib/systems.hpp (revision d39a8c2876568ee886d67f53f63cdff539166278)
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"
28fc5ae94dSOliver 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             {
1184*d39a8c28SAishwary Joshi                 if (ec.value() != EBADR &&
1185*d39a8c28SAishwary Joshi                     ec.value() != boost::asio::error::host_unreachable)
1186797d5daeSCorey Hardesty                 {
1187*d39a8c28SAishwary Joshi                     // Service not available, no error, just don't return
1188*d39a8c28SAishwary Joshi                     // RebootAttempts information
118962598e31SEd Tanous                     BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
1190ac106bf6SEd Tanous                     messages::internalError(asyncResp->res);
1191797d5daeSCorey Hardesty                 }
1192797d5daeSCorey Hardesty                 return;
1193797d5daeSCorey Hardesty             }
1194797d5daeSCorey Hardesty 
1195797d5daeSCorey Hardesty             const uint32_t* attemptsLeft = nullptr;
1196797d5daeSCorey Hardesty             const uint32_t* retryAttempts = nullptr;
1197797d5daeSCorey Hardesty 
1198797d5daeSCorey Hardesty             const bool success = sdbusplus::unpackPropertiesNoThrow(
1199bd79bce8SPatrick Williams                 dbus_utils::UnpackErrorPrinter(), propertiesList,
1200bd79bce8SPatrick Williams                 "AttemptsLeft", attemptsLeft, "RetryAttempts", retryAttempts);
1201797d5daeSCorey Hardesty 
1202797d5daeSCorey Hardesty             if (!success)
1203797d5daeSCorey Hardesty             {
1204ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
1205797d5daeSCorey Hardesty                 return;
1206797d5daeSCorey Hardesty             }
1207797d5daeSCorey Hardesty 
1208797d5daeSCorey Hardesty             if (attemptsLeft != nullptr)
1209797d5daeSCorey Hardesty             {
1210ac106bf6SEd Tanous                 asyncResp->res
1211ac106bf6SEd Tanous                     .jsonValue["Boot"]["RemainingAutomaticRetryAttempts"] =
1212797d5daeSCorey Hardesty                     *attemptsLeft;
1213797d5daeSCorey Hardesty             }
1214797d5daeSCorey Hardesty 
1215797d5daeSCorey Hardesty             if (retryAttempts != nullptr)
1216797d5daeSCorey Hardesty             {
1217ac106bf6SEd Tanous                 asyncResp->res.jsonValue["Boot"]["AutomaticRetryAttempts"] =
1218797d5daeSCorey Hardesty                     *retryAttempts;
1219797d5daeSCorey Hardesty             }
1220797d5daeSCorey Hardesty         });
1221797d5daeSCorey Hardesty }
1222797d5daeSCorey Hardesty 
1223797d5daeSCorey Hardesty /**
12246bd5a8d2SGunnar Mills  * @brief Retrieves Automatic Retry properties. Known on D-Bus as AutoReboot.
12256bd5a8d2SGunnar Mills  *
1226ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
12276bd5a8d2SGunnar Mills  *
12286bd5a8d2SGunnar Mills  * @return None.
12296bd5a8d2SGunnar Mills  */
1230504af5a0SPatrick Williams inline void getAutomaticRetryPolicy(
1231504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12326bd5a8d2SGunnar Mills {
123362598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get Automatic Retry policy");
12346bd5a8d2SGunnar Mills 
1235deae6a78SEd Tanous     dbus::utility::getProperty<bool>(
1236deae6a78SEd Tanous         "xyz.openbmc_project.Settings",
12371e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/auto_reboot",
12381e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
1239ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
1240ac106bf6SEd Tanous                     bool autoRebootEnabled) {
12416bd5a8d2SGunnar Mills             if (ec)
12426bd5a8d2SGunnar Mills             {
1243*d39a8c28SAishwary Joshi                 // Service not available, no error, just don't return
1244*d39a8c28SAishwary Joshi                 // AutoReboot information
1245*d39a8c28SAishwary Joshi                 if (ec.value() != EBADR &&
1246*d39a8c28SAishwary Joshi                     ec.value() != boost::asio::error::host_unreachable)
1247797d5daeSCorey Hardesty                 {
124862598e31SEd Tanous                     BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
1249ac106bf6SEd Tanous                     messages::internalError(asyncResp->res);
1250797d5daeSCorey Hardesty                 }
12516bd5a8d2SGunnar Mills                 return;
12526bd5a8d2SGunnar Mills             }
12536bd5a8d2SGunnar Mills 
125462598e31SEd Tanous             BMCWEB_LOG_DEBUG("Auto Reboot: {}", autoRebootEnabled);
1255e05aec50SEd Tanous             if (autoRebootEnabled)
12566bd5a8d2SGunnar Mills             {
1257ac106bf6SEd Tanous                 asyncResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
12586bd5a8d2SGunnar Mills                     "RetryAttempts";
12596bd5a8d2SGunnar Mills             }
12606bd5a8d2SGunnar Mills             else
12616bd5a8d2SGunnar Mills             {
1262ac106bf6SEd Tanous                 asyncResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
1263ac106bf6SEd Tanous                     "Disabled";
12646bd5a8d2SGunnar Mills             }
1265ac106bf6SEd Tanous             getAutomaticRebootAttempts(asyncResp);
126669f35306SGunnar Mills 
126769f35306SGunnar Mills             // "AutomaticRetryConfig" can be 3 values, Disabled, RetryAlways,
126869f35306SGunnar Mills             // and RetryAttempts. OpenBMC only supports Disabled and
126969f35306SGunnar Mills             // RetryAttempts.
127020fa6a2cSEd Tanous             nlohmann::json::array_t allowed;
127120fa6a2cSEd Tanous             allowed.emplace_back("Disabled");
127220fa6a2cSEd Tanous             allowed.emplace_back("RetryAttempts");
1273ac106bf6SEd Tanous             asyncResp->res
1274bd79bce8SPatrick Williams                 .jsonValue["Boot"]
1275bd79bce8SPatrick Williams                           ["AutomaticRetryConfig@Redfish.AllowableValues"] =
127620fa6a2cSEd Tanous                 std::move(allowed);
12771e1e598dSJonathan Doman         });
12786bd5a8d2SGunnar Mills }
12796bd5a8d2SGunnar Mills 
12806bd5a8d2SGunnar Mills /**
1281797d5daeSCorey Hardesty  * @brief Sets RetryAttempts
1282797d5daeSCorey Hardesty  *
1283ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
1284797d5daeSCorey Hardesty  * @param[in] retryAttempts  "AutomaticRetryAttempts" from request.
1285797d5daeSCorey Hardesty  *
1286797d5daeSCorey Hardesty  *@return None.
1287797d5daeSCorey Hardesty  */
1288797d5daeSCorey Hardesty 
1289ac106bf6SEd Tanous inline void setAutomaticRetryAttempts(
1290ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1291797d5daeSCorey Hardesty     const uint32_t retryAttempts)
1292797d5daeSCorey Hardesty {
129362598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set Automatic Retry Attempts.");
129487c44966SAsmitha Karunanithi     setDbusProperty(
1295e93abac6SGinu George         asyncResp, "Boot/AutomaticRetryAttempts",
1296e93abac6SGinu George         "xyz.openbmc_project.State.Host",
129787c44966SAsmitha Karunanithi         sdbusplus::message::object_path("/xyz/openbmc_project/state/host0"),
12989ae226faSGeorge Liu         "xyz.openbmc_project.Control.Boot.RebootAttempts", "RetryAttempts",
1299e93abac6SGinu George         retryAttempts);
1300797d5daeSCorey Hardesty }
1301797d5daeSCorey Hardesty 
13028d69c668SEd Tanous inline computer_system::PowerRestorePolicyTypes
13038d69c668SEd Tanous     redfishPowerRestorePolicyFromDbus(std::string_view value)
13048d69c668SEd Tanous {
13058d69c668SEd Tanous     if (value ==
13068d69c668SEd Tanous         "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn")
13078d69c668SEd Tanous     {
13088d69c668SEd Tanous         return computer_system::PowerRestorePolicyTypes::AlwaysOn;
13098d69c668SEd Tanous     }
13108d69c668SEd Tanous     if (value ==
13118d69c668SEd Tanous         "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff")
13128d69c668SEd Tanous     {
13138d69c668SEd Tanous         return computer_system::PowerRestorePolicyTypes::AlwaysOff;
13148d69c668SEd Tanous     }
13158d69c668SEd Tanous     if (value ==
13163a34b742SGunnar Mills         "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore")
13178d69c668SEd Tanous     {
13188d69c668SEd Tanous         return computer_system::PowerRestorePolicyTypes::LastState;
13198d69c668SEd Tanous     }
13208d69c668SEd Tanous     if (value == "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.None")
13218d69c668SEd Tanous     {
13228d69c668SEd Tanous         return computer_system::PowerRestorePolicyTypes::AlwaysOff;
13238d69c668SEd Tanous     }
13248d69c668SEd Tanous     return computer_system::PowerRestorePolicyTypes::Invalid;
13258d69c668SEd Tanous }
1326797d5daeSCorey Hardesty /**
1327c6a620f2SGeorge Liu  * @brief Retrieves power restore policy over DBUS.
1328c6a620f2SGeorge Liu  *
1329ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
1330c6a620f2SGeorge Liu  *
1331c6a620f2SGeorge Liu  * @return None.
1332c6a620f2SGeorge Liu  */
1333504af5a0SPatrick Williams inline void getPowerRestorePolicy(
1334504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1335c6a620f2SGeorge Liu {
133662598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get power restore policy");
1337c6a620f2SGeorge Liu 
1338deae6a78SEd Tanous     dbus::utility::getProperty<std::string>(
1339deae6a78SEd Tanous         "xyz.openbmc_project.Settings",
13401e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/power_restore_policy",
13411e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
1342ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
13435e7e2dc5SEd Tanous                     const std::string& policy) {
1344c6a620f2SGeorge Liu             if (ec)
1345c6a620f2SGeorge Liu             {
134662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
1347c6a620f2SGeorge Liu                 return;
1348c6a620f2SGeorge Liu             }
13498d69c668SEd Tanous             computer_system::PowerRestorePolicyTypes restore =
13508d69c668SEd Tanous                 redfishPowerRestorePolicyFromDbus(policy);
13518d69c668SEd Tanous             if (restore == computer_system::PowerRestorePolicyTypes::Invalid)
1352c6a620f2SGeorge Liu             {
1353ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
1354c6a620f2SGeorge Liu                 return;
1355c6a620f2SGeorge Liu             }
1356c6a620f2SGeorge Liu 
13578d69c668SEd Tanous             asyncResp->res.jsonValue["PowerRestorePolicy"] = restore;
13581e1e598dSJonathan Doman         });
1359c6a620f2SGeorge Liu }
1360c6a620f2SGeorge Liu 
1361c6a620f2SGeorge Liu /**
13629dcfe8c1SAlbert Zhang  * @brief Stop Boot On Fault over DBUS.
13639dcfe8c1SAlbert Zhang  *
13649dcfe8c1SAlbert Zhang  * @param[in] asyncResp     Shared pointer for generating response message.
13659dcfe8c1SAlbert Zhang  *
13669dcfe8c1SAlbert Zhang  * @return None.
13679dcfe8c1SAlbert Zhang  */
1368504af5a0SPatrick Williams inline void getStopBootOnFault(
1369504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13709dcfe8c1SAlbert Zhang {
137162598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get Stop Boot On Fault");
13729dcfe8c1SAlbert Zhang 
1373deae6a78SEd Tanous     dbus::utility::getProperty<bool>(
1374deae6a78SEd Tanous         "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings",
13759dcfe8c1SAlbert Zhang         "xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError",
13769dcfe8c1SAlbert Zhang         [asyncResp](const boost::system::error_code& ec, bool value) {
13779dcfe8c1SAlbert Zhang             if (ec)
13789dcfe8c1SAlbert Zhang             {
1379*d39a8c28SAishwary Joshi                 // Service not available, no error, just don't return
1380*d39a8c28SAishwary Joshi                 // StopBootOnFault information
1381*d39a8c28SAishwary Joshi                 if (ec.value() != EBADR ||
1382*d39a8c28SAishwary Joshi                     ec.value() != boost::asio::error::host_unreachable)
13839dcfe8c1SAlbert Zhang                 {
1384b3e86cb0SGunnar Mills                     BMCWEB_LOG_ERROR("DBUS response error {}", ec);
13859dcfe8c1SAlbert Zhang                     messages::internalError(asyncResp->res);
13869dcfe8c1SAlbert Zhang                 }
13879dcfe8c1SAlbert Zhang                 return;
13889dcfe8c1SAlbert Zhang             }
13899dcfe8c1SAlbert Zhang 
13909dcfe8c1SAlbert Zhang             if (value)
13919dcfe8c1SAlbert Zhang             {
1392539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Boot"]["StopBootOnFault"] =
1393539d8c6bSEd Tanous                     computer_system::StopBootOnFault::AnyFault;
13949dcfe8c1SAlbert Zhang             }
13959dcfe8c1SAlbert Zhang             else
13969dcfe8c1SAlbert Zhang             {
1397539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Boot"]["StopBootOnFault"] =
1398539d8c6bSEd Tanous                     computer_system::StopBootOnFault::Never;
13999dcfe8c1SAlbert Zhang             }
14009dcfe8c1SAlbert Zhang         });
14019dcfe8c1SAlbert Zhang }
14029dcfe8c1SAlbert Zhang 
14039dcfe8c1SAlbert Zhang /**
14041981771bSAli Ahmed  * @brief Get TrustedModuleRequiredToBoot property. Determines whether or not
14051981771bSAli Ahmed  * TPM is required for booting the host.
14061981771bSAli Ahmed  *
1407ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
14081981771bSAli Ahmed  *
14091981771bSAli Ahmed  * @return None.
14101981771bSAli Ahmed  */
14111981771bSAli Ahmed inline void getTrustedModuleRequiredToBoot(
1412ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14131981771bSAli Ahmed {
141462598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get TPM required to boot.");
1415e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1416e99073f5SGeorge Liu         "xyz.openbmc_project.Control.TPM.Policy"};
1417e99073f5SGeorge Liu     dbus::utility::getSubTree(
1418e99073f5SGeorge Liu         "/", 0, interfaces,
1419ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
1420b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
14211981771bSAli Ahmed             if (ec)
14221981771bSAli Ahmed             {
1423bd79bce8SPatrick Williams                 BMCWEB_LOG_DEBUG(
1424bd79bce8SPatrick Williams                     "DBUS response error on TPM.Policy GetSubTree{}", ec);
14251981771bSAli Ahmed                 // This is an optional D-Bus object so just return if
14261981771bSAli Ahmed                 // error occurs
14271981771bSAli Ahmed                 return;
14281981771bSAli Ahmed             }
142926f6976fSEd Tanous             if (subtree.empty())
14301981771bSAli Ahmed             {
14311981771bSAli Ahmed                 // As noted above, this is an optional interface so just return
14321981771bSAli Ahmed                 // if there is no instance found
14331981771bSAli Ahmed                 return;
14341981771bSAli Ahmed             }
14351981771bSAli Ahmed 
14361981771bSAli Ahmed             /* When there is more than one TPMEnable object... */
14371981771bSAli Ahmed             if (subtree.size() > 1)
14381981771bSAli Ahmed             {
143962598e31SEd Tanous                 BMCWEB_LOG_DEBUG(
144062598e31SEd Tanous                     "DBUS response has more than 1 TPM Enable object:{}",
144162598e31SEd Tanous                     subtree.size());
14421981771bSAli Ahmed                 // Throw an internal Error and return
1443ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
14441981771bSAli Ahmed                 return;
14451981771bSAli Ahmed             }
14461981771bSAli Ahmed 
14471981771bSAli Ahmed             // Make sure the Dbus response map has a service and objectPath
14481981771bSAli Ahmed             // field
14491981771bSAli Ahmed             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
14501981771bSAli Ahmed             {
145162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("TPM.Policy mapper error!");
1452ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
14531981771bSAli Ahmed                 return;
14541981771bSAli Ahmed             }
14551981771bSAli Ahmed 
14561981771bSAli Ahmed             const std::string& path = subtree[0].first;
14571981771bSAli Ahmed             const std::string& serv = subtree[0].second.begin()->first;
14581981771bSAli Ahmed 
14591981771bSAli Ahmed             // Valid TPM Enable object found, now reading the current value
1460deae6a78SEd Tanous             dbus::utility::getProperty<bool>(
1461deae6a78SEd Tanous                 serv, path, "xyz.openbmc_project.Control.TPM.Policy",
1462deae6a78SEd Tanous                 "TPMEnable",
1463ac106bf6SEd Tanous                 [asyncResp](const boost::system::error_code& ec2,
1464ac106bf6SEd Tanous                             bool tpmRequired) {
14658a592810SEd Tanous                     if (ec2)
14661981771bSAli Ahmed                     {
1467bd79bce8SPatrick Williams                         BMCWEB_LOG_ERROR(
1468bd79bce8SPatrick Williams                             "D-BUS response error on TPM.Policy Get{}", ec2);
1469ac106bf6SEd Tanous                         messages::internalError(asyncResp->res);
14701981771bSAli Ahmed                         return;
14711981771bSAli Ahmed                     }
14721981771bSAli Ahmed 
14731e1e598dSJonathan Doman                     if (tpmRequired)
14741981771bSAli Ahmed                     {
1475ac106bf6SEd Tanous                         asyncResp->res
1476ac106bf6SEd Tanous                             .jsonValue["Boot"]["TrustedModuleRequiredToBoot"] =
14771981771bSAli Ahmed                             "Required";
14781981771bSAli Ahmed                     }
14791981771bSAli Ahmed                     else
14801981771bSAli Ahmed                     {
1481ac106bf6SEd Tanous                         asyncResp->res
1482ac106bf6SEd Tanous                             .jsonValue["Boot"]["TrustedModuleRequiredToBoot"] =
14831981771bSAli Ahmed                             "Disabled";
14841981771bSAli Ahmed                     }
14851e1e598dSJonathan Doman                 });
1486e99073f5SGeorge Liu         });
14871981771bSAli Ahmed }
14881981771bSAli Ahmed 
14891981771bSAli Ahmed /**
14901c05dae3SAli Ahmed  * @brief Set TrustedModuleRequiredToBoot property. Determines whether or not
14911c05dae3SAli Ahmed  * TPM is required for booting the host.
14921c05dae3SAli Ahmed  *
1493ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
14941c05dae3SAli Ahmed  * @param[in] tpmRequired   Value to set TPM Required To Boot property to.
14951c05dae3SAli Ahmed  *
14961c05dae3SAli Ahmed  * @return None.
14971c05dae3SAli Ahmed  */
14981c05dae3SAli Ahmed inline void setTrustedModuleRequiredToBoot(
1499ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const bool tpmRequired)
15001c05dae3SAli Ahmed {
150162598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set TrustedModuleRequiredToBoot.");
1502e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1503e99073f5SGeorge Liu         "xyz.openbmc_project.Control.TPM.Policy"};
1504e99073f5SGeorge Liu     dbus::utility::getSubTree(
1505e99073f5SGeorge Liu         "/", 0, interfaces,
1506ac106bf6SEd Tanous         [asyncResp,
1507e99073f5SGeorge Liu          tpmRequired](const boost::system::error_code& ec,
1508e99073f5SGeorge Liu                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
15091c05dae3SAli Ahmed             if (ec)
15101c05dae3SAli Ahmed             {
1511bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR(
1512bd79bce8SPatrick Williams                     "DBUS response error on TPM.Policy GetSubTree{}", ec);
1513ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
15141c05dae3SAli Ahmed                 return;
15151c05dae3SAli Ahmed             }
151626f6976fSEd Tanous             if (subtree.empty())
15171c05dae3SAli Ahmed             {
1518bd79bce8SPatrick Williams                 messages::propertyValueNotInList(asyncResp->res,
1519bd79bce8SPatrick Williams                                                  "ComputerSystem",
15201c05dae3SAli Ahmed                                                  "TrustedModuleRequiredToBoot");
15211c05dae3SAli Ahmed                 return;
15221c05dae3SAli Ahmed             }
15231c05dae3SAli Ahmed 
15241c05dae3SAli Ahmed             /* When there is more than one TPMEnable object... */
15251c05dae3SAli Ahmed             if (subtree.size() > 1)
15261c05dae3SAli Ahmed             {
152762598e31SEd Tanous                 BMCWEB_LOG_DEBUG(
152862598e31SEd Tanous                     "DBUS response has more than 1 TPM Enable object:{}",
152962598e31SEd Tanous                     subtree.size());
15301c05dae3SAli Ahmed                 // Throw an internal Error and return
1531ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
15321c05dae3SAli Ahmed                 return;
15331c05dae3SAli Ahmed             }
15341c05dae3SAli Ahmed 
15351c05dae3SAli Ahmed             // Make sure the Dbus response map has a service and objectPath
15361c05dae3SAli Ahmed             // field
15371c05dae3SAli Ahmed             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
15381c05dae3SAli Ahmed             {
153962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("TPM.Policy mapper error!");
1540ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
15411c05dae3SAli Ahmed                 return;
15421c05dae3SAli Ahmed             }
15431c05dae3SAli Ahmed 
15441c05dae3SAli Ahmed             const std::string& path = subtree[0].first;
15451c05dae3SAli Ahmed             const std::string& serv = subtree[0].second.begin()->first;
15461c05dae3SAli Ahmed 
15471c05dae3SAli Ahmed             if (serv.empty())
15481c05dae3SAli Ahmed             {
154962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("TPM.Policy service mapper error!");
1550ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
15511c05dae3SAli Ahmed                 return;
15521c05dae3SAli Ahmed             }
15531c05dae3SAli Ahmed 
15541c05dae3SAli Ahmed             // Valid TPM Enable object found, now setting the value
1555e93abac6SGinu George             setDbusProperty(asyncResp, "Boot/TrustedModuleRequiredToBoot", serv,
1556e93abac6SGinu George                             path, "xyz.openbmc_project.Control.TPM.Policy",
1557e93abac6SGinu George                             "TPMEnable", tpmRequired);
1558e99073f5SGeorge Liu         });
15591c05dae3SAli Ahmed }
15601c05dae3SAli Ahmed 
15611c05dae3SAli Ahmed /**
1562491d8ee7SSantosh Puranik  * @brief Sets boot properties into DBUS object(s).
1563491d8ee7SSantosh Puranik  *
1564ac106bf6SEd Tanous  * @param[in] asyncResp       Shared pointer for generating response message.
1565cd9a4666SKonstantin Aladyshev  * @param[in] bootType        The boot type to set.
1566cd9a4666SKonstantin Aladyshev  * @return Integer error code.
1567cd9a4666SKonstantin Aladyshev  */
1568ac106bf6SEd Tanous inline void setBootType(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1569cd9a4666SKonstantin Aladyshev                         const std::optional<std::string>& bootType)
1570cd9a4666SKonstantin Aladyshev {
1571c21865c4SKonstantin Aladyshev     std::string bootTypeStr;
1572cd9a4666SKonstantin Aladyshev 
1573c21865c4SKonstantin Aladyshev     if (!bootType)
1574cd9a4666SKonstantin Aladyshev     {
1575c21865c4SKonstantin Aladyshev         return;
1576c21865c4SKonstantin Aladyshev     }
1577c21865c4SKonstantin Aladyshev 
1578cd9a4666SKonstantin Aladyshev     // Source target specified
157962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Boot type: {}", *bootType);
1580cd9a4666SKonstantin Aladyshev     // Figure out which DBUS interface and property to use
1581cd9a4666SKonstantin Aladyshev     if (*bootType == "Legacy")
1582cd9a4666SKonstantin Aladyshev     {
1583cd9a4666SKonstantin Aladyshev         bootTypeStr = "xyz.openbmc_project.Control.Boot.Type.Types.Legacy";
1584cd9a4666SKonstantin Aladyshev     }
1585cd9a4666SKonstantin Aladyshev     else if (*bootType == "UEFI")
1586cd9a4666SKonstantin Aladyshev     {
1587cd9a4666SKonstantin Aladyshev         bootTypeStr = "xyz.openbmc_project.Control.Boot.Type.Types.EFI";
1588cd9a4666SKonstantin Aladyshev     }
1589cd9a4666SKonstantin Aladyshev     else
1590cd9a4666SKonstantin Aladyshev     {
159162598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid property value for "
159262598e31SEd Tanous                          "BootSourceOverrideMode: {}",
159362598e31SEd Tanous                          *bootType);
1594ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *bootType,
1595cd9a4666SKonstantin Aladyshev                                          "BootSourceOverrideMode");
1596cd9a4666SKonstantin Aladyshev         return;
1597cd9a4666SKonstantin Aladyshev     }
1598cd9a4666SKonstantin Aladyshev 
1599cd9a4666SKonstantin Aladyshev     // Act on validated parameters
160062598e31SEd Tanous     BMCWEB_LOG_DEBUG("DBUS boot type: {}", bootTypeStr);
1601cd9a4666SKonstantin Aladyshev 
1602e93abac6SGinu George     setDbusProperty(asyncResp, "Boot/BootSourceOverrideMode",
1603e93abac6SGinu George                     "xyz.openbmc_project.Settings",
160487c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
160587c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/boot"),
160687c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Control.Boot.Type", "BootType",
1607e93abac6SGinu George                     bootTypeStr);
1608cd9a4666SKonstantin Aladyshev }
1609cd9a4666SKonstantin Aladyshev 
1610cd9a4666SKonstantin Aladyshev /**
1611cd9a4666SKonstantin Aladyshev  * @brief Sets boot properties into DBUS object(s).
1612cd9a4666SKonstantin Aladyshev  *
1613ac106bf6SEd Tanous  * @param[in] asyncResp           Shared pointer for generating response
1614ac106bf6SEd Tanous  * message.
1615c21865c4SKonstantin Aladyshev  * @param[in] bootType        The boot type to set.
1616c21865c4SKonstantin Aladyshev  * @return Integer error code.
1617c21865c4SKonstantin Aladyshev  */
1618ac106bf6SEd Tanous inline void setBootEnable(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1619c21865c4SKonstantin Aladyshev                           const std::optional<std::string>& bootEnable)
1620c21865c4SKonstantin Aladyshev {
1621c21865c4SKonstantin Aladyshev     if (!bootEnable)
1622c21865c4SKonstantin Aladyshev     {
1623c21865c4SKonstantin Aladyshev         return;
1624c21865c4SKonstantin Aladyshev     }
1625c21865c4SKonstantin Aladyshev     // Source target specified
162662598e31SEd Tanous     BMCWEB_LOG_DEBUG("Boot enable: {}", *bootEnable);
1627c21865c4SKonstantin Aladyshev 
1628c21865c4SKonstantin Aladyshev     bool bootOverrideEnable = false;
1629c21865c4SKonstantin Aladyshev     bool bootOverridePersistent = false;
1630c21865c4SKonstantin Aladyshev     // Figure out which DBUS interface and property to use
1631c21865c4SKonstantin Aladyshev     if (*bootEnable == "Disabled")
1632c21865c4SKonstantin Aladyshev     {
1633c21865c4SKonstantin Aladyshev         bootOverrideEnable = false;
1634c21865c4SKonstantin Aladyshev     }
1635c21865c4SKonstantin Aladyshev     else if (*bootEnable == "Once")
1636c21865c4SKonstantin Aladyshev     {
1637c21865c4SKonstantin Aladyshev         bootOverrideEnable = true;
1638c21865c4SKonstantin Aladyshev         bootOverridePersistent = false;
1639c21865c4SKonstantin Aladyshev     }
1640c21865c4SKonstantin Aladyshev     else if (*bootEnable == "Continuous")
1641c21865c4SKonstantin Aladyshev     {
1642c21865c4SKonstantin Aladyshev         bootOverrideEnable = true;
1643c21865c4SKonstantin Aladyshev         bootOverridePersistent = true;
1644c21865c4SKonstantin Aladyshev     }
1645c21865c4SKonstantin Aladyshev     else
1646c21865c4SKonstantin Aladyshev     {
164762598e31SEd Tanous         BMCWEB_LOG_DEBUG(
164862598e31SEd Tanous             "Invalid property value for BootSourceOverrideEnabled: {}",
164962598e31SEd Tanous             *bootEnable);
1650ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *bootEnable,
1651c21865c4SKonstantin Aladyshev                                          "BootSourceOverrideEnabled");
1652c21865c4SKonstantin Aladyshev         return;
1653c21865c4SKonstantin Aladyshev     }
1654c21865c4SKonstantin Aladyshev 
1655c21865c4SKonstantin Aladyshev     // Act on validated parameters
165662598e31SEd Tanous     BMCWEB_LOG_DEBUG("DBUS boot override enable: {}", bootOverrideEnable);
1657c21865c4SKonstantin Aladyshev 
1658e93abac6SGinu George     setDbusProperty(asyncResp, "Boot/BootSourceOverrideEnabled",
1659e93abac6SGinu George                     "xyz.openbmc_project.Settings",
166087c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
166187c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/boot"),
166287c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Object.Enable", "Enabled",
1663e93abac6SGinu George                     bootOverrideEnable);
1664c21865c4SKonstantin Aladyshev 
1665c21865c4SKonstantin Aladyshev     if (!bootOverrideEnable)
1666c21865c4SKonstantin Aladyshev     {
1667c21865c4SKonstantin Aladyshev         return;
1668c21865c4SKonstantin Aladyshev     }
1669c21865c4SKonstantin Aladyshev 
1670c21865c4SKonstantin Aladyshev     // In case boot override is enabled we need to set correct value for the
1671c21865c4SKonstantin Aladyshev     // 'one_time' enable DBus interface
167262598e31SEd Tanous     BMCWEB_LOG_DEBUG("DBUS boot override persistent: {}",
167362598e31SEd Tanous                      bootOverridePersistent);
1674c21865c4SKonstantin Aladyshev 
1675e93abac6SGinu George     setDbusProperty(asyncResp, "Boot/BootSourceOverrideEnabled",
1676e93abac6SGinu George                     "xyz.openbmc_project.Settings",
167787c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
167887c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/boot/one_time"),
167987c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Object.Enable", "Enabled",
1680e93abac6SGinu George                     !bootOverridePersistent);
1681c21865c4SKonstantin Aladyshev }
1682c21865c4SKonstantin Aladyshev 
1683c21865c4SKonstantin Aladyshev /**
1684c21865c4SKonstantin Aladyshev  * @brief Sets boot properties into DBUS object(s).
1685c21865c4SKonstantin Aladyshev  *
1686ac106bf6SEd Tanous  * @param[in] asyncResp       Shared pointer for generating response message.
1687491d8ee7SSantosh Puranik  * @param[in] bootSource      The boot source to set.
1688491d8ee7SSantosh Puranik  *
1689265c1602SJohnathan Mantey  * @return Integer error code.
1690491d8ee7SSantosh Puranik  */
1691504af5a0SPatrick Williams inline void setBootModeOrSource(
1692504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1693cd9a4666SKonstantin Aladyshev     const std::optional<std::string>& bootSource)
1694491d8ee7SSantosh Puranik {
1695c21865c4SKonstantin Aladyshev     std::string bootSourceStr;
1696c21865c4SKonstantin Aladyshev     std::string bootModeStr;
1697944ffaf9SJohnathan Mantey 
1698c21865c4SKonstantin Aladyshev     if (!bootSource)
1699491d8ee7SSantosh Puranik     {
1700c21865c4SKonstantin Aladyshev         return;
1701c21865c4SKonstantin Aladyshev     }
1702c21865c4SKonstantin Aladyshev 
1703491d8ee7SSantosh Puranik     // Source target specified
170462598e31SEd Tanous     BMCWEB_LOG_DEBUG("Boot source: {}", *bootSource);
1705491d8ee7SSantosh Puranik     // Figure out which DBUS interface and property to use
1706ac106bf6SEd Tanous     if (assignBootParameters(asyncResp, *bootSource, bootSourceStr,
1707ac106bf6SEd Tanous                              bootModeStr) != 0)
1708491d8ee7SSantosh Puranik     {
170962598e31SEd Tanous         BMCWEB_LOG_DEBUG(
171062598e31SEd Tanous             "Invalid property value for BootSourceOverrideTarget: {}",
171162598e31SEd Tanous             *bootSource);
1712ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *bootSource,
1713491d8ee7SSantosh Puranik                                          "BootSourceTargetOverride");
1714491d8ee7SSantosh Puranik         return;
1715491d8ee7SSantosh Puranik     }
1716491d8ee7SSantosh Puranik 
1717944ffaf9SJohnathan Mantey     // Act on validated parameters
171862598e31SEd Tanous     BMCWEB_LOG_DEBUG("DBUS boot source: {}", bootSourceStr);
171962598e31SEd Tanous     BMCWEB_LOG_DEBUG("DBUS boot mode: {}", bootModeStr);
1720944ffaf9SJohnathan Mantey 
1721e93abac6SGinu George     setDbusProperty(asyncResp, "Boot/BootSourceOverrideTarget",
1722e93abac6SGinu George                     "xyz.openbmc_project.Settings",
172387c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
172487c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/boot"),
172587c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Control.Boot.Source", "BootSource",
1726e93abac6SGinu George                     bootSourceStr);
1727e93abac6SGinu George     setDbusProperty(asyncResp, "Boot/BootSourceOverrideTarget",
1728e93abac6SGinu George                     "xyz.openbmc_project.Settings",
172987c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
173087c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/boot"),
173187c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
1732e93abac6SGinu George                     bootModeStr);
1733cd9a4666SKonstantin Aladyshev }
1734944ffaf9SJohnathan Mantey 
1735cd9a4666SKonstantin Aladyshev /**
1736c21865c4SKonstantin Aladyshev  * @brief Sets Boot source override properties.
1737491d8ee7SSantosh Puranik  *
1738ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
1739491d8ee7SSantosh Puranik  * @param[in] bootSource The boot source from incoming RF request.
1740cd9a4666SKonstantin Aladyshev  * @param[in] bootType   The boot type from incoming RF request.
1741491d8ee7SSantosh Puranik  * @param[in] bootEnable The boot override enable from incoming RF request.
1742491d8ee7SSantosh Puranik  *
1743265c1602SJohnathan Mantey  * @return Integer error code.
1744491d8ee7SSantosh Puranik  */
1745c21865c4SKonstantin Aladyshev 
1746504af5a0SPatrick Williams inline void setBootProperties(
1747504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1748c21865c4SKonstantin Aladyshev     const std::optional<std::string>& bootSource,
1749c21865c4SKonstantin Aladyshev     const std::optional<std::string>& bootType,
1750c21865c4SKonstantin Aladyshev     const std::optional<std::string>& bootEnable)
1751491d8ee7SSantosh Puranik {
175262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set boot information.");
1753491d8ee7SSantosh Puranik 
1754ac106bf6SEd Tanous     setBootModeOrSource(asyncResp, bootSource);
1755ac106bf6SEd Tanous     setBootType(asyncResp, bootType);
1756ac106bf6SEd Tanous     setBootEnable(asyncResp, bootEnable);
1757491d8ee7SSantosh Puranik }
1758491d8ee7SSantosh Puranik 
1759c6a620f2SGeorge Liu /**
176098e386ecSGunnar Mills  * @brief Sets AssetTag
176198e386ecSGunnar Mills  *
1762ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for generating response message.
176398e386ecSGunnar Mills  * @param[in] assetTag  "AssetTag" from request.
176498e386ecSGunnar Mills  *
176598e386ecSGunnar Mills  * @return None.
176698e386ecSGunnar Mills  */
1767ac106bf6SEd Tanous inline void setAssetTag(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
176898e386ecSGunnar Mills                         const std::string& assetTag)
176998e386ecSGunnar Mills {
1770e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1771e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.System"};
1772e99073f5SGeorge Liu     dbus::utility::getSubTree(
1773e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
1774ac106bf6SEd Tanous         [asyncResp,
1775e99073f5SGeorge Liu          assetTag](const boost::system::error_code& ec,
1776b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
177798e386ecSGunnar Mills             if (ec)
177898e386ecSGunnar Mills             {
177962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("D-Bus response error on GetSubTree {}", ec);
1780ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
178198e386ecSGunnar Mills                 return;
178298e386ecSGunnar Mills             }
178326f6976fSEd Tanous             if (subtree.empty())
178498e386ecSGunnar Mills             {
178562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't find system D-Bus object!");
1786ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
178798e386ecSGunnar Mills                 return;
178898e386ecSGunnar Mills             }
178998e386ecSGunnar Mills             // Assume only 1 system D-Bus object
179098e386ecSGunnar Mills             // Throw an error if there is more than 1
179198e386ecSGunnar Mills             if (subtree.size() > 1)
179298e386ecSGunnar Mills             {
179362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Found more than 1 system D-Bus object!");
1794ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
179598e386ecSGunnar Mills                 return;
179698e386ecSGunnar Mills             }
179798e386ecSGunnar Mills             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
179898e386ecSGunnar Mills             {
179962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Asset Tag Set mapper error!");
1800ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
180198e386ecSGunnar Mills                 return;
180298e386ecSGunnar Mills             }
180398e386ecSGunnar Mills 
180498e386ecSGunnar Mills             const std::string& path = subtree[0].first;
180598e386ecSGunnar Mills             const std::string& service = subtree[0].second.begin()->first;
180698e386ecSGunnar Mills 
180798e386ecSGunnar Mills             if (service.empty())
180898e386ecSGunnar Mills             {
180962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Asset Tag Set service mapper error!");
1810ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
181198e386ecSGunnar Mills                 return;
181298e386ecSGunnar Mills             }
181398e386ecSGunnar Mills 
1814e93abac6SGinu George             setDbusProperty(asyncResp, "AssetTag", service, path,
181587c44966SAsmitha Karunanithi                             "xyz.openbmc_project.Inventory.Decorator.AssetTag",
1816e93abac6SGinu George                             "AssetTag", assetTag);
1817e99073f5SGeorge Liu         });
181898e386ecSGunnar Mills }
181998e386ecSGunnar Mills 
182098e386ecSGunnar Mills /**
18219dcfe8c1SAlbert Zhang  * @brief Validate the specified stopBootOnFault is valid and return the
18229dcfe8c1SAlbert Zhang  * stopBootOnFault name associated with that string
18239dcfe8c1SAlbert Zhang  *
18249dcfe8c1SAlbert Zhang  * @param[in] stopBootOnFaultString  String representing the desired
18259dcfe8c1SAlbert Zhang  * stopBootOnFault
18269dcfe8c1SAlbert Zhang  *
18279dcfe8c1SAlbert Zhang  * @return stopBootOnFault value or empty  if incoming value is not valid
18289dcfe8c1SAlbert Zhang  */
1829504af5a0SPatrick Williams inline std::optional<bool> validstopBootOnFault(
1830504af5a0SPatrick Williams     const std::string& stopBootOnFaultString)
18319dcfe8c1SAlbert Zhang {
18329dcfe8c1SAlbert Zhang     if (stopBootOnFaultString == "AnyFault")
18339dcfe8c1SAlbert Zhang     {
18349dcfe8c1SAlbert Zhang         return true;
18359dcfe8c1SAlbert Zhang     }
18369dcfe8c1SAlbert Zhang 
18379dcfe8c1SAlbert Zhang     if (stopBootOnFaultString == "Never")
18389dcfe8c1SAlbert Zhang     {
18399dcfe8c1SAlbert Zhang         return false;
18409dcfe8c1SAlbert Zhang     }
18419dcfe8c1SAlbert Zhang 
18429dcfe8c1SAlbert Zhang     return std::nullopt;
18439dcfe8c1SAlbert Zhang }
18449dcfe8c1SAlbert Zhang 
18459dcfe8c1SAlbert Zhang /**
18469dcfe8c1SAlbert Zhang  * @brief Sets stopBootOnFault
18479dcfe8c1SAlbert Zhang  *
1848fc3edfddSEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
18499dcfe8c1SAlbert Zhang  * @param[in] stopBootOnFault  "StopBootOnFault" from request.
18509dcfe8c1SAlbert Zhang  *
18519dcfe8c1SAlbert Zhang  * @return None.
18529dcfe8c1SAlbert Zhang  */
1853504af5a0SPatrick Williams inline void setStopBootOnFault(
1854504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18559dcfe8c1SAlbert Zhang     const std::string& stopBootOnFault)
18569dcfe8c1SAlbert Zhang {
185762598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set Stop Boot On Fault.");
18589dcfe8c1SAlbert Zhang 
18599dcfe8c1SAlbert Zhang     std::optional<bool> stopBootEnabled = validstopBootOnFault(stopBootOnFault);
18609dcfe8c1SAlbert Zhang     if (!stopBootEnabled)
18619dcfe8c1SAlbert Zhang     {
186262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid property value for StopBootOnFault: {}",
186362598e31SEd Tanous                          stopBootOnFault);
1864fc3edfddSEd Tanous         messages::propertyValueNotInList(asyncResp->res, stopBootOnFault,
18659dcfe8c1SAlbert Zhang                                          "StopBootOnFault");
18669dcfe8c1SAlbert Zhang         return;
18679dcfe8c1SAlbert Zhang     }
18689dcfe8c1SAlbert Zhang 
1869e93abac6SGinu George     setDbusProperty(asyncResp, "Boot/StopBootOnFault",
1870e93abac6SGinu George                     "xyz.openbmc_project.Settings",
187187c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
187287c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/logging/settings"),
1873fc3edfddSEd Tanous                     "xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError",
1874e93abac6SGinu George                     *stopBootEnabled);
18759dcfe8c1SAlbert Zhang }
18769dcfe8c1SAlbert Zhang 
18779dcfe8c1SAlbert Zhang /**
187869f35306SGunnar Mills  * @brief Sets automaticRetry (Auto Reboot)
187969f35306SGunnar Mills  *
1880ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
188169f35306SGunnar Mills  * @param[in] automaticRetryConfig  "AutomaticRetryConfig" from request.
188269f35306SGunnar Mills  *
188369f35306SGunnar Mills  * @return None.
188469f35306SGunnar Mills  */
1885504af5a0SPatrick Williams inline void setAutomaticRetry(
1886504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1887f23b7296SEd Tanous     const std::string& automaticRetryConfig)
188869f35306SGunnar Mills {
188962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set Automatic Retry.");
189069f35306SGunnar Mills 
189169f35306SGunnar Mills     // OpenBMC only supports "Disabled" and "RetryAttempts".
1892543f4400SEd Tanous     bool autoRebootEnabled = false;
189369f35306SGunnar Mills 
189469f35306SGunnar Mills     if (automaticRetryConfig == "Disabled")
189569f35306SGunnar Mills     {
189669f35306SGunnar Mills         autoRebootEnabled = false;
189769f35306SGunnar Mills     }
189869f35306SGunnar Mills     else if (automaticRetryConfig == "RetryAttempts")
189969f35306SGunnar Mills     {
190069f35306SGunnar Mills         autoRebootEnabled = true;
190169f35306SGunnar Mills     }
190269f35306SGunnar Mills     else
190369f35306SGunnar Mills     {
190462598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid property value for AutomaticRetryConfig: {}",
190562598e31SEd Tanous                          automaticRetryConfig);
1906ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, automaticRetryConfig,
190769f35306SGunnar Mills                                          "AutomaticRetryConfig");
190869f35306SGunnar Mills         return;
190969f35306SGunnar Mills     }
191069f35306SGunnar Mills 
1911e93abac6SGinu George     setDbusProperty(asyncResp, "Boot/AutomaticRetryConfig",
1912e93abac6SGinu George                     "xyz.openbmc_project.Settings",
191387c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
191487c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/auto_reboot"),
191587c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Control.Boot.RebootPolicy",
1916e93abac6SGinu George                     "AutoReboot", autoRebootEnabled);
191769f35306SGunnar Mills }
191869f35306SGunnar Mills 
19198d69c668SEd Tanous inline std::string dbusPowerRestorePolicyFromRedfish(std::string_view policy)
19208d69c668SEd Tanous {
19218d69c668SEd Tanous     if (policy == "AlwaysOn")
19228d69c668SEd Tanous     {
19238d69c668SEd Tanous         return "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn";
19248d69c668SEd Tanous     }
19258d69c668SEd Tanous     if (policy == "AlwaysOff")
19268d69c668SEd Tanous     {
19278d69c668SEd Tanous         return "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff";
19288d69c668SEd Tanous     }
19298d69c668SEd Tanous     if (policy == "LastState")
19308d69c668SEd Tanous     {
19318d69c668SEd Tanous         return "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore";
19328d69c668SEd Tanous     }
19338d69c668SEd Tanous     return "";
19348d69c668SEd Tanous }
19358d69c668SEd Tanous 
193669f35306SGunnar Mills /**
1937c6a620f2SGeorge Liu  * @brief Sets power restore policy properties.
1938c6a620f2SGeorge Liu  *
1939ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
1940c6a620f2SGeorge Liu  * @param[in] policy  power restore policy properties from request.
1941c6a620f2SGeorge Liu  *
1942c6a620f2SGeorge Liu  * @return None.
1943c6a620f2SGeorge Liu  */
1944504af5a0SPatrick Williams inline void setPowerRestorePolicy(
1945504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19468d69c668SEd Tanous     std::string_view policy)
1947c6a620f2SGeorge Liu {
194862598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set power restore policy.");
1949c6a620f2SGeorge Liu 
19508d69c668SEd Tanous     std::string powerRestorePolicy = dbusPowerRestorePolicyFromRedfish(policy);
1951c6a620f2SGeorge Liu 
19528d69c668SEd Tanous     if (powerRestorePolicy.empty())
1953c6a620f2SGeorge Liu     {
1954ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, policy,
19554e69c904SGunnar Mills                                          "PowerRestorePolicy");
1956c6a620f2SGeorge Liu         return;
1957c6a620f2SGeorge Liu     }
1958c6a620f2SGeorge Liu 
195987c44966SAsmitha Karunanithi     setDbusProperty(
1960e93abac6SGinu George         asyncResp, "PowerRestorePolicy", "xyz.openbmc_project.Settings",
196187c44966SAsmitha Karunanithi         sdbusplus::message::object_path(
196287c44966SAsmitha Karunanithi             "/xyz/openbmc_project/control/host0/power_restore_policy"),
19639ae226faSGeorge Liu         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
1964e93abac6SGinu George         powerRestorePolicy);
1965c6a620f2SGeorge Liu }
1966c6a620f2SGeorge Liu 
1967a6349918SAppaRao Puli /**
1968a6349918SAppaRao Puli  * @brief Retrieves provisioning status
1969a6349918SAppaRao Puli  *
197025b54dbaSEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous
197125b54dbaSEd Tanous  * calls.
1972a6349918SAppaRao Puli  *
1973a6349918SAppaRao Puli  * @return None.
1974a6349918SAppaRao Puli  */
1975504af5a0SPatrick Williams inline void getProvisioningStatus(
1976504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1977a6349918SAppaRao Puli {
197862598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get OEM information.");
1979deae6a78SEd Tanous     dbus::utility::getAllProperties(
1980deae6a78SEd Tanous         "xyz.openbmc_project.PFR.Manager", "/xyz/openbmc_project/pfr",
1981deae6a78SEd Tanous         "xyz.openbmc_project.PFR.Attributes",
1982ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
1983b9d36b47SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
1984b99fb1a9SAppaRao Puli             nlohmann::json& oemPFR =
1985bd79bce8SPatrick Williams                 asyncResp->res
1986bd79bce8SPatrick Williams                     .jsonValue["Oem"]["OpenBmc"]["FirmwareProvisioning"];
1987ac106bf6SEd Tanous             asyncResp->res.jsonValue["Oem"]["OpenBmc"]["@odata.type"] =
19881d834d49SEd Tanous                 "#OpenBMCComputerSystem.v1_0_0.OpenBmc";
1989bd79bce8SPatrick Williams             oemPFR["@odata.type"] =
1990bd79bce8SPatrick Williams                 "#OpenBMCComputerSystem.FirmwareProvisioning";
199150626f4fSJames Feist 
1992a6349918SAppaRao Puli             if (ec)
1993a6349918SAppaRao Puli             {
199462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
1995b99fb1a9SAppaRao Puli                 // not an error, don't have to have the interface
1996539d8c6bSEd Tanous                 oemPFR["ProvisioningStatus"] = open_bmc_computer_system::
1997539d8c6bSEd Tanous                     FirmwareProvisioningStatus::NotProvisioned;
1998a6349918SAppaRao Puli                 return;
1999a6349918SAppaRao Puli             }
2000a6349918SAppaRao Puli 
2001a6349918SAppaRao Puli             const bool* provState = nullptr;
2002a6349918SAppaRao Puli             const bool* lockState = nullptr;
2003bc1d29deSKrzysztof Grobelny 
2004bc1d29deSKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
2005bd79bce8SPatrick Williams                 dbus_utils::UnpackErrorPrinter(), propertiesList,
2006bd79bce8SPatrick Williams                 "UfmProvisioned", provState, "UfmLocked", lockState);
2007bc1d29deSKrzysztof Grobelny 
2008bc1d29deSKrzysztof Grobelny             if (!success)
2009a6349918SAppaRao Puli             {
2010ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
2011bc1d29deSKrzysztof Grobelny                 return;
2012a6349918SAppaRao Puli             }
2013a6349918SAppaRao Puli 
2014a6349918SAppaRao Puli             if ((provState == nullptr) || (lockState == nullptr))
2015a6349918SAppaRao Puli             {
201662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Unable to get PFR attributes.");
2017ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
2018a6349918SAppaRao Puli                 return;
2019a6349918SAppaRao Puli             }
2020a6349918SAppaRao Puli 
202125b54dbaSEd Tanous             if (*provState)
2022a6349918SAppaRao Puli             {
202325b54dbaSEd Tanous                 if (*lockState)
2024a6349918SAppaRao Puli                 {
2025539d8c6bSEd Tanous                     oemPFR["ProvisioningStatus"] = open_bmc_computer_system::
2026539d8c6bSEd Tanous                         FirmwareProvisioningStatus::ProvisionedAndLocked;
2027a6349918SAppaRao Puli                 }
2028a6349918SAppaRao Puli                 else
2029a6349918SAppaRao Puli                 {
2030539d8c6bSEd Tanous                     oemPFR["ProvisioningStatus"] = open_bmc_computer_system::
2031539d8c6bSEd Tanous                         FirmwareProvisioningStatus::ProvisionedButNotLocked;
2032a6349918SAppaRao Puli                 }
2033a6349918SAppaRao Puli             }
2034a6349918SAppaRao Puli             else
2035a6349918SAppaRao Puli             {
2036539d8c6bSEd Tanous                 oemPFR["ProvisioningStatus"] = open_bmc_computer_system::
2037539d8c6bSEd Tanous                     FirmwareProvisioningStatus::NotProvisioned;
2038a6349918SAppaRao Puli             }
2039bc1d29deSKrzysztof Grobelny         });
2040a6349918SAppaRao Puli }
2041a6349918SAppaRao Puli 
2042491d8ee7SSantosh Puranik /**
20436b9ac4f2SChris Cain  * @brief Translate the PowerMode string to enum value
20443a2d0424SChris Cain  *
20456b9ac4f2SChris Cain  * @param[in]  modeString PowerMode string to be translated
20463a2d0424SChris Cain  *
20476b9ac4f2SChris Cain  * @return PowerMode enum
20483a2d0424SChris Cain  */
2049504af5a0SPatrick Williams inline computer_system::PowerMode translatePowerModeString(
2050504af5a0SPatrick Williams     const std::string& modeString)
20513a2d0424SChris Cain {
2052b6655101SChris Cain     using PowerMode = computer_system::PowerMode;
2053b6655101SChris Cain 
20546b9ac4f2SChris Cain     if (modeString == "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static")
20553a2d0424SChris Cain     {
20566b9ac4f2SChris Cain         return PowerMode::Static;
20573a2d0424SChris Cain     }
20586b9ac4f2SChris Cain     if (modeString ==
20590fda0f12SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance")
20603a2d0424SChris Cain     {
20616b9ac4f2SChris Cain         return PowerMode::MaximumPerformance;
20623a2d0424SChris Cain     }
20636b9ac4f2SChris Cain     if (modeString ==
20640fda0f12SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving")
20653a2d0424SChris Cain     {
20666b9ac4f2SChris Cain         return PowerMode::PowerSaving;
2067b6655101SChris Cain     }
20686b9ac4f2SChris Cain     if (modeString ==
2069b6655101SChris Cain         "xyz.openbmc_project.Control.Power.Mode.PowerMode.BalancedPerformance")
2070b6655101SChris Cain     {
20716b9ac4f2SChris Cain         return PowerMode::BalancedPerformance;
2072b6655101SChris Cain     }
20736b9ac4f2SChris Cain     if (modeString ==
2074b6655101SChris Cain         "xyz.openbmc_project.Control.Power.Mode.PowerMode.EfficiencyFavorPerformance")
2075b6655101SChris Cain     {
20766b9ac4f2SChris Cain         return PowerMode::EfficiencyFavorPerformance;
2077b6655101SChris Cain     }
20786b9ac4f2SChris Cain     if (modeString ==
2079b6655101SChris Cain         "xyz.openbmc_project.Control.Power.Mode.PowerMode.EfficiencyFavorPower")
2080b6655101SChris Cain     {
20816b9ac4f2SChris Cain         return PowerMode::EfficiencyFavorPower;
20823a2d0424SChris Cain     }
20836b9ac4f2SChris Cain     if (modeString == "xyz.openbmc_project.Control.Power.Mode.PowerMode.OEM")
20843a2d0424SChris Cain     {
20856b9ac4f2SChris Cain         return PowerMode::OEM;
20866b9ac4f2SChris Cain     }
20876b9ac4f2SChris Cain     // Any other values would be invalid
20886b9ac4f2SChris Cain     BMCWEB_LOG_ERROR("PowerMode value was not valid: {}", modeString);
20896b9ac4f2SChris Cain     return PowerMode::Invalid;
20906b9ac4f2SChris Cain }
20916b9ac4f2SChris Cain 
2092504af5a0SPatrick Williams inline void afterGetPowerMode(
2093504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
20946b9ac4f2SChris Cain     const boost::system::error_code& ec,
20956b9ac4f2SChris Cain     const dbus::utility::DBusPropertiesMap& properties)
20966b9ac4f2SChris Cain {
20976b9ac4f2SChris Cain     if (ec)
20986b9ac4f2SChris Cain     {
20996b9ac4f2SChris Cain         BMCWEB_LOG_ERROR("DBUS response error on PowerMode GetAll: {}", ec);
21006b9ac4f2SChris Cain         messages::internalError(asyncResp->res);
21016b9ac4f2SChris Cain         return;
21026b9ac4f2SChris Cain     }
21036b9ac4f2SChris Cain 
21046b9ac4f2SChris Cain     std::string powerMode;
21056b9ac4f2SChris Cain     const std::vector<std::string>* allowedModes = nullptr;
21066b9ac4f2SChris Cain     const bool success = sdbusplus::unpackPropertiesNoThrow(
21076b9ac4f2SChris Cain         dbus_utils::UnpackErrorPrinter(), properties, "PowerMode", powerMode,
21086b9ac4f2SChris Cain         "AllowedPowerModes", allowedModes);
21096b9ac4f2SChris Cain 
21106b9ac4f2SChris Cain     if (!success)
21116b9ac4f2SChris Cain     {
21126b9ac4f2SChris Cain         messages::internalError(asyncResp->res);
21136b9ac4f2SChris Cain         return;
21146b9ac4f2SChris Cain     }
21156b9ac4f2SChris Cain 
21166b9ac4f2SChris Cain     nlohmann::json::array_t modeList;
21176b9ac4f2SChris Cain     if (allowedModes == nullptr)
21186b9ac4f2SChris Cain     {
21196b9ac4f2SChris Cain         modeList.emplace_back("Static");
21206b9ac4f2SChris Cain         modeList.emplace_back("MaximumPerformance");
21216b9ac4f2SChris Cain         modeList.emplace_back("PowerSaving");
21223a2d0424SChris Cain     }
21233a2d0424SChris Cain     else
21243a2d0424SChris Cain     {
21256b9ac4f2SChris Cain         for (const auto& aMode : *allowedModes)
21266b9ac4f2SChris Cain         {
21276b9ac4f2SChris Cain             computer_system::PowerMode modeValue =
21286b9ac4f2SChris Cain                 translatePowerModeString(aMode);
21296b9ac4f2SChris Cain             if (modeValue == computer_system::PowerMode::Invalid)
21306b9ac4f2SChris Cain             {
2131ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
21326b9ac4f2SChris Cain                 continue;
21336b9ac4f2SChris Cain             }
21346b9ac4f2SChris Cain             modeList.emplace_back(modeValue);
21353a2d0424SChris Cain         }
21363a2d0424SChris Cain     }
21376b9ac4f2SChris Cain     asyncResp->res.jsonValue["PowerMode@Redfish.AllowableValues"] = modeList;
21383a2d0424SChris Cain 
21396b9ac4f2SChris Cain     BMCWEB_LOG_DEBUG("Current power mode: {}", powerMode);
21406b9ac4f2SChris Cain     const computer_system::PowerMode modeValue =
21416b9ac4f2SChris Cain         translatePowerModeString(powerMode);
21426b9ac4f2SChris Cain     if (modeValue == computer_system::PowerMode::Invalid)
21436b9ac4f2SChris Cain     {
21446b9ac4f2SChris Cain         messages::internalError(asyncResp->res);
21456b9ac4f2SChris Cain         return;
21466b9ac4f2SChris Cain     }
21476b9ac4f2SChris Cain     asyncResp->res.jsonValue["PowerMode"] = modeValue;
21486b9ac4f2SChris Cain }
21493a2d0424SChris Cain /**
21503a2d0424SChris Cain  * @brief Retrieves system power mode
21513a2d0424SChris Cain  *
2152ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
21533a2d0424SChris Cain  *
21543a2d0424SChris Cain  * @return None.
21553a2d0424SChris Cain  */
2156ac106bf6SEd Tanous inline void getPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
21573a2d0424SChris Cain {
215862598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get power mode.");
21593a2d0424SChris Cain 
21603a2d0424SChris Cain     // Get Power Mode object path:
2161e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2162e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode"};
2163e99073f5SGeorge Liu     dbus::utility::getSubTree(
2164e99073f5SGeorge Liu         "/", 0, interfaces,
2165ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
2166b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
21673a2d0424SChris Cain             if (ec)
21683a2d0424SChris Cain             {
2169bd79bce8SPatrick Williams                 BMCWEB_LOG_DEBUG(
2170bd79bce8SPatrick Williams                     "DBUS response error on Power.Mode GetSubTree {}", ec);
21713a2d0424SChris Cain                 // This is an optional D-Bus object so just return if
21723a2d0424SChris Cain                 // error occurs
21733a2d0424SChris Cain                 return;
21743a2d0424SChris Cain             }
21753a2d0424SChris Cain             if (subtree.empty())
21763a2d0424SChris Cain             {
21773a2d0424SChris Cain                 // As noted above, this is an optional interface so just return
21783a2d0424SChris Cain                 // if there is no instance found
21793a2d0424SChris Cain                 return;
21803a2d0424SChris Cain             }
21813a2d0424SChris Cain             if (subtree.size() > 1)
21823a2d0424SChris Cain             {
21833a2d0424SChris Cain                 // More then one PowerMode object is not supported and is an
21843a2d0424SChris Cain                 // error
218562598e31SEd Tanous                 BMCWEB_LOG_DEBUG(
218662598e31SEd Tanous                     "Found more than 1 system D-Bus Power.Mode objects: {}",
218762598e31SEd Tanous                     subtree.size());
2188ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
21893a2d0424SChris Cain                 return;
21903a2d0424SChris Cain             }
21913a2d0424SChris Cain             if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
21923a2d0424SChris Cain             {
219362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.Mode mapper error!");
2194ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
21953a2d0424SChris Cain                 return;
21963a2d0424SChris Cain             }
21973a2d0424SChris Cain             const std::string& path = subtree[0].first;
21983a2d0424SChris Cain             const std::string& service = subtree[0].second.begin()->first;
21993a2d0424SChris Cain             if (service.empty())
22003a2d0424SChris Cain             {
220162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.Mode service mapper error!");
2202ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
22033a2d0424SChris Cain                 return;
22043a2d0424SChris Cain             }
22056b9ac4f2SChris Cain 
22066b9ac4f2SChris Cain             // Valid Power Mode object found, now read the mode properties
2207deae6a78SEd Tanous             dbus::utility::getAllProperties(
22081e1e598dSJonathan Doman                 *crow::connections::systemBus, service, path,
22096b9ac4f2SChris Cain                 "xyz.openbmc_project.Control.Power.Mode",
2210bd79bce8SPatrick Williams                 [asyncResp](
2211bd79bce8SPatrick Williams                     const boost::system::error_code& ec2,
22126b9ac4f2SChris Cain                     const dbus::utility::DBusPropertiesMap& properties) {
22136b9ac4f2SChris Cain                     afterGetPowerMode(asyncResp, ec2, properties);
22141e1e598dSJonathan Doman                 });
2215e99073f5SGeorge Liu         });
22163a2d0424SChris Cain }
22173a2d0424SChris Cain 
22183a2d0424SChris Cain /**
22193a2d0424SChris Cain  * @brief Validate the specified mode is valid and return the PowerMode
22203a2d0424SChris Cain  * name associated with that string
22213a2d0424SChris Cain  *
2222ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
2223b6655101SChris Cain  * @param[in] modeValue   String representing the desired PowerMode
22243a2d0424SChris Cain  *
22253a2d0424SChris Cain  * @return PowerMode value or empty string if mode is not valid
22263a2d0424SChris Cain  */
2227504af5a0SPatrick Williams inline std::string validatePowerMode(
2228504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2229b6655101SChris Cain     const nlohmann::json& modeValue)
22303a2d0424SChris Cain {
2231b6655101SChris Cain     using PowerMode = computer_system::PowerMode;
22323a2d0424SChris Cain     std::string mode;
22333a2d0424SChris Cain 
2234b6655101SChris Cain     if (modeValue == PowerMode::Static)
22353a2d0424SChris Cain     {
22363a2d0424SChris Cain         mode = "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static";
22373a2d0424SChris Cain     }
2238b6655101SChris Cain     else if (modeValue == PowerMode::MaximumPerformance)
22393a2d0424SChris Cain     {
22400fda0f12SGeorge Liu         mode =
22410fda0f12SGeorge Liu             "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance";
22423a2d0424SChris Cain     }
2243b6655101SChris Cain     else if (modeValue == PowerMode::PowerSaving)
22443a2d0424SChris Cain     {
22453a2d0424SChris Cain         mode = "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving";
22463a2d0424SChris Cain     }
2247b6655101SChris Cain     else if (modeValue == PowerMode::BalancedPerformance)
2248b6655101SChris Cain     {
2249b6655101SChris Cain         mode =
2250b6655101SChris Cain             "xyz.openbmc_project.Control.Power.Mode.PowerMode.BalancedPerformance";
2251b6655101SChris Cain     }
2252b6655101SChris Cain     else if (modeValue == PowerMode::EfficiencyFavorPerformance)
2253b6655101SChris Cain     {
2254b6655101SChris Cain         mode =
2255b6655101SChris Cain             "xyz.openbmc_project.Control.Power.Mode.PowerMode.EfficiencyFavorPerformance";
2256b6655101SChris Cain     }
2257b6655101SChris Cain     else if (modeValue == PowerMode::EfficiencyFavorPower)
2258b6655101SChris Cain     {
2259b6655101SChris Cain         mode =
2260b6655101SChris Cain             "xyz.openbmc_project.Control.Power.Mode.PowerMode.EfficiencyFavorPower";
2261b6655101SChris Cain     }
22623a2d0424SChris Cain     else
22633a2d0424SChris Cain     {
2264b6655101SChris Cain         messages::propertyValueNotInList(asyncResp->res, modeValue.dump(),
2265ac106bf6SEd Tanous                                          "PowerMode");
22663a2d0424SChris Cain     }
22673a2d0424SChris Cain     return mode;
22683a2d0424SChris Cain }
22693a2d0424SChris Cain 
22703a2d0424SChris Cain /**
22713a2d0424SChris Cain  * @brief Sets system power mode.
22723a2d0424SChris Cain  *
2273ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
22743a2d0424SChris Cain  * @param[in] pmode   System power mode from request.
22753a2d0424SChris Cain  *
22763a2d0424SChris Cain  * @return None.
22773a2d0424SChris Cain  */
2278ac106bf6SEd Tanous inline void setPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22793a2d0424SChris Cain                          const std::string& pmode)
22803a2d0424SChris Cain {
228162598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set power mode.");
22823a2d0424SChris Cain 
2283ac106bf6SEd Tanous     std::string powerMode = validatePowerMode(asyncResp, pmode);
22843a2d0424SChris Cain     if (powerMode.empty())
22853a2d0424SChris Cain     {
22863a2d0424SChris Cain         return;
22873a2d0424SChris Cain     }
22883a2d0424SChris Cain 
22893a2d0424SChris Cain     // Get Power Mode object path:
2290e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2291e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode"};
2292e99073f5SGeorge Liu     dbus::utility::getSubTree(
2293e99073f5SGeorge Liu         "/", 0, interfaces,
2294ac106bf6SEd Tanous         [asyncResp,
2295e99073f5SGeorge Liu          powerMode](const boost::system::error_code& ec,
2296b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
22973a2d0424SChris Cain             if (ec)
22983a2d0424SChris Cain             {
2299bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR(
2300bd79bce8SPatrick Williams                     "DBUS response error on Power.Mode GetSubTree {}", ec);
23013a2d0424SChris Cain                 // This is an optional D-Bus object, but user attempted to patch
2302ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
23033a2d0424SChris Cain                 return;
23043a2d0424SChris Cain             }
23053a2d0424SChris Cain             if (subtree.empty())
23063a2d0424SChris Cain             {
23073a2d0424SChris Cain                 // This is an optional D-Bus object, but user attempted to patch
2308ac106bf6SEd Tanous                 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
23093a2d0424SChris Cain                                            "PowerMode");
23103a2d0424SChris Cain                 return;
23113a2d0424SChris Cain             }
23123a2d0424SChris Cain             if (subtree.size() > 1)
23133a2d0424SChris Cain             {
23143a2d0424SChris Cain                 // More then one PowerMode object is not supported and is an
23153a2d0424SChris Cain                 // error
231662598e31SEd Tanous                 BMCWEB_LOG_DEBUG(
231762598e31SEd Tanous                     "Found more than 1 system D-Bus Power.Mode objects: {}",
231862598e31SEd Tanous                     subtree.size());
2319ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
23203a2d0424SChris Cain                 return;
23213a2d0424SChris Cain             }
23223a2d0424SChris Cain             if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
23233a2d0424SChris Cain             {
232462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.Mode mapper error!");
2325ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
23263a2d0424SChris Cain                 return;
23273a2d0424SChris Cain             }
23283a2d0424SChris Cain             const std::string& path = subtree[0].first;
23293a2d0424SChris Cain             const std::string& service = subtree[0].second.begin()->first;
23303a2d0424SChris Cain             if (service.empty())
23313a2d0424SChris Cain             {
233262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.Mode service mapper error!");
2333ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
23343a2d0424SChris Cain                 return;
23353a2d0424SChris Cain             }
23363a2d0424SChris Cain 
233762598e31SEd Tanous             BMCWEB_LOG_DEBUG("Setting power mode({}) -> {}", powerMode, path);
23383a2d0424SChris Cain 
23393a2d0424SChris Cain             // Set the Power Mode property
2340e93abac6SGinu George             setDbusProperty(asyncResp, "PowerMode", service, path,
2341bd79bce8SPatrick Williams                             "xyz.openbmc_project.Control.Power.Mode",
2342bd79bce8SPatrick Williams                             "PowerMode", powerMode);
2343e99073f5SGeorge Liu         });
23443a2d0424SChris Cain }
23453a2d0424SChris Cain 
23463a2d0424SChris Cain /**
234751709ffdSYong Li  * @brief Translates watchdog timeout action DBUS property value to redfish.
234851709ffdSYong Li  *
234951709ffdSYong Li  * @param[in] dbusAction    The watchdog timeout action in D-BUS.
235051709ffdSYong Li  *
235151709ffdSYong Li  * @return Returns as a string, the timeout action in Redfish terms. If
235251709ffdSYong Li  * translation cannot be done, returns an empty string.
235351709ffdSYong Li  */
235423a21a1cSEd Tanous inline std::string dbusToRfWatchdogAction(const std::string& dbusAction)
235551709ffdSYong Li {
235651709ffdSYong Li     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.None")
235751709ffdSYong Li     {
235851709ffdSYong Li         return "None";
235951709ffdSYong Li     }
23603174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.HardReset")
236151709ffdSYong Li     {
236251709ffdSYong Li         return "ResetSystem";
236351709ffdSYong Li     }
23643174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerOff")
236551709ffdSYong Li     {
236651709ffdSYong Li         return "PowerDown";
236751709ffdSYong Li     }
23683174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerCycle")
236951709ffdSYong Li     {
237051709ffdSYong Li         return "PowerCycle";
237151709ffdSYong Li     }
237251709ffdSYong Li 
237351709ffdSYong Li     return "";
237451709ffdSYong Li }
237551709ffdSYong Li 
237651709ffdSYong Li /**
2377c45f0082SYong Li  *@brief Translates timeout action from Redfish to DBUS property value.
2378c45f0082SYong Li  *
2379c45f0082SYong Li  *@param[in] rfAction The timeout action in Redfish.
2380c45f0082SYong Li  *
2381c45f0082SYong Li  *@return Returns as a string, the time_out action as expected by DBUS.
2382c45f0082SYong Li  *If translation cannot be done, returns an empty string.
2383c45f0082SYong Li  */
2384c45f0082SYong Li 
238523a21a1cSEd Tanous inline std::string rfToDbusWDTTimeOutAct(const std::string& rfAction)
2386c45f0082SYong Li {
2387c45f0082SYong Li     if (rfAction == "None")
2388c45f0082SYong Li     {
2389c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.None";
2390c45f0082SYong Li     }
23913174e4dfSEd Tanous     if (rfAction == "PowerCycle")
2392c45f0082SYong Li     {
2393c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerCycle";
2394c45f0082SYong Li     }
23953174e4dfSEd Tanous     if (rfAction == "PowerDown")
2396c45f0082SYong Li     {
2397c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerOff";
2398c45f0082SYong Li     }
23993174e4dfSEd Tanous     if (rfAction == "ResetSystem")
2400c45f0082SYong Li     {
2401c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.HardReset";
2402c45f0082SYong Li     }
2403c45f0082SYong Li 
2404c45f0082SYong Li     return "";
2405c45f0082SYong Li }
2406c45f0082SYong Li 
2407c45f0082SYong Li /**
240851709ffdSYong Li  * @brief Retrieves host watchdog timer properties over DBUS
240951709ffdSYong Li  *
2410ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
241151709ffdSYong Li  *
241251709ffdSYong Li  * @return None.
241351709ffdSYong Li  */
2414504af5a0SPatrick Williams inline void getHostWatchdogTimer(
2415504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
241651709ffdSYong Li {
241762598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get host watchodg");
2418deae6a78SEd Tanous     dbus::utility::getAllProperties(
2419deae6a78SEd Tanous         "xyz.openbmc_project.Watchdog", "/xyz/openbmc_project/watchdog/host0",
2420bc1d29deSKrzysztof Grobelny         "xyz.openbmc_project.State.Watchdog",
2421ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
2422b9d36b47SEd Tanous                     const dbus::utility::DBusPropertiesMap& properties) {
242351709ffdSYong Li             if (ec)
242451709ffdSYong Li             {
242551709ffdSYong Li                 // watchdog service is stopped
242662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
242751709ffdSYong Li                 return;
242851709ffdSYong Li             }
242951709ffdSYong Li 
243062598e31SEd Tanous             BMCWEB_LOG_DEBUG("Got {} wdt prop.", properties.size());
243151709ffdSYong Li 
243251709ffdSYong Li             nlohmann::json& hostWatchdogTimer =
2433ac106bf6SEd Tanous                 asyncResp->res.jsonValue["HostWatchdogTimer"];
243451709ffdSYong Li 
243551709ffdSYong Li             // watchdog service is running/enabled
2436539d8c6bSEd Tanous             hostWatchdogTimer["Status"]["State"] = resource::State::Enabled;
243751709ffdSYong Li 
2438bc1d29deSKrzysztof Grobelny             const bool* enabled = nullptr;
2439bc1d29deSKrzysztof Grobelny             const std::string* expireAction = nullptr;
244051709ffdSYong Li 
2441bc1d29deSKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
2442bd79bce8SPatrick Williams                 dbus_utils::UnpackErrorPrinter(), properties, "Enabled",
2443bd79bce8SPatrick Williams                 enabled, "ExpireAction", expireAction);
2444bc1d29deSKrzysztof Grobelny 
2445bc1d29deSKrzysztof Grobelny             if (!success)
244651709ffdSYong Li             {
2447ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
2448601af5edSChicago Duan                 return;
244951709ffdSYong Li             }
245051709ffdSYong Li 
2451bc1d29deSKrzysztof Grobelny             if (enabled != nullptr)
245251709ffdSYong Li             {
2453bc1d29deSKrzysztof Grobelny                 hostWatchdogTimer["FunctionEnabled"] = *enabled;
245451709ffdSYong Li             }
245551709ffdSYong Li 
2456bc1d29deSKrzysztof Grobelny             if (expireAction != nullptr)
2457bc1d29deSKrzysztof Grobelny             {
2458bc1d29deSKrzysztof Grobelny                 std::string action = dbusToRfWatchdogAction(*expireAction);
245951709ffdSYong Li                 if (action.empty())
246051709ffdSYong Li                 {
2461ac106bf6SEd Tanous                     messages::internalError(asyncResp->res);
2462601af5edSChicago Duan                     return;
246351709ffdSYong Li                 }
246451709ffdSYong Li                 hostWatchdogTimer["TimeoutAction"] = action;
246551709ffdSYong Li             }
2466bc1d29deSKrzysztof Grobelny         });
246751709ffdSYong Li }
246851709ffdSYong Li 
246951709ffdSYong Li /**
2470c45f0082SYong Li  * @brief Sets Host WatchDog Timer properties.
2471c45f0082SYong Li  *
2472ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
2473c45f0082SYong Li  * @param[in] wdtEnable  The WDTimer Enable value (true/false) from incoming
2474c45f0082SYong Li  *                       RF request.
2475c45f0082SYong Li  * @param[in] wdtTimeOutAction The WDT Timeout action, from incoming RF request.
2476c45f0082SYong Li  *
2477c45f0082SYong Li  * @return None.
2478c45f0082SYong Li  */
2479504af5a0SPatrick Williams inline void setWDTProperties(
2480504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2481c45f0082SYong Li     const std::optional<bool> wdtEnable,
2482c45f0082SYong Li     const std::optional<std::string>& wdtTimeOutAction)
2483c45f0082SYong Li {
248462598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set host watchdog");
2485c45f0082SYong Li 
2486c45f0082SYong Li     if (wdtTimeOutAction)
2487c45f0082SYong Li     {
2488c45f0082SYong Li         std::string wdtTimeOutActStr = rfToDbusWDTTimeOutAct(*wdtTimeOutAction);
2489c45f0082SYong Li         // check if TimeOut Action is Valid
2490c45f0082SYong Li         if (wdtTimeOutActStr.empty())
2491c45f0082SYong Li         {
249262598e31SEd Tanous             BMCWEB_LOG_DEBUG("Unsupported value for TimeoutAction: {}",
249362598e31SEd Tanous                              *wdtTimeOutAction);
2494ac106bf6SEd Tanous             messages::propertyValueNotInList(asyncResp->res, *wdtTimeOutAction,
2495c45f0082SYong Li                                              "TimeoutAction");
2496c45f0082SYong Li             return;
2497c45f0082SYong Li         }
2498c45f0082SYong Li 
2499e93abac6SGinu George         setDbusProperty(asyncResp, "HostWatchdogTimer/TimeoutAction",
2500e93abac6SGinu George                         "xyz.openbmc_project.Watchdog",
250187c44966SAsmitha Karunanithi                         sdbusplus::message::object_path(
250287c44966SAsmitha Karunanithi                             "/xyz/openbmc_project/watchdog/host0"),
25039ae226faSGeorge Liu                         "xyz.openbmc_project.State.Watchdog", "ExpireAction",
2504e93abac6SGinu George                         wdtTimeOutActStr);
2505c45f0082SYong Li     }
2506c45f0082SYong Li 
2507c45f0082SYong Li     if (wdtEnable)
2508c45f0082SYong Li     {
2509e93abac6SGinu George         setDbusProperty(asyncResp, "HostWatchdogTimer/FunctionEnabled",
2510e93abac6SGinu George                         "xyz.openbmc_project.Watchdog",
251187c44966SAsmitha Karunanithi                         sdbusplus::message::object_path(
251287c44966SAsmitha Karunanithi                             "/xyz/openbmc_project/watchdog/host0"),
251387c44966SAsmitha Karunanithi                         "xyz.openbmc_project.State.Watchdog", "Enabled",
2514e93abac6SGinu George                         *wdtEnable);
2515c45f0082SYong Li     }
2516c45f0082SYong Li }
2517c45f0082SYong Li 
251837bbf98cSChris Cain /**
251937bbf98cSChris Cain  * @brief Parse the Idle Power Saver properties into json
252037bbf98cSChris Cain  *
2521ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for completing asynchronous calls.
252237bbf98cSChris Cain  * @param[in] properties  IPS property data from DBus.
252337bbf98cSChris Cain  *
252437bbf98cSChris Cain  * @return true if successful
252537bbf98cSChris Cain  */
2526504af5a0SPatrick Williams inline bool parseIpsProperties(
2527504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25281e5b7c88SJiaqing Zhao     const dbus::utility::DBusPropertiesMap& properties)
252937bbf98cSChris Cain {
2530bc1d29deSKrzysztof Grobelny     const bool* enabled = nullptr;
2531bc1d29deSKrzysztof Grobelny     const uint8_t* enterUtilizationPercent = nullptr;
2532bc1d29deSKrzysztof Grobelny     const uint64_t* enterDwellTime = nullptr;
2533bc1d29deSKrzysztof Grobelny     const uint8_t* exitUtilizationPercent = nullptr;
2534bc1d29deSKrzysztof Grobelny     const uint64_t* exitDwellTime = nullptr;
2535bc1d29deSKrzysztof Grobelny 
2536bc1d29deSKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
2537bc1d29deSKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), properties, "Enabled", enabled,
25382661b72cSChris Cain         "EnterUtilizationPercent", enterUtilizationPercent, "EnterDwellTime",
25392661b72cSChris Cain         enterDwellTime, "ExitUtilizationPercent", exitUtilizationPercent,
25402661b72cSChris Cain         "ExitDwellTime", exitDwellTime);
2541bc1d29deSKrzysztof Grobelny 
2542bc1d29deSKrzysztof Grobelny     if (!success)
254337bbf98cSChris Cain     {
254437bbf98cSChris Cain         return false;
254537bbf98cSChris Cain     }
2546bc1d29deSKrzysztof Grobelny 
2547bc1d29deSKrzysztof Grobelny     if (enabled != nullptr)
254837bbf98cSChris Cain     {
2549ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["Enabled"] = *enabled;
255037bbf98cSChris Cain     }
2551bc1d29deSKrzysztof Grobelny 
2552bc1d29deSKrzysztof Grobelny     if (enterUtilizationPercent != nullptr)
255337bbf98cSChris Cain     {
2554ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["EnterUtilizationPercent"] =
2555bc1d29deSKrzysztof Grobelny             *enterUtilizationPercent;
255637bbf98cSChris Cain     }
2557bc1d29deSKrzysztof Grobelny 
2558bc1d29deSKrzysztof Grobelny     if (enterDwellTime != nullptr)
2559bc1d29deSKrzysztof Grobelny     {
2560bc1d29deSKrzysztof Grobelny         const std::chrono::duration<uint64_t, std::milli> ms(*enterDwellTime);
2561ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["EnterDwellTimeSeconds"] =
256237bbf98cSChris Cain             std::chrono::duration_cast<std::chrono::duration<uint64_t>>(ms)
256337bbf98cSChris Cain                 .count();
256437bbf98cSChris Cain     }
2565bc1d29deSKrzysztof Grobelny 
2566bc1d29deSKrzysztof Grobelny     if (exitUtilizationPercent != nullptr)
256737bbf98cSChris Cain     {
2568ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["ExitUtilizationPercent"] =
2569bc1d29deSKrzysztof Grobelny             *exitUtilizationPercent;
257037bbf98cSChris Cain     }
2571bc1d29deSKrzysztof Grobelny 
2572bc1d29deSKrzysztof Grobelny     if (exitDwellTime != nullptr)
257337bbf98cSChris Cain     {
2574bc1d29deSKrzysztof Grobelny         const std::chrono::duration<uint64_t, std::milli> ms(*exitDwellTime);
2575ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["ExitDwellTimeSeconds"] =
257637bbf98cSChris Cain             std::chrono::duration_cast<std::chrono::duration<uint64_t>>(ms)
257737bbf98cSChris Cain                 .count();
257837bbf98cSChris Cain     }
257937bbf98cSChris Cain 
258037bbf98cSChris Cain     return true;
258137bbf98cSChris Cain }
258237bbf98cSChris Cain 
258337bbf98cSChris Cain /**
258437bbf98cSChris Cain  * @brief Retrieves host watchdog timer properties over DBUS
258537bbf98cSChris Cain  *
2586ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
258737bbf98cSChris Cain  *
258837bbf98cSChris Cain  * @return None.
258937bbf98cSChris Cain  */
2590504af5a0SPatrick Williams inline void getIdlePowerSaver(
2591504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
259237bbf98cSChris Cain {
259362598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get idle power saver parameters");
259437bbf98cSChris Cain 
259537bbf98cSChris Cain     // Get IdlePowerSaver object path:
2596e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2597e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.IdlePowerSaver"};
2598e99073f5SGeorge Liu     dbus::utility::getSubTree(
2599e99073f5SGeorge Liu         "/", 0, interfaces,
2600ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
2601b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
260237bbf98cSChris Cain             if (ec)
260337bbf98cSChris Cain             {
2604b3e86cb0SGunnar Mills                 BMCWEB_LOG_ERROR(
260562598e31SEd Tanous                     "DBUS response error on Power.IdlePowerSaver GetSubTree {}",
260662598e31SEd Tanous                     ec);
2607ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
260837bbf98cSChris Cain                 return;
260937bbf98cSChris Cain             }
261037bbf98cSChris Cain             if (subtree.empty())
261137bbf98cSChris Cain             {
261237bbf98cSChris Cain                 // This is an optional interface so just return
261337bbf98cSChris Cain                 // if there is no instance found
261462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("No instances found");
261537bbf98cSChris Cain                 return;
261637bbf98cSChris Cain             }
261737bbf98cSChris Cain             if (subtree.size() > 1)
261837bbf98cSChris Cain             {
261937bbf98cSChris Cain                 // More then one PowerIdlePowerSaver object is not supported and
262037bbf98cSChris Cain                 // is an error
262162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Found more than 1 system D-Bus "
262262598e31SEd Tanous                                  "Power.IdlePowerSaver objects: {}",
262362598e31SEd Tanous                                  subtree.size());
2624ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
262537bbf98cSChris Cain                 return;
262637bbf98cSChris Cain             }
262737bbf98cSChris Cain             if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
262837bbf98cSChris Cain             {
262962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.IdlePowerSaver mapper error!");
2630ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
263137bbf98cSChris Cain                 return;
263237bbf98cSChris Cain             }
263337bbf98cSChris Cain             const std::string& path = subtree[0].first;
263437bbf98cSChris Cain             const std::string& service = subtree[0].second.begin()->first;
263537bbf98cSChris Cain             if (service.empty())
263637bbf98cSChris Cain             {
263762598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.IdlePowerSaver service mapper error!");
2638ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
263937bbf98cSChris Cain                 return;
264037bbf98cSChris Cain             }
264137bbf98cSChris Cain 
264237bbf98cSChris Cain             // Valid IdlePowerSaver object found, now read the current values
2643deae6a78SEd Tanous             dbus::utility::getAllProperties(
2644bc1d29deSKrzysztof Grobelny                 *crow::connections::systemBus, service, path,
2645bc1d29deSKrzysztof Grobelny                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2646bd79bce8SPatrick Williams                 [asyncResp](
2647bd79bce8SPatrick Williams                     const boost::system::error_code& ec2,
26481e5b7c88SJiaqing Zhao                     const dbus::utility::DBusPropertiesMap& properties) {
26498a592810SEd Tanous                     if (ec2)
265037bbf98cSChris Cain                     {
265162598e31SEd Tanous                         BMCWEB_LOG_ERROR(
2652bd79bce8SPatrick Williams                             "DBUS response error on IdlePowerSaver GetAll: {}",
2653bd79bce8SPatrick Williams                             ec2);
2654ac106bf6SEd Tanous                         messages::internalError(asyncResp->res);
265537bbf98cSChris Cain                         return;
265637bbf98cSChris Cain                     }
265737bbf98cSChris Cain 
2658ac106bf6SEd Tanous                     if (!parseIpsProperties(asyncResp, properties))
265937bbf98cSChris Cain                     {
2660ac106bf6SEd Tanous                         messages::internalError(asyncResp->res);
266137bbf98cSChris Cain                         return;
266237bbf98cSChris Cain                     }
2663bc1d29deSKrzysztof Grobelny                 });
2664e99073f5SGeorge Liu         });
266537bbf98cSChris Cain 
266662598e31SEd Tanous     BMCWEB_LOG_DEBUG("EXIT: Get idle power saver parameters");
266737bbf98cSChris Cain }
266837bbf98cSChris Cain 
266937bbf98cSChris Cain /**
267037bbf98cSChris Cain  * @brief Sets Idle Power Saver properties.
267137bbf98cSChris Cain  *
2672ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
267337bbf98cSChris Cain  * @param[in] ipsEnable  The IPS Enable value (true/false) from incoming
267437bbf98cSChris Cain  *                       RF request.
267537bbf98cSChris Cain  * @param[in] ipsEnterUtil The utilization limit to enter idle state.
267637bbf98cSChris Cain  * @param[in] ipsEnterTime The time the utilization must be below ipsEnterUtil
267737bbf98cSChris Cain  * before entering idle state.
267837bbf98cSChris Cain  * @param[in] ipsExitUtil The utilization limit when exiting idle state.
267937bbf98cSChris Cain  * @param[in] ipsExitTime The time the utilization must be above ipsExutUtil
268037bbf98cSChris Cain  * before exiting idle state
268137bbf98cSChris Cain  *
268237bbf98cSChris Cain  * @return None.
268337bbf98cSChris Cain  */
2684bd79bce8SPatrick Williams inline void setIdlePowerSaver(
2685bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
268637bbf98cSChris Cain     const std::optional<bool> ipsEnable,
268737bbf98cSChris Cain     const std::optional<uint8_t> ipsEnterUtil,
268837bbf98cSChris Cain     const std::optional<uint64_t> ipsEnterTime,
268937bbf98cSChris Cain     const std::optional<uint8_t> ipsExitUtil,
269037bbf98cSChris Cain     const std::optional<uint64_t> ipsExitTime)
269137bbf98cSChris Cain {
269262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set idle power saver properties");
269337bbf98cSChris Cain 
269437bbf98cSChris Cain     // Get IdlePowerSaver object path:
2695e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2696e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.IdlePowerSaver"};
2697e99073f5SGeorge Liu     dbus::utility::getSubTree(
2698e99073f5SGeorge Liu         "/", 0, interfaces,
2699ac106bf6SEd Tanous         [asyncResp, ipsEnable, ipsEnterUtil, ipsEnterTime, ipsExitUtil,
2700e99073f5SGeorge Liu          ipsExitTime](const boost::system::error_code& ec,
2701b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
270237bbf98cSChris Cain             if (ec)
270337bbf98cSChris Cain             {
2704b3e86cb0SGunnar Mills                 BMCWEB_LOG_ERROR(
270562598e31SEd Tanous                     "DBUS response error on Power.IdlePowerSaver GetSubTree {}",
270662598e31SEd Tanous                     ec);
2707ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
270837bbf98cSChris Cain                 return;
270937bbf98cSChris Cain             }
271037bbf98cSChris Cain             if (subtree.empty())
271137bbf98cSChris Cain             {
271237bbf98cSChris Cain                 // This is an optional D-Bus object, but user attempted to patch
2713ac106bf6SEd Tanous                 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
271437bbf98cSChris Cain                                            "IdlePowerSaver");
271537bbf98cSChris Cain                 return;
271637bbf98cSChris Cain             }
271737bbf98cSChris Cain             if (subtree.size() > 1)
271837bbf98cSChris Cain             {
271937bbf98cSChris Cain                 // More then one PowerIdlePowerSaver object is not supported and
272037bbf98cSChris Cain                 // is an error
272162598e31SEd Tanous                 BMCWEB_LOG_DEBUG(
272262598e31SEd Tanous                     "Found more than 1 system D-Bus Power.IdlePowerSaver objects: {}",
272362598e31SEd Tanous                     subtree.size());
2724ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
272537bbf98cSChris Cain                 return;
272637bbf98cSChris Cain             }
272737bbf98cSChris Cain             if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
272837bbf98cSChris Cain             {
272962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.IdlePowerSaver mapper error!");
2730ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
273137bbf98cSChris Cain                 return;
273237bbf98cSChris Cain             }
273337bbf98cSChris Cain             const std::string& path = subtree[0].first;
273437bbf98cSChris Cain             const std::string& service = subtree[0].second.begin()->first;
273537bbf98cSChris Cain             if (service.empty())
273637bbf98cSChris Cain             {
273762598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power.IdlePowerSaver service mapper error!");
2738ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
273937bbf98cSChris Cain                 return;
274037bbf98cSChris Cain             }
274137bbf98cSChris Cain 
274237bbf98cSChris Cain             // Valid Power IdlePowerSaver object found, now set any values that
274337bbf98cSChris Cain             // need to be updated
274437bbf98cSChris Cain 
274537bbf98cSChris Cain             if (ipsEnable)
274637bbf98cSChris Cain             {
2747bd79bce8SPatrick Williams                 setDbusProperty(
2748bd79bce8SPatrick Williams                     asyncResp, "IdlePowerSaver/Enabled", service, path,
274987c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2750e93abac6SGinu George                     "Enabled", *ipsEnable);
275137bbf98cSChris Cain             }
275237bbf98cSChris Cain             if (ipsEnterUtil)
275337bbf98cSChris Cain             {
2754bd79bce8SPatrick Williams                 setDbusProperty(
2755bd79bce8SPatrick Williams                     asyncResp, "IdlePowerSaver/EnterUtilizationPercent",
2756e93abac6SGinu George                     service, path,
27579ae226faSGeorge Liu                     "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2758e93abac6SGinu George                     "EnterUtilizationPercent", *ipsEnterUtil);
275937bbf98cSChris Cain             }
276037bbf98cSChris Cain             if (ipsEnterTime)
276137bbf98cSChris Cain             {
276237bbf98cSChris Cain                 // Convert from seconds into milliseconds for DBus
276337bbf98cSChris Cain                 const uint64_t timeMilliseconds = *ipsEnterTime * 1000;
2764bd79bce8SPatrick Williams                 setDbusProperty(
2765bd79bce8SPatrick Williams                     asyncResp, "IdlePowerSaver/EnterDwellTimeSeconds", service,
2766bd79bce8SPatrick Williams                     path, "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2767e93abac6SGinu George                     "EnterDwellTime", timeMilliseconds);
276837bbf98cSChris Cain             }
276937bbf98cSChris Cain             if (ipsExitUtil)
277037bbf98cSChris Cain             {
2771bd79bce8SPatrick Williams                 setDbusProperty(
2772bd79bce8SPatrick Williams                     asyncResp, "IdlePowerSaver/ExitUtilizationPercent", service,
2773bd79bce8SPatrick Williams                     path, "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2774e93abac6SGinu George                     "ExitUtilizationPercent", *ipsExitUtil);
277537bbf98cSChris Cain             }
277637bbf98cSChris Cain             if (ipsExitTime)
277737bbf98cSChris Cain             {
277837bbf98cSChris Cain                 // Convert from seconds into milliseconds for DBus
277937bbf98cSChris Cain                 const uint64_t timeMilliseconds = *ipsExitTime * 1000;
2780bd79bce8SPatrick Williams                 setDbusProperty(
2781bd79bce8SPatrick Williams                     asyncResp, "IdlePowerSaver/ExitDwellTimeSeconds", service,
2782bd79bce8SPatrick Williams                     path, "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2783e93abac6SGinu George                     "ExitDwellTime", timeMilliseconds);
278437bbf98cSChris Cain             }
2785e99073f5SGeorge Liu         });
278637bbf98cSChris Cain 
278762598e31SEd Tanous     BMCWEB_LOG_DEBUG("EXIT: Set idle power saver parameters");
278837bbf98cSChris Cain }
278937bbf98cSChris Cain 
2790c1e219d5SEd Tanous inline void handleComputerSystemCollectionHead(
2791dd60b9edSEd Tanous     crow::App& app, const crow::Request& req,
2792dd60b9edSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2793dd60b9edSEd Tanous {
2794dd60b9edSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2795dd60b9edSEd Tanous     {
2796dd60b9edSEd Tanous         return;
2797dd60b9edSEd Tanous     }
2798dd60b9edSEd Tanous     asyncResp->res.addHeader(
2799dd60b9edSEd Tanous         boost::beast::http::field::link,
2800dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystemCollection/ComputerSystemCollection.json>; rel=describedby");
2801dd60b9edSEd Tanous }
2802dd60b9edSEd Tanous 
2803c1e219d5SEd Tanous inline void handleComputerSystemCollectionGet(
2804c1e219d5SEd Tanous     crow::App& app, const crow::Request& req,
2805c1e219d5SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
28061abe55efSEd Tanous {
28073ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2808f4c99e70SEd Tanous     {
2809f4c99e70SEd Tanous         return;
2810f4c99e70SEd Tanous     }
2811dd60b9edSEd Tanous 
2812dd60b9edSEd Tanous     asyncResp->res.addHeader(
2813dd60b9edSEd Tanous         boost::beast::http::field::link,
2814dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystemCollection.json>; rel=describedby");
28158d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
28160f74e643SEd Tanous         "#ComputerSystemCollection.ComputerSystemCollection";
28178d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Systems";
28188d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Computer System Collection";
2819462023adSSunitha Harish 
2820fc5ae94dSOliver Brewka     getSystemCollectionMembers(asyncResp);
2821c1e219d5SEd Tanous }
2822c1e219d5SEd Tanous 
2823c1e219d5SEd Tanous /**
28247e860f15SJohn Edward Broadbent  * Function transceives data with dbus directly.
28257e860f15SJohn Edward Broadbent  */
28264f48d5f6SEd Tanous inline void doNMI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
28277e860f15SJohn Edward Broadbent {
282889492a15SPatrick Williams     constexpr const char* serviceName = "xyz.openbmc_project.Control.Host.NMI";
282989492a15SPatrick Williams     constexpr const char* objectPath = "/xyz/openbmc_project/control/host0/nmi";
283089492a15SPatrick Williams     constexpr const char* interfaceName =
28317e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Control.Host.NMI";
283289492a15SPatrick Williams     constexpr const char* method = "NMI";
28337e860f15SJohn Edward Broadbent 
2834177612aaSEd Tanous     dbus::utility::async_method_call(
2835177612aaSEd Tanous         asyncResp,
28365e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
28377e860f15SJohn Edward Broadbent             if (ec)
28387e860f15SJohn Edward Broadbent             {
283962598e31SEd Tanous                 BMCWEB_LOG_ERROR(" Bad D-Bus request error: {}", ec);
28407e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
28417e860f15SJohn Edward Broadbent                 return;
28427e860f15SJohn Edward Broadbent             }
28437e860f15SJohn Edward Broadbent             messages::success(asyncResp->res);
28447e860f15SJohn Edward Broadbent         },
28457e860f15SJohn Edward Broadbent         serviceName, objectPath, interfaceName, method);
28467e860f15SJohn Edward Broadbent }
2847c5b2abe0SLewanczyk, Dawid 
2848c1e219d5SEd Tanous inline void handleComputerSystemResetActionPost(
2849c1e219d5SEd Tanous     crow::App& app, const crow::Request& req,
28507f3e84a1SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2851c1e219d5SEd Tanous     const std::string& systemName)
2852c1e219d5SEd Tanous {
28533ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
285445ca1b86SEd Tanous     {
285545ca1b86SEd Tanous         return;
285645ca1b86SEd Tanous     }
2857dd7090e6SGunnar Mills 
2858dd7090e6SGunnar Mills     if constexpr (BMCWEB_HYPERVISOR_COMPUTER_SYSTEM)
2859dd7090e6SGunnar Mills     {
2860dd7090e6SGunnar Mills         if (systemName == "hypervisor")
2861dd7090e6SGunnar Mills         {
2862dd7090e6SGunnar Mills             handleHypervisorSystemResetPost(req, asyncResp);
2863dd7090e6SGunnar Mills             return;
2864dd7090e6SGunnar Mills         }
2865dd7090e6SGunnar Mills     }
2866dd7090e6SGunnar Mills 
2867253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
2868c1e219d5SEd Tanous     {
2869c1e219d5SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2870c1e219d5SEd Tanous                                    systemName);
2871c1e219d5SEd Tanous         return;
2872c1e219d5SEd Tanous     }
287325b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
28747f3e84a1SEd Tanous     {
28757f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
28767f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2877c1e219d5SEd Tanous                                    systemName);
28787f3e84a1SEd Tanous         return;
28797f3e84a1SEd Tanous     }
28809712f8acSEd Tanous     std::string resetType;
2881c1e219d5SEd Tanous     if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
2882cc340dd9SEd Tanous     {
2883cc340dd9SEd Tanous         return;
2884cc340dd9SEd Tanous     }
2885cc340dd9SEd Tanous 
2886d22c8396SJason M. Bills     // Get the command and host vs. chassis
2887cc340dd9SEd Tanous     std::string command;
2888543f4400SEd Tanous     bool hostCommand = true;
2889d4d25793SEd Tanous     if ((resetType == "On") || (resetType == "ForceOn"))
2890cc340dd9SEd Tanous     {
2891cc340dd9SEd Tanous         command = "xyz.openbmc_project.State.Host.Transition.On";
2892d22c8396SJason M. Bills         hostCommand = true;
2893d22c8396SJason M. Bills     }
2894d22c8396SJason M. Bills     else if (resetType == "ForceOff")
2895d22c8396SJason M. Bills     {
2896d22c8396SJason M. Bills         command = "xyz.openbmc_project.State.Chassis.Transition.Off";
2897d22c8396SJason M. Bills         hostCommand = false;
2898d22c8396SJason M. Bills     }
2899d22c8396SJason M. Bills     else if (resetType == "ForceRestart")
2900d22c8396SJason M. Bills     {
2901c1e219d5SEd Tanous         command = "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot";
290286a0851aSJason M. Bills         hostCommand = true;
2903cc340dd9SEd Tanous     }
29049712f8acSEd Tanous     else if (resetType == "GracefulShutdown")
2905cc340dd9SEd Tanous     {
2906cc340dd9SEd Tanous         command = "xyz.openbmc_project.State.Host.Transition.Off";
2907d22c8396SJason M. Bills         hostCommand = true;
2908cc340dd9SEd Tanous     }
29099712f8acSEd Tanous     else if (resetType == "GracefulRestart")
2910cc340dd9SEd Tanous     {
29110fda0f12SGeorge Liu         command =
29120fda0f12SGeorge Liu             "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot";
2913d22c8396SJason M. Bills         hostCommand = true;
2914d22c8396SJason M. Bills     }
2915d22c8396SJason M. Bills     else if (resetType == "PowerCycle")
2916d22c8396SJason M. Bills     {
291786a0851aSJason M. Bills         command = "xyz.openbmc_project.State.Host.Transition.Reboot";
291886a0851aSJason M. Bills         hostCommand = true;
2919cc340dd9SEd Tanous     }
2920bfd5b826SLakshminarayana R. Kammath     else if (resetType == "Nmi")
2921bfd5b826SLakshminarayana R. Kammath     {
2922bfd5b826SLakshminarayana R. Kammath         doNMI(asyncResp);
2923bfd5b826SLakshminarayana R. Kammath         return;
2924bfd5b826SLakshminarayana R. Kammath     }
2925cc340dd9SEd Tanous     else
2926cc340dd9SEd Tanous     {
2927c1e219d5SEd Tanous         messages::actionParameterUnknown(asyncResp->res, "Reset", resetType);
2928cc340dd9SEd Tanous         return;
2929cc340dd9SEd Tanous     }
2930d02aad39SEd Tanous     sdbusplus::message::object_path statePath("/xyz/openbmc_project/state");
2931cc340dd9SEd Tanous 
2932d22c8396SJason M. Bills     if (hostCommand)
2933d22c8396SJason M. Bills     {
2934e93abac6SGinu George         setDbusProperty(asyncResp, "Reset", "xyz.openbmc_project.State.Host",
2935d02aad39SEd Tanous                         statePath / "host0", "xyz.openbmc_project.State.Host",
2936e93abac6SGinu George                         "RequestedHostTransition", command);
2937cc340dd9SEd Tanous     }
2938d22c8396SJason M. Bills     else
2939d22c8396SJason M. Bills     {
2940e93abac6SGinu George         setDbusProperty(asyncResp, "Reset", "xyz.openbmc_project.State.Chassis",
2941d02aad39SEd Tanous                         statePath / "chassis0",
2942d02aad39SEd Tanous                         "xyz.openbmc_project.State.Chassis",
2943e93abac6SGinu George                         "RequestedPowerTransition", command);
2944d22c8396SJason M. Bills     }
2945d22c8396SJason M. Bills }
2946cc340dd9SEd Tanous 
2947c1e219d5SEd Tanous inline void handleComputerSystemHead(
2948dd60b9edSEd Tanous     App& app, const crow::Request& req,
29497f3e84a1SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29507f3e84a1SEd Tanous     const std::string& /*systemName*/)
2951dd60b9edSEd Tanous {
2952dd60b9edSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2953dd60b9edSEd Tanous     {
2954dd60b9edSEd Tanous         return;
2955dd60b9edSEd Tanous     }
2956dd60b9edSEd Tanous 
2957dd60b9edSEd Tanous     asyncResp->res.addHeader(
2958dd60b9edSEd Tanous         boost::beast::http::field::link,
2959dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystem/ComputerSystem.json>; rel=describedby");
2960dd60b9edSEd Tanous }
2961dd60b9edSEd Tanous 
29625c3e9272SAbhishek Patel inline void afterPortRequest(
29635c3e9272SAbhishek Patel     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29645c3e9272SAbhishek Patel     const boost::system::error_code& ec,
29655c3e9272SAbhishek Patel     const std::vector<std::tuple<std::string, std::string, bool>>& socketData)
29665c3e9272SAbhishek Patel {
29675c3e9272SAbhishek Patel     if (ec)
29685c3e9272SAbhishek Patel     {
2969b3e86cb0SGunnar Mills         BMCWEB_LOG_ERROR("DBUS response error {}", ec);
29705c3e9272SAbhishek Patel         messages::internalError(asyncResp->res);
29715c3e9272SAbhishek Patel         return;
29725c3e9272SAbhishek Patel     }
29735c3e9272SAbhishek Patel     for (const auto& data : socketData)
29745c3e9272SAbhishek Patel     {
29755c3e9272SAbhishek Patel         const std::string& socketPath = get<0>(data);
29765c3e9272SAbhishek Patel         const std::string& protocolName = get<1>(data);
29775c3e9272SAbhishek Patel         bool isProtocolEnabled = get<2>(data);
29785c3e9272SAbhishek Patel         nlohmann::json& dataJson = asyncResp->res.jsonValue["SerialConsole"];
29795c3e9272SAbhishek Patel         dataJson[protocolName]["ServiceEnabled"] = isProtocolEnabled;
29805c3e9272SAbhishek Patel         // need to retrieve port number for
29815c3e9272SAbhishek Patel         // obmc-console-ssh service
29825c3e9272SAbhishek Patel         if (protocolName == "SSH")
29835c3e9272SAbhishek Patel         {
29845c3e9272SAbhishek Patel             getPortNumber(socketPath, [asyncResp, protocolName](
298581c4e330SEd Tanous                                           const boost::system::error_code& ec1,
29865c3e9272SAbhishek Patel                                           int portNumber) {
29875c3e9272SAbhishek Patel                 if (ec1)
29885c3e9272SAbhishek Patel                 {
2989b3e86cb0SGunnar Mills                     BMCWEB_LOG_ERROR("DBUS response error {}", ec1);
29905c3e9272SAbhishek Patel                     messages::internalError(asyncResp->res);
29915c3e9272SAbhishek Patel                     return;
29925c3e9272SAbhishek Patel                 }
29935c3e9272SAbhishek Patel                 nlohmann::json& dataJson1 =
29945c3e9272SAbhishek Patel                     asyncResp->res.jsonValue["SerialConsole"];
29955c3e9272SAbhishek Patel                 dataJson1[protocolName]["Port"] = portNumber;
29965c3e9272SAbhishek Patel             });
29975c3e9272SAbhishek Patel         }
29985c3e9272SAbhishek Patel     }
29995c3e9272SAbhishek Patel }
3000c1e219d5SEd Tanous 
3001504af5a0SPatrick Williams inline void handleComputerSystemGet(
3002504af5a0SPatrick Williams     crow::App& app, const crow::Request& req,
300322d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3004c1e219d5SEd Tanous     const std::string& systemName)
3005c1e219d5SEd Tanous {
30063ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
300745ca1b86SEd Tanous     {
300845ca1b86SEd Tanous         return;
300945ca1b86SEd Tanous     }
3010746b56f3SAsmitha Karunanithi 
301125b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
30127f3e84a1SEd Tanous     {
30137f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
30147f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
30157f3e84a1SEd Tanous                                    systemName);
30167f3e84a1SEd Tanous         return;
30177f3e84a1SEd Tanous     }
30187f3e84a1SEd Tanous 
301968896206SGunnar Mills     if constexpr (BMCWEB_HYPERVISOR_COMPUTER_SYSTEM)
302068896206SGunnar Mills     {
3021746b56f3SAsmitha Karunanithi         if (systemName == "hypervisor")
3022746b56f3SAsmitha Karunanithi         {
3023746b56f3SAsmitha Karunanithi             handleHypervisorSystemGet(asyncResp);
3024746b56f3SAsmitha Karunanithi             return;
3025746b56f3SAsmitha Karunanithi         }
302668896206SGunnar Mills     }
3027746b56f3SAsmitha Karunanithi 
3028253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
302922d268cbSEd Tanous     {
303022d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
303122d268cbSEd Tanous                                    systemName);
303222d268cbSEd Tanous         return;
303322d268cbSEd Tanous     }
3034dd60b9edSEd Tanous     asyncResp->res.addHeader(
3035dd60b9edSEd Tanous         boost::beast::http::field::link,
3036dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystem/ComputerSystem.json>; rel=describedby");
30378d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
3038b6655101SChris Cain         "#ComputerSystem.v1_22_0.ComputerSystem";
3039253f11b8SEd Tanous     asyncResp->res.jsonValue["Name"] = BMCWEB_REDFISH_SYSTEM_URI_NAME;
3040253f11b8SEd Tanous     asyncResp->res.jsonValue["Id"] = BMCWEB_REDFISH_SYSTEM_URI_NAME;
3041539d8c6bSEd Tanous     asyncResp->res.jsonValue["SystemType"] =
3042539d8c6bSEd Tanous         computer_system::SystemType::Physical;
30438d1b46d7Szhanghch05     asyncResp->res.jsonValue["Description"] = "Computer System";
30448d1b46d7Szhanghch05     asyncResp->res.jsonValue["ProcessorSummary"]["Count"] = 0;
3045cf0e004cSNinad Palsule     asyncResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] =
3046dfb2b408SPriyanga Ramasamy         double(0);
3047253f11b8SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
3048253f11b8SEd Tanous         "/redfish/v1/Systems/{}", BMCWEB_REDFISH_SYSTEM_URI_NAME);
304904a258f4SEd Tanous 
3050253f11b8SEd Tanous     asyncResp->res.jsonValue["Processors"]["@odata.id"] = boost::urls::format(
3051253f11b8SEd Tanous         "/redfish/v1/Systems/{}/Processors", BMCWEB_REDFISH_SYSTEM_URI_NAME);
3052253f11b8SEd Tanous     asyncResp->res.jsonValue["Memory"]["@odata.id"] = boost::urls::format(
3053253f11b8SEd Tanous         "/redfish/v1/Systems/{}/Memory", BMCWEB_REDFISH_SYSTEM_URI_NAME);
3054253f11b8SEd Tanous     asyncResp->res.jsonValue["Storage"]["@odata.id"] = boost::urls::format(
3055253f11b8SEd Tanous         "/redfish/v1/Systems/{}/Storage", BMCWEB_REDFISH_SYSTEM_URI_NAME);
30563179105bSSunny Srivastava     asyncResp->res.jsonValue["FabricAdapters"]["@odata.id"] =
3057253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Systems/{}/FabricAdapters",
3058253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME);
3059029573d4SEd Tanous 
3060002d39b4SEd Tanous     asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"]["target"] =
3061253f11b8SEd Tanous         boost::urls::format(
3062253f11b8SEd Tanous             "/redfish/v1/Systems/{}/Actions/ComputerSystem.Reset",
3063253f11b8SEd Tanous             BMCWEB_REDFISH_SYSTEM_URI_NAME);
3064c1e219d5SEd Tanous     asyncResp->res
3065c1e219d5SEd Tanous         .jsonValue["Actions"]["#ComputerSystem.Reset"]["@Redfish.ActionInfo"] =
3066253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Systems/{}/ResetActionInfo",
3067253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME);
3068c5b2abe0SLewanczyk, Dawid 
3069253f11b8SEd Tanous     asyncResp->res.jsonValue["LogServices"]["@odata.id"] = boost::urls::format(
3070253f11b8SEd Tanous         "/redfish/v1/Systems/{}/LogServices", BMCWEB_REDFISH_SYSTEM_URI_NAME);
3071253f11b8SEd Tanous     asyncResp->res.jsonValue["Bios"]["@odata.id"] = boost::urls::format(
3072253f11b8SEd Tanous         "/redfish/v1/Systems/{}/Bios", BMCWEB_REDFISH_SYSTEM_URI_NAME);
3073c4bf6374SJason M. Bills 
30741476687dSEd Tanous     nlohmann::json::array_t managedBy;
30751476687dSEd Tanous     nlohmann::json& manager = managedBy.emplace_back();
3076253f11b8SEd Tanous     manager["@odata.id"] = boost::urls::format("/redfish/v1/Managers/{}",
3077253f11b8SEd Tanous                                                BMCWEB_REDFISH_MANAGER_URI_NAME);
3078002d39b4SEd Tanous     asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy);
3079539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
3080539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
30810e8ac5e7SGunnar Mills 
30820e8ac5e7SGunnar Mills     // Fill in SerialConsole info
3083002d39b4SEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
3084c1e219d5SEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["IPMI"]["ServiceEnabled"] = true;
30851476687dSEd Tanous 
3086c1e219d5SEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["SSH"]["ServiceEnabled"] = true;
30871476687dSEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["SSH"]["Port"] = 2200;
3088c1e219d5SEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["SSH"]["HotKeySequenceDisplay"] =
30891476687dSEd Tanous         "Press ~. to exit console";
30905c3e9272SAbhishek Patel     getPortStatusAndPath(std::span{protocolToDBusForSystems},
30915c3e9272SAbhishek Patel                          std::bind_front(afterPortRequest, asyncResp));
30920e8ac5e7SGunnar Mills 
309325b54dbaSEd Tanous     if constexpr (BMCWEB_KVM)
309425b54dbaSEd Tanous     {
30950e8ac5e7SGunnar Mills         // Fill in GraphicalConsole info
3096002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
309725b54dbaSEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
309825b54dbaSEd Tanous             4;
3099613dabeaSEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
3100613dabeaSEd Tanous             nlohmann::json::array_t({"KVMIP"});
310125b54dbaSEd Tanous     }
310213451e39SWilly Tu 
3103bd79bce8SPatrick Williams     getMainChassisId(
3104bd79bce8SPatrick Williams         asyncResp, [](const std::string& chassisId,
31058d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
3106b2c7e208SEd Tanous             nlohmann::json::array_t chassisArray;
3107b2c7e208SEd Tanous             nlohmann::json& chassis = chassisArray.emplace_back();
3108bd79bce8SPatrick Williams             chassis["@odata.id"] =
3109bd79bce8SPatrick Williams                 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
3110002d39b4SEd Tanous             aRsp->res.jsonValue["Links"]["Chassis"] = std::move(chassisArray);
3111c5d03ff4SJennifer Lee         });
3112a3002228SAppaRao Puli 
311359a17e4fSGeorge Liu     getSystemLocationIndicatorActive(asyncResp);
31149f8bfa7cSGunnar Mills     // TODO (Gunnar): Remove IndicatorLED after enough time has passed
3115a3002228SAppaRao Puli     getIndicatorLedState(asyncResp);
311651bd2d8aSGunnar Mills     getComputerSystem(asyncResp);
31176c34de48SEd Tanous     getHostState(asyncResp);
3118491d8ee7SSantosh Puranik     getBootProperties(asyncResp);
3119978b8803SAndrew Geissler     getBootProgress(asyncResp);
3120b6d5d45cSHieu Huynh     getBootProgressLastStateTime(asyncResp);
312170c4d545SLakshmi Yadlapati     pcie_util::getPCIeDeviceList(asyncResp,
312270c4d545SLakshmi Yadlapati                                  nlohmann::json::json_pointer("/PCIeDevices"));
312351709ffdSYong Li     getHostWatchdogTimer(asyncResp);
3124c6a620f2SGeorge Liu     getPowerRestorePolicy(asyncResp);
31259dcfe8c1SAlbert Zhang     getStopBootOnFault(asyncResp);
3126797d5daeSCorey Hardesty     getAutomaticRetryPolicy(asyncResp);
3127c0557e1aSGunnar Mills     getLastResetTime(asyncResp);
312825b54dbaSEd Tanous     if constexpr (BMCWEB_REDFISH_PROVISIONING_FEATURE)
312925b54dbaSEd Tanous     {
3130a6349918SAppaRao Puli         getProvisioningStatus(asyncResp);
313125b54dbaSEd Tanous     }
31321981771bSAli Ahmed     getTrustedModuleRequiredToBoot(asyncResp);
31333a2d0424SChris Cain     getPowerMode(asyncResp);
313437bbf98cSChris Cain     getIdlePowerSaver(asyncResp);
3135c1e219d5SEd Tanous }
3136550a6bf8SJiaqing Zhao 
3137c1e219d5SEd Tanous inline void handleComputerSystemPatch(
3138c1e219d5SEd Tanous     crow::App& app, const crow::Request& req,
313922d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3140c1e219d5SEd Tanous     const std::string& systemName)
3141c1e219d5SEd Tanous {
31423ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
314345ca1b86SEd Tanous     {
314445ca1b86SEd Tanous         return;
314545ca1b86SEd Tanous     }
314625b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
31477f3e84a1SEd Tanous     {
31487f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
31497f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
31507f3e84a1SEd Tanous                                    systemName);
31517f3e84a1SEd Tanous         return;
31527f3e84a1SEd Tanous     }
3153253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
315422d268cbSEd Tanous     {
315522d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
315622d268cbSEd Tanous                                    systemName);
315722d268cbSEd Tanous         return;
315822d268cbSEd Tanous     }
315922d268cbSEd Tanous 
3160dd60b9edSEd Tanous     asyncResp->res.addHeader(
3161dd60b9edSEd Tanous         boost::beast::http::field::link,
3162dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystem/ComputerSystem.json>; rel=describedby");
3163dd60b9edSEd Tanous 
31649f8bfa7cSGunnar Mills     std::optional<bool> locationIndicatorActive;
3165cde19e5fSSantosh Puranik     std::optional<std::string> indicatorLed;
316698e386ecSGunnar Mills     std::optional<std::string> assetTag;
3167c6a620f2SGeorge Liu     std::optional<std::string> powerRestorePolicy;
31683a2d0424SChris Cain     std::optional<std::string> powerMode;
3169550a6bf8SJiaqing Zhao     std::optional<bool> wdtEnable;
3170550a6bf8SJiaqing Zhao     std::optional<std::string> wdtTimeOutAction;
3171550a6bf8SJiaqing Zhao     std::optional<std::string> bootSource;
3172550a6bf8SJiaqing Zhao     std::optional<std::string> bootType;
3173550a6bf8SJiaqing Zhao     std::optional<std::string> bootEnable;
3174550a6bf8SJiaqing Zhao     std::optional<std::string> bootAutomaticRetry;
3175797d5daeSCorey Hardesty     std::optional<uint32_t> bootAutomaticRetryAttempts;
3176550a6bf8SJiaqing Zhao     std::optional<bool> bootTrustedModuleRequired;
31779dcfe8c1SAlbert Zhang     std::optional<std::string> stopBootOnFault;
3178550a6bf8SJiaqing Zhao     std::optional<bool> ipsEnable;
3179550a6bf8SJiaqing Zhao     std::optional<uint8_t> ipsEnterUtil;
3180550a6bf8SJiaqing Zhao     std::optional<uint64_t> ipsEnterTime;
3181550a6bf8SJiaqing Zhao     std::optional<uint8_t> ipsExitUtil;
3182550a6bf8SJiaqing Zhao     std::optional<uint64_t> ipsExitTime;
3183550a6bf8SJiaqing Zhao 
3184afc474aeSMyung Bae     if (!json_util::readJsonPatch(                                         //
3185afc474aeSMyung Bae             req, asyncResp->res,                                           //
3186afc474aeSMyung Bae             "AssetTag", assetTag,                                          //
3187afc474aeSMyung Bae             "Boot/AutomaticRetryAttempts", bootAutomaticRetryAttempts,     //
3188afc474aeSMyung Bae             "Boot/AutomaticRetryConfig", bootAutomaticRetry,               //
3189afc474aeSMyung Bae             "Boot/BootSourceOverrideEnabled", bootEnable,                  //
3190afc474aeSMyung Bae             "Boot/BootSourceOverrideMode", bootType,                       //
3191afc474aeSMyung Bae             "Boot/BootSourceOverrideTarget", bootSource,                   //
3192afc474aeSMyung Bae             "Boot/StopBootOnFault", stopBootOnFault,                       //
3193afc474aeSMyung Bae             "Boot/TrustedModuleRequiredToBoot", bootTrustedModuleRequired, //
3194afc474aeSMyung Bae             "HostWatchdogTimer/FunctionEnabled", wdtEnable,                //
3195afc474aeSMyung Bae             "HostWatchdogTimer/TimeoutAction", wdtTimeOutAction,           //
3196afc474aeSMyung Bae             "IdlePowerSaver/Enabled", ipsEnable,                           //
3197afc474aeSMyung Bae             "IdlePowerSaver/EnterDwellTimeSeconds", ipsEnterTime,          //
3198afc474aeSMyung Bae             "IdlePowerSaver/EnterUtilizationPercent", ipsEnterUtil,        //
3199afc474aeSMyung Bae             "IdlePowerSaver/ExitDwellTimeSeconds", ipsExitTime,            //
3200afc474aeSMyung Bae             "IdlePowerSaver/ExitUtilizationPercent", ipsExitUtil,          //
3201afc474aeSMyung Bae             "IndicatorLED", indicatorLed,                                  //
3202afc474aeSMyung Bae             "LocationIndicatorActive", locationIndicatorActive,            //
3203afc474aeSMyung Bae             "PowerMode", powerMode,                                        //
3204afc474aeSMyung Bae             "PowerRestorePolicy", powerRestorePolicy                       //
3205afc474aeSMyung Bae             ))
32066617338dSEd Tanous     {
32076617338dSEd Tanous         return;
32086617338dSEd Tanous     }
3209491d8ee7SSantosh Puranik 
32108d1b46d7Szhanghch05     asyncResp->res.result(boost::beast::http::status::no_content);
3211c45f0082SYong Li 
321298e386ecSGunnar Mills     if (assetTag)
321398e386ecSGunnar Mills     {
321498e386ecSGunnar Mills         setAssetTag(asyncResp, *assetTag);
321598e386ecSGunnar Mills     }
321698e386ecSGunnar Mills 
3217550a6bf8SJiaqing Zhao     if (wdtEnable || wdtTimeOutAction)
3218c45f0082SYong Li     {
3219f23b7296SEd Tanous         setWDTProperties(asyncResp, wdtEnable, wdtTimeOutAction);
3220c45f0082SYong Li     }
3221c45f0082SYong Li 
3222cd9a4666SKonstantin Aladyshev     if (bootSource || bootType || bootEnable)
322369f35306SGunnar Mills     {
3224002d39b4SEd Tanous         setBootProperties(asyncResp, bootSource, bootType, bootEnable);
3225491d8ee7SSantosh Puranik     }
3226550a6bf8SJiaqing Zhao     if (bootAutomaticRetry)
322769f35306SGunnar Mills     {
3228550a6bf8SJiaqing Zhao         setAutomaticRetry(asyncResp, *bootAutomaticRetry);
322969f35306SGunnar Mills     }
3230ac7e1e0bSAli Ahmed 
3231797d5daeSCorey Hardesty     if (bootAutomaticRetryAttempts)
3232797d5daeSCorey Hardesty     {
3233797d5daeSCorey Hardesty         setAutomaticRetryAttempts(asyncResp,
3234797d5daeSCorey Hardesty                                   bootAutomaticRetryAttempts.value());
3235797d5daeSCorey Hardesty     }
3236797d5daeSCorey Hardesty 
3237550a6bf8SJiaqing Zhao     if (bootTrustedModuleRequired)
3238ac7e1e0bSAli Ahmed     {
3239c1e219d5SEd Tanous         setTrustedModuleRequiredToBoot(asyncResp, *bootTrustedModuleRequired);
324069f35306SGunnar Mills     }
3241265c1602SJohnathan Mantey 
32429dcfe8c1SAlbert Zhang     if (stopBootOnFault)
32439dcfe8c1SAlbert Zhang     {
32449dcfe8c1SAlbert Zhang         setStopBootOnFault(asyncResp, *stopBootOnFault);
32459dcfe8c1SAlbert Zhang     }
32469dcfe8c1SAlbert Zhang 
32479f8bfa7cSGunnar Mills     if (locationIndicatorActive)
32489f8bfa7cSGunnar Mills     {
324959a17e4fSGeorge Liu         setSystemLocationIndicatorActive(asyncResp, *locationIndicatorActive);
32509f8bfa7cSGunnar Mills     }
32519f8bfa7cSGunnar Mills 
32527e860f15SJohn Edward Broadbent     // TODO (Gunnar): Remove IndicatorLED after enough time has
32537e860f15SJohn Edward Broadbent     // passed
32549712f8acSEd Tanous     if (indicatorLed)
32556617338dSEd Tanous     {
3256f23b7296SEd Tanous         setIndicatorLedState(asyncResp, *indicatorLed);
3257002d39b4SEd Tanous         asyncResp->res.addHeader(boost::beast::http::field::warning,
3258d6aa0093SGunnar Mills                                  "299 - \"IndicatorLED is deprecated. Use "
3259d6aa0093SGunnar Mills                                  "LocationIndicatorActive instead.\"");
32606617338dSEd Tanous     }
3261c6a620f2SGeorge Liu 
3262c6a620f2SGeorge Liu     if (powerRestorePolicy)
3263c6a620f2SGeorge Liu     {
32644e69c904SGunnar Mills         setPowerRestorePolicy(asyncResp, *powerRestorePolicy);
3265c6a620f2SGeorge Liu     }
32663a2d0424SChris Cain 
32673a2d0424SChris Cain     if (powerMode)
32683a2d0424SChris Cain     {
32693a2d0424SChris Cain         setPowerMode(asyncResp, *powerMode);
32703a2d0424SChris Cain     }
327137bbf98cSChris Cain 
3272c1e219d5SEd Tanous     if (ipsEnable || ipsEnterUtil || ipsEnterTime || ipsExitUtil || ipsExitTime)
327337bbf98cSChris Cain     {
3274002d39b4SEd Tanous         setIdlePowerSaver(asyncResp, ipsEnable, ipsEnterUtil, ipsEnterTime,
3275002d39b4SEd Tanous                           ipsExitUtil, ipsExitTime);
327637bbf98cSChris Cain     }
3277c1e219d5SEd Tanous }
32781cb1a9e6SAppaRao Puli 
327938c8a6f2SEd Tanous inline void handleSystemCollectionResetActionHead(
3280dd60b9edSEd Tanous     crow::App& app, const crow::Request& req,
32817f3e84a1SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3282c1e219d5SEd Tanous     const std::string& /*systemName*/)
3283dd60b9edSEd Tanous {
3284dd60b9edSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3285dd60b9edSEd Tanous     {
3286dd60b9edSEd Tanous         return;
3287dd60b9edSEd Tanous     }
3288dd60b9edSEd Tanous     asyncResp->res.addHeader(
3289dd60b9edSEd Tanous         boost::beast::http::field::link,
3290dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ActionInfo/ActionInfo.json>; rel=describedby");
3291dd60b9edSEd Tanous }
329233e1f122SAndrew Geissler 
329333e1f122SAndrew Geissler /**
329433e1f122SAndrew Geissler  * @brief Translates allowed host transitions to redfish string
329533e1f122SAndrew Geissler  *
329633e1f122SAndrew Geissler  * @param[in]  dbusAllowedHostTran The allowed host transition on dbus
329733e1f122SAndrew Geissler  * @param[out] allowableValues     The translated host transition(s)
329833e1f122SAndrew Geissler  *
3299efff2b5dSManojkiran Eda  * @return Emplaces corresponding Redfish translated value(s) in
330033e1f122SAndrew Geissler  * allowableValues. If translation not possible, does nothing to
330133e1f122SAndrew Geissler  * allowableValues.
330233e1f122SAndrew Geissler  */
3303504af5a0SPatrick Williams inline void dbusToRfAllowedHostTransitions(
3304504af5a0SPatrick Williams     const std::string& dbusAllowedHostTran,
330533e1f122SAndrew Geissler     nlohmann::json::array_t& allowableValues)
330633e1f122SAndrew Geissler {
330733e1f122SAndrew Geissler     if (dbusAllowedHostTran == "xyz.openbmc_project.State.Host.Transition.On")
330833e1f122SAndrew Geissler     {
330933e1f122SAndrew Geissler         allowableValues.emplace_back(resource::ResetType::On);
331033e1f122SAndrew Geissler         allowableValues.emplace_back(resource::ResetType::ForceOn);
331133e1f122SAndrew Geissler     }
331233e1f122SAndrew Geissler     else if (dbusAllowedHostTran ==
331333e1f122SAndrew Geissler              "xyz.openbmc_project.State.Host.Transition.Off")
331433e1f122SAndrew Geissler     {
331533e1f122SAndrew Geissler         allowableValues.emplace_back(resource::ResetType::GracefulShutdown);
331633e1f122SAndrew Geissler     }
331733e1f122SAndrew Geissler     else if (dbusAllowedHostTran ==
331833e1f122SAndrew Geissler              "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot")
331933e1f122SAndrew Geissler     {
332033e1f122SAndrew Geissler         allowableValues.emplace_back(resource::ResetType::GracefulRestart);
332133e1f122SAndrew Geissler     }
332233e1f122SAndrew Geissler     else if (dbusAllowedHostTran ==
332333e1f122SAndrew Geissler              "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot")
332433e1f122SAndrew Geissler     {
332533e1f122SAndrew Geissler         allowableValues.emplace_back(resource::ResetType::ForceRestart);
332633e1f122SAndrew Geissler     }
332733e1f122SAndrew Geissler     else
332833e1f122SAndrew Geissler     {
332933e1f122SAndrew Geissler         BMCWEB_LOG_WARNING("Unsupported host tran {}", dbusAllowedHostTran);
333033e1f122SAndrew Geissler     }
333133e1f122SAndrew Geissler }
333233e1f122SAndrew Geissler 
333333e1f122SAndrew Geissler inline void afterGetAllowedHostTransitions(
333433e1f122SAndrew Geissler     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
333533e1f122SAndrew Geissler     const boost::system::error_code& ec,
333633e1f122SAndrew Geissler     const std::vector<std::string>& allowedHostTransitions)
333733e1f122SAndrew Geissler {
333833e1f122SAndrew Geissler     nlohmann::json::array_t allowableValues;
333933e1f122SAndrew Geissler 
334033e1f122SAndrew Geissler     // Supported on all systems currently
334133e1f122SAndrew Geissler     allowableValues.emplace_back(resource::ResetType::ForceOff);
334233e1f122SAndrew Geissler     allowableValues.emplace_back(resource::ResetType::PowerCycle);
334333e1f122SAndrew Geissler     allowableValues.emplace_back(resource::ResetType::Nmi);
334433e1f122SAndrew Geissler 
334533e1f122SAndrew Geissler     if (ec)
334633e1f122SAndrew Geissler     {
3347e715d14bSEd Tanous         if ((ec.value() ==
3348e715d14bSEd Tanous              boost::system::linux_error::bad_request_descriptor) ||
3349e715d14bSEd Tanous             (ec.value() == boost::asio::error::basic_errors::host_unreachable))
335033e1f122SAndrew Geissler         {
335133e1f122SAndrew Geissler             // Property not implemented so just return defaults
335233e1f122SAndrew Geissler             BMCWEB_LOG_DEBUG("Property not available {}", ec);
335333e1f122SAndrew Geissler             allowableValues.emplace_back(resource::ResetType::On);
335433e1f122SAndrew Geissler             allowableValues.emplace_back(resource::ResetType::ForceOn);
335533e1f122SAndrew Geissler             allowableValues.emplace_back(resource::ResetType::ForceRestart);
335633e1f122SAndrew Geissler             allowableValues.emplace_back(resource::ResetType::GracefulRestart);
335733e1f122SAndrew Geissler             allowableValues.emplace_back(resource::ResetType::GracefulShutdown);
335833e1f122SAndrew Geissler         }
335933e1f122SAndrew Geissler         else
336033e1f122SAndrew Geissler         {
336133e1f122SAndrew Geissler             BMCWEB_LOG_ERROR("DBUS response error {}", ec);
336233e1f122SAndrew Geissler             messages::internalError(asyncResp->res);
336333e1f122SAndrew Geissler             return;
336433e1f122SAndrew Geissler         }
336533e1f122SAndrew Geissler     }
336633e1f122SAndrew Geissler     else
336733e1f122SAndrew Geissler     {
336833e1f122SAndrew Geissler         for (const std::string& transition : allowedHostTransitions)
336933e1f122SAndrew Geissler         {
337033e1f122SAndrew Geissler             BMCWEB_LOG_DEBUG("Found allowed host tran {}", transition);
337133e1f122SAndrew Geissler             dbusToRfAllowedHostTransitions(transition, allowableValues);
337233e1f122SAndrew Geissler         }
337333e1f122SAndrew Geissler     }
337433e1f122SAndrew Geissler 
337533e1f122SAndrew Geissler     nlohmann::json::object_t parameter;
337633e1f122SAndrew Geissler     parameter["Name"] = "ResetType";
337733e1f122SAndrew Geissler     parameter["Required"] = true;
3378539d8c6bSEd Tanous     parameter["DataType"] = action_info::ParameterTypes::String;
337933e1f122SAndrew Geissler     parameter["AllowableValues"] = std::move(allowableValues);
338033e1f122SAndrew Geissler     nlohmann::json::array_t parameters;
338133e1f122SAndrew Geissler     parameters.emplace_back(std::move(parameter));
338233e1f122SAndrew Geissler     asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
338333e1f122SAndrew Geissler }
338433e1f122SAndrew Geissler 
3385c1e219d5SEd Tanous inline void handleSystemCollectionResetActionGet(
3386c1e219d5SEd Tanous     crow::App& app, const crow::Request& req,
338722d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3388c1e219d5SEd Tanous     const std::string& systemName)
3389c1e219d5SEd Tanous {
33903ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
339145ca1b86SEd Tanous     {
339245ca1b86SEd Tanous         return;
339345ca1b86SEd Tanous     }
339425b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
33957f3e84a1SEd Tanous     {
33967f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
33977f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
33987f3e84a1SEd Tanous                                    systemName);
33997f3e84a1SEd Tanous         return;
34007f3e84a1SEd Tanous     }
3401746b56f3SAsmitha Karunanithi 
340268896206SGunnar Mills     if constexpr (BMCWEB_HYPERVISOR_COMPUTER_SYSTEM)
340368896206SGunnar Mills     {
3404746b56f3SAsmitha Karunanithi         if (systemName == "hypervisor")
3405746b56f3SAsmitha Karunanithi         {
3406746b56f3SAsmitha Karunanithi             handleHypervisorResetActionGet(asyncResp);
3407746b56f3SAsmitha Karunanithi             return;
3408746b56f3SAsmitha Karunanithi         }
340968896206SGunnar Mills     }
3410746b56f3SAsmitha Karunanithi 
3411253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
341222d268cbSEd Tanous     {
341322d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
341422d268cbSEd Tanous                                    systemName);
341522d268cbSEd Tanous         return;
341622d268cbSEd Tanous     }
341722d268cbSEd Tanous 
3418dd60b9edSEd Tanous     asyncResp->res.addHeader(
3419dd60b9edSEd Tanous         boost::beast::http::field::link,
3420dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ActionInfo/ActionInfo.json>; rel=describedby");
34211476687dSEd Tanous 
34221476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
3423253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Systems/{}/ResetActionInfo",
3424253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME);
3425c1e219d5SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ActionInfo.v1_1_2.ActionInfo";
34261476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Reset Action Info";
34271476687dSEd Tanous     asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
34283215e700SNan Zhou 
342933e1f122SAndrew Geissler     // Look to see if system defines AllowedHostTransitions
3430deae6a78SEd Tanous     dbus::utility::getProperty<std::vector<std::string>>(
3431deae6a78SEd Tanous         "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
3432deae6a78SEd Tanous         "xyz.openbmc_project.State.Host", "AllowedHostTransitions",
343333e1f122SAndrew Geissler         [asyncResp](const boost::system::error_code& ec,
343433e1f122SAndrew Geissler                     const std::vector<std::string>& allowedHostTransitions) {
3435bd79bce8SPatrick Williams             afterGetAllowedHostTransitions(asyncResp, ec,
3436bd79bce8SPatrick Williams                                            allowedHostTransitions);
343733e1f122SAndrew Geissler         });
3438c1e219d5SEd Tanous }
3439c1e219d5SEd Tanous /**
3440c1e219d5SEd Tanous  * SystemResetActionInfo derived class for delivering Computer Systems
3441c1e219d5SEd Tanous  * ResetType AllowableValues using ResetInfo schema.
3442c1e219d5SEd Tanous  */
3443100afe56SEd Tanous inline void requestRoutesSystems(App& app)
3444c1e219d5SEd Tanous {
3445100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/")
3446100afe56SEd Tanous         .privileges(redfish::privileges::headComputerSystemCollection)
3447100afe56SEd Tanous         .methods(boost::beast::http::verb::head)(
3448100afe56SEd Tanous             std::bind_front(handleComputerSystemCollectionHead, std::ref(app)));
3449100afe56SEd Tanous 
3450100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/")
3451100afe56SEd Tanous         .privileges(redfish::privileges::getComputerSystemCollection)
3452100afe56SEd Tanous         .methods(boost::beast::http::verb::get)(
3453100afe56SEd Tanous             std::bind_front(handleComputerSystemCollectionGet, std::ref(app)));
3454100afe56SEd Tanous 
3455100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/")
3456100afe56SEd Tanous         .privileges(redfish::privileges::headComputerSystem)
3457100afe56SEd Tanous         .methods(boost::beast::http::verb::head)(
3458100afe56SEd Tanous             std::bind_front(handleComputerSystemHead, std::ref(app)));
3459100afe56SEd Tanous 
3460100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/")
3461100afe56SEd Tanous         .privileges(redfish::privileges::getComputerSystem)
3462100afe56SEd Tanous         .methods(boost::beast::http::verb::get)(
3463100afe56SEd Tanous             std::bind_front(handleComputerSystemGet, std::ref(app)));
3464100afe56SEd Tanous 
3465100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/")
3466100afe56SEd Tanous         .privileges(redfish::privileges::patchComputerSystem)
3467100afe56SEd Tanous         .methods(boost::beast::http::verb::patch)(
3468100afe56SEd Tanous             std::bind_front(handleComputerSystemPatch, std::ref(app)));
3469100afe56SEd Tanous 
3470100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Actions/ComputerSystem.Reset/")
3471100afe56SEd Tanous         .privileges(redfish::privileges::postComputerSystem)
3472100afe56SEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
3473100afe56SEd Tanous             handleComputerSystemResetActionPost, std::ref(app)));
3474100afe56SEd Tanous 
3475c1e219d5SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/ResetActionInfo/")
3476c1e219d5SEd Tanous         .privileges(redfish::privileges::headActionInfo)
3477c1e219d5SEd Tanous         .methods(boost::beast::http::verb::head)(std::bind_front(
3478c1e219d5SEd Tanous             handleSystemCollectionResetActionHead, std::ref(app)));
3479c1e219d5SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/ResetActionInfo/")
3480c1e219d5SEd Tanous         .privileges(redfish::privileges::getActionInfo)
3481c1e219d5SEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
3482c1e219d5SEd Tanous             handleSystemCollectionResetActionGet, std::ref(app)));
34831cb1a9e6SAppaRao Puli }
3484c5b2abe0SLewanczyk, Dawid } // namespace redfish
3485