xref: /openbmc/bmcweb/features/redfish/lib/systems.hpp (revision 472bd202da308ea7ba6c9f5606e025367b52fccc)
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"
23b49ac873SJames Feist #include "health.hpp"
24746b56f3SAsmitha Karunanithi #include "hypervisor_system.hpp"
251c8fba97SJames Feist #include "led.hpp"
26f4c99e70SEd Tanous #include "query.hpp"
27c5d03ff4SJennifer Lee #include "redfish_util.hpp"
283ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
293ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
303ccb3adbSEd Tanous #include "utils/json_utils.hpp"
31*472bd202SLakshmi Yadlapati #include "utils/pcie_util.hpp"
323ccb3adbSEd Tanous #include "utils/sw_utils.hpp"
332b82937eSEd Tanous #include "utils/time_utils.hpp"
34c5d03ff4SJennifer Lee 
359712f8acSEd Tanous #include <boost/container/flat_map.hpp>
36e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
37ef4c65b7SEd Tanous #include <boost/url/format.hpp>
381e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
39bc1d29deSKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
401214b7e7SGunnar Mills 
417a1dbc48SGeorge Liu #include <array>
427a1dbc48SGeorge Liu #include <string_view>
43abf2add6SEd Tanous #include <variant>
44c5b2abe0SLewanczyk, Dawid 
451abe55efSEd Tanous namespace redfish
461abe55efSEd Tanous {
47c5b2abe0SLewanczyk, Dawid 
485c3e9272SAbhishek Patel const static std::array<std::pair<std::string_view, std::string_view>, 2>
495c3e9272SAbhishek Patel     protocolToDBusForSystems{
505c3e9272SAbhishek Patel         {{"SSH", "obmc-console-ssh"}, {"IPMI", "phosphor-ipmi-net"}}};
515c3e9272SAbhishek Patel 
529d3ae10eSAlpana Kumari /**
539d3ae10eSAlpana Kumari  * @brief Updates the Functional State of DIMMs
549d3ae10eSAlpana Kumari  *
559d3ae10eSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
569d3ae10eSAlpana Kumari  * @param[in] dimmState Dimm's Functional state, true/false
579d3ae10eSAlpana Kumari  *
589d3ae10eSAlpana Kumari  * @return None.
599d3ae10eSAlpana Kumari  */
608d1b46d7Szhanghch05 inline void
618d1b46d7Szhanghch05     updateDimmProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
621e1e598dSJonathan Doman                          bool isDimmFunctional)
639d3ae10eSAlpana Kumari {
641e1e598dSJonathan Doman     BMCWEB_LOG_DEBUG << "Dimm Functional: " << isDimmFunctional;
659d3ae10eSAlpana Kumari 
669d3ae10eSAlpana Kumari     // Set it as Enabled if at least one DIMM is functional
679d3ae10eSAlpana Kumari     // Update STATE only if previous State was DISABLED and current Dimm is
689d3ae10eSAlpana Kumari     // ENABLED.
6902cad96eSEd Tanous     const nlohmann::json& prevMemSummary =
709d3ae10eSAlpana Kumari         aResp->res.jsonValue["MemorySummary"]["Status"]["State"];
719d3ae10eSAlpana Kumari     if (prevMemSummary == "Disabled")
729d3ae10eSAlpana Kumari     {
73e05aec50SEd Tanous         if (isDimmFunctional)
749d3ae10eSAlpana Kumari         {
759d3ae10eSAlpana Kumari             aResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
769d3ae10eSAlpana Kumari                 "Enabled";
779d3ae10eSAlpana Kumari         }
789d3ae10eSAlpana Kumari     }
799d3ae10eSAlpana Kumari }
809d3ae10eSAlpana Kumari 
8157e8c9beSAlpana Kumari /*
8257e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Status" "State" based on
8357e8c9beSAlpana Kumari  *        CPU Functional State
8457e8c9beSAlpana Kumari  *
8557e8c9beSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
8657e8c9beSAlpana Kumari  * @param[in] cpuFunctionalState is CPU functional true/false
8757e8c9beSAlpana Kumari  *
8857e8c9beSAlpana Kumari  * @return None.
8957e8c9beSAlpana Kumari  */
901e1e598dSJonathan Doman inline void
911e1e598dSJonathan Doman     modifyCpuFunctionalState(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
921e1e598dSJonathan Doman                              bool isCpuFunctional)
9357e8c9beSAlpana Kumari {
941e1e598dSJonathan Doman     BMCWEB_LOG_DEBUG << "Cpu Functional: " << isCpuFunctional;
9557e8c9beSAlpana Kumari 
9602cad96eSEd Tanous     const nlohmann::json& prevProcState =
9757e8c9beSAlpana Kumari         aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"];
9857e8c9beSAlpana Kumari 
9957e8c9beSAlpana Kumari     // Set it as Enabled if at least one CPU is functional
10057e8c9beSAlpana Kumari     // Update STATE only if previous State was Non_Functional and current CPU is
10157e8c9beSAlpana Kumari     // Functional.
10257e8c9beSAlpana Kumari     if (prevProcState == "Disabled")
10357e8c9beSAlpana Kumari     {
104e05aec50SEd Tanous         if (isCpuFunctional)
10557e8c9beSAlpana Kumari         {
10657e8c9beSAlpana Kumari             aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
10757e8c9beSAlpana Kumari                 "Enabled";
10857e8c9beSAlpana Kumari         }
10957e8c9beSAlpana Kumari     }
11057e8c9beSAlpana Kumari }
11157e8c9beSAlpana Kumari 
112cf0e004cSNinad Palsule /*
113cf0e004cSNinad Palsule  * @brief Update "ProcessorSummary" "Count" based on Cpu PresenceState
114cf0e004cSNinad Palsule  *
115cf0e004cSNinad Palsule  * @param[in] aResp Shared pointer for completing asynchronous calls
116cf0e004cSNinad Palsule  * @param[in] cpuPresenceState CPU present or not
117cf0e004cSNinad Palsule  *
118cf0e004cSNinad Palsule  * @return None.
119cf0e004cSNinad Palsule  */
120cf0e004cSNinad Palsule inline void
121cf0e004cSNinad Palsule     modifyCpuPresenceState(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
122cf0e004cSNinad Palsule                            bool isCpuPresent)
123cf0e004cSNinad Palsule {
124cf0e004cSNinad Palsule     BMCWEB_LOG_DEBUG << "Cpu Present: " << isCpuPresent;
125cf0e004cSNinad Palsule 
126cf0e004cSNinad Palsule     if (isCpuPresent)
127cf0e004cSNinad Palsule     {
128cf0e004cSNinad Palsule         nlohmann::json& procCount =
129cf0e004cSNinad Palsule             aResp->res.jsonValue["ProcessorSummary"]["Count"];
130cf0e004cSNinad Palsule         auto* procCountPtr =
131cf0e004cSNinad Palsule             procCount.get_ptr<nlohmann::json::number_integer_t*>();
132cf0e004cSNinad Palsule         if (procCountPtr != nullptr)
133cf0e004cSNinad Palsule         {
134cf0e004cSNinad Palsule             // shouldn't be possible to be nullptr
135cf0e004cSNinad Palsule             *procCountPtr += 1;
136cf0e004cSNinad Palsule         }
137cf0e004cSNinad Palsule     }
138cf0e004cSNinad Palsule }
139cf0e004cSNinad Palsule 
140382d6475SAli Ahmed inline void getProcessorProperties(
141382d6475SAli Ahmed     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
142382d6475SAli Ahmed     const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
143382d6475SAli Ahmed         properties)
14403fbed92SAli Ahmed {
14503fbed92SAli Ahmed     BMCWEB_LOG_DEBUG << "Got " << properties.size() << " Cpu properties.";
14603fbed92SAli Ahmed 
14703fbed92SAli Ahmed     // TODO: Get Model
14803fbed92SAli Ahmed 
149bc1d29deSKrzysztof Grobelny     const uint16_t* coreCount = nullptr;
15003fbed92SAli Ahmed 
151bc1d29deSKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
152bc1d29deSKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), properties, "CoreCount", coreCount);
15303fbed92SAli Ahmed 
154bc1d29deSKrzysztof Grobelny     if (!success)
15503fbed92SAli Ahmed     {
15603fbed92SAli Ahmed         messages::internalError(aResp->res);
15703fbed92SAli Ahmed         return;
15803fbed92SAli Ahmed     }
15903fbed92SAli Ahmed 
160bc1d29deSKrzysztof Grobelny     if (coreCount != nullptr)
16103fbed92SAli Ahmed     {
162bc1d29deSKrzysztof Grobelny         nlohmann::json& coreCountJson =
163bc1d29deSKrzysztof Grobelny             aResp->res.jsonValue["ProcessorSummary"]["CoreCount"];
164bc1d29deSKrzysztof Grobelny         uint64_t* coreCountJsonPtr = coreCountJson.get_ptr<uint64_t*>();
165bc1d29deSKrzysztof Grobelny 
166bc1d29deSKrzysztof Grobelny         if (coreCountJsonPtr == nullptr)
167bc1d29deSKrzysztof Grobelny         {
168bc1d29deSKrzysztof Grobelny             coreCountJson = *coreCount;
16903fbed92SAli Ahmed         }
17003fbed92SAli Ahmed         else
17103fbed92SAli Ahmed         {
172bc1d29deSKrzysztof Grobelny             *coreCountJsonPtr += *coreCount;
17303fbed92SAli Ahmed         }
17403fbed92SAli Ahmed     }
17503fbed92SAli Ahmed }
17603fbed92SAli Ahmed 
17703fbed92SAli Ahmed /*
17803fbed92SAli Ahmed  * @brief Get ProcessorSummary fields
17903fbed92SAli Ahmed  *
18003fbed92SAli Ahmed  * @param[in] aResp Shared pointer for completing asynchronous calls
18103fbed92SAli Ahmed  * @param[in] service dbus service for Cpu Information
18203fbed92SAli Ahmed  * @param[in] path dbus path for Cpu
18303fbed92SAli Ahmed  *
18403fbed92SAli Ahmed  * @return None.
18503fbed92SAli Ahmed  */
18603fbed92SAli Ahmed inline void getProcessorSummary(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
18703fbed92SAli Ahmed                                 const std::string& service,
18803fbed92SAli Ahmed                                 const std::string& path)
18903fbed92SAli Ahmed {
1905e7e2dc5SEd Tanous     auto getCpuPresenceState = [aResp](const boost::system::error_code& ec3,
191382d6475SAli Ahmed                                        const bool cpuPresenceCheck) {
192382d6475SAli Ahmed         if (ec3)
193382d6475SAli Ahmed         {
194382d6475SAli Ahmed             BMCWEB_LOG_ERROR << "DBUS response error " << ec3;
195382d6475SAli Ahmed             return;
196382d6475SAli Ahmed         }
197382d6475SAli Ahmed         modifyCpuPresenceState(aResp, cpuPresenceCheck);
198382d6475SAli Ahmed     };
199382d6475SAli Ahmed 
200cf0e004cSNinad Palsule     // Get the Presence of CPU
201cf0e004cSNinad Palsule     sdbusplus::asio::getProperty<bool>(
202cf0e004cSNinad Palsule         *crow::connections::systemBus, service, path,
203cf0e004cSNinad Palsule         "xyz.openbmc_project.Inventory.Item", "Present",
204cf0e004cSNinad Palsule         std::move(getCpuPresenceState));
205cf0e004cSNinad Palsule 
2065fd0aafbSNinad Palsule     if constexpr (bmcwebEnableProcMemStatus)
2075fd0aafbSNinad Palsule     {
2085fd0aafbSNinad Palsule         auto getCpuFunctionalState =
2095fd0aafbSNinad Palsule             [aResp](const boost::system::error_code& ec3,
210382d6475SAli Ahmed                     const bool cpuFunctionalCheck) {
211382d6475SAli Ahmed             if (ec3)
212382d6475SAli Ahmed             {
213382d6475SAli Ahmed                 BMCWEB_LOG_ERROR << "DBUS response error " << ec3;
214382d6475SAli Ahmed                 return;
215382d6475SAli Ahmed             }
216382d6475SAli Ahmed             modifyCpuFunctionalState(aResp, cpuFunctionalCheck);
217382d6475SAli Ahmed         };
218382d6475SAli Ahmed 
219382d6475SAli Ahmed         // Get the Functional State
220382d6475SAli Ahmed         sdbusplus::asio::getProperty<bool>(
221382d6475SAli Ahmed             *crow::connections::systemBus, service, path,
2225fd0aafbSNinad Palsule             "xyz.openbmc_project.State.Decorator.OperationalStatus",
2235fd0aafbSNinad Palsule             "Functional", std::move(getCpuFunctionalState));
2245fd0aafbSNinad Palsule     }
225382d6475SAli Ahmed 
226bc1d29deSKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
227bc1d29deSKrzysztof Grobelny         *crow::connections::systemBus, service, path,
228bc1d29deSKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Item.Cpu",
22903fbed92SAli Ahmed         [aResp, service,
2305e7e2dc5SEd Tanous          path](const boost::system::error_code& ec2,
231b9d36b47SEd Tanous                const dbus::utility::DBusPropertiesMap& properties) {
23203fbed92SAli Ahmed         if (ec2)
23303fbed92SAli Ahmed         {
23403fbed92SAli Ahmed             BMCWEB_LOG_ERROR << "DBUS response error " << ec2;
23503fbed92SAli Ahmed             messages::internalError(aResp->res);
23603fbed92SAli Ahmed             return;
23703fbed92SAli Ahmed         }
238382d6475SAli Ahmed         getProcessorProperties(aResp, properties);
239bc1d29deSKrzysztof Grobelny         });
24003fbed92SAli Ahmed }
24103fbed92SAli Ahmed 
24257e8c9beSAlpana Kumari /*
243cf0e004cSNinad Palsule  * @brief processMemoryProperties fields
244cf0e004cSNinad Palsule  *
245cf0e004cSNinad Palsule  * @param[in] aResp Shared pointer for completing asynchronous calls
246cf0e004cSNinad Palsule  * @param[in] service dbus service for memory Information
247cf0e004cSNinad Palsule  * @param[in] path dbus path for Memory
248cf0e004cSNinad Palsule  * @param[in] DBUS properties for memory
249cf0e004cSNinad Palsule  *
250cf0e004cSNinad Palsule  * @return None.
251cf0e004cSNinad Palsule  */
252cf0e004cSNinad Palsule inline void
253cf0e004cSNinad Palsule     processMemoryProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2545fd0aafbSNinad Palsule                             [[maybe_unused]] const std::string& service,
2555fd0aafbSNinad Palsule                             [[maybe_unused]] const std::string& path,
256cf0e004cSNinad Palsule                             const dbus::utility::DBusPropertiesMap& properties)
257cf0e004cSNinad Palsule {
258cf0e004cSNinad Palsule     BMCWEB_LOG_DEBUG << "Got " << properties.size() << " Dimm properties.";
259cf0e004cSNinad Palsule 
260cf0e004cSNinad Palsule     if (properties.empty())
261cf0e004cSNinad Palsule     {
2625fd0aafbSNinad Palsule         if constexpr (bmcwebEnableProcMemStatus)
2635fd0aafbSNinad Palsule         {
264cf0e004cSNinad Palsule             sdbusplus::asio::getProperty<bool>(
265cf0e004cSNinad Palsule                 *crow::connections::systemBus, service, path,
266cf0e004cSNinad Palsule                 "xyz.openbmc_project.State."
267cf0e004cSNinad Palsule                 "Decorator.OperationalStatus",
268cf0e004cSNinad Palsule                 "Functional",
269cf0e004cSNinad Palsule                 [aResp](const boost::system::error_code& ec3, bool dimmState) {
270cf0e004cSNinad Palsule                 if (ec3)
271cf0e004cSNinad Palsule                 {
272cf0e004cSNinad Palsule                     BMCWEB_LOG_ERROR << "DBUS response error " << ec3;
273cf0e004cSNinad Palsule                     return;
274cf0e004cSNinad Palsule                 }
275cf0e004cSNinad Palsule                 updateDimmProperties(aResp, dimmState);
276cf0e004cSNinad Palsule                 });
2775fd0aafbSNinad Palsule         }
278cf0e004cSNinad Palsule         return;
279cf0e004cSNinad Palsule     }
280cf0e004cSNinad Palsule 
281cf0e004cSNinad Palsule     const size_t* memorySizeInKB = nullptr;
282cf0e004cSNinad Palsule 
283cf0e004cSNinad Palsule     const bool success = sdbusplus::unpackPropertiesNoThrow(
284cf0e004cSNinad Palsule         dbus_utils::UnpackErrorPrinter(), properties, "MemorySizeInKB",
285cf0e004cSNinad Palsule         memorySizeInKB);
286cf0e004cSNinad Palsule 
287cf0e004cSNinad Palsule     if (!success)
288cf0e004cSNinad Palsule     {
289cf0e004cSNinad Palsule         messages::internalError(aResp->res);
290cf0e004cSNinad Palsule         return;
291cf0e004cSNinad Palsule     }
292cf0e004cSNinad Palsule 
293cf0e004cSNinad Palsule     if (memorySizeInKB != nullptr)
294cf0e004cSNinad Palsule     {
295cf0e004cSNinad Palsule         nlohmann::json& totalMemory =
296cf0e004cSNinad Palsule             aResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"];
297cf0e004cSNinad Palsule         const uint64_t* preValue = totalMemory.get_ptr<const uint64_t*>();
298cf0e004cSNinad Palsule         if (preValue == nullptr)
299cf0e004cSNinad Palsule         {
300cf0e004cSNinad Palsule             aResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] =
301cf0e004cSNinad Palsule                 *memorySizeInKB / static_cast<size_t>(1024 * 1024);
302cf0e004cSNinad Palsule         }
303cf0e004cSNinad Palsule         else
304cf0e004cSNinad Palsule         {
305cf0e004cSNinad Palsule             aResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] =
306cf0e004cSNinad Palsule                 *memorySizeInKB / static_cast<size_t>(1024 * 1024) + *preValue;
307cf0e004cSNinad Palsule         }
3085fd0aafbSNinad Palsule         if constexpr (bmcwebEnableProcMemStatus)
3095fd0aafbSNinad Palsule         {
3105fd0aafbSNinad Palsule             aResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
3115fd0aafbSNinad Palsule                 "Enabled";
3125fd0aafbSNinad Palsule         }
313cf0e004cSNinad Palsule     }
314cf0e004cSNinad Palsule }
315cf0e004cSNinad Palsule 
316cf0e004cSNinad Palsule /*
317cf0e004cSNinad Palsule  * @brief Get getMemorySummary fields
318cf0e004cSNinad Palsule  *
319cf0e004cSNinad Palsule  * @param[in] aResp Shared pointer for completing asynchronous calls
320cf0e004cSNinad Palsule  * @param[in] service dbus service for memory Information
321cf0e004cSNinad Palsule  * @param[in] path dbus path for memory
322cf0e004cSNinad Palsule  *
323cf0e004cSNinad Palsule  * @return None.
324cf0e004cSNinad Palsule  */
325cf0e004cSNinad Palsule inline void getMemorySummary(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
326cf0e004cSNinad Palsule                              const std::string& service,
327cf0e004cSNinad Palsule                              const std::string& path)
328cf0e004cSNinad Palsule {
329cf0e004cSNinad Palsule     sdbusplus::asio::getAllProperties(
330cf0e004cSNinad Palsule         *crow::connections::systemBus, service, path,
331cf0e004cSNinad Palsule         "xyz.openbmc_project.Inventory.Item.Dimm",
332cf0e004cSNinad Palsule         [aResp, service,
333cf0e004cSNinad Palsule          path](const boost::system::error_code& ec2,
334cf0e004cSNinad Palsule                const dbus::utility::DBusPropertiesMap& properties) {
335cf0e004cSNinad Palsule         if (ec2)
336cf0e004cSNinad Palsule         {
337cf0e004cSNinad Palsule             BMCWEB_LOG_ERROR << "DBUS response error " << ec2;
338cf0e004cSNinad Palsule             messages::internalError(aResp->res);
339cf0e004cSNinad Palsule             return;
340cf0e004cSNinad Palsule         }
341cf0e004cSNinad Palsule         processMemoryProperties(aResp, service, path, properties);
342cf0e004cSNinad Palsule         });
343cf0e004cSNinad Palsule }
344cf0e004cSNinad Palsule 
345cf0e004cSNinad Palsule /*
346c5b2abe0SLewanczyk, Dawid  * @brief Retrieves computer system properties over dbus
347c5b2abe0SLewanczyk, Dawid  *
348c5b2abe0SLewanczyk, Dawid  * @param[in] aResp Shared pointer for completing asynchronous calls
3498f9ee3cdSGunnar Mills  * @param[in] systemHealth  Shared HealthPopulate pointer
350c5b2abe0SLewanczyk, Dawid  *
351c5b2abe0SLewanczyk, Dawid  * @return None.
352c5b2abe0SLewanczyk, Dawid  */
353b5a76932SEd Tanous inline void
3548d1b46d7Szhanghch05     getComputerSystem(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
355b5a76932SEd Tanous                       const std::shared_ptr<HealthPopulate>& systemHealth)
3561abe55efSEd Tanous {
35755c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get available system components.";
358e99073f5SGeorge Liu     constexpr std::array<std::string_view, 5> interfaces = {
359e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Decorator.Asset",
360e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Cpu",
361e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Dimm",
362e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.System",
363e99073f5SGeorge Liu         "xyz.openbmc_project.Common.UUID",
364e99073f5SGeorge Liu     };
365e99073f5SGeorge Liu     dbus::utility::getSubTree(
366e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
367b9d36b47SEd Tanous         [aResp,
368e99073f5SGeorge Liu          systemHealth](const boost::system::error_code& ec,
369b9d36b47SEd Tanous                        const dbus::utility::MapperGetSubTreeResponse& subtree) {
3701abe55efSEd Tanous         if (ec)
3711abe55efSEd Tanous         {
37255c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error";
373f12894f8SJason M. Bills             messages::internalError(aResp->res);
374c5b2abe0SLewanczyk, Dawid             return;
375c5b2abe0SLewanczyk, Dawid         }
376c5b2abe0SLewanczyk, Dawid         // Iterate over all retrieved ObjectPaths.
377002d39b4SEd Tanous         for (const std::pair<
378002d39b4SEd Tanous                  std::string,
379002d39b4SEd Tanous                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3801214b7e7SGunnar Mills                  object : subtree)
3811abe55efSEd Tanous         {
382c5b2abe0SLewanczyk, Dawid             const std::string& path = object.first;
38355c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Got path: " << path;
384002d39b4SEd Tanous             const std::vector<std::pair<std::string, std::vector<std::string>>>&
3851214b7e7SGunnar Mills                 connectionNames = object.second;
38626f6976fSEd Tanous             if (connectionNames.empty())
3871abe55efSEd Tanous             {
388c5b2abe0SLewanczyk, Dawid                 continue;
389c5b2abe0SLewanczyk, Dawid             }
390029573d4SEd Tanous 
3915fd0aafbSNinad Palsule             std::shared_ptr<HealthPopulate> memoryHealth = nullptr;
3925fd0aafbSNinad Palsule             std::shared_ptr<HealthPopulate> cpuHealth = nullptr;
3935bc2dc8eSJames Feist 
3945fd0aafbSNinad Palsule             if constexpr (bmcwebEnableProcMemStatus)
3955fd0aafbSNinad Palsule             {
3965fd0aafbSNinad Palsule                 memoryHealth = std::make_shared<HealthPopulate>(
3975fd0aafbSNinad Palsule                     aResp, "/MemorySummary/Status"_json_pointer);
3985fd0aafbSNinad Palsule                 systemHealth->children.emplace_back(memoryHealth);
3995bc2dc8eSJames Feist 
40013451e39SWilly Tu                 if constexpr (bmcwebEnableHealthPopulate)
40113451e39SWilly Tu                 {
4025fd0aafbSNinad Palsule                     cpuHealth = std::make_shared<HealthPopulate>(
4035fd0aafbSNinad Palsule                         aResp, "/ProcessorSummary/Status"_json_pointer);
4045fd0aafbSNinad Palsule 
4055bc2dc8eSJames Feist                     systemHealth->children.emplace_back(cpuHealth);
40613451e39SWilly Tu                 }
4075fd0aafbSNinad Palsule             }
4085bc2dc8eSJames Feist 
4096c34de48SEd Tanous             // This is not system, so check if it's cpu, dimm, UUID or
4106c34de48SEd Tanous             // BiosVer
41104a258f4SEd Tanous             for (const auto& connection : connectionNames)
4121abe55efSEd Tanous             {
41304a258f4SEd Tanous                 for (const auto& interfaceName : connection.second)
4141abe55efSEd Tanous                 {
41504a258f4SEd Tanous                     if (interfaceName ==
41604a258f4SEd Tanous                         "xyz.openbmc_project.Inventory.Item.Dimm")
4171abe55efSEd Tanous                     {
4181abe55efSEd Tanous                         BMCWEB_LOG_DEBUG
41904a258f4SEd Tanous                             << "Found Dimm, now get its properties.";
4209d3ae10eSAlpana Kumari 
421cf0e004cSNinad Palsule                         getMemorySummary(aResp, connection.first, path);
4225bc2dc8eSJames Feist 
4235fd0aafbSNinad Palsule                         if constexpr (bmcwebEnableProcMemStatus)
4245fd0aafbSNinad Palsule                         {
4255bc2dc8eSJames Feist                             memoryHealth->inventory.emplace_back(path);
4261abe55efSEd Tanous                         }
4275fd0aafbSNinad Palsule                     }
42804a258f4SEd Tanous                     else if (interfaceName ==
42904a258f4SEd Tanous                              "xyz.openbmc_project.Inventory.Item.Cpu")
4301abe55efSEd Tanous                     {
4311abe55efSEd Tanous                         BMCWEB_LOG_DEBUG
43204a258f4SEd Tanous                             << "Found Cpu, now get its properties.";
43357e8c9beSAlpana Kumari 
43403fbed92SAli Ahmed                         getProcessorSummary(aResp, connection.first, path);
4355bc2dc8eSJames Feist 
4365fd0aafbSNinad Palsule                         if constexpr (bmcwebEnableProcMemStatus)
4375fd0aafbSNinad Palsule                         {
4385bc2dc8eSJames Feist                             cpuHealth->inventory.emplace_back(path);
4391abe55efSEd Tanous                         }
4405fd0aafbSNinad Palsule                     }
441002d39b4SEd Tanous                     else if (interfaceName == "xyz.openbmc_project.Common.UUID")
4421abe55efSEd Tanous                     {
4431abe55efSEd Tanous                         BMCWEB_LOG_DEBUG
44404a258f4SEd Tanous                             << "Found UUID, now get its properties.";
445bc1d29deSKrzysztof Grobelny 
446bc1d29deSKrzysztof Grobelny                         sdbusplus::asio::getAllProperties(
447bc1d29deSKrzysztof Grobelny                             *crow::connections::systemBus, connection.first,
448bc1d29deSKrzysztof Grobelny                             path, "xyz.openbmc_project.Common.UUID",
4495e7e2dc5SEd Tanous                             [aResp](const boost::system::error_code& ec3,
450b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
4511214b7e7SGunnar Mills                                         properties) {
452cb13a392SEd Tanous                             if (ec3)
4531abe55efSEd Tanous                             {
454002d39b4SEd Tanous                                 BMCWEB_LOG_DEBUG << "DBUS response error "
455002d39b4SEd Tanous                                                  << ec3;
456f12894f8SJason M. Bills                                 messages::internalError(aResp->res);
457c5b2abe0SLewanczyk, Dawid                                 return;
458c5b2abe0SLewanczyk, Dawid                             }
459002d39b4SEd Tanous                             BMCWEB_LOG_DEBUG << "Got " << properties.size()
460c5b2abe0SLewanczyk, Dawid                                              << " UUID properties.";
46104a258f4SEd Tanous 
462bc1d29deSKrzysztof Grobelny                             const std::string* uUID = nullptr;
463bc1d29deSKrzysztof Grobelny 
464bc1d29deSKrzysztof Grobelny                             const bool success =
465bc1d29deSKrzysztof Grobelny                                 sdbusplus::unpackPropertiesNoThrow(
466bc1d29deSKrzysztof Grobelny                                     dbus_utils::UnpackErrorPrinter(),
467bc1d29deSKrzysztof Grobelny                                     properties, "UUID", uUID);
468bc1d29deSKrzysztof Grobelny 
469bc1d29deSKrzysztof Grobelny                             if (!success)
4701abe55efSEd Tanous                             {
471bc1d29deSKrzysztof Grobelny                                 messages::internalError(aResp->res);
472bc1d29deSKrzysztof Grobelny                                 return;
473bc1d29deSKrzysztof Grobelny                             }
474bc1d29deSKrzysztof Grobelny 
475bc1d29deSKrzysztof Grobelny                             if (uUID != nullptr)
476bc1d29deSKrzysztof Grobelny                             {
477bc1d29deSKrzysztof Grobelny                                 std::string valueStr = *uUID;
47804a258f4SEd Tanous                                 if (valueStr.size() == 32)
4791abe55efSEd Tanous                                 {
480029573d4SEd Tanous                                     valueStr.insert(8, 1, '-');
481029573d4SEd Tanous                                     valueStr.insert(13, 1, '-');
482029573d4SEd Tanous                                     valueStr.insert(18, 1, '-');
483029573d4SEd Tanous                                     valueStr.insert(23, 1, '-');
48404a258f4SEd Tanous                                 }
485bc1d29deSKrzysztof Grobelny                                 BMCWEB_LOG_DEBUG << "UUID = " << valueStr;
486002d39b4SEd Tanous                                 aResp->res.jsonValue["UUID"] = valueStr;
487c5b2abe0SLewanczyk, Dawid                             }
488bc1d29deSKrzysztof Grobelny                             });
489c5b2abe0SLewanczyk, Dawid                     }
490029573d4SEd Tanous                     else if (interfaceName ==
491029573d4SEd Tanous                              "xyz.openbmc_project.Inventory.Item.System")
4921abe55efSEd Tanous                     {
493bc1d29deSKrzysztof Grobelny                         sdbusplus::asio::getAllProperties(
494bc1d29deSKrzysztof Grobelny                             *crow::connections::systemBus, connection.first,
495bc1d29deSKrzysztof Grobelny                             path,
496bc1d29deSKrzysztof Grobelny                             "xyz.openbmc_project.Inventory.Decorator.Asset",
4975e7e2dc5SEd Tanous                             [aResp](const boost::system::error_code& ec2,
498b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
4991214b7e7SGunnar Mills                                         propertiesList) {
500cb13a392SEd Tanous                             if (ec2)
501029573d4SEd Tanous                             {
502e4a4b9a9SJames Feist                                 // doesn't have to include this
503e4a4b9a9SJames Feist                                 // interface
504029573d4SEd Tanous                                 return;
505029573d4SEd Tanous                             }
506002d39b4SEd Tanous                             BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
507029573d4SEd Tanous                                              << " properties for system";
508bc1d29deSKrzysztof Grobelny 
509bc1d29deSKrzysztof Grobelny                             const std::string* partNumber = nullptr;
510bc1d29deSKrzysztof Grobelny                             const std::string* serialNumber = nullptr;
511bc1d29deSKrzysztof Grobelny                             const std::string* manufacturer = nullptr;
512bc1d29deSKrzysztof Grobelny                             const std::string* model = nullptr;
513bc1d29deSKrzysztof Grobelny                             const std::string* subModel = nullptr;
514bc1d29deSKrzysztof Grobelny 
515bc1d29deSKrzysztof Grobelny                             const bool success =
516bc1d29deSKrzysztof Grobelny                                 sdbusplus::unpackPropertiesNoThrow(
517bc1d29deSKrzysztof Grobelny                                     dbus_utils::UnpackErrorPrinter(),
518bc1d29deSKrzysztof Grobelny                                     propertiesList, "PartNumber", partNumber,
519bc1d29deSKrzysztof Grobelny                                     "SerialNumber", serialNumber,
520bc1d29deSKrzysztof Grobelny                                     "Manufacturer", manufacturer, "Model",
521bc1d29deSKrzysztof Grobelny                                     model, "SubModel", subModel);
522bc1d29deSKrzysztof Grobelny 
523bc1d29deSKrzysztof Grobelny                             if (!success)
524029573d4SEd Tanous                             {
525bc1d29deSKrzysztof Grobelny                                 messages::internalError(aResp->res);
526bc1d29deSKrzysztof Grobelny                                 return;
527029573d4SEd Tanous                             }
528bc1d29deSKrzysztof Grobelny 
529bc1d29deSKrzysztof Grobelny                             if (partNumber != nullptr)
530bc1d29deSKrzysztof Grobelny                             {
531bc1d29deSKrzysztof Grobelny                                 aResp->res.jsonValue["PartNumber"] =
532bc1d29deSKrzysztof Grobelny                                     *partNumber;
533029573d4SEd Tanous                             }
534bc1d29deSKrzysztof Grobelny 
535bc1d29deSKrzysztof Grobelny                             if (serialNumber != nullptr)
536bc1d29deSKrzysztof Grobelny                             {
537bc1d29deSKrzysztof Grobelny                                 aResp->res.jsonValue["SerialNumber"] =
538bc1d29deSKrzysztof Grobelny                                     *serialNumber;
539bc1d29deSKrzysztof Grobelny                             }
540bc1d29deSKrzysztof Grobelny 
541bc1d29deSKrzysztof Grobelny                             if (manufacturer != nullptr)
542bc1d29deSKrzysztof Grobelny                             {
543bc1d29deSKrzysztof Grobelny                                 aResp->res.jsonValue["Manufacturer"] =
544bc1d29deSKrzysztof Grobelny                                     *manufacturer;
545bc1d29deSKrzysztof Grobelny                             }
546bc1d29deSKrzysztof Grobelny 
547bc1d29deSKrzysztof Grobelny                             if (model != nullptr)
548bc1d29deSKrzysztof Grobelny                             {
549bc1d29deSKrzysztof Grobelny                                 aResp->res.jsonValue["Model"] = *model;
550bc1d29deSKrzysztof Grobelny                             }
551bc1d29deSKrzysztof Grobelny 
552bc1d29deSKrzysztof Grobelny                             if (subModel != nullptr)
553bc1d29deSKrzysztof Grobelny                             {
554bc1d29deSKrzysztof Grobelny                                 aResp->res.jsonValue["SubModel"] = *subModel;
555fc5afcf9Sbeccabroek                             }
556c1e236a6SGunnar Mills 
557cb7e1e7bSAndrew Geissler                             // Grab the bios version
558eee0013eSWilly Tu                             sw_util::populateSoftwareInformation(
559eee0013eSWilly Tu                                 aResp, sw_util::biosPurpose, "BiosVersion",
560002d39b4SEd Tanous                                 false);
561bc1d29deSKrzysztof Grobelny                             });
562e4a4b9a9SJames Feist 
5631e1e598dSJonathan Doman                         sdbusplus::asio::getProperty<std::string>(
5641e1e598dSJonathan Doman                             *crow::connections::systemBus, connection.first,
5651e1e598dSJonathan Doman                             path,
5661e1e598dSJonathan Doman                             "xyz.openbmc_project.Inventory.Decorator."
5671e1e598dSJonathan Doman                             "AssetTag",
5681e1e598dSJonathan Doman                             "AssetTag",
5695e7e2dc5SEd Tanous                             [aResp](const boost::system::error_code& ec2,
5701e1e598dSJonathan Doman                                     const std::string& value) {
571cb13a392SEd Tanous                             if (ec2)
572e4a4b9a9SJames Feist                             {
573e4a4b9a9SJames Feist                                 // doesn't have to include this
574e4a4b9a9SJames Feist                                 // interface
575e4a4b9a9SJames Feist                                 return;
576e4a4b9a9SJames Feist                             }
577e4a4b9a9SJames Feist 
5781e1e598dSJonathan Doman                             aResp->res.jsonValue["AssetTag"] = value;
5791e1e598dSJonathan Doman                             });
580029573d4SEd Tanous                     }
581029573d4SEd Tanous                 }
582029573d4SEd Tanous             }
583c5b2abe0SLewanczyk, Dawid         }
5846617338dSEd Tanous         });
585c5b2abe0SLewanczyk, Dawid }
586c5b2abe0SLewanczyk, Dawid 
587c5b2abe0SLewanczyk, Dawid /**
588c5b2abe0SLewanczyk, Dawid  * @brief Retrieves host state properties over dbus
589c5b2abe0SLewanczyk, Dawid  *
590c5b2abe0SLewanczyk, Dawid  * @param[in] aResp     Shared pointer for completing asynchronous calls.
591c5b2abe0SLewanczyk, Dawid  *
592c5b2abe0SLewanczyk, Dawid  * @return None.
593c5b2abe0SLewanczyk, Dawid  */
5948d1b46d7Szhanghch05 inline void getHostState(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
5951abe55efSEd Tanous {
59655c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get host information.";
5971e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
5981e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
5991e1e598dSJonathan Doman         "/xyz/openbmc_project/state/host0", "xyz.openbmc_project.State.Host",
6001e1e598dSJonathan Doman         "CurrentHostState",
6015e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec,
6021e1e598dSJonathan Doman                 const std::string& hostState) {
6031abe55efSEd Tanous         if (ec)
6041abe55efSEd Tanous         {
60522228c28SAndrew Geissler             if (ec == boost::system::errc::host_unreachable)
60622228c28SAndrew Geissler             {
60722228c28SAndrew Geissler                 // Service not available, no error, just don't return
60822228c28SAndrew Geissler                 // host state info
60922228c28SAndrew Geissler                 BMCWEB_LOG_DEBUG << "Service not available " << ec;
61022228c28SAndrew Geissler                 return;
61122228c28SAndrew Geissler             }
61222228c28SAndrew Geissler             BMCWEB_LOG_ERROR << "DBUS response error " << ec;
613f12894f8SJason M. Bills             messages::internalError(aResp->res);
614c5b2abe0SLewanczyk, Dawid             return;
615c5b2abe0SLewanczyk, Dawid         }
6166617338dSEd Tanous 
6171e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Host state: " << hostState;
618c5b2abe0SLewanczyk, Dawid         // Verify Host State
6191e1e598dSJonathan Doman         if (hostState == "xyz.openbmc_project.State.Host.HostState.Running")
6201abe55efSEd Tanous         {
62155c7b7a2SEd Tanous             aResp->res.jsonValue["PowerState"] = "On";
6226617338dSEd Tanous             aResp->res.jsonValue["Status"]["State"] = "Enabled";
6231abe55efSEd Tanous         }
6241e1e598dSJonathan Doman         else if (hostState ==
6250fda0f12SGeorge Liu                  "xyz.openbmc_project.State.Host.HostState.Quiesced")
6268c888608SGunnar Mills         {
6278c888608SGunnar Mills             aResp->res.jsonValue["PowerState"] = "On";
6288c888608SGunnar Mills             aResp->res.jsonValue["Status"]["State"] = "Quiesced";
6298c888608SGunnar Mills         }
6301e1e598dSJonathan Doman         else if (hostState ==
6310fda0f12SGeorge Liu                  "xyz.openbmc_project.State.Host.HostState.DiagnosticMode")
63283935af9SAndrew Geissler         {
63383935af9SAndrew Geissler             aResp->res.jsonValue["PowerState"] = "On";
63483935af9SAndrew Geissler             aResp->res.jsonValue["Status"]["State"] = "InTest";
63583935af9SAndrew Geissler         }
6360fda0f12SGeorge Liu         else if (
6371e1e598dSJonathan Doman             hostState ==
6380fda0f12SGeorge Liu             "xyz.openbmc_project.State.Host.HostState.TransitioningToRunning")
6391a2a1437SAndrew Geissler         {
6401a2a1437SAndrew Geissler             aResp->res.jsonValue["PowerState"] = "PoweringOn";
64115c27bf8SNoah Brewer             aResp->res.jsonValue["Status"]["State"] = "Starting";
6421a2a1437SAndrew Geissler         }
643002d39b4SEd Tanous         else if (hostState ==
6440fda0f12SGeorge Liu                  "xyz.openbmc_project.State.Host.HostState.TransitioningToOff")
6451a2a1437SAndrew Geissler         {
6461a2a1437SAndrew Geissler             aResp->res.jsonValue["PowerState"] = "PoweringOff";
6471a2a1437SAndrew Geissler             aResp->res.jsonValue["Status"]["State"] = "Disabled";
6481a2a1437SAndrew Geissler         }
6491abe55efSEd Tanous         else
6501abe55efSEd Tanous         {
65155c7b7a2SEd Tanous             aResp->res.jsonValue["PowerState"] = "Off";
6526617338dSEd Tanous             aResp->res.jsonValue["Status"]["State"] = "Disabled";
653c5b2abe0SLewanczyk, Dawid         }
6541e1e598dSJonathan Doman         });
655c5b2abe0SLewanczyk, Dawid }
656c5b2abe0SLewanczyk, Dawid 
657c5b2abe0SLewanczyk, Dawid /**
658786d0f60SGunnar Mills  * @brief Translates boot source DBUS property value to redfish.
659491d8ee7SSantosh Puranik  *
660491d8ee7SSantosh Puranik  * @param[in] dbusSource    The boot source in DBUS speak.
661491d8ee7SSantosh Puranik  *
662491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source in Redfish terms. If translation
663491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
664491d8ee7SSantosh Puranik  */
66523a21a1cSEd Tanous inline std::string dbusToRfBootSource(const std::string& dbusSource)
666491d8ee7SSantosh Puranik {
667491d8ee7SSantosh Puranik     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
668491d8ee7SSantosh Puranik     {
669491d8ee7SSantosh Puranik         return "None";
670491d8ee7SSantosh Puranik     }
6713174e4dfSEd Tanous     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Disk")
672491d8ee7SSantosh Puranik     {
673491d8ee7SSantosh Puranik         return "Hdd";
674491d8ee7SSantosh Puranik     }
6753174e4dfSEd Tanous     if (dbusSource ==
676a71dc0b7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia")
677491d8ee7SSantosh Puranik     {
678491d8ee7SSantosh Puranik         return "Cd";
679491d8ee7SSantosh Puranik     }
6803174e4dfSEd Tanous     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Network")
681491d8ee7SSantosh Puranik     {
682491d8ee7SSantosh Puranik         return "Pxe";
683491d8ee7SSantosh Puranik     }
6843174e4dfSEd Tanous     if (dbusSource ==
685944ffaf9SJohnathan Mantey         "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia")
6869f16b2c1SJennifer Lee     {
6879f16b2c1SJennifer Lee         return "Usb";
6889f16b2c1SJennifer Lee     }
689491d8ee7SSantosh Puranik     return "";
690491d8ee7SSantosh Puranik }
691491d8ee7SSantosh Puranik 
692491d8ee7SSantosh Puranik /**
693cd9a4666SKonstantin Aladyshev  * @brief Translates boot type DBUS property value to redfish.
694cd9a4666SKonstantin Aladyshev  *
695cd9a4666SKonstantin Aladyshev  * @param[in] dbusType    The boot type in DBUS speak.
696cd9a4666SKonstantin Aladyshev  *
697cd9a4666SKonstantin Aladyshev  * @return Returns as a string, the boot type in Redfish terms. If translation
698cd9a4666SKonstantin Aladyshev  * cannot be done, returns an empty string.
699cd9a4666SKonstantin Aladyshev  */
700cd9a4666SKonstantin Aladyshev inline std::string dbusToRfBootType(const std::string& dbusType)
701cd9a4666SKonstantin Aladyshev {
702cd9a4666SKonstantin Aladyshev     if (dbusType == "xyz.openbmc_project.Control.Boot.Type.Types.Legacy")
703cd9a4666SKonstantin Aladyshev     {
704cd9a4666SKonstantin Aladyshev         return "Legacy";
705cd9a4666SKonstantin Aladyshev     }
706cd9a4666SKonstantin Aladyshev     if (dbusType == "xyz.openbmc_project.Control.Boot.Type.Types.EFI")
707cd9a4666SKonstantin Aladyshev     {
708cd9a4666SKonstantin Aladyshev         return "UEFI";
709cd9a4666SKonstantin Aladyshev     }
710cd9a4666SKonstantin Aladyshev     return "";
711cd9a4666SKonstantin Aladyshev }
712cd9a4666SKonstantin Aladyshev 
713cd9a4666SKonstantin Aladyshev /**
714786d0f60SGunnar Mills  * @brief Translates boot mode DBUS property value to redfish.
715491d8ee7SSantosh Puranik  *
716491d8ee7SSantosh Puranik  * @param[in] dbusMode    The boot mode in DBUS speak.
717491d8ee7SSantosh Puranik  *
718491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode in Redfish terms. If translation
719491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
720491d8ee7SSantosh Puranik  */
72123a21a1cSEd Tanous inline std::string dbusToRfBootMode(const std::string& dbusMode)
722491d8ee7SSantosh Puranik {
723491d8ee7SSantosh Puranik     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
724491d8ee7SSantosh Puranik     {
725491d8ee7SSantosh Puranik         return "None";
726491d8ee7SSantosh Puranik     }
7273174e4dfSEd Tanous     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe")
728491d8ee7SSantosh Puranik     {
729491d8ee7SSantosh Puranik         return "Diags";
730491d8ee7SSantosh Puranik     }
7313174e4dfSEd Tanous     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup")
732491d8ee7SSantosh Puranik     {
733491d8ee7SSantosh Puranik         return "BiosSetup";
734491d8ee7SSantosh Puranik     }
735491d8ee7SSantosh Puranik     return "";
736491d8ee7SSantosh Puranik }
737491d8ee7SSantosh Puranik 
738491d8ee7SSantosh Puranik /**
739e43914b3SAndrew Geissler  * @brief Translates boot progress DBUS property value to redfish.
740e43914b3SAndrew Geissler  *
741e43914b3SAndrew Geissler  * @param[in] dbusBootProgress    The boot progress in DBUS speak.
742e43914b3SAndrew Geissler  *
743e43914b3SAndrew Geissler  * @return Returns as a string, the boot progress in Redfish terms. If
744e43914b3SAndrew Geissler  *         translation cannot be done, returns "None".
745e43914b3SAndrew Geissler  */
746e43914b3SAndrew Geissler inline std::string dbusToRfBootProgress(const std::string& dbusBootProgress)
747e43914b3SAndrew Geissler {
748e43914b3SAndrew Geissler     // Now convert the D-Bus BootProgress to the appropriate Redfish
749e43914b3SAndrew Geissler     // enum
750e43914b3SAndrew Geissler     std::string rfBpLastState = "None";
751e43914b3SAndrew Geissler     if (dbusBootProgress == "xyz.openbmc_project.State.Boot.Progress."
752e43914b3SAndrew Geissler                             "ProgressStages.Unspecified")
753e43914b3SAndrew Geissler     {
754e43914b3SAndrew Geissler         rfBpLastState = "None";
755e43914b3SAndrew Geissler     }
756e43914b3SAndrew Geissler     else if (dbusBootProgress ==
757e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
758e43914b3SAndrew Geissler              "PrimaryProcInit")
759e43914b3SAndrew Geissler     {
760e43914b3SAndrew Geissler         rfBpLastState = "PrimaryProcessorInitializationStarted";
761e43914b3SAndrew Geissler     }
762e43914b3SAndrew Geissler     else if (dbusBootProgress ==
763e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
764e43914b3SAndrew Geissler              "BusInit")
765e43914b3SAndrew Geissler     {
766e43914b3SAndrew Geissler         rfBpLastState = "BusInitializationStarted";
767e43914b3SAndrew Geissler     }
768e43914b3SAndrew Geissler     else if (dbusBootProgress ==
769e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
770e43914b3SAndrew Geissler              "MemoryInit")
771e43914b3SAndrew Geissler     {
772e43914b3SAndrew Geissler         rfBpLastState = "MemoryInitializationStarted";
773e43914b3SAndrew Geissler     }
774e43914b3SAndrew Geissler     else if (dbusBootProgress ==
775e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
776e43914b3SAndrew Geissler              "SecondaryProcInit")
777e43914b3SAndrew Geissler     {
778e43914b3SAndrew Geissler         rfBpLastState = "SecondaryProcessorInitializationStarted";
779e43914b3SAndrew Geissler     }
780e43914b3SAndrew Geissler     else if (dbusBootProgress ==
781e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
782e43914b3SAndrew Geissler              "PCIInit")
783e43914b3SAndrew Geissler     {
784e43914b3SAndrew Geissler         rfBpLastState = "PCIResourceConfigStarted";
785e43914b3SAndrew Geissler     }
786e43914b3SAndrew Geissler     else if (dbusBootProgress ==
787e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
788e43914b3SAndrew Geissler              "SystemSetup")
789e43914b3SAndrew Geissler     {
790e43914b3SAndrew Geissler         rfBpLastState = "SetupEntered";
791e43914b3SAndrew Geissler     }
792e43914b3SAndrew Geissler     else if (dbusBootProgress ==
793e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
794e43914b3SAndrew Geissler              "SystemInitComplete")
795e43914b3SAndrew Geissler     {
796e43914b3SAndrew Geissler         rfBpLastState = "SystemHardwareInitializationComplete";
797e43914b3SAndrew Geissler     }
798e43914b3SAndrew Geissler     else if (dbusBootProgress ==
799e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
800e43914b3SAndrew Geissler              "OSStart")
801e43914b3SAndrew Geissler     {
802e43914b3SAndrew Geissler         rfBpLastState = "OSBootStarted";
803e43914b3SAndrew Geissler     }
804e43914b3SAndrew Geissler     else if (dbusBootProgress ==
805e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
806e43914b3SAndrew Geissler              "OSRunning")
807e43914b3SAndrew Geissler     {
808e43914b3SAndrew Geissler         rfBpLastState = "OSRunning";
809e43914b3SAndrew Geissler     }
810e43914b3SAndrew Geissler     else
811e43914b3SAndrew Geissler     {
812e43914b3SAndrew Geissler         BMCWEB_LOG_DEBUG << "Unsupported D-Bus BootProgress "
813e43914b3SAndrew Geissler                          << dbusBootProgress;
814e43914b3SAndrew Geissler         // Just return the default
815e43914b3SAndrew Geissler     }
816e43914b3SAndrew Geissler     return rfBpLastState;
817e43914b3SAndrew Geissler }
818e43914b3SAndrew Geissler 
819e43914b3SAndrew Geissler /**
820786d0f60SGunnar Mills  * @brief Translates boot source from Redfish to the DBus boot paths.
821491d8ee7SSantosh Puranik  *
822491d8ee7SSantosh Puranik  * @param[in] rfSource    The boot source in Redfish.
823944ffaf9SJohnathan Mantey  * @param[out] bootSource The DBus source
824944ffaf9SJohnathan Mantey  * @param[out] bootMode   the DBus boot mode
825491d8ee7SSantosh Puranik  *
826944ffaf9SJohnathan Mantey  * @return Integer error code.
827491d8ee7SSantosh Puranik  */
8288d1b46d7Szhanghch05 inline int assignBootParameters(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
829944ffaf9SJohnathan Mantey                                 const std::string& rfSource,
830944ffaf9SJohnathan Mantey                                 std::string& bootSource, std::string& bootMode)
831491d8ee7SSantosh Puranik {
832c21865c4SKonstantin Aladyshev     bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
833c21865c4SKonstantin Aladyshev     bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
834944ffaf9SJohnathan Mantey 
835491d8ee7SSantosh Puranik     if (rfSource == "None")
836491d8ee7SSantosh Puranik     {
837944ffaf9SJohnathan Mantey         return 0;
838491d8ee7SSantosh Puranik     }
8393174e4dfSEd Tanous     if (rfSource == "Pxe")
840491d8ee7SSantosh Puranik     {
841944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Network";
842944ffaf9SJohnathan Mantey     }
843944ffaf9SJohnathan Mantey     else if (rfSource == "Hdd")
844944ffaf9SJohnathan Mantey     {
845944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Disk";
846944ffaf9SJohnathan Mantey     }
847944ffaf9SJohnathan Mantey     else if (rfSource == "Diags")
848944ffaf9SJohnathan Mantey     {
849944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe";
850944ffaf9SJohnathan Mantey     }
851944ffaf9SJohnathan Mantey     else if (rfSource == "Cd")
852944ffaf9SJohnathan Mantey     {
853944ffaf9SJohnathan Mantey         bootSource =
854944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia";
855944ffaf9SJohnathan Mantey     }
856944ffaf9SJohnathan Mantey     else if (rfSource == "BiosSetup")
857944ffaf9SJohnathan Mantey     {
858944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup";
859491d8ee7SSantosh Puranik     }
8609f16b2c1SJennifer Lee     else if (rfSource == "Usb")
8619f16b2c1SJennifer Lee     {
862944ffaf9SJohnathan Mantey         bootSource =
863944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia";
8649f16b2c1SJennifer Lee     }
865491d8ee7SSantosh Puranik     else
866491d8ee7SSantosh Puranik     {
8670fda0f12SGeorge Liu         BMCWEB_LOG_DEBUG
8680fda0f12SGeorge Liu             << "Invalid property value for BootSourceOverrideTarget: "
869944ffaf9SJohnathan Mantey             << bootSource;
870944ffaf9SJohnathan Mantey         messages::propertyValueNotInList(aResp->res, rfSource,
871944ffaf9SJohnathan Mantey                                          "BootSourceTargetOverride");
872944ffaf9SJohnathan Mantey         return -1;
873491d8ee7SSantosh Puranik     }
874944ffaf9SJohnathan Mantey     return 0;
875491d8ee7SSantosh Puranik }
8761981771bSAli Ahmed 
877978b8803SAndrew Geissler /**
878978b8803SAndrew Geissler  * @brief Retrieves boot progress of the system
879978b8803SAndrew Geissler  *
880978b8803SAndrew Geissler  * @param[in] aResp  Shared pointer for generating response message.
881978b8803SAndrew Geissler  *
882978b8803SAndrew Geissler  * @return None.
883978b8803SAndrew Geissler  */
8848d1b46d7Szhanghch05 inline void getBootProgress(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
885978b8803SAndrew Geissler {
8861e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
8871e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
8881e1e598dSJonathan Doman         "/xyz/openbmc_project/state/host0",
8891e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.Progress", "BootProgress",
8905e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec,
8911e1e598dSJonathan Doman                 const std::string& bootProgressStr) {
892978b8803SAndrew Geissler         if (ec)
893978b8803SAndrew Geissler         {
894978b8803SAndrew Geissler             // BootProgress is an optional object so just do nothing if
895978b8803SAndrew Geissler             // not found
896978b8803SAndrew Geissler             return;
897978b8803SAndrew Geissler         }
898978b8803SAndrew Geissler 
8991e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Boot Progress: " << bootProgressStr;
900978b8803SAndrew Geissler 
901e43914b3SAndrew Geissler         aResp->res.jsonValue["BootProgress"]["LastState"] =
902e43914b3SAndrew Geissler             dbusToRfBootProgress(bootProgressStr);
9031e1e598dSJonathan Doman         });
904978b8803SAndrew Geissler }
905491d8ee7SSantosh Puranik 
906491d8ee7SSantosh Puranik /**
907b6d5d45cSHieu Huynh  * @brief Retrieves boot progress Last Update of the system
908b6d5d45cSHieu Huynh  *
909b6d5d45cSHieu Huynh  * @param[in] aResp  Shared pointer for generating response message.
910b6d5d45cSHieu Huynh  *
911b6d5d45cSHieu Huynh  * @return None.
912b6d5d45cSHieu Huynh  */
913b6d5d45cSHieu Huynh inline void getBootProgressLastStateTime(
914b6d5d45cSHieu Huynh     const std::shared_ptr<bmcweb::AsyncResp>& aResp)
915b6d5d45cSHieu Huynh {
916b6d5d45cSHieu Huynh     sdbusplus::asio::getProperty<uint64_t>(
917b6d5d45cSHieu Huynh         *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
918b6d5d45cSHieu Huynh         "/xyz/openbmc_project/state/host0",
919b6d5d45cSHieu Huynh         "xyz.openbmc_project.State.Boot.Progress", "BootProgressLastUpdate",
9205e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec,
921b6d5d45cSHieu Huynh                 const uint64_t lastStateTime) {
922b6d5d45cSHieu Huynh         if (ec)
923b6d5d45cSHieu Huynh         {
924b6d5d45cSHieu Huynh             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
925b6d5d45cSHieu Huynh             return;
926b6d5d45cSHieu Huynh         }
927b6d5d45cSHieu Huynh 
928b6d5d45cSHieu Huynh         // BootProgressLastUpdate is the last time the BootProgress property
929b6d5d45cSHieu Huynh         // was updated. The time is the Epoch time, number of microseconds
930b6d5d45cSHieu Huynh         // since 1 Jan 1970 00::00::00 UTC."
931b6d5d45cSHieu Huynh         // https://github.com/openbmc/phosphor-dbus-interfaces/blob/master/
932b6d5d45cSHieu Huynh         // yaml/xyz/openbmc_project/State/Boot/Progress.interface.yaml#L11
933b6d5d45cSHieu Huynh 
934b6d5d45cSHieu Huynh         // Convert to ISO 8601 standard
935b6d5d45cSHieu Huynh         aResp->res.jsonValue["BootProgress"]["LastStateTime"] =
936b6d5d45cSHieu Huynh             redfish::time_utils::getDateTimeUintUs(lastStateTime);
937b6d5d45cSHieu Huynh         });
938b6d5d45cSHieu Huynh }
939b6d5d45cSHieu Huynh 
940b6d5d45cSHieu Huynh /**
941c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override type over DBUS and fills out the response
942cd9a4666SKonstantin Aladyshev  *
943cd9a4666SKonstantin Aladyshev  * @param[in] aResp         Shared pointer for generating response message.
944cd9a4666SKonstantin Aladyshev  *
945cd9a4666SKonstantin Aladyshev  * @return None.
946cd9a4666SKonstantin Aladyshev  */
947cd9a4666SKonstantin Aladyshev 
948c21865c4SKonstantin Aladyshev inline void getBootOverrideType(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
949cd9a4666SKonstantin Aladyshev {
9501e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
9511e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
9521e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
9531e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Type", "BootType",
9545e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec,
9551e1e598dSJonathan Doman                 const std::string& bootType) {
956cd9a4666SKonstantin Aladyshev         if (ec)
957cd9a4666SKonstantin Aladyshev         {
958cd9a4666SKonstantin Aladyshev             // not an error, don't have to have the interface
959cd9a4666SKonstantin Aladyshev             return;
960cd9a4666SKonstantin Aladyshev         }
961cd9a4666SKonstantin Aladyshev 
9621e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Boot type: " << bootType;
963cd9a4666SKonstantin Aladyshev 
964002d39b4SEd Tanous         aResp->res.jsonValue["Boot"]
965002d39b4SEd Tanous                             ["BootSourceOverrideMode@Redfish.AllowableValues"] =
966613dabeaSEd Tanous             nlohmann::json::array_t({"Legacy", "UEFI"});
967cd9a4666SKonstantin Aladyshev 
9681e1e598dSJonathan Doman         auto rfType = dbusToRfBootType(bootType);
969cd9a4666SKonstantin Aladyshev         if (rfType.empty())
970cd9a4666SKonstantin Aladyshev         {
971cd9a4666SKonstantin Aladyshev             messages::internalError(aResp->res);
972cd9a4666SKonstantin Aladyshev             return;
973cd9a4666SKonstantin Aladyshev         }
974cd9a4666SKonstantin Aladyshev 
975cd9a4666SKonstantin Aladyshev         aResp->res.jsonValue["Boot"]["BootSourceOverrideMode"] = rfType;
9761e1e598dSJonathan Doman         });
977cd9a4666SKonstantin Aladyshev }
978cd9a4666SKonstantin Aladyshev 
979cd9a4666SKonstantin Aladyshev /**
980c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override mode over DBUS and fills out the response
981491d8ee7SSantosh Puranik  *
982491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
983491d8ee7SSantosh Puranik  *
984491d8ee7SSantosh Puranik  * @return None.
985491d8ee7SSantosh Puranik  */
986c21865c4SKonstantin Aladyshev 
987c21865c4SKonstantin Aladyshev inline void getBootOverrideMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
988491d8ee7SSantosh Puranik {
9891e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
9901e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
9911e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
9921e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
9935e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec,
9941e1e598dSJonathan Doman                 const std::string& bootModeStr) {
995491d8ee7SSantosh Puranik         if (ec)
996491d8ee7SSantosh Puranik         {
997491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
998491d8ee7SSantosh Puranik             messages::internalError(aResp->res);
999491d8ee7SSantosh Puranik             return;
1000491d8ee7SSantosh Puranik         }
1001491d8ee7SSantosh Puranik 
10021e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Boot mode: " << bootModeStr;
1003491d8ee7SSantosh Puranik 
10040fda0f12SGeorge Liu         aResp->res
10050fda0f12SGeorge Liu             .jsonValue["Boot"]
1006002d39b4SEd Tanous                       ["BootSourceOverrideTarget@Redfish.AllowableValues"] = {
1007002d39b4SEd Tanous             "None", "Pxe", "Hdd", "Cd", "Diags", "BiosSetup", "Usb"};
1008491d8ee7SSantosh Puranik 
10091e1e598dSJonathan Doman         if (bootModeStr !=
1010491d8ee7SSantosh Puranik             "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
1011491d8ee7SSantosh Puranik         {
10121e1e598dSJonathan Doman             auto rfMode = dbusToRfBootMode(bootModeStr);
1013491d8ee7SSantosh Puranik             if (!rfMode.empty())
1014491d8ee7SSantosh Puranik             {
1015491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
1016491d8ee7SSantosh Puranik                     rfMode;
1017491d8ee7SSantosh Puranik             }
1018491d8ee7SSantosh Puranik         }
10191e1e598dSJonathan Doman         });
1020491d8ee7SSantosh Puranik }
1021491d8ee7SSantosh Puranik 
1022491d8ee7SSantosh Puranik /**
1023c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override source over DBUS
1024491d8ee7SSantosh Puranik  *
1025491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
1026491d8ee7SSantosh Puranik  *
1027491d8ee7SSantosh Puranik  * @return None.
1028491d8ee7SSantosh Puranik  */
1029c21865c4SKonstantin Aladyshev 
1030c21865c4SKonstantin Aladyshev inline void
1031c21865c4SKonstantin Aladyshev     getBootOverrideSource(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1032491d8ee7SSantosh Puranik {
10331e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
10341e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
10351e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
10361e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Source", "BootSource",
10375e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec,
10381e1e598dSJonathan Doman                 const std::string& bootSourceStr) {
1039491d8ee7SSantosh Puranik         if (ec)
1040491d8ee7SSantosh Puranik         {
1041491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
10425ef735c8SNan Zhou             if (ec.value() == boost::asio::error::host_unreachable)
10435ef735c8SNan Zhou             {
10445ef735c8SNan Zhou                 return;
10455ef735c8SNan Zhou             }
1046491d8ee7SSantosh Puranik             messages::internalError(aResp->res);
1047491d8ee7SSantosh Puranik             return;
1048491d8ee7SSantosh Puranik         }
1049491d8ee7SSantosh Puranik 
10501e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Boot source: " << bootSourceStr;
1051491d8ee7SSantosh Puranik 
10521e1e598dSJonathan Doman         auto rfSource = dbusToRfBootSource(bootSourceStr);
1053491d8ee7SSantosh Puranik         if (!rfSource.empty())
1054491d8ee7SSantosh Puranik         {
1055002d39b4SEd Tanous             aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] = rfSource;
1056491d8ee7SSantosh Puranik         }
1057cd9a4666SKonstantin Aladyshev 
1058cd9a4666SKonstantin Aladyshev         // Get BootMode as BootSourceOverrideTarget is constructed
1059cd9a4666SKonstantin Aladyshev         // from both BootSource and BootMode
1060c21865c4SKonstantin Aladyshev         getBootOverrideMode(aResp);
10611e1e598dSJonathan Doman         });
1062491d8ee7SSantosh Puranik }
1063491d8ee7SSantosh Puranik 
1064491d8ee7SSantosh Puranik /**
1065c21865c4SKonstantin Aladyshev  * @brief This functions abstracts all the logic behind getting a
1066c21865c4SKonstantin Aladyshev  * "BootSourceOverrideEnabled" property from an overall boot override enable
1067c21865c4SKonstantin Aladyshev  * state
1068491d8ee7SSantosh Puranik  *
1069491d8ee7SSantosh Puranik  * @param[in] aResp     Shared pointer for generating response message.
1070491d8ee7SSantosh Puranik  *
1071491d8ee7SSantosh Puranik  * @return None.
1072491d8ee7SSantosh Puranik  */
1073491d8ee7SSantosh Puranik 
1074c21865c4SKonstantin Aladyshev inline void
1075c21865c4SKonstantin Aladyshev     processBootOverrideEnable(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1076c21865c4SKonstantin Aladyshev                               const bool bootOverrideEnableSetting)
1077c21865c4SKonstantin Aladyshev {
1078c21865c4SKonstantin Aladyshev     if (!bootOverrideEnableSetting)
1079c21865c4SKonstantin Aladyshev     {
1080c21865c4SKonstantin Aladyshev         aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] = "Disabled";
1081c21865c4SKonstantin Aladyshev         return;
1082c21865c4SKonstantin Aladyshev     }
1083c21865c4SKonstantin Aladyshev 
1084c21865c4SKonstantin Aladyshev     // If boot source override is enabled, we need to check 'one_time'
1085c21865c4SKonstantin Aladyshev     // property to set a correct value for the "BootSourceOverrideEnabled"
10861e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
10871e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
10881e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot/one_time",
10891e1e598dSJonathan Doman         "xyz.openbmc_project.Object.Enable", "Enabled",
10905e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec, bool oneTimeSetting) {
1091491d8ee7SSantosh Puranik         if (ec)
1092491d8ee7SSantosh Puranik         {
1093491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1094c21865c4SKonstantin Aladyshev             messages::internalError(aResp->res);
1095491d8ee7SSantosh Puranik             return;
1096491d8ee7SSantosh Puranik         }
1097491d8ee7SSantosh Puranik 
1098c21865c4SKonstantin Aladyshev         if (oneTimeSetting)
1099c21865c4SKonstantin Aladyshev         {
1100002d39b4SEd Tanous             aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] = "Once";
1101c21865c4SKonstantin Aladyshev         }
1102c21865c4SKonstantin Aladyshev         else
1103c21865c4SKonstantin Aladyshev         {
1104c21865c4SKonstantin Aladyshev             aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
1105c21865c4SKonstantin Aladyshev                 "Continuous";
1106c21865c4SKonstantin Aladyshev         }
11071e1e598dSJonathan Doman         });
1108491d8ee7SSantosh Puranik }
1109491d8ee7SSantosh Puranik 
1110491d8ee7SSantosh Puranik /**
1111c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override enable over DBUS
1112c21865c4SKonstantin Aladyshev  *
1113c21865c4SKonstantin Aladyshev  * @param[in] aResp     Shared pointer for generating response message.
1114c21865c4SKonstantin Aladyshev  *
1115c21865c4SKonstantin Aladyshev  * @return None.
1116c21865c4SKonstantin Aladyshev  */
1117c21865c4SKonstantin Aladyshev 
1118c21865c4SKonstantin Aladyshev inline void
1119c21865c4SKonstantin Aladyshev     getBootOverrideEnable(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1120c21865c4SKonstantin Aladyshev {
11211e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
11221e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
11231e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
11241e1e598dSJonathan Doman         "xyz.openbmc_project.Object.Enable", "Enabled",
11255e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec,
11261e1e598dSJonathan Doman                 const bool bootOverrideEnable) {
1127c21865c4SKonstantin Aladyshev         if (ec)
1128c21865c4SKonstantin Aladyshev         {
1129c21865c4SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
11305ef735c8SNan Zhou             if (ec.value() == boost::asio::error::host_unreachable)
11315ef735c8SNan Zhou             {
11325ef735c8SNan Zhou                 return;
11335ef735c8SNan Zhou             }
1134c21865c4SKonstantin Aladyshev             messages::internalError(aResp->res);
1135c21865c4SKonstantin Aladyshev             return;
1136c21865c4SKonstantin Aladyshev         }
1137c21865c4SKonstantin Aladyshev 
11381e1e598dSJonathan Doman         processBootOverrideEnable(aResp, bootOverrideEnable);
11391e1e598dSJonathan Doman         });
1140c21865c4SKonstantin Aladyshev }
1141c21865c4SKonstantin Aladyshev 
1142c21865c4SKonstantin Aladyshev /**
1143c21865c4SKonstantin Aladyshev  * @brief Retrieves boot source override properties
1144c21865c4SKonstantin Aladyshev  *
1145c21865c4SKonstantin Aladyshev  * @param[in] aResp     Shared pointer for generating response message.
1146c21865c4SKonstantin Aladyshev  *
1147c21865c4SKonstantin Aladyshev  * @return None.
1148c21865c4SKonstantin Aladyshev  */
1149c21865c4SKonstantin Aladyshev inline void getBootProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1150c21865c4SKonstantin Aladyshev {
1151c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Get boot information.";
1152c21865c4SKonstantin Aladyshev 
1153c21865c4SKonstantin Aladyshev     getBootOverrideSource(aResp);
1154c21865c4SKonstantin Aladyshev     getBootOverrideType(aResp);
1155c21865c4SKonstantin Aladyshev     getBootOverrideEnable(aResp);
1156c21865c4SKonstantin Aladyshev }
1157c21865c4SKonstantin Aladyshev 
1158c21865c4SKonstantin Aladyshev /**
1159c0557e1aSGunnar Mills  * @brief Retrieves the Last Reset Time
1160c0557e1aSGunnar Mills  *
1161c0557e1aSGunnar Mills  * "Reset" is an overloaded term in Redfish, "Reset" includes power on
1162c0557e1aSGunnar Mills  * and power off. Even though this is the "system" Redfish object look at the
1163c0557e1aSGunnar Mills  * chassis D-Bus interface for the LastStateChangeTime since this has the
1164c0557e1aSGunnar Mills  * last power operation time.
1165c0557e1aSGunnar Mills  *
1166c0557e1aSGunnar Mills  * @param[in] aResp     Shared pointer for generating response message.
1167c0557e1aSGunnar Mills  *
1168c0557e1aSGunnar Mills  * @return None.
1169c0557e1aSGunnar Mills  */
11708d1b46d7Szhanghch05 inline void getLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1171c0557e1aSGunnar Mills {
1172c0557e1aSGunnar Mills     BMCWEB_LOG_DEBUG << "Getting System Last Reset Time";
1173c0557e1aSGunnar Mills 
11741e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
11751e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Chassis",
11761e1e598dSJonathan Doman         "/xyz/openbmc_project/state/chassis0",
11771e1e598dSJonathan Doman         "xyz.openbmc_project.State.Chassis", "LastStateChangeTime",
11785e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec, uint64_t lastResetTime) {
1179c0557e1aSGunnar Mills         if (ec)
1180c0557e1aSGunnar Mills         {
1181c0557e1aSGunnar Mills             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
1182c0557e1aSGunnar Mills             return;
1183c0557e1aSGunnar Mills         }
1184c0557e1aSGunnar Mills 
1185c0557e1aSGunnar Mills         // LastStateChangeTime is epoch time, in milliseconds
1186c0557e1aSGunnar Mills         // https://github.com/openbmc/phosphor-dbus-interfaces/blob/33e8e1dd64da53a66e888d33dc82001305cd0bf9/xyz/openbmc_project/State/Chassis.interface.yaml#L19
11871e1e598dSJonathan Doman         uint64_t lastResetTimeStamp = lastResetTime / 1000;
1188c0557e1aSGunnar Mills 
1189c0557e1aSGunnar Mills         // Convert to ISO 8601 standard
1190c0557e1aSGunnar Mills         aResp->res.jsonValue["LastResetTime"] =
11912b82937eSEd Tanous             redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
11921e1e598dSJonathan Doman         });
1193c0557e1aSGunnar Mills }
1194c0557e1aSGunnar Mills 
1195c0557e1aSGunnar Mills /**
1196797d5daeSCorey Hardesty  * @brief Retrieves the number of automatic boot Retry attempts allowed/left.
1197797d5daeSCorey Hardesty  *
1198797d5daeSCorey Hardesty  * The total number of automatic reboot retries allowed "RetryAttempts" and its
1199797d5daeSCorey Hardesty  * corresponding property "AttemptsLeft" that keeps track of the amount of
1200797d5daeSCorey Hardesty  * automatic retry attempts left are hosted in phosphor-state-manager through
1201797d5daeSCorey Hardesty  * dbus.
1202797d5daeSCorey Hardesty  *
1203797d5daeSCorey Hardesty  * @param[in] aResp     Shared pointer for generating response message.
1204797d5daeSCorey Hardesty  *
1205797d5daeSCorey Hardesty  * @return None.
1206797d5daeSCorey Hardesty  */
1207797d5daeSCorey Hardesty inline void
1208797d5daeSCorey Hardesty     getAutomaticRebootAttempts(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1209797d5daeSCorey Hardesty {
1210797d5daeSCorey Hardesty     BMCWEB_LOG_DEBUG << "Get Automatic Retry policy";
1211797d5daeSCorey Hardesty 
1212797d5daeSCorey Hardesty     sdbusplus::asio::getAllProperties(
1213797d5daeSCorey Hardesty         *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
1214797d5daeSCorey Hardesty         "/xyz/openbmc_project/state/host0",
1215797d5daeSCorey Hardesty         "xyz.openbmc_project.Control.Boot.RebootAttempts",
1216797d5daeSCorey Hardesty         [aResp{aResp}](const boost::system::error_code& ec,
1217797d5daeSCorey Hardesty                        const dbus::utility::DBusPropertiesMap& propertiesList) {
1218797d5daeSCorey Hardesty         if (ec)
1219797d5daeSCorey Hardesty         {
1220797d5daeSCorey Hardesty             if (ec.value() != EBADR)
1221797d5daeSCorey Hardesty             {
1222797d5daeSCorey Hardesty                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1223797d5daeSCorey Hardesty                 messages::internalError(aResp->res);
1224797d5daeSCorey Hardesty             }
1225797d5daeSCorey Hardesty             return;
1226797d5daeSCorey Hardesty         }
1227797d5daeSCorey Hardesty 
1228797d5daeSCorey Hardesty         const uint32_t* attemptsLeft = nullptr;
1229797d5daeSCorey Hardesty         const uint32_t* retryAttempts = nullptr;
1230797d5daeSCorey Hardesty 
1231797d5daeSCorey Hardesty         const bool success = sdbusplus::unpackPropertiesNoThrow(
1232797d5daeSCorey Hardesty             dbus_utils::UnpackErrorPrinter(), propertiesList, "AttemptsLeft",
1233797d5daeSCorey Hardesty             attemptsLeft, "RetryAttempts", retryAttempts);
1234797d5daeSCorey Hardesty 
1235797d5daeSCorey Hardesty         if (!success)
1236797d5daeSCorey Hardesty         {
1237797d5daeSCorey Hardesty             messages::internalError(aResp->res);
1238797d5daeSCorey Hardesty             return;
1239797d5daeSCorey Hardesty         }
1240797d5daeSCorey Hardesty 
1241797d5daeSCorey Hardesty         if (attemptsLeft != nullptr)
1242797d5daeSCorey Hardesty         {
1243797d5daeSCorey Hardesty             aResp->res.jsonValue["Boot"]["RemainingAutomaticRetryAttempts"] =
1244797d5daeSCorey Hardesty                 *attemptsLeft;
1245797d5daeSCorey Hardesty         }
1246797d5daeSCorey Hardesty 
1247797d5daeSCorey Hardesty         if (retryAttempts != nullptr)
1248797d5daeSCorey Hardesty         {
1249797d5daeSCorey Hardesty             aResp->res.jsonValue["Boot"]["AutomaticRetryAttempts"] =
1250797d5daeSCorey Hardesty                 *retryAttempts;
1251797d5daeSCorey Hardesty         }
1252797d5daeSCorey Hardesty         });
1253797d5daeSCorey Hardesty }
1254797d5daeSCorey Hardesty 
1255797d5daeSCorey Hardesty /**
12566bd5a8d2SGunnar Mills  * @brief Retrieves Automatic Retry properties. Known on D-Bus as AutoReboot.
12576bd5a8d2SGunnar Mills  *
12586bd5a8d2SGunnar Mills  * @param[in] aResp     Shared pointer for generating response message.
12596bd5a8d2SGunnar Mills  *
12606bd5a8d2SGunnar Mills  * @return None.
12616bd5a8d2SGunnar Mills  */
1262797d5daeSCorey Hardesty inline void
1263797d5daeSCorey Hardesty     getAutomaticRetryPolicy(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
12646bd5a8d2SGunnar Mills {
12656bd5a8d2SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Automatic Retry policy";
12666bd5a8d2SGunnar Mills 
12671e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
12681e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
12691e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/auto_reboot",
12701e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
12715e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec, bool autoRebootEnabled) {
12726bd5a8d2SGunnar Mills         if (ec)
12736bd5a8d2SGunnar Mills         {
1274797d5daeSCorey Hardesty             if (ec.value() != EBADR)
1275797d5daeSCorey Hardesty             {
1276797d5daeSCorey Hardesty                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1277797d5daeSCorey Hardesty                 messages::internalError(aResp->res);
1278797d5daeSCorey Hardesty             }
12796bd5a8d2SGunnar Mills             return;
12806bd5a8d2SGunnar Mills         }
12816bd5a8d2SGunnar Mills 
12821e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Auto Reboot: " << autoRebootEnabled;
1283e05aec50SEd Tanous         if (autoRebootEnabled)
12846bd5a8d2SGunnar Mills         {
12856bd5a8d2SGunnar Mills             aResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
12866bd5a8d2SGunnar Mills                 "RetryAttempts";
12876bd5a8d2SGunnar Mills         }
12886bd5a8d2SGunnar Mills         else
12896bd5a8d2SGunnar Mills         {
1290002d39b4SEd Tanous             aResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] = "Disabled";
12916bd5a8d2SGunnar Mills         }
1292797d5daeSCorey Hardesty         getAutomaticRebootAttempts(aResp);
129369f35306SGunnar Mills 
129469f35306SGunnar Mills         // "AutomaticRetryConfig" can be 3 values, Disabled, RetryAlways,
129569f35306SGunnar Mills         // and RetryAttempts. OpenBMC only supports Disabled and
129669f35306SGunnar Mills         // RetryAttempts.
1297002d39b4SEd Tanous         aResp->res.jsonValue["Boot"]
12980fda0f12SGeorge Liu                             ["AutomaticRetryConfig@Redfish.AllowableValues"] = {
12990fda0f12SGeorge Liu             "Disabled", "RetryAttempts"};
13001e1e598dSJonathan Doman         });
13016bd5a8d2SGunnar Mills }
13026bd5a8d2SGunnar Mills 
13036bd5a8d2SGunnar Mills /**
1304797d5daeSCorey Hardesty  * @brief Sets RetryAttempts
1305797d5daeSCorey Hardesty  *
1306797d5daeSCorey Hardesty  * @param[in] aResp   Shared pointer for generating response message.
1307797d5daeSCorey Hardesty  * @param[in] retryAttempts  "AutomaticRetryAttempts" from request.
1308797d5daeSCorey Hardesty  *
1309797d5daeSCorey Hardesty  *@return None.
1310797d5daeSCorey Hardesty  */
1311797d5daeSCorey Hardesty 
1312797d5daeSCorey Hardesty inline void
1313797d5daeSCorey Hardesty     setAutomaticRetryAttempts(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1314797d5daeSCorey Hardesty                               const uint32_t retryAttempts)
1315797d5daeSCorey Hardesty {
1316797d5daeSCorey Hardesty     BMCWEB_LOG_DEBUG << "Set Automatic Retry Attempts.";
1317797d5daeSCorey Hardesty     crow::connections::systemBus->async_method_call(
1318797d5daeSCorey Hardesty         [aResp](const boost::system::error_code& ec) {
1319797d5daeSCorey Hardesty         if (ec)
1320797d5daeSCorey Hardesty         {
1321797d5daeSCorey Hardesty             BMCWEB_LOG_ERROR
1322797d5daeSCorey Hardesty                 << "DBUS response error: Set setAutomaticRetryAttempts" << ec;
1323797d5daeSCorey Hardesty             messages::internalError(aResp->res);
1324797d5daeSCorey Hardesty             return;
1325797d5daeSCorey Hardesty         }
1326797d5daeSCorey Hardesty         },
1327797d5daeSCorey Hardesty         "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
1328797d5daeSCorey Hardesty         "org.freedesktop.DBus.Properties", "Set",
1329797d5daeSCorey Hardesty         "xyz.openbmc_project.Control.Boot.RebootAttempts", "RetryAttempts",
1330797d5daeSCorey Hardesty         std::variant<uint32_t>(retryAttempts));
1331797d5daeSCorey Hardesty }
1332797d5daeSCorey Hardesty 
1333797d5daeSCorey Hardesty /**
1334c6a620f2SGeorge Liu  * @brief Retrieves power restore policy over DBUS.
1335c6a620f2SGeorge Liu  *
1336c6a620f2SGeorge Liu  * @param[in] aResp     Shared pointer for generating response message.
1337c6a620f2SGeorge Liu  *
1338c6a620f2SGeorge Liu  * @return None.
1339c6a620f2SGeorge Liu  */
13408d1b46d7Szhanghch05 inline void
13418d1b46d7Szhanghch05     getPowerRestorePolicy(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1342c6a620f2SGeorge Liu {
1343c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Get power restore policy";
1344c6a620f2SGeorge Liu 
13451e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
13461e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
13471e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/power_restore_policy",
13481e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
13495e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec,
13505e7e2dc5SEd Tanous                 const std::string& policy) {
1351c6a620f2SGeorge Liu         if (ec)
1352c6a620f2SGeorge Liu         {
1353c6a620f2SGeorge Liu             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1354c6a620f2SGeorge Liu             return;
1355c6a620f2SGeorge Liu         }
1356c6a620f2SGeorge Liu 
13570fda0f12SGeorge Liu         const boost::container::flat_map<std::string, std::string> policyMaps = {
13580fda0f12SGeorge Liu             {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn",
1359c6a620f2SGeorge Liu              "AlwaysOn"},
13600fda0f12SGeorge Liu             {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff",
1361c6a620f2SGeorge Liu              "AlwaysOff"},
13620fda0f12SGeorge Liu             {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore",
13634ed47cb8SMatthew Barth              "LastState"},
13644ed47cb8SMatthew Barth             // Return `AlwaysOff` when power restore policy set to "None"
13654ed47cb8SMatthew Barth             {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.None",
13664ed47cb8SMatthew Barth              "AlwaysOff"}};
1367c6a620f2SGeorge Liu 
13681e1e598dSJonathan Doman         auto policyMapsIt = policyMaps.find(policy);
1369c6a620f2SGeorge Liu         if (policyMapsIt == policyMaps.end())
1370c6a620f2SGeorge Liu         {
1371c6a620f2SGeorge Liu             messages::internalError(aResp->res);
1372c6a620f2SGeorge Liu             return;
1373c6a620f2SGeorge Liu         }
1374c6a620f2SGeorge Liu 
1375c6a620f2SGeorge Liu         aResp->res.jsonValue["PowerRestorePolicy"] = policyMapsIt->second;
13761e1e598dSJonathan Doman         });
1377c6a620f2SGeorge Liu }
1378c6a620f2SGeorge Liu 
1379c6a620f2SGeorge Liu /**
13801981771bSAli Ahmed  * @brief Get TrustedModuleRequiredToBoot property. Determines whether or not
13811981771bSAli Ahmed  * TPM is required for booting the host.
13821981771bSAli Ahmed  *
13831981771bSAli Ahmed  * @param[in] aResp     Shared pointer for generating response message.
13841981771bSAli Ahmed  *
13851981771bSAli Ahmed  * @return None.
13861981771bSAli Ahmed  */
13871981771bSAli Ahmed inline void getTrustedModuleRequiredToBoot(
13881981771bSAli Ahmed     const std::shared_ptr<bmcweb::AsyncResp>& aResp)
13891981771bSAli Ahmed {
13901981771bSAli Ahmed     BMCWEB_LOG_DEBUG << "Get TPM required to boot.";
1391e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1392e99073f5SGeorge Liu         "xyz.openbmc_project.Control.TPM.Policy"};
1393e99073f5SGeorge Liu     dbus::utility::getSubTree(
1394e99073f5SGeorge Liu         "/", 0, interfaces,
1395e99073f5SGeorge Liu         [aResp](const boost::system::error_code& ec,
1396b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
13971981771bSAli Ahmed         if (ec)
13981981771bSAli Ahmed         {
1399002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on TPM.Policy GetSubTree"
1400002d39b4SEd Tanous                              << ec;
14011981771bSAli Ahmed             // This is an optional D-Bus object so just return if
14021981771bSAli Ahmed             // error occurs
14031981771bSAli Ahmed             return;
14041981771bSAli Ahmed         }
140526f6976fSEd Tanous         if (subtree.empty())
14061981771bSAli Ahmed         {
14071981771bSAli Ahmed             // As noted above, this is an optional interface so just return
14081981771bSAli Ahmed             // if there is no instance found
14091981771bSAli Ahmed             return;
14101981771bSAli Ahmed         }
14111981771bSAli Ahmed 
14121981771bSAli Ahmed         /* When there is more than one TPMEnable object... */
14131981771bSAli Ahmed         if (subtree.size() > 1)
14141981771bSAli Ahmed         {
14151981771bSAli Ahmed             BMCWEB_LOG_DEBUG
14161981771bSAli Ahmed                 << "DBUS response has more than 1 TPM Enable object:"
14171981771bSAli Ahmed                 << subtree.size();
14181981771bSAli Ahmed             // Throw an internal Error and return
14191981771bSAli Ahmed             messages::internalError(aResp->res);
14201981771bSAli Ahmed             return;
14211981771bSAli Ahmed         }
14221981771bSAli Ahmed 
14231981771bSAli Ahmed         // Make sure the Dbus response map has a service and objectPath
14241981771bSAli Ahmed         // field
14251981771bSAli Ahmed         if (subtree[0].first.empty() || subtree[0].second.size() != 1)
14261981771bSAli Ahmed         {
14271981771bSAli Ahmed             BMCWEB_LOG_DEBUG << "TPM.Policy mapper error!";
14281981771bSAli Ahmed             messages::internalError(aResp->res);
14291981771bSAli Ahmed             return;
14301981771bSAli Ahmed         }
14311981771bSAli Ahmed 
14321981771bSAli Ahmed         const std::string& path = subtree[0].first;
14331981771bSAli Ahmed         const std::string& serv = subtree[0].second.begin()->first;
14341981771bSAli Ahmed 
14351981771bSAli Ahmed         // Valid TPM Enable object found, now reading the current value
14361e1e598dSJonathan Doman         sdbusplus::asio::getProperty<bool>(
14371e1e598dSJonathan Doman             *crow::connections::systemBus, serv, path,
14381e1e598dSJonathan Doman             "xyz.openbmc_project.Control.TPM.Policy", "TPMEnable",
14395e7e2dc5SEd Tanous             [aResp](const boost::system::error_code& ec2, bool tpmRequired) {
14408a592810SEd Tanous             if (ec2)
14411981771bSAli Ahmed             {
1442002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "D-BUS response error on TPM.Policy Get"
14438a592810SEd Tanous                                  << ec2;
14441981771bSAli Ahmed                 messages::internalError(aResp->res);
14451981771bSAli Ahmed                 return;
14461981771bSAli Ahmed             }
14471981771bSAli Ahmed 
14481e1e598dSJonathan Doman             if (tpmRequired)
14491981771bSAli Ahmed             {
1450002d39b4SEd Tanous                 aResp->res.jsonValue["Boot"]["TrustedModuleRequiredToBoot"] =
14511981771bSAli Ahmed                     "Required";
14521981771bSAli Ahmed             }
14531981771bSAli Ahmed             else
14541981771bSAli Ahmed             {
1455002d39b4SEd Tanous                 aResp->res.jsonValue["Boot"]["TrustedModuleRequiredToBoot"] =
14561981771bSAli Ahmed                     "Disabled";
14571981771bSAli Ahmed             }
14581e1e598dSJonathan Doman             });
1459e99073f5SGeorge Liu         });
14601981771bSAli Ahmed }
14611981771bSAli Ahmed 
14621981771bSAli Ahmed /**
14631c05dae3SAli Ahmed  * @brief Set TrustedModuleRequiredToBoot property. Determines whether or not
14641c05dae3SAli Ahmed  * TPM is required for booting the host.
14651c05dae3SAli Ahmed  *
14661c05dae3SAli Ahmed  * @param[in] aResp         Shared pointer for generating response message.
14671c05dae3SAli Ahmed  * @param[in] tpmRequired   Value to set TPM Required To Boot property to.
14681c05dae3SAli Ahmed  *
14691c05dae3SAli Ahmed  * @return None.
14701c05dae3SAli Ahmed  */
14711c05dae3SAli Ahmed inline void setTrustedModuleRequiredToBoot(
14721c05dae3SAli Ahmed     const std::shared_ptr<bmcweb::AsyncResp>& aResp, const bool tpmRequired)
14731c05dae3SAli Ahmed {
14741c05dae3SAli Ahmed     BMCWEB_LOG_DEBUG << "Set TrustedModuleRequiredToBoot.";
1475e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1476e99073f5SGeorge Liu         "xyz.openbmc_project.Control.TPM.Policy"};
1477e99073f5SGeorge Liu     dbus::utility::getSubTree(
1478e99073f5SGeorge Liu         "/", 0, interfaces,
1479e99073f5SGeorge Liu         [aResp,
1480e99073f5SGeorge Liu          tpmRequired](const boost::system::error_code& ec,
1481e99073f5SGeorge Liu                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
14821c05dae3SAli Ahmed         if (ec)
14831c05dae3SAli Ahmed         {
1484002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on TPM.Policy GetSubTree"
1485002d39b4SEd Tanous                              << ec;
14861c05dae3SAli Ahmed             messages::internalError(aResp->res);
14871c05dae3SAli Ahmed             return;
14881c05dae3SAli Ahmed         }
148926f6976fSEd Tanous         if (subtree.empty())
14901c05dae3SAli Ahmed         {
14911c05dae3SAli Ahmed             messages::propertyValueNotInList(aResp->res, "ComputerSystem",
14921c05dae3SAli Ahmed                                              "TrustedModuleRequiredToBoot");
14931c05dae3SAli Ahmed             return;
14941c05dae3SAli Ahmed         }
14951c05dae3SAli Ahmed 
14961c05dae3SAli Ahmed         /* When there is more than one TPMEnable object... */
14971c05dae3SAli Ahmed         if (subtree.size() > 1)
14981c05dae3SAli Ahmed         {
14991c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG
15001c05dae3SAli Ahmed                 << "DBUS response has more than 1 TPM Enable object:"
15011c05dae3SAli Ahmed                 << subtree.size();
15021c05dae3SAli Ahmed             // Throw an internal Error and return
15031c05dae3SAli Ahmed             messages::internalError(aResp->res);
15041c05dae3SAli Ahmed             return;
15051c05dae3SAli Ahmed         }
15061c05dae3SAli Ahmed 
15071c05dae3SAli Ahmed         // Make sure the Dbus response map has a service and objectPath
15081c05dae3SAli Ahmed         // field
15091c05dae3SAli Ahmed         if (subtree[0].first.empty() || subtree[0].second.size() != 1)
15101c05dae3SAli Ahmed         {
15111c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG << "TPM.Policy mapper error!";
15121c05dae3SAli Ahmed             messages::internalError(aResp->res);
15131c05dae3SAli Ahmed             return;
15141c05dae3SAli Ahmed         }
15151c05dae3SAli Ahmed 
15161c05dae3SAli Ahmed         const std::string& path = subtree[0].first;
15171c05dae3SAli Ahmed         const std::string& serv = subtree[0].second.begin()->first;
15181c05dae3SAli Ahmed 
15191c05dae3SAli Ahmed         if (serv.empty())
15201c05dae3SAli Ahmed         {
15211c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG << "TPM.Policy service mapper error!";
15221c05dae3SAli Ahmed             messages::internalError(aResp->res);
15231c05dae3SAli Ahmed             return;
15241c05dae3SAli Ahmed         }
15251c05dae3SAli Ahmed 
15261c05dae3SAli Ahmed         // Valid TPM Enable object found, now setting the value
15271c05dae3SAli Ahmed         crow::connections::systemBus->async_method_call(
15285e7e2dc5SEd Tanous             [aResp](const boost::system::error_code& ec2) {
15298a592810SEd Tanous             if (ec2)
15301c05dae3SAli Ahmed             {
15310fda0f12SGeorge Liu                 BMCWEB_LOG_DEBUG
15320fda0f12SGeorge Liu                     << "DBUS response error: Set TrustedModuleRequiredToBoot"
15338a592810SEd Tanous                     << ec2;
15341c05dae3SAli Ahmed                 messages::internalError(aResp->res);
15351c05dae3SAli Ahmed                 return;
15361c05dae3SAli Ahmed             }
15371c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG << "Set TrustedModuleRequiredToBoot done.";
15381c05dae3SAli Ahmed             },
15391c05dae3SAli Ahmed             serv, path, "org.freedesktop.DBus.Properties", "Set",
15401c05dae3SAli Ahmed             "xyz.openbmc_project.Control.TPM.Policy", "TPMEnable",
1541168e20c1SEd Tanous             dbus::utility::DbusVariantType(tpmRequired));
1542e99073f5SGeorge Liu         });
15431c05dae3SAli Ahmed }
15441c05dae3SAli Ahmed 
15451c05dae3SAli Ahmed /**
1546491d8ee7SSantosh Puranik  * @brief Sets boot properties into DBUS object(s).
1547491d8ee7SSantosh Puranik  *
1548491d8ee7SSantosh Puranik  * @param[in] aResp           Shared pointer for generating response message.
1549cd9a4666SKonstantin Aladyshev  * @param[in] bootType        The boot type to set.
1550cd9a4666SKonstantin Aladyshev  * @return Integer error code.
1551cd9a4666SKonstantin Aladyshev  */
1552cd9a4666SKonstantin Aladyshev inline void setBootType(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1553cd9a4666SKonstantin Aladyshev                         const std::optional<std::string>& bootType)
1554cd9a4666SKonstantin Aladyshev {
1555c21865c4SKonstantin Aladyshev     std::string bootTypeStr;
1556cd9a4666SKonstantin Aladyshev 
1557c21865c4SKonstantin Aladyshev     if (!bootType)
1558cd9a4666SKonstantin Aladyshev     {
1559c21865c4SKonstantin Aladyshev         return;
1560c21865c4SKonstantin Aladyshev     }
1561c21865c4SKonstantin Aladyshev 
1562cd9a4666SKonstantin Aladyshev     // Source target specified
1563cd9a4666SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Boot type: " << *bootType;
1564cd9a4666SKonstantin Aladyshev     // Figure out which DBUS interface and property to use
1565cd9a4666SKonstantin Aladyshev     if (*bootType == "Legacy")
1566cd9a4666SKonstantin Aladyshev     {
1567cd9a4666SKonstantin Aladyshev         bootTypeStr = "xyz.openbmc_project.Control.Boot.Type.Types.Legacy";
1568cd9a4666SKonstantin Aladyshev     }
1569cd9a4666SKonstantin Aladyshev     else if (*bootType == "UEFI")
1570cd9a4666SKonstantin Aladyshev     {
1571cd9a4666SKonstantin Aladyshev         bootTypeStr = "xyz.openbmc_project.Control.Boot.Type.Types.EFI";
1572cd9a4666SKonstantin Aladyshev     }
1573cd9a4666SKonstantin Aladyshev     else
1574cd9a4666SKonstantin Aladyshev     {
1575cd9a4666SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Invalid property value for "
1576cd9a4666SKonstantin Aladyshev                             "BootSourceOverrideMode: "
1577cd9a4666SKonstantin Aladyshev                          << *bootType;
1578cd9a4666SKonstantin Aladyshev         messages::propertyValueNotInList(aResp->res, *bootType,
1579cd9a4666SKonstantin Aladyshev                                          "BootSourceOverrideMode");
1580cd9a4666SKonstantin Aladyshev         return;
1581cd9a4666SKonstantin Aladyshev     }
1582cd9a4666SKonstantin Aladyshev 
1583cd9a4666SKonstantin Aladyshev     // Act on validated parameters
1584cd9a4666SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot type: " << bootTypeStr;
1585cd9a4666SKonstantin Aladyshev 
1586cd9a4666SKonstantin Aladyshev     crow::connections::systemBus->async_method_call(
15875e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec) {
1588cd9a4666SKonstantin Aladyshev         if (ec)
1589cd9a4666SKonstantin Aladyshev         {
1590cd9a4666SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1591cd9a4666SKonstantin Aladyshev             if (ec.value() == boost::asio::error::host_unreachable)
1592cd9a4666SKonstantin Aladyshev             {
1593cd9a4666SKonstantin Aladyshev                 messages::resourceNotFound(aResp->res, "Set", "BootType");
1594cd9a4666SKonstantin Aladyshev                 return;
1595cd9a4666SKonstantin Aladyshev             }
1596cd9a4666SKonstantin Aladyshev             messages::internalError(aResp->res);
1597cd9a4666SKonstantin Aladyshev             return;
1598cd9a4666SKonstantin Aladyshev         }
1599cd9a4666SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Boot type update done.";
1600cd9a4666SKonstantin Aladyshev         },
1601c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1602c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1603cd9a4666SKonstantin Aladyshev         "org.freedesktop.DBus.Properties", "Set",
1604cd9a4666SKonstantin Aladyshev         "xyz.openbmc_project.Control.Boot.Type", "BootType",
1605168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootTypeStr));
1606cd9a4666SKonstantin Aladyshev }
1607cd9a4666SKonstantin Aladyshev 
1608cd9a4666SKonstantin Aladyshev /**
1609cd9a4666SKonstantin Aladyshev  * @brief Sets boot properties into DBUS object(s).
1610cd9a4666SKonstantin Aladyshev  *
1611cd9a4666SKonstantin Aladyshev  * @param[in] aResp           Shared pointer for generating response message.
1612c21865c4SKonstantin Aladyshev  * @param[in] bootType        The boot type to set.
1613c21865c4SKonstantin Aladyshev  * @return Integer error code.
1614c21865c4SKonstantin Aladyshev  */
1615c21865c4SKonstantin Aladyshev inline void setBootEnable(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1616c21865c4SKonstantin Aladyshev                           const std::optional<std::string>& bootEnable)
1617c21865c4SKonstantin Aladyshev {
1618c21865c4SKonstantin Aladyshev     if (!bootEnable)
1619c21865c4SKonstantin Aladyshev     {
1620c21865c4SKonstantin Aladyshev         return;
1621c21865c4SKonstantin Aladyshev     }
1622c21865c4SKonstantin Aladyshev     // Source target specified
1623c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Boot enable: " << *bootEnable;
1624c21865c4SKonstantin Aladyshev 
1625c21865c4SKonstantin Aladyshev     bool bootOverrideEnable = false;
1626c21865c4SKonstantin Aladyshev     bool bootOverridePersistent = false;
1627c21865c4SKonstantin Aladyshev     // Figure out which DBUS interface and property to use
1628c21865c4SKonstantin Aladyshev     if (*bootEnable == "Disabled")
1629c21865c4SKonstantin Aladyshev     {
1630c21865c4SKonstantin Aladyshev         bootOverrideEnable = false;
1631c21865c4SKonstantin Aladyshev     }
1632c21865c4SKonstantin Aladyshev     else if (*bootEnable == "Once")
1633c21865c4SKonstantin Aladyshev     {
1634c21865c4SKonstantin Aladyshev         bootOverrideEnable = true;
1635c21865c4SKonstantin Aladyshev         bootOverridePersistent = false;
1636c21865c4SKonstantin Aladyshev     }
1637c21865c4SKonstantin Aladyshev     else if (*bootEnable == "Continuous")
1638c21865c4SKonstantin Aladyshev     {
1639c21865c4SKonstantin Aladyshev         bootOverrideEnable = true;
1640c21865c4SKonstantin Aladyshev         bootOverridePersistent = true;
1641c21865c4SKonstantin Aladyshev     }
1642c21865c4SKonstantin Aladyshev     else
1643c21865c4SKonstantin Aladyshev     {
16440fda0f12SGeorge Liu         BMCWEB_LOG_DEBUG
16450fda0f12SGeorge Liu             << "Invalid property value for BootSourceOverrideEnabled: "
1646c21865c4SKonstantin Aladyshev             << *bootEnable;
1647c21865c4SKonstantin Aladyshev         messages::propertyValueNotInList(aResp->res, *bootEnable,
1648c21865c4SKonstantin Aladyshev                                          "BootSourceOverrideEnabled");
1649c21865c4SKonstantin Aladyshev         return;
1650c21865c4SKonstantin Aladyshev     }
1651c21865c4SKonstantin Aladyshev 
1652c21865c4SKonstantin Aladyshev     // Act on validated parameters
1653c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot override enable: " << bootOverrideEnable;
1654c21865c4SKonstantin Aladyshev 
1655c21865c4SKonstantin Aladyshev     crow::connections::systemBus->async_method_call(
16565e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec2) {
16578a592810SEd Tanous         if (ec2)
1658c21865c4SKonstantin Aladyshev         {
16598a592810SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
1660c21865c4SKonstantin Aladyshev             messages::internalError(aResp->res);
1661c21865c4SKonstantin Aladyshev             return;
1662c21865c4SKonstantin Aladyshev         }
1663c21865c4SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Boot override enable update done.";
1664c21865c4SKonstantin Aladyshev         },
1665c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1666c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1667c21865c4SKonstantin Aladyshev         "org.freedesktop.DBus.Properties", "Set",
1668c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Object.Enable", "Enabled",
1669168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootOverrideEnable));
1670c21865c4SKonstantin Aladyshev 
1671c21865c4SKonstantin Aladyshev     if (!bootOverrideEnable)
1672c21865c4SKonstantin Aladyshev     {
1673c21865c4SKonstantin Aladyshev         return;
1674c21865c4SKonstantin Aladyshev     }
1675c21865c4SKonstantin Aladyshev 
1676c21865c4SKonstantin Aladyshev     // In case boot override is enabled we need to set correct value for the
1677c21865c4SKonstantin Aladyshev     // 'one_time' enable DBus interface
1678c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot override persistent: "
1679c21865c4SKonstantin Aladyshev                      << bootOverridePersistent;
1680c21865c4SKonstantin Aladyshev 
1681c21865c4SKonstantin Aladyshev     crow::connections::systemBus->async_method_call(
16825e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec) {
1683c21865c4SKonstantin Aladyshev         if (ec)
1684c21865c4SKonstantin Aladyshev         {
1685c21865c4SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1686c21865c4SKonstantin Aladyshev             messages::internalError(aResp->res);
1687c21865c4SKonstantin Aladyshev             return;
1688c21865c4SKonstantin Aladyshev         }
1689c21865c4SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Boot one_time update done.";
1690c21865c4SKonstantin Aladyshev         },
1691c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1692c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot/one_time",
1693c21865c4SKonstantin Aladyshev         "org.freedesktop.DBus.Properties", "Set",
1694c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Object.Enable", "Enabled",
1695168e20c1SEd Tanous         dbus::utility::DbusVariantType(!bootOverridePersistent));
1696c21865c4SKonstantin Aladyshev }
1697c21865c4SKonstantin Aladyshev 
1698c21865c4SKonstantin Aladyshev /**
1699c21865c4SKonstantin Aladyshev  * @brief Sets boot properties into DBUS object(s).
1700c21865c4SKonstantin Aladyshev  *
1701c21865c4SKonstantin Aladyshev  * @param[in] aResp           Shared pointer for generating response message.
1702491d8ee7SSantosh Puranik  * @param[in] bootSource      The boot source to set.
1703491d8ee7SSantosh Puranik  *
1704265c1602SJohnathan Mantey  * @return Integer error code.
1705491d8ee7SSantosh Puranik  */
1706cd9a4666SKonstantin Aladyshev inline void setBootModeOrSource(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1707cd9a4666SKonstantin Aladyshev                                 const std::optional<std::string>& bootSource)
1708491d8ee7SSantosh Puranik {
1709c21865c4SKonstantin Aladyshev     std::string bootSourceStr;
1710c21865c4SKonstantin Aladyshev     std::string bootModeStr;
1711944ffaf9SJohnathan Mantey 
1712c21865c4SKonstantin Aladyshev     if (!bootSource)
1713491d8ee7SSantosh Puranik     {
1714c21865c4SKonstantin Aladyshev         return;
1715c21865c4SKonstantin Aladyshev     }
1716c21865c4SKonstantin Aladyshev 
1717491d8ee7SSantosh Puranik     // Source target specified
1718491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Boot source: " << *bootSource;
1719491d8ee7SSantosh Puranik     // Figure out which DBUS interface and property to use
1720e662eae8SEd Tanous     if (assignBootParameters(aResp, *bootSource, bootSourceStr, bootModeStr) !=
1721e662eae8SEd Tanous         0)
1722491d8ee7SSantosh Puranik     {
1723944ffaf9SJohnathan Mantey         BMCWEB_LOG_DEBUG
1724944ffaf9SJohnathan Mantey             << "Invalid property value for BootSourceOverrideTarget: "
1725491d8ee7SSantosh Puranik             << *bootSource;
1726491d8ee7SSantosh Puranik         messages::propertyValueNotInList(aResp->res, *bootSource,
1727491d8ee7SSantosh Puranik                                          "BootSourceTargetOverride");
1728491d8ee7SSantosh Puranik         return;
1729491d8ee7SSantosh Puranik     }
1730491d8ee7SSantosh Puranik 
1731944ffaf9SJohnathan Mantey     // Act on validated parameters
1732944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
1733944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
1734944ffaf9SJohnathan Mantey 
1735491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
17365e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec) {
1737491d8ee7SSantosh Puranik         if (ec)
1738491d8ee7SSantosh Puranik         {
1739491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1740491d8ee7SSantosh Puranik             messages::internalError(aResp->res);
1741491d8ee7SSantosh Puranik             return;
1742491d8ee7SSantosh Puranik         }
1743491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot source update done.";
1744491d8ee7SSantosh Puranik         },
1745c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1746c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1747491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1748491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource",
1749168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootSourceStr));
1750944ffaf9SJohnathan Mantey 
1751491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
17525e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec) {
1753491d8ee7SSantosh Puranik         if (ec)
1754491d8ee7SSantosh Puranik         {
1755491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1756491d8ee7SSantosh Puranik             messages::internalError(aResp->res);
1757491d8ee7SSantosh Puranik             return;
1758491d8ee7SSantosh Puranik         }
1759491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot mode update done.";
1760491d8ee7SSantosh Puranik         },
1761c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1762c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1763491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1764491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
1765168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootModeStr));
1766cd9a4666SKonstantin Aladyshev }
1767944ffaf9SJohnathan Mantey 
1768cd9a4666SKonstantin Aladyshev /**
1769c21865c4SKonstantin Aladyshev  * @brief Sets Boot source override properties.
1770491d8ee7SSantosh Puranik  *
1771491d8ee7SSantosh Puranik  * @param[in] aResp      Shared pointer for generating response message.
1772491d8ee7SSantosh Puranik  * @param[in] bootSource The boot source from incoming RF request.
1773cd9a4666SKonstantin Aladyshev  * @param[in] bootType   The boot type from incoming RF request.
1774491d8ee7SSantosh Puranik  * @param[in] bootEnable The boot override enable from incoming RF request.
1775491d8ee7SSantosh Puranik  *
1776265c1602SJohnathan Mantey  * @return Integer error code.
1777491d8ee7SSantosh Puranik  */
1778c21865c4SKonstantin Aladyshev 
1779c21865c4SKonstantin Aladyshev inline void setBootProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1780c21865c4SKonstantin Aladyshev                               const std::optional<std::string>& bootSource,
1781c21865c4SKonstantin Aladyshev                               const std::optional<std::string>& bootType,
1782c21865c4SKonstantin Aladyshev                               const std::optional<std::string>& bootEnable)
1783491d8ee7SSantosh Puranik {
1784491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set boot information.";
1785491d8ee7SSantosh Puranik 
1786c21865c4SKonstantin Aladyshev     setBootModeOrSource(aResp, bootSource);
1787c21865c4SKonstantin Aladyshev     setBootType(aResp, bootType);
1788c21865c4SKonstantin Aladyshev     setBootEnable(aResp, bootEnable);
1789491d8ee7SSantosh Puranik }
1790491d8ee7SSantosh Puranik 
1791c6a620f2SGeorge Liu /**
179298e386ecSGunnar Mills  * @brief Sets AssetTag
179398e386ecSGunnar Mills  *
179498e386ecSGunnar Mills  * @param[in] aResp   Shared pointer for generating response message.
179598e386ecSGunnar Mills  * @param[in] assetTag  "AssetTag" from request.
179698e386ecSGunnar Mills  *
179798e386ecSGunnar Mills  * @return None.
179898e386ecSGunnar Mills  */
17998d1b46d7Szhanghch05 inline void setAssetTag(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
180098e386ecSGunnar Mills                         const std::string& assetTag)
180198e386ecSGunnar Mills {
1802e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1803e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.System"};
1804e99073f5SGeorge Liu     dbus::utility::getSubTree(
1805e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
1806b9d36b47SEd Tanous         [aResp,
1807e99073f5SGeorge Liu          assetTag](const boost::system::error_code& ec,
1808b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
180998e386ecSGunnar Mills         if (ec)
181098e386ecSGunnar Mills         {
181198e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " << ec;
181298e386ecSGunnar Mills             messages::internalError(aResp->res);
181398e386ecSGunnar Mills             return;
181498e386ecSGunnar Mills         }
181526f6976fSEd Tanous         if (subtree.empty())
181698e386ecSGunnar Mills         {
181798e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find system D-Bus object!";
181898e386ecSGunnar Mills             messages::internalError(aResp->res);
181998e386ecSGunnar Mills             return;
182098e386ecSGunnar Mills         }
182198e386ecSGunnar Mills         // Assume only 1 system D-Bus object
182298e386ecSGunnar Mills         // Throw an error if there is more than 1
182398e386ecSGunnar Mills         if (subtree.size() > 1)
182498e386ecSGunnar Mills         {
182598e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Found more than 1 system D-Bus object!";
182698e386ecSGunnar Mills             messages::internalError(aResp->res);
182798e386ecSGunnar Mills             return;
182898e386ecSGunnar Mills         }
182998e386ecSGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.size() != 1)
183098e386ecSGunnar Mills         {
183198e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Asset Tag Set mapper error!";
183298e386ecSGunnar Mills             messages::internalError(aResp->res);
183398e386ecSGunnar Mills             return;
183498e386ecSGunnar Mills         }
183598e386ecSGunnar Mills 
183698e386ecSGunnar Mills         const std::string& path = subtree[0].first;
183798e386ecSGunnar Mills         const std::string& service = subtree[0].second.begin()->first;
183898e386ecSGunnar Mills 
183998e386ecSGunnar Mills         if (service.empty())
184098e386ecSGunnar Mills         {
184198e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Asset Tag Set service mapper error!";
184298e386ecSGunnar Mills             messages::internalError(aResp->res);
184398e386ecSGunnar Mills             return;
184498e386ecSGunnar Mills         }
184598e386ecSGunnar Mills 
184698e386ecSGunnar Mills         crow::connections::systemBus->async_method_call(
18475e7e2dc5SEd Tanous             [aResp](const boost::system::error_code& ec2) {
184898e386ecSGunnar Mills             if (ec2)
184998e386ecSGunnar Mills             {
1850002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "D-Bus response error on AssetTag Set "
1851002d39b4SEd Tanous                                  << ec2;
185298e386ecSGunnar Mills                 messages::internalError(aResp->res);
185398e386ecSGunnar Mills                 return;
185498e386ecSGunnar Mills             }
185598e386ecSGunnar Mills             },
185698e386ecSGunnar Mills             service, path, "org.freedesktop.DBus.Properties", "Set",
185798e386ecSGunnar Mills             "xyz.openbmc_project.Inventory.Decorator.AssetTag", "AssetTag",
1858168e20c1SEd Tanous             dbus::utility::DbusVariantType(assetTag));
1859e99073f5SGeorge Liu         });
186098e386ecSGunnar Mills }
186198e386ecSGunnar Mills 
186298e386ecSGunnar Mills /**
186369f35306SGunnar Mills  * @brief Sets automaticRetry (Auto Reboot)
186469f35306SGunnar Mills  *
186569f35306SGunnar Mills  * @param[in] aResp   Shared pointer for generating response message.
186669f35306SGunnar Mills  * @param[in] automaticRetryConfig  "AutomaticRetryConfig" from request.
186769f35306SGunnar Mills  *
186869f35306SGunnar Mills  * @return None.
186969f35306SGunnar Mills  */
18708d1b46d7Szhanghch05 inline void setAutomaticRetry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1871f23b7296SEd Tanous                               const std::string& automaticRetryConfig)
187269f35306SGunnar Mills {
187369f35306SGunnar Mills     BMCWEB_LOG_DEBUG << "Set Automatic Retry.";
187469f35306SGunnar Mills 
187569f35306SGunnar Mills     // OpenBMC only supports "Disabled" and "RetryAttempts".
1876543f4400SEd Tanous     bool autoRebootEnabled = false;
187769f35306SGunnar Mills 
187869f35306SGunnar Mills     if (automaticRetryConfig == "Disabled")
187969f35306SGunnar Mills     {
188069f35306SGunnar Mills         autoRebootEnabled = false;
188169f35306SGunnar Mills     }
188269f35306SGunnar Mills     else if (automaticRetryConfig == "RetryAttempts")
188369f35306SGunnar Mills     {
188469f35306SGunnar Mills         autoRebootEnabled = true;
188569f35306SGunnar Mills     }
188669f35306SGunnar Mills     else
188769f35306SGunnar Mills     {
18880fda0f12SGeorge Liu         BMCWEB_LOG_DEBUG << "Invalid property value for AutomaticRetryConfig: "
188969f35306SGunnar Mills                          << automaticRetryConfig;
189069f35306SGunnar Mills         messages::propertyValueNotInList(aResp->res, automaticRetryConfig,
189169f35306SGunnar Mills                                          "AutomaticRetryConfig");
189269f35306SGunnar Mills         return;
189369f35306SGunnar Mills     }
189469f35306SGunnar Mills 
189569f35306SGunnar Mills     crow::connections::systemBus->async_method_call(
18965e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec) {
189769f35306SGunnar Mills         if (ec)
189869f35306SGunnar Mills         {
189969f35306SGunnar Mills             messages::internalError(aResp->res);
190069f35306SGunnar Mills             return;
190169f35306SGunnar Mills         }
190269f35306SGunnar Mills         },
190369f35306SGunnar Mills         "xyz.openbmc_project.Settings",
190469f35306SGunnar Mills         "/xyz/openbmc_project/control/host0/auto_reboot",
190569f35306SGunnar Mills         "org.freedesktop.DBus.Properties", "Set",
190669f35306SGunnar Mills         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
1907168e20c1SEd Tanous         dbus::utility::DbusVariantType(autoRebootEnabled));
190869f35306SGunnar Mills }
190969f35306SGunnar Mills 
191069f35306SGunnar Mills /**
1911c6a620f2SGeorge Liu  * @brief Sets power restore policy properties.
1912c6a620f2SGeorge Liu  *
1913c6a620f2SGeorge Liu  * @param[in] aResp   Shared pointer for generating response message.
1914c6a620f2SGeorge Liu  * @param[in] policy  power restore policy properties from request.
1915c6a620f2SGeorge Liu  *
1916c6a620f2SGeorge Liu  * @return None.
1917c6a620f2SGeorge Liu  */
19188d1b46d7Szhanghch05 inline void
19198d1b46d7Szhanghch05     setPowerRestorePolicy(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
19204e69c904SGunnar Mills                           const std::string& policy)
1921c6a620f2SGeorge Liu {
1922c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Set power restore policy.";
1923c6a620f2SGeorge Liu 
1924c6a620f2SGeorge Liu     const boost::container::flat_map<std::string, std::string> policyMaps = {
19250fda0f12SGeorge Liu         {"AlwaysOn",
19260fda0f12SGeorge Liu          "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn"},
19270fda0f12SGeorge Liu         {"AlwaysOff",
19280fda0f12SGeorge Liu          "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff"},
19290fda0f12SGeorge Liu         {"LastState",
19300fda0f12SGeorge Liu          "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore"}};
1931c6a620f2SGeorge Liu 
1932c6a620f2SGeorge Liu     std::string powerRestorPolicy;
1933c6a620f2SGeorge Liu 
19344e69c904SGunnar Mills     auto policyMapsIt = policyMaps.find(policy);
1935c6a620f2SGeorge Liu     if (policyMapsIt == policyMaps.end())
1936c6a620f2SGeorge Liu     {
19374e69c904SGunnar Mills         messages::propertyValueNotInList(aResp->res, policy,
19384e69c904SGunnar Mills                                          "PowerRestorePolicy");
1939c6a620f2SGeorge Liu         return;
1940c6a620f2SGeorge Liu     }
1941c6a620f2SGeorge Liu 
1942c6a620f2SGeorge Liu     powerRestorPolicy = policyMapsIt->second;
1943c6a620f2SGeorge Liu 
1944c6a620f2SGeorge Liu     crow::connections::systemBus->async_method_call(
19455e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec) {
1946c6a620f2SGeorge Liu         if (ec)
1947c6a620f2SGeorge Liu         {
1948c6a620f2SGeorge Liu             messages::internalError(aResp->res);
1949c6a620f2SGeorge Liu             return;
1950c6a620f2SGeorge Liu         }
1951c6a620f2SGeorge Liu         },
1952c6a620f2SGeorge Liu         "xyz.openbmc_project.Settings",
1953c6a620f2SGeorge Liu         "/xyz/openbmc_project/control/host0/power_restore_policy",
1954c6a620f2SGeorge Liu         "org.freedesktop.DBus.Properties", "Set",
1955c6a620f2SGeorge Liu         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
1956168e20c1SEd Tanous         dbus::utility::DbusVariantType(powerRestorPolicy));
1957c6a620f2SGeorge Liu }
1958c6a620f2SGeorge Liu 
1959a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
1960a6349918SAppaRao Puli /**
1961a6349918SAppaRao Puli  * @brief Retrieves provisioning status
1962a6349918SAppaRao Puli  *
1963a6349918SAppaRao Puli  * @param[in] aResp     Shared pointer for completing asynchronous calls.
1964a6349918SAppaRao Puli  *
1965a6349918SAppaRao Puli  * @return None.
1966a6349918SAppaRao Puli  */
19678d1b46d7Szhanghch05 inline void getProvisioningStatus(std::shared_ptr<bmcweb::AsyncResp> aResp)
1968a6349918SAppaRao Puli {
1969a6349918SAppaRao Puli     BMCWEB_LOG_DEBUG << "Get OEM information.";
1970bc1d29deSKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1971bc1d29deSKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.PFR.Manager",
1972bc1d29deSKrzysztof Grobelny         "/xyz/openbmc_project/pfr", "xyz.openbmc_project.PFR.Attributes",
19735e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec,
1974b9d36b47SEd Tanous                 const dbus::utility::DBusPropertiesMap& propertiesList) {
1975b99fb1a9SAppaRao Puli         nlohmann::json& oemPFR =
1976b99fb1a9SAppaRao Puli             aResp->res.jsonValue["Oem"]["OpenBmc"]["FirmwareProvisioning"];
197750626f4fSJames Feist         aResp->res.jsonValue["Oem"]["OpenBmc"]["@odata.type"] =
197850626f4fSJames Feist             "#OemComputerSystem.OpenBmc";
197950626f4fSJames Feist         oemPFR["@odata.type"] = "#OemComputerSystem.FirmwareProvisioning";
198050626f4fSJames Feist 
1981a6349918SAppaRao Puli         if (ec)
1982a6349918SAppaRao Puli         {
1983a6349918SAppaRao Puli             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1984b99fb1a9SAppaRao Puli             // not an error, don't have to have the interface
1985b99fb1a9SAppaRao Puli             oemPFR["ProvisioningStatus"] = "NotProvisioned";
1986a6349918SAppaRao Puli             return;
1987a6349918SAppaRao Puli         }
1988a6349918SAppaRao Puli 
1989a6349918SAppaRao Puli         const bool* provState = nullptr;
1990a6349918SAppaRao Puli         const bool* lockState = nullptr;
1991bc1d29deSKrzysztof Grobelny 
1992bc1d29deSKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
19930d4befa8SJiaqing Zhao             dbus_utils::UnpackErrorPrinter(), propertiesList, "UfmProvisioned",
19940d4befa8SJiaqing Zhao             provState, "UfmLocked", lockState);
1995bc1d29deSKrzysztof Grobelny 
1996bc1d29deSKrzysztof Grobelny         if (!success)
1997a6349918SAppaRao Puli         {
1998bc1d29deSKrzysztof Grobelny             messages::internalError(aResp->res);
1999bc1d29deSKrzysztof Grobelny             return;
2000a6349918SAppaRao Puli         }
2001a6349918SAppaRao Puli 
2002a6349918SAppaRao Puli         if ((provState == nullptr) || (lockState == nullptr))
2003a6349918SAppaRao Puli         {
2004a6349918SAppaRao Puli             BMCWEB_LOG_DEBUG << "Unable to get PFR attributes.";
2005a6349918SAppaRao Puli             messages::internalError(aResp->res);
2006a6349918SAppaRao Puli             return;
2007a6349918SAppaRao Puli         }
2008a6349918SAppaRao Puli 
2009a6349918SAppaRao Puli         if (*provState == true)
2010a6349918SAppaRao Puli         {
2011a6349918SAppaRao Puli             if (*lockState == true)
2012a6349918SAppaRao Puli             {
2013a6349918SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "ProvisionedAndLocked";
2014a6349918SAppaRao Puli             }
2015a6349918SAppaRao Puli             else
2016a6349918SAppaRao Puli             {
2017a6349918SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "ProvisionedButNotLocked";
2018a6349918SAppaRao Puli             }
2019a6349918SAppaRao Puli         }
2020a6349918SAppaRao Puli         else
2021a6349918SAppaRao Puli         {
2022a6349918SAppaRao Puli             oemPFR["ProvisioningStatus"] = "NotProvisioned";
2023a6349918SAppaRao Puli         }
2024bc1d29deSKrzysztof Grobelny         });
2025a6349918SAppaRao Puli }
2026a6349918SAppaRao Puli #endif
2027a6349918SAppaRao Puli 
2028491d8ee7SSantosh Puranik /**
20293a2d0424SChris Cain  * @brief Translate the PowerMode to a response message.
20303a2d0424SChris Cain  *
20313a2d0424SChris Cain  * @param[in] aResp  Shared pointer for generating response message.
20323a2d0424SChris Cain  * @param[in] modeValue  PowerMode value to be translated
20333a2d0424SChris Cain  *
20343a2d0424SChris Cain  * @return None.
20353a2d0424SChris Cain  */
20363a2d0424SChris Cain inline void translatePowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
20373a2d0424SChris Cain                                const std::string& modeValue)
20383a2d0424SChris Cain {
20390fda0f12SGeorge Liu     if (modeValue == "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static")
20403a2d0424SChris Cain     {
20413a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "Static";
20423a2d0424SChris Cain     }
20430fda0f12SGeorge Liu     else if (
20440fda0f12SGeorge Liu         modeValue ==
20450fda0f12SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance")
20463a2d0424SChris Cain     {
20473a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "MaximumPerformance";
20483a2d0424SChris Cain     }
20490fda0f12SGeorge Liu     else if (modeValue ==
20500fda0f12SGeorge Liu              "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving")
20513a2d0424SChris Cain     {
20523a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "PowerSaving";
20533a2d0424SChris Cain     }
20540fda0f12SGeorge Liu     else if (modeValue ==
20550fda0f12SGeorge Liu              "xyz.openbmc_project.Control.Power.Mode.PowerMode.OEM")
20563a2d0424SChris Cain     {
20573a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "OEM";
20583a2d0424SChris Cain     }
20593a2d0424SChris Cain     else
20603a2d0424SChris Cain     {
20613a2d0424SChris Cain         // Any other values would be invalid
20623a2d0424SChris Cain         BMCWEB_LOG_DEBUG << "PowerMode value was not valid: " << modeValue;
20633a2d0424SChris Cain         messages::internalError(aResp->res);
20643a2d0424SChris Cain     }
20653a2d0424SChris Cain }
20663a2d0424SChris Cain 
20673a2d0424SChris Cain /**
20683a2d0424SChris Cain  * @brief Retrieves system power mode
20693a2d0424SChris Cain  *
20703a2d0424SChris Cain  * @param[in] aResp  Shared pointer for generating response message.
20713a2d0424SChris Cain  *
20723a2d0424SChris Cain  * @return None.
20733a2d0424SChris Cain  */
20743a2d0424SChris Cain inline void getPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
20753a2d0424SChris Cain {
20763a2d0424SChris Cain     BMCWEB_LOG_DEBUG << "Get power mode.";
20773a2d0424SChris Cain 
20783a2d0424SChris Cain     // Get Power Mode object path:
2079e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2080e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode"};
2081e99073f5SGeorge Liu     dbus::utility::getSubTree(
2082e99073f5SGeorge Liu         "/", 0, interfaces,
2083e99073f5SGeorge Liu         [aResp](const boost::system::error_code& ec,
2084b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
20853a2d0424SChris Cain         if (ec)
20863a2d0424SChris Cain         {
2087002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on Power.Mode GetSubTree "
2088002d39b4SEd Tanous                              << ec;
20893a2d0424SChris Cain             // This is an optional D-Bus object so just return if
20903a2d0424SChris Cain             // error occurs
20913a2d0424SChris Cain             return;
20923a2d0424SChris Cain         }
20933a2d0424SChris Cain         if (subtree.empty())
20943a2d0424SChris Cain         {
20953a2d0424SChris Cain             // As noted above, this is an optional interface so just return
20963a2d0424SChris Cain             // if there is no instance found
20973a2d0424SChris Cain             return;
20983a2d0424SChris Cain         }
20993a2d0424SChris Cain         if (subtree.size() > 1)
21003a2d0424SChris Cain         {
21013a2d0424SChris Cain             // More then one PowerMode object is not supported and is an
21023a2d0424SChris Cain             // error
21033a2d0424SChris Cain             BMCWEB_LOG_DEBUG
21043a2d0424SChris Cain                 << "Found more than 1 system D-Bus Power.Mode objects: "
21053a2d0424SChris Cain                 << subtree.size();
21063a2d0424SChris Cain             messages::internalError(aResp->res);
21073a2d0424SChris Cain             return;
21083a2d0424SChris Cain         }
21093a2d0424SChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
21103a2d0424SChris Cain         {
21113a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode mapper error!";
21123a2d0424SChris Cain             messages::internalError(aResp->res);
21133a2d0424SChris Cain             return;
21143a2d0424SChris Cain         }
21153a2d0424SChris Cain         const std::string& path = subtree[0].first;
21163a2d0424SChris Cain         const std::string& service = subtree[0].second.begin()->first;
21173a2d0424SChris Cain         if (service.empty())
21183a2d0424SChris Cain         {
21193a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode service mapper error!";
21203a2d0424SChris Cain             messages::internalError(aResp->res);
21213a2d0424SChris Cain             return;
21223a2d0424SChris Cain         }
21233a2d0424SChris Cain         // Valid Power Mode object found, now read the current value
21241e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
21251e1e598dSJonathan Doman             *crow::connections::systemBus, service, path,
21261e1e598dSJonathan Doman             "xyz.openbmc_project.Control.Power.Mode", "PowerMode",
21275e7e2dc5SEd Tanous             [aResp](const boost::system::error_code& ec2,
21281e1e598dSJonathan Doman                     const std::string& pmode) {
21298a592810SEd Tanous             if (ec2)
21303a2d0424SChris Cain             {
2131002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error on PowerMode Get: "
21328a592810SEd Tanous                                  << ec2;
21333a2d0424SChris Cain                 messages::internalError(aResp->res);
21343a2d0424SChris Cain                 return;
21353a2d0424SChris Cain             }
21363a2d0424SChris Cain 
2137002d39b4SEd Tanous             aResp->res.jsonValue["PowerMode@Redfish.AllowableValues"] = {
2138002d39b4SEd Tanous                 "Static", "MaximumPerformance", "PowerSaving"};
21393a2d0424SChris Cain 
21401e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Current power mode: " << pmode;
21411e1e598dSJonathan Doman             translatePowerMode(aResp, pmode);
21421e1e598dSJonathan Doman             });
2143e99073f5SGeorge Liu         });
21443a2d0424SChris Cain }
21453a2d0424SChris Cain 
21463a2d0424SChris Cain /**
21473a2d0424SChris Cain  * @brief Validate the specified mode is valid and return the PowerMode
21483a2d0424SChris Cain  * name associated with that string
21493a2d0424SChris Cain  *
21503a2d0424SChris Cain  * @param[in] aResp   Shared pointer for generating response message.
21513a2d0424SChris Cain  * @param[in] modeString  String representing the desired PowerMode
21523a2d0424SChris Cain  *
21533a2d0424SChris Cain  * @return PowerMode value or empty string if mode is not valid
21543a2d0424SChris Cain  */
21553a2d0424SChris Cain inline std::string
21563a2d0424SChris Cain     validatePowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
21573a2d0424SChris Cain                       const std::string& modeString)
21583a2d0424SChris Cain {
21593a2d0424SChris Cain     std::string mode;
21603a2d0424SChris Cain 
21613a2d0424SChris Cain     if (modeString == "Static")
21623a2d0424SChris Cain     {
21633a2d0424SChris Cain         mode = "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static";
21643a2d0424SChris Cain     }
21653a2d0424SChris Cain     else if (modeString == "MaximumPerformance")
21663a2d0424SChris Cain     {
21670fda0f12SGeorge Liu         mode =
21680fda0f12SGeorge Liu             "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance";
21693a2d0424SChris Cain     }
21703a2d0424SChris Cain     else if (modeString == "PowerSaving")
21713a2d0424SChris Cain     {
21723a2d0424SChris Cain         mode = "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving";
21733a2d0424SChris Cain     }
21743a2d0424SChris Cain     else
21753a2d0424SChris Cain     {
21763a2d0424SChris Cain         messages::propertyValueNotInList(aResp->res, modeString, "PowerMode");
21773a2d0424SChris Cain     }
21783a2d0424SChris Cain     return mode;
21793a2d0424SChris Cain }
21803a2d0424SChris Cain 
21813a2d0424SChris Cain /**
21823a2d0424SChris Cain  * @brief Sets system power mode.
21833a2d0424SChris Cain  *
21843a2d0424SChris Cain  * @param[in] aResp   Shared pointer for generating response message.
21853a2d0424SChris Cain  * @param[in] pmode   System power mode from request.
21863a2d0424SChris Cain  *
21873a2d0424SChris Cain  * @return None.
21883a2d0424SChris Cain  */
21893a2d0424SChris Cain inline void setPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
21903a2d0424SChris Cain                          const std::string& pmode)
21913a2d0424SChris Cain {
21923a2d0424SChris Cain     BMCWEB_LOG_DEBUG << "Set power mode.";
21933a2d0424SChris Cain 
21943a2d0424SChris Cain     std::string powerMode = validatePowerMode(aResp, pmode);
21953a2d0424SChris Cain     if (powerMode.empty())
21963a2d0424SChris Cain     {
21973a2d0424SChris Cain         return;
21983a2d0424SChris Cain     }
21993a2d0424SChris Cain 
22003a2d0424SChris Cain     // Get Power Mode object path:
2201e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2202e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode"};
2203e99073f5SGeorge Liu     dbus::utility::getSubTree(
2204e99073f5SGeorge Liu         "/", 0, interfaces,
2205b9d36b47SEd Tanous         [aResp,
2206e99073f5SGeorge Liu          powerMode](const boost::system::error_code& ec,
2207b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
22083a2d0424SChris Cain         if (ec)
22093a2d0424SChris Cain         {
2210002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on Power.Mode GetSubTree "
2211002d39b4SEd Tanous                              << ec;
22123a2d0424SChris Cain             // This is an optional D-Bus object, but user attempted to patch
22133a2d0424SChris Cain             messages::internalError(aResp->res);
22143a2d0424SChris Cain             return;
22153a2d0424SChris Cain         }
22163a2d0424SChris Cain         if (subtree.empty())
22173a2d0424SChris Cain         {
22183a2d0424SChris Cain             // This is an optional D-Bus object, but user attempted to patch
22193a2d0424SChris Cain             messages::resourceNotFound(aResp->res, "ComputerSystem",
22203a2d0424SChris Cain                                        "PowerMode");
22213a2d0424SChris Cain             return;
22223a2d0424SChris Cain         }
22233a2d0424SChris Cain         if (subtree.size() > 1)
22243a2d0424SChris Cain         {
22253a2d0424SChris Cain             // More then one PowerMode object is not supported and is an
22263a2d0424SChris Cain             // error
22273a2d0424SChris Cain             BMCWEB_LOG_DEBUG
22283a2d0424SChris Cain                 << "Found more than 1 system D-Bus Power.Mode objects: "
22293a2d0424SChris Cain                 << subtree.size();
22303a2d0424SChris Cain             messages::internalError(aResp->res);
22313a2d0424SChris Cain             return;
22323a2d0424SChris Cain         }
22333a2d0424SChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
22343a2d0424SChris Cain         {
22353a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode mapper error!";
22363a2d0424SChris Cain             messages::internalError(aResp->res);
22373a2d0424SChris Cain             return;
22383a2d0424SChris Cain         }
22393a2d0424SChris Cain         const std::string& path = subtree[0].first;
22403a2d0424SChris Cain         const std::string& service = subtree[0].second.begin()->first;
22413a2d0424SChris Cain         if (service.empty())
22423a2d0424SChris Cain         {
22433a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode service mapper error!";
22443a2d0424SChris Cain             messages::internalError(aResp->res);
22453a2d0424SChris Cain             return;
22463a2d0424SChris Cain         }
22473a2d0424SChris Cain 
22483a2d0424SChris Cain         BMCWEB_LOG_DEBUG << "Setting power mode(" << powerMode << ") -> "
22493a2d0424SChris Cain                          << path;
22503a2d0424SChris Cain 
22513a2d0424SChris Cain         // Set the Power Mode property
22523a2d0424SChris Cain         crow::connections::systemBus->async_method_call(
22535e7e2dc5SEd Tanous             [aResp](const boost::system::error_code& ec2) {
22548a592810SEd Tanous             if (ec2)
22553a2d0424SChris Cain             {
22563a2d0424SChris Cain                 messages::internalError(aResp->res);
22573a2d0424SChris Cain                 return;
22583a2d0424SChris Cain             }
22593a2d0424SChris Cain             },
22603a2d0424SChris Cain             service, path, "org.freedesktop.DBus.Properties", "Set",
22613a2d0424SChris Cain             "xyz.openbmc_project.Control.Power.Mode", "PowerMode",
2262168e20c1SEd Tanous             dbus::utility::DbusVariantType(powerMode));
2263e99073f5SGeorge Liu         });
22643a2d0424SChris Cain }
22653a2d0424SChris Cain 
22663a2d0424SChris Cain /**
226751709ffdSYong Li  * @brief Translates watchdog timeout action DBUS property value to redfish.
226851709ffdSYong Li  *
226951709ffdSYong Li  * @param[in] dbusAction    The watchdog timeout action in D-BUS.
227051709ffdSYong Li  *
227151709ffdSYong Li  * @return Returns as a string, the timeout action in Redfish terms. If
227251709ffdSYong Li  * translation cannot be done, returns an empty string.
227351709ffdSYong Li  */
227423a21a1cSEd Tanous inline std::string dbusToRfWatchdogAction(const std::string& dbusAction)
227551709ffdSYong Li {
227651709ffdSYong Li     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.None")
227751709ffdSYong Li     {
227851709ffdSYong Li         return "None";
227951709ffdSYong Li     }
22803174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.HardReset")
228151709ffdSYong Li     {
228251709ffdSYong Li         return "ResetSystem";
228351709ffdSYong Li     }
22843174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerOff")
228551709ffdSYong Li     {
228651709ffdSYong Li         return "PowerDown";
228751709ffdSYong Li     }
22883174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerCycle")
228951709ffdSYong Li     {
229051709ffdSYong Li         return "PowerCycle";
229151709ffdSYong Li     }
229251709ffdSYong Li 
229351709ffdSYong Li     return "";
229451709ffdSYong Li }
229551709ffdSYong Li 
229651709ffdSYong Li /**
2297c45f0082SYong Li  *@brief Translates timeout action from Redfish to DBUS property value.
2298c45f0082SYong Li  *
2299c45f0082SYong Li  *@param[in] rfAction The timeout action in Redfish.
2300c45f0082SYong Li  *
2301c45f0082SYong Li  *@return Returns as a string, the time_out action as expected by DBUS.
2302c45f0082SYong Li  *If translation cannot be done, returns an empty string.
2303c45f0082SYong Li  */
2304c45f0082SYong Li 
230523a21a1cSEd Tanous inline std::string rfToDbusWDTTimeOutAct(const std::string& rfAction)
2306c45f0082SYong Li {
2307c45f0082SYong Li     if (rfAction == "None")
2308c45f0082SYong Li     {
2309c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.None";
2310c45f0082SYong Li     }
23113174e4dfSEd Tanous     if (rfAction == "PowerCycle")
2312c45f0082SYong Li     {
2313c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerCycle";
2314c45f0082SYong Li     }
23153174e4dfSEd Tanous     if (rfAction == "PowerDown")
2316c45f0082SYong Li     {
2317c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerOff";
2318c45f0082SYong Li     }
23193174e4dfSEd Tanous     if (rfAction == "ResetSystem")
2320c45f0082SYong Li     {
2321c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.HardReset";
2322c45f0082SYong Li     }
2323c45f0082SYong Li 
2324c45f0082SYong Li     return "";
2325c45f0082SYong Li }
2326c45f0082SYong Li 
2327c45f0082SYong Li /**
232851709ffdSYong Li  * @brief Retrieves host watchdog timer properties over DBUS
232951709ffdSYong Li  *
233051709ffdSYong Li  * @param[in] aResp     Shared pointer for completing asynchronous calls.
233151709ffdSYong Li  *
233251709ffdSYong Li  * @return None.
233351709ffdSYong Li  */
23348d1b46d7Szhanghch05 inline void
23358d1b46d7Szhanghch05     getHostWatchdogTimer(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
233651709ffdSYong Li {
233751709ffdSYong Li     BMCWEB_LOG_DEBUG << "Get host watchodg";
2338bc1d29deSKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
2339bc1d29deSKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.Watchdog",
2340bc1d29deSKrzysztof Grobelny         "/xyz/openbmc_project/watchdog/host0",
2341bc1d29deSKrzysztof Grobelny         "xyz.openbmc_project.State.Watchdog",
23425e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec,
2343b9d36b47SEd Tanous                 const dbus::utility::DBusPropertiesMap& properties) {
234451709ffdSYong Li         if (ec)
234551709ffdSYong Li         {
234651709ffdSYong Li             // watchdog service is stopped
234751709ffdSYong Li             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
234851709ffdSYong Li             return;
234951709ffdSYong Li         }
235051709ffdSYong Li 
235151709ffdSYong Li         BMCWEB_LOG_DEBUG << "Got " << properties.size() << " wdt prop.";
235251709ffdSYong Li 
235351709ffdSYong Li         nlohmann::json& hostWatchdogTimer =
235451709ffdSYong Li             aResp->res.jsonValue["HostWatchdogTimer"];
235551709ffdSYong Li 
235651709ffdSYong Li         // watchdog service is running/enabled
235751709ffdSYong Li         hostWatchdogTimer["Status"]["State"] = "Enabled";
235851709ffdSYong Li 
2359bc1d29deSKrzysztof Grobelny         const bool* enabled = nullptr;
2360bc1d29deSKrzysztof Grobelny         const std::string* expireAction = nullptr;
236151709ffdSYong Li 
2362bc1d29deSKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
2363bc1d29deSKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), properties, "Enabled", enabled,
2364bc1d29deSKrzysztof Grobelny             "ExpireAction", expireAction);
2365bc1d29deSKrzysztof Grobelny 
2366bc1d29deSKrzysztof Grobelny         if (!success)
236751709ffdSYong Li         {
236851709ffdSYong Li             messages::internalError(aResp->res);
2369601af5edSChicago Duan             return;
237051709ffdSYong Li         }
237151709ffdSYong Li 
2372bc1d29deSKrzysztof Grobelny         if (enabled != nullptr)
237351709ffdSYong Li         {
2374bc1d29deSKrzysztof Grobelny             hostWatchdogTimer["FunctionEnabled"] = *enabled;
237551709ffdSYong Li         }
237651709ffdSYong Li 
2377bc1d29deSKrzysztof Grobelny         if (expireAction != nullptr)
2378bc1d29deSKrzysztof Grobelny         {
2379bc1d29deSKrzysztof Grobelny             std::string action = dbusToRfWatchdogAction(*expireAction);
238051709ffdSYong Li             if (action.empty())
238151709ffdSYong Li             {
238251709ffdSYong Li                 messages::internalError(aResp->res);
2383601af5edSChicago Duan                 return;
238451709ffdSYong Li             }
238551709ffdSYong Li             hostWatchdogTimer["TimeoutAction"] = action;
238651709ffdSYong Li         }
2387bc1d29deSKrzysztof Grobelny         });
238851709ffdSYong Li }
238951709ffdSYong Li 
239051709ffdSYong Li /**
2391c45f0082SYong Li  * @brief Sets Host WatchDog Timer properties.
2392c45f0082SYong Li  *
2393c45f0082SYong Li  * @param[in] aResp      Shared pointer for generating response message.
2394c45f0082SYong Li  * @param[in] wdtEnable  The WDTimer Enable value (true/false) from incoming
2395c45f0082SYong Li  *                       RF request.
2396c45f0082SYong Li  * @param[in] wdtTimeOutAction The WDT Timeout action, from incoming RF request.
2397c45f0082SYong Li  *
2398c45f0082SYong Li  * @return None.
2399c45f0082SYong Li  */
24008d1b46d7Szhanghch05 inline void setWDTProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2401c45f0082SYong Li                              const std::optional<bool> wdtEnable,
2402c45f0082SYong Li                              const std::optional<std::string>& wdtTimeOutAction)
2403c45f0082SYong Li {
2404c45f0082SYong Li     BMCWEB_LOG_DEBUG << "Set host watchdog";
2405c45f0082SYong Li 
2406c45f0082SYong Li     if (wdtTimeOutAction)
2407c45f0082SYong Li     {
2408c45f0082SYong Li         std::string wdtTimeOutActStr = rfToDbusWDTTimeOutAct(*wdtTimeOutAction);
2409c45f0082SYong Li         // check if TimeOut Action is Valid
2410c45f0082SYong Li         if (wdtTimeOutActStr.empty())
2411c45f0082SYong Li         {
2412c45f0082SYong Li             BMCWEB_LOG_DEBUG << "Unsupported value for TimeoutAction: "
2413c45f0082SYong Li                              << *wdtTimeOutAction;
2414c45f0082SYong Li             messages::propertyValueNotInList(aResp->res, *wdtTimeOutAction,
2415c45f0082SYong Li                                              "TimeoutAction");
2416c45f0082SYong Li             return;
2417c45f0082SYong Li         }
2418c45f0082SYong Li 
2419c45f0082SYong Li         crow::connections::systemBus->async_method_call(
24205e7e2dc5SEd Tanous             [aResp](const boost::system::error_code& ec) {
2421c45f0082SYong Li             if (ec)
2422c45f0082SYong Li             {
2423c45f0082SYong Li                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
2424c45f0082SYong Li                 messages::internalError(aResp->res);
2425c45f0082SYong Li                 return;
2426c45f0082SYong Li             }
2427c45f0082SYong Li             },
2428c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
2429c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
2430c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
2431c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "ExpireAction",
2432168e20c1SEd Tanous             dbus::utility::DbusVariantType(wdtTimeOutActStr));
2433c45f0082SYong Li     }
2434c45f0082SYong Li 
2435c45f0082SYong Li     if (wdtEnable)
2436c45f0082SYong Li     {
2437c45f0082SYong Li         crow::connections::systemBus->async_method_call(
24385e7e2dc5SEd Tanous             [aResp](const boost::system::error_code& ec) {
2439c45f0082SYong Li             if (ec)
2440c45f0082SYong Li             {
2441c45f0082SYong Li                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
2442c45f0082SYong Li                 messages::internalError(aResp->res);
2443c45f0082SYong Li                 return;
2444c45f0082SYong Li             }
2445c45f0082SYong Li             },
2446c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
2447c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
2448c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
2449c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "Enabled",
2450168e20c1SEd Tanous             dbus::utility::DbusVariantType(*wdtEnable));
2451c45f0082SYong Li     }
2452c45f0082SYong Li }
2453c45f0082SYong Li 
245437bbf98cSChris Cain /**
245537bbf98cSChris Cain  * @brief Parse the Idle Power Saver properties into json
245637bbf98cSChris Cain  *
245737bbf98cSChris Cain  * @param[in] aResp     Shared pointer for completing asynchronous calls.
245837bbf98cSChris Cain  * @param[in] properties  IPS property data from DBus.
245937bbf98cSChris Cain  *
246037bbf98cSChris Cain  * @return true if successful
246137bbf98cSChris Cain  */
24621e5b7c88SJiaqing Zhao inline bool
24631e5b7c88SJiaqing Zhao     parseIpsProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
24641e5b7c88SJiaqing Zhao                        const dbus::utility::DBusPropertiesMap& properties)
246537bbf98cSChris Cain {
2466bc1d29deSKrzysztof Grobelny     const bool* enabled = nullptr;
2467bc1d29deSKrzysztof Grobelny     const uint8_t* enterUtilizationPercent = nullptr;
2468bc1d29deSKrzysztof Grobelny     const uint64_t* enterDwellTime = nullptr;
2469bc1d29deSKrzysztof Grobelny     const uint8_t* exitUtilizationPercent = nullptr;
2470bc1d29deSKrzysztof Grobelny     const uint64_t* exitDwellTime = nullptr;
2471bc1d29deSKrzysztof Grobelny 
2472bc1d29deSKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
2473bc1d29deSKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), properties, "Enabled", enabled,
24742661b72cSChris Cain         "EnterUtilizationPercent", enterUtilizationPercent, "EnterDwellTime",
24752661b72cSChris Cain         enterDwellTime, "ExitUtilizationPercent", exitUtilizationPercent,
24762661b72cSChris Cain         "ExitDwellTime", exitDwellTime);
2477bc1d29deSKrzysztof Grobelny 
2478bc1d29deSKrzysztof Grobelny     if (!success)
247937bbf98cSChris Cain     {
248037bbf98cSChris Cain         return false;
248137bbf98cSChris Cain     }
2482bc1d29deSKrzysztof Grobelny 
2483bc1d29deSKrzysztof Grobelny     if (enabled != nullptr)
248437bbf98cSChris Cain     {
2485bc1d29deSKrzysztof Grobelny         aResp->res.jsonValue["IdlePowerSaver"]["Enabled"] = *enabled;
248637bbf98cSChris Cain     }
2487bc1d29deSKrzysztof Grobelny 
2488bc1d29deSKrzysztof Grobelny     if (enterUtilizationPercent != nullptr)
248937bbf98cSChris Cain     {
2490bc1d29deSKrzysztof Grobelny         aResp->res.jsonValue["IdlePowerSaver"]["EnterUtilizationPercent"] =
2491bc1d29deSKrzysztof Grobelny             *enterUtilizationPercent;
249237bbf98cSChris Cain     }
2493bc1d29deSKrzysztof Grobelny 
2494bc1d29deSKrzysztof Grobelny     if (enterDwellTime != nullptr)
2495bc1d29deSKrzysztof Grobelny     {
2496bc1d29deSKrzysztof Grobelny         const std::chrono::duration<uint64_t, std::milli> ms(*enterDwellTime);
249737bbf98cSChris Cain         aResp->res.jsonValue["IdlePowerSaver"]["EnterDwellTimeSeconds"] =
249837bbf98cSChris Cain             std::chrono::duration_cast<std::chrono::duration<uint64_t>>(ms)
249937bbf98cSChris Cain                 .count();
250037bbf98cSChris Cain     }
2501bc1d29deSKrzysztof Grobelny 
2502bc1d29deSKrzysztof Grobelny     if (exitUtilizationPercent != nullptr)
250337bbf98cSChris Cain     {
2504bc1d29deSKrzysztof Grobelny         aResp->res.jsonValue["IdlePowerSaver"]["ExitUtilizationPercent"] =
2505bc1d29deSKrzysztof Grobelny             *exitUtilizationPercent;
250637bbf98cSChris Cain     }
2507bc1d29deSKrzysztof Grobelny 
2508bc1d29deSKrzysztof Grobelny     if (exitDwellTime != nullptr)
250937bbf98cSChris Cain     {
2510bc1d29deSKrzysztof Grobelny         const std::chrono::duration<uint64_t, std::milli> ms(*exitDwellTime);
251137bbf98cSChris Cain         aResp->res.jsonValue["IdlePowerSaver"]["ExitDwellTimeSeconds"] =
251237bbf98cSChris Cain             std::chrono::duration_cast<std::chrono::duration<uint64_t>>(ms)
251337bbf98cSChris Cain                 .count();
251437bbf98cSChris Cain     }
251537bbf98cSChris Cain 
251637bbf98cSChris Cain     return true;
251737bbf98cSChris Cain }
251837bbf98cSChris Cain 
251937bbf98cSChris Cain /**
252037bbf98cSChris Cain  * @brief Retrieves host watchdog timer properties over DBUS
252137bbf98cSChris Cain  *
252237bbf98cSChris Cain  * @param[in] aResp     Shared pointer for completing asynchronous calls.
252337bbf98cSChris Cain  *
252437bbf98cSChris Cain  * @return None.
252537bbf98cSChris Cain  */
252637bbf98cSChris Cain inline void getIdlePowerSaver(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
252737bbf98cSChris Cain {
252837bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "Get idle power saver parameters";
252937bbf98cSChris Cain 
253037bbf98cSChris Cain     // Get IdlePowerSaver object path:
2531e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2532e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.IdlePowerSaver"};
2533e99073f5SGeorge Liu     dbus::utility::getSubTree(
2534e99073f5SGeorge Liu         "/", 0, interfaces,
2535e99073f5SGeorge Liu         [aResp](const boost::system::error_code& ec,
2536b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
253737bbf98cSChris Cain         if (ec)
253837bbf98cSChris Cain         {
253937bbf98cSChris Cain             BMCWEB_LOG_DEBUG
254037bbf98cSChris Cain                 << "DBUS response error on Power.IdlePowerSaver GetSubTree "
254137bbf98cSChris Cain                 << ec;
254237bbf98cSChris Cain             messages::internalError(aResp->res);
254337bbf98cSChris Cain             return;
254437bbf98cSChris Cain         }
254537bbf98cSChris Cain         if (subtree.empty())
254637bbf98cSChris Cain         {
254737bbf98cSChris Cain             // This is an optional interface so just return
254837bbf98cSChris Cain             // if there is no instance found
254937bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "No instances found";
255037bbf98cSChris Cain             return;
255137bbf98cSChris Cain         }
255237bbf98cSChris Cain         if (subtree.size() > 1)
255337bbf98cSChris Cain         {
255437bbf98cSChris Cain             // More then one PowerIdlePowerSaver object is not supported and
255537bbf98cSChris Cain             // is an error
255637bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "Found more than 1 system D-Bus "
255737bbf98cSChris Cain                                 "Power.IdlePowerSaver objects: "
255837bbf98cSChris Cain                              << subtree.size();
255937bbf98cSChris Cain             messages::internalError(aResp->res);
256037bbf98cSChris Cain             return;
256137bbf98cSChris Cain         }
256237bbf98cSChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
256337bbf98cSChris Cain         {
256437bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver mapper error!";
256537bbf98cSChris Cain             messages::internalError(aResp->res);
256637bbf98cSChris Cain             return;
256737bbf98cSChris Cain         }
256837bbf98cSChris Cain         const std::string& path = subtree[0].first;
256937bbf98cSChris Cain         const std::string& service = subtree[0].second.begin()->first;
257037bbf98cSChris Cain         if (service.empty())
257137bbf98cSChris Cain         {
2572002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver service mapper error!";
257337bbf98cSChris Cain             messages::internalError(aResp->res);
257437bbf98cSChris Cain             return;
257537bbf98cSChris Cain         }
257637bbf98cSChris Cain 
257737bbf98cSChris Cain         // Valid IdlePowerSaver object found, now read the current values
2578bc1d29deSKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
2579bc1d29deSKrzysztof Grobelny             *crow::connections::systemBus, service, path,
2580bc1d29deSKrzysztof Grobelny             "xyz.openbmc_project.Control.Power.IdlePowerSaver",
25815e7e2dc5SEd Tanous             [aResp](const boost::system::error_code& ec2,
25821e5b7c88SJiaqing Zhao                     const dbus::utility::DBusPropertiesMap& properties) {
25838a592810SEd Tanous             if (ec2)
258437bbf98cSChris Cain             {
258537bbf98cSChris Cain                 BMCWEB_LOG_ERROR
25868a592810SEd Tanous                     << "DBUS response error on IdlePowerSaver GetAll: " << ec2;
258737bbf98cSChris Cain                 messages::internalError(aResp->res);
258837bbf98cSChris Cain                 return;
258937bbf98cSChris Cain             }
259037bbf98cSChris Cain 
2591e05aec50SEd Tanous             if (!parseIpsProperties(aResp, properties))
259237bbf98cSChris Cain             {
259337bbf98cSChris Cain                 messages::internalError(aResp->res);
259437bbf98cSChris Cain                 return;
259537bbf98cSChris Cain             }
2596bc1d29deSKrzysztof Grobelny             });
2597e99073f5SGeorge Liu         });
259837bbf98cSChris Cain 
259937bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "EXIT: Get idle power saver parameters";
260037bbf98cSChris Cain }
260137bbf98cSChris Cain 
260237bbf98cSChris Cain /**
260337bbf98cSChris Cain  * @brief Sets Idle Power Saver properties.
260437bbf98cSChris Cain  *
260537bbf98cSChris Cain  * @param[in] aResp      Shared pointer for generating response message.
260637bbf98cSChris Cain  * @param[in] ipsEnable  The IPS Enable value (true/false) from incoming
260737bbf98cSChris Cain  *                       RF request.
260837bbf98cSChris Cain  * @param[in] ipsEnterUtil The utilization limit to enter idle state.
260937bbf98cSChris Cain  * @param[in] ipsEnterTime The time the utilization must be below ipsEnterUtil
261037bbf98cSChris Cain  * before entering idle state.
261137bbf98cSChris Cain  * @param[in] ipsExitUtil The utilization limit when exiting idle state.
261237bbf98cSChris Cain  * @param[in] ipsExitTime The time the utilization must be above ipsExutUtil
261337bbf98cSChris Cain  * before exiting idle state
261437bbf98cSChris Cain  *
261537bbf98cSChris Cain  * @return None.
261637bbf98cSChris Cain  */
261737bbf98cSChris Cain inline void setIdlePowerSaver(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
261837bbf98cSChris Cain                               const std::optional<bool> ipsEnable,
261937bbf98cSChris Cain                               const std::optional<uint8_t> ipsEnterUtil,
262037bbf98cSChris Cain                               const std::optional<uint64_t> ipsEnterTime,
262137bbf98cSChris Cain                               const std::optional<uint8_t> ipsExitUtil,
262237bbf98cSChris Cain                               const std::optional<uint64_t> ipsExitTime)
262337bbf98cSChris Cain {
262437bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "Set idle power saver properties";
262537bbf98cSChris Cain 
262637bbf98cSChris Cain     // Get IdlePowerSaver object path:
2627e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2628e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Power.IdlePowerSaver"};
2629e99073f5SGeorge Liu     dbus::utility::getSubTree(
2630e99073f5SGeorge Liu         "/", 0, interfaces,
263137bbf98cSChris Cain         [aResp, ipsEnable, ipsEnterUtil, ipsEnterTime, ipsExitUtil,
2632e99073f5SGeorge Liu          ipsExitTime](const boost::system::error_code& ec,
2633b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
263437bbf98cSChris Cain         if (ec)
263537bbf98cSChris Cain         {
263637bbf98cSChris Cain             BMCWEB_LOG_DEBUG
263737bbf98cSChris Cain                 << "DBUS response error on Power.IdlePowerSaver GetSubTree "
263837bbf98cSChris Cain                 << ec;
263937bbf98cSChris Cain             messages::internalError(aResp->res);
264037bbf98cSChris Cain             return;
264137bbf98cSChris Cain         }
264237bbf98cSChris Cain         if (subtree.empty())
264337bbf98cSChris Cain         {
264437bbf98cSChris Cain             // This is an optional D-Bus object, but user attempted to patch
264537bbf98cSChris Cain             messages::resourceNotFound(aResp->res, "ComputerSystem",
264637bbf98cSChris Cain                                        "IdlePowerSaver");
264737bbf98cSChris Cain             return;
264837bbf98cSChris Cain         }
264937bbf98cSChris Cain         if (subtree.size() > 1)
265037bbf98cSChris Cain         {
265137bbf98cSChris Cain             // More then one PowerIdlePowerSaver object is not supported and
265237bbf98cSChris Cain             // is an error
26530fda0f12SGeorge Liu             BMCWEB_LOG_DEBUG
26540fda0f12SGeorge Liu                 << "Found more than 1 system D-Bus Power.IdlePowerSaver objects: "
265537bbf98cSChris Cain                 << subtree.size();
265637bbf98cSChris Cain             messages::internalError(aResp->res);
265737bbf98cSChris Cain             return;
265837bbf98cSChris Cain         }
265937bbf98cSChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
266037bbf98cSChris Cain         {
266137bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver mapper error!";
266237bbf98cSChris Cain             messages::internalError(aResp->res);
266337bbf98cSChris Cain             return;
266437bbf98cSChris Cain         }
266537bbf98cSChris Cain         const std::string& path = subtree[0].first;
266637bbf98cSChris Cain         const std::string& service = subtree[0].second.begin()->first;
266737bbf98cSChris Cain         if (service.empty())
266837bbf98cSChris Cain         {
2669002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver service mapper error!";
267037bbf98cSChris Cain             messages::internalError(aResp->res);
267137bbf98cSChris Cain             return;
267237bbf98cSChris Cain         }
267337bbf98cSChris Cain 
267437bbf98cSChris Cain         // Valid Power IdlePowerSaver object found, now set any values that
267537bbf98cSChris Cain         // need to be updated
267637bbf98cSChris Cain 
267737bbf98cSChris Cain         if (ipsEnable)
267837bbf98cSChris Cain         {
267937bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
26805e7e2dc5SEd Tanous                 [aResp](const boost::system::error_code& ec2) {
26818a592810SEd Tanous                 if (ec2)
268237bbf98cSChris Cain                 {
26838a592810SEd Tanous                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
268437bbf98cSChris Cain                     messages::internalError(aResp->res);
268537bbf98cSChris Cain                     return;
268637bbf98cSChris Cain                 }
268737bbf98cSChris Cain                 },
268837bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
2689002d39b4SEd Tanous                 "xyz.openbmc_project.Control.Power.IdlePowerSaver", "Enabled",
2690002d39b4SEd Tanous                 dbus::utility::DbusVariantType(*ipsEnable));
269137bbf98cSChris Cain         }
269237bbf98cSChris Cain         if (ipsEnterUtil)
269337bbf98cSChris Cain         {
269437bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
26955e7e2dc5SEd Tanous                 [aResp](const boost::system::error_code& ec2) {
26968a592810SEd Tanous                 if (ec2)
269737bbf98cSChris Cain                 {
26988a592810SEd Tanous                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
269937bbf98cSChris Cain                     messages::internalError(aResp->res);
270037bbf98cSChris Cain                     return;
270137bbf98cSChris Cain                 }
270237bbf98cSChris Cain                 },
270337bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
270437bbf98cSChris Cain                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
270537bbf98cSChris Cain                 "EnterUtilizationPercent",
2706168e20c1SEd Tanous                 dbus::utility::DbusVariantType(*ipsEnterUtil));
270737bbf98cSChris Cain         }
270837bbf98cSChris Cain         if (ipsEnterTime)
270937bbf98cSChris Cain         {
271037bbf98cSChris Cain             // Convert from seconds into milliseconds for DBus
271137bbf98cSChris Cain             const uint64_t timeMilliseconds = *ipsEnterTime * 1000;
271237bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
27135e7e2dc5SEd Tanous                 [aResp](const boost::system::error_code& ec2) {
27148a592810SEd Tanous                 if (ec2)
271537bbf98cSChris Cain                 {
27168a592810SEd Tanous                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
271737bbf98cSChris Cain                     messages::internalError(aResp->res);
271837bbf98cSChris Cain                     return;
271937bbf98cSChris Cain                 }
272037bbf98cSChris Cain                 },
272137bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
272237bbf98cSChris Cain                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2723168e20c1SEd Tanous                 "EnterDwellTime",
2724168e20c1SEd Tanous                 dbus::utility::DbusVariantType(timeMilliseconds));
272537bbf98cSChris Cain         }
272637bbf98cSChris Cain         if (ipsExitUtil)
272737bbf98cSChris Cain         {
272837bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
27295e7e2dc5SEd Tanous                 [aResp](const boost::system::error_code& ec2) {
27308a592810SEd Tanous                 if (ec2)
273137bbf98cSChris Cain                 {
27328a592810SEd Tanous                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
273337bbf98cSChris Cain                     messages::internalError(aResp->res);
273437bbf98cSChris Cain                     return;
273537bbf98cSChris Cain                 }
273637bbf98cSChris Cain                 },
273737bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
273837bbf98cSChris Cain                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
273937bbf98cSChris Cain                 "ExitUtilizationPercent",
2740168e20c1SEd Tanous                 dbus::utility::DbusVariantType(*ipsExitUtil));
274137bbf98cSChris Cain         }
274237bbf98cSChris Cain         if (ipsExitTime)
274337bbf98cSChris Cain         {
274437bbf98cSChris Cain             // Convert from seconds into milliseconds for DBus
274537bbf98cSChris Cain             const uint64_t timeMilliseconds = *ipsExitTime * 1000;
274637bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
27475e7e2dc5SEd Tanous                 [aResp](const boost::system::error_code& ec2) {
27488a592810SEd Tanous                 if (ec2)
274937bbf98cSChris Cain                 {
27508a592810SEd Tanous                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
275137bbf98cSChris Cain                     messages::internalError(aResp->res);
275237bbf98cSChris Cain                     return;
275337bbf98cSChris Cain                 }
275437bbf98cSChris Cain                 },
275537bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
275637bbf98cSChris Cain                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2757168e20c1SEd Tanous                 "ExitDwellTime",
2758168e20c1SEd Tanous                 dbus::utility::DbusVariantType(timeMilliseconds));
275937bbf98cSChris Cain         }
2760e99073f5SGeorge Liu         });
276137bbf98cSChris Cain 
276237bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "EXIT: Set idle power saver parameters";
276337bbf98cSChris Cain }
276437bbf98cSChris Cain 
2765dd60b9edSEd Tanous inline void handleComputerSystemHead(
2766dd60b9edSEd Tanous     crow::App& app, const crow::Request& req,
2767dd60b9edSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2768dd60b9edSEd Tanous {
2769dd60b9edSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2770dd60b9edSEd Tanous     {
2771dd60b9edSEd Tanous         return;
2772dd60b9edSEd Tanous     }
2773dd60b9edSEd Tanous     asyncResp->res.addHeader(
2774dd60b9edSEd Tanous         boost::beast::http::field::link,
2775dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystemCollection/ComputerSystemCollection.json>; rel=describedby");
2776dd60b9edSEd Tanous }
2777dd60b9edSEd Tanous 
2778c45f0082SYong Li /**
2779c5b2abe0SLewanczyk, Dawid  * SystemsCollection derived class for delivering ComputerSystems Collection
2780c5b2abe0SLewanczyk, Dawid  * Schema
2781c5b2abe0SLewanczyk, Dawid  */
27827e860f15SJohn Edward Broadbent inline void requestRoutesSystemsCollection(App& app)
27831abe55efSEd Tanous {
27847e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/")
2785dd60b9edSEd Tanous         .privileges(redfish::privileges::headComputerSystemCollection)
2786dd60b9edSEd Tanous         .methods(boost::beast::http::verb::head)(
2787dd60b9edSEd Tanous             std::bind_front(handleComputerSystemHead, std::ref(app)));
2788dd60b9edSEd Tanous 
2789dd60b9edSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/")
2790ed398213SEd Tanous         .privileges(redfish::privileges::getComputerSystemCollection)
27917e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2792f4c99e70SEd Tanous             [&app](const crow::Request& req,
27937e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
27943ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2795f4c99e70SEd Tanous         {
2796f4c99e70SEd Tanous             return;
2797f4c99e70SEd Tanous         }
2798dd60b9edSEd Tanous 
2799dd60b9edSEd Tanous         asyncResp->res.addHeader(
2800dd60b9edSEd Tanous             boost::beast::http::field::link,
2801dd60b9edSEd Tanous             "</redfish/v1/JsonSchemas/ComputerSystemCollection.json>; rel=describedby");
28028d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
28030f74e643SEd Tanous             "#ComputerSystemCollection.ComputerSystemCollection";
28048d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Systems";
28058d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Computer System Collection";
2806462023adSSunitha Harish 
28071e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
2808002d39b4SEd Tanous             *crow::connections::systemBus, "xyz.openbmc_project.Settings",
28091e1e598dSJonathan Doman             "/xyz/openbmc_project/network/hypervisor",
2810002d39b4SEd Tanous             "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
28115e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec2,
28121e1e598dSJonathan Doman                         const std::string& /*hostName*/) {
2813002d39b4SEd Tanous             nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
28142c70f800SEd Tanous             ifaceArray = nlohmann::json::array();
2815002d39b4SEd Tanous             auto& count = asyncResp->res.jsonValue["Members@odata.count"];
28161476687dSEd Tanous 
28171476687dSEd Tanous             nlohmann::json::object_t system;
28181476687dSEd Tanous             system["@odata.id"] = "/redfish/v1/Systems/system";
2819b2ba3072SPatrick Williams             ifaceArray.emplace_back(std::move(system));
282094bda602STim Lee             count = ifaceArray.size();
28218a592810SEd Tanous             if (!ec2)
2822462023adSSunitha Harish             {
2823462023adSSunitha Harish                 BMCWEB_LOG_DEBUG << "Hypervisor is available";
28241476687dSEd Tanous                 nlohmann::json::object_t hypervisor;
2825002d39b4SEd Tanous                 hypervisor["@odata.id"] = "/redfish/v1/Systems/hypervisor";
2826b2ba3072SPatrick Williams                 ifaceArray.emplace_back(std::move(hypervisor));
28272c70f800SEd Tanous                 count = ifaceArray.size();
2828cb13a392SEd Tanous             }
28291e1e598dSJonathan Doman             });
28307e860f15SJohn Edward Broadbent         });
2831c5b2abe0SLewanczyk, Dawid }
28327e860f15SJohn Edward Broadbent 
28337e860f15SJohn Edward Broadbent /**
28347e860f15SJohn Edward Broadbent  * Function transceives data with dbus directly.
28357e860f15SJohn Edward Broadbent  */
28364f48d5f6SEd Tanous inline void doNMI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
28377e860f15SJohn Edward Broadbent {
283889492a15SPatrick Williams     constexpr const char* serviceName = "xyz.openbmc_project.Control.Host.NMI";
283989492a15SPatrick Williams     constexpr const char* objectPath = "/xyz/openbmc_project/control/host0/nmi";
284089492a15SPatrick Williams     constexpr const char* interfaceName =
28417e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Control.Host.NMI";
284289492a15SPatrick Williams     constexpr const char* method = "NMI";
28437e860f15SJohn Edward Broadbent 
28447e860f15SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
28455e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
28467e860f15SJohn Edward Broadbent         if (ec)
28477e860f15SJohn Edward Broadbent         {
28487e860f15SJohn Edward Broadbent             BMCWEB_LOG_ERROR << " Bad D-Bus request error: " << ec;
28497e860f15SJohn Edward Broadbent             messages::internalError(asyncResp->res);
28507e860f15SJohn Edward Broadbent             return;
28517e860f15SJohn Edward Broadbent         }
28527e860f15SJohn Edward Broadbent         messages::success(asyncResp->res);
28537e860f15SJohn Edward Broadbent         },
28547e860f15SJohn Edward Broadbent         serviceName, objectPath, interfaceName, method);
28557e860f15SJohn Edward Broadbent }
2856c5b2abe0SLewanczyk, Dawid 
2857c5b2abe0SLewanczyk, Dawid /**
2858cc340dd9SEd Tanous  * SystemActionsReset class supports handle POST method for Reset action.
2859cc340dd9SEd Tanous  * The class retrieves and sends data directly to D-Bus.
2860cc340dd9SEd Tanous  */
28617e860f15SJohn Edward Broadbent inline void requestRoutesSystemActionsReset(App& app)
2862cc340dd9SEd Tanous {
2863cc340dd9SEd Tanous     /**
2864cc340dd9SEd Tanous      * Function handles POST method request.
2865cc340dd9SEd Tanous      * Analyzes POST body message before sends Reset request data to D-Bus.
2866cc340dd9SEd Tanous      */
28677e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28687e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset/")
2869ed398213SEd Tanous         .privileges(redfish::privileges::postComputerSystem)
2870002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
2871002d39b4SEd Tanous             [&app](const crow::Request& req,
28727e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
28733ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
287445ca1b86SEd Tanous         {
287545ca1b86SEd Tanous             return;
287645ca1b86SEd Tanous         }
28779712f8acSEd Tanous         std::string resetType;
287815ed6780SWilly Tu         if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
28797e860f15SJohn Edward Broadbent                                        resetType))
2880cc340dd9SEd Tanous         {
2881cc340dd9SEd Tanous             return;
2882cc340dd9SEd Tanous         }
2883cc340dd9SEd Tanous 
2884d22c8396SJason M. Bills         // Get the command and host vs. chassis
2885cc340dd9SEd Tanous         std::string command;
2886543f4400SEd Tanous         bool hostCommand = true;
2887d4d25793SEd Tanous         if ((resetType == "On") || (resetType == "ForceOn"))
2888cc340dd9SEd Tanous         {
2889cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.On";
2890d22c8396SJason M. Bills             hostCommand = true;
2891d22c8396SJason M. Bills         }
2892d22c8396SJason M. Bills         else if (resetType == "ForceOff")
2893d22c8396SJason M. Bills         {
2894d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Chassis.Transition.Off";
2895d22c8396SJason M. Bills             hostCommand = false;
2896d22c8396SJason M. Bills         }
2897d22c8396SJason M. Bills         else if (resetType == "ForceRestart")
2898d22c8396SJason M. Bills         {
289986a0851aSJason M. Bills             command =
290086a0851aSJason M. Bills                 "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot";
290186a0851aSJason M. Bills             hostCommand = true;
2902cc340dd9SEd Tanous         }
29039712f8acSEd Tanous         else if (resetType == "GracefulShutdown")
2904cc340dd9SEd Tanous         {
2905cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.Off";
2906d22c8396SJason M. Bills             hostCommand = true;
2907cc340dd9SEd Tanous         }
29089712f8acSEd Tanous         else if (resetType == "GracefulRestart")
2909cc340dd9SEd Tanous         {
29100fda0f12SGeorge Liu             command =
29110fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot";
2912d22c8396SJason M. Bills             hostCommand = true;
2913d22c8396SJason M. Bills         }
2914d22c8396SJason M. Bills         else if (resetType == "PowerCycle")
2915d22c8396SJason M. Bills         {
291686a0851aSJason M. Bills             command = "xyz.openbmc_project.State.Host.Transition.Reboot";
291786a0851aSJason M. Bills             hostCommand = true;
2918cc340dd9SEd Tanous         }
2919bfd5b826SLakshminarayana R. Kammath         else if (resetType == "Nmi")
2920bfd5b826SLakshminarayana R. Kammath         {
2921bfd5b826SLakshminarayana R. Kammath             doNMI(asyncResp);
2922bfd5b826SLakshminarayana R. Kammath             return;
2923bfd5b826SLakshminarayana R. Kammath         }
2924cc340dd9SEd Tanous         else
2925cc340dd9SEd Tanous         {
29268d1b46d7Szhanghch05             messages::actionParameterUnknown(asyncResp->res, "Reset",
29278d1b46d7Szhanghch05                                              resetType);
2928cc340dd9SEd Tanous             return;
2929cc340dd9SEd Tanous         }
2930cc340dd9SEd Tanous 
2931d22c8396SJason M. Bills         if (hostCommand)
2932d22c8396SJason M. Bills         {
2933cc340dd9SEd Tanous             crow::connections::systemBus->async_method_call(
29345e7e2dc5SEd Tanous                 [asyncResp, resetType](const boost::system::error_code& ec) {
2935cc340dd9SEd Tanous                 if (ec)
2936cc340dd9SEd Tanous                 {
2937cc340dd9SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
2938002d39b4SEd Tanous                     if (ec.value() == boost::asio::error::invalid_argument)
2939d22c8396SJason M. Bills                     {
2940d22c8396SJason M. Bills                         messages::actionParameterNotSupported(
2941d22c8396SJason M. Bills                             asyncResp->res, resetType, "Reset");
2942d22c8396SJason M. Bills                     }
2943d22c8396SJason M. Bills                     else
2944d22c8396SJason M. Bills                     {
2945f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2946d22c8396SJason M. Bills                     }
2947cc340dd9SEd Tanous                     return;
2948cc340dd9SEd Tanous                 }
2949f12894f8SJason M. Bills                 messages::success(asyncResp->res);
2950cc340dd9SEd Tanous                 },
2951cc340dd9SEd Tanous                 "xyz.openbmc_project.State.Host",
2952cc340dd9SEd Tanous                 "/xyz/openbmc_project/state/host0",
2953cc340dd9SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
29549712f8acSEd Tanous                 "xyz.openbmc_project.State.Host", "RequestedHostTransition",
2955168e20c1SEd Tanous                 dbus::utility::DbusVariantType{command});
2956cc340dd9SEd Tanous         }
2957d22c8396SJason M. Bills         else
2958d22c8396SJason M. Bills         {
2959d22c8396SJason M. Bills             crow::connections::systemBus->async_method_call(
29605e7e2dc5SEd Tanous                 [asyncResp, resetType](const boost::system::error_code& ec) {
2961d22c8396SJason M. Bills                 if (ec)
2962d22c8396SJason M. Bills                 {
2963d22c8396SJason M. Bills                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
2964002d39b4SEd Tanous                     if (ec.value() == boost::asio::error::invalid_argument)
2965d22c8396SJason M. Bills                     {
2966d22c8396SJason M. Bills                         messages::actionParameterNotSupported(
2967d22c8396SJason M. Bills                             asyncResp->res, resetType, "Reset");
2968d22c8396SJason M. Bills                     }
2969d22c8396SJason M. Bills                     else
2970d22c8396SJason M. Bills                     {
2971d22c8396SJason M. Bills                         messages::internalError(asyncResp->res);
2972d22c8396SJason M. Bills                     }
2973d22c8396SJason M. Bills                     return;
2974d22c8396SJason M. Bills                 }
2975d22c8396SJason M. Bills                 messages::success(asyncResp->res);
2976d22c8396SJason M. Bills                 },
2977d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis",
2978d22c8396SJason M. Bills                 "/xyz/openbmc_project/state/chassis0",
2979d22c8396SJason M. Bills                 "org.freedesktop.DBus.Properties", "Set",
2980002d39b4SEd Tanous                 "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition",
2981168e20c1SEd Tanous                 dbus::utility::DbusVariantType{command});
2982d22c8396SJason M. Bills         }
29837e860f15SJohn Edward Broadbent         });
2984d22c8396SJason M. Bills }
2985cc340dd9SEd Tanous 
298638c8a6f2SEd Tanous inline void handleComputerSystemCollectionHead(
2987dd60b9edSEd Tanous     App& app, const crow::Request& req,
2988dd60b9edSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2989dd60b9edSEd Tanous {
2990dd60b9edSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2991dd60b9edSEd Tanous     {
2992dd60b9edSEd Tanous         return;
2993dd60b9edSEd Tanous     }
2994dd60b9edSEd Tanous 
2995dd60b9edSEd Tanous     asyncResp->res.addHeader(
2996dd60b9edSEd Tanous         boost::beast::http::field::link,
2997dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ComputerSystem/ComputerSystem.json>; rel=describedby");
2998dd60b9edSEd Tanous }
2999dd60b9edSEd Tanous 
30005c3e9272SAbhishek Patel inline void afterPortRequest(
30015c3e9272SAbhishek Patel     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30025c3e9272SAbhishek Patel     const boost::system::error_code& ec,
30035c3e9272SAbhishek Patel     const std::vector<std::tuple<std::string, std::string, bool>>& socketData)
30045c3e9272SAbhishek Patel {
30055c3e9272SAbhishek Patel     if (ec)
30065c3e9272SAbhishek Patel     {
30075c3e9272SAbhishek Patel         messages::internalError(asyncResp->res);
30085c3e9272SAbhishek Patel         return;
30095c3e9272SAbhishek Patel     }
30105c3e9272SAbhishek Patel     for (const auto& data : socketData)
30115c3e9272SAbhishek Patel     {
30125c3e9272SAbhishek Patel         const std::string& socketPath = get<0>(data);
30135c3e9272SAbhishek Patel         const std::string& protocolName = get<1>(data);
30145c3e9272SAbhishek Patel         bool isProtocolEnabled = get<2>(data);
30155c3e9272SAbhishek Patel         nlohmann::json& dataJson = asyncResp->res.jsonValue["SerialConsole"];
30165c3e9272SAbhishek Patel         dataJson[protocolName]["ServiceEnabled"] = isProtocolEnabled;
30175c3e9272SAbhishek Patel         // need to retrieve port number for
30185c3e9272SAbhishek Patel         // obmc-console-ssh service
30195c3e9272SAbhishek Patel         if (protocolName == "SSH")
30205c3e9272SAbhishek Patel         {
30215c3e9272SAbhishek Patel             getPortNumber(socketPath, [asyncResp, protocolName](
302281c4e330SEd Tanous                                           const boost::system::error_code& ec1,
30235c3e9272SAbhishek Patel                                           int portNumber) {
30245c3e9272SAbhishek Patel                 if (ec1)
30255c3e9272SAbhishek Patel                 {
30265c3e9272SAbhishek Patel                     messages::internalError(asyncResp->res);
30275c3e9272SAbhishek Patel                     return;
30285c3e9272SAbhishek Patel                 }
30295c3e9272SAbhishek Patel                 nlohmann::json& dataJson1 =
30305c3e9272SAbhishek Patel                     asyncResp->res.jsonValue["SerialConsole"];
30315c3e9272SAbhishek Patel                 dataJson1[protocolName]["Port"] = portNumber;
30325c3e9272SAbhishek Patel             });
30335c3e9272SAbhishek Patel         }
30345c3e9272SAbhishek Patel     }
30355c3e9272SAbhishek Patel }
3036cc340dd9SEd Tanous /**
30376617338dSEd Tanous  * Systems derived class for delivering Computer Systems Schema.
3038c5b2abe0SLewanczyk, Dawid  */
30397e860f15SJohn Edward Broadbent inline void requestRoutesSystems(App& app)
30401abe55efSEd Tanous {
3041dd60b9edSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/")
3042dd60b9edSEd Tanous         .privileges(redfish::privileges::headComputerSystem)
3043dd60b9edSEd Tanous         .methods(boost::beast::http::verb::head)(
3044dd60b9edSEd Tanous             std::bind_front(handleComputerSystemCollectionHead, std::ref(app)));
3045c5b2abe0SLewanczyk, Dawid     /**
3046c5b2abe0SLewanczyk, Dawid      * Functions triggers appropriate requests on DBus
3047c5b2abe0SLewanczyk, Dawid      */
304822d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/")
3049ed398213SEd Tanous         .privileges(redfish::privileges::getComputerSystem)
3050002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3051002d39b4SEd Tanous             [&app](const crow::Request& req,
305222d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
305322d268cbSEd Tanous                    const std::string& systemName) {
30543ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
305545ca1b86SEd Tanous         {
305645ca1b86SEd Tanous             return;
305745ca1b86SEd Tanous         }
3058746b56f3SAsmitha Karunanithi 
3059746b56f3SAsmitha Karunanithi         if (systemName == "hypervisor")
3060746b56f3SAsmitha Karunanithi         {
3061746b56f3SAsmitha Karunanithi             handleHypervisorSystemGet(asyncResp);
3062746b56f3SAsmitha Karunanithi             return;
3063746b56f3SAsmitha Karunanithi         }
3064746b56f3SAsmitha Karunanithi 
306522d268cbSEd Tanous         if (systemName != "system")
306622d268cbSEd Tanous         {
306722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
306822d268cbSEd Tanous                                        systemName);
306922d268cbSEd Tanous             return;
307022d268cbSEd Tanous         }
3071dd60b9edSEd Tanous         asyncResp->res.addHeader(
3072dd60b9edSEd Tanous             boost::beast::http::field::link,
3073dd60b9edSEd Tanous             "</redfish/v1/JsonSchemas/ComputerSystem/ComputerSystem.json>; rel=describedby");
30748d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
307537bbf98cSChris Cain             "#ComputerSystem.v1_16_0.ComputerSystem";
30768d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "system";
30778d1b46d7Szhanghch05         asyncResp->res.jsonValue["Id"] = "system";
30788d1b46d7Szhanghch05         asyncResp->res.jsonValue["SystemType"] = "Physical";
30798d1b46d7Szhanghch05         asyncResp->res.jsonValue["Description"] = "Computer System";
30808d1b46d7Szhanghch05         asyncResp->res.jsonValue["ProcessorSummary"]["Count"] = 0;
30815fd0aafbSNinad Palsule         if constexpr (bmcwebEnableProcMemStatus)
30825fd0aafbSNinad Palsule         {
30838d1b46d7Szhanghch05             asyncResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
30848d1b46d7Szhanghch05                 "Disabled";
30858d1b46d7Szhanghch05             asyncResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
30868d1b46d7Szhanghch05                 "Disabled";
30875fd0aafbSNinad Palsule         }
3088cf0e004cSNinad Palsule         asyncResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] =
3089cf0e004cSNinad Palsule             uint64_t(0);
3090002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Systems/system";
309104a258f4SEd Tanous 
30921476687dSEd Tanous         asyncResp->res.jsonValue["Processors"]["@odata.id"] =
30931476687dSEd Tanous             "/redfish/v1/Systems/system/Processors";
30941476687dSEd Tanous         asyncResp->res.jsonValue["Memory"]["@odata.id"] =
30951476687dSEd Tanous             "/redfish/v1/Systems/system/Memory";
30961476687dSEd Tanous         asyncResp->res.jsonValue["Storage"]["@odata.id"] =
30971476687dSEd Tanous             "/redfish/v1/Systems/system/Storage";
30983179105bSSunny Srivastava         asyncResp->res.jsonValue["FabricAdapters"]["@odata.id"] =
30993179105bSSunny Srivastava             "/redfish/v1/Systems/system/FabricAdapters";
3100029573d4SEd Tanous 
3101002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"]["target"] =
31021476687dSEd Tanous             "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset";
31031476687dSEd Tanous         asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"]
31041476687dSEd Tanous                                 ["@Redfish.ActionInfo"] =
31051476687dSEd Tanous             "/redfish/v1/Systems/system/ResetActionInfo";
3106c5b2abe0SLewanczyk, Dawid 
31071476687dSEd Tanous         asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
31081476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices";
31091476687dSEd Tanous         asyncResp->res.jsonValue["Bios"]["@odata.id"] =
31101476687dSEd Tanous             "/redfish/v1/Systems/system/Bios";
3111c4bf6374SJason M. Bills 
31121476687dSEd Tanous         nlohmann::json::array_t managedBy;
31131476687dSEd Tanous         nlohmann::json& manager = managedBy.emplace_back();
31141476687dSEd Tanous         manager["@odata.id"] = "/redfish/v1/Managers/bmc";
3115002d39b4SEd Tanous         asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy);
31161476687dSEd Tanous         asyncResp->res.jsonValue["Status"]["Health"] = "OK";
31171476687dSEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
31180e8ac5e7SGunnar Mills 
31190e8ac5e7SGunnar Mills         // Fill in SerialConsole info
3120002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
3121002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["IPMI"]["ServiceEnabled"] =
3122002d39b4SEd Tanous             true;
31231476687dSEd Tanous 
31241476687dSEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["SSH"]["ServiceEnabled"] =
31251476687dSEd Tanous             true;
31261476687dSEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["SSH"]["Port"] = 2200;
31271476687dSEd Tanous         asyncResp->res
31281476687dSEd Tanous             .jsonValue["SerialConsole"]["SSH"]["HotKeySequenceDisplay"] =
31291476687dSEd Tanous             "Press ~. to exit console";
31305c3e9272SAbhishek Patel         getPortStatusAndPath(std::span{protocolToDBusForSystems},
31315c3e9272SAbhishek Patel                              std::bind_front(afterPortRequest, asyncResp));
31320e8ac5e7SGunnar Mills 
31330e8ac5e7SGunnar Mills #ifdef BMCWEB_ENABLE_KVM
31340e8ac5e7SGunnar Mills         // Fill in GraphicalConsole info
3135002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
3136002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
3137002d39b4SEd Tanous             4;
3138613dabeaSEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
3139613dabeaSEd Tanous             nlohmann::json::array_t({"KVMIP"});
31401476687dSEd Tanous 
31410e8ac5e7SGunnar Mills #endif // BMCWEB_ENABLE_KVM
314213451e39SWilly Tu 
314313451e39SWilly Tu         auto health = std::make_shared<HealthPopulate>(asyncResp);
314413451e39SWilly Tu         if constexpr (bmcwebEnableHealthPopulate)
314513451e39SWilly Tu         {
31467a1dbc48SGeorge Liu             constexpr std::array<std::string_view, 4> inventoryForSystems{
3147b49ac873SJames Feist                 "xyz.openbmc_project.Inventory.Item.Dimm",
31482ad9c2f6SJames Feist                 "xyz.openbmc_project.Inventory.Item.Cpu",
3149e284a7c1SJames Feist                 "xyz.openbmc_project.Inventory.Item.Drive",
3150e284a7c1SJames Feist                 "xyz.openbmc_project.Inventory.Item.StorageController"};
3151b49ac873SJames Feist 
31527a1dbc48SGeorge Liu             dbus::utility::getSubTreePaths(
31537a1dbc48SGeorge Liu                 "/", 0, inventoryForSystems,
31547a1dbc48SGeorge Liu                 [health](const boost::system::error_code& ec,
3155914e2d5dSEd Tanous                          const std::vector<std::string>& resp) {
3156b49ac873SJames Feist                 if (ec)
3157b49ac873SJames Feist                 {
3158b49ac873SJames Feist                     // no inventory
3159b49ac873SJames Feist                     return;
3160b49ac873SJames Feist                 }
3161b49ac873SJames Feist 
3162914e2d5dSEd Tanous                 health->inventory = resp;
31637a1dbc48SGeorge Liu                 });
3164b49ac873SJames Feist             health->populate();
316513451e39SWilly Tu         }
3166b49ac873SJames Feist 
3167002d39b4SEd Tanous         getMainChassisId(asyncResp,
3168002d39b4SEd Tanous                          [](const std::string& chassisId,
31698d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
3170b2c7e208SEd Tanous             nlohmann::json::array_t chassisArray;
3171b2c7e208SEd Tanous             nlohmann::json& chassis = chassisArray.emplace_back();
3172ef4c65b7SEd Tanous             chassis["@odata.id"] = boost::urls::format("/redfish/v1/Chassis/{}",
3173ef4c65b7SEd Tanous                                                        chassisId);
3174002d39b4SEd Tanous             aRsp->res.jsonValue["Links"]["Chassis"] = std::move(chassisArray);
3175c5d03ff4SJennifer Lee         });
3176a3002228SAppaRao Puli 
31779f8bfa7cSGunnar Mills         getLocationIndicatorActive(asyncResp);
31789f8bfa7cSGunnar Mills         // TODO (Gunnar): Remove IndicatorLED after enough time has passed
3179a3002228SAppaRao Puli         getIndicatorLedState(asyncResp);
31805bc2dc8eSJames Feist         getComputerSystem(asyncResp, health);
31816c34de48SEd Tanous         getHostState(asyncResp);
3182491d8ee7SSantosh Puranik         getBootProperties(asyncResp);
3183978b8803SAndrew Geissler         getBootProgress(asyncResp);
3184b6d5d45cSHieu Huynh         getBootProgressLastStateTime(asyncResp);
3185*472bd202SLakshmi Yadlapati         pcie_util::getPCIeDeviceList(asyncResp, "PCIeDevices");
318651709ffdSYong Li         getHostWatchdogTimer(asyncResp);
3187c6a620f2SGeorge Liu         getPowerRestorePolicy(asyncResp);
3188797d5daeSCorey Hardesty         getAutomaticRetryPolicy(asyncResp);
3189c0557e1aSGunnar Mills         getLastResetTime(asyncResp);
3190a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
3191a6349918SAppaRao Puli         getProvisioningStatus(asyncResp);
3192a6349918SAppaRao Puli #endif
31931981771bSAli Ahmed         getTrustedModuleRequiredToBoot(asyncResp);
31943a2d0424SChris Cain         getPowerMode(asyncResp);
319537bbf98cSChris Cain         getIdlePowerSaver(asyncResp);
31967e860f15SJohn Edward Broadbent         });
3197550a6bf8SJiaqing Zhao 
319822d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/")
3199ed398213SEd Tanous         .privileges(redfish::privileges::patchComputerSystem)
32007e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
320145ca1b86SEd Tanous             [&app](const crow::Request& req,
320222d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
320322d268cbSEd Tanous                    const std::string& systemName) {
32043ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
320545ca1b86SEd Tanous         {
320645ca1b86SEd Tanous             return;
320745ca1b86SEd Tanous         }
320822d268cbSEd Tanous         if (systemName != "system")
320922d268cbSEd Tanous         {
321022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
321122d268cbSEd Tanous                                        systemName);
321222d268cbSEd Tanous             return;
321322d268cbSEd Tanous         }
321422d268cbSEd Tanous 
3215dd60b9edSEd Tanous         asyncResp->res.addHeader(
3216dd60b9edSEd Tanous             boost::beast::http::field::link,
3217dd60b9edSEd Tanous             "</redfish/v1/JsonSchemas/ComputerSystem/ComputerSystem.json>; rel=describedby");
3218dd60b9edSEd Tanous 
32199f8bfa7cSGunnar Mills         std::optional<bool> locationIndicatorActive;
3220cde19e5fSSantosh Puranik         std::optional<std::string> indicatorLed;
322198e386ecSGunnar Mills         std::optional<std::string> assetTag;
3222c6a620f2SGeorge Liu         std::optional<std::string> powerRestorePolicy;
32233a2d0424SChris Cain         std::optional<std::string> powerMode;
3224550a6bf8SJiaqing Zhao         std::optional<bool> wdtEnable;
3225550a6bf8SJiaqing Zhao         std::optional<std::string> wdtTimeOutAction;
3226550a6bf8SJiaqing Zhao         std::optional<std::string> bootSource;
3227550a6bf8SJiaqing Zhao         std::optional<std::string> bootType;
3228550a6bf8SJiaqing Zhao         std::optional<std::string> bootEnable;
3229550a6bf8SJiaqing Zhao         std::optional<std::string> bootAutomaticRetry;
3230797d5daeSCorey Hardesty         std::optional<uint32_t> bootAutomaticRetryAttempts;
3231550a6bf8SJiaqing Zhao         std::optional<bool> bootTrustedModuleRequired;
3232550a6bf8SJiaqing Zhao         std::optional<bool> ipsEnable;
3233550a6bf8SJiaqing Zhao         std::optional<uint8_t> ipsEnterUtil;
3234550a6bf8SJiaqing Zhao         std::optional<uint64_t> ipsEnterTime;
3235550a6bf8SJiaqing Zhao         std::optional<uint8_t> ipsExitUtil;
3236550a6bf8SJiaqing Zhao         std::optional<uint64_t> ipsExitTime;
3237550a6bf8SJiaqing Zhao 
3238550a6bf8SJiaqing Zhao         // clang-format off
323915ed6780SWilly Tu                 if (!json_util::readJsonPatch(
3240550a6bf8SJiaqing Zhao                         req, asyncResp->res,
3241550a6bf8SJiaqing Zhao                         "IndicatorLED", indicatorLed,
32427e860f15SJohn Edward Broadbent                         "LocationIndicatorActive", locationIndicatorActive,
3243550a6bf8SJiaqing Zhao                         "AssetTag", assetTag,
3244550a6bf8SJiaqing Zhao                         "PowerRestorePolicy", powerRestorePolicy,
3245550a6bf8SJiaqing Zhao                         "PowerMode", powerMode,
3246550a6bf8SJiaqing Zhao                         "HostWatchdogTimer/FunctionEnabled", wdtEnable,
3247550a6bf8SJiaqing Zhao                         "HostWatchdogTimer/TimeoutAction", wdtTimeOutAction,
3248550a6bf8SJiaqing Zhao                         "Boot/BootSourceOverrideTarget", bootSource,
3249550a6bf8SJiaqing Zhao                         "Boot/BootSourceOverrideMode", bootType,
3250550a6bf8SJiaqing Zhao                         "Boot/BootSourceOverrideEnabled", bootEnable,
3251550a6bf8SJiaqing Zhao                         "Boot/AutomaticRetryConfig", bootAutomaticRetry,
3252797d5daeSCorey Hardesty                         "Boot/AutomaticRetryAttempts", bootAutomaticRetryAttempts,
3253550a6bf8SJiaqing Zhao                         "Boot/TrustedModuleRequiredToBoot", bootTrustedModuleRequired,
3254550a6bf8SJiaqing Zhao                         "IdlePowerSaver/Enabled", ipsEnable,
3255550a6bf8SJiaqing Zhao                         "IdlePowerSaver/EnterUtilizationPercent", ipsEnterUtil,
3256550a6bf8SJiaqing Zhao                         "IdlePowerSaver/EnterDwellTimeSeconds", ipsEnterTime,
3257550a6bf8SJiaqing Zhao                         "IdlePowerSaver/ExitUtilizationPercent", ipsExitUtil,
3258550a6bf8SJiaqing Zhao                         "IdlePowerSaver/ExitDwellTimeSeconds", ipsExitTime))
32596617338dSEd Tanous                 {
32606617338dSEd Tanous                     return;
32616617338dSEd Tanous                 }
3262550a6bf8SJiaqing Zhao         // clang-format on
3263491d8ee7SSantosh Puranik 
32648d1b46d7Szhanghch05         asyncResp->res.result(boost::beast::http::status::no_content);
3265c45f0082SYong Li 
326698e386ecSGunnar Mills         if (assetTag)
326798e386ecSGunnar Mills         {
326898e386ecSGunnar Mills             setAssetTag(asyncResp, *assetTag);
326998e386ecSGunnar Mills         }
327098e386ecSGunnar Mills 
3271550a6bf8SJiaqing Zhao         if (wdtEnable || wdtTimeOutAction)
3272c45f0082SYong Li         {
3273f23b7296SEd Tanous             setWDTProperties(asyncResp, wdtEnable, wdtTimeOutAction);
3274c45f0082SYong Li         }
3275c45f0082SYong Li 
3276cd9a4666SKonstantin Aladyshev         if (bootSource || bootType || bootEnable)
327769f35306SGunnar Mills         {
3278002d39b4SEd Tanous             setBootProperties(asyncResp, bootSource, bootType, bootEnable);
3279491d8ee7SSantosh Puranik         }
3280550a6bf8SJiaqing Zhao         if (bootAutomaticRetry)
328169f35306SGunnar Mills         {
3282550a6bf8SJiaqing Zhao             setAutomaticRetry(asyncResp, *bootAutomaticRetry);
328369f35306SGunnar Mills         }
3284ac7e1e0bSAli Ahmed 
3285797d5daeSCorey Hardesty         if (bootAutomaticRetryAttempts)
3286797d5daeSCorey Hardesty         {
3287797d5daeSCorey Hardesty             setAutomaticRetryAttempts(asyncResp,
3288797d5daeSCorey Hardesty                                       bootAutomaticRetryAttempts.value());
3289797d5daeSCorey Hardesty         }
3290797d5daeSCorey Hardesty 
3291550a6bf8SJiaqing Zhao         if (bootTrustedModuleRequired)
3292ac7e1e0bSAli Ahmed         {
3293550a6bf8SJiaqing Zhao             setTrustedModuleRequiredToBoot(asyncResp,
3294550a6bf8SJiaqing Zhao                                            *bootTrustedModuleRequired);
329569f35306SGunnar Mills         }
3296265c1602SJohnathan Mantey 
32979f8bfa7cSGunnar Mills         if (locationIndicatorActive)
32989f8bfa7cSGunnar Mills         {
3299002d39b4SEd Tanous             setLocationIndicatorActive(asyncResp, *locationIndicatorActive);
33009f8bfa7cSGunnar Mills         }
33019f8bfa7cSGunnar Mills 
33027e860f15SJohn Edward Broadbent         // TODO (Gunnar): Remove IndicatorLED after enough time has
33037e860f15SJohn Edward Broadbent         // passed
33049712f8acSEd Tanous         if (indicatorLed)
33056617338dSEd Tanous         {
3306f23b7296SEd Tanous             setIndicatorLedState(asyncResp, *indicatorLed);
3307002d39b4SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::warning,
3308d6aa0093SGunnar Mills                                      "299 - \"IndicatorLED is deprecated. Use "
3309d6aa0093SGunnar Mills                                      "LocationIndicatorActive instead.\"");
33106617338dSEd Tanous         }
3311c6a620f2SGeorge Liu 
3312c6a620f2SGeorge Liu         if (powerRestorePolicy)
3313c6a620f2SGeorge Liu         {
33144e69c904SGunnar Mills             setPowerRestorePolicy(asyncResp, *powerRestorePolicy);
3315c6a620f2SGeorge Liu         }
33163a2d0424SChris Cain 
33173a2d0424SChris Cain         if (powerMode)
33183a2d0424SChris Cain         {
33193a2d0424SChris Cain             setPowerMode(asyncResp, *powerMode);
33203a2d0424SChris Cain         }
332137bbf98cSChris Cain 
3322550a6bf8SJiaqing Zhao         if (ipsEnable || ipsEnterUtil || ipsEnterTime || ipsExitUtil ||
3323550a6bf8SJiaqing Zhao             ipsExitTime)
332437bbf98cSChris Cain         {
3325002d39b4SEd Tanous             setIdlePowerSaver(asyncResp, ipsEnable, ipsEnterUtil, ipsEnterTime,
3326002d39b4SEd Tanous                               ipsExitUtil, ipsExitTime);
332737bbf98cSChris Cain         }
33287e860f15SJohn Edward Broadbent         });
3329c5b2abe0SLewanczyk, Dawid }
33301cb1a9e6SAppaRao Puli 
333138c8a6f2SEd Tanous inline void handleSystemCollectionResetActionHead(
3332dd60b9edSEd Tanous     crow::App& app, const crow::Request& req,
3333dd60b9edSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3334dd60b9edSEd Tanous {
3335dd60b9edSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3336dd60b9edSEd Tanous     {
3337dd60b9edSEd Tanous         return;
3338dd60b9edSEd Tanous     }
3339dd60b9edSEd Tanous     asyncResp->res.addHeader(
3340dd60b9edSEd Tanous         boost::beast::http::field::link,
3341dd60b9edSEd Tanous         "</redfish/v1/JsonSchemas/ActionInfo/ActionInfo.json>; rel=describedby");
3342dd60b9edSEd Tanous }
3343dd60b9edSEd Tanous 
33441cb1a9e6SAppaRao Puli /**
33451cb1a9e6SAppaRao Puli  * SystemResetActionInfo derived class for delivering Computer Systems
33461cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
33471cb1a9e6SAppaRao Puli  */
33487e860f15SJohn Edward Broadbent inline void requestRoutesSystemResetActionInfo(App& app)
33491cb1a9e6SAppaRao Puli {
3350dd60b9edSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/ResetActionInfo/")
3351dd60b9edSEd Tanous         .privileges(redfish::privileges::headActionInfo)
3352dd60b9edSEd Tanous         .methods(boost::beast::http::verb::head)(std::bind_front(
3353dd60b9edSEd Tanous             handleSystemCollectionResetActionHead, std::ref(app)));
33541cb1a9e6SAppaRao Puli     /**
33551cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
33561cb1a9e6SAppaRao Puli      */
335722d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/ResetActionInfo/")
3358ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
33597e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
336045ca1b86SEd Tanous             [&app](const crow::Request& req,
336122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
336222d268cbSEd Tanous                    const std::string& systemName) {
33633ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
336445ca1b86SEd Tanous         {
336545ca1b86SEd Tanous             return;
336645ca1b86SEd Tanous         }
3367746b56f3SAsmitha Karunanithi 
3368746b56f3SAsmitha Karunanithi         if (systemName == "hypervisor")
3369746b56f3SAsmitha Karunanithi         {
3370746b56f3SAsmitha Karunanithi             handleHypervisorResetActionGet(asyncResp);
3371746b56f3SAsmitha Karunanithi             return;
3372746b56f3SAsmitha Karunanithi         }
3373746b56f3SAsmitha Karunanithi 
337422d268cbSEd Tanous         if (systemName != "system")
337522d268cbSEd Tanous         {
337622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
337722d268cbSEd Tanous                                        systemName);
337822d268cbSEd Tanous             return;
337922d268cbSEd Tanous         }
338022d268cbSEd Tanous 
3381dd60b9edSEd Tanous         asyncResp->res.addHeader(
3382dd60b9edSEd Tanous             boost::beast::http::field::link,
3383dd60b9edSEd Tanous             "</redfish/v1/JsonSchemas/ActionInfo/ActionInfo.json>; rel=describedby");
33841476687dSEd Tanous 
33851476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
33861476687dSEd Tanous             "/redfish/v1/Systems/system/ResetActionInfo";
33871476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
33881476687dSEd Tanous             "#ActionInfo.v1_1_2.ActionInfo";
33891476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "Reset Action Info";
33901476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
33913215e700SNan Zhou 
33923215e700SNan Zhou         nlohmann::json::array_t parameters;
33933215e700SNan Zhou         nlohmann::json::object_t parameter;
33943215e700SNan Zhou 
33953215e700SNan Zhou         parameter["Name"] = "ResetType";
33963215e700SNan Zhou         parameter["Required"] = true;
33973215e700SNan Zhou         parameter["DataType"] = "String";
33983215e700SNan Zhou         nlohmann::json::array_t allowableValues;
33993215e700SNan Zhou         allowableValues.emplace_back("On");
34003215e700SNan Zhou         allowableValues.emplace_back("ForceOff");
34013215e700SNan Zhou         allowableValues.emplace_back("ForceOn");
34023215e700SNan Zhou         allowableValues.emplace_back("ForceRestart");
34033215e700SNan Zhou         allowableValues.emplace_back("GracefulRestart");
34043215e700SNan Zhou         allowableValues.emplace_back("GracefulShutdown");
34053215e700SNan Zhou         allowableValues.emplace_back("PowerCycle");
34063215e700SNan Zhou         allowableValues.emplace_back("Nmi");
34073215e700SNan Zhou         parameter["AllowableValues"] = std::move(allowableValues);
34083215e700SNan Zhou         parameters.emplace_back(std::move(parameter));
34093215e700SNan Zhou 
34103215e700SNan Zhou         asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
34117e860f15SJohn Edward Broadbent         });
34121cb1a9e6SAppaRao Puli }
3413c5b2abe0SLewanczyk, Dawid } // namespace redfish
3414