xref: /openbmc/bmcweb/features/redfish/lib/systems.hpp (revision 1e1e598df6d1d9530dde6e92d8f74f8143f60e50)
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 
18*1e1e598dSJonathan Doman #include "dbus_singleton.hpp"
19b49ac873SJames Feist #include "health.hpp"
201c8fba97SJames Feist #include "led.hpp"
21f5c9f8bdSJason M. Bills #include "pcie.hpp"
22c5d03ff4SJennifer Lee #include "redfish_util.hpp"
23c5d03ff4SJennifer Lee 
247e860f15SJohn Edward Broadbent #include <app.hpp>
259712f8acSEd Tanous #include <boost/container/flat_map.hpp>
26168e20c1SEd Tanous #include <dbus_utility.hpp>
27ed398213SEd Tanous #include <registries/privilege_registry.hpp>
28*1e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
29cb7e1e7bSAndrew Geissler #include <utils/fw_utils.hpp>
30c5b2abe0SLewanczyk, Dawid #include <utils/json_utils.hpp>
311214b7e7SGunnar Mills 
32abf2add6SEd Tanous #include <variant>
33c5b2abe0SLewanczyk, Dawid 
341abe55efSEd Tanous namespace redfish
351abe55efSEd Tanous {
36c5b2abe0SLewanczyk, Dawid 
379d3ae10eSAlpana Kumari /**
389d3ae10eSAlpana Kumari  * @brief Updates the Functional State of DIMMs
399d3ae10eSAlpana Kumari  *
409d3ae10eSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
419d3ae10eSAlpana Kumari  * @param[in] dimmState Dimm's Functional state, true/false
429d3ae10eSAlpana Kumari  *
439d3ae10eSAlpana Kumari  * @return None.
449d3ae10eSAlpana Kumari  */
458d1b46d7Szhanghch05 inline void
468d1b46d7Szhanghch05     updateDimmProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
47*1e1e598dSJonathan Doman                          bool isDimmFunctional)
489d3ae10eSAlpana Kumari {
49*1e1e598dSJonathan Doman     BMCWEB_LOG_DEBUG << "Dimm Functional: " << isDimmFunctional;
509d3ae10eSAlpana Kumari 
519d3ae10eSAlpana Kumari     // Set it as Enabled if at least one DIMM is functional
529d3ae10eSAlpana Kumari     // Update STATE only if previous State was DISABLED and current Dimm is
539d3ae10eSAlpana Kumari     // ENABLED.
549d3ae10eSAlpana Kumari     nlohmann::json& prevMemSummary =
559d3ae10eSAlpana Kumari         aResp->res.jsonValue["MemorySummary"]["Status"]["State"];
569d3ae10eSAlpana Kumari     if (prevMemSummary == "Disabled")
579d3ae10eSAlpana Kumari     {
58*1e1e598dSJonathan Doman         if (isDimmFunctional == true)
599d3ae10eSAlpana Kumari         {
609d3ae10eSAlpana Kumari             aResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
619d3ae10eSAlpana Kumari                 "Enabled";
629d3ae10eSAlpana Kumari         }
639d3ae10eSAlpana Kumari     }
649d3ae10eSAlpana Kumari }
659d3ae10eSAlpana Kumari 
6657e8c9beSAlpana Kumari /*
6757e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Count" based on Cpu PresenceState
6857e8c9beSAlpana Kumari  *
6957e8c9beSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
7057e8c9beSAlpana Kumari  * @param[in] cpuPresenceState CPU present or not
7157e8c9beSAlpana Kumari  *
7257e8c9beSAlpana Kumari  * @return None.
7357e8c9beSAlpana Kumari  */
74*1e1e598dSJonathan Doman inline void
75*1e1e598dSJonathan Doman     modifyCpuPresenceState(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
76*1e1e598dSJonathan Doman                            bool isCpuPresent)
7757e8c9beSAlpana Kumari {
78*1e1e598dSJonathan Doman     BMCWEB_LOG_DEBUG << "Cpu Present: " << isCpuPresent;
7957e8c9beSAlpana Kumari 
80*1e1e598dSJonathan Doman     if (isCpuPresent == true)
8157e8c9beSAlpana Kumari     {
82b4b9595aSJames Feist         nlohmann::json& procCount =
83b4b9595aSJames Feist             aResp->res.jsonValue["ProcessorSummary"]["Count"];
84b4b9595aSJames Feist         auto procCountPtr =
85b4b9595aSJames Feist             procCount.get_ptr<nlohmann::json::number_integer_t*>();
86b4b9595aSJames Feist         if (procCountPtr != nullptr)
87b4b9595aSJames Feist         {
88b4b9595aSJames Feist             // shouldn't be possible to be nullptr
89b4b9595aSJames Feist             *procCountPtr += 1;
9057e8c9beSAlpana Kumari         }
91b4b9595aSJames Feist     }
9257e8c9beSAlpana Kumari }
9357e8c9beSAlpana Kumari 
9457e8c9beSAlpana Kumari /*
9557e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Status" "State" based on
9657e8c9beSAlpana Kumari  *        CPU Functional State
9757e8c9beSAlpana Kumari  *
9857e8c9beSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
9957e8c9beSAlpana Kumari  * @param[in] cpuFunctionalState is CPU functional true/false
10057e8c9beSAlpana Kumari  *
10157e8c9beSAlpana Kumari  * @return None.
10257e8c9beSAlpana Kumari  */
103*1e1e598dSJonathan Doman inline void
104*1e1e598dSJonathan Doman     modifyCpuFunctionalState(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
105*1e1e598dSJonathan Doman                              bool isCpuFunctional)
10657e8c9beSAlpana Kumari {
107*1e1e598dSJonathan Doman     BMCWEB_LOG_DEBUG << "Cpu Functional: " << isCpuFunctional;
10857e8c9beSAlpana Kumari 
10957e8c9beSAlpana Kumari     nlohmann::json& prevProcState =
11057e8c9beSAlpana Kumari         aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"];
11157e8c9beSAlpana Kumari 
11257e8c9beSAlpana Kumari     // Set it as Enabled if at least one CPU is functional
11357e8c9beSAlpana Kumari     // Update STATE only if previous State was Non_Functional and current CPU is
11457e8c9beSAlpana Kumari     // Functional.
11557e8c9beSAlpana Kumari     if (prevProcState == "Disabled")
11657e8c9beSAlpana Kumari     {
117*1e1e598dSJonathan Doman         if (isCpuFunctional == true)
11857e8c9beSAlpana Kumari         {
11957e8c9beSAlpana Kumari             aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
12057e8c9beSAlpana Kumari                 "Enabled";
12157e8c9beSAlpana Kumari         }
12257e8c9beSAlpana Kumari     }
12357e8c9beSAlpana Kumari }
12457e8c9beSAlpana Kumari 
12503fbed92SAli Ahmed inline void getProcessorProperties(
12603fbed92SAli Ahmed     const std::shared_ptr<bmcweb::AsyncResp>& aResp, const std::string& service,
12703fbed92SAli Ahmed     const std::string& path,
128168e20c1SEd Tanous     const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
12903fbed92SAli Ahmed         properties)
13003fbed92SAli Ahmed {
13103fbed92SAli Ahmed 
13203fbed92SAli Ahmed     BMCWEB_LOG_DEBUG << "Got " << properties.size() << " Cpu properties.";
13303fbed92SAli Ahmed 
134*1e1e598dSJonathan Doman     auto getCpuPresenceState = [aResp](const boost::system::error_code ec3,
135*1e1e598dSJonathan Doman                                        const bool cpuPresenceCheck) {
13603fbed92SAli Ahmed         if (ec3)
13703fbed92SAli Ahmed         {
13803fbed92SAli Ahmed             BMCWEB_LOG_ERROR << "DBUS response error " << ec3;
13903fbed92SAli Ahmed             return;
14003fbed92SAli Ahmed         }
14103fbed92SAli Ahmed         modifyCpuPresenceState(aResp, cpuPresenceCheck);
14203fbed92SAli Ahmed     };
14303fbed92SAli Ahmed 
144*1e1e598dSJonathan Doman     auto getCpuFunctionalState = [aResp](const boost::system::error_code ec3,
145*1e1e598dSJonathan Doman                                          const bool cpuFunctionalCheck) {
14603fbed92SAli Ahmed         if (ec3)
14703fbed92SAli Ahmed         {
14803fbed92SAli Ahmed             BMCWEB_LOG_ERROR << "DBUS response error " << ec3;
14903fbed92SAli Ahmed             return;
15003fbed92SAli Ahmed         }
15103fbed92SAli Ahmed         modifyCpuFunctionalState(aResp, cpuFunctionalCheck);
15203fbed92SAli Ahmed     };
15303fbed92SAli Ahmed 
15403fbed92SAli Ahmed     // Get the Presence of CPU
155*1e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
156*1e1e598dSJonathan Doman         *crow::connections::systemBus, service, path,
157*1e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
158*1e1e598dSJonathan Doman         std::move(getCpuPresenceState));
15903fbed92SAli Ahmed 
16003fbed92SAli Ahmed     // Get the Functional State
161*1e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
162*1e1e598dSJonathan Doman         *crow::connections::systemBus, service, path,
163*1e1e598dSJonathan Doman         "xyz.openbmc_project.State.Decorator.OperationalStatus", "Functional",
164*1e1e598dSJonathan Doman         std::move(getCpuFunctionalState));
16503fbed92SAli Ahmed 
16603fbed92SAli Ahmed     for (const auto& property : properties)
16703fbed92SAli Ahmed     {
16803fbed92SAli Ahmed 
16903fbed92SAli Ahmed         // TODO: Get Model
17003fbed92SAli Ahmed 
17103fbed92SAli Ahmed         // Get CoreCount
17203fbed92SAli Ahmed         if (property.first == "CoreCount")
17303fbed92SAli Ahmed         {
17403fbed92SAli Ahmed 
17503fbed92SAli Ahmed             // Get CPU CoreCount and add it to the total
17603fbed92SAli Ahmed             const uint16_t* coreCountVal =
17703fbed92SAli Ahmed                 std::get_if<uint16_t>(&property.second);
17803fbed92SAli Ahmed 
17903fbed92SAli Ahmed             if (!coreCountVal)
18003fbed92SAli Ahmed             {
18103fbed92SAli Ahmed                 messages::internalError(aResp->res);
18203fbed92SAli Ahmed                 return;
18303fbed92SAli Ahmed             }
18403fbed92SAli Ahmed 
18503fbed92SAli Ahmed             nlohmann::json& coreCount =
18603fbed92SAli Ahmed                 aResp->res.jsonValue["ProcessorSummary"]["CoreCount"];
18703fbed92SAli Ahmed             uint64_t* coreCountPtr = coreCount.get_ptr<uint64_t*>();
18803fbed92SAli Ahmed 
18903fbed92SAli Ahmed             if (coreCountPtr == nullptr)
19003fbed92SAli Ahmed             {
19103fbed92SAli Ahmed                 coreCount = 0;
19203fbed92SAli Ahmed             }
19303fbed92SAli Ahmed             else
19403fbed92SAli Ahmed             {
19503fbed92SAli Ahmed                 *coreCountPtr += *coreCountVal;
19603fbed92SAli Ahmed             }
19703fbed92SAli Ahmed         }
19803fbed92SAli Ahmed     }
19903fbed92SAli Ahmed }
20003fbed92SAli Ahmed 
20103fbed92SAli Ahmed /*
20203fbed92SAli Ahmed  * @brief Get ProcessorSummary fields
20303fbed92SAli Ahmed  *
20403fbed92SAli Ahmed  * @param[in] aResp Shared pointer for completing asynchronous calls
20503fbed92SAli Ahmed  * @param[in] service dbus service for Cpu Information
20603fbed92SAli Ahmed  * @param[in] path dbus path for Cpu
20703fbed92SAli Ahmed  *
20803fbed92SAli Ahmed  * @return None.
20903fbed92SAli Ahmed  */
21003fbed92SAli Ahmed inline void getProcessorSummary(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
21103fbed92SAli Ahmed                                 const std::string& service,
21203fbed92SAli Ahmed                                 const std::string& path)
21303fbed92SAli Ahmed {
21403fbed92SAli Ahmed 
21503fbed92SAli Ahmed     crow::connections::systemBus->async_method_call(
21603fbed92SAli Ahmed         [aResp, service,
21703fbed92SAli Ahmed          path](const boost::system::error_code ec2,
21803fbed92SAli Ahmed                const std::vector<std::pair<
219168e20c1SEd Tanous                    std::string, dbus::utility::DbusVariantType>>& properties) {
22003fbed92SAli Ahmed             if (ec2)
22103fbed92SAli Ahmed             {
22203fbed92SAli Ahmed                 BMCWEB_LOG_ERROR << "DBUS response error " << ec2;
22303fbed92SAli Ahmed                 messages::internalError(aResp->res);
22403fbed92SAli Ahmed                 return;
22503fbed92SAli Ahmed             }
22603fbed92SAli Ahmed             getProcessorProperties(aResp, service, path, properties);
22703fbed92SAli Ahmed         },
22803fbed92SAli Ahmed         service, path, "org.freedesktop.DBus.Properties", "GetAll",
22903fbed92SAli Ahmed         "xyz.openbmc_project.Inventory.Item.Cpu");
23003fbed92SAli Ahmed }
23103fbed92SAli Ahmed 
23257e8c9beSAlpana Kumari /*
233c5b2abe0SLewanczyk, Dawid  * @brief Retrieves computer system properties over dbus
234c5b2abe0SLewanczyk, Dawid  *
235c5b2abe0SLewanczyk, Dawid  * @param[in] aResp Shared pointer for completing asynchronous calls
2368f9ee3cdSGunnar Mills  * @param[in] systemHealth  Shared HealthPopulate pointer
237c5b2abe0SLewanczyk, Dawid  *
238c5b2abe0SLewanczyk, Dawid  * @return None.
239c5b2abe0SLewanczyk, Dawid  */
240b5a76932SEd Tanous inline void
2418d1b46d7Szhanghch05     getComputerSystem(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
242b5a76932SEd Tanous                       const std::shared_ptr<HealthPopulate>& systemHealth)
2431abe55efSEd Tanous {
24455c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get available system components.";
2459d3ae10eSAlpana Kumari 
24655c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
2475bc2dc8eSJames Feist         [aResp, systemHealth](
248c5b2abe0SLewanczyk, Dawid             const boost::system::error_code ec,
249c5b2abe0SLewanczyk, Dawid             const std::vector<std::pair<
2506c34de48SEd Tanous                 std::string,
2511214b7e7SGunnar Mills                 std::vector<std::pair<std::string, std::vector<std::string>>>>>&
2521214b7e7SGunnar Mills                 subtree) {
2531abe55efSEd Tanous             if (ec)
2541abe55efSEd Tanous             {
25555c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error";
256f12894f8SJason M. Bills                 messages::internalError(aResp->res);
257c5b2abe0SLewanczyk, Dawid                 return;
258c5b2abe0SLewanczyk, Dawid             }
259c5b2abe0SLewanczyk, Dawid             // Iterate over all retrieved ObjectPaths.
2606c34de48SEd Tanous             for (const std::pair<std::string,
2616c34de48SEd Tanous                                  std::vector<std::pair<
2621214b7e7SGunnar Mills                                      std::string, std::vector<std::string>>>>&
2631214b7e7SGunnar Mills                      object : subtree)
2641abe55efSEd Tanous             {
265c5b2abe0SLewanczyk, Dawid                 const std::string& path = object.first;
26655c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "Got path: " << path;
2671abe55efSEd Tanous                 const std::vector<
2681214b7e7SGunnar Mills                     std::pair<std::string, std::vector<std::string>>>&
2691214b7e7SGunnar Mills                     connectionNames = object.second;
2701abe55efSEd Tanous                 if (connectionNames.size() < 1)
2711abe55efSEd Tanous                 {
272c5b2abe0SLewanczyk, Dawid                     continue;
273c5b2abe0SLewanczyk, Dawid                 }
274029573d4SEd Tanous 
2755bc2dc8eSJames Feist                 auto memoryHealth = std::make_shared<HealthPopulate>(
2765bc2dc8eSJames Feist                     aResp, aResp->res.jsonValue["MemorySummary"]["Status"]);
2775bc2dc8eSJames Feist 
2785bc2dc8eSJames Feist                 auto cpuHealth = std::make_shared<HealthPopulate>(
2795bc2dc8eSJames Feist                     aResp, aResp->res.jsonValue["ProcessorSummary"]["Status"]);
2805bc2dc8eSJames Feist 
2815bc2dc8eSJames Feist                 systemHealth->children.emplace_back(memoryHealth);
2825bc2dc8eSJames Feist                 systemHealth->children.emplace_back(cpuHealth);
2835bc2dc8eSJames Feist 
2846c34de48SEd Tanous                 // This is not system, so check if it's cpu, dimm, UUID or
2856c34de48SEd Tanous                 // BiosVer
28604a258f4SEd Tanous                 for (const auto& connection : connectionNames)
2871abe55efSEd Tanous                 {
28804a258f4SEd Tanous                     for (const auto& interfaceName : connection.second)
2891abe55efSEd Tanous                     {
29004a258f4SEd Tanous                         if (interfaceName ==
29104a258f4SEd Tanous                             "xyz.openbmc_project.Inventory.Item.Dimm")
2921abe55efSEd Tanous                         {
2931abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
29404a258f4SEd Tanous                                 << "Found Dimm, now get its properties.";
2959d3ae10eSAlpana Kumari 
29655c7b7a2SEd Tanous                             crow::connections::systemBus->async_method_call(
2979d3ae10eSAlpana Kumari                                 [aResp, service{connection.first},
298f23b7296SEd Tanous                                  path](const boost::system::error_code ec2,
299168e20c1SEd Tanous                                        const std::vector<std::pair<
300168e20c1SEd Tanous                                            std::string,
301168e20c1SEd Tanous                                            dbus::utility::DbusVariantType>>&
3021214b7e7SGunnar Mills                                            properties) {
303cb13a392SEd Tanous                                     if (ec2)
3041abe55efSEd Tanous                                     {
3051abe55efSEd Tanous                                         BMCWEB_LOG_ERROR
306cb13a392SEd Tanous                                             << "DBUS response error " << ec2;
307f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
308c5b2abe0SLewanczyk, Dawid                                         return;
309c5b2abe0SLewanczyk, Dawid                                     }
3106c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
3116c34de48SEd Tanous                                                      << properties.size()
312c5b2abe0SLewanczyk, Dawid                                                      << " Dimm properties.";
3139d3ae10eSAlpana Kumari 
3149d3ae10eSAlpana Kumari                                     if (properties.size() > 0)
3159d3ae10eSAlpana Kumari                                     {
316168e20c1SEd Tanous                                         for (const std::pair<
317168e20c1SEd Tanous                                                  std::string,
318168e20c1SEd Tanous                                                  dbus::utility::
319168e20c1SEd Tanous                                                      DbusVariantType>&
3201214b7e7SGunnar Mills                                                  property : properties)
3211abe55efSEd Tanous                                         {
3225fd7ba65SCheng C Yang                                             if (property.first !=
3235fd7ba65SCheng C Yang                                                 "MemorySizeInKB")
3241abe55efSEd Tanous                                             {
3255fd7ba65SCheng C Yang                                                 continue;
3265fd7ba65SCheng C Yang                                             }
3275fd7ba65SCheng C Yang                                             const uint32_t* value =
3288d78b7a9SPatrick Williams                                                 std::get_if<uint32_t>(
3291b6b96c5SEd Tanous                                                     &property.second);
3305fd7ba65SCheng C Yang                                             if (value == nullptr)
3311abe55efSEd Tanous                                             {
3325fd7ba65SCheng C Yang                                                 BMCWEB_LOG_DEBUG
3330fda0f12SGeorge Liu                                                     << "Find incorrect type of MemorySize";
3345fd7ba65SCheng C Yang                                                 continue;
3355fd7ba65SCheng C Yang                                             }
3365fd7ba65SCheng C Yang                                             nlohmann::json& totalMemory =
3370fda0f12SGeorge Liu                                                 aResp->res.jsonValue
3380fda0f12SGeorge Liu                                                     ["MemorySummary"]
3390fda0f12SGeorge Liu                                                     ["TotalSystemMemoryGiB"];
3405fd7ba65SCheng C Yang                                             uint64_t* preValue =
3415fd7ba65SCheng C Yang                                                 totalMemory
3425fd7ba65SCheng C Yang                                                     .get_ptr<uint64_t*>();
3435fd7ba65SCheng C Yang                                             if (preValue == nullptr)
3445fd7ba65SCheng C Yang                                             {
3455fd7ba65SCheng C Yang                                                 continue;
3465fd7ba65SCheng C Yang                                             }
3470fda0f12SGeorge Liu                                             aResp->res.jsonValue
3480fda0f12SGeorge Liu                                                 ["MemorySummary"]
3490fda0f12SGeorge Liu                                                 ["TotalSystemMemoryGiB"] =
3505fd7ba65SCheng C Yang                                                 *value / (1024 * 1024) +
3515fd7ba65SCheng C Yang                                                 *preValue;
3525fd7ba65SCheng C Yang                                             aResp->res
3535fd7ba65SCheng C Yang                                                 .jsonValue["MemorySummary"]
3549d3ae10eSAlpana Kumari                                                           ["Status"]["State"] =
3551abe55efSEd Tanous                                                 "Enabled";
356c5b2abe0SLewanczyk, Dawid                                         }
357c5b2abe0SLewanczyk, Dawid                                     }
3589d3ae10eSAlpana Kumari                                     else
3599d3ae10eSAlpana Kumari                                     {
360*1e1e598dSJonathan Doman                                         sdbusplus::asio::getProperty<bool>(
361*1e1e598dSJonathan Doman                                             *crow::connections::systemBus,
362*1e1e598dSJonathan Doman                                             service, path,
363*1e1e598dSJonathan Doman                                             "xyz.openbmc_project.State."
364*1e1e598dSJonathan Doman                                             "Decorator.OperationalStatus",
365*1e1e598dSJonathan Doman                                             "Functional",
3669d3ae10eSAlpana Kumari                                             [aResp](
3679d3ae10eSAlpana Kumari                                                 const boost::system::error_code
368cb13a392SEd Tanous                                                     ec3,
369*1e1e598dSJonathan Doman                                                 bool dimmState) {
370cb13a392SEd Tanous                                                 if (ec3)
3719d3ae10eSAlpana Kumari                                                 {
3729d3ae10eSAlpana Kumari                                                     BMCWEB_LOG_ERROR
3730fda0f12SGeorge Liu                                                         << "DBUS response error "
374cb13a392SEd Tanous                                                         << ec3;
3759d3ae10eSAlpana Kumari                                                     return;
3769d3ae10eSAlpana Kumari                                                 }
3779d3ae10eSAlpana Kumari                                                 updateDimmProperties(aResp,
3789d3ae10eSAlpana Kumari                                                                      dimmState);
379*1e1e598dSJonathan Doman                                             });
3809d3ae10eSAlpana Kumari                                     }
381c5b2abe0SLewanczyk, Dawid                                 },
38204a258f4SEd Tanous                                 connection.first, path,
3836c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
3846c34de48SEd Tanous                                 "xyz.openbmc_project.Inventory.Item.Dimm");
3855bc2dc8eSJames Feist 
3865bc2dc8eSJames Feist                             memoryHealth->inventory.emplace_back(path);
3871abe55efSEd Tanous                         }
38804a258f4SEd Tanous                         else if (interfaceName ==
38904a258f4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.Cpu")
3901abe55efSEd Tanous                         {
3911abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
39204a258f4SEd Tanous                                 << "Found Cpu, now get its properties.";
39357e8c9beSAlpana Kumari 
39403fbed92SAli Ahmed                             getProcessorSummary(aResp, connection.first, path);
3955bc2dc8eSJames Feist 
3965bc2dc8eSJames Feist                             cpuHealth->inventory.emplace_back(path);
3971abe55efSEd Tanous                         }
39804a258f4SEd Tanous                         else if (interfaceName ==
39904a258f4SEd Tanous                                  "xyz.openbmc_project.Common.UUID")
4001abe55efSEd Tanous                         {
4011abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
40204a258f4SEd Tanous                                 << "Found UUID, now get its properties.";
40355c7b7a2SEd Tanous                             crow::connections::systemBus->async_method_call(
404168e20c1SEd Tanous                                 [aResp](const boost::system::error_code ec3,
405168e20c1SEd Tanous                                         const std::vector<std::pair<
406168e20c1SEd Tanous                                             std::string,
407168e20c1SEd Tanous                                             dbus::utility::DbusVariantType>>&
4081214b7e7SGunnar Mills                                             properties) {
409cb13a392SEd Tanous                                     if (ec3)
4101abe55efSEd Tanous                                     {
4111abe55efSEd Tanous                                         BMCWEB_LOG_DEBUG
412cb13a392SEd Tanous                                             << "DBUS response error " << ec3;
413f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
414c5b2abe0SLewanczyk, Dawid                                         return;
415c5b2abe0SLewanczyk, Dawid                                     }
4166c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
4176c34de48SEd Tanous                                                      << properties.size()
418c5b2abe0SLewanczyk, Dawid                                                      << " UUID properties.";
419168e20c1SEd Tanous                                     for (const std::pair<
420168e20c1SEd Tanous                                              std::string,
421168e20c1SEd Tanous                                              dbus::utility::DbusVariantType>&
4221214b7e7SGunnar Mills                                              property : properties)
4231abe55efSEd Tanous                                     {
42404a258f4SEd Tanous                                         if (property.first == "UUID")
4251abe55efSEd Tanous                                         {
426c5b2abe0SLewanczyk, Dawid                                             const std::string* value =
4278d78b7a9SPatrick Williams                                                 std::get_if<std::string>(
4281b6b96c5SEd Tanous                                                     &property.second);
42904a258f4SEd Tanous 
4301abe55efSEd Tanous                                             if (value != nullptr)
4311abe55efSEd Tanous                                             {
432029573d4SEd Tanous                                                 std::string valueStr = *value;
43304a258f4SEd Tanous                                                 if (valueStr.size() == 32)
4341abe55efSEd Tanous                                                 {
435029573d4SEd Tanous                                                     valueStr.insert(8, 1, '-');
436029573d4SEd Tanous                                                     valueStr.insert(13, 1, '-');
437029573d4SEd Tanous                                                     valueStr.insert(18, 1, '-');
438029573d4SEd Tanous                                                     valueStr.insert(23, 1, '-');
43904a258f4SEd Tanous                                                 }
440029573d4SEd Tanous                                                 BMCWEB_LOG_DEBUG << "UUID = "
44104a258f4SEd Tanous                                                                  << valueStr;
442029573d4SEd Tanous                                                 aResp->res.jsonValue["UUID"] =
44304a258f4SEd Tanous                                                     valueStr;
444c5b2abe0SLewanczyk, Dawid                                             }
445c5b2abe0SLewanczyk, Dawid                                         }
446c5b2abe0SLewanczyk, Dawid                                     }
447c5b2abe0SLewanczyk, Dawid                                 },
44804a258f4SEd Tanous                                 connection.first, path,
4496c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
4501abe55efSEd Tanous                                 "xyz.openbmc_project.Common.UUID");
451c5b2abe0SLewanczyk, Dawid                         }
452029573d4SEd Tanous                         else if (interfaceName ==
453029573d4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.System")
4541abe55efSEd Tanous                         {
455029573d4SEd Tanous                             crow::connections::systemBus->async_method_call(
456168e20c1SEd Tanous                                 [aResp](const boost::system::error_code ec2,
457168e20c1SEd Tanous                                         const std::vector<std::pair<
458168e20c1SEd Tanous                                             std::string,
459168e20c1SEd Tanous                                             dbus::utility::DbusVariantType>>&
4601214b7e7SGunnar Mills                                             propertiesList) {
461cb13a392SEd Tanous                                     if (ec2)
462029573d4SEd Tanous                                     {
463e4a4b9a9SJames Feist                                         // doesn't have to include this
464e4a4b9a9SJames Feist                                         // interface
465029573d4SEd Tanous                                         return;
466029573d4SEd Tanous                                     }
467698654b6SGunnar Mills                                     BMCWEB_LOG_DEBUG
468698654b6SGunnar Mills                                         << "Got " << propertiesList.size()
469029573d4SEd Tanous                                         << " properties for system";
470168e20c1SEd Tanous                                     for (const std::pair<
471168e20c1SEd Tanous                                              std::string,
472168e20c1SEd Tanous                                              dbus::utility::DbusVariantType>&
4731214b7e7SGunnar Mills                                              property : propertiesList)
474029573d4SEd Tanous                                     {
475fc5afcf9Sbeccabroek                                         const std::string& propertyName =
476fc5afcf9Sbeccabroek                                             property.first;
477fc5afcf9Sbeccabroek                                         if ((propertyName == "PartNumber") ||
478fc5afcf9Sbeccabroek                                             (propertyName == "SerialNumber") ||
479fc5afcf9Sbeccabroek                                             (propertyName == "Manufacturer") ||
4805235d964SSunnySrivastava1984                                             (propertyName == "Model") ||
4815235d964SSunnySrivastava1984                                             (propertyName == "SubModel"))
482fc5afcf9Sbeccabroek                                         {
483029573d4SEd Tanous                                             const std::string* value =
484fc5afcf9Sbeccabroek                                                 std::get_if<std::string>(
485029573d4SEd Tanous                                                     &property.second);
486029573d4SEd Tanous                                             if (value != nullptr)
487029573d4SEd Tanous                                             {
488029573d4SEd Tanous                                                 aResp->res
489fc5afcf9Sbeccabroek                                                     .jsonValue[propertyName] =
490029573d4SEd Tanous                                                     *value;
491029573d4SEd Tanous                                             }
492029573d4SEd Tanous                                         }
493fc5afcf9Sbeccabroek                                     }
494c1e236a6SGunnar Mills 
495cb7e1e7bSAndrew Geissler                                     // Grab the bios version
496f97ddba7SGunnar Mills                                     fw_util::populateFirmwareInformation(
497cb7e1e7bSAndrew Geissler                                         aResp, fw_util::biosPurpose,
49872d566d9SGunnar Mills                                         "BiosVersion", false);
499029573d4SEd Tanous                                 },
500029573d4SEd Tanous                                 connection.first, path,
501029573d4SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
5020fda0f12SGeorge Liu                                 "xyz.openbmc_project.Inventory.Decorator.Asset");
503e4a4b9a9SJames Feist 
504*1e1e598dSJonathan Doman                             sdbusplus::asio::getProperty<std::string>(
505*1e1e598dSJonathan Doman                                 *crow::connections::systemBus, connection.first,
506*1e1e598dSJonathan Doman                                 path,
507*1e1e598dSJonathan Doman                                 "xyz.openbmc_project.Inventory.Decorator."
508*1e1e598dSJonathan Doman                                 "AssetTag",
509*1e1e598dSJonathan Doman                                 "AssetTag",
510168e20c1SEd Tanous                                 [aResp](const boost::system::error_code ec2,
511*1e1e598dSJonathan Doman                                         const std::string& value) {
512cb13a392SEd Tanous                                     if (ec2)
513e4a4b9a9SJames Feist                                     {
514e4a4b9a9SJames Feist                                         // doesn't have to include this
515e4a4b9a9SJames Feist                                         // interface
516e4a4b9a9SJames Feist                                         return;
517e4a4b9a9SJames Feist                                     }
518e4a4b9a9SJames Feist 
519*1e1e598dSJonathan Doman                                     aResp->res.jsonValue["AssetTag"] = value;
520*1e1e598dSJonathan Doman                                 });
521029573d4SEd Tanous                         }
522029573d4SEd Tanous                     }
523029573d4SEd Tanous                 }
524c5b2abe0SLewanczyk, Dawid             }
525c5b2abe0SLewanczyk, Dawid         },
526c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper",
527c5b2abe0SLewanczyk, Dawid         "/xyz/openbmc_project/object_mapper",
528c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
5296617338dSEd Tanous         "/xyz/openbmc_project/inventory", int32_t(0),
5306617338dSEd Tanous         std::array<const char*, 5>{
5316617338dSEd Tanous             "xyz.openbmc_project.Inventory.Decorator.Asset",
5326617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Cpu",
5336617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Dimm",
5346617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.System",
5356617338dSEd Tanous             "xyz.openbmc_project.Common.UUID",
5366617338dSEd Tanous         });
537c5b2abe0SLewanczyk, Dawid }
538c5b2abe0SLewanczyk, Dawid 
539c5b2abe0SLewanczyk, Dawid /**
540c5b2abe0SLewanczyk, Dawid  * @brief Retrieves host state properties over dbus
541c5b2abe0SLewanczyk, Dawid  *
542c5b2abe0SLewanczyk, Dawid  * @param[in] aResp     Shared pointer for completing asynchronous calls.
543c5b2abe0SLewanczyk, Dawid  *
544c5b2abe0SLewanczyk, Dawid  * @return None.
545c5b2abe0SLewanczyk, Dawid  */
5468d1b46d7Szhanghch05 inline void getHostState(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
5471abe55efSEd Tanous {
54855c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get host information.";
549*1e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
550*1e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
551*1e1e598dSJonathan Doman         "/xyz/openbmc_project/state/host0", "xyz.openbmc_project.State.Host",
552*1e1e598dSJonathan Doman         "CurrentHostState",
553c5d03ff4SJennifer Lee         [aResp](const boost::system::error_code ec,
554*1e1e598dSJonathan Doman                 const std::string& hostState) {
5551abe55efSEd Tanous             if (ec)
5561abe55efSEd Tanous             {
55755c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
558f12894f8SJason M. Bills                 messages::internalError(aResp->res);
559c5b2abe0SLewanczyk, Dawid                 return;
560c5b2abe0SLewanczyk, Dawid             }
5616617338dSEd Tanous 
562*1e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Host state: " << hostState;
563c5b2abe0SLewanczyk, Dawid             // Verify Host State
564*1e1e598dSJonathan Doman             if (hostState == "xyz.openbmc_project.State.Host.HostState.Running")
5651abe55efSEd Tanous             {
56655c7b7a2SEd Tanous                 aResp->res.jsonValue["PowerState"] = "On";
5676617338dSEd Tanous                 aResp->res.jsonValue["Status"]["State"] = "Enabled";
5681abe55efSEd Tanous             }
569*1e1e598dSJonathan Doman             else if (hostState ==
5700fda0f12SGeorge Liu                      "xyz.openbmc_project.State.Host.HostState.Quiesced")
5718c888608SGunnar Mills             {
5728c888608SGunnar Mills                 aResp->res.jsonValue["PowerState"] = "On";
5738c888608SGunnar Mills                 aResp->res.jsonValue["Status"]["State"] = "Quiesced";
5748c888608SGunnar Mills             }
575*1e1e598dSJonathan Doman             else if (hostState ==
5760fda0f12SGeorge Liu                      "xyz.openbmc_project.State.Host.HostState.DiagnosticMode")
57783935af9SAndrew Geissler             {
57883935af9SAndrew Geissler                 aResp->res.jsonValue["PowerState"] = "On";
57983935af9SAndrew Geissler                 aResp->res.jsonValue["Status"]["State"] = "InTest";
58083935af9SAndrew Geissler             }
5810fda0f12SGeorge Liu             else if (
582*1e1e598dSJonathan Doman                 hostState ==
5830fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Host.HostState.TransitioningToRunning")
5841a2a1437SAndrew Geissler             {
5851a2a1437SAndrew Geissler                 aResp->res.jsonValue["PowerState"] = "PoweringOn";
58615c27bf8SNoah Brewer                 aResp->res.jsonValue["Status"]["State"] = "Starting";
5871a2a1437SAndrew Geissler             }
5880fda0f12SGeorge Liu             else if (
589*1e1e598dSJonathan Doman                 hostState ==
5900fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Host.HostState.TransitioningToOff")
5911a2a1437SAndrew Geissler             {
5921a2a1437SAndrew Geissler                 aResp->res.jsonValue["PowerState"] = "PoweringOff";
5931a2a1437SAndrew Geissler                 aResp->res.jsonValue["Status"]["State"] = "Disabled";
5941a2a1437SAndrew Geissler             }
5951abe55efSEd Tanous             else
5961abe55efSEd Tanous             {
59755c7b7a2SEd Tanous                 aResp->res.jsonValue["PowerState"] = "Off";
5986617338dSEd Tanous                 aResp->res.jsonValue["Status"]["State"] = "Disabled";
599c5b2abe0SLewanczyk, Dawid             }
600*1e1e598dSJonathan Doman         });
601c5b2abe0SLewanczyk, Dawid }
602c5b2abe0SLewanczyk, Dawid 
603c5b2abe0SLewanczyk, Dawid /**
604786d0f60SGunnar Mills  * @brief Translates boot source DBUS property value to redfish.
605491d8ee7SSantosh Puranik  *
606491d8ee7SSantosh Puranik  * @param[in] dbusSource    The boot source in DBUS speak.
607491d8ee7SSantosh Puranik  *
608491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source in Redfish terms. If translation
609491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
610491d8ee7SSantosh Puranik  */
61123a21a1cSEd Tanous inline std::string dbusToRfBootSource(const std::string& dbusSource)
612491d8ee7SSantosh Puranik {
613491d8ee7SSantosh Puranik     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
614491d8ee7SSantosh Puranik     {
615491d8ee7SSantosh Puranik         return "None";
616491d8ee7SSantosh Puranik     }
6173174e4dfSEd Tanous     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Disk")
618491d8ee7SSantosh Puranik     {
619491d8ee7SSantosh Puranik         return "Hdd";
620491d8ee7SSantosh Puranik     }
6213174e4dfSEd Tanous     if (dbusSource ==
622a71dc0b7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia")
623491d8ee7SSantosh Puranik     {
624491d8ee7SSantosh Puranik         return "Cd";
625491d8ee7SSantosh Puranik     }
6263174e4dfSEd Tanous     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Network")
627491d8ee7SSantosh Puranik     {
628491d8ee7SSantosh Puranik         return "Pxe";
629491d8ee7SSantosh Puranik     }
6303174e4dfSEd Tanous     if (dbusSource ==
631944ffaf9SJohnathan Mantey         "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia")
6329f16b2c1SJennifer Lee     {
6339f16b2c1SJennifer Lee         return "Usb";
6349f16b2c1SJennifer Lee     }
635491d8ee7SSantosh Puranik     return "";
636491d8ee7SSantosh Puranik }
637491d8ee7SSantosh Puranik 
638491d8ee7SSantosh Puranik /**
639cd9a4666SKonstantin Aladyshev  * @brief Translates boot type DBUS property value to redfish.
640cd9a4666SKonstantin Aladyshev  *
641cd9a4666SKonstantin Aladyshev  * @param[in] dbusType    The boot type in DBUS speak.
642cd9a4666SKonstantin Aladyshev  *
643cd9a4666SKonstantin Aladyshev  * @return Returns as a string, the boot type in Redfish terms. If translation
644cd9a4666SKonstantin Aladyshev  * cannot be done, returns an empty string.
645cd9a4666SKonstantin Aladyshev  */
646cd9a4666SKonstantin Aladyshev inline std::string dbusToRfBootType(const std::string& dbusType)
647cd9a4666SKonstantin Aladyshev {
648cd9a4666SKonstantin Aladyshev     if (dbusType == "xyz.openbmc_project.Control.Boot.Type.Types.Legacy")
649cd9a4666SKonstantin Aladyshev     {
650cd9a4666SKonstantin Aladyshev         return "Legacy";
651cd9a4666SKonstantin Aladyshev     }
652cd9a4666SKonstantin Aladyshev     if (dbusType == "xyz.openbmc_project.Control.Boot.Type.Types.EFI")
653cd9a4666SKonstantin Aladyshev     {
654cd9a4666SKonstantin Aladyshev         return "UEFI";
655cd9a4666SKonstantin Aladyshev     }
656cd9a4666SKonstantin Aladyshev     return "";
657cd9a4666SKonstantin Aladyshev }
658cd9a4666SKonstantin Aladyshev 
659cd9a4666SKonstantin Aladyshev /**
660786d0f60SGunnar Mills  * @brief Translates boot mode DBUS property value to redfish.
661491d8ee7SSantosh Puranik  *
662491d8ee7SSantosh Puranik  * @param[in] dbusMode    The boot mode in DBUS speak.
663491d8ee7SSantosh Puranik  *
664491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode in Redfish terms. If translation
665491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
666491d8ee7SSantosh Puranik  */
66723a21a1cSEd Tanous inline std::string dbusToRfBootMode(const std::string& dbusMode)
668491d8ee7SSantosh Puranik {
669491d8ee7SSantosh Puranik     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
670491d8ee7SSantosh Puranik     {
671491d8ee7SSantosh Puranik         return "None";
672491d8ee7SSantosh Puranik     }
6733174e4dfSEd Tanous     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe")
674491d8ee7SSantosh Puranik     {
675491d8ee7SSantosh Puranik         return "Diags";
676491d8ee7SSantosh Puranik     }
6773174e4dfSEd Tanous     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup")
678491d8ee7SSantosh Puranik     {
679491d8ee7SSantosh Puranik         return "BiosSetup";
680491d8ee7SSantosh Puranik     }
681491d8ee7SSantosh Puranik     return "";
682491d8ee7SSantosh Puranik }
683491d8ee7SSantosh Puranik 
684491d8ee7SSantosh Puranik /**
685786d0f60SGunnar Mills  * @brief Translates boot source from Redfish to the DBus boot paths.
686491d8ee7SSantosh Puranik  *
687491d8ee7SSantosh Puranik  * @param[in] rfSource    The boot source in Redfish.
688944ffaf9SJohnathan Mantey  * @param[out] bootSource The DBus source
689944ffaf9SJohnathan Mantey  * @param[out] bootMode   the DBus boot mode
690491d8ee7SSantosh Puranik  *
691944ffaf9SJohnathan Mantey  * @return Integer error code.
692491d8ee7SSantosh Puranik  */
6938d1b46d7Szhanghch05 inline int assignBootParameters(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
694944ffaf9SJohnathan Mantey                                 const std::string& rfSource,
695944ffaf9SJohnathan Mantey                                 std::string& bootSource, std::string& bootMode)
696491d8ee7SSantosh Puranik {
697c21865c4SKonstantin Aladyshev     bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
698c21865c4SKonstantin Aladyshev     bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
699944ffaf9SJohnathan Mantey 
700491d8ee7SSantosh Puranik     if (rfSource == "None")
701491d8ee7SSantosh Puranik     {
702944ffaf9SJohnathan Mantey         return 0;
703491d8ee7SSantosh Puranik     }
7043174e4dfSEd Tanous     if (rfSource == "Pxe")
705491d8ee7SSantosh Puranik     {
706944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Network";
707944ffaf9SJohnathan Mantey     }
708944ffaf9SJohnathan Mantey     else if (rfSource == "Hdd")
709944ffaf9SJohnathan Mantey     {
710944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Disk";
711944ffaf9SJohnathan Mantey     }
712944ffaf9SJohnathan Mantey     else if (rfSource == "Diags")
713944ffaf9SJohnathan Mantey     {
714944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe";
715944ffaf9SJohnathan Mantey     }
716944ffaf9SJohnathan Mantey     else if (rfSource == "Cd")
717944ffaf9SJohnathan Mantey     {
718944ffaf9SJohnathan Mantey         bootSource =
719944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia";
720944ffaf9SJohnathan Mantey     }
721944ffaf9SJohnathan Mantey     else if (rfSource == "BiosSetup")
722944ffaf9SJohnathan Mantey     {
723944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup";
724491d8ee7SSantosh Puranik     }
7259f16b2c1SJennifer Lee     else if (rfSource == "Usb")
7269f16b2c1SJennifer Lee     {
727944ffaf9SJohnathan Mantey         bootSource =
728944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia";
7299f16b2c1SJennifer Lee     }
730491d8ee7SSantosh Puranik     else
731491d8ee7SSantosh Puranik     {
7320fda0f12SGeorge Liu         BMCWEB_LOG_DEBUG
7330fda0f12SGeorge Liu             << "Invalid property value for BootSourceOverrideTarget: "
734944ffaf9SJohnathan Mantey             << bootSource;
735944ffaf9SJohnathan Mantey         messages::propertyValueNotInList(aResp->res, rfSource,
736944ffaf9SJohnathan Mantey                                          "BootSourceTargetOverride");
737944ffaf9SJohnathan Mantey         return -1;
738491d8ee7SSantosh Puranik     }
739944ffaf9SJohnathan Mantey     return 0;
740491d8ee7SSantosh Puranik }
7411981771bSAli Ahmed 
742978b8803SAndrew Geissler /**
743978b8803SAndrew Geissler  * @brief Retrieves boot progress of the system
744978b8803SAndrew Geissler  *
745978b8803SAndrew Geissler  * @param[in] aResp  Shared pointer for generating response message.
746978b8803SAndrew Geissler  *
747978b8803SAndrew Geissler  * @return None.
748978b8803SAndrew Geissler  */
7498d1b46d7Szhanghch05 inline void getBootProgress(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
750978b8803SAndrew Geissler {
751*1e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
752*1e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
753*1e1e598dSJonathan Doman         "/xyz/openbmc_project/state/host0",
754*1e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.Progress", "BootProgress",
755978b8803SAndrew Geissler         [aResp](const boost::system::error_code ec,
756*1e1e598dSJonathan Doman                 const std::string& bootProgressStr) {
757978b8803SAndrew Geissler             if (ec)
758978b8803SAndrew Geissler             {
759978b8803SAndrew Geissler                 // BootProgress is an optional object so just do nothing if
760978b8803SAndrew Geissler                 // not found
761978b8803SAndrew Geissler                 return;
762978b8803SAndrew Geissler             }
763978b8803SAndrew Geissler 
764*1e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Boot Progress: " << bootProgressStr;
765978b8803SAndrew Geissler 
766978b8803SAndrew Geissler             // Now convert the D-Bus BootProgress to the appropriate Redfish
767978b8803SAndrew Geissler             // enum
768978b8803SAndrew Geissler             std::string rfBpLastState = "None";
769*1e1e598dSJonathan Doman             if (bootProgressStr ==
7700fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.Unspecified")
771978b8803SAndrew Geissler             {
772978b8803SAndrew Geissler                 rfBpLastState = "None";
773978b8803SAndrew Geissler             }
7740fda0f12SGeorge Liu             else if (
775*1e1e598dSJonathan Doman                 bootProgressStr ==
7760fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.PrimaryProcInit")
777978b8803SAndrew Geissler             {
778978b8803SAndrew Geissler                 rfBpLastState = "PrimaryProcessorInitializationStarted";
779978b8803SAndrew Geissler             }
7800fda0f12SGeorge Liu             else if (
781*1e1e598dSJonathan Doman                 bootProgressStr ==
7820fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.BusInit")
783978b8803SAndrew Geissler             {
784978b8803SAndrew Geissler                 rfBpLastState = "BusInitializationStarted";
785978b8803SAndrew Geissler             }
7860fda0f12SGeorge Liu             else if (
787*1e1e598dSJonathan Doman                 bootProgressStr ==
7880fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.MemoryInit")
789978b8803SAndrew Geissler             {
790978b8803SAndrew Geissler                 rfBpLastState = "MemoryInitializationStarted";
791978b8803SAndrew Geissler             }
7920fda0f12SGeorge Liu             else if (
793*1e1e598dSJonathan Doman                 bootProgressStr ==
7940fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.SecondaryProcInit")
795978b8803SAndrew Geissler             {
796978b8803SAndrew Geissler                 rfBpLastState = "SecondaryProcessorInitializationStarted";
797978b8803SAndrew Geissler             }
7980fda0f12SGeorge Liu             else if (
799*1e1e598dSJonathan Doman                 bootProgressStr ==
8000fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.PCIInit")
801978b8803SAndrew Geissler             {
802978b8803SAndrew Geissler                 rfBpLastState = "PCIResourceConfigStarted";
803978b8803SAndrew Geissler             }
8040fda0f12SGeorge Liu             else if (
805*1e1e598dSJonathan Doman                 bootProgressStr ==
8060fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.SystemInitComplete")
807978b8803SAndrew Geissler             {
808978b8803SAndrew Geissler                 rfBpLastState = "SystemHardwareInitializationComplete";
809978b8803SAndrew Geissler             }
8100fda0f12SGeorge Liu             else if (
811*1e1e598dSJonathan Doman                 bootProgressStr ==
8120fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.OSStart")
813978b8803SAndrew Geissler             {
814978b8803SAndrew Geissler                 rfBpLastState = "OSBootStarted";
815978b8803SAndrew Geissler             }
8160fda0f12SGeorge Liu             else if (
817*1e1e598dSJonathan Doman                 bootProgressStr ==
8180fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.OSRunning")
819978b8803SAndrew Geissler             {
820978b8803SAndrew Geissler                 rfBpLastState = "OSRunning";
821978b8803SAndrew Geissler             }
822978b8803SAndrew Geissler             else
823978b8803SAndrew Geissler             {
824978b8803SAndrew Geissler                 BMCWEB_LOG_DEBUG << "Unsupported D-Bus BootProgress "
825*1e1e598dSJonathan Doman                                  << bootProgressStr;
826978b8803SAndrew Geissler                 // Just return the default
827978b8803SAndrew Geissler             }
828978b8803SAndrew Geissler 
829978b8803SAndrew Geissler             aResp->res.jsonValue["BootProgress"]["LastState"] = rfBpLastState;
830*1e1e598dSJonathan Doman         });
831978b8803SAndrew Geissler }
832491d8ee7SSantosh Puranik 
833491d8ee7SSantosh Puranik /**
834c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override type over DBUS and fills out the response
835cd9a4666SKonstantin Aladyshev  *
836cd9a4666SKonstantin Aladyshev  * @param[in] aResp         Shared pointer for generating response message.
837cd9a4666SKonstantin Aladyshev  *
838cd9a4666SKonstantin Aladyshev  * @return None.
839cd9a4666SKonstantin Aladyshev  */
840cd9a4666SKonstantin Aladyshev 
841c21865c4SKonstantin Aladyshev inline void getBootOverrideType(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
842cd9a4666SKonstantin Aladyshev {
843*1e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
844*1e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
845*1e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
846*1e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Type", "BootType",
847cd9a4666SKonstantin Aladyshev         [aResp](const boost::system::error_code ec,
848*1e1e598dSJonathan Doman                 const std::string& bootType) {
849cd9a4666SKonstantin Aladyshev             if (ec)
850cd9a4666SKonstantin Aladyshev             {
851cd9a4666SKonstantin Aladyshev                 // not an error, don't have to have the interface
852cd9a4666SKonstantin Aladyshev                 return;
853cd9a4666SKonstantin Aladyshev             }
854cd9a4666SKonstantin Aladyshev 
855*1e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Boot type: " << bootType;
856cd9a4666SKonstantin Aladyshev 
8570fda0f12SGeorge Liu             aResp->res
8580fda0f12SGeorge Liu                 .jsonValue["Boot"]
8590fda0f12SGeorge Liu                           ["BootSourceOverrideMode@Redfish.AllowableValues"] = {
8600fda0f12SGeorge Liu                 "Legacy", "UEFI"};
861cd9a4666SKonstantin Aladyshev 
862*1e1e598dSJonathan Doman             auto rfType = dbusToRfBootType(bootType);
863cd9a4666SKonstantin Aladyshev             if (rfType.empty())
864cd9a4666SKonstantin Aladyshev             {
865cd9a4666SKonstantin Aladyshev                 messages::internalError(aResp->res);
866cd9a4666SKonstantin Aladyshev                 return;
867cd9a4666SKonstantin Aladyshev             }
868cd9a4666SKonstantin Aladyshev 
869cd9a4666SKonstantin Aladyshev             aResp->res.jsonValue["Boot"]["BootSourceOverrideMode"] = rfType;
870*1e1e598dSJonathan Doman         });
871cd9a4666SKonstantin Aladyshev }
872cd9a4666SKonstantin Aladyshev 
873cd9a4666SKonstantin Aladyshev /**
874c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override mode over DBUS and fills out the response
875491d8ee7SSantosh Puranik  *
876491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
877491d8ee7SSantosh Puranik  *
878491d8ee7SSantosh Puranik  * @return None.
879491d8ee7SSantosh Puranik  */
880c21865c4SKonstantin Aladyshev 
881c21865c4SKonstantin Aladyshev inline void getBootOverrideMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
882491d8ee7SSantosh Puranik {
883*1e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
884*1e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
885*1e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
886*1e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
887c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec,
888*1e1e598dSJonathan Doman                 const std::string& bootModeStr) {
889491d8ee7SSantosh Puranik             if (ec)
890491d8ee7SSantosh Puranik             {
891491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
892491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
893491d8ee7SSantosh Puranik                 return;
894491d8ee7SSantosh Puranik             }
895491d8ee7SSantosh Puranik 
896*1e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Boot mode: " << bootModeStr;
897491d8ee7SSantosh Puranik 
8980fda0f12SGeorge Liu             aResp->res
8990fda0f12SGeorge Liu                 .jsonValue["Boot"]
9000fda0f12SGeorge Liu                           ["BootSourceOverrideTarget@Redfish.AllowableValues"] =
9010fda0f12SGeorge Liu                 {"None", "Pxe", "Hdd", "Cd", "Diags", "BiosSetup", "Usb"};
902491d8ee7SSantosh Puranik 
903*1e1e598dSJonathan Doman             if (bootModeStr !=
904491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
905491d8ee7SSantosh Puranik             {
906*1e1e598dSJonathan Doman                 auto rfMode = dbusToRfBootMode(bootModeStr);
907491d8ee7SSantosh Puranik                 if (!rfMode.empty())
908491d8ee7SSantosh Puranik                 {
909491d8ee7SSantosh Puranik                     aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
910491d8ee7SSantosh Puranik                         rfMode;
911491d8ee7SSantosh Puranik                 }
912491d8ee7SSantosh Puranik             }
913*1e1e598dSJonathan Doman         });
914491d8ee7SSantosh Puranik }
915491d8ee7SSantosh Puranik 
916491d8ee7SSantosh Puranik /**
917c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override source over DBUS
918491d8ee7SSantosh Puranik  *
919491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
920491d8ee7SSantosh Puranik  *
921491d8ee7SSantosh Puranik  * @return None.
922491d8ee7SSantosh Puranik  */
923c21865c4SKonstantin Aladyshev 
924c21865c4SKonstantin Aladyshev inline void
925c21865c4SKonstantin Aladyshev     getBootOverrideSource(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
926491d8ee7SSantosh Puranik {
927*1e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
928*1e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
929*1e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
930*1e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Source", "BootSource",
931c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec,
932*1e1e598dSJonathan Doman                 const std::string& bootSourceStr) {
933491d8ee7SSantosh Puranik             if (ec)
934491d8ee7SSantosh Puranik             {
935491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
936491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
937491d8ee7SSantosh Puranik                 return;
938491d8ee7SSantosh Puranik             }
939491d8ee7SSantosh Puranik 
940*1e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Boot source: " << bootSourceStr;
941491d8ee7SSantosh Puranik 
942*1e1e598dSJonathan Doman             auto rfSource = dbusToRfBootSource(bootSourceStr);
943491d8ee7SSantosh Puranik             if (!rfSource.empty())
944491d8ee7SSantosh Puranik             {
945491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
946491d8ee7SSantosh Puranik                     rfSource;
947491d8ee7SSantosh Puranik             }
948cd9a4666SKonstantin Aladyshev 
949cd9a4666SKonstantin Aladyshev             // Get BootMode as BootSourceOverrideTarget is constructed
950cd9a4666SKonstantin Aladyshev             // from both BootSource and BootMode
951c21865c4SKonstantin Aladyshev             getBootOverrideMode(aResp);
952*1e1e598dSJonathan Doman         });
953491d8ee7SSantosh Puranik }
954491d8ee7SSantosh Puranik 
955491d8ee7SSantosh Puranik /**
956c21865c4SKonstantin Aladyshev  * @brief This functions abstracts all the logic behind getting a
957c21865c4SKonstantin Aladyshev  * "BootSourceOverrideEnabled" property from an overall boot override enable
958c21865c4SKonstantin Aladyshev  * state
959491d8ee7SSantosh Puranik  *
960491d8ee7SSantosh Puranik  * @param[in] aResp     Shared pointer for generating response message.
961491d8ee7SSantosh Puranik  *
962491d8ee7SSantosh Puranik  * @return None.
963491d8ee7SSantosh Puranik  */
964491d8ee7SSantosh Puranik 
965c21865c4SKonstantin Aladyshev inline void
966c21865c4SKonstantin Aladyshev     processBootOverrideEnable(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
967c21865c4SKonstantin Aladyshev                               const bool bootOverrideEnableSetting)
968c21865c4SKonstantin Aladyshev {
969c21865c4SKonstantin Aladyshev     if (!bootOverrideEnableSetting)
970c21865c4SKonstantin Aladyshev     {
971c21865c4SKonstantin Aladyshev         aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] = "Disabled";
972c21865c4SKonstantin Aladyshev         return;
973c21865c4SKonstantin Aladyshev     }
974c21865c4SKonstantin Aladyshev 
975c21865c4SKonstantin Aladyshev     // If boot source override is enabled, we need to check 'one_time'
976c21865c4SKonstantin Aladyshev     // property to set a correct value for the "BootSourceOverrideEnabled"
977*1e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
978*1e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
979*1e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot/one_time",
980*1e1e598dSJonathan Doman         "xyz.openbmc_project.Object.Enable", "Enabled",
981*1e1e598dSJonathan Doman         [aResp](const boost::system::error_code ec, bool oneTimeSetting) {
982491d8ee7SSantosh Puranik             if (ec)
983491d8ee7SSantosh Puranik             {
984491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
985c21865c4SKonstantin Aladyshev                 messages::internalError(aResp->res);
986491d8ee7SSantosh Puranik                 return;
987491d8ee7SSantosh Puranik             }
988491d8ee7SSantosh Puranik 
989c21865c4SKonstantin Aladyshev             if (oneTimeSetting)
990c21865c4SKonstantin Aladyshev             {
991c21865c4SKonstantin Aladyshev                 aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
992c21865c4SKonstantin Aladyshev                     "Once";
993c21865c4SKonstantin Aladyshev             }
994c21865c4SKonstantin Aladyshev             else
995c21865c4SKonstantin Aladyshev             {
996c21865c4SKonstantin Aladyshev                 aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
997c21865c4SKonstantin Aladyshev                     "Continuous";
998c21865c4SKonstantin Aladyshev             }
999*1e1e598dSJonathan Doman         });
1000491d8ee7SSantosh Puranik }
1001491d8ee7SSantosh Puranik 
1002491d8ee7SSantosh Puranik /**
1003c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override enable over DBUS
1004c21865c4SKonstantin Aladyshev  *
1005c21865c4SKonstantin Aladyshev  * @param[in] aResp     Shared pointer for generating response message.
1006c21865c4SKonstantin Aladyshev  *
1007c21865c4SKonstantin Aladyshev  * @return None.
1008c21865c4SKonstantin Aladyshev  */
1009c21865c4SKonstantin Aladyshev 
1010c21865c4SKonstantin Aladyshev inline void
1011c21865c4SKonstantin Aladyshev     getBootOverrideEnable(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1012c21865c4SKonstantin Aladyshev {
1013*1e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
1014*1e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
1015*1e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
1016*1e1e598dSJonathan Doman         "xyz.openbmc_project.Object.Enable", "Enabled",
1017c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec,
1018*1e1e598dSJonathan Doman                 const bool bootOverrideEnable) {
1019c21865c4SKonstantin Aladyshev             if (ec)
1020c21865c4SKonstantin Aladyshev             {
1021c21865c4SKonstantin Aladyshev                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1022c21865c4SKonstantin Aladyshev                 messages::internalError(aResp->res);
1023c21865c4SKonstantin Aladyshev                 return;
1024c21865c4SKonstantin Aladyshev             }
1025c21865c4SKonstantin Aladyshev 
1026*1e1e598dSJonathan Doman             processBootOverrideEnable(aResp, bootOverrideEnable);
1027*1e1e598dSJonathan Doman         });
1028c21865c4SKonstantin Aladyshev }
1029c21865c4SKonstantin Aladyshev 
1030c21865c4SKonstantin Aladyshev /**
1031c21865c4SKonstantin Aladyshev  * @brief Retrieves boot source override properties
1032c21865c4SKonstantin Aladyshev  *
1033c21865c4SKonstantin Aladyshev  * @param[in] aResp     Shared pointer for generating response message.
1034c21865c4SKonstantin Aladyshev  *
1035c21865c4SKonstantin Aladyshev  * @return None.
1036c21865c4SKonstantin Aladyshev  */
1037c21865c4SKonstantin Aladyshev inline void getBootProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1038c21865c4SKonstantin Aladyshev {
1039c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Get boot information.";
1040c21865c4SKonstantin Aladyshev 
1041c21865c4SKonstantin Aladyshev     getBootOverrideSource(aResp);
1042c21865c4SKonstantin Aladyshev     getBootOverrideType(aResp);
1043c21865c4SKonstantin Aladyshev     getBootOverrideEnable(aResp);
1044c21865c4SKonstantin Aladyshev }
1045c21865c4SKonstantin Aladyshev 
1046c21865c4SKonstantin Aladyshev /**
1047c0557e1aSGunnar Mills  * @brief Retrieves the Last Reset Time
1048c0557e1aSGunnar Mills  *
1049c0557e1aSGunnar Mills  * "Reset" is an overloaded term in Redfish, "Reset" includes power on
1050c0557e1aSGunnar Mills  * and power off. Even though this is the "system" Redfish object look at the
1051c0557e1aSGunnar Mills  * chassis D-Bus interface for the LastStateChangeTime since this has the
1052c0557e1aSGunnar Mills  * last power operation time.
1053c0557e1aSGunnar Mills  *
1054c0557e1aSGunnar Mills  * @param[in] aResp     Shared pointer for generating response message.
1055c0557e1aSGunnar Mills  *
1056c0557e1aSGunnar Mills  * @return None.
1057c0557e1aSGunnar Mills  */
10588d1b46d7Szhanghch05 inline void getLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1059c0557e1aSGunnar Mills {
1060c0557e1aSGunnar Mills     BMCWEB_LOG_DEBUG << "Getting System Last Reset Time";
1061c0557e1aSGunnar Mills 
1062*1e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
1063*1e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Chassis",
1064*1e1e598dSJonathan Doman         "/xyz/openbmc_project/state/chassis0",
1065*1e1e598dSJonathan Doman         "xyz.openbmc_project.State.Chassis", "LastStateChangeTime",
1066*1e1e598dSJonathan Doman         [aResp](const boost::system::error_code ec, uint64_t lastResetTime) {
1067c0557e1aSGunnar Mills             if (ec)
1068c0557e1aSGunnar Mills             {
1069c0557e1aSGunnar Mills                 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
1070c0557e1aSGunnar Mills                 return;
1071c0557e1aSGunnar Mills             }
1072c0557e1aSGunnar Mills 
1073c0557e1aSGunnar Mills             // LastStateChangeTime is epoch time, in milliseconds
1074c0557e1aSGunnar Mills             // https://github.com/openbmc/phosphor-dbus-interfaces/blob/33e8e1dd64da53a66e888d33dc82001305cd0bf9/xyz/openbmc_project/State/Chassis.interface.yaml#L19
1075*1e1e598dSJonathan Doman             uint64_t lastResetTimeStamp = lastResetTime / 1000;
1076c0557e1aSGunnar Mills 
1077c0557e1aSGunnar Mills             // Convert to ISO 8601 standard
1078c0557e1aSGunnar Mills             aResp->res.jsonValue["LastResetTime"] =
10791d8782e7SNan Zhou                 crow::utility::getDateTimeUint(lastResetTimeStamp);
1080*1e1e598dSJonathan Doman         });
1081c0557e1aSGunnar Mills }
1082c0557e1aSGunnar Mills 
1083c0557e1aSGunnar Mills /**
10846bd5a8d2SGunnar Mills  * @brief Retrieves Automatic Retry properties. Known on D-Bus as AutoReboot.
10856bd5a8d2SGunnar Mills  *
10866bd5a8d2SGunnar Mills  * @param[in] aResp     Shared pointer for generating response message.
10876bd5a8d2SGunnar Mills  *
10886bd5a8d2SGunnar Mills  * @return None.
10896bd5a8d2SGunnar Mills  */
10908d1b46d7Szhanghch05 inline void getAutomaticRetry(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
10916bd5a8d2SGunnar Mills {
10926bd5a8d2SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Automatic Retry policy";
10936bd5a8d2SGunnar Mills 
1094*1e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
1095*1e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
1096*1e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/auto_reboot",
1097*1e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
1098*1e1e598dSJonathan Doman         [aResp](const boost::system::error_code ec, bool autoRebootEnabled) {
10996bd5a8d2SGunnar Mills             if (ec)
11006bd5a8d2SGunnar Mills             {
11016bd5a8d2SGunnar Mills                 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
11026bd5a8d2SGunnar Mills                 return;
11036bd5a8d2SGunnar Mills             }
11046bd5a8d2SGunnar Mills 
1105*1e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Auto Reboot: " << autoRebootEnabled;
1106*1e1e598dSJonathan Doman             if (autoRebootEnabled == true)
11076bd5a8d2SGunnar Mills             {
11086bd5a8d2SGunnar Mills                 aResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
11096bd5a8d2SGunnar Mills                     "RetryAttempts";
11106bd5a8d2SGunnar Mills                 // If AutomaticRetry (AutoReboot) is enabled see how many
11116bd5a8d2SGunnar Mills                 // attempts are left
1112*1e1e598dSJonathan Doman                 sdbusplus::asio::getProperty<uint32_t>(
1113*1e1e598dSJonathan Doman                     *crow::connections::systemBus,
1114*1e1e598dSJonathan Doman                     "xyz.openbmc_project.State.Host",
1115*1e1e598dSJonathan Doman                     "/xyz/openbmc_project/state/host0",
1116*1e1e598dSJonathan Doman                     "xyz.openbmc_project.Control.Boot.RebootAttempts",
1117*1e1e598dSJonathan Doman                     "AttemptsLeft",
1118cb13a392SEd Tanous                     [aResp](const boost::system::error_code ec2,
1119*1e1e598dSJonathan Doman                             uint32_t autoRebootAttemptsLeft) {
1120cb13a392SEd Tanous                         if (ec2)
11216bd5a8d2SGunnar Mills                         {
1122cb13a392SEd Tanous                             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec2;
11236bd5a8d2SGunnar Mills                             return;
11246bd5a8d2SGunnar Mills                         }
11256bd5a8d2SGunnar Mills 
11266bd5a8d2SGunnar Mills                         BMCWEB_LOG_DEBUG << "Auto Reboot Attempts Left: "
1127*1e1e598dSJonathan Doman                                          << autoRebootAttemptsLeft;
11286bd5a8d2SGunnar Mills 
11296bd5a8d2SGunnar Mills                         aResp->res
11306bd5a8d2SGunnar Mills                             .jsonValue["Boot"]
11316bd5a8d2SGunnar Mills                                       ["RemainingAutomaticRetryAttempts"] =
1132*1e1e598dSJonathan Doman                             autoRebootAttemptsLeft;
1133*1e1e598dSJonathan Doman                     });
11346bd5a8d2SGunnar Mills             }
11356bd5a8d2SGunnar Mills             else
11366bd5a8d2SGunnar Mills             {
11376bd5a8d2SGunnar Mills                 aResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
11386bd5a8d2SGunnar Mills                     "Disabled";
11396bd5a8d2SGunnar Mills             }
11406bd5a8d2SGunnar Mills 
11416bd5a8d2SGunnar Mills             // Not on D-Bus. Hardcoded here:
11426bd5a8d2SGunnar Mills             // https://github.com/openbmc/phosphor-state-manager/blob/1dbbef42675e94fb1f78edb87d6b11380260535a/meson_options.txt#L71
11436bd5a8d2SGunnar Mills             aResp->res.jsonValue["Boot"]["AutomaticRetryAttempts"] = 3;
114469f35306SGunnar Mills 
114569f35306SGunnar Mills             // "AutomaticRetryConfig" can be 3 values, Disabled, RetryAlways,
114669f35306SGunnar Mills             // and RetryAttempts. OpenBMC only supports Disabled and
114769f35306SGunnar Mills             // RetryAttempts.
11480fda0f12SGeorge Liu             aResp->res
11490fda0f12SGeorge Liu                 .jsonValue["Boot"]
11500fda0f12SGeorge Liu                           ["AutomaticRetryConfig@Redfish.AllowableValues"] = {
11510fda0f12SGeorge Liu                 "Disabled", "RetryAttempts"};
1152*1e1e598dSJonathan Doman         });
11536bd5a8d2SGunnar Mills }
11546bd5a8d2SGunnar Mills 
11556bd5a8d2SGunnar Mills /**
1156c6a620f2SGeorge Liu  * @brief Retrieves power restore policy over DBUS.
1157c6a620f2SGeorge Liu  *
1158c6a620f2SGeorge Liu  * @param[in] aResp     Shared pointer for generating response message.
1159c6a620f2SGeorge Liu  *
1160c6a620f2SGeorge Liu  * @return None.
1161c6a620f2SGeorge Liu  */
11628d1b46d7Szhanghch05 inline void
11638d1b46d7Szhanghch05     getPowerRestorePolicy(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1164c6a620f2SGeorge Liu {
1165c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Get power restore policy";
1166c6a620f2SGeorge Liu 
1167*1e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
1168*1e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
1169*1e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/power_restore_policy",
1170*1e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
1171*1e1e598dSJonathan Doman         [aResp](const boost::system::error_code ec, const std::string& policy) {
1172c6a620f2SGeorge Liu             if (ec)
1173c6a620f2SGeorge Liu             {
1174c6a620f2SGeorge Liu                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1175c6a620f2SGeorge Liu                 return;
1176c6a620f2SGeorge Liu             }
1177c6a620f2SGeorge Liu 
11780fda0f12SGeorge Liu             const boost::container::flat_map<std::string, std::string> policyMaps = {
11790fda0f12SGeorge Liu                 {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn",
1180c6a620f2SGeorge Liu                  "AlwaysOn"},
11810fda0f12SGeorge Liu                 {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff",
1182c6a620f2SGeorge Liu                  "AlwaysOff"},
11830fda0f12SGeorge Liu                 {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore",
1184c6a620f2SGeorge Liu                  "LastState"}};
1185c6a620f2SGeorge Liu 
1186*1e1e598dSJonathan Doman             auto policyMapsIt = policyMaps.find(policy);
1187c6a620f2SGeorge Liu             if (policyMapsIt == policyMaps.end())
1188c6a620f2SGeorge Liu             {
1189c6a620f2SGeorge Liu                 messages::internalError(aResp->res);
1190c6a620f2SGeorge Liu                 return;
1191c6a620f2SGeorge Liu             }
1192c6a620f2SGeorge Liu 
1193c6a620f2SGeorge Liu             aResp->res.jsonValue["PowerRestorePolicy"] = policyMapsIt->second;
1194*1e1e598dSJonathan Doman         });
1195c6a620f2SGeorge Liu }
1196c6a620f2SGeorge Liu 
1197c6a620f2SGeorge Liu /**
11981981771bSAli Ahmed  * @brief Get TrustedModuleRequiredToBoot property. Determines whether or not
11991981771bSAli Ahmed  * TPM is required for booting the host.
12001981771bSAli Ahmed  *
12011981771bSAli Ahmed  * @param[in] aResp     Shared pointer for generating response message.
12021981771bSAli Ahmed  *
12031981771bSAli Ahmed  * @return None.
12041981771bSAli Ahmed  */
12051981771bSAli Ahmed inline void getTrustedModuleRequiredToBoot(
12061981771bSAli Ahmed     const std::shared_ptr<bmcweb::AsyncResp>& aResp)
12071981771bSAli Ahmed {
12081981771bSAli Ahmed     BMCWEB_LOG_DEBUG << "Get TPM required to boot.";
12091981771bSAli Ahmed 
12101981771bSAli Ahmed     crow::connections::systemBus->async_method_call(
12111981771bSAli Ahmed         [aResp](
12121981771bSAli Ahmed             const boost::system::error_code ec,
12131981771bSAli Ahmed             std::vector<std::pair<
12141981771bSAli Ahmed                 std::string,
12151981771bSAli Ahmed                 std::vector<std::pair<std::string, std::vector<std::string>>>>>&
12161981771bSAli Ahmed                 subtree) {
12171981771bSAli Ahmed             if (ec)
12181981771bSAli Ahmed             {
12191981771bSAli Ahmed                 BMCWEB_LOG_DEBUG
12201981771bSAli Ahmed                     << "DBUS response error on TPM.Policy GetSubTree" << ec;
12211981771bSAli Ahmed                 // This is an optional D-Bus object so just return if
12221981771bSAli Ahmed                 // error occurs
12231981771bSAli Ahmed                 return;
12241981771bSAli Ahmed             }
12251981771bSAli Ahmed             if (subtree.size() == 0)
12261981771bSAli Ahmed             {
12271981771bSAli Ahmed                 // As noted above, this is an optional interface so just return
12281981771bSAli Ahmed                 // if there is no instance found
12291981771bSAli Ahmed                 return;
12301981771bSAli Ahmed             }
12311981771bSAli Ahmed 
12321981771bSAli Ahmed             /* When there is more than one TPMEnable object... */
12331981771bSAli Ahmed             if (subtree.size() > 1)
12341981771bSAli Ahmed             {
12351981771bSAli Ahmed                 BMCWEB_LOG_DEBUG
12361981771bSAli Ahmed                     << "DBUS response has more than 1 TPM Enable object:"
12371981771bSAli Ahmed                     << subtree.size();
12381981771bSAli Ahmed                 // Throw an internal Error and return
12391981771bSAli Ahmed                 messages::internalError(aResp->res);
12401981771bSAli Ahmed                 return;
12411981771bSAli Ahmed             }
12421981771bSAli Ahmed 
12431981771bSAli Ahmed             // Make sure the Dbus response map has a service and objectPath
12441981771bSAli Ahmed             // field
12451981771bSAli Ahmed             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
12461981771bSAli Ahmed             {
12471981771bSAli Ahmed                 BMCWEB_LOG_DEBUG << "TPM.Policy mapper error!";
12481981771bSAli Ahmed                 messages::internalError(aResp->res);
12491981771bSAli Ahmed                 return;
12501981771bSAli Ahmed             }
12511981771bSAli Ahmed 
12521981771bSAli Ahmed             const std::string& path = subtree[0].first;
12531981771bSAli Ahmed             const std::string& serv = subtree[0].second.begin()->first;
12541981771bSAli Ahmed 
12551981771bSAli Ahmed             // Valid TPM Enable object found, now reading the current value
1256*1e1e598dSJonathan Doman             sdbusplus::asio::getProperty<bool>(
1257*1e1e598dSJonathan Doman                 *crow::connections::systemBus, serv, path,
1258*1e1e598dSJonathan Doman                 "xyz.openbmc_project.Control.TPM.Policy", "TPMEnable",
1259*1e1e598dSJonathan Doman                 [aResp](const boost::system::error_code ec, bool tpmRequired) {
12601981771bSAli Ahmed                     if (ec)
12611981771bSAli Ahmed                     {
12621981771bSAli Ahmed                         BMCWEB_LOG_DEBUG
12631981771bSAli Ahmed                             << "D-BUS response error on TPM.Policy Get" << ec;
12641981771bSAli Ahmed                         messages::internalError(aResp->res);
12651981771bSAli Ahmed                         return;
12661981771bSAli Ahmed                     }
12671981771bSAli Ahmed 
1268*1e1e598dSJonathan Doman                     if (tpmRequired)
12691981771bSAli Ahmed                     {
12701981771bSAli Ahmed                         aResp->res
12711981771bSAli Ahmed                             .jsonValue["Boot"]["TrustedModuleRequiredToBoot"] =
12721981771bSAli Ahmed                             "Required";
12731981771bSAli Ahmed                     }
12741981771bSAli Ahmed                     else
12751981771bSAli Ahmed                     {
12761981771bSAli Ahmed                         aResp->res
12771981771bSAli Ahmed                             .jsonValue["Boot"]["TrustedModuleRequiredToBoot"] =
12781981771bSAli Ahmed                             "Disabled";
12791981771bSAli Ahmed                     }
1280*1e1e598dSJonathan Doman                 });
12811981771bSAli Ahmed         },
12821981771bSAli Ahmed         "xyz.openbmc_project.ObjectMapper",
12831981771bSAli Ahmed         "/xyz/openbmc_project/object_mapper",
12841981771bSAli Ahmed         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
12851981771bSAli Ahmed         std::array<const char*, 1>{"xyz.openbmc_project.Control.TPM.Policy"});
12861981771bSAli Ahmed }
12871981771bSAli Ahmed 
12881981771bSAli Ahmed /**
12891c05dae3SAli Ahmed  * @brief Set TrustedModuleRequiredToBoot property. Determines whether or not
12901c05dae3SAli Ahmed  * TPM is required for booting the host.
12911c05dae3SAli Ahmed  *
12921c05dae3SAli Ahmed  * @param[in] aResp         Shared pointer for generating response message.
12931c05dae3SAli Ahmed  * @param[in] tpmRequired   Value to set TPM Required To Boot property to.
12941c05dae3SAli Ahmed  *
12951c05dae3SAli Ahmed  * @return None.
12961c05dae3SAli Ahmed  */
12971c05dae3SAli Ahmed inline void setTrustedModuleRequiredToBoot(
12981c05dae3SAli Ahmed     const std::shared_ptr<bmcweb::AsyncResp>& aResp, const bool tpmRequired)
12991c05dae3SAli Ahmed {
13001c05dae3SAli Ahmed     BMCWEB_LOG_DEBUG << "Set TrustedModuleRequiredToBoot.";
13011c05dae3SAli Ahmed 
13021c05dae3SAli Ahmed     crow::connections::systemBus->async_method_call(
13031c05dae3SAli Ahmed         [aResp, tpmRequired](
13041c05dae3SAli Ahmed             const boost::system::error_code ec,
13051c05dae3SAli Ahmed             std::vector<std::pair<
13061c05dae3SAli Ahmed                 std::string,
13071c05dae3SAli Ahmed                 std::vector<std::pair<std::string, std::vector<std::string>>>>>&
13081c05dae3SAli Ahmed                 subtree) {
13091c05dae3SAli Ahmed             if (ec)
13101c05dae3SAli Ahmed             {
13111c05dae3SAli Ahmed                 BMCWEB_LOG_DEBUG
13121c05dae3SAli Ahmed                     << "DBUS response error on TPM.Policy GetSubTree" << ec;
13131c05dae3SAli Ahmed                 messages::internalError(aResp->res);
13141c05dae3SAli Ahmed                 return;
13151c05dae3SAli Ahmed             }
13161c05dae3SAli Ahmed             if (subtree.size() == 0)
13171c05dae3SAli Ahmed             {
13181c05dae3SAli Ahmed                 messages::propertyValueNotInList(aResp->res, "ComputerSystem",
13191c05dae3SAli Ahmed                                                  "TrustedModuleRequiredToBoot");
13201c05dae3SAli Ahmed                 return;
13211c05dae3SAli Ahmed             }
13221c05dae3SAli Ahmed 
13231c05dae3SAli Ahmed             /* When there is more than one TPMEnable object... */
13241c05dae3SAli Ahmed             if (subtree.size() > 1)
13251c05dae3SAli Ahmed             {
13261c05dae3SAli Ahmed                 BMCWEB_LOG_DEBUG
13271c05dae3SAli Ahmed                     << "DBUS response has more than 1 TPM Enable object:"
13281c05dae3SAli Ahmed                     << subtree.size();
13291c05dae3SAli Ahmed                 // Throw an internal Error and return
13301c05dae3SAli Ahmed                 messages::internalError(aResp->res);
13311c05dae3SAli Ahmed                 return;
13321c05dae3SAli Ahmed             }
13331c05dae3SAli Ahmed 
13341c05dae3SAli Ahmed             // Make sure the Dbus response map has a service and objectPath
13351c05dae3SAli Ahmed             // field
13361c05dae3SAli Ahmed             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
13371c05dae3SAli Ahmed             {
13381c05dae3SAli Ahmed                 BMCWEB_LOG_DEBUG << "TPM.Policy mapper error!";
13391c05dae3SAli Ahmed                 messages::internalError(aResp->res);
13401c05dae3SAli Ahmed                 return;
13411c05dae3SAli Ahmed             }
13421c05dae3SAli Ahmed 
13431c05dae3SAli Ahmed             const std::string& path = subtree[0].first;
13441c05dae3SAli Ahmed             const std::string& serv = subtree[0].second.begin()->first;
13451c05dae3SAli Ahmed 
13461c05dae3SAli Ahmed             if (serv.empty())
13471c05dae3SAli Ahmed             {
13481c05dae3SAli Ahmed                 BMCWEB_LOG_DEBUG << "TPM.Policy service mapper error!";
13491c05dae3SAli Ahmed                 messages::internalError(aResp->res);
13501c05dae3SAli Ahmed                 return;
13511c05dae3SAli Ahmed             }
13521c05dae3SAli Ahmed 
13531c05dae3SAli Ahmed             // Valid TPM Enable object found, now setting the value
13541c05dae3SAli Ahmed             crow::connections::systemBus->async_method_call(
13551c05dae3SAli Ahmed                 [aResp](const boost::system::error_code ec) {
13561c05dae3SAli Ahmed                     if (ec)
13571c05dae3SAli Ahmed                     {
13580fda0f12SGeorge Liu                         BMCWEB_LOG_DEBUG
13590fda0f12SGeorge Liu                             << "DBUS response error: Set TrustedModuleRequiredToBoot"
13601c05dae3SAli Ahmed                             << ec;
13611c05dae3SAli Ahmed                         messages::internalError(aResp->res);
13621c05dae3SAli Ahmed                         return;
13631c05dae3SAli Ahmed                     }
13641c05dae3SAli Ahmed                     BMCWEB_LOG_DEBUG << "Set TrustedModuleRequiredToBoot done.";
13651c05dae3SAli Ahmed                 },
13661c05dae3SAli Ahmed                 serv, path, "org.freedesktop.DBus.Properties", "Set",
13671c05dae3SAli Ahmed                 "xyz.openbmc_project.Control.TPM.Policy", "TPMEnable",
1368168e20c1SEd Tanous                 dbus::utility::DbusVariantType(tpmRequired));
13691c05dae3SAli Ahmed         },
13701c05dae3SAli Ahmed         "xyz.openbmc_project.ObjectMapper",
13711c05dae3SAli Ahmed         "/xyz/openbmc_project/object_mapper",
13721c05dae3SAli Ahmed         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
13731c05dae3SAli Ahmed         std::array<const char*, 1>{"xyz.openbmc_project.Control.TPM.Policy"});
13741c05dae3SAli Ahmed }
13751c05dae3SAli Ahmed 
13761c05dae3SAli Ahmed /**
1377491d8ee7SSantosh Puranik  * @brief Sets boot properties into DBUS object(s).
1378491d8ee7SSantosh Puranik  *
1379491d8ee7SSantosh Puranik  * @param[in] aResp           Shared pointer for generating response message.
1380cd9a4666SKonstantin Aladyshev  * @param[in] bootType        The boot type to set.
1381cd9a4666SKonstantin Aladyshev  * @return Integer error code.
1382cd9a4666SKonstantin Aladyshev  */
1383cd9a4666SKonstantin Aladyshev inline void setBootType(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1384cd9a4666SKonstantin Aladyshev                         const std::optional<std::string>& bootType)
1385cd9a4666SKonstantin Aladyshev {
1386c21865c4SKonstantin Aladyshev     std::string bootTypeStr;
1387cd9a4666SKonstantin Aladyshev 
1388c21865c4SKonstantin Aladyshev     if (!bootType)
1389cd9a4666SKonstantin Aladyshev     {
1390c21865c4SKonstantin Aladyshev         return;
1391c21865c4SKonstantin Aladyshev     }
1392c21865c4SKonstantin Aladyshev 
1393cd9a4666SKonstantin Aladyshev     // Source target specified
1394cd9a4666SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Boot type: " << *bootType;
1395cd9a4666SKonstantin Aladyshev     // Figure out which DBUS interface and property to use
1396cd9a4666SKonstantin Aladyshev     if (*bootType == "Legacy")
1397cd9a4666SKonstantin Aladyshev     {
1398cd9a4666SKonstantin Aladyshev         bootTypeStr = "xyz.openbmc_project.Control.Boot.Type.Types.Legacy";
1399cd9a4666SKonstantin Aladyshev     }
1400cd9a4666SKonstantin Aladyshev     else if (*bootType == "UEFI")
1401cd9a4666SKonstantin Aladyshev     {
1402cd9a4666SKonstantin Aladyshev         bootTypeStr = "xyz.openbmc_project.Control.Boot.Type.Types.EFI";
1403cd9a4666SKonstantin Aladyshev     }
1404cd9a4666SKonstantin Aladyshev     else
1405cd9a4666SKonstantin Aladyshev     {
1406cd9a4666SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Invalid property value for "
1407cd9a4666SKonstantin Aladyshev                             "BootSourceOverrideMode: "
1408cd9a4666SKonstantin Aladyshev                          << *bootType;
1409cd9a4666SKonstantin Aladyshev         messages::propertyValueNotInList(aResp->res, *bootType,
1410cd9a4666SKonstantin Aladyshev                                          "BootSourceOverrideMode");
1411cd9a4666SKonstantin Aladyshev         return;
1412cd9a4666SKonstantin Aladyshev     }
1413cd9a4666SKonstantin Aladyshev 
1414cd9a4666SKonstantin Aladyshev     // Act on validated parameters
1415cd9a4666SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot type: " << bootTypeStr;
1416cd9a4666SKonstantin Aladyshev 
1417cd9a4666SKonstantin Aladyshev     crow::connections::systemBus->async_method_call(
1418c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec) {
1419cd9a4666SKonstantin Aladyshev             if (ec)
1420cd9a4666SKonstantin Aladyshev             {
1421cd9a4666SKonstantin Aladyshev                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1422cd9a4666SKonstantin Aladyshev                 if (ec.value() == boost::asio::error::host_unreachable)
1423cd9a4666SKonstantin Aladyshev                 {
1424cd9a4666SKonstantin Aladyshev                     messages::resourceNotFound(aResp->res, "Set", "BootType");
1425cd9a4666SKonstantin Aladyshev                     return;
1426cd9a4666SKonstantin Aladyshev                 }
1427cd9a4666SKonstantin Aladyshev                 messages::internalError(aResp->res);
1428cd9a4666SKonstantin Aladyshev                 return;
1429cd9a4666SKonstantin Aladyshev             }
1430cd9a4666SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "Boot type update done.";
1431cd9a4666SKonstantin Aladyshev         },
1432c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1433c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1434cd9a4666SKonstantin Aladyshev         "org.freedesktop.DBus.Properties", "Set",
1435cd9a4666SKonstantin Aladyshev         "xyz.openbmc_project.Control.Boot.Type", "BootType",
1436168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootTypeStr));
1437cd9a4666SKonstantin Aladyshev }
1438cd9a4666SKonstantin Aladyshev 
1439cd9a4666SKonstantin Aladyshev /**
1440cd9a4666SKonstantin Aladyshev  * @brief Sets boot properties into DBUS object(s).
1441cd9a4666SKonstantin Aladyshev  *
1442cd9a4666SKonstantin Aladyshev  * @param[in] aResp           Shared pointer for generating response message.
1443c21865c4SKonstantin Aladyshev  * @param[in] bootType        The boot type to set.
1444c21865c4SKonstantin Aladyshev  * @return Integer error code.
1445c21865c4SKonstantin Aladyshev  */
1446c21865c4SKonstantin Aladyshev inline void setBootEnable(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1447c21865c4SKonstantin Aladyshev                           const std::optional<std::string>& bootEnable)
1448c21865c4SKonstantin Aladyshev {
1449c21865c4SKonstantin Aladyshev     if (!bootEnable)
1450c21865c4SKonstantin Aladyshev     {
1451c21865c4SKonstantin Aladyshev         return;
1452c21865c4SKonstantin Aladyshev     }
1453c21865c4SKonstantin Aladyshev     // Source target specified
1454c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Boot enable: " << *bootEnable;
1455c21865c4SKonstantin Aladyshev 
1456c21865c4SKonstantin Aladyshev     bool bootOverrideEnable = false;
1457c21865c4SKonstantin Aladyshev     bool bootOverridePersistent = false;
1458c21865c4SKonstantin Aladyshev     // Figure out which DBUS interface and property to use
1459c21865c4SKonstantin Aladyshev     if (*bootEnable == "Disabled")
1460c21865c4SKonstantin Aladyshev     {
1461c21865c4SKonstantin Aladyshev         bootOverrideEnable = false;
1462c21865c4SKonstantin Aladyshev     }
1463c21865c4SKonstantin Aladyshev     else if (*bootEnable == "Once")
1464c21865c4SKonstantin Aladyshev     {
1465c21865c4SKonstantin Aladyshev         bootOverrideEnable = true;
1466c21865c4SKonstantin Aladyshev         bootOverridePersistent = false;
1467c21865c4SKonstantin Aladyshev     }
1468c21865c4SKonstantin Aladyshev     else if (*bootEnable == "Continuous")
1469c21865c4SKonstantin Aladyshev     {
1470c21865c4SKonstantin Aladyshev         bootOverrideEnable = true;
1471c21865c4SKonstantin Aladyshev         bootOverridePersistent = true;
1472c21865c4SKonstantin Aladyshev     }
1473c21865c4SKonstantin Aladyshev     else
1474c21865c4SKonstantin Aladyshev     {
14750fda0f12SGeorge Liu         BMCWEB_LOG_DEBUG
14760fda0f12SGeorge Liu             << "Invalid property value for BootSourceOverrideEnabled: "
1477c21865c4SKonstantin Aladyshev             << *bootEnable;
1478c21865c4SKonstantin Aladyshev         messages::propertyValueNotInList(aResp->res, *bootEnable,
1479c21865c4SKonstantin Aladyshev                                          "BootSourceOverrideEnabled");
1480c21865c4SKonstantin Aladyshev         return;
1481c21865c4SKonstantin Aladyshev     }
1482c21865c4SKonstantin Aladyshev 
1483c21865c4SKonstantin Aladyshev     // Act on validated parameters
1484c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot override enable: " << bootOverrideEnable;
1485c21865c4SKonstantin Aladyshev 
1486c21865c4SKonstantin Aladyshev     crow::connections::systemBus->async_method_call(
1487c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec) {
1488c21865c4SKonstantin Aladyshev             if (ec)
1489c21865c4SKonstantin Aladyshev             {
1490c21865c4SKonstantin Aladyshev                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1491c21865c4SKonstantin Aladyshev                 messages::internalError(aResp->res);
1492c21865c4SKonstantin Aladyshev                 return;
1493c21865c4SKonstantin Aladyshev             }
1494c21865c4SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "Boot override enable update done.";
1495c21865c4SKonstantin Aladyshev         },
1496c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1497c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1498c21865c4SKonstantin Aladyshev         "org.freedesktop.DBus.Properties", "Set",
1499c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Object.Enable", "Enabled",
1500168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootOverrideEnable));
1501c21865c4SKonstantin Aladyshev 
1502c21865c4SKonstantin Aladyshev     if (!bootOverrideEnable)
1503c21865c4SKonstantin Aladyshev     {
1504c21865c4SKonstantin Aladyshev         return;
1505c21865c4SKonstantin Aladyshev     }
1506c21865c4SKonstantin Aladyshev 
1507c21865c4SKonstantin Aladyshev     // In case boot override is enabled we need to set correct value for the
1508c21865c4SKonstantin Aladyshev     // 'one_time' enable DBus interface
1509c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot override persistent: "
1510c21865c4SKonstantin Aladyshev                      << bootOverridePersistent;
1511c21865c4SKonstantin Aladyshev 
1512c21865c4SKonstantin Aladyshev     crow::connections::systemBus->async_method_call(
1513c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec) {
1514c21865c4SKonstantin Aladyshev             if (ec)
1515c21865c4SKonstantin Aladyshev             {
1516c21865c4SKonstantin Aladyshev                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1517c21865c4SKonstantin Aladyshev                 messages::internalError(aResp->res);
1518c21865c4SKonstantin Aladyshev                 return;
1519c21865c4SKonstantin Aladyshev             }
1520c21865c4SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "Boot one_time update done.";
1521c21865c4SKonstantin Aladyshev         },
1522c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1523c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot/one_time",
1524c21865c4SKonstantin Aladyshev         "org.freedesktop.DBus.Properties", "Set",
1525c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Object.Enable", "Enabled",
1526168e20c1SEd Tanous         dbus::utility::DbusVariantType(!bootOverridePersistent));
1527c21865c4SKonstantin Aladyshev }
1528c21865c4SKonstantin Aladyshev 
1529c21865c4SKonstantin Aladyshev /**
1530c21865c4SKonstantin Aladyshev  * @brief Sets boot properties into DBUS object(s).
1531c21865c4SKonstantin Aladyshev  *
1532c21865c4SKonstantin Aladyshev  * @param[in] aResp           Shared pointer for generating response message.
1533491d8ee7SSantosh Puranik  * @param[in] bootSource      The boot source to set.
1534491d8ee7SSantosh Puranik  *
1535265c1602SJohnathan Mantey  * @return Integer error code.
1536491d8ee7SSantosh Puranik  */
1537cd9a4666SKonstantin Aladyshev inline void setBootModeOrSource(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1538cd9a4666SKonstantin Aladyshev                                 const std::optional<std::string>& bootSource)
1539491d8ee7SSantosh Puranik {
1540c21865c4SKonstantin Aladyshev     std::string bootSourceStr;
1541c21865c4SKonstantin Aladyshev     std::string bootModeStr;
1542944ffaf9SJohnathan Mantey 
1543c21865c4SKonstantin Aladyshev     if (!bootSource)
1544491d8ee7SSantosh Puranik     {
1545c21865c4SKonstantin Aladyshev         return;
1546c21865c4SKonstantin Aladyshev     }
1547c21865c4SKonstantin Aladyshev 
1548491d8ee7SSantosh Puranik     // Source target specified
1549491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Boot source: " << *bootSource;
1550491d8ee7SSantosh Puranik     // Figure out which DBUS interface and property to use
1551c21865c4SKonstantin Aladyshev     if (assignBootParameters(aResp, *bootSource, bootSourceStr, bootModeStr))
1552491d8ee7SSantosh Puranik     {
1553944ffaf9SJohnathan Mantey         BMCWEB_LOG_DEBUG
1554944ffaf9SJohnathan Mantey             << "Invalid property value for BootSourceOverrideTarget: "
1555491d8ee7SSantosh Puranik             << *bootSource;
1556491d8ee7SSantosh Puranik         messages::propertyValueNotInList(aResp->res, *bootSource,
1557491d8ee7SSantosh Puranik                                          "BootSourceTargetOverride");
1558491d8ee7SSantosh Puranik         return;
1559491d8ee7SSantosh Puranik     }
1560491d8ee7SSantosh Puranik 
1561944ffaf9SJohnathan Mantey     // Act on validated parameters
1562944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
1563944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
1564944ffaf9SJohnathan Mantey 
1565491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1566491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec) {
1567491d8ee7SSantosh Puranik             if (ec)
1568491d8ee7SSantosh Puranik             {
1569491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1570491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1571491d8ee7SSantosh Puranik                 return;
1572491d8ee7SSantosh Puranik             }
1573491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot source update done.";
1574491d8ee7SSantosh Puranik         },
1575c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1576c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1577491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1578491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource",
1579168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootSourceStr));
1580944ffaf9SJohnathan Mantey 
1581491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1582491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec) {
1583491d8ee7SSantosh Puranik             if (ec)
1584491d8ee7SSantosh Puranik             {
1585491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1586491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1587491d8ee7SSantosh Puranik                 return;
1588491d8ee7SSantosh Puranik             }
1589491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot mode update done.";
1590491d8ee7SSantosh Puranik         },
1591c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1592c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1593491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1594491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
1595168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootModeStr));
1596cd9a4666SKonstantin Aladyshev }
1597944ffaf9SJohnathan Mantey 
1598cd9a4666SKonstantin Aladyshev /**
1599c21865c4SKonstantin Aladyshev  * @brief Sets Boot source override properties.
1600491d8ee7SSantosh Puranik  *
1601491d8ee7SSantosh Puranik  * @param[in] aResp      Shared pointer for generating response message.
1602491d8ee7SSantosh Puranik  * @param[in] bootSource The boot source from incoming RF request.
1603cd9a4666SKonstantin Aladyshev  * @param[in] bootType   The boot type from incoming RF request.
1604491d8ee7SSantosh Puranik  * @param[in] bootEnable The boot override enable from incoming RF request.
1605491d8ee7SSantosh Puranik  *
1606265c1602SJohnathan Mantey  * @return Integer error code.
1607491d8ee7SSantosh Puranik  */
1608c21865c4SKonstantin Aladyshev 
1609c21865c4SKonstantin Aladyshev inline void setBootProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1610c21865c4SKonstantin Aladyshev                               const std::optional<std::string>& bootSource,
1611c21865c4SKonstantin Aladyshev                               const std::optional<std::string>& bootType,
1612c21865c4SKonstantin Aladyshev                               const std::optional<std::string>& bootEnable)
1613491d8ee7SSantosh Puranik {
1614491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set boot information.";
1615491d8ee7SSantosh Puranik 
1616c21865c4SKonstantin Aladyshev     setBootModeOrSource(aResp, bootSource);
1617c21865c4SKonstantin Aladyshev     setBootType(aResp, bootType);
1618c21865c4SKonstantin Aladyshev     setBootEnable(aResp, bootEnable);
1619491d8ee7SSantosh Puranik }
1620491d8ee7SSantosh Puranik 
1621c6a620f2SGeorge Liu /**
162298e386ecSGunnar Mills  * @brief Sets AssetTag
162398e386ecSGunnar Mills  *
162498e386ecSGunnar Mills  * @param[in] aResp   Shared pointer for generating response message.
162598e386ecSGunnar Mills  * @param[in] assetTag  "AssetTag" from request.
162698e386ecSGunnar Mills  *
162798e386ecSGunnar Mills  * @return None.
162898e386ecSGunnar Mills  */
16298d1b46d7Szhanghch05 inline void setAssetTag(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
163098e386ecSGunnar Mills                         const std::string& assetTag)
163198e386ecSGunnar Mills {
163298e386ecSGunnar Mills     crow::connections::systemBus->async_method_call(
163398e386ecSGunnar Mills         [aResp, assetTag](
163498e386ecSGunnar Mills             const boost::system::error_code ec,
163598e386ecSGunnar Mills             const std::vector<std::pair<
163698e386ecSGunnar Mills                 std::string,
163798e386ecSGunnar Mills                 std::vector<std::pair<std::string, std::vector<std::string>>>>>&
163898e386ecSGunnar Mills                 subtree) {
163998e386ecSGunnar Mills             if (ec)
164098e386ecSGunnar Mills             {
164198e386ecSGunnar Mills                 BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " << ec;
164298e386ecSGunnar Mills                 messages::internalError(aResp->res);
164398e386ecSGunnar Mills                 return;
164498e386ecSGunnar Mills             }
164598e386ecSGunnar Mills             if (subtree.size() == 0)
164698e386ecSGunnar Mills             {
164798e386ecSGunnar Mills                 BMCWEB_LOG_DEBUG << "Can't find system D-Bus object!";
164898e386ecSGunnar Mills                 messages::internalError(aResp->res);
164998e386ecSGunnar Mills                 return;
165098e386ecSGunnar Mills             }
165198e386ecSGunnar Mills             // Assume only 1 system D-Bus object
165298e386ecSGunnar Mills             // Throw an error if there is more than 1
165398e386ecSGunnar Mills             if (subtree.size() > 1)
165498e386ecSGunnar Mills             {
165598e386ecSGunnar Mills                 BMCWEB_LOG_DEBUG << "Found more than 1 system D-Bus object!";
165698e386ecSGunnar Mills                 messages::internalError(aResp->res);
165798e386ecSGunnar Mills                 return;
165898e386ecSGunnar Mills             }
165998e386ecSGunnar Mills             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
166098e386ecSGunnar Mills             {
166198e386ecSGunnar Mills                 BMCWEB_LOG_DEBUG << "Asset Tag Set mapper error!";
166298e386ecSGunnar Mills                 messages::internalError(aResp->res);
166398e386ecSGunnar Mills                 return;
166498e386ecSGunnar Mills             }
166598e386ecSGunnar Mills 
166698e386ecSGunnar Mills             const std::string& path = subtree[0].first;
166798e386ecSGunnar Mills             const std::string& service = subtree[0].second.begin()->first;
166898e386ecSGunnar Mills 
166998e386ecSGunnar Mills             if (service.empty())
167098e386ecSGunnar Mills             {
167198e386ecSGunnar Mills                 BMCWEB_LOG_DEBUG << "Asset Tag Set service mapper error!";
167298e386ecSGunnar Mills                 messages::internalError(aResp->res);
167398e386ecSGunnar Mills                 return;
167498e386ecSGunnar Mills             }
167598e386ecSGunnar Mills 
167698e386ecSGunnar Mills             crow::connections::systemBus->async_method_call(
167798e386ecSGunnar Mills                 [aResp](const boost::system::error_code ec2) {
167898e386ecSGunnar Mills                     if (ec2)
167998e386ecSGunnar Mills                     {
168098e386ecSGunnar Mills                         BMCWEB_LOG_DEBUG
168198e386ecSGunnar Mills                             << "D-Bus response error on AssetTag Set " << ec2;
168298e386ecSGunnar Mills                         messages::internalError(aResp->res);
168398e386ecSGunnar Mills                         return;
168498e386ecSGunnar Mills                     }
168598e386ecSGunnar Mills                 },
168698e386ecSGunnar Mills                 service, path, "org.freedesktop.DBus.Properties", "Set",
168798e386ecSGunnar Mills                 "xyz.openbmc_project.Inventory.Decorator.AssetTag", "AssetTag",
1688168e20c1SEd Tanous                 dbus::utility::DbusVariantType(assetTag));
168998e386ecSGunnar Mills         },
169098e386ecSGunnar Mills         "xyz.openbmc_project.ObjectMapper",
169198e386ecSGunnar Mills         "/xyz/openbmc_project/object_mapper",
169298e386ecSGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
169398e386ecSGunnar Mills         "/xyz/openbmc_project/inventory", int32_t(0),
169498e386ecSGunnar Mills         std::array<const char*, 1>{
169598e386ecSGunnar Mills             "xyz.openbmc_project.Inventory.Item.System"});
169698e386ecSGunnar Mills }
169798e386ecSGunnar Mills 
169898e386ecSGunnar Mills /**
169969f35306SGunnar Mills  * @brief Sets automaticRetry (Auto Reboot)
170069f35306SGunnar Mills  *
170169f35306SGunnar Mills  * @param[in] aResp   Shared pointer for generating response message.
170269f35306SGunnar Mills  * @param[in] automaticRetryConfig  "AutomaticRetryConfig" from request.
170369f35306SGunnar Mills  *
170469f35306SGunnar Mills  * @return None.
170569f35306SGunnar Mills  */
17068d1b46d7Szhanghch05 inline void setAutomaticRetry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1707f23b7296SEd Tanous                               const std::string& automaticRetryConfig)
170869f35306SGunnar Mills {
170969f35306SGunnar Mills     BMCWEB_LOG_DEBUG << "Set Automatic Retry.";
171069f35306SGunnar Mills 
171169f35306SGunnar Mills     // OpenBMC only supports "Disabled" and "RetryAttempts".
171269f35306SGunnar Mills     bool autoRebootEnabled;
171369f35306SGunnar Mills 
171469f35306SGunnar Mills     if (automaticRetryConfig == "Disabled")
171569f35306SGunnar Mills     {
171669f35306SGunnar Mills         autoRebootEnabled = false;
171769f35306SGunnar Mills     }
171869f35306SGunnar Mills     else if (automaticRetryConfig == "RetryAttempts")
171969f35306SGunnar Mills     {
172069f35306SGunnar Mills         autoRebootEnabled = true;
172169f35306SGunnar Mills     }
172269f35306SGunnar Mills     else
172369f35306SGunnar Mills     {
17240fda0f12SGeorge Liu         BMCWEB_LOG_DEBUG << "Invalid property value for AutomaticRetryConfig: "
172569f35306SGunnar Mills                          << automaticRetryConfig;
172669f35306SGunnar Mills         messages::propertyValueNotInList(aResp->res, automaticRetryConfig,
172769f35306SGunnar Mills                                          "AutomaticRetryConfig");
172869f35306SGunnar Mills         return;
172969f35306SGunnar Mills     }
173069f35306SGunnar Mills 
173169f35306SGunnar Mills     crow::connections::systemBus->async_method_call(
173269f35306SGunnar Mills         [aResp](const boost::system::error_code ec) {
173369f35306SGunnar Mills             if (ec)
173469f35306SGunnar Mills             {
173569f35306SGunnar Mills                 messages::internalError(aResp->res);
173669f35306SGunnar Mills                 return;
173769f35306SGunnar Mills             }
173869f35306SGunnar Mills         },
173969f35306SGunnar Mills         "xyz.openbmc_project.Settings",
174069f35306SGunnar Mills         "/xyz/openbmc_project/control/host0/auto_reboot",
174169f35306SGunnar Mills         "org.freedesktop.DBus.Properties", "Set",
174269f35306SGunnar Mills         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
1743168e20c1SEd Tanous         dbus::utility::DbusVariantType(autoRebootEnabled));
174469f35306SGunnar Mills }
174569f35306SGunnar Mills 
174669f35306SGunnar Mills /**
1747c6a620f2SGeorge Liu  * @brief Sets power restore policy properties.
1748c6a620f2SGeorge Liu  *
1749c6a620f2SGeorge Liu  * @param[in] aResp   Shared pointer for generating response message.
1750c6a620f2SGeorge Liu  * @param[in] policy  power restore policy properties from request.
1751c6a620f2SGeorge Liu  *
1752c6a620f2SGeorge Liu  * @return None.
1753c6a620f2SGeorge Liu  */
17548d1b46d7Szhanghch05 inline void
17558d1b46d7Szhanghch05     setPowerRestorePolicy(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
17564e69c904SGunnar Mills                           const std::string& policy)
1757c6a620f2SGeorge Liu {
1758c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Set power restore policy.";
1759c6a620f2SGeorge Liu 
1760c6a620f2SGeorge Liu     const boost::container::flat_map<std::string, std::string> policyMaps = {
17610fda0f12SGeorge Liu         {"AlwaysOn",
17620fda0f12SGeorge Liu          "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn"},
17630fda0f12SGeorge Liu         {"AlwaysOff",
17640fda0f12SGeorge Liu          "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff"},
17650fda0f12SGeorge Liu         {"LastState",
17660fda0f12SGeorge Liu          "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore"}};
1767c6a620f2SGeorge Liu 
1768c6a620f2SGeorge Liu     std::string powerRestorPolicy;
1769c6a620f2SGeorge Liu 
17704e69c904SGunnar Mills     auto policyMapsIt = policyMaps.find(policy);
1771c6a620f2SGeorge Liu     if (policyMapsIt == policyMaps.end())
1772c6a620f2SGeorge Liu     {
17734e69c904SGunnar Mills         messages::propertyValueNotInList(aResp->res, policy,
17744e69c904SGunnar Mills                                          "PowerRestorePolicy");
1775c6a620f2SGeorge Liu         return;
1776c6a620f2SGeorge Liu     }
1777c6a620f2SGeorge Liu 
1778c6a620f2SGeorge Liu     powerRestorPolicy = policyMapsIt->second;
1779c6a620f2SGeorge Liu 
1780c6a620f2SGeorge Liu     crow::connections::systemBus->async_method_call(
1781c6a620f2SGeorge Liu         [aResp](const boost::system::error_code ec) {
1782c6a620f2SGeorge Liu             if (ec)
1783c6a620f2SGeorge Liu             {
1784c6a620f2SGeorge Liu                 messages::internalError(aResp->res);
1785c6a620f2SGeorge Liu                 return;
1786c6a620f2SGeorge Liu             }
1787c6a620f2SGeorge Liu         },
1788c6a620f2SGeorge Liu         "xyz.openbmc_project.Settings",
1789c6a620f2SGeorge Liu         "/xyz/openbmc_project/control/host0/power_restore_policy",
1790c6a620f2SGeorge Liu         "org.freedesktop.DBus.Properties", "Set",
1791c6a620f2SGeorge Liu         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
1792168e20c1SEd Tanous         dbus::utility::DbusVariantType(powerRestorPolicy));
1793c6a620f2SGeorge Liu }
1794c6a620f2SGeorge Liu 
1795a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
1796a6349918SAppaRao Puli /**
1797a6349918SAppaRao Puli  * @brief Retrieves provisioning status
1798a6349918SAppaRao Puli  *
1799a6349918SAppaRao Puli  * @param[in] aResp     Shared pointer for completing asynchronous calls.
1800a6349918SAppaRao Puli  *
1801a6349918SAppaRao Puli  * @return None.
1802a6349918SAppaRao Puli  */
18038d1b46d7Szhanghch05 inline void getProvisioningStatus(std::shared_ptr<bmcweb::AsyncResp> aResp)
1804a6349918SAppaRao Puli {
1805a6349918SAppaRao Puli     BMCWEB_LOG_DEBUG << "Get OEM information.";
1806a6349918SAppaRao Puli     crow::connections::systemBus->async_method_call(
1807a6349918SAppaRao Puli         [aResp](const boost::system::error_code ec,
18081214b7e7SGunnar Mills                 const std::vector<std::pair<std::string, VariantType>>&
18091214b7e7SGunnar Mills                     propertiesList) {
1810b99fb1a9SAppaRao Puli             nlohmann::json& oemPFR =
1811b99fb1a9SAppaRao Puli                 aResp->res.jsonValue["Oem"]["OpenBmc"]["FirmwareProvisioning"];
181250626f4fSJames Feist             aResp->res.jsonValue["Oem"]["OpenBmc"]["@odata.type"] =
181350626f4fSJames Feist                 "#OemComputerSystem.OpenBmc";
181450626f4fSJames Feist             oemPFR["@odata.type"] = "#OemComputerSystem.FirmwareProvisioning";
181550626f4fSJames Feist 
1816a6349918SAppaRao Puli             if (ec)
1817a6349918SAppaRao Puli             {
1818a6349918SAppaRao Puli                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1819b99fb1a9SAppaRao Puli                 // not an error, don't have to have the interface
1820b99fb1a9SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "NotProvisioned";
1821a6349918SAppaRao Puli                 return;
1822a6349918SAppaRao Puli             }
1823a6349918SAppaRao Puli 
1824a6349918SAppaRao Puli             const bool* provState = nullptr;
1825a6349918SAppaRao Puli             const bool* lockState = nullptr;
1826a6349918SAppaRao Puli             for (const std::pair<std::string, VariantType>& property :
1827a6349918SAppaRao Puli                  propertiesList)
1828a6349918SAppaRao Puli             {
1829a6349918SAppaRao Puli                 if (property.first == "UfmProvisioned")
1830a6349918SAppaRao Puli                 {
1831a6349918SAppaRao Puli                     provState = std::get_if<bool>(&property.second);
1832a6349918SAppaRao Puli                 }
1833a6349918SAppaRao Puli                 else if (property.first == "UfmLocked")
1834a6349918SAppaRao Puli                 {
1835a6349918SAppaRao Puli                     lockState = std::get_if<bool>(&property.second);
1836a6349918SAppaRao Puli                 }
1837a6349918SAppaRao Puli             }
1838a6349918SAppaRao Puli 
1839a6349918SAppaRao Puli             if ((provState == nullptr) || (lockState == nullptr))
1840a6349918SAppaRao Puli             {
1841a6349918SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Unable to get PFR attributes.";
1842a6349918SAppaRao Puli                 messages::internalError(aResp->res);
1843a6349918SAppaRao Puli                 return;
1844a6349918SAppaRao Puli             }
1845a6349918SAppaRao Puli 
1846a6349918SAppaRao Puli             if (*provState == true)
1847a6349918SAppaRao Puli             {
1848a6349918SAppaRao Puli                 if (*lockState == true)
1849a6349918SAppaRao Puli                 {
1850a6349918SAppaRao Puli                     oemPFR["ProvisioningStatus"] = "ProvisionedAndLocked";
1851a6349918SAppaRao Puli                 }
1852a6349918SAppaRao Puli                 else
1853a6349918SAppaRao Puli                 {
1854a6349918SAppaRao Puli                     oemPFR["ProvisioningStatus"] = "ProvisionedButNotLocked";
1855a6349918SAppaRao Puli                 }
1856a6349918SAppaRao Puli             }
1857a6349918SAppaRao Puli             else
1858a6349918SAppaRao Puli             {
1859a6349918SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "NotProvisioned";
1860a6349918SAppaRao Puli             }
1861a6349918SAppaRao Puli         },
1862a6349918SAppaRao Puli         "xyz.openbmc_project.PFR.Manager", "/xyz/openbmc_project/pfr",
1863a6349918SAppaRao Puli         "org.freedesktop.DBus.Properties", "GetAll",
1864a6349918SAppaRao Puli         "xyz.openbmc_project.PFR.Attributes");
1865a6349918SAppaRao Puli }
1866a6349918SAppaRao Puli #endif
1867a6349918SAppaRao Puli 
1868491d8ee7SSantosh Puranik /**
18693a2d0424SChris Cain  * @brief Translate the PowerMode to a response message.
18703a2d0424SChris Cain  *
18713a2d0424SChris Cain  * @param[in] aResp  Shared pointer for generating response message.
18723a2d0424SChris Cain  * @param[in] modeValue  PowerMode value to be translated
18733a2d0424SChris Cain  *
18743a2d0424SChris Cain  * @return None.
18753a2d0424SChris Cain  */
18763a2d0424SChris Cain inline void translatePowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
18773a2d0424SChris Cain                                const std::string& modeValue)
18783a2d0424SChris Cain {
18793a2d0424SChris Cain     std::string modeString;
18803a2d0424SChris Cain 
18810fda0f12SGeorge Liu     if (modeValue == "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static")
18823a2d0424SChris Cain     {
18833a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "Static";
18843a2d0424SChris Cain     }
18850fda0f12SGeorge Liu     else if (
18860fda0f12SGeorge Liu         modeValue ==
18870fda0f12SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance")
18883a2d0424SChris Cain     {
18893a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "MaximumPerformance";
18903a2d0424SChris Cain     }
18910fda0f12SGeorge Liu     else if (modeValue ==
18920fda0f12SGeorge Liu              "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving")
18933a2d0424SChris Cain     {
18943a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "PowerSaving";
18953a2d0424SChris Cain     }
18960fda0f12SGeorge Liu     else if (modeValue ==
18970fda0f12SGeorge Liu              "xyz.openbmc_project.Control.Power.Mode.PowerMode.OEM")
18983a2d0424SChris Cain     {
18993a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "OEM";
19003a2d0424SChris Cain     }
19013a2d0424SChris Cain     else
19023a2d0424SChris Cain     {
19033a2d0424SChris Cain         // Any other values would be invalid
19043a2d0424SChris Cain         BMCWEB_LOG_DEBUG << "PowerMode value was not valid: " << modeValue;
19053a2d0424SChris Cain         messages::internalError(aResp->res);
19063a2d0424SChris Cain     }
19073a2d0424SChris Cain }
19083a2d0424SChris Cain 
19093a2d0424SChris Cain /**
19103a2d0424SChris Cain  * @brief Retrieves system power mode
19113a2d0424SChris Cain  *
19123a2d0424SChris Cain  * @param[in] aResp  Shared pointer for generating response message.
19133a2d0424SChris Cain  *
19143a2d0424SChris Cain  * @return None.
19153a2d0424SChris Cain  */
19163a2d0424SChris Cain inline void getPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
19173a2d0424SChris Cain {
19183a2d0424SChris Cain     BMCWEB_LOG_DEBUG << "Get power mode.";
19193a2d0424SChris Cain 
19203a2d0424SChris Cain     // Get Power Mode object path:
19213a2d0424SChris Cain     crow::connections::systemBus->async_method_call(
19223a2d0424SChris Cain         [aResp](
19233a2d0424SChris Cain             const boost::system::error_code ec,
19243a2d0424SChris Cain             const std::vector<std::pair<
19253a2d0424SChris Cain                 std::string,
19263a2d0424SChris Cain                 std::vector<std::pair<std::string, std::vector<std::string>>>>>&
19273a2d0424SChris Cain                 subtree) {
19283a2d0424SChris Cain             if (ec)
19293a2d0424SChris Cain             {
19303a2d0424SChris Cain                 BMCWEB_LOG_DEBUG
19313a2d0424SChris Cain                     << "DBUS response error on Power.Mode GetSubTree " << ec;
19323a2d0424SChris Cain                 // This is an optional D-Bus object so just return if
19333a2d0424SChris Cain                 // error occurs
19343a2d0424SChris Cain                 return;
19353a2d0424SChris Cain             }
19363a2d0424SChris Cain             if (subtree.empty())
19373a2d0424SChris Cain             {
19383a2d0424SChris Cain                 // As noted above, this is an optional interface so just return
19393a2d0424SChris Cain                 // if there is no instance found
19403a2d0424SChris Cain                 return;
19413a2d0424SChris Cain             }
19423a2d0424SChris Cain             if (subtree.size() > 1)
19433a2d0424SChris Cain             {
19443a2d0424SChris Cain                 // More then one PowerMode object is not supported and is an
19453a2d0424SChris Cain                 // error
19463a2d0424SChris Cain                 BMCWEB_LOG_DEBUG
19473a2d0424SChris Cain                     << "Found more than 1 system D-Bus Power.Mode objects: "
19483a2d0424SChris Cain                     << subtree.size();
19493a2d0424SChris Cain                 messages::internalError(aResp->res);
19503a2d0424SChris Cain                 return;
19513a2d0424SChris Cain             }
19523a2d0424SChris Cain             if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
19533a2d0424SChris Cain             {
19543a2d0424SChris Cain                 BMCWEB_LOG_DEBUG << "Power.Mode mapper error!";
19553a2d0424SChris Cain                 messages::internalError(aResp->res);
19563a2d0424SChris Cain                 return;
19573a2d0424SChris Cain             }
19583a2d0424SChris Cain             const std::string& path = subtree[0].first;
19593a2d0424SChris Cain             const std::string& service = subtree[0].second.begin()->first;
19603a2d0424SChris Cain             if (service.empty())
19613a2d0424SChris Cain             {
19623a2d0424SChris Cain                 BMCWEB_LOG_DEBUG << "Power.Mode service mapper error!";
19633a2d0424SChris Cain                 messages::internalError(aResp->res);
19643a2d0424SChris Cain                 return;
19653a2d0424SChris Cain             }
19663a2d0424SChris Cain             // Valid Power Mode object found, now read the current value
1967*1e1e598dSJonathan Doman             sdbusplus::asio::getProperty<std::string>(
1968*1e1e598dSJonathan Doman                 *crow::connections::systemBus, service, path,
1969*1e1e598dSJonathan Doman                 "xyz.openbmc_project.Control.Power.Mode", "PowerMode",
19703a2d0424SChris Cain                 [aResp](const boost::system::error_code ec,
1971*1e1e598dSJonathan Doman                         const std::string& pmode) {
19723a2d0424SChris Cain                     if (ec)
19733a2d0424SChris Cain                     {
19743a2d0424SChris Cain                         BMCWEB_LOG_DEBUG
19753a2d0424SChris Cain                             << "DBUS response error on PowerMode Get: " << ec;
19763a2d0424SChris Cain                         messages::internalError(aResp->res);
19773a2d0424SChris Cain                         return;
19783a2d0424SChris Cain                     }
19793a2d0424SChris Cain 
19803a2d0424SChris Cain                     aResp->res.jsonValue["PowerMode@Redfish.AllowableValues"] =
19813a2d0424SChris Cain                         {"Static", "MaximumPerformance", "PowerSaving"};
19823a2d0424SChris Cain 
1983*1e1e598dSJonathan Doman                     BMCWEB_LOG_DEBUG << "Current power mode: " << pmode;
1984*1e1e598dSJonathan Doman                     translatePowerMode(aResp, pmode);
1985*1e1e598dSJonathan Doman                 });
19863a2d0424SChris Cain         },
19873a2d0424SChris Cain         "xyz.openbmc_project.ObjectMapper",
19883a2d0424SChris Cain         "/xyz/openbmc_project/object_mapper",
19893a2d0424SChris Cain         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
19903a2d0424SChris Cain         std::array<const char*, 1>{"xyz.openbmc_project.Control.Power.Mode"});
19913a2d0424SChris Cain }
19923a2d0424SChris Cain 
19933a2d0424SChris Cain /**
19943a2d0424SChris Cain  * @brief Validate the specified mode is valid and return the PowerMode
19953a2d0424SChris Cain  * name associated with that string
19963a2d0424SChris Cain  *
19973a2d0424SChris Cain  * @param[in] aResp   Shared pointer for generating response message.
19983a2d0424SChris Cain  * @param[in] modeString  String representing the desired PowerMode
19993a2d0424SChris Cain  *
20003a2d0424SChris Cain  * @return PowerMode value or empty string if mode is not valid
20013a2d0424SChris Cain  */
20023a2d0424SChris Cain inline std::string
20033a2d0424SChris Cain     validatePowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
20043a2d0424SChris Cain                       const std::string& modeString)
20053a2d0424SChris Cain {
20063a2d0424SChris Cain     std::string mode;
20073a2d0424SChris Cain 
20083a2d0424SChris Cain     if (modeString == "Static")
20093a2d0424SChris Cain     {
20103a2d0424SChris Cain         mode = "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static";
20113a2d0424SChris Cain     }
20123a2d0424SChris Cain     else if (modeString == "MaximumPerformance")
20133a2d0424SChris Cain     {
20140fda0f12SGeorge Liu         mode =
20150fda0f12SGeorge Liu             "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance";
20163a2d0424SChris Cain     }
20173a2d0424SChris Cain     else if (modeString == "PowerSaving")
20183a2d0424SChris Cain     {
20193a2d0424SChris Cain         mode = "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving";
20203a2d0424SChris Cain     }
20213a2d0424SChris Cain     else
20223a2d0424SChris Cain     {
20233a2d0424SChris Cain         messages::propertyValueNotInList(aResp->res, modeString, "PowerMode");
20243a2d0424SChris Cain     }
20253a2d0424SChris Cain     return mode;
20263a2d0424SChris Cain }
20273a2d0424SChris Cain 
20283a2d0424SChris Cain /**
20293a2d0424SChris Cain  * @brief Sets system power mode.
20303a2d0424SChris Cain  *
20313a2d0424SChris Cain  * @param[in] aResp   Shared pointer for generating response message.
20323a2d0424SChris Cain  * @param[in] pmode   System power mode from request.
20333a2d0424SChris Cain  *
20343a2d0424SChris Cain  * @return None.
20353a2d0424SChris Cain  */
20363a2d0424SChris Cain inline void setPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
20373a2d0424SChris Cain                          const std::string& pmode)
20383a2d0424SChris Cain {
20393a2d0424SChris Cain     BMCWEB_LOG_DEBUG << "Set power mode.";
20403a2d0424SChris Cain 
20413a2d0424SChris Cain     std::string powerMode = validatePowerMode(aResp, pmode);
20423a2d0424SChris Cain     if (powerMode.empty())
20433a2d0424SChris Cain     {
20443a2d0424SChris Cain         return;
20453a2d0424SChris Cain     }
20463a2d0424SChris Cain 
20473a2d0424SChris Cain     // Get Power Mode object path:
20483a2d0424SChris Cain     crow::connections::systemBus->async_method_call(
20493a2d0424SChris Cain         [aResp, powerMode](
20503a2d0424SChris Cain             const boost::system::error_code ec,
20513a2d0424SChris Cain             const std::vector<std::pair<
20523a2d0424SChris Cain                 std::string,
20533a2d0424SChris Cain                 std::vector<std::pair<std::string, std::vector<std::string>>>>>&
20543a2d0424SChris Cain                 subtree) {
20553a2d0424SChris Cain             if (ec)
20563a2d0424SChris Cain             {
20573a2d0424SChris Cain                 BMCWEB_LOG_DEBUG
20583a2d0424SChris Cain                     << "DBUS response error on Power.Mode GetSubTree " << ec;
20593a2d0424SChris Cain                 // This is an optional D-Bus object, but user attempted to patch
20603a2d0424SChris Cain                 messages::internalError(aResp->res);
20613a2d0424SChris Cain                 return;
20623a2d0424SChris Cain             }
20633a2d0424SChris Cain             if (subtree.empty())
20643a2d0424SChris Cain             {
20653a2d0424SChris Cain                 // This is an optional D-Bus object, but user attempted to patch
20663a2d0424SChris Cain                 messages::resourceNotFound(aResp->res, "ComputerSystem",
20673a2d0424SChris Cain                                            "PowerMode");
20683a2d0424SChris Cain                 return;
20693a2d0424SChris Cain             }
20703a2d0424SChris Cain             if (subtree.size() > 1)
20713a2d0424SChris Cain             {
20723a2d0424SChris Cain                 // More then one PowerMode object is not supported and is an
20733a2d0424SChris Cain                 // error
20743a2d0424SChris Cain                 BMCWEB_LOG_DEBUG
20753a2d0424SChris Cain                     << "Found more than 1 system D-Bus Power.Mode objects: "
20763a2d0424SChris Cain                     << subtree.size();
20773a2d0424SChris Cain                 messages::internalError(aResp->res);
20783a2d0424SChris Cain                 return;
20793a2d0424SChris Cain             }
20803a2d0424SChris Cain             if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
20813a2d0424SChris Cain             {
20823a2d0424SChris Cain                 BMCWEB_LOG_DEBUG << "Power.Mode mapper error!";
20833a2d0424SChris Cain                 messages::internalError(aResp->res);
20843a2d0424SChris Cain                 return;
20853a2d0424SChris Cain             }
20863a2d0424SChris Cain             const std::string& path = subtree[0].first;
20873a2d0424SChris Cain             const std::string& service = subtree[0].second.begin()->first;
20883a2d0424SChris Cain             if (service.empty())
20893a2d0424SChris Cain             {
20903a2d0424SChris Cain                 BMCWEB_LOG_DEBUG << "Power.Mode service mapper error!";
20913a2d0424SChris Cain                 messages::internalError(aResp->res);
20923a2d0424SChris Cain                 return;
20933a2d0424SChris Cain             }
20943a2d0424SChris Cain 
20953a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Setting power mode(" << powerMode << ") -> "
20963a2d0424SChris Cain                              << path;
20973a2d0424SChris Cain 
20983a2d0424SChris Cain             // Set the Power Mode property
20993a2d0424SChris Cain             crow::connections::systemBus->async_method_call(
21003a2d0424SChris Cain                 [aResp](const boost::system::error_code ec) {
21013a2d0424SChris Cain                     if (ec)
21023a2d0424SChris Cain                     {
21033a2d0424SChris Cain                         messages::internalError(aResp->res);
21043a2d0424SChris Cain                         return;
21053a2d0424SChris Cain                     }
21063a2d0424SChris Cain                 },
21073a2d0424SChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
21083a2d0424SChris Cain                 "xyz.openbmc_project.Control.Power.Mode", "PowerMode",
2109168e20c1SEd Tanous                 dbus::utility::DbusVariantType(powerMode));
21103a2d0424SChris Cain         },
21113a2d0424SChris Cain         "xyz.openbmc_project.ObjectMapper",
21123a2d0424SChris Cain         "/xyz/openbmc_project/object_mapper",
21133a2d0424SChris Cain         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
21143a2d0424SChris Cain         std::array<const char*, 1>{"xyz.openbmc_project.Control.Power.Mode"});
21153a2d0424SChris Cain }
21163a2d0424SChris Cain 
21173a2d0424SChris Cain /**
211851709ffdSYong Li  * @brief Translates watchdog timeout action DBUS property value to redfish.
211951709ffdSYong Li  *
212051709ffdSYong Li  * @param[in] dbusAction    The watchdog timeout action in D-BUS.
212151709ffdSYong Li  *
212251709ffdSYong Li  * @return Returns as a string, the timeout action in Redfish terms. If
212351709ffdSYong Li  * translation cannot be done, returns an empty string.
212451709ffdSYong Li  */
212523a21a1cSEd Tanous inline std::string dbusToRfWatchdogAction(const std::string& dbusAction)
212651709ffdSYong Li {
212751709ffdSYong Li     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.None")
212851709ffdSYong Li     {
212951709ffdSYong Li         return "None";
213051709ffdSYong Li     }
21313174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.HardReset")
213251709ffdSYong Li     {
213351709ffdSYong Li         return "ResetSystem";
213451709ffdSYong Li     }
21353174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerOff")
213651709ffdSYong Li     {
213751709ffdSYong Li         return "PowerDown";
213851709ffdSYong Li     }
21393174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerCycle")
214051709ffdSYong Li     {
214151709ffdSYong Li         return "PowerCycle";
214251709ffdSYong Li     }
214351709ffdSYong Li 
214451709ffdSYong Li     return "";
214551709ffdSYong Li }
214651709ffdSYong Li 
214751709ffdSYong Li /**
2148c45f0082SYong Li  *@brief Translates timeout action from Redfish to DBUS property value.
2149c45f0082SYong Li  *
2150c45f0082SYong Li  *@param[in] rfAction The timeout action in Redfish.
2151c45f0082SYong Li  *
2152c45f0082SYong Li  *@return Returns as a string, the time_out action as expected by DBUS.
2153c45f0082SYong Li  *If translation cannot be done, returns an empty string.
2154c45f0082SYong Li  */
2155c45f0082SYong Li 
215623a21a1cSEd Tanous inline std::string rfToDbusWDTTimeOutAct(const std::string& rfAction)
2157c45f0082SYong Li {
2158c45f0082SYong Li     if (rfAction == "None")
2159c45f0082SYong Li     {
2160c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.None";
2161c45f0082SYong Li     }
21623174e4dfSEd Tanous     if (rfAction == "PowerCycle")
2163c45f0082SYong Li     {
2164c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerCycle";
2165c45f0082SYong Li     }
21663174e4dfSEd Tanous     if (rfAction == "PowerDown")
2167c45f0082SYong Li     {
2168c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerOff";
2169c45f0082SYong Li     }
21703174e4dfSEd Tanous     if (rfAction == "ResetSystem")
2171c45f0082SYong Li     {
2172c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.HardReset";
2173c45f0082SYong Li     }
2174c45f0082SYong Li 
2175c45f0082SYong Li     return "";
2176c45f0082SYong Li }
2177c45f0082SYong Li 
2178c45f0082SYong Li /**
217951709ffdSYong Li  * @brief Retrieves host watchdog timer properties over DBUS
218051709ffdSYong Li  *
218151709ffdSYong Li  * @param[in] aResp     Shared pointer for completing asynchronous calls.
218251709ffdSYong Li  *
218351709ffdSYong Li  * @return None.
218451709ffdSYong Li  */
21858d1b46d7Szhanghch05 inline void
21868d1b46d7Szhanghch05     getHostWatchdogTimer(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
218751709ffdSYong Li {
218851709ffdSYong Li     BMCWEB_LOG_DEBUG << "Get host watchodg";
218951709ffdSYong Li     crow::connections::systemBus->async_method_call(
219051709ffdSYong Li         [aResp](const boost::system::error_code ec,
219151709ffdSYong Li                 PropertiesType& properties) {
219251709ffdSYong Li             if (ec)
219351709ffdSYong Li             {
219451709ffdSYong Li                 // watchdog service is stopped
219551709ffdSYong Li                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
219651709ffdSYong Li                 return;
219751709ffdSYong Li             }
219851709ffdSYong Li 
219951709ffdSYong Li             BMCWEB_LOG_DEBUG << "Got " << properties.size() << " wdt prop.";
220051709ffdSYong Li 
220151709ffdSYong Li             nlohmann::json& hostWatchdogTimer =
220251709ffdSYong Li                 aResp->res.jsonValue["HostWatchdogTimer"];
220351709ffdSYong Li 
220451709ffdSYong Li             // watchdog service is running/enabled
220551709ffdSYong Li             hostWatchdogTimer["Status"]["State"] = "Enabled";
220651709ffdSYong Li 
220751709ffdSYong Li             for (const auto& property : properties)
220851709ffdSYong Li             {
220951709ffdSYong Li                 BMCWEB_LOG_DEBUG << "prop=" << property.first;
221051709ffdSYong Li                 if (property.first == "Enabled")
221151709ffdSYong Li                 {
221251709ffdSYong Li                     const bool* state = std::get_if<bool>(&property.second);
221351709ffdSYong Li 
221451709ffdSYong Li                     if (!state)
221551709ffdSYong Li                     {
221651709ffdSYong Li                         messages::internalError(aResp->res);
2217601af5edSChicago Duan                         return;
221851709ffdSYong Li                     }
221951709ffdSYong Li 
222051709ffdSYong Li                     hostWatchdogTimer["FunctionEnabled"] = *state;
222151709ffdSYong Li                 }
222251709ffdSYong Li                 else if (property.first == "ExpireAction")
222351709ffdSYong Li                 {
222451709ffdSYong Li                     const std::string* s =
222551709ffdSYong Li                         std::get_if<std::string>(&property.second);
222651709ffdSYong Li                     if (!s)
222751709ffdSYong Li                     {
222851709ffdSYong Li                         messages::internalError(aResp->res);
2229601af5edSChicago Duan                         return;
223051709ffdSYong Li                     }
223151709ffdSYong Li 
223251709ffdSYong Li                     std::string action = dbusToRfWatchdogAction(*s);
223351709ffdSYong Li                     if (action.empty())
223451709ffdSYong Li                     {
223551709ffdSYong Li                         messages::internalError(aResp->res);
2236601af5edSChicago Duan                         return;
223751709ffdSYong Li                     }
223851709ffdSYong Li                     hostWatchdogTimer["TimeoutAction"] = action;
223951709ffdSYong Li                 }
224051709ffdSYong Li             }
224151709ffdSYong Li         },
224251709ffdSYong Li         "xyz.openbmc_project.Watchdog", "/xyz/openbmc_project/watchdog/host0",
224351709ffdSYong Li         "org.freedesktop.DBus.Properties", "GetAll",
224451709ffdSYong Li         "xyz.openbmc_project.State.Watchdog");
224551709ffdSYong Li }
224651709ffdSYong Li 
224751709ffdSYong Li /**
2248c45f0082SYong Li  * @brief Sets Host WatchDog Timer properties.
2249c45f0082SYong Li  *
2250c45f0082SYong Li  * @param[in] aResp      Shared pointer for generating response message.
2251c45f0082SYong Li  * @param[in] wdtEnable  The WDTimer Enable value (true/false) from incoming
2252c45f0082SYong Li  *                       RF request.
2253c45f0082SYong Li  * @param[in] wdtTimeOutAction The WDT Timeout action, from incoming RF request.
2254c45f0082SYong Li  *
2255c45f0082SYong Li  * @return None.
2256c45f0082SYong Li  */
22578d1b46d7Szhanghch05 inline void setWDTProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2258c45f0082SYong Li                              const std::optional<bool> wdtEnable,
2259c45f0082SYong Li                              const std::optional<std::string>& wdtTimeOutAction)
2260c45f0082SYong Li {
2261c45f0082SYong Li     BMCWEB_LOG_DEBUG << "Set host watchdog";
2262c45f0082SYong Li 
2263c45f0082SYong Li     if (wdtTimeOutAction)
2264c45f0082SYong Li     {
2265c45f0082SYong Li         std::string wdtTimeOutActStr = rfToDbusWDTTimeOutAct(*wdtTimeOutAction);
2266c45f0082SYong Li         // check if TimeOut Action is Valid
2267c45f0082SYong Li         if (wdtTimeOutActStr.empty())
2268c45f0082SYong Li         {
2269c45f0082SYong Li             BMCWEB_LOG_DEBUG << "Unsupported value for TimeoutAction: "
2270c45f0082SYong Li                              << *wdtTimeOutAction;
2271c45f0082SYong Li             messages::propertyValueNotInList(aResp->res, *wdtTimeOutAction,
2272c45f0082SYong Li                                              "TimeoutAction");
2273c45f0082SYong Li             return;
2274c45f0082SYong Li         }
2275c45f0082SYong Li 
2276c45f0082SYong Li         crow::connections::systemBus->async_method_call(
2277c45f0082SYong Li             [aResp](const boost::system::error_code ec) {
2278c45f0082SYong Li                 if (ec)
2279c45f0082SYong Li                 {
2280c45f0082SYong Li                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
2281c45f0082SYong Li                     messages::internalError(aResp->res);
2282c45f0082SYong Li                     return;
2283c45f0082SYong Li                 }
2284c45f0082SYong Li             },
2285c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
2286c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
2287c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
2288c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "ExpireAction",
2289168e20c1SEd Tanous             dbus::utility::DbusVariantType(wdtTimeOutActStr));
2290c45f0082SYong Li     }
2291c45f0082SYong Li 
2292c45f0082SYong Li     if (wdtEnable)
2293c45f0082SYong Li     {
2294c45f0082SYong Li         crow::connections::systemBus->async_method_call(
2295c45f0082SYong Li             [aResp](const boost::system::error_code ec) {
2296c45f0082SYong Li                 if (ec)
2297c45f0082SYong Li                 {
2298c45f0082SYong Li                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
2299c45f0082SYong Li                     messages::internalError(aResp->res);
2300c45f0082SYong Li                     return;
2301c45f0082SYong Li                 }
2302c45f0082SYong Li             },
2303c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
2304c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
2305c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
2306c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "Enabled",
2307168e20c1SEd Tanous             dbus::utility::DbusVariantType(*wdtEnable));
2308c45f0082SYong Li     }
2309c45f0082SYong Li }
2310c45f0082SYong Li 
231137bbf98cSChris Cain using ipsPropertiesType =
231237bbf98cSChris Cain     std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>;
231337bbf98cSChris Cain /**
231437bbf98cSChris Cain  * @brief Parse the Idle Power Saver properties into json
231537bbf98cSChris Cain  *
231637bbf98cSChris Cain  * @param[in] aResp     Shared pointer for completing asynchronous calls.
231737bbf98cSChris Cain  * @param[in] properties  IPS property data from DBus.
231837bbf98cSChris Cain  *
231937bbf98cSChris Cain  * @return true if successful
232037bbf98cSChris Cain  */
2321f6674220SEd Tanous inline bool parseIpsProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
232237bbf98cSChris Cain                                ipsPropertiesType& properties)
232337bbf98cSChris Cain {
232437bbf98cSChris Cain     for (const auto& property : properties)
232537bbf98cSChris Cain     {
232637bbf98cSChris Cain         if (property.first == "Enabled")
232737bbf98cSChris Cain         {
232837bbf98cSChris Cain             const bool* state = std::get_if<bool>(&property.second);
232937bbf98cSChris Cain             if (!state)
233037bbf98cSChris Cain             {
233137bbf98cSChris Cain                 return false;
233237bbf98cSChris Cain             }
233337bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"][property.first] = *state;
233437bbf98cSChris Cain         }
233537bbf98cSChris Cain         else if (property.first == "EnterUtilizationPercent")
233637bbf98cSChris Cain         {
233737bbf98cSChris Cain             const uint8_t* util = std::get_if<uint8_t>(&property.second);
233837bbf98cSChris Cain             if (!util)
233937bbf98cSChris Cain             {
234037bbf98cSChris Cain                 return false;
234137bbf98cSChris Cain             }
234237bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"][property.first] = *util;
234337bbf98cSChris Cain         }
234437bbf98cSChris Cain         else if (property.first == "EnterDwellTime")
234537bbf98cSChris Cain         {
234637bbf98cSChris Cain             // Convert Dbus time from milliseconds to seconds
234737bbf98cSChris Cain             const uint64_t* timeMilliseconds =
234837bbf98cSChris Cain                 std::get_if<uint64_t>(&property.second);
234937bbf98cSChris Cain             if (!timeMilliseconds)
235037bbf98cSChris Cain             {
235137bbf98cSChris Cain                 return false;
235237bbf98cSChris Cain             }
235337bbf98cSChris Cain             const std::chrono::duration<uint64_t, std::milli> ms(
235437bbf98cSChris Cain                 *timeMilliseconds);
235537bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"]["EnterDwellTimeSeconds"] =
235637bbf98cSChris Cain                 std::chrono::duration_cast<std::chrono::duration<uint64_t>>(ms)
235737bbf98cSChris Cain                     .count();
235837bbf98cSChris Cain         }
235937bbf98cSChris Cain         else if (property.first == "ExitUtilizationPercent")
236037bbf98cSChris Cain         {
236137bbf98cSChris Cain             const uint8_t* util = std::get_if<uint8_t>(&property.second);
236237bbf98cSChris Cain             if (!util)
236337bbf98cSChris Cain             {
236437bbf98cSChris Cain                 return false;
236537bbf98cSChris Cain             }
236637bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"][property.first] = *util;
236737bbf98cSChris Cain         }
236837bbf98cSChris Cain         else if (property.first == "ExitDwellTime")
236937bbf98cSChris Cain         {
237037bbf98cSChris Cain             // Convert Dbus time from milliseconds to seconds
237137bbf98cSChris Cain             const uint64_t* timeMilliseconds =
237237bbf98cSChris Cain                 std::get_if<uint64_t>(&property.second);
237337bbf98cSChris Cain             if (!timeMilliseconds)
237437bbf98cSChris Cain             {
237537bbf98cSChris Cain                 return false;
237637bbf98cSChris Cain             }
237737bbf98cSChris Cain             const std::chrono::duration<uint64_t, std::milli> ms(
237837bbf98cSChris Cain                 *timeMilliseconds);
237937bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"]["ExitDwellTimeSeconds"] =
238037bbf98cSChris Cain                 std::chrono::duration_cast<std::chrono::duration<uint64_t>>(ms)
238137bbf98cSChris Cain                     .count();
238237bbf98cSChris Cain         }
238337bbf98cSChris Cain         else
238437bbf98cSChris Cain         {
238537bbf98cSChris Cain             BMCWEB_LOG_WARNING << "Unexpected IdlePowerSaver property: "
238637bbf98cSChris Cain                                << property.first;
238737bbf98cSChris Cain         }
238837bbf98cSChris Cain     }
238937bbf98cSChris Cain 
239037bbf98cSChris Cain     return true;
239137bbf98cSChris Cain }
239237bbf98cSChris Cain 
239337bbf98cSChris Cain /**
239437bbf98cSChris Cain  * @brief Retrieves host watchdog timer properties over DBUS
239537bbf98cSChris Cain  *
239637bbf98cSChris Cain  * @param[in] aResp     Shared pointer for completing asynchronous calls.
239737bbf98cSChris Cain  *
239837bbf98cSChris Cain  * @return None.
239937bbf98cSChris Cain  */
240037bbf98cSChris Cain inline void getIdlePowerSaver(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
240137bbf98cSChris Cain {
240237bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "Get idle power saver parameters";
240337bbf98cSChris Cain 
240437bbf98cSChris Cain     // Get IdlePowerSaver object path:
240537bbf98cSChris Cain     crow::connections::systemBus->async_method_call(
240637bbf98cSChris Cain         [aResp](
240737bbf98cSChris Cain             const boost::system::error_code ec,
240837bbf98cSChris Cain             const std::vector<std::pair<
240937bbf98cSChris Cain                 std::string,
241037bbf98cSChris Cain                 std::vector<std::pair<std::string, std::vector<std::string>>>>>&
241137bbf98cSChris Cain                 subtree) {
241237bbf98cSChris Cain             if (ec)
241337bbf98cSChris Cain             {
241437bbf98cSChris Cain                 BMCWEB_LOG_DEBUG
241537bbf98cSChris Cain                     << "DBUS response error on Power.IdlePowerSaver GetSubTree "
241637bbf98cSChris Cain                     << ec;
241737bbf98cSChris Cain                 messages::internalError(aResp->res);
241837bbf98cSChris Cain                 return;
241937bbf98cSChris Cain             }
242037bbf98cSChris Cain             if (subtree.empty())
242137bbf98cSChris Cain             {
242237bbf98cSChris Cain                 // This is an optional interface so just return
242337bbf98cSChris Cain                 // if there is no instance found
242437bbf98cSChris Cain                 BMCWEB_LOG_DEBUG << "No instances found";
242537bbf98cSChris Cain                 return;
242637bbf98cSChris Cain             }
242737bbf98cSChris Cain             if (subtree.size() > 1)
242837bbf98cSChris Cain             {
242937bbf98cSChris Cain                 // More then one PowerIdlePowerSaver object is not supported and
243037bbf98cSChris Cain                 // is an error
243137bbf98cSChris Cain                 BMCWEB_LOG_DEBUG << "Found more than 1 system D-Bus "
243237bbf98cSChris Cain                                     "Power.IdlePowerSaver objects: "
243337bbf98cSChris Cain                                  << subtree.size();
243437bbf98cSChris Cain                 messages::internalError(aResp->res);
243537bbf98cSChris Cain                 return;
243637bbf98cSChris Cain             }
243737bbf98cSChris Cain             if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
243837bbf98cSChris Cain             {
243937bbf98cSChris Cain                 BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver mapper error!";
244037bbf98cSChris Cain                 messages::internalError(aResp->res);
244137bbf98cSChris Cain                 return;
244237bbf98cSChris Cain             }
244337bbf98cSChris Cain             const std::string& path = subtree[0].first;
244437bbf98cSChris Cain             const std::string& service = subtree[0].second.begin()->first;
244537bbf98cSChris Cain             if (service.empty())
244637bbf98cSChris Cain             {
244737bbf98cSChris Cain                 BMCWEB_LOG_DEBUG
244837bbf98cSChris Cain                     << "Power.IdlePowerSaver service mapper error!";
244937bbf98cSChris Cain                 messages::internalError(aResp->res);
245037bbf98cSChris Cain                 return;
245137bbf98cSChris Cain             }
245237bbf98cSChris Cain 
245337bbf98cSChris Cain             // Valid IdlePowerSaver object found, now read the current values
245437bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
245537bbf98cSChris Cain                 [aResp](const boost::system::error_code ec,
245637bbf98cSChris Cain                         ipsPropertiesType& properties) {
245737bbf98cSChris Cain                     if (ec)
245837bbf98cSChris Cain                     {
245937bbf98cSChris Cain                         BMCWEB_LOG_ERROR
246037bbf98cSChris Cain                             << "DBUS response error on IdlePowerSaver GetAll: "
246137bbf98cSChris Cain                             << ec;
246237bbf98cSChris Cain                         messages::internalError(aResp->res);
246337bbf98cSChris Cain                         return;
246437bbf98cSChris Cain                     }
246537bbf98cSChris Cain 
246637bbf98cSChris Cain                     if (parseIpsProperties(aResp, properties) == false)
246737bbf98cSChris Cain                     {
246837bbf98cSChris Cain                         messages::internalError(aResp->res);
246937bbf98cSChris Cain                         return;
247037bbf98cSChris Cain                     }
247137bbf98cSChris Cain                 },
247237bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "GetAll",
247337bbf98cSChris Cain                 "xyz.openbmc_project.Control.Power.IdlePowerSaver");
247437bbf98cSChris Cain         },
247537bbf98cSChris Cain         "xyz.openbmc_project.ObjectMapper",
247637bbf98cSChris Cain         "/xyz/openbmc_project/object_mapper",
247737bbf98cSChris Cain         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
247837bbf98cSChris Cain         std::array<const char*, 1>{
247937bbf98cSChris Cain             "xyz.openbmc_project.Control.Power.IdlePowerSaver"});
248037bbf98cSChris Cain 
248137bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "EXIT: Get idle power saver parameters";
248237bbf98cSChris Cain }
248337bbf98cSChris Cain 
248437bbf98cSChris Cain /**
248537bbf98cSChris Cain  * @brief Sets Idle Power Saver properties.
248637bbf98cSChris Cain  *
248737bbf98cSChris Cain  * @param[in] aResp      Shared pointer for generating response message.
248837bbf98cSChris Cain  * @param[in] ipsEnable  The IPS Enable value (true/false) from incoming
248937bbf98cSChris Cain  *                       RF request.
249037bbf98cSChris Cain  * @param[in] ipsEnterUtil The utilization limit to enter idle state.
249137bbf98cSChris Cain  * @param[in] ipsEnterTime The time the utilization must be below ipsEnterUtil
249237bbf98cSChris Cain  * before entering idle state.
249337bbf98cSChris Cain  * @param[in] ipsExitUtil The utilization limit when exiting idle state.
249437bbf98cSChris Cain  * @param[in] ipsExitTime The time the utilization must be above ipsExutUtil
249537bbf98cSChris Cain  * before exiting idle state
249637bbf98cSChris Cain  *
249737bbf98cSChris Cain  * @return None.
249837bbf98cSChris Cain  */
249937bbf98cSChris Cain inline void setIdlePowerSaver(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
250037bbf98cSChris Cain                               const std::optional<bool> ipsEnable,
250137bbf98cSChris Cain                               const std::optional<uint8_t> ipsEnterUtil,
250237bbf98cSChris Cain                               const std::optional<uint64_t> ipsEnterTime,
250337bbf98cSChris Cain                               const std::optional<uint8_t> ipsExitUtil,
250437bbf98cSChris Cain                               const std::optional<uint64_t> ipsExitTime)
250537bbf98cSChris Cain {
250637bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "Set idle power saver properties";
250737bbf98cSChris Cain 
250837bbf98cSChris Cain     // Get IdlePowerSaver object path:
250937bbf98cSChris Cain     crow::connections::systemBus->async_method_call(
251037bbf98cSChris Cain         [aResp, ipsEnable, ipsEnterUtil, ipsEnterTime, ipsExitUtil,
251137bbf98cSChris Cain          ipsExitTime](
251237bbf98cSChris Cain             const boost::system::error_code ec,
251337bbf98cSChris Cain             const std::vector<std::pair<
251437bbf98cSChris Cain                 std::string,
251537bbf98cSChris Cain                 std::vector<std::pair<std::string, std::vector<std::string>>>>>&
251637bbf98cSChris Cain                 subtree) {
251737bbf98cSChris Cain             if (ec)
251837bbf98cSChris Cain             {
251937bbf98cSChris Cain                 BMCWEB_LOG_DEBUG
252037bbf98cSChris Cain                     << "DBUS response error on Power.IdlePowerSaver GetSubTree "
252137bbf98cSChris Cain                     << ec;
252237bbf98cSChris Cain                 messages::internalError(aResp->res);
252337bbf98cSChris Cain                 return;
252437bbf98cSChris Cain             }
252537bbf98cSChris Cain             if (subtree.empty())
252637bbf98cSChris Cain             {
252737bbf98cSChris Cain                 // This is an optional D-Bus object, but user attempted to patch
252837bbf98cSChris Cain                 messages::resourceNotFound(aResp->res, "ComputerSystem",
252937bbf98cSChris Cain                                            "IdlePowerSaver");
253037bbf98cSChris Cain                 return;
253137bbf98cSChris Cain             }
253237bbf98cSChris Cain             if (subtree.size() > 1)
253337bbf98cSChris Cain             {
253437bbf98cSChris Cain                 // More then one PowerIdlePowerSaver object is not supported and
253537bbf98cSChris Cain                 // is an error
25360fda0f12SGeorge Liu                 BMCWEB_LOG_DEBUG
25370fda0f12SGeorge Liu                     << "Found more than 1 system D-Bus Power.IdlePowerSaver objects: "
253837bbf98cSChris Cain                     << subtree.size();
253937bbf98cSChris Cain                 messages::internalError(aResp->res);
254037bbf98cSChris Cain                 return;
254137bbf98cSChris Cain             }
254237bbf98cSChris Cain             if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
254337bbf98cSChris Cain             {
254437bbf98cSChris Cain                 BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver mapper error!";
254537bbf98cSChris Cain                 messages::internalError(aResp->res);
254637bbf98cSChris Cain                 return;
254737bbf98cSChris Cain             }
254837bbf98cSChris Cain             const std::string& path = subtree[0].first;
254937bbf98cSChris Cain             const std::string& service = subtree[0].second.begin()->first;
255037bbf98cSChris Cain             if (service.empty())
255137bbf98cSChris Cain             {
255237bbf98cSChris Cain                 BMCWEB_LOG_DEBUG
255337bbf98cSChris Cain                     << "Power.IdlePowerSaver service mapper error!";
255437bbf98cSChris Cain                 messages::internalError(aResp->res);
255537bbf98cSChris Cain                 return;
255637bbf98cSChris Cain             }
255737bbf98cSChris Cain 
255837bbf98cSChris Cain             // Valid Power IdlePowerSaver object found, now set any values that
255937bbf98cSChris Cain             // need to be updated
256037bbf98cSChris Cain 
256137bbf98cSChris Cain             if (ipsEnable)
256237bbf98cSChris Cain             {
256337bbf98cSChris Cain                 crow::connections::systemBus->async_method_call(
256437bbf98cSChris Cain                     [aResp](const boost::system::error_code ec) {
256537bbf98cSChris Cain                         if (ec)
256637bbf98cSChris Cain                         {
256737bbf98cSChris Cain                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
256837bbf98cSChris Cain                             messages::internalError(aResp->res);
256937bbf98cSChris Cain                             return;
257037bbf98cSChris Cain                         }
257137bbf98cSChris Cain                     },
257237bbf98cSChris Cain                     service, path, "org.freedesktop.DBus.Properties", "Set",
257337bbf98cSChris Cain                     "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2574168e20c1SEd Tanous                     "Enabled", dbus::utility::DbusVariantType(*ipsEnable));
257537bbf98cSChris Cain             }
257637bbf98cSChris Cain             if (ipsEnterUtil)
257737bbf98cSChris Cain             {
257837bbf98cSChris Cain                 crow::connections::systemBus->async_method_call(
257937bbf98cSChris Cain                     [aResp](const boost::system::error_code ec) {
258037bbf98cSChris Cain                         if (ec)
258137bbf98cSChris Cain                         {
258237bbf98cSChris Cain                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
258337bbf98cSChris Cain                             messages::internalError(aResp->res);
258437bbf98cSChris Cain                             return;
258537bbf98cSChris Cain                         }
258637bbf98cSChris Cain                     },
258737bbf98cSChris Cain                     service, path, "org.freedesktop.DBus.Properties", "Set",
258837bbf98cSChris Cain                     "xyz.openbmc_project.Control.Power.IdlePowerSaver",
258937bbf98cSChris Cain                     "EnterUtilizationPercent",
2590168e20c1SEd Tanous                     dbus::utility::DbusVariantType(*ipsEnterUtil));
259137bbf98cSChris Cain             }
259237bbf98cSChris Cain             if (ipsEnterTime)
259337bbf98cSChris Cain             {
259437bbf98cSChris Cain                 // Convert from seconds into milliseconds for DBus
259537bbf98cSChris Cain                 const uint64_t timeMilliseconds = *ipsEnterTime * 1000;
259637bbf98cSChris Cain                 crow::connections::systemBus->async_method_call(
259737bbf98cSChris Cain                     [aResp](const boost::system::error_code ec) {
259837bbf98cSChris Cain                         if (ec)
259937bbf98cSChris Cain                         {
260037bbf98cSChris Cain                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
260137bbf98cSChris Cain                             messages::internalError(aResp->res);
260237bbf98cSChris Cain                             return;
260337bbf98cSChris Cain                         }
260437bbf98cSChris Cain                     },
260537bbf98cSChris Cain                     service, path, "org.freedesktop.DBus.Properties", "Set",
260637bbf98cSChris Cain                     "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2607168e20c1SEd Tanous                     "EnterDwellTime",
2608168e20c1SEd Tanous                     dbus::utility::DbusVariantType(timeMilliseconds));
260937bbf98cSChris Cain             }
261037bbf98cSChris Cain             if (ipsExitUtil)
261137bbf98cSChris Cain             {
261237bbf98cSChris Cain                 crow::connections::systemBus->async_method_call(
261337bbf98cSChris Cain                     [aResp](const boost::system::error_code ec) {
261437bbf98cSChris Cain                         if (ec)
261537bbf98cSChris Cain                         {
261637bbf98cSChris Cain                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
261737bbf98cSChris Cain                             messages::internalError(aResp->res);
261837bbf98cSChris Cain                             return;
261937bbf98cSChris Cain                         }
262037bbf98cSChris Cain                     },
262137bbf98cSChris Cain                     service, path, "org.freedesktop.DBus.Properties", "Set",
262237bbf98cSChris Cain                     "xyz.openbmc_project.Control.Power.IdlePowerSaver",
262337bbf98cSChris Cain                     "ExitUtilizationPercent",
2624168e20c1SEd Tanous                     dbus::utility::DbusVariantType(*ipsExitUtil));
262537bbf98cSChris Cain             }
262637bbf98cSChris Cain             if (ipsExitTime)
262737bbf98cSChris Cain             {
262837bbf98cSChris Cain                 // Convert from seconds into milliseconds for DBus
262937bbf98cSChris Cain                 const uint64_t timeMilliseconds = *ipsExitTime * 1000;
263037bbf98cSChris Cain                 crow::connections::systemBus->async_method_call(
263137bbf98cSChris Cain                     [aResp](const boost::system::error_code ec) {
263237bbf98cSChris Cain                         if (ec)
263337bbf98cSChris Cain                         {
263437bbf98cSChris Cain                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
263537bbf98cSChris Cain                             messages::internalError(aResp->res);
263637bbf98cSChris Cain                             return;
263737bbf98cSChris Cain                         }
263837bbf98cSChris Cain                     },
263937bbf98cSChris Cain                     service, path, "org.freedesktop.DBus.Properties", "Set",
264037bbf98cSChris Cain                     "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2641168e20c1SEd Tanous                     "ExitDwellTime",
2642168e20c1SEd Tanous                     dbus::utility::DbusVariantType(timeMilliseconds));
264337bbf98cSChris Cain             }
264437bbf98cSChris Cain         },
264537bbf98cSChris Cain         "xyz.openbmc_project.ObjectMapper",
264637bbf98cSChris Cain         "/xyz/openbmc_project/object_mapper",
264737bbf98cSChris Cain         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
264837bbf98cSChris Cain         std::array<const char*, 1>{
264937bbf98cSChris Cain             "xyz.openbmc_project.Control.Power.IdlePowerSaver"});
265037bbf98cSChris Cain 
265137bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "EXIT: Set idle power saver parameters";
265237bbf98cSChris Cain }
265337bbf98cSChris Cain 
2654c45f0082SYong Li /**
2655c5b2abe0SLewanczyk, Dawid  * SystemsCollection derived class for delivering ComputerSystems Collection
2656c5b2abe0SLewanczyk, Dawid  * Schema
2657c5b2abe0SLewanczyk, Dawid  */
26587e860f15SJohn Edward Broadbent inline void requestRoutesSystemsCollection(App& app)
26591abe55efSEd Tanous {
26607e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/")
2661ed398213SEd Tanous         .privileges(redfish::privileges::getComputerSystemCollection)
26627e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
26634f48d5f6SEd Tanous             [](const crow::Request& /*req*/,
26647e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26658d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
26660f74e643SEd Tanous                     "#ComputerSystemCollection.ComputerSystemCollection";
26678d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Systems";
26688d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Computer System Collection";
2669462023adSSunitha Harish 
2670*1e1e598dSJonathan Doman                 sdbusplus::asio::getProperty<std::string>(
2671*1e1e598dSJonathan Doman                     *crow::connections::systemBus,
2672*1e1e598dSJonathan Doman                     "xyz.openbmc_project.Settings",
2673*1e1e598dSJonathan Doman                     "/xyz/openbmc_project/network/hypervisor",
2674*1e1e598dSJonathan Doman                     "xyz.openbmc_project.Network.SystemConfiguration",
2675*1e1e598dSJonathan Doman                     "HostName",
2676*1e1e598dSJonathan Doman                     [asyncResp](const boost::system::error_code ec,
2677*1e1e598dSJonathan Doman                                 const std::string& /*hostName*/) {
26782c70f800SEd Tanous                         nlohmann::json& ifaceArray =
2679462023adSSunitha Harish                             asyncResp->res.jsonValue["Members"];
26802c70f800SEd Tanous                         ifaceArray = nlohmann::json::array();
26817e860f15SJohn Edward Broadbent                         auto& count =
26827e860f15SJohn Edward Broadbent                             asyncResp->res.jsonValue["Members@odata.count"];
26832c70f800SEd Tanous                         ifaceArray.push_back(
2684cb13a392SEd Tanous                             {{"@odata.id", "/redfish/v1/Systems/system"}});
268594bda602STim Lee                         count = ifaceArray.size();
2686cb13a392SEd Tanous                         if (!ec)
2687462023adSSunitha Harish                         {
2688462023adSSunitha Harish                             BMCWEB_LOG_DEBUG << "Hypervisor is available";
26892c70f800SEd Tanous                             ifaceArray.push_back(
26907e860f15SJohn Edward Broadbent                                 {{"@odata.id",
26917e860f15SJohn Edward Broadbent                                   "/redfish/v1/Systems/hypervisor"}});
26922c70f800SEd Tanous                             count = ifaceArray.size();
2693cb13a392SEd Tanous                         }
2694*1e1e598dSJonathan Doman                     });
26957e860f15SJohn Edward Broadbent             });
2696c5b2abe0SLewanczyk, Dawid }
26977e860f15SJohn Edward Broadbent 
26987e860f15SJohn Edward Broadbent /**
26997e860f15SJohn Edward Broadbent  * Function transceives data with dbus directly.
27007e860f15SJohn Edward Broadbent  */
27014f48d5f6SEd Tanous inline void doNMI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
27027e860f15SJohn Edward Broadbent {
27037e860f15SJohn Edward Broadbent     constexpr char const* serviceName = "xyz.openbmc_project.Control.Host.NMI";
27047e860f15SJohn Edward Broadbent     constexpr char const* objectPath = "/xyz/openbmc_project/control/host0/nmi";
27057e860f15SJohn Edward Broadbent     constexpr char const* interfaceName =
27067e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Control.Host.NMI";
27077e860f15SJohn Edward Broadbent     constexpr char const* method = "NMI";
27087e860f15SJohn Edward Broadbent 
27097e860f15SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
27107e860f15SJohn Edward Broadbent         [asyncResp](const boost::system::error_code ec) {
27117e860f15SJohn Edward Broadbent             if (ec)
27127e860f15SJohn Edward Broadbent             {
27137e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << " Bad D-Bus request error: " << ec;
27147e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
27157e860f15SJohn Edward Broadbent                 return;
27167e860f15SJohn Edward Broadbent             }
27177e860f15SJohn Edward Broadbent             messages::success(asyncResp->res);
27187e860f15SJohn Edward Broadbent         },
27197e860f15SJohn Edward Broadbent         serviceName, objectPath, interfaceName, method);
27207e860f15SJohn Edward Broadbent }
2721c5b2abe0SLewanczyk, Dawid 
2722c5b2abe0SLewanczyk, Dawid /**
2723cc340dd9SEd Tanous  * SystemActionsReset class supports handle POST method for Reset action.
2724cc340dd9SEd Tanous  * The class retrieves and sends data directly to D-Bus.
2725cc340dd9SEd Tanous  */
27267e860f15SJohn Edward Broadbent inline void requestRoutesSystemActionsReset(App& app)
2727cc340dd9SEd Tanous {
2728cc340dd9SEd Tanous     /**
2729cc340dd9SEd Tanous      * Function handles POST method request.
2730cc340dd9SEd Tanous      * Analyzes POST body message before sends Reset request data to D-Bus.
2731cc340dd9SEd Tanous      */
27327e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
27337e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset/")
2734ed398213SEd Tanous         .privileges(redfish::privileges::postComputerSystem)
27357e860f15SJohn Edward Broadbent         .methods(
27367e860f15SJohn Edward Broadbent             boost::beast::http::verb::
27377e860f15SJohn Edward Broadbent                 post)([](const crow::Request& req,
27387e860f15SJohn Edward Broadbent                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
27399712f8acSEd Tanous             std::string resetType;
27407e860f15SJohn Edward Broadbent             if (!json_util::readJson(req, asyncResp->res, "ResetType",
27417e860f15SJohn Edward Broadbent                                      resetType))
2742cc340dd9SEd Tanous             {
2743cc340dd9SEd Tanous                 return;
2744cc340dd9SEd Tanous             }
2745cc340dd9SEd Tanous 
2746d22c8396SJason M. Bills             // Get the command and host vs. chassis
2747cc340dd9SEd Tanous             std::string command;
2748d22c8396SJason M. Bills             bool hostCommand;
2749d4d25793SEd Tanous             if ((resetType == "On") || (resetType == "ForceOn"))
2750cc340dd9SEd Tanous             {
2751cc340dd9SEd Tanous                 command = "xyz.openbmc_project.State.Host.Transition.On";
2752d22c8396SJason M. Bills                 hostCommand = true;
2753d22c8396SJason M. Bills             }
2754d22c8396SJason M. Bills             else if (resetType == "ForceOff")
2755d22c8396SJason M. Bills             {
2756d22c8396SJason M. Bills                 command = "xyz.openbmc_project.State.Chassis.Transition.Off";
2757d22c8396SJason M. Bills                 hostCommand = false;
2758d22c8396SJason M. Bills             }
2759d22c8396SJason M. Bills             else if (resetType == "ForceRestart")
2760d22c8396SJason M. Bills             {
276186a0851aSJason M. Bills                 command =
276286a0851aSJason M. Bills                     "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot";
276386a0851aSJason M. Bills                 hostCommand = true;
2764cc340dd9SEd Tanous             }
27659712f8acSEd Tanous             else if (resetType == "GracefulShutdown")
2766cc340dd9SEd Tanous             {
2767cc340dd9SEd Tanous                 command = "xyz.openbmc_project.State.Host.Transition.Off";
2768d22c8396SJason M. Bills                 hostCommand = true;
2769cc340dd9SEd Tanous             }
27709712f8acSEd Tanous             else if (resetType == "GracefulRestart")
2771cc340dd9SEd Tanous             {
27720fda0f12SGeorge Liu                 command =
27730fda0f12SGeorge Liu                     "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot";
2774d22c8396SJason M. Bills                 hostCommand = true;
2775d22c8396SJason M. Bills             }
2776d22c8396SJason M. Bills             else if (resetType == "PowerCycle")
2777d22c8396SJason M. Bills             {
277886a0851aSJason M. Bills                 command = "xyz.openbmc_project.State.Host.Transition.Reboot";
277986a0851aSJason M. Bills                 hostCommand = true;
2780cc340dd9SEd Tanous             }
2781bfd5b826SLakshminarayana R. Kammath             else if (resetType == "Nmi")
2782bfd5b826SLakshminarayana R. Kammath             {
2783bfd5b826SLakshminarayana R. Kammath                 doNMI(asyncResp);
2784bfd5b826SLakshminarayana R. Kammath                 return;
2785bfd5b826SLakshminarayana R. Kammath             }
2786cc340dd9SEd Tanous             else
2787cc340dd9SEd Tanous             {
27888d1b46d7Szhanghch05                 messages::actionParameterUnknown(asyncResp->res, "Reset",
27898d1b46d7Szhanghch05                                                  resetType);
2790cc340dd9SEd Tanous                 return;
2791cc340dd9SEd Tanous             }
2792cc340dd9SEd Tanous 
2793d22c8396SJason M. Bills             if (hostCommand)
2794d22c8396SJason M. Bills             {
2795cc340dd9SEd Tanous                 crow::connections::systemBus->async_method_call(
2796d22c8396SJason M. Bills                     [asyncResp, resetType](const boost::system::error_code ec) {
2797cc340dd9SEd Tanous                         if (ec)
2798cc340dd9SEd Tanous                         {
2799cc340dd9SEd Tanous                             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
28007e860f15SJohn Edward Broadbent                             if (ec.value() ==
28017e860f15SJohn Edward Broadbent                                 boost::asio::error::invalid_argument)
2802d22c8396SJason M. Bills                             {
2803d22c8396SJason M. Bills                                 messages::actionParameterNotSupported(
2804d22c8396SJason M. Bills                                     asyncResp->res, resetType, "Reset");
2805d22c8396SJason M. Bills                             }
2806d22c8396SJason M. Bills                             else
2807d22c8396SJason M. Bills                             {
2808f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
2809d22c8396SJason M. Bills                             }
2810cc340dd9SEd Tanous                             return;
2811cc340dd9SEd Tanous                         }
2812f12894f8SJason M. Bills                         messages::success(asyncResp->res);
2813cc340dd9SEd Tanous                     },
2814cc340dd9SEd Tanous                     "xyz.openbmc_project.State.Host",
2815cc340dd9SEd Tanous                     "/xyz/openbmc_project/state/host0",
2816cc340dd9SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
28179712f8acSEd Tanous                     "xyz.openbmc_project.State.Host", "RequestedHostTransition",
2818168e20c1SEd Tanous                     dbus::utility::DbusVariantType{command});
2819cc340dd9SEd Tanous             }
2820d22c8396SJason M. Bills             else
2821d22c8396SJason M. Bills             {
2822d22c8396SJason M. Bills                 crow::connections::systemBus->async_method_call(
2823d22c8396SJason M. Bills                     [asyncResp, resetType](const boost::system::error_code ec) {
2824d22c8396SJason M. Bills                         if (ec)
2825d22c8396SJason M. Bills                         {
2826d22c8396SJason M. Bills                             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
28277e860f15SJohn Edward Broadbent                             if (ec.value() ==
28287e860f15SJohn Edward Broadbent                                 boost::asio::error::invalid_argument)
2829d22c8396SJason M. Bills                             {
2830d22c8396SJason M. Bills                                 messages::actionParameterNotSupported(
2831d22c8396SJason M. Bills                                     asyncResp->res, resetType, "Reset");
2832d22c8396SJason M. Bills                             }
2833d22c8396SJason M. Bills                             else
2834d22c8396SJason M. Bills                             {
2835d22c8396SJason M. Bills                                 messages::internalError(asyncResp->res);
2836d22c8396SJason M. Bills                             }
2837d22c8396SJason M. Bills                             return;
2838d22c8396SJason M. Bills                         }
2839d22c8396SJason M. Bills                         messages::success(asyncResp->res);
2840d22c8396SJason M. Bills                     },
2841d22c8396SJason M. Bills                     "xyz.openbmc_project.State.Chassis",
2842d22c8396SJason M. Bills                     "/xyz/openbmc_project/state/chassis0",
2843d22c8396SJason M. Bills                     "org.freedesktop.DBus.Properties", "Set",
28447e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.State.Chassis",
28457e860f15SJohn Edward Broadbent                     "RequestedPowerTransition",
2846168e20c1SEd Tanous                     dbus::utility::DbusVariantType{command});
2847d22c8396SJason M. Bills             }
28487e860f15SJohn Edward Broadbent         });
2849d22c8396SJason M. Bills }
2850cc340dd9SEd Tanous 
2851cc340dd9SEd Tanous /**
28526617338dSEd Tanous  * Systems derived class for delivering Computer Systems Schema.
2853c5b2abe0SLewanczyk, Dawid  */
28547e860f15SJohn Edward Broadbent inline void requestRoutesSystems(App& app)
28551abe55efSEd Tanous {
2856c5b2abe0SLewanczyk, Dawid 
2857c5b2abe0SLewanczyk, Dawid     /**
2858c5b2abe0SLewanczyk, Dawid      * Functions triggers appropriate requests on DBus
2859c5b2abe0SLewanczyk, Dawid      */
28607e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/")
2861ed398213SEd Tanous         .privileges(redfish::privileges::getComputerSystem)
28627e860f15SJohn Edward Broadbent         .methods(
28637e860f15SJohn Edward Broadbent             boost::beast::http::verb::
28647e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
28657e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
28668d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] =
286737bbf98cSChris Cain                 "#ComputerSystem.v1_16_0.ComputerSystem";
28688d1b46d7Szhanghch05             asyncResp->res.jsonValue["Name"] = "system";
28698d1b46d7Szhanghch05             asyncResp->res.jsonValue["Id"] = "system";
28708d1b46d7Szhanghch05             asyncResp->res.jsonValue["SystemType"] = "Physical";
28718d1b46d7Szhanghch05             asyncResp->res.jsonValue["Description"] = "Computer System";
28728d1b46d7Szhanghch05             asyncResp->res.jsonValue["ProcessorSummary"]["Count"] = 0;
28738d1b46d7Szhanghch05             asyncResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
28748d1b46d7Szhanghch05                 "Disabled";
28758d1b46d7Szhanghch05             asyncResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] =
28768d1b46d7Szhanghch05                 uint64_t(0);
28778d1b46d7Szhanghch05             asyncResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
28788d1b46d7Szhanghch05                 "Disabled";
28797e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
28807e860f15SJohn Edward Broadbent                 "/redfish/v1/Systems/system";
288104a258f4SEd Tanous 
28828d1b46d7Szhanghch05             asyncResp->res.jsonValue["Processors"] = {
2883029573d4SEd Tanous                 {"@odata.id", "/redfish/v1/Systems/system/Processors"}};
28848d1b46d7Szhanghch05             asyncResp->res.jsonValue["Memory"] = {
2885029573d4SEd Tanous                 {"@odata.id", "/redfish/v1/Systems/system/Memory"}};
28868d1b46d7Szhanghch05             asyncResp->res.jsonValue["Storage"] = {
2887a25aeccfSNikhil Potade                 {"@odata.id", "/redfish/v1/Systems/system/Storage"}};
2888029573d4SEd Tanous 
28898d1b46d7Szhanghch05             asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"] = {
2890cc340dd9SEd Tanous                 {"target",
2891029573d4SEd Tanous                  "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset"},
28921cb1a9e6SAppaRao Puli                 {"@Redfish.ActionInfo",
28931cb1a9e6SAppaRao Puli                  "/redfish/v1/Systems/system/ResetActionInfo"}};
2894c5b2abe0SLewanczyk, Dawid 
28958d1b46d7Szhanghch05             asyncResp->res.jsonValue["LogServices"] = {
2896029573d4SEd Tanous                 {"@odata.id", "/redfish/v1/Systems/system/LogServices"}};
2897c4bf6374SJason M. Bills 
28988d1b46d7Szhanghch05             asyncResp->res.jsonValue["Bios"] = {
2899d82a3acdSCarol Wang                 {"@odata.id", "/redfish/v1/Systems/system/Bios"}};
2900d82a3acdSCarol Wang 
29018d1b46d7Szhanghch05             asyncResp->res.jsonValue["Links"]["ManagedBy"] = {
2902c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
2903c5d03ff4SJennifer Lee 
29048d1b46d7Szhanghch05             asyncResp->res.jsonValue["Status"] = {
2905c5d03ff4SJennifer Lee                 {"Health", "OK"},
2906c5d03ff4SJennifer Lee                 {"State", "Enabled"},
2907c5d03ff4SJennifer Lee             };
29080e8ac5e7SGunnar Mills 
29090e8ac5e7SGunnar Mills             // Fill in SerialConsole info
29100e8ac5e7SGunnar Mills             asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] =
29110e8ac5e7SGunnar Mills                 15;
29120e8ac5e7SGunnar Mills             asyncResp->res.jsonValue["SerialConsole"]["IPMI"] = {
29130e8ac5e7SGunnar Mills                 {"ServiceEnabled", true},
29140e8ac5e7SGunnar Mills             };
29150e8ac5e7SGunnar Mills             // TODO (Gunnar): Should look for obmc-console-ssh@2200.service
29160e8ac5e7SGunnar Mills             asyncResp->res.jsonValue["SerialConsole"]["SSH"] = {
29170e8ac5e7SGunnar Mills                 {"ServiceEnabled", true},
29180e8ac5e7SGunnar Mills                 {"Port", 2200},
29190e8ac5e7SGunnar Mills                 // https://github.com/openbmc/docs/blob/master/console.md
29200e8ac5e7SGunnar Mills                 {"HotKeySequenceDisplay", "Press ~. to exit console"},
29210e8ac5e7SGunnar Mills             };
29220e8ac5e7SGunnar Mills 
29230e8ac5e7SGunnar Mills #ifdef BMCWEB_ENABLE_KVM
29240e8ac5e7SGunnar Mills             // Fill in GraphicalConsole info
29250e8ac5e7SGunnar Mills             asyncResp->res.jsonValue["GraphicalConsole"] = {
29260e8ac5e7SGunnar Mills                 {"ServiceEnabled", true},
29270e8ac5e7SGunnar Mills                 {"MaxConcurrentSessions", 4},
29280e8ac5e7SGunnar Mills                 {"ConnectTypesSupported", {"KVMIP"}},
29290e8ac5e7SGunnar Mills             };
29300e8ac5e7SGunnar Mills #endif // BMCWEB_ENABLE_KVM
2931e284a7c1SJames Feist             constexpr const std::array<const char*, 4> inventoryForSystems = {
2932b49ac873SJames Feist                 "xyz.openbmc_project.Inventory.Item.Dimm",
29332ad9c2f6SJames Feist                 "xyz.openbmc_project.Inventory.Item.Cpu",
2934e284a7c1SJames Feist                 "xyz.openbmc_project.Inventory.Item.Drive",
2935e284a7c1SJames Feist                 "xyz.openbmc_project.Inventory.Item.StorageController"};
2936b49ac873SJames Feist 
2937b49ac873SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
2938b49ac873SJames Feist             crow::connections::systemBus->async_method_call(
2939b49ac873SJames Feist                 [health](const boost::system::error_code ec,
2940b49ac873SJames Feist                          std::vector<std::string>& resp) {
2941b49ac873SJames Feist                     if (ec)
2942b49ac873SJames Feist                     {
2943b49ac873SJames Feist                         // no inventory
2944b49ac873SJames Feist                         return;
2945b49ac873SJames Feist                     }
2946b49ac873SJames Feist 
2947b49ac873SJames Feist                     health->inventory = std::move(resp);
2948b49ac873SJames Feist                 },
2949b49ac873SJames Feist                 "xyz.openbmc_project.ObjectMapper",
2950b49ac873SJames Feist                 "/xyz/openbmc_project/object_mapper",
2951b49ac873SJames Feist                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
2952b49ac873SJames Feist                 int32_t(0), inventoryForSystems);
2953b49ac873SJames Feist 
2954b49ac873SJames Feist             health->populate();
2955b49ac873SJames Feist 
29568d1b46d7Szhanghch05             getMainChassisId(
29578d1b46d7Szhanghch05                 asyncResp, [](const std::string& chassisId,
29588d1b46d7Szhanghch05                               const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2959c5d03ff4SJennifer Lee                     aRsp->res.jsonValue["Links"]["Chassis"] = {
2960c5d03ff4SJennifer Lee                         {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
2961c5d03ff4SJennifer Lee                 });
2962a3002228SAppaRao Puli 
29639f8bfa7cSGunnar Mills             getLocationIndicatorActive(asyncResp);
29649f8bfa7cSGunnar Mills             // TODO (Gunnar): Remove IndicatorLED after enough time has passed
2965a3002228SAppaRao Puli             getIndicatorLedState(asyncResp);
29665bc2dc8eSJames Feist             getComputerSystem(asyncResp, health);
29676c34de48SEd Tanous             getHostState(asyncResp);
2968491d8ee7SSantosh Puranik             getBootProperties(asyncResp);
2969978b8803SAndrew Geissler             getBootProgress(asyncResp);
2970adbe192aSJason M. Bills             getPCIeDeviceList(asyncResp, "PCIeDevices");
297151709ffdSYong Li             getHostWatchdogTimer(asyncResp);
2972c6a620f2SGeorge Liu             getPowerRestorePolicy(asyncResp);
29736bd5a8d2SGunnar Mills             getAutomaticRetry(asyncResp);
2974c0557e1aSGunnar Mills             getLastResetTime(asyncResp);
2975a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
2976a6349918SAppaRao Puli             getProvisioningStatus(asyncResp);
2977a6349918SAppaRao Puli #endif
29781981771bSAli Ahmed             getTrustedModuleRequiredToBoot(asyncResp);
29793a2d0424SChris Cain             getPowerMode(asyncResp);
298037bbf98cSChris Cain             getIdlePowerSaver(asyncResp);
29817e860f15SJohn Edward Broadbent         });
29827e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/")
2983ed398213SEd Tanous         .privileges(redfish::privileges::patchComputerSystem)
29847e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
29857e860f15SJohn Edward Broadbent             [](const crow::Request& req,
29867e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
29879f8bfa7cSGunnar Mills                 std::optional<bool> locationIndicatorActive;
2988cde19e5fSSantosh Puranik                 std::optional<std::string> indicatorLed;
2989491d8ee7SSantosh Puranik                 std::optional<nlohmann::json> bootProps;
2990c45f0082SYong Li                 std::optional<nlohmann::json> wdtTimerProps;
299198e386ecSGunnar Mills                 std::optional<std::string> assetTag;
2992c6a620f2SGeorge Liu                 std::optional<std::string> powerRestorePolicy;
29933a2d0424SChris Cain                 std::optional<std::string> powerMode;
299437bbf98cSChris Cain                 std::optional<nlohmann::json> ipsProps;
29959f8bfa7cSGunnar Mills                 if (!json_util::readJson(
29968d1b46d7Szhanghch05                         req, asyncResp->res, "IndicatorLED", indicatorLed,
29977e860f15SJohn Edward Broadbent                         "LocationIndicatorActive", locationIndicatorActive,
29987e860f15SJohn Edward Broadbent                         "Boot", bootProps, "WatchdogTimer", wdtTimerProps,
29997e860f15SJohn Edward Broadbent                         "PowerRestorePolicy", powerRestorePolicy, "AssetTag",
300037bbf98cSChris Cain                         assetTag, "PowerMode", powerMode, "IdlePowerSaver",
300137bbf98cSChris Cain                         ipsProps))
30026617338dSEd Tanous                 {
30036617338dSEd Tanous                     return;
30046617338dSEd Tanous                 }
3005491d8ee7SSantosh Puranik 
30068d1b46d7Szhanghch05                 asyncResp->res.result(boost::beast::http::status::no_content);
3007c45f0082SYong Li 
300898e386ecSGunnar Mills                 if (assetTag)
300998e386ecSGunnar Mills                 {
301098e386ecSGunnar Mills                     setAssetTag(asyncResp, *assetTag);
301198e386ecSGunnar Mills                 }
301298e386ecSGunnar Mills 
3013c45f0082SYong Li                 if (wdtTimerProps)
3014c45f0082SYong Li                 {
3015c45f0082SYong Li                     std::optional<bool> wdtEnable;
3016c45f0082SYong Li                     std::optional<std::string> wdtTimeOutAction;
3017c45f0082SYong Li 
3018c45f0082SYong Li                     if (!json_util::readJson(*wdtTimerProps, asyncResp->res,
3019c45f0082SYong Li                                              "FunctionEnabled", wdtEnable,
3020c45f0082SYong Li                                              "TimeoutAction", wdtTimeOutAction))
3021c45f0082SYong Li                     {
3022c45f0082SYong Li                         return;
3023c45f0082SYong Li                     }
3024f23b7296SEd Tanous                     setWDTProperties(asyncResp, wdtEnable, wdtTimeOutAction);
3025c45f0082SYong Li                 }
3026c45f0082SYong Li 
3027491d8ee7SSantosh Puranik                 if (bootProps)
3028491d8ee7SSantosh Puranik                 {
3029491d8ee7SSantosh Puranik                     std::optional<std::string> bootSource;
3030cd9a4666SKonstantin Aladyshev                     std::optional<std::string> bootType;
3031491d8ee7SSantosh Puranik                     std::optional<std::string> bootEnable;
303269f35306SGunnar Mills                     std::optional<std::string> automaticRetryConfig;
3033ac7e1e0bSAli Ahmed                     std::optional<bool> trustedModuleRequiredToBoot;
3034491d8ee7SSantosh Puranik 
303569f35306SGunnar Mills                     if (!json_util::readJson(
30367e860f15SJohn Edward Broadbent                             *bootProps, asyncResp->res,
30377e860f15SJohn Edward Broadbent                             "BootSourceOverrideTarget", bootSource,
3038cd9a4666SKonstantin Aladyshev                             "BootSourceOverrideMode", bootType,
30397e860f15SJohn Edward Broadbent                             "BootSourceOverrideEnabled", bootEnable,
3040ac7e1e0bSAli Ahmed                             "AutomaticRetryConfig", automaticRetryConfig,
3041ac7e1e0bSAli Ahmed                             "TrustedModuleRequiredToBoot",
3042ac7e1e0bSAli Ahmed                             trustedModuleRequiredToBoot))
3043491d8ee7SSantosh Puranik                     {
3044491d8ee7SSantosh Puranik                         return;
3045491d8ee7SSantosh Puranik                     }
3046c21865c4SKonstantin Aladyshev 
3047cd9a4666SKonstantin Aladyshev                     if (bootSource || bootType || bootEnable)
304869f35306SGunnar Mills                     {
3049c21865c4SKonstantin Aladyshev                         setBootProperties(asyncResp, bootSource, bootType,
3050c21865c4SKonstantin Aladyshev                                           bootEnable);
3051491d8ee7SSantosh Puranik                     }
305269f35306SGunnar Mills                     if (automaticRetryConfig)
305369f35306SGunnar Mills                     {
3054f23b7296SEd Tanous                         setAutomaticRetry(asyncResp, *automaticRetryConfig);
305569f35306SGunnar Mills                     }
3056ac7e1e0bSAli Ahmed 
3057ac7e1e0bSAli Ahmed                     if (trustedModuleRequiredToBoot)
3058ac7e1e0bSAli Ahmed                     {
3059ac7e1e0bSAli Ahmed                         setTrustedModuleRequiredToBoot(
3060ac7e1e0bSAli Ahmed                             asyncResp, *trustedModuleRequiredToBoot);
3061ac7e1e0bSAli Ahmed                     }
306269f35306SGunnar Mills                 }
3063265c1602SJohnathan Mantey 
30649f8bfa7cSGunnar Mills                 if (locationIndicatorActive)
30659f8bfa7cSGunnar Mills                 {
30667e860f15SJohn Edward Broadbent                     setLocationIndicatorActive(asyncResp,
30677e860f15SJohn Edward Broadbent                                                *locationIndicatorActive);
30689f8bfa7cSGunnar Mills                 }
30699f8bfa7cSGunnar Mills 
30707e860f15SJohn Edward Broadbent                 // TODO (Gunnar): Remove IndicatorLED after enough time has
30717e860f15SJohn Edward Broadbent                 // passed
30729712f8acSEd Tanous                 if (indicatorLed)
30736617338dSEd Tanous                 {
3074f23b7296SEd Tanous                     setIndicatorLedState(asyncResp, *indicatorLed);
30757e860f15SJohn Edward Broadbent                     asyncResp->res.addHeader(
30767e860f15SJohn Edward Broadbent                         boost::beast::http::field::warning,
3077d6aa0093SGunnar Mills                         "299 - \"IndicatorLED is deprecated. Use "
3078d6aa0093SGunnar Mills                         "LocationIndicatorActive instead.\"");
30796617338dSEd Tanous                 }
3080c6a620f2SGeorge Liu 
3081c6a620f2SGeorge Liu                 if (powerRestorePolicy)
3082c6a620f2SGeorge Liu                 {
30834e69c904SGunnar Mills                     setPowerRestorePolicy(asyncResp, *powerRestorePolicy);
3084c6a620f2SGeorge Liu                 }
30853a2d0424SChris Cain 
30863a2d0424SChris Cain                 if (powerMode)
30873a2d0424SChris Cain                 {
30883a2d0424SChris Cain                     setPowerMode(asyncResp, *powerMode);
30893a2d0424SChris Cain                 }
309037bbf98cSChris Cain 
309137bbf98cSChris Cain                 if (ipsProps)
309237bbf98cSChris Cain                 {
309337bbf98cSChris Cain                     std::optional<bool> ipsEnable;
309437bbf98cSChris Cain                     std::optional<uint8_t> ipsEnterUtil;
309537bbf98cSChris Cain                     std::optional<uint64_t> ipsEnterTime;
309637bbf98cSChris Cain                     std::optional<uint8_t> ipsExitUtil;
309737bbf98cSChris Cain                     std::optional<uint64_t> ipsExitTime;
309837bbf98cSChris Cain 
309937bbf98cSChris Cain                     if (!json_util::readJson(
310037bbf98cSChris Cain                             *ipsProps, asyncResp->res, "Enabled", ipsEnable,
310137bbf98cSChris Cain                             "EnterUtilizationPercent", ipsEnterUtil,
310237bbf98cSChris Cain                             "EnterDwellTimeSeconds", ipsEnterTime,
310337bbf98cSChris Cain                             "ExitUtilizationPercent", ipsExitUtil,
310437bbf98cSChris Cain                             "ExitDwellTimeSeconds", ipsExitTime))
310537bbf98cSChris Cain                     {
310637bbf98cSChris Cain                         return;
310737bbf98cSChris Cain                     }
310837bbf98cSChris Cain                     setIdlePowerSaver(asyncResp, ipsEnable, ipsEnterUtil,
310937bbf98cSChris Cain                                       ipsEnterTime, ipsExitUtil, ipsExitTime);
311037bbf98cSChris Cain                 }
31117e860f15SJohn Edward Broadbent             });
3112c5b2abe0SLewanczyk, Dawid }
31131cb1a9e6SAppaRao Puli 
31141cb1a9e6SAppaRao Puli /**
31151cb1a9e6SAppaRao Puli  * SystemResetActionInfo derived class for delivering Computer Systems
31161cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
31171cb1a9e6SAppaRao Puli  */
31187e860f15SJohn Edward Broadbent inline void requestRoutesSystemResetActionInfo(App& app)
31191cb1a9e6SAppaRao Puli {
31201cb1a9e6SAppaRao Puli 
31211cb1a9e6SAppaRao Puli     /**
31221cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
31231cb1a9e6SAppaRao Puli      */
31247e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/ResetActionInfo/")
3125ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
31267e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
31277e860f15SJohn Edward Broadbent             [](const crow::Request&,
31287e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
31298d1b46d7Szhanghch05                 asyncResp->res.jsonValue = {
31301cb1a9e6SAppaRao Puli                     {"@odata.type", "#ActionInfo.v1_1_2.ActionInfo"},
31311cb1a9e6SAppaRao Puli                     {"@odata.id", "/redfish/v1/Systems/system/ResetActionInfo"},
31321cb1a9e6SAppaRao Puli                     {"Name", "Reset Action Info"},
31331cb1a9e6SAppaRao Puli                     {"Id", "ResetActionInfo"},
31341cb1a9e6SAppaRao Puli                     {"Parameters",
31351cb1a9e6SAppaRao Puli                      {{{"Name", "ResetType"},
31361cb1a9e6SAppaRao Puli                        {"Required", true},
31371cb1a9e6SAppaRao Puli                        {"DataType", "String"},
31381cb1a9e6SAppaRao Puli                        {"AllowableValues",
31397e860f15SJohn Edward Broadbent                         {"On", "ForceOff", "ForceOn", "ForceRestart",
31407e860f15SJohn Edward Broadbent                          "GracefulRestart", "GracefulShutdown", "PowerCycle",
31417e860f15SJohn Edward Broadbent                          "Nmi"}}}}}};
31427e860f15SJohn Edward Broadbent             });
31431cb1a9e6SAppaRao Puli }
3144c5b2abe0SLewanczyk, Dawid } // namespace redfish
3145