xref: /openbmc/bmcweb/features/redfish/lib/systems.hpp (revision 9ae226fabb21700e6a73ef1d0a9d1cdead70fea5)
1c5b2abe0SLewanczyk, Dawid /*
2c5b2abe0SLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation
3c5b2abe0SLewanczyk, Dawid //
4c5b2abe0SLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License");
5c5b2abe0SLewanczyk, Dawid // you may not use this file except in compliance with the License.
6c5b2abe0SLewanczyk, Dawid // You may obtain a copy of the License at
7c5b2abe0SLewanczyk, Dawid //
8c5b2abe0SLewanczyk, Dawid //      http://www.apache.org/licenses/LICENSE-2.0
9c5b2abe0SLewanczyk, Dawid //
10c5b2abe0SLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software
11c5b2abe0SLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS,
12c5b2abe0SLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c5b2abe0SLewanczyk, Dawid // See the License for the specific language governing permissions and
14c5b2abe0SLewanczyk, Dawid // limitations under the License.
15c5b2abe0SLewanczyk, Dawid */
16c5b2abe0SLewanczyk, Dawid #pragma once
17c5b2abe0SLewanczyk, Dawid 
1813451e39SWilly Tu #include "bmcweb_config.h"
1913451e39SWilly Tu 
203ccb3adbSEd Tanous #include "app.hpp"
211e1e598dSJonathan Doman #include "dbus_singleton.hpp"
227a1dbc48SGeorge Liu #include "dbus_utility.hpp"
238d69c668SEd Tanous #include "generated/enums/computer_system.hpp"
24b49ac873SJames Feist #include "health.hpp"
25746b56f3SAsmitha Karunanithi #include "hypervisor_system.hpp"
261c8fba97SJames Feist #include "led.hpp"
27f4c99e70SEd Tanous #include "query.hpp"
28c5d03ff4SJennifer Lee #include "redfish_util.hpp"
293ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
303ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
313ccb3adbSEd Tanous #include "utils/json_utils.hpp"
32472bd202SLakshmi Yadlapati #include "utils/pcie_util.hpp"
333ccb3adbSEd Tanous #include "utils/sw_utils.hpp"
342b82937eSEd Tanous #include "utils/time_utils.hpp"
35c5d03ff4SJennifer Lee 
36fc903b3dSAndrew Geissler #include <boost/asio/error.hpp>
379712f8acSEd Tanous #include <boost/container/flat_map.hpp>
38e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
39ef4c65b7SEd Tanous #include <boost/url/format.hpp>
401e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
41fc903b3dSAndrew Geissler #include <sdbusplus/message.hpp>
42bc1d29deSKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
431214b7e7SGunnar Mills 
447a1dbc48SGeorge Liu #include <array>
457a1dbc48SGeorge Liu #include <string_view>
46abf2add6SEd Tanous #include <variant>
47c5b2abe0SLewanczyk, Dawid 
481abe55efSEd Tanous namespace redfish
491abe55efSEd Tanous {
50c5b2abe0SLewanczyk, Dawid 
515c3e9272SAbhishek Patel const static std::array<std::pair<std::string_view, std::string_view>, 2>
525c3e9272SAbhishek Patel     protocolToDBusForSystems{
535c3e9272SAbhishek Patel         {{"SSH", "obmc-console-ssh"}, {"IPMI", "phosphor-ipmi-net"}}};
545c3e9272SAbhishek Patel 
559d3ae10eSAlpana Kumari /**
569d3ae10eSAlpana Kumari  * @brief Updates the Functional State of DIMMs
579d3ae10eSAlpana Kumari  *
58ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
599d3ae10eSAlpana Kumari  * @param[in] dimmState Dimm's Functional state, true/false
609d3ae10eSAlpana Kumari  *
619d3ae10eSAlpana Kumari  * @return None.
629d3ae10eSAlpana Kumari  */
638d1b46d7Szhanghch05 inline void
64ac106bf6SEd Tanous     updateDimmProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
651e1e598dSJonathan Doman                          bool isDimmFunctional)
669d3ae10eSAlpana Kumari {
671e1e598dSJonathan Doman     BMCWEB_LOG_DEBUG << "Dimm Functional: " << isDimmFunctional;
689d3ae10eSAlpana Kumari 
699d3ae10eSAlpana Kumari     // Set it as Enabled if at least one DIMM is functional
709d3ae10eSAlpana Kumari     // Update STATE only if previous State was DISABLED and current Dimm is
719d3ae10eSAlpana Kumari     // ENABLED.
7202cad96eSEd Tanous     const nlohmann::json& prevMemSummary =
73ac106bf6SEd Tanous         asyncResp->res.jsonValue["MemorySummary"]["Status"]["State"];
749d3ae10eSAlpana Kumari     if (prevMemSummary == "Disabled")
759d3ae10eSAlpana Kumari     {
76e05aec50SEd Tanous         if (isDimmFunctional)
779d3ae10eSAlpana Kumari         {
78ac106bf6SEd Tanous             asyncResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
799d3ae10eSAlpana Kumari                 "Enabled";
809d3ae10eSAlpana Kumari         }
819d3ae10eSAlpana Kumari     }
829d3ae10eSAlpana Kumari }
839d3ae10eSAlpana Kumari 
8457e8c9beSAlpana Kumari /*
8557e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Status" "State" based on
8657e8c9beSAlpana Kumari  *        CPU Functional State
8757e8c9beSAlpana Kumari  *
88ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
8957e8c9beSAlpana Kumari  * @param[in] cpuFunctionalState is CPU functional true/false
9057e8c9beSAlpana Kumari  *
9157e8c9beSAlpana Kumari  * @return None.
9257e8c9beSAlpana Kumari  */
93ac106bf6SEd Tanous inline void modifyCpuFunctionalState(
94ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, bool isCpuFunctional)
9557e8c9beSAlpana Kumari {
961e1e598dSJonathan Doman     BMCWEB_LOG_DEBUG << "Cpu Functional: " << isCpuFunctional;
9757e8c9beSAlpana Kumari 
9802cad96eSEd Tanous     const nlohmann::json& prevProcState =
99ac106bf6SEd Tanous         asyncResp->res.jsonValue["ProcessorSummary"]["Status"]["State"];
10057e8c9beSAlpana Kumari 
10157e8c9beSAlpana Kumari     // Set it as Enabled if at least one CPU is functional
10257e8c9beSAlpana Kumari     // Update STATE only if previous State was Non_Functional and current CPU is
10357e8c9beSAlpana Kumari     // Functional.
10457e8c9beSAlpana Kumari     if (prevProcState == "Disabled")
10557e8c9beSAlpana Kumari     {
106e05aec50SEd Tanous         if (isCpuFunctional)
10757e8c9beSAlpana Kumari         {
108ac106bf6SEd Tanous             asyncResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
10957e8c9beSAlpana Kumari                 "Enabled";
11057e8c9beSAlpana Kumari         }
11157e8c9beSAlpana Kumari     }
11257e8c9beSAlpana Kumari }
11357e8c9beSAlpana Kumari 
114cf0e004cSNinad Palsule /*
115cf0e004cSNinad Palsule  * @brief Update "ProcessorSummary" "Count" based on Cpu PresenceState
116cf0e004cSNinad Palsule  *
117ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
118cf0e004cSNinad Palsule  * @param[in] cpuPresenceState CPU present or not
119cf0e004cSNinad Palsule  *
120cf0e004cSNinad Palsule  * @return None.
121cf0e004cSNinad Palsule  */
122cf0e004cSNinad Palsule inline void
123ac106bf6SEd Tanous     modifyCpuPresenceState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
124cf0e004cSNinad Palsule                            bool isCpuPresent)
125cf0e004cSNinad Palsule {
126cf0e004cSNinad Palsule     BMCWEB_LOG_DEBUG << "Cpu Present: " << isCpuPresent;
127cf0e004cSNinad Palsule 
128cf0e004cSNinad Palsule     if (isCpuPresent)
129cf0e004cSNinad Palsule     {
130cf0e004cSNinad Palsule         nlohmann::json& procCount =
131ac106bf6SEd Tanous             asyncResp->res.jsonValue["ProcessorSummary"]["Count"];
132cf0e004cSNinad Palsule         auto* procCountPtr =
133cf0e004cSNinad Palsule             procCount.get_ptr<nlohmann::json::number_integer_t*>();
134cf0e004cSNinad Palsule         if (procCountPtr != nullptr)
135cf0e004cSNinad Palsule         {
136cf0e004cSNinad Palsule             // shouldn't be possible to be nullptr
137cf0e004cSNinad Palsule             *procCountPtr += 1;
138cf0e004cSNinad Palsule         }
139cf0e004cSNinad Palsule     }
140cf0e004cSNinad Palsule }
141cf0e004cSNinad Palsule 
142382d6475SAli Ahmed inline void getProcessorProperties(
143ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
144382d6475SAli Ahmed     const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
145382d6475SAli Ahmed         properties)
14603fbed92SAli Ahmed {
14703fbed92SAli Ahmed     BMCWEB_LOG_DEBUG << "Got " << properties.size() << " Cpu properties.";
14803fbed92SAli Ahmed 
14903fbed92SAli Ahmed     // TODO: Get Model
15003fbed92SAli Ahmed 
151bc1d29deSKrzysztof Grobelny     const uint16_t* coreCount = nullptr;
15203fbed92SAli Ahmed 
153bc1d29deSKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
154bc1d29deSKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), properties, "CoreCount", coreCount);
15503fbed92SAli Ahmed 
156bc1d29deSKrzysztof Grobelny     if (!success)
15703fbed92SAli Ahmed     {
158ac106bf6SEd Tanous         messages::internalError(asyncResp->res);
15903fbed92SAli Ahmed         return;
16003fbed92SAli Ahmed     }
16103fbed92SAli Ahmed 
162bc1d29deSKrzysztof Grobelny     if (coreCount != nullptr)
16303fbed92SAli Ahmed     {
164bc1d29deSKrzysztof Grobelny         nlohmann::json& coreCountJson =
165ac106bf6SEd Tanous             asyncResp->res.jsonValue["ProcessorSummary"]["CoreCount"];
166bc1d29deSKrzysztof Grobelny         uint64_t* coreCountJsonPtr = coreCountJson.get_ptr<uint64_t*>();
167bc1d29deSKrzysztof Grobelny 
168bc1d29deSKrzysztof Grobelny         if (coreCountJsonPtr == nullptr)
169bc1d29deSKrzysztof Grobelny         {
170bc1d29deSKrzysztof Grobelny             coreCountJson = *coreCount;
17103fbed92SAli Ahmed         }
17203fbed92SAli Ahmed         else
17303fbed92SAli Ahmed         {
174bc1d29deSKrzysztof Grobelny             *coreCountJsonPtr += *coreCount;
17503fbed92SAli Ahmed         }
17603fbed92SAli Ahmed     }
17703fbed92SAli Ahmed }
17803fbed92SAli Ahmed 
17903fbed92SAli Ahmed /*
18003fbed92SAli Ahmed  * @brief Get ProcessorSummary fields
18103fbed92SAli Ahmed  *
182ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
18303fbed92SAli Ahmed  * @param[in] service dbus service for Cpu Information
18403fbed92SAli Ahmed  * @param[in] path dbus path for Cpu
18503fbed92SAli Ahmed  *
18603fbed92SAli Ahmed  * @return None.
18703fbed92SAli Ahmed  */
188ac106bf6SEd Tanous inline void
189ac106bf6SEd Tanous     getProcessorSummary(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
190ac106bf6SEd Tanous                         const std::string& service, const std::string& path)
19103fbed92SAli Ahmed {
192ac106bf6SEd Tanous     auto getCpuPresenceState = [asyncResp](const boost::system::error_code& ec3,
193382d6475SAli Ahmed                                            const bool cpuPresenceCheck) {
194382d6475SAli Ahmed         if (ec3)
195382d6475SAli Ahmed         {
196382d6475SAli Ahmed             BMCWEB_LOG_ERROR << "DBUS response error " << ec3;
197382d6475SAli Ahmed             return;
198382d6475SAli Ahmed         }
199ac106bf6SEd Tanous         modifyCpuPresenceState(asyncResp, cpuPresenceCheck);
200382d6475SAli Ahmed     };
201382d6475SAli Ahmed 
202cf0e004cSNinad Palsule     // Get the Presence of CPU
203cf0e004cSNinad Palsule     sdbusplus::asio::getProperty<bool>(
204cf0e004cSNinad Palsule         *crow::connections::systemBus, service, path,
205cf0e004cSNinad Palsule         "xyz.openbmc_project.Inventory.Item", "Present",
206cf0e004cSNinad Palsule         std::move(getCpuPresenceState));
207cf0e004cSNinad Palsule 
2085fd0aafbSNinad Palsule     if constexpr (bmcwebEnableProcMemStatus)
2095fd0aafbSNinad Palsule     {
2105fd0aafbSNinad Palsule         auto getCpuFunctionalState =
211ac106bf6SEd Tanous             [asyncResp](const boost::system::error_code& ec3,
212382d6475SAli Ahmed                         const bool cpuFunctionalCheck) {
213382d6475SAli Ahmed             if (ec3)
214382d6475SAli Ahmed             {
215382d6475SAli Ahmed                 BMCWEB_LOG_ERROR << "DBUS response error " << ec3;
216382d6475SAli Ahmed                 return;
217382d6475SAli Ahmed             }
218ac106bf6SEd Tanous             modifyCpuFunctionalState(asyncResp, cpuFunctionalCheck);
219382d6475SAli Ahmed         };
220382d6475SAli Ahmed 
221382d6475SAli Ahmed         // Get the Functional State
222382d6475SAli Ahmed         sdbusplus::asio::getProperty<bool>(
223382d6475SAli Ahmed             *crow::connections::systemBus, service, path,
2245fd0aafbSNinad Palsule             "xyz.openbmc_project.State.Decorator.OperationalStatus",
2255fd0aafbSNinad Palsule             "Functional", std::move(getCpuFunctionalState));
2265fd0aafbSNinad Palsule     }
227382d6475SAli Ahmed 
228bc1d29deSKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
229bc1d29deSKrzysztof Grobelny         *crow::connections::systemBus, service, path,
230bc1d29deSKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Item.Cpu",
231ac106bf6SEd Tanous         [asyncResp, service,
2325e7e2dc5SEd Tanous          path](const boost::system::error_code& ec2,
233b9d36b47SEd Tanous                const dbus::utility::DBusPropertiesMap& properties) {
23403fbed92SAli Ahmed         if (ec2)
23503fbed92SAli Ahmed         {
23603fbed92SAli Ahmed             BMCWEB_LOG_ERROR << "DBUS response error " << ec2;
237ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
23803fbed92SAli Ahmed             return;
23903fbed92SAli Ahmed         }
240ac106bf6SEd Tanous         getProcessorProperties(asyncResp, properties);
241bc1d29deSKrzysztof Grobelny         });
24203fbed92SAli Ahmed }
24303fbed92SAli Ahmed 
24457e8c9beSAlpana Kumari /*
245cf0e004cSNinad Palsule  * @brief processMemoryProperties fields
246cf0e004cSNinad Palsule  *
247ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
248cf0e004cSNinad Palsule  * @param[in] service dbus service for memory Information
249cf0e004cSNinad Palsule  * @param[in] path dbus path for Memory
250cf0e004cSNinad Palsule  * @param[in] DBUS properties for memory
251cf0e004cSNinad Palsule  *
252cf0e004cSNinad Palsule  * @return None.
253cf0e004cSNinad Palsule  */
254cf0e004cSNinad Palsule inline void
255ac106bf6SEd Tanous     processMemoryProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2565fd0aafbSNinad Palsule                             [[maybe_unused]] const std::string& service,
2575fd0aafbSNinad Palsule                             [[maybe_unused]] const std::string& path,
258cf0e004cSNinad Palsule                             const dbus::utility::DBusPropertiesMap& properties)
259cf0e004cSNinad Palsule {
260cf0e004cSNinad Palsule     BMCWEB_LOG_DEBUG << "Got " << properties.size() << " Dimm properties.";
261cf0e004cSNinad Palsule 
262cf0e004cSNinad Palsule     if (properties.empty())
263cf0e004cSNinad Palsule     {
2645fd0aafbSNinad Palsule         if constexpr (bmcwebEnableProcMemStatus)
2655fd0aafbSNinad Palsule         {
266cf0e004cSNinad Palsule             sdbusplus::asio::getProperty<bool>(
267cf0e004cSNinad Palsule                 *crow::connections::systemBus, service, path,
268cf0e004cSNinad Palsule                 "xyz.openbmc_project.State."
269cf0e004cSNinad Palsule                 "Decorator.OperationalStatus",
270cf0e004cSNinad Palsule                 "Functional",
271ac106bf6SEd Tanous                 [asyncResp](const boost::system::error_code& ec3,
272ac106bf6SEd Tanous                             bool dimmState) {
273cf0e004cSNinad Palsule                 if (ec3)
274cf0e004cSNinad Palsule                 {
275cf0e004cSNinad Palsule                     BMCWEB_LOG_ERROR << "DBUS response error " << ec3;
276cf0e004cSNinad Palsule                     return;
277cf0e004cSNinad Palsule                 }
278ac106bf6SEd Tanous                 updateDimmProperties(asyncResp, dimmState);
279cf0e004cSNinad Palsule                 });
2805fd0aafbSNinad Palsule         }
281cf0e004cSNinad Palsule         return;
282cf0e004cSNinad Palsule     }
283cf0e004cSNinad Palsule 
284cf0e004cSNinad Palsule     const size_t* memorySizeInKB = nullptr;
285cf0e004cSNinad Palsule 
286cf0e004cSNinad Palsule     const bool success = sdbusplus::unpackPropertiesNoThrow(
287cf0e004cSNinad Palsule         dbus_utils::UnpackErrorPrinter(), properties, "MemorySizeInKB",
288cf0e004cSNinad Palsule         memorySizeInKB);
289cf0e004cSNinad Palsule 
290cf0e004cSNinad Palsule     if (!success)
291cf0e004cSNinad Palsule     {
292ac106bf6SEd Tanous         messages::internalError(asyncResp->res);
293cf0e004cSNinad Palsule         return;
294cf0e004cSNinad Palsule     }
295cf0e004cSNinad Palsule 
296cf0e004cSNinad Palsule     if (memorySizeInKB != nullptr)
297cf0e004cSNinad Palsule     {
298cf0e004cSNinad Palsule         nlohmann::json& totalMemory =
299ac106bf6SEd Tanous             asyncResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"];
300cf0e004cSNinad Palsule         const uint64_t* preValue = totalMemory.get_ptr<const uint64_t*>();
301cf0e004cSNinad Palsule         if (preValue == nullptr)
302cf0e004cSNinad Palsule         {
303ac106bf6SEd Tanous             asyncResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] =
304cf0e004cSNinad Palsule                 *memorySizeInKB / static_cast<size_t>(1024 * 1024);
305cf0e004cSNinad Palsule         }
306cf0e004cSNinad Palsule         else
307cf0e004cSNinad Palsule         {
308ac106bf6SEd Tanous             asyncResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] =
309cf0e004cSNinad Palsule                 *memorySizeInKB / static_cast<size_t>(1024 * 1024) + *preValue;
310cf0e004cSNinad Palsule         }
3115fd0aafbSNinad Palsule         if constexpr (bmcwebEnableProcMemStatus)
3125fd0aafbSNinad Palsule         {
313ac106bf6SEd Tanous             asyncResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
3145fd0aafbSNinad Palsule                 "Enabled";
3155fd0aafbSNinad Palsule         }
316cf0e004cSNinad Palsule     }
317cf0e004cSNinad Palsule }
318cf0e004cSNinad Palsule 
319cf0e004cSNinad Palsule /*
320cf0e004cSNinad Palsule  * @brief Get getMemorySummary fields
321cf0e004cSNinad Palsule  *
322ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
323cf0e004cSNinad Palsule  * @param[in] service dbus service for memory Information
324cf0e004cSNinad Palsule  * @param[in] path dbus path for memory
325cf0e004cSNinad Palsule  *
326cf0e004cSNinad Palsule  * @return None.
327cf0e004cSNinad Palsule  */
328ac106bf6SEd Tanous inline void
329ac106bf6SEd Tanous     getMemorySummary(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
330ac106bf6SEd Tanous                      const std::string& service, const std::string& path)
331cf0e004cSNinad Palsule {
332cf0e004cSNinad Palsule     sdbusplus::asio::getAllProperties(
333cf0e004cSNinad Palsule         *crow::connections::systemBus, service, path,
334cf0e004cSNinad Palsule         "xyz.openbmc_project.Inventory.Item.Dimm",
335ac106bf6SEd Tanous         [asyncResp, service,
336cf0e004cSNinad Palsule          path](const boost::system::error_code& ec2,
337cf0e004cSNinad Palsule                const dbus::utility::DBusPropertiesMap& properties) {
338cf0e004cSNinad Palsule         if (ec2)
339cf0e004cSNinad Palsule         {
340cf0e004cSNinad Palsule             BMCWEB_LOG_ERROR << "DBUS response error " << ec2;
341ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
342cf0e004cSNinad Palsule             return;
343cf0e004cSNinad Palsule         }
344ac106bf6SEd Tanous         processMemoryProperties(asyncResp, service, path, properties);
345cf0e004cSNinad Palsule         });
346cf0e004cSNinad Palsule }
347cf0e004cSNinad Palsule 
348cf0e004cSNinad Palsule /*
349c5b2abe0SLewanczyk, Dawid  * @brief Retrieves computer system properties over dbus
350c5b2abe0SLewanczyk, Dawid  *
351ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
3528f9ee3cdSGunnar Mills  * @param[in] systemHealth  Shared HealthPopulate pointer
353c5b2abe0SLewanczyk, Dawid  *
354c5b2abe0SLewanczyk, Dawid  * @return None.
355c5b2abe0SLewanczyk, Dawid  */
356b5a76932SEd Tanous inline void
357ac106bf6SEd Tanous     getComputerSystem(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
358b5a76932SEd Tanous                       const std::shared_ptr<HealthPopulate>& systemHealth)
3591abe55efSEd Tanous {
36055c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get available system components.";
361e99073f5SGeorge Liu     constexpr std::array<std::string_view, 5> interfaces = {
362e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Decorator.Asset",
363e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Cpu",
364e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Dimm",
365e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.System",
366e99073f5SGeorge Liu         "xyz.openbmc_project.Common.UUID",
367e99073f5SGeorge Liu     };
368e99073f5SGeorge Liu     dbus::utility::getSubTree(
369e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
370ac106bf6SEd Tanous         [asyncResp,
371e99073f5SGeorge Liu          systemHealth](const boost::system::error_code& ec,
372b9d36b47SEd Tanous                        const dbus::utility::MapperGetSubTreeResponse& subtree) {
3731abe55efSEd Tanous         if (ec)
3741abe55efSEd Tanous         {
37555c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error";
376ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
377c5b2abe0SLewanczyk, Dawid             return;
378c5b2abe0SLewanczyk, Dawid         }
379c5b2abe0SLewanczyk, Dawid         // Iterate over all retrieved ObjectPaths.
380002d39b4SEd Tanous         for (const std::pair<
381002d39b4SEd Tanous                  std::string,
382002d39b4SEd Tanous                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3831214b7e7SGunnar Mills                  object : subtree)
3841abe55efSEd Tanous         {
385c5b2abe0SLewanczyk, Dawid             const std::string& path = object.first;
38655c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Got path: " << path;
387002d39b4SEd Tanous             const std::vector<std::pair<std::string, std::vector<std::string>>>&
3881214b7e7SGunnar Mills                 connectionNames = object.second;
38926f6976fSEd Tanous             if (connectionNames.empty())
3901abe55efSEd Tanous             {
391c5b2abe0SLewanczyk, Dawid                 continue;
392c5b2abe0SLewanczyk, Dawid             }
393029573d4SEd Tanous 
3945fd0aafbSNinad Palsule             std::shared_ptr<HealthPopulate> memoryHealth = nullptr;
3955fd0aafbSNinad Palsule             std::shared_ptr<HealthPopulate> cpuHealth = nullptr;
3965bc2dc8eSJames Feist 
3975fd0aafbSNinad Palsule             if constexpr (bmcwebEnableProcMemStatus)
3985fd0aafbSNinad Palsule             {
3995fd0aafbSNinad Palsule                 memoryHealth = std::make_shared<HealthPopulate>(
400ac106bf6SEd Tanous                     asyncResp, "/MemorySummary/Status"_json_pointer);
4015fd0aafbSNinad Palsule                 systemHealth->children.emplace_back(memoryHealth);
4025bc2dc8eSJames Feist 
40313451e39SWilly Tu                 if constexpr (bmcwebEnableHealthPopulate)
40413451e39SWilly Tu                 {
4055fd0aafbSNinad Palsule                     cpuHealth = std::make_shared<HealthPopulate>(
406ac106bf6SEd Tanous                         asyncResp, "/ProcessorSummary/Status"_json_pointer);
4075fd0aafbSNinad Palsule 
4085bc2dc8eSJames Feist                     systemHealth->children.emplace_back(cpuHealth);
40913451e39SWilly Tu                 }
4105fd0aafbSNinad Palsule             }
4115bc2dc8eSJames Feist 
4126c34de48SEd Tanous             // This is not system, so check if it's cpu, dimm, UUID or
4136c34de48SEd Tanous             // BiosVer
41404a258f4SEd Tanous             for (const auto& connection : connectionNames)
4151abe55efSEd Tanous             {
41604a258f4SEd Tanous                 for (const auto& interfaceName : connection.second)
4171abe55efSEd Tanous                 {
41804a258f4SEd Tanous                     if (interfaceName ==
41904a258f4SEd Tanous                         "xyz.openbmc_project.Inventory.Item.Dimm")
4201abe55efSEd Tanous                     {
4211abe55efSEd Tanous                         BMCWEB_LOG_DEBUG
42204a258f4SEd Tanous                             << "Found Dimm, now get its properties.";
4239d3ae10eSAlpana Kumari 
424ac106bf6SEd Tanous                         getMemorySummary(asyncResp, connection.first, path);
4255bc2dc8eSJames Feist 
4265fd0aafbSNinad Palsule                         if constexpr (bmcwebEnableProcMemStatus)
4275fd0aafbSNinad Palsule                         {
4285bc2dc8eSJames Feist                             memoryHealth->inventory.emplace_back(path);
4291abe55efSEd Tanous                         }
4305fd0aafbSNinad Palsule                     }
43104a258f4SEd Tanous                     else if (interfaceName ==
43204a258f4SEd Tanous                              "xyz.openbmc_project.Inventory.Item.Cpu")
4331abe55efSEd Tanous                     {
4341abe55efSEd Tanous                         BMCWEB_LOG_DEBUG
43504a258f4SEd Tanous                             << "Found Cpu, now get its properties.";
43657e8c9beSAlpana Kumari 
437ac106bf6SEd Tanous                         getProcessorSummary(asyncResp, connection.first, path);
4385bc2dc8eSJames Feist 
4395fd0aafbSNinad Palsule                         if constexpr (bmcwebEnableProcMemStatus)
4405fd0aafbSNinad Palsule                         {
4415bc2dc8eSJames Feist                             cpuHealth->inventory.emplace_back(path);
4421abe55efSEd Tanous                         }
4435fd0aafbSNinad Palsule                     }
444002d39b4SEd Tanous                     else if (interfaceName == "xyz.openbmc_project.Common.UUID")
4451abe55efSEd Tanous                     {
4461abe55efSEd Tanous                         BMCWEB_LOG_DEBUG
44704a258f4SEd Tanous                             << "Found UUID, now get its properties.";
448bc1d29deSKrzysztof Grobelny 
449bc1d29deSKrzysztof Grobelny                         sdbusplus::asio::getAllProperties(
450bc1d29deSKrzysztof Grobelny                             *crow::connections::systemBus, connection.first,
451bc1d29deSKrzysztof Grobelny                             path, "xyz.openbmc_project.Common.UUID",
452ac106bf6SEd Tanous                             [asyncResp](const boost::system::error_code& ec3,
453b9d36b47SEd Tanous                                         const dbus::utility::DBusPropertiesMap&
4541214b7e7SGunnar Mills                                             properties) {
455cb13a392SEd Tanous                             if (ec3)
4561abe55efSEd Tanous                             {
457002d39b4SEd Tanous                                 BMCWEB_LOG_DEBUG << "DBUS response error "
458002d39b4SEd Tanous                                                  << ec3;
459ac106bf6SEd Tanous                                 messages::internalError(asyncResp->res);
460c5b2abe0SLewanczyk, Dawid                                 return;
461c5b2abe0SLewanczyk, Dawid                             }
462002d39b4SEd Tanous                             BMCWEB_LOG_DEBUG << "Got " << properties.size()
463c5b2abe0SLewanczyk, Dawid                                              << " UUID properties.";
46404a258f4SEd Tanous 
465bc1d29deSKrzysztof Grobelny                             const std::string* uUID = nullptr;
466bc1d29deSKrzysztof Grobelny 
467bc1d29deSKrzysztof Grobelny                             const bool success =
468bc1d29deSKrzysztof Grobelny                                 sdbusplus::unpackPropertiesNoThrow(
469bc1d29deSKrzysztof Grobelny                                     dbus_utils::UnpackErrorPrinter(),
470bc1d29deSKrzysztof Grobelny                                     properties, "UUID", uUID);
471bc1d29deSKrzysztof Grobelny 
472bc1d29deSKrzysztof Grobelny                             if (!success)
4731abe55efSEd Tanous                             {
474ac106bf6SEd Tanous                                 messages::internalError(asyncResp->res);
475bc1d29deSKrzysztof Grobelny                                 return;
476bc1d29deSKrzysztof Grobelny                             }
477bc1d29deSKrzysztof Grobelny 
478bc1d29deSKrzysztof Grobelny                             if (uUID != nullptr)
479bc1d29deSKrzysztof Grobelny                             {
480bc1d29deSKrzysztof Grobelny                                 std::string valueStr = *uUID;
48104a258f4SEd Tanous                                 if (valueStr.size() == 32)
4821abe55efSEd Tanous                                 {
483029573d4SEd Tanous                                     valueStr.insert(8, 1, '-');
484029573d4SEd Tanous                                     valueStr.insert(13, 1, '-');
485029573d4SEd Tanous                                     valueStr.insert(18, 1, '-');
486029573d4SEd Tanous                                     valueStr.insert(23, 1, '-');
48704a258f4SEd Tanous                                 }
488bc1d29deSKrzysztof Grobelny                                 BMCWEB_LOG_DEBUG << "UUID = " << valueStr;
489ac106bf6SEd Tanous                                 asyncResp->res.jsonValue["UUID"] = valueStr;
490c5b2abe0SLewanczyk, Dawid                             }
491bc1d29deSKrzysztof Grobelny                             });
492c5b2abe0SLewanczyk, Dawid                     }
493029573d4SEd Tanous                     else if (interfaceName ==
494029573d4SEd Tanous                              "xyz.openbmc_project.Inventory.Item.System")
4951abe55efSEd Tanous                     {
496bc1d29deSKrzysztof Grobelny                         sdbusplus::asio::getAllProperties(
497bc1d29deSKrzysztof Grobelny                             *crow::connections::systemBus, connection.first,
498bc1d29deSKrzysztof Grobelny                             path,
499bc1d29deSKrzysztof Grobelny                             "xyz.openbmc_project.Inventory.Decorator.Asset",
500ac106bf6SEd Tanous                             [asyncResp](const boost::system::error_code& ec2,
501b9d36b47SEd Tanous                                         const dbus::utility::DBusPropertiesMap&
5021214b7e7SGunnar Mills                                             propertiesList) {
503cb13a392SEd Tanous                             if (ec2)
504029573d4SEd Tanous                             {
505e4a4b9a9SJames Feist                                 // doesn't have to include this
506e4a4b9a9SJames Feist                                 // interface
507029573d4SEd Tanous                                 return;
508029573d4SEd Tanous                             }
509002d39b4SEd Tanous                             BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
510029573d4SEd Tanous                                              << " properties for system";
511bc1d29deSKrzysztof Grobelny 
512bc1d29deSKrzysztof Grobelny                             const std::string* partNumber = nullptr;
513bc1d29deSKrzysztof Grobelny                             const std::string* serialNumber = nullptr;
514bc1d29deSKrzysztof Grobelny                             const std::string* manufacturer = nullptr;
515bc1d29deSKrzysztof Grobelny                             const std::string* model = nullptr;
516bc1d29deSKrzysztof Grobelny                             const std::string* subModel = nullptr;
517bc1d29deSKrzysztof Grobelny 
518bc1d29deSKrzysztof Grobelny                             const bool success =
519bc1d29deSKrzysztof Grobelny                                 sdbusplus::unpackPropertiesNoThrow(
520bc1d29deSKrzysztof Grobelny                                     dbus_utils::UnpackErrorPrinter(),
521bc1d29deSKrzysztof Grobelny                                     propertiesList, "PartNumber", partNumber,
522bc1d29deSKrzysztof Grobelny                                     "SerialNumber", serialNumber,
523bc1d29deSKrzysztof Grobelny                                     "Manufacturer", manufacturer, "Model",
524bc1d29deSKrzysztof Grobelny                                     model, "SubModel", subModel);
525bc1d29deSKrzysztof Grobelny 
526bc1d29deSKrzysztof Grobelny                             if (!success)
527029573d4SEd Tanous                             {
528ac106bf6SEd Tanous                                 messages::internalError(asyncResp->res);
529bc1d29deSKrzysztof Grobelny                                 return;
530029573d4SEd Tanous                             }
531bc1d29deSKrzysztof Grobelny 
532bc1d29deSKrzysztof Grobelny                             if (partNumber != nullptr)
533bc1d29deSKrzysztof Grobelny                             {
534ac106bf6SEd Tanous                                 asyncResp->res.jsonValue["PartNumber"] =
535bc1d29deSKrzysztof Grobelny                                     *partNumber;
536029573d4SEd Tanous                             }
537bc1d29deSKrzysztof Grobelny 
538bc1d29deSKrzysztof Grobelny                             if (serialNumber != nullptr)
539bc1d29deSKrzysztof Grobelny                             {
540ac106bf6SEd Tanous                                 asyncResp->res.jsonValue["SerialNumber"] =
541bc1d29deSKrzysztof Grobelny                                     *serialNumber;
542bc1d29deSKrzysztof Grobelny                             }
543bc1d29deSKrzysztof Grobelny 
544bc1d29deSKrzysztof Grobelny                             if (manufacturer != nullptr)
545bc1d29deSKrzysztof Grobelny                             {
546ac106bf6SEd Tanous                                 asyncResp->res.jsonValue["Manufacturer"] =
547bc1d29deSKrzysztof Grobelny                                     *manufacturer;
548bc1d29deSKrzysztof Grobelny                             }
549bc1d29deSKrzysztof Grobelny 
550bc1d29deSKrzysztof Grobelny                             if (model != nullptr)
551bc1d29deSKrzysztof Grobelny                             {
552ac106bf6SEd Tanous                                 asyncResp->res.jsonValue["Model"] = *model;
553bc1d29deSKrzysztof Grobelny                             }
554bc1d29deSKrzysztof Grobelny 
555bc1d29deSKrzysztof Grobelny                             if (subModel != nullptr)
556bc1d29deSKrzysztof Grobelny                             {
557ac106bf6SEd Tanous                                 asyncResp->res.jsonValue["SubModel"] =
558ac106bf6SEd Tanous                                     *subModel;
559fc5afcf9Sbeccabroek                             }
560c1e236a6SGunnar Mills 
561cb7e1e7bSAndrew Geissler                             // Grab the bios version
562eee0013eSWilly Tu                             sw_util::populateSoftwareInformation(
563ac106bf6SEd Tanous                                 asyncResp, sw_util::biosPurpose, "BiosVersion",
564002d39b4SEd Tanous                                 false);
565bc1d29deSKrzysztof Grobelny                             });
566e4a4b9a9SJames Feist 
5671e1e598dSJonathan Doman                         sdbusplus::asio::getProperty<std::string>(
5681e1e598dSJonathan Doman                             *crow::connections::systemBus, connection.first,
5691e1e598dSJonathan Doman                             path,
5701e1e598dSJonathan Doman                             "xyz.openbmc_project.Inventory.Decorator."
5711e1e598dSJonathan Doman                             "AssetTag",
5721e1e598dSJonathan Doman                             "AssetTag",
573ac106bf6SEd Tanous                             [asyncResp](const boost::system::error_code& ec2,
5741e1e598dSJonathan Doman                                         const std::string& value) {
575cb13a392SEd Tanous                             if (ec2)
576e4a4b9a9SJames Feist                             {
577e4a4b9a9SJames Feist                                 // doesn't have to include this
578e4a4b9a9SJames Feist                                 // interface
579e4a4b9a9SJames Feist                                 return;
580e4a4b9a9SJames Feist                             }
581e4a4b9a9SJames Feist 
582ac106bf6SEd Tanous                             asyncResp->res.jsonValue["AssetTag"] = value;
5831e1e598dSJonathan Doman                             });
584029573d4SEd Tanous                     }
585029573d4SEd Tanous                 }
586029573d4SEd Tanous             }
587c5b2abe0SLewanczyk, Dawid         }
5886617338dSEd Tanous         });
589c5b2abe0SLewanczyk, Dawid }
590c5b2abe0SLewanczyk, Dawid 
591c5b2abe0SLewanczyk, Dawid /**
592c5b2abe0SLewanczyk, Dawid  * @brief Retrieves host state properties over dbus
593c5b2abe0SLewanczyk, Dawid  *
594ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
595c5b2abe0SLewanczyk, Dawid  *
596c5b2abe0SLewanczyk, Dawid  * @return None.
597c5b2abe0SLewanczyk, Dawid  */
598ac106bf6SEd Tanous inline void getHostState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
5991abe55efSEd Tanous {
60055c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get host information.";
6011e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
6021e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
6031e1e598dSJonathan Doman         "/xyz/openbmc_project/state/host0", "xyz.openbmc_project.State.Host",
6041e1e598dSJonathan Doman         "CurrentHostState",
605ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
6061e1e598dSJonathan Doman                     const std::string& hostState) {
6071abe55efSEd Tanous         if (ec)
6081abe55efSEd Tanous         {
60922228c28SAndrew Geissler             if (ec == boost::system::errc::host_unreachable)
61022228c28SAndrew Geissler             {
61122228c28SAndrew Geissler                 // Service not available, no error, just don't return
61222228c28SAndrew Geissler                 // host state info
61322228c28SAndrew Geissler                 BMCWEB_LOG_DEBUG << "Service not available " << ec;
61422228c28SAndrew Geissler                 return;
61522228c28SAndrew Geissler             }
61622228c28SAndrew Geissler             BMCWEB_LOG_ERROR << "DBUS response error " << ec;
617ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
618c5b2abe0SLewanczyk, Dawid             return;
619c5b2abe0SLewanczyk, Dawid         }
6206617338dSEd Tanous 
6211e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Host state: " << hostState;
622c5b2abe0SLewanczyk, Dawid         // Verify Host State
6231e1e598dSJonathan Doman         if (hostState == "xyz.openbmc_project.State.Host.HostState.Running")
6241abe55efSEd Tanous         {
625ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "On";
626ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
6271abe55efSEd Tanous         }
6281e1e598dSJonathan Doman         else if (hostState ==
6290fda0f12SGeorge Liu                  "xyz.openbmc_project.State.Host.HostState.Quiesced")
6308c888608SGunnar Mills         {
631ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "On";
632ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "Quiesced";
6338c888608SGunnar Mills         }
6341e1e598dSJonathan Doman         else if (hostState ==
6350fda0f12SGeorge Liu                  "xyz.openbmc_project.State.Host.HostState.DiagnosticMode")
63683935af9SAndrew Geissler         {
637ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "On";
638ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "InTest";
63983935af9SAndrew Geissler         }
6400fda0f12SGeorge Liu         else if (
6411e1e598dSJonathan Doman             hostState ==
6420fda0f12SGeorge Liu             "xyz.openbmc_project.State.Host.HostState.TransitioningToRunning")
6431a2a1437SAndrew Geissler         {
644ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "PoweringOn";
645ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "Starting";
6461a2a1437SAndrew Geissler         }
647002d39b4SEd Tanous         else if (hostState ==
6480fda0f12SGeorge Liu                  "xyz.openbmc_project.State.Host.HostState.TransitioningToOff")
6491a2a1437SAndrew Geissler         {
650ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "PoweringOff";
651ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
6521a2a1437SAndrew Geissler         }
6531abe55efSEd Tanous         else
6541abe55efSEd Tanous         {
655ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "Off";
656ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
657c5b2abe0SLewanczyk, Dawid         }
6581e1e598dSJonathan Doman         });
659c5b2abe0SLewanczyk, Dawid }
660c5b2abe0SLewanczyk, Dawid 
661c5b2abe0SLewanczyk, Dawid /**
662786d0f60SGunnar Mills  * @brief Translates boot source DBUS property value to redfish.
663491d8ee7SSantosh Puranik  *
664491d8ee7SSantosh Puranik  * @param[in] dbusSource    The boot source in DBUS speak.
665491d8ee7SSantosh Puranik  *
666491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source in Redfish terms. If translation
667491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
668491d8ee7SSantosh Puranik  */
66923a21a1cSEd Tanous inline std::string dbusToRfBootSource(const std::string& dbusSource)
670491d8ee7SSantosh Puranik {
671491d8ee7SSantosh Puranik     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
672491d8ee7SSantosh Puranik     {
673491d8ee7SSantosh Puranik         return "None";
674491d8ee7SSantosh Puranik     }
6753174e4dfSEd Tanous     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Disk")
676491d8ee7SSantosh Puranik     {
677491d8ee7SSantosh Puranik         return "Hdd";
678491d8ee7SSantosh Puranik     }
6793174e4dfSEd Tanous     if (dbusSource ==
680a71dc0b7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia")
681491d8ee7SSantosh Puranik     {
682491d8ee7SSantosh Puranik         return "Cd";
683491d8ee7SSantosh Puranik     }
6843174e4dfSEd Tanous     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Network")
685491d8ee7SSantosh Puranik     {
686491d8ee7SSantosh Puranik         return "Pxe";
687491d8ee7SSantosh Puranik     }
6883174e4dfSEd Tanous     if (dbusSource ==
689944ffaf9SJohnathan Mantey         "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia")
6909f16b2c1SJennifer Lee     {
6919f16b2c1SJennifer Lee         return "Usb";
6929f16b2c1SJennifer Lee     }
693491d8ee7SSantosh Puranik     return "";
694491d8ee7SSantosh Puranik }
695491d8ee7SSantosh Puranik 
696491d8ee7SSantosh Puranik /**
697cd9a4666SKonstantin Aladyshev  * @brief Translates boot type DBUS property value to redfish.
698cd9a4666SKonstantin Aladyshev  *
699cd9a4666SKonstantin Aladyshev  * @param[in] dbusType    The boot type in DBUS speak.
700cd9a4666SKonstantin Aladyshev  *
701cd9a4666SKonstantin Aladyshev  * @return Returns as a string, the boot type in Redfish terms. If translation
702cd9a4666SKonstantin Aladyshev  * cannot be done, returns an empty string.
703cd9a4666SKonstantin Aladyshev  */
704cd9a4666SKonstantin Aladyshev inline std::string dbusToRfBootType(const std::string& dbusType)
705cd9a4666SKonstantin Aladyshev {
706cd9a4666SKonstantin Aladyshev     if (dbusType == "xyz.openbmc_project.Control.Boot.Type.Types.Legacy")
707cd9a4666SKonstantin Aladyshev     {
708cd9a4666SKonstantin Aladyshev         return "Legacy";
709cd9a4666SKonstantin Aladyshev     }
710cd9a4666SKonstantin Aladyshev     if (dbusType == "xyz.openbmc_project.Control.Boot.Type.Types.EFI")
711cd9a4666SKonstantin Aladyshev     {
712cd9a4666SKonstantin Aladyshev         return "UEFI";
713cd9a4666SKonstantin Aladyshev     }
714cd9a4666SKonstantin Aladyshev     return "";
715cd9a4666SKonstantin Aladyshev }
716cd9a4666SKonstantin Aladyshev 
717cd9a4666SKonstantin Aladyshev /**
718786d0f60SGunnar Mills  * @brief Translates boot mode DBUS property value to redfish.
719491d8ee7SSantosh Puranik  *
720491d8ee7SSantosh Puranik  * @param[in] dbusMode    The boot mode in DBUS speak.
721491d8ee7SSantosh Puranik  *
722491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode in Redfish terms. If translation
723491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
724491d8ee7SSantosh Puranik  */
72523a21a1cSEd Tanous inline std::string dbusToRfBootMode(const std::string& dbusMode)
726491d8ee7SSantosh Puranik {
727491d8ee7SSantosh Puranik     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
728491d8ee7SSantosh Puranik     {
729491d8ee7SSantosh Puranik         return "None";
730491d8ee7SSantosh Puranik     }
7313174e4dfSEd Tanous     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe")
732491d8ee7SSantosh Puranik     {
733491d8ee7SSantosh Puranik         return "Diags";
734491d8ee7SSantosh Puranik     }
7353174e4dfSEd Tanous     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup")
736491d8ee7SSantosh Puranik     {
737491d8ee7SSantosh Puranik         return "BiosSetup";
738491d8ee7SSantosh Puranik     }
739491d8ee7SSantosh Puranik     return "";
740491d8ee7SSantosh Puranik }
741491d8ee7SSantosh Puranik 
742491d8ee7SSantosh Puranik /**
743e43914b3SAndrew Geissler  * @brief Translates boot progress DBUS property value to redfish.
744e43914b3SAndrew Geissler  *
745e43914b3SAndrew Geissler  * @param[in] dbusBootProgress    The boot progress in DBUS speak.
746e43914b3SAndrew Geissler  *
747e43914b3SAndrew Geissler  * @return Returns as a string, the boot progress in Redfish terms. If
748e43914b3SAndrew Geissler  *         translation cannot be done, returns "None".
749e43914b3SAndrew Geissler  */
750e43914b3SAndrew Geissler inline std::string dbusToRfBootProgress(const std::string& dbusBootProgress)
751e43914b3SAndrew Geissler {
752e43914b3SAndrew Geissler     // Now convert the D-Bus BootProgress to the appropriate Redfish
753e43914b3SAndrew Geissler     // enum
754e43914b3SAndrew Geissler     std::string rfBpLastState = "None";
755e43914b3SAndrew Geissler     if (dbusBootProgress == "xyz.openbmc_project.State.Boot.Progress."
756e43914b3SAndrew Geissler                             "ProgressStages.Unspecified")
757e43914b3SAndrew Geissler     {
758e43914b3SAndrew Geissler         rfBpLastState = "None";
759e43914b3SAndrew Geissler     }
760e43914b3SAndrew Geissler     else if (dbusBootProgress ==
761e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
762e43914b3SAndrew Geissler              "PrimaryProcInit")
763e43914b3SAndrew Geissler     {
764e43914b3SAndrew Geissler         rfBpLastState = "PrimaryProcessorInitializationStarted";
765e43914b3SAndrew Geissler     }
766e43914b3SAndrew Geissler     else if (dbusBootProgress ==
767e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
768e43914b3SAndrew Geissler              "BusInit")
769e43914b3SAndrew Geissler     {
770e43914b3SAndrew Geissler         rfBpLastState = "BusInitializationStarted";
771e43914b3SAndrew Geissler     }
772e43914b3SAndrew Geissler     else if (dbusBootProgress ==
773e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
774e43914b3SAndrew Geissler              "MemoryInit")
775e43914b3SAndrew Geissler     {
776e43914b3SAndrew Geissler         rfBpLastState = "MemoryInitializationStarted";
777e43914b3SAndrew Geissler     }
778e43914b3SAndrew Geissler     else if (dbusBootProgress ==
779e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
780e43914b3SAndrew Geissler              "SecondaryProcInit")
781e43914b3SAndrew Geissler     {
782e43914b3SAndrew Geissler         rfBpLastState = "SecondaryProcessorInitializationStarted";
783e43914b3SAndrew Geissler     }
784e43914b3SAndrew Geissler     else if (dbusBootProgress ==
785e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
786e43914b3SAndrew Geissler              "PCIInit")
787e43914b3SAndrew Geissler     {
788e43914b3SAndrew Geissler         rfBpLastState = "PCIResourceConfigStarted";
789e43914b3SAndrew Geissler     }
790e43914b3SAndrew Geissler     else if (dbusBootProgress ==
791e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
792e43914b3SAndrew Geissler              "SystemSetup")
793e43914b3SAndrew Geissler     {
794e43914b3SAndrew Geissler         rfBpLastState = "SetupEntered";
795e43914b3SAndrew Geissler     }
796e43914b3SAndrew Geissler     else if (dbusBootProgress ==
797e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
798e43914b3SAndrew Geissler              "SystemInitComplete")
799e43914b3SAndrew Geissler     {
800e43914b3SAndrew Geissler         rfBpLastState = "SystemHardwareInitializationComplete";
801e43914b3SAndrew Geissler     }
802e43914b3SAndrew Geissler     else if (dbusBootProgress ==
803e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
804e43914b3SAndrew Geissler              "OSStart")
805e43914b3SAndrew Geissler     {
806e43914b3SAndrew Geissler         rfBpLastState = "OSBootStarted";
807e43914b3SAndrew Geissler     }
808e43914b3SAndrew Geissler     else if (dbusBootProgress ==
809e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
810e43914b3SAndrew Geissler              "OSRunning")
811e43914b3SAndrew Geissler     {
812e43914b3SAndrew Geissler         rfBpLastState = "OSRunning";
813e43914b3SAndrew Geissler     }
814e43914b3SAndrew Geissler     else
815e43914b3SAndrew Geissler     {
816e43914b3SAndrew Geissler         BMCWEB_LOG_DEBUG << "Unsupported D-Bus BootProgress "
817e43914b3SAndrew Geissler                          << dbusBootProgress;
818e43914b3SAndrew Geissler         // Just return the default
819e43914b3SAndrew Geissler     }
820e43914b3SAndrew Geissler     return rfBpLastState;
821e43914b3SAndrew Geissler }
822e43914b3SAndrew Geissler 
823e43914b3SAndrew Geissler /**
824786d0f60SGunnar Mills  * @brief Translates boot source from Redfish to the DBus boot paths.
825491d8ee7SSantosh Puranik  *
826491d8ee7SSantosh Puranik  * @param[in] rfSource    The boot source in Redfish.
827944ffaf9SJohnathan Mantey  * @param[out] bootSource The DBus source
828944ffaf9SJohnathan Mantey  * @param[out] bootMode   the DBus boot mode
829491d8ee7SSantosh Puranik  *
830944ffaf9SJohnathan Mantey  * @return Integer error code.
831491d8ee7SSantosh Puranik  */
832ac106bf6SEd Tanous inline int
833ac106bf6SEd Tanous     assignBootParameters(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
834ac106bf6SEd Tanous                          const std::string& rfSource, std::string& bootSource,
835ac106bf6SEd Tanous                          std::string& bootMode)
836491d8ee7SSantosh Puranik {
837c21865c4SKonstantin Aladyshev     bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
838c21865c4SKonstantin Aladyshev     bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
839944ffaf9SJohnathan Mantey 
840491d8ee7SSantosh Puranik     if (rfSource == "None")
841491d8ee7SSantosh Puranik     {
842944ffaf9SJohnathan Mantey         return 0;
843491d8ee7SSantosh Puranik     }
8443174e4dfSEd Tanous     if (rfSource == "Pxe")
845491d8ee7SSantosh Puranik     {
846944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Network";
847944ffaf9SJohnathan Mantey     }
848944ffaf9SJohnathan Mantey     else if (rfSource == "Hdd")
849944ffaf9SJohnathan Mantey     {
850944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Disk";
851944ffaf9SJohnathan Mantey     }
852944ffaf9SJohnathan Mantey     else if (rfSource == "Diags")
853944ffaf9SJohnathan Mantey     {
854944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe";
855944ffaf9SJohnathan Mantey     }
856944ffaf9SJohnathan Mantey     else if (rfSource == "Cd")
857944ffaf9SJohnathan Mantey     {
858944ffaf9SJohnathan Mantey         bootSource =
859944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia";
860944ffaf9SJohnathan Mantey     }
861944ffaf9SJohnathan Mantey     else if (rfSource == "BiosSetup")
862944ffaf9SJohnathan Mantey     {
863944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup";
864491d8ee7SSantosh Puranik     }
8659f16b2c1SJennifer Lee     else if (rfSource == "Usb")
8669f16b2c1SJennifer Lee     {
867944ffaf9SJohnathan Mantey         bootSource =
868944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia";
8699f16b2c1SJennifer Lee     }
870491d8ee7SSantosh Puranik     else
871491d8ee7SSantosh Puranik     {
8720fda0f12SGeorge Liu         BMCWEB_LOG_DEBUG
8730fda0f12SGeorge Liu             << "Invalid property value for BootSourceOverrideTarget: "
874944ffaf9SJohnathan Mantey             << bootSource;
875ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, rfSource,
876944ffaf9SJohnathan Mantey                                          "BootSourceTargetOverride");
877944ffaf9SJohnathan Mantey         return -1;
878491d8ee7SSantosh Puranik     }
879944ffaf9SJohnathan Mantey     return 0;
880491d8ee7SSantosh Puranik }
8811981771bSAli Ahmed 
882978b8803SAndrew Geissler /**
883978b8803SAndrew Geissler  * @brief Retrieves boot progress of the system
884978b8803SAndrew Geissler  *
885ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
886978b8803SAndrew Geissler  *
887978b8803SAndrew Geissler  * @return None.
888978b8803SAndrew Geissler  */
889ac106bf6SEd Tanous inline void getBootProgress(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
890978b8803SAndrew Geissler {
8911e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
8921e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
8931e1e598dSJonathan Doman         "/xyz/openbmc_project/state/host0",
8941e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.Progress", "BootProgress",
895ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
8961e1e598dSJonathan Doman                     const std::string& bootProgressStr) {
897978b8803SAndrew Geissler         if (ec)
898978b8803SAndrew Geissler         {
899978b8803SAndrew Geissler             // BootProgress is an optional object so just do nothing if
900978b8803SAndrew Geissler             // not found
901978b8803SAndrew Geissler             return;
902978b8803SAndrew Geissler         }
903978b8803SAndrew Geissler 
9041e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Boot Progress: " << bootProgressStr;
905978b8803SAndrew Geissler 
906ac106bf6SEd Tanous         asyncResp->res.jsonValue["BootProgress"]["LastState"] =
907e43914b3SAndrew Geissler             dbusToRfBootProgress(bootProgressStr);
9081e1e598dSJonathan Doman         });
909978b8803SAndrew Geissler }
910491d8ee7SSantosh Puranik 
911491d8ee7SSantosh Puranik /**
912b6d5d45cSHieu Huynh  * @brief Retrieves boot progress Last Update of the system
913b6d5d45cSHieu Huynh  *
914ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
915b6d5d45cSHieu Huynh  *
916b6d5d45cSHieu Huynh  * @return None.
917b6d5d45cSHieu Huynh  */
918b6d5d45cSHieu Huynh inline void getBootProgressLastStateTime(
919ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
920b6d5d45cSHieu Huynh {
921b6d5d45cSHieu Huynh     sdbusplus::asio::getProperty<uint64_t>(
922b6d5d45cSHieu Huynh         *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
923b6d5d45cSHieu Huynh         "/xyz/openbmc_project/state/host0",
924b6d5d45cSHieu Huynh         "xyz.openbmc_project.State.Boot.Progress", "BootProgressLastUpdate",
925ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
926b6d5d45cSHieu Huynh                     const uint64_t lastStateTime) {
927b6d5d45cSHieu Huynh         if (ec)
928b6d5d45cSHieu Huynh         {
929b6d5d45cSHieu Huynh             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
930b6d5d45cSHieu Huynh             return;
931b6d5d45cSHieu Huynh         }
932b6d5d45cSHieu Huynh 
933b6d5d45cSHieu Huynh         // BootProgressLastUpdate is the last time the BootProgress property
934b6d5d45cSHieu Huynh         // was updated. The time is the Epoch time, number of microseconds
935b6d5d45cSHieu Huynh         // since 1 Jan 1970 00::00::00 UTC."
936b6d5d45cSHieu Huynh         // https://github.com/openbmc/phosphor-dbus-interfaces/blob/master/
937b6d5d45cSHieu Huynh         // yaml/xyz/openbmc_project/State/Boot/Progress.interface.yaml#L11
938b6d5d45cSHieu Huynh 
939b6d5d45cSHieu Huynh         // Convert to ISO 8601 standard
940ac106bf6SEd Tanous         asyncResp->res.jsonValue["BootProgress"]["LastStateTime"] =
941b6d5d45cSHieu Huynh             redfish::time_utils::getDateTimeUintUs(lastStateTime);
942b6d5d45cSHieu Huynh         });
943b6d5d45cSHieu Huynh }
944b6d5d45cSHieu Huynh 
945b6d5d45cSHieu Huynh /**
946c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override type over DBUS and fills out the response
947cd9a4666SKonstantin Aladyshev  *
948ac106bf6SEd Tanous  * @param[in] asyncResp         Shared pointer for generating response message.
949cd9a4666SKonstantin Aladyshev  *
950cd9a4666SKonstantin Aladyshev  * @return None.
951cd9a4666SKonstantin Aladyshev  */
952cd9a4666SKonstantin Aladyshev 
953ac106bf6SEd Tanous inline void
954ac106bf6SEd Tanous     getBootOverrideType(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
955cd9a4666SKonstantin Aladyshev {
9561e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
9571e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
9581e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
9591e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Type", "BootType",
960ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
9611e1e598dSJonathan Doman                     const std::string& bootType) {
962cd9a4666SKonstantin Aladyshev         if (ec)
963cd9a4666SKonstantin Aladyshev         {
964cd9a4666SKonstantin Aladyshev             // not an error, don't have to have the interface
965cd9a4666SKonstantin Aladyshev             return;
966cd9a4666SKonstantin Aladyshev         }
967cd9a4666SKonstantin Aladyshev 
9681e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Boot type: " << bootType;
969cd9a4666SKonstantin Aladyshev 
970ac106bf6SEd Tanous         asyncResp->res
971ac106bf6SEd Tanous             .jsonValue["Boot"]
972002d39b4SEd Tanous                       ["BootSourceOverrideMode@Redfish.AllowableValues"] =
973613dabeaSEd Tanous             nlohmann::json::array_t({"Legacy", "UEFI"});
974cd9a4666SKonstantin Aladyshev 
9751e1e598dSJonathan Doman         auto rfType = dbusToRfBootType(bootType);
976cd9a4666SKonstantin Aladyshev         if (rfType.empty())
977cd9a4666SKonstantin Aladyshev         {
978ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
979cd9a4666SKonstantin Aladyshev             return;
980cd9a4666SKonstantin Aladyshev         }
981cd9a4666SKonstantin Aladyshev 
982ac106bf6SEd Tanous         asyncResp->res.jsonValue["Boot"]["BootSourceOverrideMode"] = rfType;
9831e1e598dSJonathan Doman         });
984cd9a4666SKonstantin Aladyshev }
985cd9a4666SKonstantin Aladyshev 
986cd9a4666SKonstantin Aladyshev /**
987c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override mode over DBUS and fills out the response
988491d8ee7SSantosh Puranik  *
989ac106bf6SEd Tanous  * @param[in] asyncResp         Shared pointer for generating response message.
990491d8ee7SSantosh Puranik  *
991491d8ee7SSantosh Puranik  * @return None.
992491d8ee7SSantosh Puranik  */
993c21865c4SKonstantin Aladyshev 
994ac106bf6SEd Tanous inline void
995ac106bf6SEd Tanous     getBootOverrideMode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
996491d8ee7SSantosh Puranik {
9971e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
9981e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
9991e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
10001e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
1001ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
10021e1e598dSJonathan Doman                     const std::string& bootModeStr) {
1003491d8ee7SSantosh Puranik         if (ec)
1004491d8ee7SSantosh Puranik         {
1005491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1006ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1007491d8ee7SSantosh Puranik             return;
1008491d8ee7SSantosh Puranik         }
1009491d8ee7SSantosh Puranik 
10101e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Boot mode: " << bootModeStr;
1011491d8ee7SSantosh Puranik 
1012ac106bf6SEd Tanous         asyncResp->res
10130fda0f12SGeorge Liu             .jsonValue["Boot"]
1014002d39b4SEd Tanous                       ["BootSourceOverrideTarget@Redfish.AllowableValues"] = {
1015002d39b4SEd Tanous             "None", "Pxe", "Hdd", "Cd", "Diags", "BiosSetup", "Usb"};
1016491d8ee7SSantosh Puranik 
10171e1e598dSJonathan Doman         if (bootModeStr !=
1018491d8ee7SSantosh Puranik             "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
1019491d8ee7SSantosh Puranik         {
10201e1e598dSJonathan Doman             auto rfMode = dbusToRfBootMode(bootModeStr);
1021491d8ee7SSantosh Puranik             if (!rfMode.empty())
1022491d8ee7SSantosh Puranik             {
1023ac106bf6SEd Tanous                 asyncResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
1024491d8ee7SSantosh Puranik                     rfMode;
1025491d8ee7SSantosh Puranik             }
1026491d8ee7SSantosh Puranik         }
10271e1e598dSJonathan Doman         });
1028491d8ee7SSantosh Puranik }
1029491d8ee7SSantosh Puranik 
1030491d8ee7SSantosh Puranik /**
1031c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override source over DBUS
1032491d8ee7SSantosh Puranik  *
1033ac106bf6SEd Tanous  * @param[in] asyncResp         Shared pointer for generating response message.
1034491d8ee7SSantosh Puranik  *
1035491d8ee7SSantosh Puranik  * @return None.
1036491d8ee7SSantosh Puranik  */
1037c21865c4SKonstantin Aladyshev 
1038c21865c4SKonstantin Aladyshev inline void
1039ac106bf6SEd Tanous     getBootOverrideSource(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1040491d8ee7SSantosh Puranik {
10411e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
10421e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
10431e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
10441e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Source", "BootSource",
1045ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
10461e1e598dSJonathan Doman                     const std::string& bootSourceStr) {
1047491d8ee7SSantosh Puranik         if (ec)
1048491d8ee7SSantosh Puranik         {
1049491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
10505ef735c8SNan Zhou             if (ec.value() == boost::asio::error::host_unreachable)
10515ef735c8SNan Zhou             {
10525ef735c8SNan Zhou                 return;
10535ef735c8SNan Zhou             }
1054ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1055491d8ee7SSantosh Puranik             return;
1056491d8ee7SSantosh Puranik         }
1057491d8ee7SSantosh Puranik 
10581e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Boot source: " << bootSourceStr;
1059491d8ee7SSantosh Puranik 
10601e1e598dSJonathan Doman         auto rfSource = dbusToRfBootSource(bootSourceStr);
1061491d8ee7SSantosh Puranik         if (!rfSource.empty())
1062491d8ee7SSantosh Puranik         {
1063ac106bf6SEd Tanous             asyncResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
1064ac106bf6SEd Tanous                 rfSource;
1065491d8ee7SSantosh Puranik         }
1066cd9a4666SKonstantin Aladyshev 
1067cd9a4666SKonstantin Aladyshev         // Get BootMode as BootSourceOverrideTarget is constructed
1068cd9a4666SKonstantin Aladyshev         // from both BootSource and BootMode
1069ac106bf6SEd Tanous         getBootOverrideMode(asyncResp);
10701e1e598dSJonathan Doman         });
1071491d8ee7SSantosh Puranik }
1072491d8ee7SSantosh Puranik 
1073491d8ee7SSantosh Puranik /**
1074c21865c4SKonstantin Aladyshev  * @brief This functions abstracts all the logic behind getting a
1075c21865c4SKonstantin Aladyshev  * "BootSourceOverrideEnabled" property from an overall boot override enable
1076c21865c4SKonstantin Aladyshev  * state
1077491d8ee7SSantosh Puranik  *
1078ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
1079491d8ee7SSantosh Puranik  *
1080491d8ee7SSantosh Puranik  * @return None.
1081491d8ee7SSantosh Puranik  */
1082491d8ee7SSantosh Puranik 
1083ac106bf6SEd Tanous inline void processBootOverrideEnable(
1084ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1085c21865c4SKonstantin Aladyshev     const bool bootOverrideEnableSetting)
1086c21865c4SKonstantin Aladyshev {
1087c21865c4SKonstantin Aladyshev     if (!bootOverrideEnableSetting)
1088c21865c4SKonstantin Aladyshev     {
1089ac106bf6SEd Tanous         asyncResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
1090ac106bf6SEd Tanous             "Disabled";
1091c21865c4SKonstantin Aladyshev         return;
1092c21865c4SKonstantin Aladyshev     }
1093c21865c4SKonstantin Aladyshev 
1094c21865c4SKonstantin Aladyshev     // If boot source override is enabled, we need to check 'one_time'
1095c21865c4SKonstantin Aladyshev     // property to set a correct value for the "BootSourceOverrideEnabled"
10961e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
10971e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
10981e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot/one_time",
10991e1e598dSJonathan Doman         "xyz.openbmc_project.Object.Enable", "Enabled",
1100ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec, bool oneTimeSetting) {
1101491d8ee7SSantosh Puranik         if (ec)
1102491d8ee7SSantosh Puranik         {
1103491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1104ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1105491d8ee7SSantosh Puranik             return;
1106491d8ee7SSantosh Puranik         }
1107491d8ee7SSantosh Puranik 
1108c21865c4SKonstantin Aladyshev         if (oneTimeSetting)
1109c21865c4SKonstantin Aladyshev         {
1110ac106bf6SEd Tanous             asyncResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
1111ac106bf6SEd Tanous                 "Once";
1112c21865c4SKonstantin Aladyshev         }
1113c21865c4SKonstantin Aladyshev         else
1114c21865c4SKonstantin Aladyshev         {
1115ac106bf6SEd Tanous             asyncResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
1116c21865c4SKonstantin Aladyshev                 "Continuous";
1117c21865c4SKonstantin Aladyshev         }
11181e1e598dSJonathan Doman         });
1119491d8ee7SSantosh Puranik }
1120491d8ee7SSantosh Puranik 
1121491d8ee7SSantosh Puranik /**
1122c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override enable over DBUS
1123c21865c4SKonstantin Aladyshev  *
1124ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
1125c21865c4SKonstantin Aladyshev  *
1126c21865c4SKonstantin Aladyshev  * @return None.
1127c21865c4SKonstantin Aladyshev  */
1128c21865c4SKonstantin Aladyshev 
1129c21865c4SKonstantin Aladyshev inline void
1130ac106bf6SEd Tanous     getBootOverrideEnable(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1131c21865c4SKonstantin Aladyshev {
11321e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
11331e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
11341e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
11351e1e598dSJonathan Doman         "xyz.openbmc_project.Object.Enable", "Enabled",
1136ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
11371e1e598dSJonathan Doman                     const bool bootOverrideEnable) {
1138c21865c4SKonstantin Aladyshev         if (ec)
1139c21865c4SKonstantin Aladyshev         {
1140c21865c4SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
11415ef735c8SNan Zhou             if (ec.value() == boost::asio::error::host_unreachable)
11425ef735c8SNan Zhou             {
11435ef735c8SNan Zhou                 return;
11445ef735c8SNan Zhou             }
1145ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1146c21865c4SKonstantin Aladyshev             return;
1147c21865c4SKonstantin Aladyshev         }
1148c21865c4SKonstantin Aladyshev 
1149ac106bf6SEd Tanous         processBootOverrideEnable(asyncResp, bootOverrideEnable);
11501e1e598dSJonathan Doman         });
1151c21865c4SKonstantin Aladyshev }
1152c21865c4SKonstantin Aladyshev 
1153c21865c4SKonstantin Aladyshev /**
1154c21865c4SKonstantin Aladyshev  * @brief Retrieves boot source override properties
1155c21865c4SKonstantin Aladyshev  *
1156ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
1157c21865c4SKonstantin Aladyshev  *
1158c21865c4SKonstantin Aladyshev  * @return None.
1159c21865c4SKonstantin Aladyshev  */
1160ac106bf6SEd Tanous inline void
1161ac106bf6SEd Tanous     getBootProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1162c21865c4SKonstantin Aladyshev {
1163c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Get boot information.";
1164c21865c4SKonstantin Aladyshev 
1165ac106bf6SEd Tanous     getBootOverrideSource(asyncResp);
1166ac106bf6SEd Tanous     getBootOverrideType(asyncResp);
1167ac106bf6SEd Tanous     getBootOverrideEnable(asyncResp);
1168c21865c4SKonstantin Aladyshev }
1169c21865c4SKonstantin Aladyshev 
1170c21865c4SKonstantin Aladyshev /**
1171c0557e1aSGunnar Mills  * @brief Retrieves the Last Reset Time
1172c0557e1aSGunnar Mills  *
1173c0557e1aSGunnar Mills  * "Reset" is an overloaded term in Redfish, "Reset" includes power on
1174c0557e1aSGunnar Mills  * and power off. Even though this is the "system" Redfish object look at the
1175c0557e1aSGunnar Mills  * chassis D-Bus interface for the LastStateChangeTime since this has the
1176c0557e1aSGunnar Mills  * last power operation time.
1177c0557e1aSGunnar Mills  *
1178ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
1179c0557e1aSGunnar Mills  *
1180c0557e1aSGunnar Mills  * @return None.
1181c0557e1aSGunnar Mills  */
1182ac106bf6SEd Tanous inline void
1183ac106bf6SEd Tanous     getLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1184c0557e1aSGunnar Mills {
1185c0557e1aSGunnar Mills     BMCWEB_LOG_DEBUG << "Getting System Last Reset Time";
1186c0557e1aSGunnar Mills 
11871e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
11881e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Chassis",
11891e1e598dSJonathan Doman         "/xyz/openbmc_project/state/chassis0",
11901e1e598dSJonathan Doman         "xyz.openbmc_project.State.Chassis", "LastStateChangeTime",
1191ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
1192ac106bf6SEd Tanous                     uint64_t lastResetTime) {
1193c0557e1aSGunnar Mills         if (ec)
1194c0557e1aSGunnar Mills         {
1195c0557e1aSGunnar Mills             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
1196c0557e1aSGunnar Mills             return;
1197c0557e1aSGunnar Mills         }
1198c0557e1aSGunnar Mills 
1199c0557e1aSGunnar Mills         // LastStateChangeTime is epoch time, in milliseconds
1200c0557e1aSGunnar Mills         // https://github.com/openbmc/phosphor-dbus-interfaces/blob/33e8e1dd64da53a66e888d33dc82001305cd0bf9/xyz/openbmc_project/State/Chassis.interface.yaml#L19
12011e1e598dSJonathan Doman         uint64_t lastResetTimeStamp = lastResetTime / 1000;
1202c0557e1aSGunnar Mills 
1203c0557e1aSGunnar Mills         // Convert to ISO 8601 standard
1204ac106bf6SEd Tanous         asyncResp->res.jsonValue["LastResetTime"] =
12052b82937eSEd Tanous             redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
12061e1e598dSJonathan Doman         });
1207c0557e1aSGunnar Mills }
1208c0557e1aSGunnar Mills 
1209c0557e1aSGunnar Mills /**
1210797d5daeSCorey Hardesty  * @brief Retrieves the number of automatic boot Retry attempts allowed/left.
1211797d5daeSCorey Hardesty  *
1212797d5daeSCorey Hardesty  * The total number of automatic reboot retries allowed "RetryAttempts" and its
1213797d5daeSCorey Hardesty  * corresponding property "AttemptsLeft" that keeps track of the amount of
1214797d5daeSCorey Hardesty  * automatic retry attempts left are hosted in phosphor-state-manager through
1215797d5daeSCorey Hardesty  * dbus.
1216797d5daeSCorey Hardesty  *
1217ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
1218797d5daeSCorey Hardesty  *
1219797d5daeSCorey Hardesty  * @return None.
1220797d5daeSCorey Hardesty  */
1221ac106bf6SEd Tanous inline void getAutomaticRebootAttempts(
1222ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1223797d5daeSCorey Hardesty {
1224797d5daeSCorey Hardesty     BMCWEB_LOG_DEBUG << "Get Automatic Retry policy";
1225797d5daeSCorey Hardesty 
1226797d5daeSCorey Hardesty     sdbusplus::asio::getAllProperties(
1227797d5daeSCorey Hardesty         *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
1228797d5daeSCorey Hardesty         "/xyz/openbmc_project/state/host0",
1229797d5daeSCorey Hardesty         "xyz.openbmc_project.Control.Boot.RebootAttempts",
1230ac106bf6SEd Tanous         [asyncResp{asyncResp}](
1231ac106bf6SEd Tanous             const boost::system::error_code& ec,
1232797d5daeSCorey Hardesty             const dbus::utility::DBusPropertiesMap& propertiesList) {
1233797d5daeSCorey Hardesty         if (ec)
1234797d5daeSCorey Hardesty         {
1235797d5daeSCorey Hardesty             if (ec.value() != EBADR)
1236797d5daeSCorey Hardesty             {
1237797d5daeSCorey Hardesty                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1238ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
1239797d5daeSCorey Hardesty             }
1240797d5daeSCorey Hardesty             return;
1241797d5daeSCorey Hardesty         }
1242797d5daeSCorey Hardesty 
1243797d5daeSCorey Hardesty         const uint32_t* attemptsLeft = nullptr;
1244797d5daeSCorey Hardesty         const uint32_t* retryAttempts = nullptr;
1245797d5daeSCorey Hardesty 
1246797d5daeSCorey Hardesty         const bool success = sdbusplus::unpackPropertiesNoThrow(
1247797d5daeSCorey Hardesty             dbus_utils::UnpackErrorPrinter(), propertiesList, "AttemptsLeft",
1248797d5daeSCorey Hardesty             attemptsLeft, "RetryAttempts", retryAttempts);
1249797d5daeSCorey Hardesty 
1250797d5daeSCorey Hardesty         if (!success)
1251797d5daeSCorey Hardesty         {
1252ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1253797d5daeSCorey Hardesty             return;
1254797d5daeSCorey Hardesty         }
1255797d5daeSCorey Hardesty 
1256797d5daeSCorey Hardesty         if (attemptsLeft != nullptr)
1257797d5daeSCorey Hardesty         {
1258ac106bf6SEd Tanous             asyncResp->res
1259ac106bf6SEd Tanous                 .jsonValue["Boot"]["RemainingAutomaticRetryAttempts"] =
1260797d5daeSCorey Hardesty                 *attemptsLeft;
1261797d5daeSCorey Hardesty         }
1262797d5daeSCorey Hardesty 
1263797d5daeSCorey Hardesty         if (retryAttempts != nullptr)
1264797d5daeSCorey Hardesty         {
1265ac106bf6SEd Tanous             asyncResp->res.jsonValue["Boot"]["AutomaticRetryAttempts"] =
1266797d5daeSCorey Hardesty                 *retryAttempts;
1267797d5daeSCorey Hardesty         }
1268797d5daeSCorey Hardesty         });
1269797d5daeSCorey Hardesty }
1270797d5daeSCorey Hardesty 
1271797d5daeSCorey Hardesty /**
12726bd5a8d2SGunnar Mills  * @brief Retrieves Automatic Retry properties. Known on D-Bus as AutoReboot.
12736bd5a8d2SGunnar Mills  *
1274ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
12756bd5a8d2SGunnar Mills  *
12766bd5a8d2SGunnar Mills  * @return None.
12776bd5a8d2SGunnar Mills  */
1278797d5daeSCorey Hardesty inline void
1279ac106bf6SEd Tanous     getAutomaticRetryPolicy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12806bd5a8d2SGunnar Mills {
12816bd5a8d2SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Automatic Retry policy";
12826bd5a8d2SGunnar Mills 
12831e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
12841e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
12851e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/auto_reboot",
12861e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
1287ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
1288ac106bf6SEd Tanous                     bool autoRebootEnabled) {
12896bd5a8d2SGunnar Mills         if (ec)
12906bd5a8d2SGunnar Mills         {
1291797d5daeSCorey Hardesty             if (ec.value() != EBADR)
1292797d5daeSCorey Hardesty             {
1293797d5daeSCorey Hardesty                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1294ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
1295797d5daeSCorey Hardesty             }
12966bd5a8d2SGunnar Mills             return;
12976bd5a8d2SGunnar Mills         }
12986bd5a8d2SGunnar Mills 
12991e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Auto Reboot: " << autoRebootEnabled;
1300e05aec50SEd Tanous         if (autoRebootEnabled)
13016bd5a8d2SGunnar Mills         {
1302ac106bf6SEd Tanous             asyncResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
13036bd5a8d2SGunnar Mills                 "RetryAttempts";
13046bd5a8d2SGunnar Mills         }
13056bd5a8d2SGunnar Mills         else
13066bd5a8d2SGunnar Mills         {
1307ac106bf6SEd Tanous             asyncResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
1308ac106bf6SEd Tanous                 "Disabled";
13096bd5a8d2SGunnar Mills         }
1310ac106bf6SEd Tanous         getAutomaticRebootAttempts(asyncResp);
131169f35306SGunnar Mills 
131269f35306SGunnar Mills         // "AutomaticRetryConfig" can be 3 values, Disabled, RetryAlways,
131369f35306SGunnar Mills         // and RetryAttempts. OpenBMC only supports Disabled and
131469f35306SGunnar Mills         // RetryAttempts.
1315ac106bf6SEd Tanous         asyncResp->res
1316ac106bf6SEd Tanous             .jsonValue["Boot"]["AutomaticRetryConfig@Redfish.AllowableValues"] =
1317ac106bf6SEd Tanous             {"Disabled", "RetryAttempts"};
13181e1e598dSJonathan Doman         });
13196bd5a8d2SGunnar Mills }
13206bd5a8d2SGunnar Mills 
13216bd5a8d2SGunnar Mills /**
1322797d5daeSCorey Hardesty  * @brief Sets RetryAttempts
1323797d5daeSCorey Hardesty  *
1324ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
1325797d5daeSCorey Hardesty  * @param[in] retryAttempts  "AutomaticRetryAttempts" from request.
1326797d5daeSCorey Hardesty  *
1327797d5daeSCorey Hardesty  *@return None.
1328797d5daeSCorey Hardesty  */
1329797d5daeSCorey Hardesty 
1330ac106bf6SEd Tanous inline void setAutomaticRetryAttempts(
1331ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1332797d5daeSCorey Hardesty     const uint32_t retryAttempts)
1333797d5daeSCorey Hardesty {
1334797d5daeSCorey Hardesty     BMCWEB_LOG_DEBUG << "Set Automatic Retry Attempts.";
1335*9ae226faSGeorge Liu     sdbusplus::asio::setProperty(
1336*9ae226faSGeorge Liu         *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
1337*9ae226faSGeorge Liu         "/xyz/openbmc_project/state/host0",
1338*9ae226faSGeorge Liu         "xyz.openbmc_project.Control.Boot.RebootAttempts", "RetryAttempts",
1339*9ae226faSGeorge Liu         retryAttempts, [asyncResp](const boost::system::error_code& ec) {
1340797d5daeSCorey Hardesty             if (ec)
1341797d5daeSCorey Hardesty             {
1342797d5daeSCorey Hardesty                 BMCWEB_LOG_ERROR
1343*9ae226faSGeorge Liu                     << "DBUS response error: Set setAutomaticRetryAttempts"
1344*9ae226faSGeorge Liu                     << ec;
1345ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
1346797d5daeSCorey Hardesty                 return;
1347797d5daeSCorey Hardesty             }
1348*9ae226faSGeorge Liu         });
1349797d5daeSCorey Hardesty }
1350797d5daeSCorey Hardesty 
13518d69c668SEd Tanous inline computer_system::PowerRestorePolicyTypes
13528d69c668SEd Tanous     redfishPowerRestorePolicyFromDbus(std::string_view value)
13538d69c668SEd Tanous {
13548d69c668SEd Tanous     if (value ==
13558d69c668SEd Tanous         "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn")
13568d69c668SEd Tanous     {
13578d69c668SEd Tanous         return computer_system::PowerRestorePolicyTypes::AlwaysOn;
13588d69c668SEd Tanous     }
13598d69c668SEd Tanous     if (value ==
13608d69c668SEd Tanous         "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff")
13618d69c668SEd Tanous     {
13628d69c668SEd Tanous         return computer_system::PowerRestorePolicyTypes::AlwaysOff;
13638d69c668SEd Tanous     }
13648d69c668SEd Tanous     if (value ==
13658d69c668SEd Tanous         "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysRestore")
13668d69c668SEd Tanous     {
13678d69c668SEd Tanous         return computer_system::PowerRestorePolicyTypes::LastState;
13688d69c668SEd Tanous     }
13698d69c668SEd Tanous     if (value == "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.None")
13708d69c668SEd Tanous     {
13718d69c668SEd Tanous         return computer_system::PowerRestorePolicyTypes::AlwaysOff;
13728d69c668SEd Tanous     }
13738d69c668SEd Tanous     return computer_system::PowerRestorePolicyTypes::Invalid;
13748d69c668SEd Tanous }
1375797d5daeSCorey Hardesty /**
1376c6a620f2SGeorge Liu  * @brief Retrieves power restore policy over DBUS.
1377c6a620f2SGeorge Liu  *
1378ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
1379c6a620f2SGeorge Liu  *
1380c6a620f2SGeorge Liu  * @return None.
1381c6a620f2SGeorge Liu  */
13828d1b46d7Szhanghch05 inline void
1383ac106bf6SEd Tanous     getPowerRestorePolicy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1384c6a620f2SGeorge Liu {
1385c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Get power restore policy";
1386c6a620f2SGeorge Liu 
13871e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
13881e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
13891e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/power_restore_policy",
13901e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
1391ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
13925e7e2dc5SEd Tanous                     const std::string& policy) {
1393c6a620f2SGeorge Liu         if (ec)
1394c6a620f2SGeorge Liu         {
1395c6a620f2SGeorge Liu             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1396c6a620f2SGeorge Liu             return;
1397c6a620f2SGeorge Liu         }
13988d69c668SEd Tanous         computer_system::PowerRestorePolicyTypes restore =
13998d69c668SEd Tanous             redfishPowerRestorePolicyFromDbus(policy);
14008d69c668SEd Tanous         if (restore == computer_system::PowerRestorePolicyTypes::Invalid)
1401c6a620f2SGeorge Liu         {
1402ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1403c6a620f2SGeorge Liu             return;
1404c6a620f2SGeorge Liu         }
1405c6a620f2SGeorge Liu 
14068d69c668SEd Tanous         asyncResp->res.jsonValue["PowerRestorePolicy"] = restore;
14071e1e598dSJonathan Doman         });
1408c6a620f2SGeorge Liu }
1409c6a620f2SGeorge Liu 
1410c6a620f2SGeorge Liu /**
14111981771bSAli Ahmed  * @brief Get TrustedModuleRequiredToBoot property. Determines whether or not
14121981771bSAli Ahmed  * TPM is required for booting the host.
14131981771bSAli Ahmed  *
1414ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
14151981771bSAli Ahmed  *
14161981771bSAli Ahmed  * @return None.
14171981771bSAli Ahmed  */
14181981771bSAli Ahmed inline void getTrustedModuleRequiredToBoot(
1419ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14201981771bSAli Ahmed {
14211981771bSAli Ahmed     BMCWEB_LOG_DEBUG << "Get TPM required to boot.";
1422e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1423e99073f5SGeorge Liu         "xyz.openbmc_project.Control.TPM.Policy"};
1424e99073f5SGeorge Liu     dbus::utility::getSubTree(
1425e99073f5SGeorge Liu         "/", 0, interfaces,
1426ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
1427b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
14281981771bSAli Ahmed         if (ec)
14291981771bSAli Ahmed         {
1430002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on TPM.Policy GetSubTree"
1431002d39b4SEd Tanous                              << ec;
14321981771bSAli Ahmed             // This is an optional D-Bus object so just return if
14331981771bSAli Ahmed             // error occurs
14341981771bSAli Ahmed             return;
14351981771bSAli Ahmed         }
143626f6976fSEd Tanous         if (subtree.empty())
14371981771bSAli Ahmed         {
14381981771bSAli Ahmed             // As noted above, this is an optional interface so just return
14391981771bSAli Ahmed             // if there is no instance found
14401981771bSAli Ahmed             return;
14411981771bSAli Ahmed         }
14421981771bSAli Ahmed 
14431981771bSAli Ahmed         /* When there is more than one TPMEnable object... */
14441981771bSAli Ahmed         if (subtree.size() > 1)
14451981771bSAli Ahmed         {
14461981771bSAli Ahmed             BMCWEB_LOG_DEBUG
14471981771bSAli Ahmed                 << "DBUS response has more than 1 TPM Enable object:"
14481981771bSAli Ahmed                 << subtree.size();
14491981771bSAli Ahmed             // Throw an internal Error and return
1450ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
14511981771bSAli Ahmed             return;
14521981771bSAli Ahmed         }
14531981771bSAli Ahmed 
14541981771bSAli Ahmed         // Make sure the Dbus response map has a service and objectPath
14551981771bSAli Ahmed         // field
14561981771bSAli Ahmed         if (subtree[0].first.empty() || subtree[0].second.size() != 1)
14571981771bSAli Ahmed         {
14581981771bSAli Ahmed             BMCWEB_LOG_DEBUG << "TPM.Policy mapper error!";
1459ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
14601981771bSAli Ahmed             return;
14611981771bSAli Ahmed         }
14621981771bSAli Ahmed 
14631981771bSAli Ahmed         const std::string& path = subtree[0].first;
14641981771bSAli Ahmed         const std::string& serv = subtree[0].second.begin()->first;
14651981771bSAli Ahmed 
14661981771bSAli Ahmed         // Valid TPM Enable object found, now reading the current value
14671e1e598dSJonathan Doman         sdbusplus::asio::getProperty<bool>(
14681e1e598dSJonathan Doman             *crow::connections::systemBus, serv, path,
14691e1e598dSJonathan Doman             "xyz.openbmc_project.Control.TPM.Policy", "TPMEnable",
1470ac106bf6SEd Tanous             [asyncResp](const boost::system::error_code& ec2,
1471ac106bf6SEd Tanous                         bool tpmRequired) {
14728a592810SEd Tanous             if (ec2)
14731981771bSAli Ahmed             {
1474002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "D-BUS response error on TPM.Policy Get"
14758a592810SEd Tanous                                  << ec2;
1476ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
14771981771bSAli Ahmed                 return;
14781981771bSAli Ahmed             }
14791981771bSAli Ahmed 
14801e1e598dSJonathan Doman             if (tpmRequired)
14811981771bSAli Ahmed             {
1482ac106bf6SEd Tanous                 asyncResp->res
1483ac106bf6SEd Tanous                     .jsonValue["Boot"]["TrustedModuleRequiredToBoot"] =
14841981771bSAli Ahmed                     "Required";
14851981771bSAli Ahmed             }
14861981771bSAli Ahmed             else
14871981771bSAli Ahmed             {
1488ac106bf6SEd Tanous                 asyncResp->res
1489ac106bf6SEd Tanous                     .jsonValue["Boot"]["TrustedModuleRequiredToBoot"] =
14901981771bSAli Ahmed                     "Disabled";
14911981771bSAli Ahmed             }
14921e1e598dSJonathan Doman             });
1493e99073f5SGeorge Liu         });
14941981771bSAli Ahmed }
14951981771bSAli Ahmed 
14961981771bSAli Ahmed /**
14971c05dae3SAli Ahmed  * @brief Set TrustedModuleRequiredToBoot property. Determines whether or not
14981c05dae3SAli Ahmed  * TPM is required for booting the host.
14991c05dae3SAli Ahmed  *
1500ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
15011c05dae3SAli Ahmed  * @param[in] tpmRequired   Value to set TPM Required To Boot property to.
15021c05dae3SAli Ahmed  *
15031c05dae3SAli Ahmed  * @return None.
15041c05dae3SAli Ahmed  */
15051c05dae3SAli Ahmed inline void setTrustedModuleRequiredToBoot(
1506ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const bool tpmRequired)
15071c05dae3SAli Ahmed {
15081c05dae3SAli Ahmed     BMCWEB_LOG_DEBUG << "Set TrustedModuleRequiredToBoot.";
1509e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1510e99073f5SGeorge Liu         "xyz.openbmc_project.Control.TPM.Policy"};
1511e99073f5SGeorge Liu     dbus::utility::getSubTree(
1512e99073f5SGeorge Liu         "/", 0, interfaces,
1513ac106bf6SEd Tanous         [asyncResp,
1514e99073f5SGeorge Liu          tpmRequired](const boost::system::error_code& ec,
1515e99073f5SGeorge Liu                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
15161c05dae3SAli Ahmed         if (ec)
15171c05dae3SAli Ahmed         {
1518002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on TPM.Policy GetSubTree"
1519002d39b4SEd Tanous                              << ec;
1520ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
15211c05dae3SAli Ahmed             return;
15221c05dae3SAli Ahmed         }
152326f6976fSEd Tanous         if (subtree.empty())
15241c05dae3SAli Ahmed         {
1525ac106bf6SEd Tanous             messages::propertyValueNotInList(asyncResp->res, "ComputerSystem",
15261c05dae3SAli Ahmed                                              "TrustedModuleRequiredToBoot");
15271c05dae3SAli Ahmed             return;
15281c05dae3SAli Ahmed         }
15291c05dae3SAli Ahmed 
15301c05dae3SAli Ahmed         /* When there is more than one TPMEnable object... */
15311c05dae3SAli Ahmed         if (subtree.size() > 1)
15321c05dae3SAli Ahmed         {
15331c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG
15341c05dae3SAli Ahmed                 << "DBUS response has more than 1 TPM Enable object:"
15351c05dae3SAli Ahmed                 << subtree.size();
15361c05dae3SAli Ahmed             // Throw an internal Error and return
1537ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
15381c05dae3SAli Ahmed             return;
15391c05dae3SAli Ahmed         }
15401c05dae3SAli Ahmed 
15411c05dae3SAli Ahmed         // Make sure the Dbus response map has a service and objectPath
15421c05dae3SAli Ahmed         // field
15431c05dae3SAli Ahmed         if (subtree[0].first.empty() || subtree[0].second.size() != 1)
15441c05dae3SAli Ahmed         {
15451c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG << "TPM.Policy mapper error!";
1546ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
15471c05dae3SAli Ahmed             return;
15481c05dae3SAli Ahmed         }
15491c05dae3SAli Ahmed 
15501c05dae3SAli Ahmed         const std::string& path = subtree[0].first;
15511c05dae3SAli Ahmed         const std::string& serv = subtree[0].second.begin()->first;
15521c05dae3SAli Ahmed 
15531c05dae3SAli Ahmed         if (serv.empty())
15541c05dae3SAli Ahmed         {
15551c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG << "TPM.Policy service mapper error!";
1556ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
15571c05dae3SAli Ahmed             return;
15581c05dae3SAli Ahmed         }
15591c05dae3SAli Ahmed 
15601c05dae3SAli Ahmed         // Valid TPM Enable object found, now setting the value
1561*9ae226faSGeorge Liu         sdbusplus::asio::setProperty(
1562*9ae226faSGeorge Liu             *crow::connections::systemBus, serv, path,
1563*9ae226faSGeorge Liu             "xyz.openbmc_project.Control.TPM.Policy", "TPMEnable", tpmRequired,
1564ac106bf6SEd Tanous             [asyncResp](const boost::system::error_code& ec2) {
15658a592810SEd Tanous             if (ec2)
15661c05dae3SAli Ahmed             {
15670fda0f12SGeorge Liu                 BMCWEB_LOG_DEBUG
15680fda0f12SGeorge Liu                     << "DBUS response error: Set TrustedModuleRequiredToBoot"
15698a592810SEd Tanous                     << ec2;
1570ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
15711c05dae3SAli Ahmed                 return;
15721c05dae3SAli Ahmed             }
15731c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG << "Set TrustedModuleRequiredToBoot done.";
1574*9ae226faSGeorge Liu             });
1575e99073f5SGeorge Liu         });
15761c05dae3SAli Ahmed }
15771c05dae3SAli Ahmed 
15781c05dae3SAli Ahmed /**
1579491d8ee7SSantosh Puranik  * @brief Sets boot properties into DBUS object(s).
1580491d8ee7SSantosh Puranik  *
1581ac106bf6SEd Tanous  * @param[in] asyncResp       Shared pointer for generating response message.
1582cd9a4666SKonstantin Aladyshev  * @param[in] bootType        The boot type to set.
1583cd9a4666SKonstantin Aladyshev  * @return Integer error code.
1584cd9a4666SKonstantin Aladyshev  */
1585ac106bf6SEd Tanous inline void setBootType(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1586cd9a4666SKonstantin Aladyshev                         const std::optional<std::string>& bootType)
1587cd9a4666SKonstantin Aladyshev {
1588c21865c4SKonstantin Aladyshev     std::string bootTypeStr;
1589cd9a4666SKonstantin Aladyshev 
1590c21865c4SKonstantin Aladyshev     if (!bootType)
1591cd9a4666SKonstantin Aladyshev     {
1592c21865c4SKonstantin Aladyshev         return;
1593c21865c4SKonstantin Aladyshev     }
1594c21865c4SKonstantin Aladyshev 
1595cd9a4666SKonstantin Aladyshev     // Source target specified
1596cd9a4666SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Boot type: " << *bootType;
1597cd9a4666SKonstantin Aladyshev     // Figure out which DBUS interface and property to use
1598cd9a4666SKonstantin Aladyshev     if (*bootType == "Legacy")
1599cd9a4666SKonstantin Aladyshev     {
1600cd9a4666SKonstantin Aladyshev         bootTypeStr = "xyz.openbmc_project.Control.Boot.Type.Types.Legacy";
1601cd9a4666SKonstantin Aladyshev     }
1602cd9a4666SKonstantin Aladyshev     else if (*bootType == "UEFI")
1603cd9a4666SKonstantin Aladyshev     {
1604cd9a4666SKonstantin Aladyshev         bootTypeStr = "xyz.openbmc_project.Control.Boot.Type.Types.EFI";
1605cd9a4666SKonstantin Aladyshev     }
1606cd9a4666SKonstantin Aladyshev     else
1607cd9a4666SKonstantin Aladyshev     {
1608cd9a4666SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Invalid property value for "
1609cd9a4666SKonstantin Aladyshev                             "BootSourceOverrideMode: "
1610cd9a4666SKonstantin Aladyshev                          << *bootType;
1611ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *bootType,
1612cd9a4666SKonstantin Aladyshev                                          "BootSourceOverrideMode");
1613cd9a4666SKonstantin Aladyshev         return;
1614cd9a4666SKonstantin Aladyshev     }
1615cd9a4666SKonstantin Aladyshev 
1616cd9a4666SKonstantin Aladyshev     // Act on validated parameters
1617cd9a4666SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot type: " << bootTypeStr;
1618cd9a4666SKonstantin Aladyshev 
1619*9ae226faSGeorge Liu     sdbusplus::asio::setProperty(
1620*9ae226faSGeorge Liu         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
1621*9ae226faSGeorge Liu         "/xyz/openbmc_project/control/host0/boot",
1622*9ae226faSGeorge Liu         "xyz.openbmc_project.Control.Boot.Type", "BootType", bootTypeStr,
1623ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1624cd9a4666SKonstantin Aladyshev         if (ec)
1625cd9a4666SKonstantin Aladyshev         {
1626cd9a4666SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1627cd9a4666SKonstantin Aladyshev             if (ec.value() == boost::asio::error::host_unreachable)
1628cd9a4666SKonstantin Aladyshev             {
1629ac106bf6SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Set", "BootType");
1630cd9a4666SKonstantin Aladyshev                 return;
1631cd9a4666SKonstantin Aladyshev             }
1632ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1633cd9a4666SKonstantin Aladyshev             return;
1634cd9a4666SKonstantin Aladyshev         }
1635cd9a4666SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Boot type update done.";
1636*9ae226faSGeorge Liu         });
1637cd9a4666SKonstantin Aladyshev }
1638cd9a4666SKonstantin Aladyshev 
1639cd9a4666SKonstantin Aladyshev /**
1640cd9a4666SKonstantin Aladyshev  * @brief Sets boot properties into DBUS object(s).
1641cd9a4666SKonstantin Aladyshev  *
1642ac106bf6SEd Tanous  * @param[in] asyncResp           Shared pointer for generating response
1643ac106bf6SEd Tanous  * message.
1644c21865c4SKonstantin Aladyshev  * @param[in] bootType        The boot type to set.
1645c21865c4SKonstantin Aladyshev  * @return Integer error code.
1646c21865c4SKonstantin Aladyshev  */
1647ac106bf6SEd Tanous inline void setBootEnable(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1648c21865c4SKonstantin Aladyshev                           const std::optional<std::string>& bootEnable)
1649c21865c4SKonstantin Aladyshev {
1650c21865c4SKonstantin Aladyshev     if (!bootEnable)
1651c21865c4SKonstantin Aladyshev     {
1652c21865c4SKonstantin Aladyshev         return;
1653c21865c4SKonstantin Aladyshev     }
1654c21865c4SKonstantin Aladyshev     // Source target specified
1655c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Boot enable: " << *bootEnable;
1656c21865c4SKonstantin Aladyshev 
1657c21865c4SKonstantin Aladyshev     bool bootOverrideEnable = false;
1658c21865c4SKonstantin Aladyshev     bool bootOverridePersistent = false;
1659c21865c4SKonstantin Aladyshev     // Figure out which DBUS interface and property to use
1660c21865c4SKonstantin Aladyshev     if (*bootEnable == "Disabled")
1661c21865c4SKonstantin Aladyshev     {
1662c21865c4SKonstantin Aladyshev         bootOverrideEnable = false;
1663c21865c4SKonstantin Aladyshev     }
1664c21865c4SKonstantin Aladyshev     else if (*bootEnable == "Once")
1665c21865c4SKonstantin Aladyshev     {
1666c21865c4SKonstantin Aladyshev         bootOverrideEnable = true;
1667c21865c4SKonstantin Aladyshev         bootOverridePersistent = false;
1668c21865c4SKonstantin Aladyshev     }
1669c21865c4SKonstantin Aladyshev     else if (*bootEnable == "Continuous")
1670c21865c4SKonstantin Aladyshev     {
1671c21865c4SKonstantin Aladyshev         bootOverrideEnable = true;
1672c21865c4SKonstantin Aladyshev         bootOverridePersistent = true;
1673c21865c4SKonstantin Aladyshev     }
1674c21865c4SKonstantin Aladyshev     else
1675c21865c4SKonstantin Aladyshev     {
16760fda0f12SGeorge Liu         BMCWEB_LOG_DEBUG
16770fda0f12SGeorge Liu             << "Invalid property value for BootSourceOverrideEnabled: "
1678c21865c4SKonstantin Aladyshev             << *bootEnable;
1679ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *bootEnable,
1680c21865c4SKonstantin Aladyshev                                          "BootSourceOverrideEnabled");
1681c21865c4SKonstantin Aladyshev         return;
1682c21865c4SKonstantin Aladyshev     }
1683c21865c4SKonstantin Aladyshev 
1684c21865c4SKonstantin Aladyshev     // Act on validated parameters
1685c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot override enable: " << bootOverrideEnable;
1686c21865c4SKonstantin Aladyshev 
1687*9ae226faSGeorge Liu     sdbusplus::asio::setProperty(
1688*9ae226faSGeorge Liu         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
1689*9ae226faSGeorge Liu         "/xyz/openbmc_project/control/host0/boot",
1690*9ae226faSGeorge Liu         "xyz.openbmc_project.Object.Enable", "Enabled", bootOverrideEnable,
1691ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec2) {
16928a592810SEd Tanous         if (ec2)
1693c21865c4SKonstantin Aladyshev         {
16948a592810SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
1695ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1696c21865c4SKonstantin Aladyshev             return;
1697c21865c4SKonstantin Aladyshev         }
1698c21865c4SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Boot override enable update done.";
1699*9ae226faSGeorge Liu         });
1700c21865c4SKonstantin Aladyshev 
1701c21865c4SKonstantin Aladyshev     if (!bootOverrideEnable)
1702c21865c4SKonstantin Aladyshev     {
1703c21865c4SKonstantin Aladyshev         return;
1704c21865c4SKonstantin Aladyshev     }
1705c21865c4SKonstantin Aladyshev 
1706c21865c4SKonstantin Aladyshev     // In case boot override is enabled we need to set correct value for the
1707c21865c4SKonstantin Aladyshev     // 'one_time' enable DBus interface
1708c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot override persistent: "
1709c21865c4SKonstantin Aladyshev                      << bootOverridePersistent;
1710c21865c4SKonstantin Aladyshev 
1711*9ae226faSGeorge Liu     sdbusplus::asio::setProperty(
1712*9ae226faSGeorge Liu         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
1713*9ae226faSGeorge Liu         "/xyz/openbmc_project/control/host0/boot/one_time",
1714*9ae226faSGeorge Liu         "xyz.openbmc_project.Object.Enable", "Enabled", !bootOverridePersistent,
1715ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1716c21865c4SKonstantin Aladyshev         if (ec)
1717c21865c4SKonstantin Aladyshev         {
1718c21865c4SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1719ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1720c21865c4SKonstantin Aladyshev             return;
1721c21865c4SKonstantin Aladyshev         }
1722c21865c4SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Boot one_time update done.";
1723*9ae226faSGeorge Liu         });
1724c21865c4SKonstantin Aladyshev }
1725c21865c4SKonstantin Aladyshev 
1726c21865c4SKonstantin Aladyshev /**
1727c21865c4SKonstantin Aladyshev  * @brief Sets boot properties into DBUS object(s).
1728c21865c4SKonstantin Aladyshev  *
1729ac106bf6SEd Tanous  * @param[in] asyncResp       Shared pointer for generating response message.
1730491d8ee7SSantosh Puranik  * @param[in] bootSource      The boot source to set.
1731491d8ee7SSantosh Puranik  *
1732265c1602SJohnathan Mantey  * @return Integer error code.
1733491d8ee7SSantosh Puranik  */
1734ac106bf6SEd Tanous inline void
1735ac106bf6SEd Tanous     setBootModeOrSource(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1736cd9a4666SKonstantin Aladyshev                         const std::optional<std::string>& bootSource)
1737491d8ee7SSantosh Puranik {
1738c21865c4SKonstantin Aladyshev     std::string bootSourceStr;
1739c21865c4SKonstantin Aladyshev     std::string bootModeStr;
1740944ffaf9SJohnathan Mantey 
1741c21865c4SKonstantin Aladyshev     if (!bootSource)
1742491d8ee7SSantosh Puranik     {
1743c21865c4SKonstantin Aladyshev         return;
1744c21865c4SKonstantin Aladyshev     }
1745c21865c4SKonstantin Aladyshev 
1746491d8ee7SSantosh Puranik     // Source target specified
1747491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Boot source: " << *bootSource;
1748491d8ee7SSantosh Puranik     // Figure out which DBUS interface and property to use
1749ac106bf6SEd Tanous     if (assignBootParameters(asyncResp, *bootSource, bootSourceStr,
1750ac106bf6SEd Tanous                              bootModeStr) != 0)
1751491d8ee7SSantosh Puranik     {
1752944ffaf9SJohnathan Mantey         BMCWEB_LOG_DEBUG
1753944ffaf9SJohnathan Mantey             << "Invalid property value for BootSourceOverrideTarget: "
1754491d8ee7SSantosh Puranik             << *bootSource;
1755ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *bootSource,
1756491d8ee7SSantosh Puranik                                          "BootSourceTargetOverride");
1757491d8ee7SSantosh Puranik         return;
1758491d8ee7SSantosh Puranik     }
1759491d8ee7SSantosh Puranik 
1760944ffaf9SJohnathan Mantey     // Act on validated parameters
1761944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
1762944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
1763944ffaf9SJohnathan Mantey 
1764*9ae226faSGeorge Liu     sdbusplus::asio::setProperty(
1765*9ae226faSGeorge Liu         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
1766*9ae226faSGeorge Liu         "/xyz/openbmc_project/control/host0/boot",
1767*9ae226faSGeorge Liu         "xyz.openbmc_project.Control.Boot.Source", "BootSource", bootSourceStr,
1768ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1769491d8ee7SSantosh Puranik         if (ec)
1770491d8ee7SSantosh Puranik         {
1771491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1772ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1773491d8ee7SSantosh Puranik             return;
1774491d8ee7SSantosh Puranik         }
1775491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot source update done.";
1776*9ae226faSGeorge Liu         });
1777944ffaf9SJohnathan Mantey 
1778*9ae226faSGeorge Liu     sdbusplus::asio::setProperty(
1779*9ae226faSGeorge Liu         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
1780*9ae226faSGeorge Liu         "/xyz/openbmc_project/control/host0/boot",
1781*9ae226faSGeorge Liu         "xyz.openbmc_project.Control.Boot.Mode", "BootMode", bootModeStr,
1782ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1783491d8ee7SSantosh Puranik         if (ec)
1784491d8ee7SSantosh Puranik         {
1785491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1786ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1787491d8ee7SSantosh Puranik             return;
1788491d8ee7SSantosh Puranik         }
1789491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot mode update done.";
1790*9ae226faSGeorge Liu         });
1791cd9a4666SKonstantin Aladyshev }
1792944ffaf9SJohnathan Mantey 
1793cd9a4666SKonstantin Aladyshev /**
1794c21865c4SKonstantin Aladyshev  * @brief Sets Boot source override properties.
1795491d8ee7SSantosh Puranik  *
1796ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
1797491d8ee7SSantosh Puranik  * @param[in] bootSource The boot source from incoming RF request.
1798cd9a4666SKonstantin Aladyshev  * @param[in] bootType   The boot type from incoming RF request.
1799491d8ee7SSantosh Puranik  * @param[in] bootEnable The boot override enable from incoming RF request.
1800491d8ee7SSantosh Puranik  *
1801265c1602SJohnathan Mantey  * @return Integer error code.
1802491d8ee7SSantosh Puranik  */
1803c21865c4SKonstantin Aladyshev 
1804ac106bf6SEd Tanous inline void
1805ac106bf6SEd Tanous     setBootProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1806c21865c4SKonstantin Aladyshev                       const std::optional<std::string>& bootSource,
1807c21865c4SKonstantin Aladyshev                       const std::optional<std::string>& bootType,
1808c21865c4SKonstantin Aladyshev                       const std::optional<std::string>& bootEnable)
1809491d8ee7SSantosh Puranik {
1810491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set boot information.";
1811491d8ee7SSantosh Puranik 
1812ac106bf6SEd Tanous     setBootModeOrSource(asyncResp, bootSource);
1813ac106bf6SEd Tanous     setBootType(asyncResp, bootType);
1814ac106bf6SEd Tanous     setBootEnable(asyncResp, bootEnable);
1815491d8ee7SSantosh Puranik }
1816491d8ee7SSantosh Puranik 
1817c6a620f2SGeorge Liu /**
181898e386ecSGunnar Mills  * @brief Sets AssetTag
181998e386ecSGunnar Mills  *
1820ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for generating response message.
182198e386ecSGunnar Mills  * @param[in] assetTag  "AssetTag" from request.
182298e386ecSGunnar Mills  *
182398e386ecSGunnar Mills  * @return None.
182498e386ecSGunnar Mills  */
1825ac106bf6SEd Tanous inline void setAssetTag(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
182698e386ecSGunnar Mills                         const std::string& assetTag)
182798e386ecSGunnar Mills {
1828e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1829e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.System"};
1830e99073f5SGeorge Liu     dbus::utility::getSubTree(
1831e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
1832ac106bf6SEd Tanous         [asyncResp,
1833e99073f5SGeorge Liu          assetTag](const boost::system::error_code& ec,
1834b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
183598e386ecSGunnar Mills         if (ec)
183698e386ecSGunnar Mills         {
183798e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " << ec;
1838ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
183998e386ecSGunnar Mills             return;
184098e386ecSGunnar Mills         }
184126f6976fSEd Tanous         if (subtree.empty())
184298e386ecSGunnar Mills         {
184398e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find system D-Bus object!";
1844ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
184598e386ecSGunnar Mills             return;
184698e386ecSGunnar Mills         }
184798e386ecSGunnar Mills         // Assume only 1 system D-Bus object
184898e386ecSGunnar Mills         // Throw an error if there is more than 1
184998e386ecSGunnar Mills         if (subtree.size() > 1)
185098e386ecSGunnar Mills         {
185198e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Found more than 1 system D-Bus object!";
1852ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
185398e386ecSGunnar Mills             return;
185498e386ecSGunnar Mills         }
185598e386ecSGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.size() != 1)
185698e386ecSGunnar Mills         {
185798e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Asset Tag Set mapper error!";
1858ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
185998e386ecSGunnar Mills             return;
186098e386ecSGunnar Mills         }
186198e386ecSGunnar Mills 
186298e386ecSGunnar Mills         const std::string& path = subtree[0].first;
186398e386ecSGunnar Mills         const std::string& service = subtree[0].second.begin()->first;
186498e386ecSGunnar Mills 
186598e386ecSGunnar Mills         if (service.empty())
186698e386ecSGunnar Mills         {
186798e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Asset Tag Set service mapper error!";
1868ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
186998e386ecSGunnar Mills             return;
187098e386ecSGunnar Mills         }
187198e386ecSGunnar Mills 
1872*9ae226faSGeorge Liu         sdbusplus::asio::setProperty(
1873*9ae226faSGeorge Liu             *crow::connections::systemBus, service, path,
1874*9ae226faSGeorge Liu             "xyz.openbmc_project.Inventory.Decorator.AssetTag", "AssetTag",
1875*9ae226faSGeorge Liu             assetTag, [asyncResp](const boost::system::error_code& ec2) {
187698e386ecSGunnar Mills                 if (ec2)
187798e386ecSGunnar Mills                 {
1878002d39b4SEd Tanous                     BMCWEB_LOG_DEBUG << "D-Bus response error on AssetTag Set "
1879002d39b4SEd Tanous                                      << ec2;
1880ac106bf6SEd Tanous                     messages::internalError(asyncResp->res);
188198e386ecSGunnar Mills                     return;
188298e386ecSGunnar Mills                 }
1883*9ae226faSGeorge Liu             });
1884e99073f5SGeorge Liu         });
188598e386ecSGunnar Mills }
188698e386ecSGunnar Mills 
188798e386ecSGunnar Mills /**
188869f35306SGunnar Mills  * @brief Sets automaticRetry (Auto Reboot)
188969f35306SGunnar Mills  *
1890ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
189169f35306SGunnar Mills  * @param[in] automaticRetryConfig  "AutomaticRetryConfig" from request.
189269f35306SGunnar Mills  *
189369f35306SGunnar Mills  * @return None.
189469f35306SGunnar Mills  */
1895ac106bf6SEd Tanous inline void
1896ac106bf6SEd Tanous     setAutomaticRetry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1897f23b7296SEd Tanous                       const std::string& automaticRetryConfig)
189869f35306SGunnar Mills {
189969f35306SGunnar Mills     BMCWEB_LOG_DEBUG << "Set Automatic Retry.";
190069f35306SGunnar Mills 
190169f35306SGunnar Mills     // OpenBMC only supports "Disabled" and "RetryAttempts".
1902543f4400SEd Tanous     bool autoRebootEnabled = false;
190369f35306SGunnar Mills 
190469f35306SGunnar Mills     if (automaticRetryConfig == "Disabled")
190569f35306SGunnar Mills     {
190669f35306SGunnar Mills         autoRebootEnabled = false;
190769f35306SGunnar Mills     }
190869f35306SGunnar Mills     else if (automaticRetryConfig == "RetryAttempts")
190969f35306SGunnar Mills     {
191069f35306SGunnar Mills         autoRebootEnabled = true;
191169f35306SGunnar Mills     }
191269f35306SGunnar Mills     else
191369f35306SGunnar Mills     {
19140fda0f12SGeorge Liu         BMCWEB_LOG_DEBUG << "Invalid property value for AutomaticRetryConfig: "
191569f35306SGunnar Mills                          << automaticRetryConfig;
1916ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, automaticRetryConfig,
191769f35306SGunnar Mills                                          "AutomaticRetryConfig");
191869f35306SGunnar Mills         return;
191969f35306SGunnar Mills     }
192069f35306SGunnar Mills 
1921*9ae226faSGeorge Liu     sdbusplus::asio::setProperty(
1922*9ae226faSGeorge Liu         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
1923*9ae226faSGeorge Liu         "/xyz/openbmc_project/control/host0/auto_reboot",
1924*9ae226faSGeorge Liu         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
1925*9ae226faSGeorge Liu         autoRebootEnabled, [asyncResp](const boost::system::error_code& ec) {
192669f35306SGunnar Mills             if (ec)
192769f35306SGunnar Mills             {
1928ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
192969f35306SGunnar Mills                 return;
193069f35306SGunnar Mills             }
1931*9ae226faSGeorge Liu         });
193269f35306SGunnar Mills }
193369f35306SGunnar Mills 
19348d69c668SEd Tanous inline std::string dbusPowerRestorePolicyFromRedfish(std::string_view policy)
19358d69c668SEd Tanous {
19368d69c668SEd Tanous     if (policy == "AlwaysOn")
19378d69c668SEd Tanous     {
19388d69c668SEd Tanous         return "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn";
19398d69c668SEd Tanous     }
19408d69c668SEd Tanous     if (policy == "AlwaysOff")
19418d69c668SEd Tanous     {
19428d69c668SEd Tanous         return "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff";
19438d69c668SEd Tanous     }
19448d69c668SEd Tanous     if (policy == "LastState")
19458d69c668SEd Tanous     {
19468d69c668SEd Tanous         return "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore";
19478d69c668SEd Tanous     }
19488d69c668SEd Tanous     return "";
19498d69c668SEd Tanous }
19508d69c668SEd Tanous 
195169f35306SGunnar Mills /**
1952c6a620f2SGeorge Liu  * @brief Sets power restore policy properties.
1953c6a620f2SGeorge Liu  *
1954ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
1955c6a620f2SGeorge Liu  * @param[in] policy  power restore policy properties from request.
1956c6a620f2SGeorge Liu  *
1957c6a620f2SGeorge Liu  * @return None.
1958c6a620f2SGeorge Liu  */
19598d1b46d7Szhanghch05 inline void
1960ac106bf6SEd Tanous     setPowerRestorePolicy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19618d69c668SEd Tanous                           std::string_view policy)
1962c6a620f2SGeorge Liu {
1963c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Set power restore policy.";
1964c6a620f2SGeorge Liu 
19658d69c668SEd Tanous     std::string powerRestorePolicy = dbusPowerRestorePolicyFromRedfish(policy);
1966c6a620f2SGeorge Liu 
19678d69c668SEd Tanous     if (powerRestorePolicy.empty())
1968c6a620f2SGeorge Liu     {
1969ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, policy,
19704e69c904SGunnar Mills                                          "PowerRestorePolicy");
1971c6a620f2SGeorge Liu         return;
1972c6a620f2SGeorge Liu     }
1973c6a620f2SGeorge Liu 
1974*9ae226faSGeorge Liu     sdbusplus::asio::setProperty(
1975*9ae226faSGeorge Liu         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
1976*9ae226faSGeorge Liu         "/xyz/openbmc_project/control/host0/power_restore_policy",
1977*9ae226faSGeorge Liu         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
1978*9ae226faSGeorge Liu         powerRestorePolicy, [asyncResp](const boost::system::error_code& ec) {
1979c6a620f2SGeorge Liu             if (ec)
1980c6a620f2SGeorge Liu             {
1981ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
1982c6a620f2SGeorge Liu                 return;
1983c6a620f2SGeorge Liu             }
1984*9ae226faSGeorge Liu         });
1985c6a620f2SGeorge Liu }
1986c6a620f2SGeorge Liu 
1987a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
1988a6349918SAppaRao Puli /**
1989a6349918SAppaRao Puli  * @brief Retrieves provisioning status
1990a6349918SAppaRao Puli  *
1991ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
1992a6349918SAppaRao Puli  *
1993a6349918SAppaRao Puli  * @return None.
1994a6349918SAppaRao Puli  */
1995ac106bf6SEd Tanous inline void getProvisioningStatus(std::shared_ptr<bmcweb::AsyncResp> asyncResp)
1996a6349918SAppaRao Puli {
1997a6349918SAppaRao Puli     BMCWEB_LOG_DEBUG << "Get OEM information.";
1998bc1d29deSKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1999bc1d29deSKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.PFR.Manager",
2000bc1d29deSKrzysztof Grobelny         "/xyz/openbmc_project/pfr", "xyz.openbmc_project.PFR.Attributes",
2001ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
2002b9d36b47SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
2003b99fb1a9SAppaRao Puli         nlohmann::json& oemPFR =
2004ac106bf6SEd Tanous             asyncResp->res.jsonValue["Oem"]["OpenBmc"]["FirmwareProvisioning"];
2005ac106bf6SEd Tanous         asyncResp->res.jsonValue["Oem"]["OpenBmc"]["@odata.type"] =
200650626f4fSJames Feist             "#OemComputerSystem.OpenBmc";
200750626f4fSJames Feist         oemPFR["@odata.type"] = "#OemComputerSystem.FirmwareProvisioning";
200850626f4fSJames Feist 
2009a6349918SAppaRao Puli         if (ec)
2010a6349918SAppaRao Puli         {
2011a6349918SAppaRao Puli             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
2012b99fb1a9SAppaRao Puli             // not an error, don't have to have the interface
2013b99fb1a9SAppaRao Puli             oemPFR["ProvisioningStatus"] = "NotProvisioned";
2014a6349918SAppaRao Puli             return;
2015a6349918SAppaRao Puli         }
2016a6349918SAppaRao Puli 
2017a6349918SAppaRao Puli         const bool* provState = nullptr;
2018a6349918SAppaRao Puli         const bool* lockState = nullptr;
2019bc1d29deSKrzysztof Grobelny 
2020bc1d29deSKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
20210d4befa8SJiaqing Zhao             dbus_utils::UnpackErrorPrinter(), propertiesList, "UfmProvisioned",
20220d4befa8SJiaqing Zhao             provState, "UfmLocked", lockState);
2023bc1d29deSKrzysztof Grobelny 
2024bc1d29deSKrzysztof Grobelny         if (!success)
2025a6349918SAppaRao Puli         {
2026ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
2027bc1d29deSKrzysztof Grobelny             return;
2028a6349918SAppaRao Puli         }
2029a6349918SAppaRao Puli 
2030a6349918SAppaRao Puli         if ((provState == nullptr) || (lockState == nullptr))
2031a6349918SAppaRao Puli         {
2032a6349918SAppaRao Puli             BMCWEB_LOG_DEBUG << "Unable to get PFR attributes.";
2033ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
2034a6349918SAppaRao Puli             return;
2035a6349918SAppaRao Puli         }
2036a6349918SAppaRao Puli 
2037a6349918SAppaRao Puli         if (*provState == true)
2038a6349918SAppaRao Puli         {
2039a6349918SAppaRao Puli             if (*lockState == true)
2040a6349918SAppaRao Puli             {
2041a6349918SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "ProvisionedAndLocked";
2042a6349918SAppaRao Puli             }
2043a6349918SAppaRao Puli             else
2044a6349918SAppaRao Puli             {
2045a6349918SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "ProvisionedButNotLocked";
2046a6349918SAppaRao Puli             }
2047a6349918SAppaRao Puli         }
2048a6349918SAppaRao Puli         else
2049a6349918SAppaRao Puli         {
2050a6349918SAppaRao Puli             oemPFR["ProvisioningStatus"] = "NotProvisioned";
2051a6349918SAppaRao Puli         }
2052bc1d29deSKrzysztof Grobelny         });
2053a6349918SAppaRao Puli }
2054a6349918SAppaRao Puli #endif
2055a6349918SAppaRao Puli 
2056491d8ee7SSantosh Puranik /**
20573a2d0424SChris Cain  * @brief Translate the PowerMode to a response message.
20583a2d0424SChris Cain  *
2059ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
20603a2d0424SChris Cain  * @param[in] modeValue  PowerMode value to be translated
20613a2d0424SChris Cain  *
20623a2d0424SChris Cain  * @return None.
20633a2d0424SChris Cain  */
2064ac106bf6SEd Tanous inline void
2065ac106bf6SEd Tanous     translatePowerMode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
20663a2d0424SChris Cain                        const std::string& modeValue)
20673a2d0424SChris Cain {
20680fda0f12SGeorge Liu     if (modeValue == "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static")
20693a2d0424SChris Cain     {
2070ac106bf6SEd Tanous         asyncResp->res.jsonValue["PowerMode"] = "Static";
20713a2d0424SChris Cain     }
20720fda0f12SGeorge Liu     else if (
20730fda0f12SGeorge Liu         modeValue ==
20740fda0f12SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance")
20753a2d0424SChris Cain     {
2076ac106bf6SEd Tanous         asyncResp->res.jsonValue["PowerMode"] = "MaximumPerformance";
20773a2d0424SChris Cain     }
20780fda0f12SGeorge Liu     else if (modeValue ==
20790fda0f12SGeorge Liu              "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving")
20803a2d0424SChris Cain     {
2081ac106bf6SEd Tanous         asyncResp->res.jsonValue["PowerMode"] = "PowerSaving";
20823a2d0424SChris Cain     }
20830fda0f12SGeorge Liu     else if (modeValue ==
20840fda0f12SGeorge Liu              "xyz.openbmc_project.Control.Power.Mode.PowerMode.OEM")
20853a2d0424SChris Cain     {
2086ac106bf6SEd Tanous         asyncResp->res.jsonValue["PowerMode"] = "OEM";
20873a2d0424SChris Cain     }
20883a2d0424SChris Cain     else
20893a2d0424SChris Cain     {
20903a2d0424SChris Cain         // Any other values would be invalid
20913a2d0424SChris Cain         BMCWEB_LOG_DEBUG << "PowerMode value was not valid: " << modeValue;
2092ac106bf6SEd Tanous         messages::internalError(asyncResp->res);
20933a2d0424SChris Cain     }
20943a2d0424SChris Cain }
20953a2d0424SChris Cain 
20963a2d0424SChris Cain /**
20973a2d0424SChris Cain  * @brief Retrieves system power mode
20983a2d0424SChris Cain  *
2099ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
21003a2d0424SChris Cain  *
21013a2d0424SChris Cain  * @return None.
21023a2d0424SChris Cain  */
2103ac106bf6SEd Tanous inline void getPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
21043a2d0424SChris Cain {
21053a2d0424SChris Cain     BMCWEB_LOG_DEBUG << "Get power mode.";
21063a2d0424SChris Cain 
21073a2d0424SChris Cain     // Get Power Mode object path:
2108e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2109e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode"};
2110e99073f5SGeorge Liu     dbus::utility::getSubTree(
2111e99073f5SGeorge Liu         "/", 0, interfaces,
2112ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
2113b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
21143a2d0424SChris Cain         if (ec)
21153a2d0424SChris Cain         {
2116002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on Power.Mode GetSubTree "
2117002d39b4SEd Tanous                              << ec;
21183a2d0424SChris Cain             // This is an optional D-Bus object so just return if
21193a2d0424SChris Cain             // error occurs
21203a2d0424SChris Cain             return;
21213a2d0424SChris Cain         }
21223a2d0424SChris Cain         if (subtree.empty())
21233a2d0424SChris Cain         {
21243a2d0424SChris Cain             // As noted above, this is an optional interface so just return
21253a2d0424SChris Cain             // if there is no instance found
21263a2d0424SChris Cain             return;
21273a2d0424SChris Cain         }
21283a2d0424SChris Cain         if (subtree.size() > 1)
21293a2d0424SChris Cain         {
21303a2d0424SChris Cain             // More then one PowerMode object is not supported and is an
21313a2d0424SChris Cain             // error
21323a2d0424SChris Cain             BMCWEB_LOG_DEBUG
21333a2d0424SChris Cain                 << "Found more than 1 system D-Bus Power.Mode objects: "
21343a2d0424SChris Cain                 << subtree.size();
2135ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
21363a2d0424SChris Cain             return;
21373a2d0424SChris Cain         }
21383a2d0424SChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
21393a2d0424SChris Cain         {
21403a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode mapper error!";
2141ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
21423a2d0424SChris Cain             return;
21433a2d0424SChris Cain         }
21443a2d0424SChris Cain         const std::string& path = subtree[0].first;
21453a2d0424SChris Cain         const std::string& service = subtree[0].second.begin()->first;
21463a2d0424SChris Cain         if (service.empty())
21473a2d0424SChris Cain         {
21483a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode service mapper error!";
2149ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
21503a2d0424SChris Cain             return;
21513a2d0424SChris Cain         }
21523a2d0424SChris Cain         // Valid Power Mode object found, now read the current value
21531e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
21541e1e598dSJonathan Doman             *crow::connections::systemBus, service, path,
21551e1e598dSJonathan Doman             "xyz.openbmc_project.Control.Power.Mode", "PowerMode",
2156ac106bf6SEd Tanous             [asyncResp](const boost::system::error_code& ec2,
21571e1e598dSJonathan Doman                         const std::string& pmode) {
21588a592810SEd Tanous             if (ec2)
21593a2d0424SChris Cain             {
2160002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error on PowerMode Get: "
21618a592810SEd Tanous                                  << ec2;
2162ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
21633a2d0424SChris Cain                 return;
21643a2d0424SChris Cain             }
21653a2d0424SChris Cain 
2166ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerMode@Redfish.AllowableValues"] = {
2167002d39b4SEd Tanous                 "Static", "MaximumPerformance", "PowerSaving"};
21683a2d0424SChris Cain 
21691e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Current power mode: " << pmode;
2170ac106bf6SEd Tanous             translatePowerMode(asyncResp, pmode);
21711e1e598dSJonathan Doman             });
2172e99073f5SGeorge Liu         });
21733a2d0424SChris Cain }
21743a2d0424SChris Cain 
21753a2d0424SChris Cain /**
21763a2d0424SChris Cain  * @brief Validate the specified mode is valid and return the PowerMode
21773a2d0424SChris Cain  * name associated with that string
21783a2d0424SChris Cain  *
2179ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
21803a2d0424SChris Cain  * @param[in] modeString  String representing the desired PowerMode
21813a2d0424SChris Cain  *
21823a2d0424SChris Cain  * @return PowerMode value or empty string if mode is not valid
21833a2d0424SChris Cain  */
21843a2d0424SChris Cain inline std::string
2185ac106bf6SEd Tanous     validatePowerMode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21863a2d0424SChris Cain                       const std::string& modeString)
21873a2d0424SChris Cain {
21883a2d0424SChris Cain     std::string mode;
21893a2d0424SChris Cain 
21903a2d0424SChris Cain     if (modeString == "Static")
21913a2d0424SChris Cain     {
21923a2d0424SChris Cain         mode = "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static";
21933a2d0424SChris Cain     }
21943a2d0424SChris Cain     else if (modeString == "MaximumPerformance")
21953a2d0424SChris Cain     {
21960fda0f12SGeorge Liu         mode =
21970fda0f12SGeorge Liu             "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance";
21983a2d0424SChris Cain     }
21993a2d0424SChris Cain     else if (modeString == "PowerSaving")
22003a2d0424SChris Cain     {
22013a2d0424SChris Cain         mode = "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving";
22023a2d0424SChris Cain     }
22033a2d0424SChris Cain     else
22043a2d0424SChris Cain     {
2205ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, modeString,
2206ac106bf6SEd Tanous                                          "PowerMode");
22073a2d0424SChris Cain     }
22083a2d0424SChris Cain     return mode;
22093a2d0424SChris Cain }
22103a2d0424SChris Cain 
22113a2d0424SChris Cain /**
22123a2d0424SChris Cain  * @brief Sets system power mode.
22133a2d0424SChris Cain  *
2214ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for generating response message.
22153a2d0424SChris Cain  * @param[in] pmode   System power mode from request.
22163a2d0424SChris Cain  *
22173a2d0424SChris Cain  * @return None.
22183a2d0424SChris Cain  */
2219ac106bf6SEd Tanous inline void setPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22203a2d0424SChris Cain                          const std::string& pmode)
22213a2d0424SChris Cain {
22223a2d0424SChris Cain     BMCWEB_LOG_DEBUG << "Set power mode.";
22233a2d0424SChris Cain 
2224ac106bf6SEd Tanous     std::string powerMode = validatePowerMode(asyncResp, pmode);
22253a2d0424SChris Cain     if (powerMode.empty())
22263a2d0424SChris Cain     {
22273a2d0424SChris Cain         return;
22283a2d0424SChris Cain     }
22293a2d0424SChris Cain 
22303a2d0424SChris Cain     // Get Power Mode object path:
2231e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2232e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode"};
2233e99073f5SGeorge Liu     dbus::utility::getSubTree(
2234e99073f5SGeorge Liu         "/", 0, interfaces,
2235ac106bf6SEd Tanous         [asyncResp,
2236e99073f5SGeorge Liu          powerMode](const boost::system::error_code& ec,
2237b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
22383a2d0424SChris Cain         if (ec)
22393a2d0424SChris Cain         {
2240002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on Power.Mode GetSubTree "
2241002d39b4SEd Tanous                              << ec;
22423a2d0424SChris Cain             // This is an optional D-Bus object, but user attempted to patch
2243ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
22443a2d0424SChris Cain             return;
22453a2d0424SChris Cain         }
22463a2d0424SChris Cain         if (subtree.empty())
22473a2d0424SChris Cain         {
22483a2d0424SChris Cain             // This is an optional D-Bus object, but user attempted to patch
2249ac106bf6SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
22503a2d0424SChris Cain                                        "PowerMode");
22513a2d0424SChris Cain             return;
22523a2d0424SChris Cain         }
22533a2d0424SChris Cain         if (subtree.size() > 1)
22543a2d0424SChris Cain         {
22553a2d0424SChris Cain             // More then one PowerMode object is not supported and is an
22563a2d0424SChris Cain             // error
22573a2d0424SChris Cain             BMCWEB_LOG_DEBUG
22583a2d0424SChris Cain                 << "Found more than 1 system D-Bus Power.Mode objects: "
22593a2d0424SChris Cain                 << subtree.size();
2260ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
22613a2d0424SChris Cain             return;
22623a2d0424SChris Cain         }
22633a2d0424SChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
22643a2d0424SChris Cain         {
22653a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode mapper error!";
2266ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
22673a2d0424SChris Cain             return;
22683a2d0424SChris Cain         }
22693a2d0424SChris Cain         const std::string& path = subtree[0].first;
22703a2d0424SChris Cain         const std::string& service = subtree[0].second.begin()->first;
22713a2d0424SChris Cain         if (service.empty())
22723a2d0424SChris Cain         {
22733a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode service mapper error!";
2274ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
22753a2d0424SChris Cain             return;
22763a2d0424SChris Cain         }
22773a2d0424SChris Cain 
22783a2d0424SChris Cain         BMCWEB_LOG_DEBUG << "Setting power mode(" << powerMode << ") -> "
22793a2d0424SChris Cain                          << path;
22803a2d0424SChris Cain 
22813a2d0424SChris Cain         // Set the Power Mode property
2282*9ae226faSGeorge Liu         sdbusplus::asio::setProperty(
2283*9ae226faSGeorge Liu             *crow::connections::systemBus, service, path,
2284*9ae226faSGeorge Liu             "xyz.openbmc_project.Control.Power.Mode", "PowerMode", powerMode,
2285ac106bf6SEd Tanous             [asyncResp](const boost::system::error_code& ec2) {
22868a592810SEd Tanous             if (ec2)
22873a2d0424SChris Cain             {
2288ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
22893a2d0424SChris Cain                 return;
22903a2d0424SChris Cain             }
2291*9ae226faSGeorge Liu             });
2292e99073f5SGeorge Liu         });
22933a2d0424SChris Cain }
22943a2d0424SChris Cain 
22953a2d0424SChris Cain /**
229651709ffdSYong Li  * @brief Translates watchdog timeout action DBUS property value to redfish.
229751709ffdSYong Li  *
229851709ffdSYong Li  * @param[in] dbusAction    The watchdog timeout action in D-BUS.
229951709ffdSYong Li  *
230051709ffdSYong Li  * @return Returns as a string, the timeout action in Redfish terms. If
230151709ffdSYong Li  * translation cannot be done, returns an empty string.
230251709ffdSYong Li  */
230323a21a1cSEd Tanous inline std::string dbusToRfWatchdogAction(const std::string& dbusAction)
230451709ffdSYong Li {
230551709ffdSYong Li     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.None")
230651709ffdSYong Li     {
230751709ffdSYong Li         return "None";
230851709ffdSYong Li     }
23093174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.HardReset")
231051709ffdSYong Li     {
231151709ffdSYong Li         return "ResetSystem";
231251709ffdSYong Li     }
23133174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerOff")
231451709ffdSYong Li     {
231551709ffdSYong Li         return "PowerDown";
231651709ffdSYong Li     }
23173174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerCycle")
231851709ffdSYong Li     {
231951709ffdSYong Li         return "PowerCycle";
232051709ffdSYong Li     }
232151709ffdSYong Li 
232251709ffdSYong Li     return "";
232351709ffdSYong Li }
232451709ffdSYong Li 
232551709ffdSYong Li /**
2326c45f0082SYong Li  *@brief Translates timeout action from Redfish to DBUS property value.
2327c45f0082SYong Li  *
2328c45f0082SYong Li  *@param[in] rfAction The timeout action in Redfish.
2329c45f0082SYong Li  *
2330c45f0082SYong Li  *@return Returns as a string, the time_out action as expected by DBUS.
2331c45f0082SYong Li  *If translation cannot be done, returns an empty string.
2332c45f0082SYong Li  */
2333c45f0082SYong Li 
233423a21a1cSEd Tanous inline std::string rfToDbusWDTTimeOutAct(const std::string& rfAction)
2335c45f0082SYong Li {
2336c45f0082SYong Li     if (rfAction == "None")
2337c45f0082SYong Li     {
2338c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.None";
2339c45f0082SYong Li     }
23403174e4dfSEd Tanous     if (rfAction == "PowerCycle")
2341c45f0082SYong Li     {
2342c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerCycle";
2343c45f0082SYong Li     }
23443174e4dfSEd Tanous     if (rfAction == "PowerDown")
2345c45f0082SYong Li     {
2346c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerOff";
2347c45f0082SYong Li     }
23483174e4dfSEd Tanous     if (rfAction == "ResetSystem")
2349c45f0082SYong Li     {
2350c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.HardReset";
2351c45f0082SYong Li     }
2352c45f0082SYong Li 
2353c45f0082SYong Li     return "";
2354c45f0082SYong Li }
2355c45f0082SYong Li 
2356c45f0082SYong Li /**
235751709ffdSYong Li  * @brief Retrieves host watchdog timer properties over DBUS
235851709ffdSYong Li  *
2359ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
236051709ffdSYong Li  *
236151709ffdSYong Li  * @return None.
236251709ffdSYong Li  */
23638d1b46d7Szhanghch05 inline void
2364ac106bf6SEd Tanous     getHostWatchdogTimer(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
236551709ffdSYong Li {
236651709ffdSYong Li     BMCWEB_LOG_DEBUG << "Get host watchodg";
2367bc1d29deSKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
2368bc1d29deSKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.Watchdog",
2369bc1d29deSKrzysztof Grobelny         "/xyz/openbmc_project/watchdog/host0",
2370bc1d29deSKrzysztof Grobelny         "xyz.openbmc_project.State.Watchdog",
2371ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
2372b9d36b47SEd Tanous                     const dbus::utility::DBusPropertiesMap& properties) {
237351709ffdSYong Li         if (ec)
237451709ffdSYong Li         {
237551709ffdSYong Li             // watchdog service is stopped
237651709ffdSYong Li             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
237751709ffdSYong Li             return;
237851709ffdSYong Li         }
237951709ffdSYong Li 
238051709ffdSYong Li         BMCWEB_LOG_DEBUG << "Got " << properties.size() << " wdt prop.";
238151709ffdSYong Li 
238251709ffdSYong Li         nlohmann::json& hostWatchdogTimer =
2383ac106bf6SEd Tanous             asyncResp->res.jsonValue["HostWatchdogTimer"];
238451709ffdSYong Li 
238551709ffdSYong Li         // watchdog service is running/enabled
238651709ffdSYong Li         hostWatchdogTimer["Status"]["State"] = "Enabled";
238751709ffdSYong Li 
2388bc1d29deSKrzysztof Grobelny         const bool* enabled = nullptr;
2389bc1d29deSKrzysztof Grobelny         const std::string* expireAction = nullptr;
239051709ffdSYong Li 
2391bc1d29deSKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
2392bc1d29deSKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), properties, "Enabled", enabled,
2393bc1d29deSKrzysztof Grobelny             "ExpireAction", expireAction);
2394bc1d29deSKrzysztof Grobelny 
2395bc1d29deSKrzysztof Grobelny         if (!success)
239651709ffdSYong Li         {
2397ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
2398601af5edSChicago Duan             return;
239951709ffdSYong Li         }
240051709ffdSYong Li 
2401bc1d29deSKrzysztof Grobelny         if (enabled != nullptr)
240251709ffdSYong Li         {
2403bc1d29deSKrzysztof Grobelny             hostWatchdogTimer["FunctionEnabled"] = *enabled;
240451709ffdSYong Li         }
240551709ffdSYong Li 
2406bc1d29deSKrzysztof Grobelny         if (expireAction != nullptr)
2407bc1d29deSKrzysztof Grobelny         {
2408bc1d29deSKrzysztof Grobelny             std::string action = dbusToRfWatchdogAction(*expireAction);
240951709ffdSYong Li             if (action.empty())
241051709ffdSYong Li             {
2411ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
2412601af5edSChicago Duan                 return;
241351709ffdSYong Li             }
241451709ffdSYong Li             hostWatchdogTimer["TimeoutAction"] = action;
241551709ffdSYong Li         }
2416bc1d29deSKrzysztof Grobelny         });
241751709ffdSYong Li }
241851709ffdSYong Li 
241951709ffdSYong Li /**
2420c45f0082SYong Li  * @brief Sets Host WatchDog Timer properties.
2421c45f0082SYong Li  *
2422ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
2423c45f0082SYong Li  * @param[in] wdtEnable  The WDTimer Enable value (true/false) from incoming
2424c45f0082SYong Li  *                       RF request.
2425c45f0082SYong Li  * @param[in] wdtTimeOutAction The WDT Timeout action, from incoming RF request.
2426c45f0082SYong Li  *
2427c45f0082SYong Li  * @return None.
2428c45f0082SYong Li  */
2429ac106bf6SEd Tanous inline void
2430ac106bf6SEd Tanous     setWDTProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2431c45f0082SYong Li                      const std::optional<bool> wdtEnable,
2432c45f0082SYong Li                      const std::optional<std::string>& wdtTimeOutAction)
2433c45f0082SYong Li {
2434c45f0082SYong Li     BMCWEB_LOG_DEBUG << "Set host watchdog";
2435c45f0082SYong Li 
2436c45f0082SYong Li     if (wdtTimeOutAction)
2437c45f0082SYong Li     {
2438c45f0082SYong Li         std::string wdtTimeOutActStr = rfToDbusWDTTimeOutAct(*wdtTimeOutAction);
2439c45f0082SYong Li         // check if TimeOut Action is Valid
2440c45f0082SYong Li         if (wdtTimeOutActStr.empty())
2441c45f0082SYong Li         {
2442c45f0082SYong Li             BMCWEB_LOG_DEBUG << "Unsupported value for TimeoutAction: "
2443c45f0082SYong Li                              << *wdtTimeOutAction;
2444ac106bf6SEd Tanous             messages::propertyValueNotInList(asyncResp->res, *wdtTimeOutAction,
2445c45f0082SYong Li                                              "TimeoutAction");
2446c45f0082SYong Li             return;
2447c45f0082SYong Li         }
2448c45f0082SYong Li 
2449*9ae226faSGeorge Liu         sdbusplus::asio::setProperty(
2450*9ae226faSGeorge Liu             *crow::connections::systemBus, "xyz.openbmc_project.Watchdog",
2451*9ae226faSGeorge Liu             "/xyz/openbmc_project/watchdog/host0",
2452*9ae226faSGeorge Liu             "xyz.openbmc_project.State.Watchdog", "ExpireAction",
2453*9ae226faSGeorge Liu             wdtTimeOutActStr, [asyncResp](const boost::system::error_code& ec) {
2454c45f0082SYong Li                 if (ec)
2455c45f0082SYong Li                 {
2456c45f0082SYong Li                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
2457ac106bf6SEd Tanous                     messages::internalError(asyncResp->res);
2458c45f0082SYong Li                     return;
2459c45f0082SYong Li                 }
2460*9ae226faSGeorge Liu             });
2461c45f0082SYong Li     }
2462c45f0082SYong Li 
2463c45f0082SYong Li     if (wdtEnable)
2464c45f0082SYong Li     {
2465*9ae226faSGeorge Liu         sdbusplus::asio::setProperty(
2466*9ae226faSGeorge Liu             *crow::connections::systemBus, "xyz.openbmc_project.Watchdog",
2467*9ae226faSGeorge Liu             "/xyz/openbmc_project/watchdog/host0",
2468*9ae226faSGeorge Liu             "xyz.openbmc_project.State.Watchdog", "Enabled", *wdtEnable,
2469ac106bf6SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
2470c45f0082SYong Li             if (ec)
2471c45f0082SYong Li             {
2472c45f0082SYong Li                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
2473ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
2474c45f0082SYong Li                 return;
2475c45f0082SYong Li             }
2476*9ae226faSGeorge Liu             });
2477c45f0082SYong Li     }
2478c45f0082SYong Li }
2479c45f0082SYong Li 
248037bbf98cSChris Cain /**
248137bbf98cSChris Cain  * @brief Parse the Idle Power Saver properties into json
248237bbf98cSChris Cain  *
2483ac106bf6SEd Tanous  * @param[in] asyncResp   Shared pointer for completing asynchronous calls.
248437bbf98cSChris Cain  * @param[in] properties  IPS property data from DBus.
248537bbf98cSChris Cain  *
248637bbf98cSChris Cain  * @return true if successful
248737bbf98cSChris Cain  */
24881e5b7c88SJiaqing Zhao inline bool
2489ac106bf6SEd Tanous     parseIpsProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24901e5b7c88SJiaqing Zhao                        const dbus::utility::DBusPropertiesMap& properties)
249137bbf98cSChris Cain {
2492bc1d29deSKrzysztof Grobelny     const bool* enabled = nullptr;
2493bc1d29deSKrzysztof Grobelny     const uint8_t* enterUtilizationPercent = nullptr;
2494bc1d29deSKrzysztof Grobelny     const uint64_t* enterDwellTime = nullptr;
2495bc1d29deSKrzysztof Grobelny     const uint8_t* exitUtilizationPercent = nullptr;
2496bc1d29deSKrzysztof Grobelny     const uint64_t* exitDwellTime = nullptr;
2497bc1d29deSKrzysztof Grobelny 
2498bc1d29deSKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
2499bc1d29deSKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), properties, "Enabled", enabled,
25002661b72cSChris Cain         "EnterUtilizationPercent", enterUtilizationPercent, "EnterDwellTime",
25012661b72cSChris Cain         enterDwellTime, "ExitUtilizationPercent", exitUtilizationPercent,
25022661b72cSChris Cain         "ExitDwellTime", exitDwellTime);
2503bc1d29deSKrzysztof Grobelny 
2504bc1d29deSKrzysztof Grobelny     if (!success)
250537bbf98cSChris Cain     {
250637bbf98cSChris Cain         return false;
250737bbf98cSChris Cain     }
2508bc1d29deSKrzysztof Grobelny 
2509bc1d29deSKrzysztof Grobelny     if (enabled != nullptr)
251037bbf98cSChris Cain     {
2511ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["Enabled"] = *enabled;
251237bbf98cSChris Cain     }
2513bc1d29deSKrzysztof Grobelny 
2514bc1d29deSKrzysztof Grobelny     if (enterUtilizationPercent != nullptr)
251537bbf98cSChris Cain     {
2516ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["EnterUtilizationPercent"] =
2517bc1d29deSKrzysztof Grobelny             *enterUtilizationPercent;
251837bbf98cSChris Cain     }
2519bc1d29deSKrzysztof Grobelny 
2520bc1d29deSKrzysztof Grobelny     if (enterDwellTime != nullptr)
2521bc1d29deSKrzysztof Grobelny     {
2522bc1d29deSKrzysztof Grobelny         const std::chrono::duration<uint64_t, std::milli> ms(*enterDwellTime);
2523ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["EnterDwellTimeSeconds"] =
252437bbf98cSChris Cain             std::chrono::duration_cast<std::chrono::duration<uint64_t>>(ms)
252537bbf98cSChris Cain                 .count();
252637bbf98cSChris Cain     }
2527bc1d29deSKrzysztof Grobelny 
2528bc1d29deSKrzysztof Grobelny     if (exitUtilizationPercent != nullptr)
252937bbf98cSChris Cain     {
2530ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["ExitUtilizationPercent"] =
2531bc1d29deSKrzysztof Grobelny             *exitUtilizationPercent;
253237bbf98cSChris Cain     }
2533bc1d29deSKrzysztof Grobelny 
2534bc1d29deSKrzysztof Grobelny     if (exitDwellTime != nullptr)
253537bbf98cSChris Cain     {
2536bc1d29deSKrzysztof Grobelny         const std::chrono::duration<uint64_t, std::milli> ms(*exitDwellTime);
2537ac106bf6SEd Tanous         asyncResp->res.jsonValue["IdlePowerSaver"]["ExitDwellTimeSeconds"] =
253837bbf98cSChris Cain             std::chrono::duration_cast<std::chrono::duration<uint64_t>>(ms)
253937bbf98cSChris Cain                 .count();
254037bbf98cSChris Cain     }
254137bbf98cSChris Cain 
254237bbf98cSChris Cain     return true;
254337bbf98cSChris Cain }
254437bbf98cSChris Cain 
254537bbf98cSChris Cain /**
254637bbf98cSChris Cain  * @brief Retrieves host watchdog timer properties over DBUS
254737bbf98cSChris Cain  *
2548ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
254937bbf98cSChris Cain  *
255037bbf98cSChris Cain  * @return None.
255137bbf98cSChris Cain  */
2552ac106bf6SEd Tanous inline void
2553ac106bf6SEd Tanous     getIdlePowerSaver(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
255437bbf98cSChris Cain {
255537bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "Get idle power saver parameters";
255637bbf98cSChris Cain 
255737bbf98cSChris Cain     // Get IdlePowerSaver object path:
2558e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2559e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.IdlePowerSaver"};
2560e99073f5SGeorge Liu     dbus::utility::getSubTree(
2561e99073f5SGeorge Liu         "/", 0, interfaces,
2562ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
2563b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
256437bbf98cSChris Cain         if (ec)
256537bbf98cSChris Cain         {
256637bbf98cSChris Cain             BMCWEB_LOG_DEBUG
256737bbf98cSChris Cain                 << "DBUS response error on Power.IdlePowerSaver GetSubTree "
256837bbf98cSChris Cain                 << ec;
2569ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
257037bbf98cSChris Cain             return;
257137bbf98cSChris Cain         }
257237bbf98cSChris Cain         if (subtree.empty())
257337bbf98cSChris Cain         {
257437bbf98cSChris Cain             // This is an optional interface so just return
257537bbf98cSChris Cain             // if there is no instance found
257637bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "No instances found";
257737bbf98cSChris Cain             return;
257837bbf98cSChris Cain         }
257937bbf98cSChris Cain         if (subtree.size() > 1)
258037bbf98cSChris Cain         {
258137bbf98cSChris Cain             // More then one PowerIdlePowerSaver object is not supported and
258237bbf98cSChris Cain             // is an error
258337bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "Found more than 1 system D-Bus "
258437bbf98cSChris Cain                                 "Power.IdlePowerSaver objects: "
258537bbf98cSChris Cain                              << subtree.size();
2586ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
258737bbf98cSChris Cain             return;
258837bbf98cSChris Cain         }
258937bbf98cSChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
259037bbf98cSChris Cain         {
259137bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver mapper error!";
2592ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
259337bbf98cSChris Cain             return;
259437bbf98cSChris Cain         }
259537bbf98cSChris Cain         const std::string& path = subtree[0].first;
259637bbf98cSChris Cain         const std::string& service = subtree[0].second.begin()->first;
259737bbf98cSChris Cain         if (service.empty())
259837bbf98cSChris Cain         {
2599002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver service mapper error!";
2600ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
260137bbf98cSChris Cain             return;
260237bbf98cSChris Cain         }
260337bbf98cSChris Cain 
260437bbf98cSChris Cain         // Valid IdlePowerSaver object found, now read the current values
2605bc1d29deSKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
2606bc1d29deSKrzysztof Grobelny             *crow::connections::systemBus, service, path,
2607bc1d29deSKrzysztof Grobelny             "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2608ac106bf6SEd Tanous             [asyncResp](const boost::system::error_code& ec2,
26091e5b7c88SJiaqing Zhao                         const dbus::utility::DBusPropertiesMap& properties) {
26108a592810SEd Tanous             if (ec2)
261137bbf98cSChris Cain             {
261237bbf98cSChris Cain                 BMCWEB_LOG_ERROR
26138a592810SEd Tanous                     << "DBUS response error on IdlePowerSaver GetAll: " << ec2;
2614ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
261537bbf98cSChris Cain                 return;
261637bbf98cSChris Cain             }
261737bbf98cSChris Cain 
2618ac106bf6SEd Tanous             if (!parseIpsProperties(asyncResp, properties))
261937bbf98cSChris Cain             {
2620ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
262137bbf98cSChris Cain                 return;
262237bbf98cSChris Cain             }
2623bc1d29deSKrzysztof Grobelny             });
2624e99073f5SGeorge Liu         });
262537bbf98cSChris Cain 
262637bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "EXIT: Get idle power saver parameters";
262737bbf98cSChris Cain }
262837bbf98cSChris Cain 
262937bbf98cSChris Cain /**
263037bbf98cSChris Cain  * @brief Sets Idle Power Saver properties.
263137bbf98cSChris Cain  *
2632ac106bf6SEd Tanous  * @param[in] asyncResp  Shared pointer for generating response message.
263337bbf98cSChris Cain  * @param[in] ipsEnable  The IPS Enable value (true/false) from incoming
263437bbf98cSChris Cain  *                       RF request.
263537bbf98cSChris Cain  * @param[in] ipsEnterUtil The utilization limit to enter idle state.
263637bbf98cSChris Cain  * @param[in] ipsEnterTime The time the utilization must be below ipsEnterUtil
263737bbf98cSChris Cain  * before entering idle state.
263837bbf98cSChris Cain  * @param[in] ipsExitUtil The utilization limit when exiting idle state.
263937bbf98cSChris Cain  * @param[in] ipsExitTime The time the utilization must be above ipsExutUtil
264037bbf98cSChris Cain  * before exiting idle state
264137bbf98cSChris Cain  *
264237bbf98cSChris Cain  * @return None.
264337bbf98cSChris Cain  */
2644ac106bf6SEd Tanous inline void
2645ac106bf6SEd Tanous     setIdlePowerSaver(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
264637bbf98cSChris Cain                       const std::optional<bool> ipsEnable,
264737bbf98cSChris Cain                       const std::optional<uint8_t> ipsEnterUtil,
264837bbf98cSChris Cain                       const std::optional<uint64_t> ipsEnterTime,
264937bbf98cSChris Cain                       const std::optional<uint8_t> ipsExitUtil,
265037bbf98cSChris Cain                       const std::optional<uint64_t> ipsExitTime)
265137bbf98cSChris Cain {
265237bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "Set idle power saver properties";
265337bbf98cSChris Cain 
265437bbf98cSChris Cain     // Get IdlePowerSaver object path:
2655e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2656e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.IdlePowerSaver"};
2657e99073f5SGeorge Liu     dbus::utility::getSubTree(
2658e99073f5SGeorge Liu         "/", 0, interfaces,
2659ac106bf6SEd Tanous         [asyncResp, ipsEnable, ipsEnterUtil, ipsEnterTime, ipsExitUtil,
2660e99073f5SGeorge Liu          ipsExitTime](const boost::system::error_code& ec,
2661b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
266237bbf98cSChris Cain         if (ec)
266337bbf98cSChris Cain         {
266437bbf98cSChris Cain             BMCWEB_LOG_DEBUG
266537bbf98cSChris Cain                 << "DBUS response error on Power.IdlePowerSaver GetSubTree "
266637bbf98cSChris Cain                 << ec;
2667ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
266837bbf98cSChris Cain             return;
266937bbf98cSChris Cain         }
267037bbf98cSChris Cain         if (subtree.empty())
267137bbf98cSChris Cain         {
267237bbf98cSChris Cain             // This is an optional D-Bus object, but user attempted to patch
2673ac106bf6SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
267437bbf98cSChris Cain                                        "IdlePowerSaver");
267537bbf98cSChris Cain             return;
267637bbf98cSChris Cain         }
267737bbf98cSChris Cain         if (subtree.size() > 1)
267837bbf98cSChris Cain         {
267937bbf98cSChris Cain             // More then one PowerIdlePowerSaver object is not supported and
268037bbf98cSChris Cain             // is an error
26810fda0f12SGeorge Liu             BMCWEB_LOG_DEBUG
26820fda0f12SGeorge Liu                 << "Found more than 1 system D-Bus Power.IdlePowerSaver objects: "
268337bbf98cSChris Cain                 << subtree.size();
2684ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
268537bbf98cSChris Cain             return;
268637bbf98cSChris Cain         }
268737bbf98cSChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
268837bbf98cSChris Cain         {
268937bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver mapper error!";
2690ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
269137bbf98cSChris Cain             return;
269237bbf98cSChris Cain         }
269337bbf98cSChris Cain         const std::string& path = subtree[0].first;
269437bbf98cSChris Cain         const std::string& service = subtree[0].second.begin()->first;
269537bbf98cSChris Cain         if (service.empty())
269637bbf98cSChris Cain         {
2697002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver service mapper error!";
2698ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
269937bbf98cSChris Cain             return;
270037bbf98cSChris Cain         }
270137bbf98cSChris Cain 
270237bbf98cSChris Cain         // Valid Power IdlePowerSaver object found, now set any values that
270337bbf98cSChris Cain         // need to be updated
270437bbf98cSChris Cain 
270537bbf98cSChris Cain         if (ipsEnable)
270637bbf98cSChris Cain         {
2707*9ae226faSGeorge Liu             sdbusplus::asio::setProperty(
2708*9ae226faSGeorge Liu                 *crow::connections::systemBus, service, path,
2709*9ae226faSGeorge Liu                 "xyz.openbmc_project.Control.Power.IdlePowerSaver", "Enabled",
2710*9ae226faSGeorge Liu                 *ipsEnable, [asyncResp](const boost::system::error_code& ec2) {
27118a592810SEd Tanous                     if (ec2)
271237bbf98cSChris Cain                     {
27138a592810SEd Tanous                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
2714ac106bf6SEd Tanous                         messages::internalError(asyncResp->res);
271537bbf98cSChris Cain                         return;
271637bbf98cSChris Cain                     }
2717*9ae226faSGeorge Liu                 });
271837bbf98cSChris Cain         }
271937bbf98cSChris Cain         if (ipsEnterUtil)
272037bbf98cSChris Cain         {
2721*9ae226faSGeorge Liu             sdbusplus::asio::setProperty(
2722*9ae226faSGeorge Liu                 *crow::connections::systemBus, service, path,
2723*9ae226faSGeorge Liu                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2724*9ae226faSGeorge Liu                 "EnterUtilizationPercent", *ipsEnterUtil,
2725ac106bf6SEd Tanous                 [asyncResp](const boost::system::error_code& ec2) {
27268a592810SEd Tanous                 if (ec2)
272737bbf98cSChris Cain                 {
27288a592810SEd Tanous                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
2729ac106bf6SEd Tanous                     messages::internalError(asyncResp->res);
273037bbf98cSChris Cain                     return;
273137bbf98cSChris Cain                 }
2732*9ae226faSGeorge Liu                 });
273337bbf98cSChris Cain         }
273437bbf98cSChris Cain         if (ipsEnterTime)
273537bbf98cSChris Cain         {
273637bbf98cSChris Cain             // Convert from seconds into milliseconds for DBus
273737bbf98cSChris Cain             const uint64_t timeMilliseconds = *ipsEnterTime * 1000;
2738*9ae226faSGeorge Liu             sdbusplus::asio::setProperty(
2739*9ae226faSGeorge Liu                 *crow::connections::systemBus, service, path,
2740*9ae226faSGeorge Liu                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2741*9ae226faSGeorge Liu                 "EnterDwellTime", timeMilliseconds,
2742ac106bf6SEd Tanous                 [asyncResp](const boost::system::error_code& ec2) {
27438a592810SEd Tanous                 if (ec2)
274437bbf98cSChris Cain                 {
27458a592810SEd Tanous                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
2746ac106bf6SEd Tanous                     messages::internalError(asyncResp->res);
274737bbf98cSChris Cain                     return;
274837bbf98cSChris Cain                 }
2749*9ae226faSGeorge Liu                 });
275037bbf98cSChris Cain         }
275137bbf98cSChris Cain         if (ipsExitUtil)
275237bbf98cSChris Cain         {
2753*9ae226faSGeorge Liu             sdbusplus::asio::setProperty(
2754*9ae226faSGeorge Liu                 *crow::connections::systemBus, service, path,
2755*9ae226faSGeorge Liu                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2756*9ae226faSGeorge Liu                 "ExitUtilizationPercent", *ipsExitUtil,
2757ac106bf6SEd Tanous                 [asyncResp](const boost::system::error_code& ec2) {
27588a592810SEd Tanous                 if (ec2)
275937bbf98cSChris Cain                 {
27608a592810SEd Tanous                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
2761ac106bf6SEd Tanous                     messages::internalError(asyncResp->res);
276237bbf98cSChris Cain                     return;
276337bbf98cSChris Cain                 }
2764*9ae226faSGeorge Liu                 });
276537bbf98cSChris Cain         }
276637bbf98cSChris Cain         if (ipsExitTime)
276737bbf98cSChris Cain         {
276837bbf98cSChris Cain             // Convert from seconds into milliseconds for DBus
276937bbf98cSChris Cain             const uint64_t timeMilliseconds = *ipsExitTime * 1000;
2770*9ae226faSGeorge Liu             sdbusplus::asio::setProperty(
2771*9ae226faSGeorge Liu                 *crow::connections::systemBus, service, path,
2772*9ae226faSGeorge Liu                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2773*9ae226faSGeorge Liu                 "ExitDwellTime", timeMilliseconds,
2774ac106bf6SEd Tanous                 [asyncResp](const boost::system::error_code& ec2) {
27758a592810SEd Tanous                 if (ec2)
277637bbf98cSChris Cain                 {
27778a592810SEd Tanous                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
2778ac106bf6SEd Tanous                     messages::internalError(asyncResp->res);
277937bbf98cSChris Cain                     return;
278037bbf98cSChris Cain                 }
2781*9ae226faSGeorge Liu                 });
278237bbf98cSChris Cain         }
2783e99073f5SGeorge Liu         });
278437bbf98cSChris Cain 
278537bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "EXIT: Set idle power saver parameters";
278637bbf98cSChris Cain }
278737bbf98cSChris Cain 
2788c1e219d5SEd Tanous inline void handleComputerSystemCollectionHead(
2789dd60b9edSEd Tanous     crow::App& app, const crow::Request& req,
2790dd60b9edSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2791dd60b9edSEd Tanous {
2792dd60b9edSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2793dd60b9edSEd Tanous     {
2794dd60b9edSEd Tanous         return;
2795dd60b9edSEd Tanous     }
2796dd60b9edSEd Tanous     asyncResp->res.addHeader(
2797dd60b9edSEd Tanous         boost::beast::http::field::link,
2798dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystemCollection/ComputerSystemCollection.json>; rel=describedby");
2799dd60b9edSEd Tanous }
2800dd60b9edSEd Tanous 
2801c1e219d5SEd Tanous inline void handleComputerSystemCollectionGet(
2802c1e219d5SEd Tanous     crow::App& app, const crow::Request& req,
2803c1e219d5SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
28041abe55efSEd Tanous {
28053ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2806f4c99e70SEd Tanous     {
2807f4c99e70SEd Tanous         return;
2808f4c99e70SEd Tanous     }
2809dd60b9edSEd Tanous 
2810dd60b9edSEd Tanous     asyncResp->res.addHeader(
2811dd60b9edSEd Tanous         boost::beast::http::field::link,
2812dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystemCollection.json>; rel=describedby");
28138d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
28140f74e643SEd Tanous         "#ComputerSystemCollection.ComputerSystemCollection";
28158d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Systems";
28168d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Computer System Collection";
2817462023adSSunitha Harish 
28187f3e84a1SEd Tanous     nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
28197f3e84a1SEd Tanous     ifaceArray = nlohmann::json::array();
28207f3e84a1SEd Tanous     if constexpr (bmcwebEnableMultiHost)
28217f3e84a1SEd Tanous     {
28227f3e84a1SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = 0;
28237f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
28247f3e84a1SEd Tanous         return;
28257f3e84a1SEd Tanous     }
28267f3e84a1SEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] = 1;
28277f3e84a1SEd Tanous     nlohmann::json::object_t system;
28287f3e84a1SEd Tanous     system["@odata.id"] = "/redfish/v1/Systems/system";
28297f3e84a1SEd Tanous     ifaceArray.emplace_back(std::move(system));
28301e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
2831002d39b4SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
28321e1e598dSJonathan Doman         "/xyz/openbmc_project/network/hypervisor",
2833002d39b4SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
28345e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec2,
28351e1e598dSJonathan Doman                     const std::string& /*hostName*/) {
28367f3e84a1SEd Tanous         if (ec2)
2837462023adSSunitha Harish         {
28387f3e84a1SEd Tanous             return;
28397f3e84a1SEd Tanous         }
28407f3e84a1SEd Tanous         auto val = asyncResp->res.jsonValue.find("Members@odata.count");
28417f3e84a1SEd Tanous         if (val == asyncResp->res.jsonValue.end())
28427f3e84a1SEd Tanous         {
28437f3e84a1SEd Tanous             BMCWEB_LOG_CRITICAL << "Count wasn't found??";
28447f3e84a1SEd Tanous             return;
28457f3e84a1SEd Tanous         }
28467f3e84a1SEd Tanous         uint64_t* count = val->get_ptr<uint64_t*>();
28477f3e84a1SEd Tanous         if (count == nullptr)
28487f3e84a1SEd Tanous         {
28497f3e84a1SEd Tanous             BMCWEB_LOG_CRITICAL << "Count wasn't found??";
28507f3e84a1SEd Tanous             return;
28517f3e84a1SEd Tanous         }
28527f3e84a1SEd Tanous         *count = *count + 1;
2853462023adSSunitha Harish         BMCWEB_LOG_DEBUG << "Hypervisor is available";
28547f3e84a1SEd Tanous         nlohmann::json& ifaceArray2 = asyncResp->res.jsonValue["Members"];
28551476687dSEd Tanous         nlohmann::json::object_t hypervisor;
2856002d39b4SEd Tanous         hypervisor["@odata.id"] = "/redfish/v1/Systems/hypervisor";
28577f3e84a1SEd Tanous         ifaceArray2.emplace_back(std::move(hypervisor));
28581e1e598dSJonathan Doman         });
2859c1e219d5SEd Tanous }
2860c1e219d5SEd Tanous 
2861c1e219d5SEd Tanous /**
28627e860f15SJohn Edward Broadbent  * Function transceives data with dbus directly.
28637e860f15SJohn Edward Broadbent  */
28644f48d5f6SEd Tanous inline void doNMI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
28657e860f15SJohn Edward Broadbent {
286689492a15SPatrick Williams     constexpr const char* serviceName = "xyz.openbmc_project.Control.Host.NMI";
286789492a15SPatrick Williams     constexpr const char* objectPath = "/xyz/openbmc_project/control/host0/nmi";
286889492a15SPatrick Williams     constexpr const char* interfaceName =
28697e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Control.Host.NMI";
287089492a15SPatrick Williams     constexpr const char* method = "NMI";
28717e860f15SJohn Edward Broadbent 
28727e860f15SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
28735e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
28747e860f15SJohn Edward Broadbent         if (ec)
28757e860f15SJohn Edward Broadbent         {
28767e860f15SJohn Edward Broadbent             BMCWEB_LOG_ERROR << " Bad D-Bus request error: " << ec;
28777e860f15SJohn Edward Broadbent             messages::internalError(asyncResp->res);
28787e860f15SJohn Edward Broadbent             return;
28797e860f15SJohn Edward Broadbent         }
28807e860f15SJohn Edward Broadbent         messages::success(asyncResp->res);
28817e860f15SJohn Edward Broadbent         },
28827e860f15SJohn Edward Broadbent         serviceName, objectPath, interfaceName, method);
28837e860f15SJohn Edward Broadbent }
2884c5b2abe0SLewanczyk, Dawid 
2885c5b2abe0SLewanczyk, Dawid /**
2886fc903b3dSAndrew Geissler  * Handle error responses from d-bus for system power requests
2887fc903b3dSAndrew Geissler  */
2888fc903b3dSAndrew Geissler inline void handleSystemActionResetError(const boost::system::error_code& ec,
2889fc903b3dSAndrew Geissler                                          const sdbusplus::message_t& eMsg,
2890fc903b3dSAndrew Geissler                                          std::string_view resetType,
2891fc903b3dSAndrew Geissler                                          crow::Response& res)
2892fc903b3dSAndrew Geissler {
2893fc903b3dSAndrew Geissler     if (ec.value() == boost::asio::error::invalid_argument)
2894fc903b3dSAndrew Geissler     {
2895fc903b3dSAndrew Geissler         messages::actionParameterNotSupported(res, resetType, "Reset");
2896fc903b3dSAndrew Geissler         return;
2897fc903b3dSAndrew Geissler     }
2898fc903b3dSAndrew Geissler 
2899fc903b3dSAndrew Geissler     if (eMsg.get_error() == nullptr)
2900fc903b3dSAndrew Geissler     {
2901fc903b3dSAndrew Geissler         BMCWEB_LOG_ERROR << "D-Bus response error: " << ec;
2902fc903b3dSAndrew Geissler         messages::internalError(res);
2903fc903b3dSAndrew Geissler         return;
2904fc903b3dSAndrew Geissler     }
2905fc903b3dSAndrew Geissler     std::string_view errorMessage = eMsg.get_error()->name;
2906fc903b3dSAndrew Geissler 
2907fc903b3dSAndrew Geissler     // If operation failed due to BMC not being in Ready state, tell
2908fc903b3dSAndrew Geissler     // user to retry in a bit
2909fc903b3dSAndrew Geissler     if ((errorMessage ==
2910fc903b3dSAndrew Geissler          std::string_view(
2911fc903b3dSAndrew Geissler              "xyz.openbmc_project.State.Chassis.Error.BMCNotReady")) ||
2912fc903b3dSAndrew Geissler         (errorMessage ==
2913fc903b3dSAndrew Geissler          std::string_view("xyz.openbmc_project.State.Host.Error.BMCNotReady")))
2914fc903b3dSAndrew Geissler     {
2915fc903b3dSAndrew Geissler         BMCWEB_LOG_DEBUG << "BMC not ready, operation not allowed right now";
2916fc903b3dSAndrew Geissler         messages::serviceTemporarilyUnavailable(res, "10");
2917fc903b3dSAndrew Geissler         return;
2918fc903b3dSAndrew Geissler     }
2919fc903b3dSAndrew Geissler 
2920fc903b3dSAndrew Geissler     BMCWEB_LOG_ERROR << "System Action Reset transition fail " << ec
2921fc903b3dSAndrew Geissler                      << " sdbusplus:" << errorMessage;
2922fc903b3dSAndrew Geissler     messages::internalError(res);
2923fc903b3dSAndrew Geissler }
2924fc903b3dSAndrew Geissler 
2925c1e219d5SEd Tanous inline void handleComputerSystemResetActionPost(
2926c1e219d5SEd Tanous     crow::App& app, const crow::Request& req,
29277f3e84a1SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2928c1e219d5SEd Tanous     const std::string& systemName)
2929c1e219d5SEd Tanous {
29303ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
293145ca1b86SEd Tanous     {
293245ca1b86SEd Tanous         return;
293345ca1b86SEd Tanous     }
2934c1e219d5SEd Tanous     if (systemName != "system")
2935c1e219d5SEd Tanous     {
2936c1e219d5SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2937c1e219d5SEd Tanous                                    systemName);
2938c1e219d5SEd Tanous         return;
2939c1e219d5SEd Tanous     }
29407f3e84a1SEd Tanous     if constexpr (bmcwebEnableMultiHost)
29417f3e84a1SEd Tanous     {
29427f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
29437f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2944c1e219d5SEd Tanous                                    systemName);
29457f3e84a1SEd Tanous         return;
29467f3e84a1SEd Tanous     }
29479712f8acSEd Tanous     std::string resetType;
2948c1e219d5SEd Tanous     if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
2949cc340dd9SEd Tanous     {
2950cc340dd9SEd Tanous         return;
2951cc340dd9SEd Tanous     }
2952cc340dd9SEd Tanous 
2953d22c8396SJason M. Bills     // Get the command and host vs. chassis
2954cc340dd9SEd Tanous     std::string command;
2955543f4400SEd Tanous     bool hostCommand = true;
2956d4d25793SEd Tanous     if ((resetType == "On") || (resetType == "ForceOn"))
2957cc340dd9SEd Tanous     {
2958cc340dd9SEd Tanous         command = "xyz.openbmc_project.State.Host.Transition.On";
2959d22c8396SJason M. Bills         hostCommand = true;
2960d22c8396SJason M. Bills     }
2961d22c8396SJason M. Bills     else if (resetType == "ForceOff")
2962d22c8396SJason M. Bills     {
2963d22c8396SJason M. Bills         command = "xyz.openbmc_project.State.Chassis.Transition.Off";
2964d22c8396SJason M. Bills         hostCommand = false;
2965d22c8396SJason M. Bills     }
2966d22c8396SJason M. Bills     else if (resetType == "ForceRestart")
2967d22c8396SJason M. Bills     {
2968c1e219d5SEd Tanous         command = "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot";
296986a0851aSJason M. Bills         hostCommand = true;
2970cc340dd9SEd Tanous     }
29719712f8acSEd Tanous     else if (resetType == "GracefulShutdown")
2972cc340dd9SEd Tanous     {
2973cc340dd9SEd Tanous         command = "xyz.openbmc_project.State.Host.Transition.Off";
2974d22c8396SJason M. Bills         hostCommand = true;
2975cc340dd9SEd Tanous     }
29769712f8acSEd Tanous     else if (resetType == "GracefulRestart")
2977cc340dd9SEd Tanous     {
29780fda0f12SGeorge Liu         command =
29790fda0f12SGeorge Liu             "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot";
2980d22c8396SJason M. Bills         hostCommand = true;
2981d22c8396SJason M. Bills     }
2982d22c8396SJason M. Bills     else if (resetType == "PowerCycle")
2983d22c8396SJason M. Bills     {
298486a0851aSJason M. Bills         command = "xyz.openbmc_project.State.Host.Transition.Reboot";
298586a0851aSJason M. Bills         hostCommand = true;
2986cc340dd9SEd Tanous     }
2987bfd5b826SLakshminarayana R. Kammath     else if (resetType == "Nmi")
2988bfd5b826SLakshminarayana R. Kammath     {
2989bfd5b826SLakshminarayana R. Kammath         doNMI(asyncResp);
2990bfd5b826SLakshminarayana R. Kammath         return;
2991bfd5b826SLakshminarayana R. Kammath     }
2992cc340dd9SEd Tanous     else
2993cc340dd9SEd Tanous     {
2994c1e219d5SEd Tanous         messages::actionParameterUnknown(asyncResp->res, "Reset", resetType);
2995cc340dd9SEd Tanous         return;
2996cc340dd9SEd Tanous     }
2997cc340dd9SEd Tanous 
2998d22c8396SJason M. Bills     if (hostCommand)
2999d22c8396SJason M. Bills     {
3000*9ae226faSGeorge Liu         sdbusplus::asio::setProperty(
3001*9ae226faSGeorge Liu             *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
3002*9ae226faSGeorge Liu             "/xyz/openbmc_project/state/host0",
3003*9ae226faSGeorge Liu             "xyz.openbmc_project.State.Host", "RequestedHostTransition",
3004*9ae226faSGeorge Liu             command,
3005fc903b3dSAndrew Geissler             [asyncResp, resetType](const boost::system::error_code& ec,
3006fc903b3dSAndrew Geissler                                    sdbusplus::message_t& sdbusErrMsg) {
3007cc340dd9SEd Tanous             if (ec)
3008cc340dd9SEd Tanous             {
3009fc903b3dSAndrew Geissler                 handleSystemActionResetError(ec, sdbusErrMsg, resetType,
3010fc903b3dSAndrew Geissler                                              asyncResp->res);
3011fc903b3dSAndrew Geissler 
3012cc340dd9SEd Tanous                 return;
3013cc340dd9SEd Tanous             }
3014f12894f8SJason M. Bills             messages::success(asyncResp->res);
3015*9ae226faSGeorge Liu             });
3016cc340dd9SEd Tanous     }
3017d22c8396SJason M. Bills     else
3018d22c8396SJason M. Bills     {
3019*9ae226faSGeorge Liu         sdbusplus::asio::setProperty(
3020*9ae226faSGeorge Liu             *crow::connections::systemBus, "xyz.openbmc_project.State.Chassis",
3021*9ae226faSGeorge Liu             "/xyz/openbmc_project/state/chassis0",
3022*9ae226faSGeorge Liu             "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition",
3023*9ae226faSGeorge Liu             command,
3024fc903b3dSAndrew Geissler             [asyncResp, resetType](const boost::system::error_code& ec,
3025fc903b3dSAndrew Geissler                                    sdbusplus::message_t& sdbusErrMsg) {
3026d22c8396SJason M. Bills             if (ec)
3027d22c8396SJason M. Bills             {
3028fc903b3dSAndrew Geissler                 handleSystemActionResetError(ec, sdbusErrMsg, resetType,
3029fc903b3dSAndrew Geissler                                              asyncResp->res);
3030d22c8396SJason M. Bills                 return;
3031d22c8396SJason M. Bills             }
3032d22c8396SJason M. Bills             messages::success(asyncResp->res);
3033*9ae226faSGeorge Liu             });
3034d22c8396SJason M. Bills     }
3035d22c8396SJason M. Bills }
3036cc340dd9SEd Tanous 
3037c1e219d5SEd Tanous inline void handleComputerSystemHead(
3038dd60b9edSEd Tanous     App& app, const crow::Request& req,
30397f3e84a1SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30407f3e84a1SEd Tanous     const std::string& /*systemName*/)
3041dd60b9edSEd Tanous {
3042dd60b9edSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3043dd60b9edSEd Tanous     {
3044dd60b9edSEd Tanous         return;
3045dd60b9edSEd Tanous     }
3046dd60b9edSEd Tanous 
3047dd60b9edSEd Tanous     asyncResp->res.addHeader(
3048dd60b9edSEd Tanous         boost::beast::http::field::link,
3049dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystem/ComputerSystem.json>; rel=describedby");
3050dd60b9edSEd Tanous }
3051dd60b9edSEd Tanous 
30525c3e9272SAbhishek Patel inline void afterPortRequest(
30535c3e9272SAbhishek Patel     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30545c3e9272SAbhishek Patel     const boost::system::error_code& ec,
30555c3e9272SAbhishek Patel     const std::vector<std::tuple<std::string, std::string, bool>>& socketData)
30565c3e9272SAbhishek Patel {
30575c3e9272SAbhishek Patel     if (ec)
30585c3e9272SAbhishek Patel     {
30595c3e9272SAbhishek Patel         messages::internalError(asyncResp->res);
30605c3e9272SAbhishek Patel         return;
30615c3e9272SAbhishek Patel     }
30625c3e9272SAbhishek Patel     for (const auto& data : socketData)
30635c3e9272SAbhishek Patel     {
30645c3e9272SAbhishek Patel         const std::string& socketPath = get<0>(data);
30655c3e9272SAbhishek Patel         const std::string& protocolName = get<1>(data);
30665c3e9272SAbhishek Patel         bool isProtocolEnabled = get<2>(data);
30675c3e9272SAbhishek Patel         nlohmann::json& dataJson = asyncResp->res.jsonValue["SerialConsole"];
30685c3e9272SAbhishek Patel         dataJson[protocolName]["ServiceEnabled"] = isProtocolEnabled;
30695c3e9272SAbhishek Patel         // need to retrieve port number for
30705c3e9272SAbhishek Patel         // obmc-console-ssh service
30715c3e9272SAbhishek Patel         if (protocolName == "SSH")
30725c3e9272SAbhishek Patel         {
30735c3e9272SAbhishek Patel             getPortNumber(socketPath, [asyncResp, protocolName](
307481c4e330SEd Tanous                                           const boost::system::error_code& ec1,
30755c3e9272SAbhishek Patel                                           int portNumber) {
30765c3e9272SAbhishek Patel                 if (ec1)
30775c3e9272SAbhishek Patel                 {
30785c3e9272SAbhishek Patel                     messages::internalError(asyncResp->res);
30795c3e9272SAbhishek Patel                     return;
30805c3e9272SAbhishek Patel                 }
30815c3e9272SAbhishek Patel                 nlohmann::json& dataJson1 =
30825c3e9272SAbhishek Patel                     asyncResp->res.jsonValue["SerialConsole"];
30835c3e9272SAbhishek Patel                 dataJson1[protocolName]["Port"] = portNumber;
30845c3e9272SAbhishek Patel             });
30855c3e9272SAbhishek Patel         }
30865c3e9272SAbhishek Patel     }
30875c3e9272SAbhishek Patel }
3088c1e219d5SEd Tanous 
3089c1e219d5SEd Tanous inline void
3090c1e219d5SEd Tanous     handleComputerSystemGet(crow::App& app, const crow::Request& req,
309122d268cbSEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3092c1e219d5SEd Tanous                             const std::string& systemName)
3093c1e219d5SEd Tanous {
30943ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
309545ca1b86SEd Tanous     {
309645ca1b86SEd Tanous         return;
309745ca1b86SEd Tanous     }
3098746b56f3SAsmitha Karunanithi 
30997f3e84a1SEd Tanous     if constexpr (bmcwebEnableMultiHost)
31007f3e84a1SEd Tanous     {
31017f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
31027f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
31037f3e84a1SEd Tanous                                    systemName);
31047f3e84a1SEd Tanous         return;
31057f3e84a1SEd Tanous     }
31067f3e84a1SEd Tanous 
3107746b56f3SAsmitha Karunanithi     if (systemName == "hypervisor")
3108746b56f3SAsmitha Karunanithi     {
3109746b56f3SAsmitha Karunanithi         handleHypervisorSystemGet(asyncResp);
3110746b56f3SAsmitha Karunanithi         return;
3111746b56f3SAsmitha Karunanithi     }
3112746b56f3SAsmitha Karunanithi 
311322d268cbSEd Tanous     if (systemName != "system")
311422d268cbSEd Tanous     {
311522d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
311622d268cbSEd Tanous                                    systemName);
311722d268cbSEd Tanous         return;
311822d268cbSEd Tanous     }
3119dd60b9edSEd Tanous     asyncResp->res.addHeader(
3120dd60b9edSEd Tanous         boost::beast::http::field::link,
3121dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystem/ComputerSystem.json>; rel=describedby");
31228d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
312337bbf98cSChris Cain         "#ComputerSystem.v1_16_0.ComputerSystem";
31248d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "system";
31258d1b46d7Szhanghch05     asyncResp->res.jsonValue["Id"] = "system";
31268d1b46d7Szhanghch05     asyncResp->res.jsonValue["SystemType"] = "Physical";
31278d1b46d7Szhanghch05     asyncResp->res.jsonValue["Description"] = "Computer System";
31288d1b46d7Szhanghch05     asyncResp->res.jsonValue["ProcessorSummary"]["Count"] = 0;
31295fd0aafbSNinad Palsule     if constexpr (bmcwebEnableProcMemStatus)
31305fd0aafbSNinad Palsule     {
31318d1b46d7Szhanghch05         asyncResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
31328d1b46d7Szhanghch05             "Disabled";
31338d1b46d7Szhanghch05         asyncResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
31348d1b46d7Szhanghch05             "Disabled";
31355fd0aafbSNinad Palsule     }
3136cf0e004cSNinad Palsule     asyncResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] =
3137cf0e004cSNinad Palsule         uint64_t(0);
3138002d39b4SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Systems/system";
313904a258f4SEd Tanous 
31401476687dSEd Tanous     asyncResp->res.jsonValue["Processors"]["@odata.id"] =
31411476687dSEd Tanous         "/redfish/v1/Systems/system/Processors";
31421476687dSEd Tanous     asyncResp->res.jsonValue["Memory"]["@odata.id"] =
31431476687dSEd Tanous         "/redfish/v1/Systems/system/Memory";
31441476687dSEd Tanous     asyncResp->res.jsonValue["Storage"]["@odata.id"] =
31451476687dSEd Tanous         "/redfish/v1/Systems/system/Storage";
31463179105bSSunny Srivastava     asyncResp->res.jsonValue["FabricAdapters"]["@odata.id"] =
31473179105bSSunny Srivastava         "/redfish/v1/Systems/system/FabricAdapters";
3148029573d4SEd Tanous 
3149002d39b4SEd Tanous     asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"]["target"] =
31501476687dSEd Tanous         "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset";
3151c1e219d5SEd Tanous     asyncResp->res
3152c1e219d5SEd Tanous         .jsonValue["Actions"]["#ComputerSystem.Reset"]["@Redfish.ActionInfo"] =
31531476687dSEd Tanous         "/redfish/v1/Systems/system/ResetActionInfo";
3154c5b2abe0SLewanczyk, Dawid 
31551476687dSEd Tanous     asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
31561476687dSEd Tanous         "/redfish/v1/Systems/system/LogServices";
31571476687dSEd Tanous     asyncResp->res.jsonValue["Bios"]["@odata.id"] =
31581476687dSEd Tanous         "/redfish/v1/Systems/system/Bios";
3159c4bf6374SJason M. Bills 
31601476687dSEd Tanous     nlohmann::json::array_t managedBy;
31611476687dSEd Tanous     nlohmann::json& manager = managedBy.emplace_back();
31621476687dSEd Tanous     manager["@odata.id"] = "/redfish/v1/Managers/bmc";
3163002d39b4SEd Tanous     asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy);
31641476687dSEd Tanous     asyncResp->res.jsonValue["Status"]["Health"] = "OK";
31651476687dSEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
31660e8ac5e7SGunnar Mills 
31670e8ac5e7SGunnar Mills     // Fill in SerialConsole info
3168002d39b4SEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
3169c1e219d5SEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["IPMI"]["ServiceEnabled"] = true;
31701476687dSEd Tanous 
3171c1e219d5SEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["SSH"]["ServiceEnabled"] = true;
31721476687dSEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["SSH"]["Port"] = 2200;
3173c1e219d5SEd Tanous     asyncResp->res.jsonValue["SerialConsole"]["SSH"]["HotKeySequenceDisplay"] =
31741476687dSEd Tanous         "Press ~. to exit console";
31755c3e9272SAbhishek Patel     getPortStatusAndPath(std::span{protocolToDBusForSystems},
31765c3e9272SAbhishek Patel                          std::bind_front(afterPortRequest, asyncResp));
31770e8ac5e7SGunnar Mills 
31780e8ac5e7SGunnar Mills #ifdef BMCWEB_ENABLE_KVM
31790e8ac5e7SGunnar Mills     // Fill in GraphicalConsole info
3180002d39b4SEd Tanous     asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
3181c1e219d5SEd Tanous     asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
3182613dabeaSEd Tanous     asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
3183613dabeaSEd Tanous         nlohmann::json::array_t({"KVMIP"});
31841476687dSEd Tanous 
31850e8ac5e7SGunnar Mills #endif // BMCWEB_ENABLE_KVM
318613451e39SWilly Tu 
318713451e39SWilly Tu     auto health = std::make_shared<HealthPopulate>(asyncResp);
318813451e39SWilly Tu     if constexpr (bmcwebEnableHealthPopulate)
318913451e39SWilly Tu     {
31907a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 4> inventoryForSystems{
3191b49ac873SJames Feist             "xyz.openbmc_project.Inventory.Item.Dimm",
31922ad9c2f6SJames Feist             "xyz.openbmc_project.Inventory.Item.Cpu",
3193e284a7c1SJames Feist             "xyz.openbmc_project.Inventory.Item.Drive",
3194e284a7c1SJames Feist             "xyz.openbmc_project.Inventory.Item.StorageController"};
3195b49ac873SJames Feist 
31967a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
31977a1dbc48SGeorge Liu             "/", 0, inventoryForSystems,
31987a1dbc48SGeorge Liu             [health](const boost::system::error_code& ec,
3199914e2d5dSEd Tanous                      const std::vector<std::string>& resp) {
3200b49ac873SJames Feist             if (ec)
3201b49ac873SJames Feist             {
3202b49ac873SJames Feist                 // no inventory
3203b49ac873SJames Feist                 return;
3204b49ac873SJames Feist             }
3205b49ac873SJames Feist 
3206914e2d5dSEd Tanous             health->inventory = resp;
32077a1dbc48SGeorge Liu             });
3208b49ac873SJames Feist         health->populate();
320913451e39SWilly Tu     }
3210b49ac873SJames Feist 
3211002d39b4SEd Tanous     getMainChassisId(asyncResp,
3212002d39b4SEd Tanous                      [](const std::string& chassisId,
32138d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
3214b2c7e208SEd Tanous         nlohmann::json::array_t chassisArray;
3215b2c7e208SEd Tanous         nlohmann::json& chassis = chassisArray.emplace_back();
3216ef4c65b7SEd Tanous         chassis["@odata.id"] = boost::urls::format("/redfish/v1/Chassis/{}",
3217ef4c65b7SEd Tanous                                                    chassisId);
3218002d39b4SEd Tanous         aRsp->res.jsonValue["Links"]["Chassis"] = std::move(chassisArray);
3219c5d03ff4SJennifer Lee     });
3220a3002228SAppaRao Puli 
32219f8bfa7cSGunnar Mills     getLocationIndicatorActive(asyncResp);
32229f8bfa7cSGunnar Mills     // TODO (Gunnar): Remove IndicatorLED after enough time has passed
3223a3002228SAppaRao Puli     getIndicatorLedState(asyncResp);
32245bc2dc8eSJames Feist     getComputerSystem(asyncResp, health);
32256c34de48SEd Tanous     getHostState(asyncResp);
3226491d8ee7SSantosh Puranik     getBootProperties(asyncResp);
3227978b8803SAndrew Geissler     getBootProgress(asyncResp);
3228b6d5d45cSHieu Huynh     getBootProgressLastStateTime(asyncResp);
3229472bd202SLakshmi Yadlapati     pcie_util::getPCIeDeviceList(asyncResp, "PCIeDevices");
323051709ffdSYong Li     getHostWatchdogTimer(asyncResp);
3231c6a620f2SGeorge Liu     getPowerRestorePolicy(asyncResp);
3232797d5daeSCorey Hardesty     getAutomaticRetryPolicy(asyncResp);
3233c0557e1aSGunnar Mills     getLastResetTime(asyncResp);
3234a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
3235a6349918SAppaRao Puli     getProvisioningStatus(asyncResp);
3236a6349918SAppaRao Puli #endif
32371981771bSAli Ahmed     getTrustedModuleRequiredToBoot(asyncResp);
32383a2d0424SChris Cain     getPowerMode(asyncResp);
323937bbf98cSChris Cain     getIdlePowerSaver(asyncResp);
3240c1e219d5SEd Tanous }
3241550a6bf8SJiaqing Zhao 
3242c1e219d5SEd Tanous inline void handleComputerSystemPatch(
3243c1e219d5SEd Tanous     crow::App& app, const crow::Request& req,
324422d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3245c1e219d5SEd Tanous     const std::string& systemName)
3246c1e219d5SEd Tanous {
32473ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
324845ca1b86SEd Tanous     {
324945ca1b86SEd Tanous         return;
325045ca1b86SEd Tanous     }
32517f3e84a1SEd Tanous     if constexpr (bmcwebEnableMultiHost)
32527f3e84a1SEd Tanous     {
32537f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
32547f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
32557f3e84a1SEd Tanous                                    systemName);
32567f3e84a1SEd Tanous         return;
32577f3e84a1SEd Tanous     }
325822d268cbSEd Tanous     if (systemName != "system")
325922d268cbSEd Tanous     {
326022d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
326122d268cbSEd Tanous                                    systemName);
326222d268cbSEd Tanous         return;
326322d268cbSEd Tanous     }
326422d268cbSEd Tanous 
3265dd60b9edSEd Tanous     asyncResp->res.addHeader(
3266dd60b9edSEd Tanous         boost::beast::http::field::link,
3267dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystem/ComputerSystem.json>; rel=describedby");
3268dd60b9edSEd Tanous 
32699f8bfa7cSGunnar Mills     std::optional<bool> locationIndicatorActive;
3270cde19e5fSSantosh Puranik     std::optional<std::string> indicatorLed;
327198e386ecSGunnar Mills     std::optional<std::string> assetTag;
3272c6a620f2SGeorge Liu     std::optional<std::string> powerRestorePolicy;
32733a2d0424SChris Cain     std::optional<std::string> powerMode;
3274550a6bf8SJiaqing Zhao     std::optional<bool> wdtEnable;
3275550a6bf8SJiaqing Zhao     std::optional<std::string> wdtTimeOutAction;
3276550a6bf8SJiaqing Zhao     std::optional<std::string> bootSource;
3277550a6bf8SJiaqing Zhao     std::optional<std::string> bootType;
3278550a6bf8SJiaqing Zhao     std::optional<std::string> bootEnable;
3279550a6bf8SJiaqing Zhao     std::optional<std::string> bootAutomaticRetry;
3280797d5daeSCorey Hardesty     std::optional<uint32_t> bootAutomaticRetryAttempts;
3281550a6bf8SJiaqing Zhao     std::optional<bool> bootTrustedModuleRequired;
3282550a6bf8SJiaqing Zhao     std::optional<bool> ipsEnable;
3283550a6bf8SJiaqing Zhao     std::optional<uint8_t> ipsEnterUtil;
3284550a6bf8SJiaqing Zhao     std::optional<uint64_t> ipsEnterTime;
3285550a6bf8SJiaqing Zhao     std::optional<uint8_t> ipsExitUtil;
3286550a6bf8SJiaqing Zhao     std::optional<uint64_t> ipsExitTime;
3287550a6bf8SJiaqing Zhao 
3288550a6bf8SJiaqing Zhao     // clang-format off
328915ed6780SWilly Tu                 if (!json_util::readJsonPatch(
3290550a6bf8SJiaqing Zhao                         req, asyncResp->res,
3291550a6bf8SJiaqing Zhao                         "IndicatorLED", indicatorLed,
32927e860f15SJohn Edward Broadbent                         "LocationIndicatorActive", locationIndicatorActive,
3293550a6bf8SJiaqing Zhao                         "AssetTag", assetTag,
3294550a6bf8SJiaqing Zhao                         "PowerRestorePolicy", powerRestorePolicy,
3295550a6bf8SJiaqing Zhao                         "PowerMode", powerMode,
3296550a6bf8SJiaqing Zhao                         "HostWatchdogTimer/FunctionEnabled", wdtEnable,
3297550a6bf8SJiaqing Zhao                         "HostWatchdogTimer/TimeoutAction", wdtTimeOutAction,
3298550a6bf8SJiaqing Zhao                         "Boot/BootSourceOverrideTarget", bootSource,
3299550a6bf8SJiaqing Zhao                         "Boot/BootSourceOverrideMode", bootType,
3300550a6bf8SJiaqing Zhao                         "Boot/BootSourceOverrideEnabled", bootEnable,
3301550a6bf8SJiaqing Zhao                         "Boot/AutomaticRetryConfig", bootAutomaticRetry,
3302797d5daeSCorey Hardesty                         "Boot/AutomaticRetryAttempts", bootAutomaticRetryAttempts,
3303550a6bf8SJiaqing Zhao                         "Boot/TrustedModuleRequiredToBoot", bootTrustedModuleRequired,
3304550a6bf8SJiaqing Zhao                         "IdlePowerSaver/Enabled", ipsEnable,
3305550a6bf8SJiaqing Zhao                         "IdlePowerSaver/EnterUtilizationPercent", ipsEnterUtil,
3306550a6bf8SJiaqing Zhao                         "IdlePowerSaver/EnterDwellTimeSeconds", ipsEnterTime,
3307550a6bf8SJiaqing Zhao                         "IdlePowerSaver/ExitUtilizationPercent", ipsExitUtil,
3308550a6bf8SJiaqing Zhao                         "IdlePowerSaver/ExitDwellTimeSeconds", ipsExitTime))
33096617338dSEd Tanous                 {
33106617338dSEd Tanous                     return;
33116617338dSEd Tanous                 }
3312550a6bf8SJiaqing Zhao     // clang-format on
3313491d8ee7SSantosh Puranik 
33148d1b46d7Szhanghch05     asyncResp->res.result(boost::beast::http::status::no_content);
3315c45f0082SYong Li 
331698e386ecSGunnar Mills     if (assetTag)
331798e386ecSGunnar Mills     {
331898e386ecSGunnar Mills         setAssetTag(asyncResp, *assetTag);
331998e386ecSGunnar Mills     }
332098e386ecSGunnar Mills 
3321550a6bf8SJiaqing Zhao     if (wdtEnable || wdtTimeOutAction)
3322c45f0082SYong Li     {
3323f23b7296SEd Tanous         setWDTProperties(asyncResp, wdtEnable, wdtTimeOutAction);
3324c45f0082SYong Li     }
3325c45f0082SYong Li 
3326cd9a4666SKonstantin Aladyshev     if (bootSource || bootType || bootEnable)
332769f35306SGunnar Mills     {
3328002d39b4SEd Tanous         setBootProperties(asyncResp, bootSource, bootType, bootEnable);
3329491d8ee7SSantosh Puranik     }
3330550a6bf8SJiaqing Zhao     if (bootAutomaticRetry)
333169f35306SGunnar Mills     {
3332550a6bf8SJiaqing Zhao         setAutomaticRetry(asyncResp, *bootAutomaticRetry);
333369f35306SGunnar Mills     }
3334ac7e1e0bSAli Ahmed 
3335797d5daeSCorey Hardesty     if (bootAutomaticRetryAttempts)
3336797d5daeSCorey Hardesty     {
3337797d5daeSCorey Hardesty         setAutomaticRetryAttempts(asyncResp,
3338797d5daeSCorey Hardesty                                   bootAutomaticRetryAttempts.value());
3339797d5daeSCorey Hardesty     }
3340797d5daeSCorey Hardesty 
3341550a6bf8SJiaqing Zhao     if (bootTrustedModuleRequired)
3342ac7e1e0bSAli Ahmed     {
3343c1e219d5SEd Tanous         setTrustedModuleRequiredToBoot(asyncResp, *bootTrustedModuleRequired);
334469f35306SGunnar Mills     }
3345265c1602SJohnathan Mantey 
33469f8bfa7cSGunnar Mills     if (locationIndicatorActive)
33479f8bfa7cSGunnar Mills     {
3348002d39b4SEd Tanous         setLocationIndicatorActive(asyncResp, *locationIndicatorActive);
33499f8bfa7cSGunnar Mills     }
33509f8bfa7cSGunnar Mills 
33517e860f15SJohn Edward Broadbent     // TODO (Gunnar): Remove IndicatorLED after enough time has
33527e860f15SJohn Edward Broadbent     // passed
33539712f8acSEd Tanous     if (indicatorLed)
33546617338dSEd Tanous     {
3355f23b7296SEd Tanous         setIndicatorLedState(asyncResp, *indicatorLed);
3356002d39b4SEd Tanous         asyncResp->res.addHeader(boost::beast::http::field::warning,
3357d6aa0093SGunnar Mills                                  "299 - \"IndicatorLED is deprecated. Use "
3358d6aa0093SGunnar Mills                                  "LocationIndicatorActive instead.\"");
33596617338dSEd Tanous     }
3360c6a620f2SGeorge Liu 
3361c6a620f2SGeorge Liu     if (powerRestorePolicy)
3362c6a620f2SGeorge Liu     {
33634e69c904SGunnar Mills         setPowerRestorePolicy(asyncResp, *powerRestorePolicy);
3364c6a620f2SGeorge Liu     }
33653a2d0424SChris Cain 
33663a2d0424SChris Cain     if (powerMode)
33673a2d0424SChris Cain     {
33683a2d0424SChris Cain         setPowerMode(asyncResp, *powerMode);
33693a2d0424SChris Cain     }
337037bbf98cSChris Cain 
3371c1e219d5SEd Tanous     if (ipsEnable || ipsEnterUtil || ipsEnterTime || ipsExitUtil || ipsExitTime)
337237bbf98cSChris Cain     {
3373002d39b4SEd Tanous         setIdlePowerSaver(asyncResp, ipsEnable, ipsEnterUtil, ipsEnterTime,
3374002d39b4SEd Tanous                           ipsExitUtil, ipsExitTime);
337537bbf98cSChris Cain     }
3376c1e219d5SEd Tanous }
33771cb1a9e6SAppaRao Puli 
337838c8a6f2SEd Tanous inline void handleSystemCollectionResetActionHead(
3379dd60b9edSEd Tanous     crow::App& app, const crow::Request& req,
33807f3e84a1SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3381c1e219d5SEd Tanous     const std::string& /*systemName*/)
3382dd60b9edSEd Tanous {
3383dd60b9edSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3384dd60b9edSEd Tanous     {
3385dd60b9edSEd Tanous         return;
3386dd60b9edSEd Tanous     }
3387dd60b9edSEd Tanous     asyncResp->res.addHeader(
3388dd60b9edSEd Tanous         boost::beast::http::field::link,
3389dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ActionInfo/ActionInfo.json>; rel=describedby");
3390dd60b9edSEd Tanous }
3391c1e219d5SEd Tanous inline void handleSystemCollectionResetActionGet(
3392c1e219d5SEd Tanous     crow::App& app, const crow::Request& req,
339322d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3394c1e219d5SEd Tanous     const std::string& systemName)
3395c1e219d5SEd Tanous {
33963ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
339745ca1b86SEd Tanous     {
339845ca1b86SEd Tanous         return;
339945ca1b86SEd Tanous     }
34007f3e84a1SEd Tanous     if constexpr (bmcwebEnableMultiHost)
34017f3e84a1SEd Tanous     {
34027f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
34037f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
34047f3e84a1SEd Tanous                                    systemName);
34057f3e84a1SEd Tanous         return;
34067f3e84a1SEd Tanous     }
3407746b56f3SAsmitha Karunanithi 
3408746b56f3SAsmitha Karunanithi     if (systemName == "hypervisor")
3409746b56f3SAsmitha Karunanithi     {
3410746b56f3SAsmitha Karunanithi         handleHypervisorResetActionGet(asyncResp);
3411746b56f3SAsmitha Karunanithi         return;
3412746b56f3SAsmitha Karunanithi     }
3413746b56f3SAsmitha Karunanithi 
341422d268cbSEd Tanous     if (systemName != "system")
341522d268cbSEd Tanous     {
341622d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
341722d268cbSEd Tanous                                    systemName);
341822d268cbSEd Tanous         return;
341922d268cbSEd Tanous     }
342022d268cbSEd Tanous 
3421dd60b9edSEd Tanous     asyncResp->res.addHeader(
3422dd60b9edSEd Tanous         boost::beast::http::field::link,
3423dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ActionInfo/ActionInfo.json>; rel=describedby");
34241476687dSEd Tanous 
34251476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
34261476687dSEd Tanous         "/redfish/v1/Systems/system/ResetActionInfo";
3427c1e219d5SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ActionInfo.v1_1_2.ActionInfo";
34281476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Reset Action Info";
34291476687dSEd Tanous     asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
34303215e700SNan Zhou 
34313215e700SNan Zhou     nlohmann::json::array_t parameters;
34323215e700SNan Zhou     nlohmann::json::object_t parameter;
34333215e700SNan Zhou 
34343215e700SNan Zhou     parameter["Name"] = "ResetType";
34353215e700SNan Zhou     parameter["Required"] = true;
34363215e700SNan Zhou     parameter["DataType"] = "String";
34373215e700SNan Zhou     nlohmann::json::array_t allowableValues;
34383215e700SNan Zhou     allowableValues.emplace_back("On");
34393215e700SNan Zhou     allowableValues.emplace_back("ForceOff");
34403215e700SNan Zhou     allowableValues.emplace_back("ForceOn");
34413215e700SNan Zhou     allowableValues.emplace_back("ForceRestart");
34423215e700SNan Zhou     allowableValues.emplace_back("GracefulRestart");
34433215e700SNan Zhou     allowableValues.emplace_back("GracefulShutdown");
34443215e700SNan Zhou     allowableValues.emplace_back("PowerCycle");
34453215e700SNan Zhou     allowableValues.emplace_back("Nmi");
34463215e700SNan Zhou     parameter["AllowableValues"] = std::move(allowableValues);
34473215e700SNan Zhou     parameters.emplace_back(std::move(parameter));
34483215e700SNan Zhou 
34493215e700SNan Zhou     asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
3450c1e219d5SEd Tanous }
3451c1e219d5SEd Tanous /**
3452c1e219d5SEd Tanous  * SystemResetActionInfo derived class for delivering Computer Systems
3453c1e219d5SEd Tanous  * ResetType AllowableValues using ResetInfo schema.
3454c1e219d5SEd Tanous  */
3455100afe56SEd Tanous inline void requestRoutesSystems(App& app)
3456c1e219d5SEd Tanous {
3457100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/")
3458100afe56SEd Tanous         .privileges(redfish::privileges::headComputerSystemCollection)
3459100afe56SEd Tanous         .methods(boost::beast::http::verb::head)(
3460100afe56SEd Tanous             std::bind_front(handleComputerSystemCollectionHead, std::ref(app)));
3461100afe56SEd Tanous 
3462100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/")
3463100afe56SEd Tanous         .privileges(redfish::privileges::getComputerSystemCollection)
3464100afe56SEd Tanous         .methods(boost::beast::http::verb::get)(
3465100afe56SEd Tanous             std::bind_front(handleComputerSystemCollectionGet, std::ref(app)));
3466100afe56SEd Tanous 
3467100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/")
3468100afe56SEd Tanous         .privileges(redfish::privileges::headComputerSystem)
3469100afe56SEd Tanous         .methods(boost::beast::http::verb::head)(
3470100afe56SEd Tanous             std::bind_front(handleComputerSystemHead, std::ref(app)));
3471100afe56SEd Tanous 
3472100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/")
3473100afe56SEd Tanous         .privileges(redfish::privileges::getComputerSystem)
3474100afe56SEd Tanous         .methods(boost::beast::http::verb::get)(
3475100afe56SEd Tanous             std::bind_front(handleComputerSystemGet, std::ref(app)));
3476100afe56SEd Tanous 
3477100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/")
3478100afe56SEd Tanous         .privileges(redfish::privileges::patchComputerSystem)
3479100afe56SEd Tanous         .methods(boost::beast::http::verb::patch)(
3480100afe56SEd Tanous             std::bind_front(handleComputerSystemPatch, std::ref(app)));
3481100afe56SEd Tanous 
3482100afe56SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Actions/ComputerSystem.Reset/")
3483100afe56SEd Tanous         .privileges(redfish::privileges::postComputerSystem)
3484100afe56SEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
3485100afe56SEd Tanous             handleComputerSystemResetActionPost, std::ref(app)));
3486100afe56SEd Tanous 
3487c1e219d5SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/ResetActionInfo/")
3488c1e219d5SEd Tanous         .privileges(redfish::privileges::headActionInfo)
3489c1e219d5SEd Tanous         .methods(boost::beast::http::verb::head)(std::bind_front(
3490c1e219d5SEd Tanous             handleSystemCollectionResetActionHead, std::ref(app)));
3491c1e219d5SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/ResetActionInfo/")
3492c1e219d5SEd Tanous         .privileges(redfish::privileges::getActionInfo)
3493c1e219d5SEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
3494c1e219d5SEd Tanous             handleSystemCollectionResetActionGet, std::ref(app)));
34951cb1a9e6SAppaRao Puli }
3496c5b2abe0SLewanczyk, Dawid } // namespace redfish
3497