xref: /openbmc/bmcweb/features/redfish/lib/systems.hpp (revision 3ba0007367777144f474fdf99439ae8c03633486)
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 
181e1e598dSJonathan Doman #include "dbus_singleton.hpp"
19b49ac873SJames Feist #include "health.hpp"
201c8fba97SJames Feist #include "led.hpp"
21f5c9f8bdSJason M. Bills #include "pcie.hpp"
22f4c99e70SEd Tanous #include "query.hpp"
23c5d03ff4SJennifer Lee #include "redfish_util.hpp"
24c5d03ff4SJennifer Lee 
257e860f15SJohn Edward Broadbent #include <app.hpp>
269712f8acSEd Tanous #include <boost/container/flat_map.hpp>
27168e20c1SEd Tanous #include <dbus_utility.hpp>
28ed398213SEd Tanous #include <registries/privilege_registry.hpp>
291e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
30cb7e1e7bSAndrew Geissler #include <utils/fw_utils.hpp>
31c5b2abe0SLewanczyk, Dawid #include <utils/json_utils.hpp>
321214b7e7SGunnar Mills 
33abf2add6SEd Tanous #include <variant>
34c5b2abe0SLewanczyk, Dawid 
351abe55efSEd Tanous namespace redfish
361abe55efSEd Tanous {
37c5b2abe0SLewanczyk, Dawid 
389d3ae10eSAlpana Kumari /**
399d3ae10eSAlpana Kumari  * @brief Updates the Functional State of DIMMs
409d3ae10eSAlpana Kumari  *
419d3ae10eSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
429d3ae10eSAlpana Kumari  * @param[in] dimmState Dimm's Functional state, true/false
439d3ae10eSAlpana Kumari  *
449d3ae10eSAlpana Kumari  * @return None.
459d3ae10eSAlpana Kumari  */
468d1b46d7Szhanghch05 inline void
478d1b46d7Szhanghch05     updateDimmProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
481e1e598dSJonathan Doman                          bool isDimmFunctional)
499d3ae10eSAlpana Kumari {
501e1e598dSJonathan Doman     BMCWEB_LOG_DEBUG << "Dimm Functional: " << isDimmFunctional;
519d3ae10eSAlpana Kumari 
529d3ae10eSAlpana Kumari     // Set it as Enabled if at least one DIMM is functional
539d3ae10eSAlpana Kumari     // Update STATE only if previous State was DISABLED and current Dimm is
549d3ae10eSAlpana Kumari     // ENABLED.
559d3ae10eSAlpana Kumari     nlohmann::json& prevMemSummary =
569d3ae10eSAlpana Kumari         aResp->res.jsonValue["MemorySummary"]["Status"]["State"];
579d3ae10eSAlpana Kumari     if (prevMemSummary == "Disabled")
589d3ae10eSAlpana Kumari     {
59e05aec50SEd Tanous         if (isDimmFunctional)
609d3ae10eSAlpana Kumari         {
619d3ae10eSAlpana Kumari             aResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
629d3ae10eSAlpana Kumari                 "Enabled";
639d3ae10eSAlpana Kumari         }
649d3ae10eSAlpana Kumari     }
659d3ae10eSAlpana Kumari }
669d3ae10eSAlpana Kumari 
6757e8c9beSAlpana Kumari /*
6857e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Count" based on Cpu PresenceState
6957e8c9beSAlpana Kumari  *
7057e8c9beSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
7157e8c9beSAlpana Kumari  * @param[in] cpuPresenceState CPU present or not
7257e8c9beSAlpana Kumari  *
7357e8c9beSAlpana Kumari  * @return None.
7457e8c9beSAlpana Kumari  */
751e1e598dSJonathan Doman inline void
761e1e598dSJonathan Doman     modifyCpuPresenceState(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
771e1e598dSJonathan Doman                            bool isCpuPresent)
7857e8c9beSAlpana Kumari {
791e1e598dSJonathan Doman     BMCWEB_LOG_DEBUG << "Cpu Present: " << isCpuPresent;
8057e8c9beSAlpana Kumari 
8155f79e6fSEd Tanous     if (isCpuPresent)
8257e8c9beSAlpana Kumari     {
83b4b9595aSJames Feist         nlohmann::json& procCount =
84b4b9595aSJames Feist             aResp->res.jsonValue["ProcessorSummary"]["Count"];
8555f79e6fSEd Tanous         auto* procCountPtr =
86b4b9595aSJames Feist             procCount.get_ptr<nlohmann::json::number_integer_t*>();
87b4b9595aSJames Feist         if (procCountPtr != nullptr)
88b4b9595aSJames Feist         {
89b4b9595aSJames Feist             // shouldn't be possible to be nullptr
90b4b9595aSJames Feist             *procCountPtr += 1;
9157e8c9beSAlpana Kumari         }
92b4b9595aSJames Feist     }
9357e8c9beSAlpana Kumari }
9457e8c9beSAlpana Kumari 
9557e8c9beSAlpana Kumari /*
9657e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Status" "State" based on
9757e8c9beSAlpana Kumari  *        CPU Functional State
9857e8c9beSAlpana Kumari  *
9957e8c9beSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
10057e8c9beSAlpana Kumari  * @param[in] cpuFunctionalState is CPU functional true/false
10157e8c9beSAlpana Kumari  *
10257e8c9beSAlpana Kumari  * @return None.
10357e8c9beSAlpana Kumari  */
1041e1e598dSJonathan Doman inline void
1051e1e598dSJonathan Doman     modifyCpuFunctionalState(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1061e1e598dSJonathan Doman                              bool isCpuFunctional)
10757e8c9beSAlpana Kumari {
1081e1e598dSJonathan Doman     BMCWEB_LOG_DEBUG << "Cpu Functional: " << isCpuFunctional;
10957e8c9beSAlpana Kumari 
11057e8c9beSAlpana Kumari     nlohmann::json& prevProcState =
11157e8c9beSAlpana Kumari         aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"];
11257e8c9beSAlpana Kumari 
11357e8c9beSAlpana Kumari     // Set it as Enabled if at least one CPU is functional
11457e8c9beSAlpana Kumari     // Update STATE only if previous State was Non_Functional and current CPU is
11557e8c9beSAlpana Kumari     // Functional.
11657e8c9beSAlpana Kumari     if (prevProcState == "Disabled")
11757e8c9beSAlpana Kumari     {
118e05aec50SEd Tanous         if (isCpuFunctional)
11957e8c9beSAlpana Kumari         {
12057e8c9beSAlpana Kumari             aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
12157e8c9beSAlpana Kumari                 "Enabled";
12257e8c9beSAlpana Kumari         }
12357e8c9beSAlpana Kumari     }
12457e8c9beSAlpana Kumari }
12557e8c9beSAlpana Kumari 
126b9d36b47SEd Tanous inline void
127b9d36b47SEd Tanous     getProcessorProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
128b9d36b47SEd Tanous                            const std::string& service, const std::string& path,
129b9d36b47SEd Tanous                            const dbus::utility::DBusPropertiesMap& properties)
13003fbed92SAli Ahmed {
13103fbed92SAli Ahmed 
13203fbed92SAli Ahmed     BMCWEB_LOG_DEBUG << "Got " << properties.size() << " Cpu properties.";
13303fbed92SAli Ahmed 
1341e1e598dSJonathan Doman     auto getCpuPresenceState = [aResp](const boost::system::error_code ec3,
1351e1e598dSJonathan 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 
1441e1e598dSJonathan Doman     auto getCpuFunctionalState = [aResp](const boost::system::error_code ec3,
1451e1e598dSJonathan 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
1551e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
1561e1e598dSJonathan Doman         *crow::connections::systemBus, service, path,
1571e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
1581e1e598dSJonathan Doman         std::move(getCpuPresenceState));
15903fbed92SAli Ahmed 
16003fbed92SAli Ahmed     // Get the Functional State
1611e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
1621e1e598dSJonathan Doman         *crow::connections::systemBus, service, path,
1631e1e598dSJonathan Doman         "xyz.openbmc_project.State.Decorator.OperationalStatus", "Functional",
1641e1e598dSJonathan 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 
179e662eae8SEd Tanous             if (coreCountVal == nullptr)
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,
218b9d36b47SEd Tanous                const dbus::utility::DBusPropertiesMap& properties) {
21903fbed92SAli Ahmed         if (ec2)
22003fbed92SAli Ahmed         {
22103fbed92SAli Ahmed             BMCWEB_LOG_ERROR << "DBUS response error " << ec2;
22203fbed92SAli Ahmed             messages::internalError(aResp->res);
22303fbed92SAli Ahmed             return;
22403fbed92SAli Ahmed         }
22503fbed92SAli Ahmed         getProcessorProperties(aResp, service, path, properties);
22603fbed92SAli Ahmed         },
22703fbed92SAli Ahmed         service, path, "org.freedesktop.DBus.Properties", "GetAll",
22803fbed92SAli Ahmed         "xyz.openbmc_project.Inventory.Item.Cpu");
22903fbed92SAli Ahmed }
23003fbed92SAli Ahmed 
23157e8c9beSAlpana Kumari /*
232c5b2abe0SLewanczyk, Dawid  * @brief Retrieves computer system properties over dbus
233c5b2abe0SLewanczyk, Dawid  *
234c5b2abe0SLewanczyk, Dawid  * @param[in] aResp Shared pointer for completing asynchronous calls
2358f9ee3cdSGunnar Mills  * @param[in] systemHealth  Shared HealthPopulate pointer
236c5b2abe0SLewanczyk, Dawid  *
237c5b2abe0SLewanczyk, Dawid  * @return None.
238c5b2abe0SLewanczyk, Dawid  */
239b5a76932SEd Tanous inline void
2408d1b46d7Szhanghch05     getComputerSystem(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
241b5a76932SEd Tanous                       const std::shared_ptr<HealthPopulate>& systemHealth)
2421abe55efSEd Tanous {
24355c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get available system components.";
2449d3ae10eSAlpana Kumari 
24555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
246b9d36b47SEd Tanous         [aResp,
247b9d36b47SEd Tanous          systemHealth](const boost::system::error_code ec,
248b9d36b47SEd Tanous                        const dbus::utility::MapperGetSubTreeResponse& subtree) {
2491abe55efSEd Tanous         if (ec)
2501abe55efSEd Tanous         {
25155c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error";
252f12894f8SJason M. Bills             messages::internalError(aResp->res);
253c5b2abe0SLewanczyk, Dawid             return;
254c5b2abe0SLewanczyk, Dawid         }
255c5b2abe0SLewanczyk, Dawid         // Iterate over all retrieved ObjectPaths.
256002d39b4SEd Tanous         for (const std::pair<
257002d39b4SEd Tanous                  std::string,
258002d39b4SEd Tanous                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
2591214b7e7SGunnar Mills                  object : subtree)
2601abe55efSEd Tanous         {
261c5b2abe0SLewanczyk, Dawid             const std::string& path = object.first;
26255c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Got path: " << path;
263002d39b4SEd Tanous             const std::vector<std::pair<std::string, std::vector<std::string>>>&
2641214b7e7SGunnar Mills                 connectionNames = object.second;
26526f6976fSEd Tanous             if (connectionNames.empty())
2661abe55efSEd Tanous             {
267c5b2abe0SLewanczyk, Dawid                 continue;
268c5b2abe0SLewanczyk, Dawid             }
269029573d4SEd Tanous 
2705bc2dc8eSJames Feist             auto memoryHealth = std::make_shared<HealthPopulate>(
271dfababfcSNan Zhou                 aResp, "/MemorySummary/Status"_json_pointer);
2725bc2dc8eSJames Feist 
2735bc2dc8eSJames Feist             auto cpuHealth = std::make_shared<HealthPopulate>(
274dfababfcSNan Zhou                 aResp, "/ProcessorSummary/Status"_json_pointer);
2755bc2dc8eSJames Feist 
2765bc2dc8eSJames Feist             systemHealth->children.emplace_back(memoryHealth);
2775bc2dc8eSJames Feist             systemHealth->children.emplace_back(cpuHealth);
2785bc2dc8eSJames Feist 
2796c34de48SEd Tanous             // This is not system, so check if it's cpu, dimm, UUID or
2806c34de48SEd Tanous             // BiosVer
28104a258f4SEd Tanous             for (const auto& connection : connectionNames)
2821abe55efSEd Tanous             {
28304a258f4SEd Tanous                 for (const auto& interfaceName : connection.second)
2841abe55efSEd Tanous                 {
28504a258f4SEd Tanous                     if (interfaceName ==
28604a258f4SEd Tanous                         "xyz.openbmc_project.Inventory.Item.Dimm")
2871abe55efSEd Tanous                     {
2881abe55efSEd Tanous                         BMCWEB_LOG_DEBUG
28904a258f4SEd Tanous                             << "Found Dimm, now get its properties.";
2909d3ae10eSAlpana Kumari 
29155c7b7a2SEd Tanous                         crow::connections::systemBus->async_method_call(
2929d3ae10eSAlpana Kumari                             [aResp, service{connection.first},
293f23b7296SEd Tanous                              path](const boost::system::error_code ec2,
294b9d36b47SEd Tanous                                    const dbus::utility::DBusPropertiesMap&
2951214b7e7SGunnar Mills                                        properties) {
296cb13a392SEd Tanous                             if (ec2)
2971abe55efSEd Tanous                             {
298002d39b4SEd Tanous                                 BMCWEB_LOG_ERROR << "DBUS response error "
299002d39b4SEd Tanous                                                  << ec2;
300f12894f8SJason M. Bills                                 messages::internalError(aResp->res);
301c5b2abe0SLewanczyk, Dawid                                 return;
302c5b2abe0SLewanczyk, Dawid                             }
303002d39b4SEd Tanous                             BMCWEB_LOG_DEBUG << "Got " << properties.size()
304c5b2abe0SLewanczyk, Dawid                                              << " Dimm properties.";
3059d3ae10eSAlpana Kumari 
30626f6976fSEd Tanous                             if (!properties.empty())
3079d3ae10eSAlpana Kumari                             {
308168e20c1SEd Tanous                                 for (const std::pair<
309168e20c1SEd Tanous                                          std::string,
310002d39b4SEd Tanous                                          dbus::utility::DbusVariantType>&
3111214b7e7SGunnar Mills                                          property : properties)
3121abe55efSEd Tanous                                 {
313002d39b4SEd Tanous                                     if (property.first != "MemorySizeInKB")
3141abe55efSEd Tanous                                     {
3155fd7ba65SCheng C Yang                                         continue;
3165fd7ba65SCheng C Yang                                     }
3175fd7ba65SCheng C Yang                                     const uint32_t* value =
318002d39b4SEd Tanous                                         std::get_if<uint32_t>(&property.second);
3195fd7ba65SCheng C Yang                                     if (value == nullptr)
3201abe55efSEd Tanous                                     {
3215fd7ba65SCheng C Yang                                         BMCWEB_LOG_DEBUG
3220fda0f12SGeorge Liu                                             << "Find incorrect type of MemorySize";
3235fd7ba65SCheng C Yang                                         continue;
3245fd7ba65SCheng C Yang                                     }
3255fd7ba65SCheng C Yang                                     nlohmann::json& totalMemory =
326002d39b4SEd Tanous                                         aResp->res
327002d39b4SEd Tanous                                             .jsonValue["MemorySummary"]
3280fda0f12SGeorge Liu                                                       ["TotalSystemMemoryGiB"];
3295fd7ba65SCheng C Yang                                     uint64_t* preValue =
330002d39b4SEd Tanous                                         totalMemory.get_ptr<uint64_t*>();
3315fd7ba65SCheng C Yang                                     if (preValue == nullptr)
3325fd7ba65SCheng C Yang                                     {
3335fd7ba65SCheng C Yang                                         continue;
3345fd7ba65SCheng C Yang                                     }
3355fd7ba65SCheng C Yang                                     aResp->res
3365fd7ba65SCheng C Yang                                         .jsonValue["MemorySummary"]
337002d39b4SEd Tanous                                                   ["TotalSystemMemoryGiB"] =
338002d39b4SEd Tanous                                         *value / (1024 * 1024) + *preValue;
339002d39b4SEd Tanous                                     aResp->res.jsonValue["MemorySummary"]
3409d3ae10eSAlpana Kumari                                                         ["Status"]["State"] =
3411abe55efSEd Tanous                                         "Enabled";
342c5b2abe0SLewanczyk, Dawid                                 }
343c5b2abe0SLewanczyk, Dawid                             }
3449d3ae10eSAlpana Kumari                             else
3459d3ae10eSAlpana Kumari                             {
3461e1e598dSJonathan Doman                                 sdbusplus::asio::getProperty<bool>(
347002d39b4SEd Tanous                                     *crow::connections::systemBus, service,
348002d39b4SEd Tanous                                     path,
3491e1e598dSJonathan Doman                                     "xyz.openbmc_project.State."
3501e1e598dSJonathan Doman                                     "Decorator.OperationalStatus",
3511e1e598dSJonathan Doman                                     "Functional",
352002d39b4SEd Tanous                                     [aResp](const boost::system::error_code ec3,
3531e1e598dSJonathan Doman                                             bool dimmState) {
354cb13a392SEd Tanous                                     if (ec3)
3559d3ae10eSAlpana Kumari                                     {
3569d3ae10eSAlpana Kumari                                         BMCWEB_LOG_ERROR
357002d39b4SEd Tanous                                             << "DBUS response error " << ec3;
3589d3ae10eSAlpana Kumari                                         return;
3599d3ae10eSAlpana Kumari                                     }
360002d39b4SEd Tanous                                     updateDimmProperties(aResp, dimmState);
3611e1e598dSJonathan Doman                                     });
3629d3ae10eSAlpana Kumari                             }
363c5b2abe0SLewanczyk, Dawid                             },
36404a258f4SEd Tanous                             connection.first, path,
3656c34de48SEd Tanous                             "org.freedesktop.DBus.Properties", "GetAll",
3666c34de48SEd Tanous                             "xyz.openbmc_project.Inventory.Item.Dimm");
3675bc2dc8eSJames Feist 
3685bc2dc8eSJames Feist                         memoryHealth->inventory.emplace_back(path);
3691abe55efSEd Tanous                     }
37004a258f4SEd Tanous                     else if (interfaceName ==
37104a258f4SEd Tanous                              "xyz.openbmc_project.Inventory.Item.Cpu")
3721abe55efSEd Tanous                     {
3731abe55efSEd Tanous                         BMCWEB_LOG_DEBUG
37404a258f4SEd Tanous                             << "Found Cpu, now get its properties.";
37557e8c9beSAlpana Kumari 
37603fbed92SAli Ahmed                         getProcessorSummary(aResp, connection.first, path);
3775bc2dc8eSJames Feist 
3785bc2dc8eSJames Feist                         cpuHealth->inventory.emplace_back(path);
3791abe55efSEd Tanous                     }
380002d39b4SEd Tanous                     else if (interfaceName == "xyz.openbmc_project.Common.UUID")
3811abe55efSEd Tanous                     {
3821abe55efSEd Tanous                         BMCWEB_LOG_DEBUG
38304a258f4SEd Tanous                             << "Found UUID, now get its properties.";
38455c7b7a2SEd Tanous                         crow::connections::systemBus->async_method_call(
385168e20c1SEd Tanous                             [aResp](const boost::system::error_code ec3,
386b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
3871214b7e7SGunnar Mills                                         properties) {
388cb13a392SEd Tanous                             if (ec3)
3891abe55efSEd Tanous                             {
390002d39b4SEd Tanous                                 BMCWEB_LOG_DEBUG << "DBUS response error "
391002d39b4SEd Tanous                                                  << ec3;
392f12894f8SJason M. Bills                                 messages::internalError(aResp->res);
393c5b2abe0SLewanczyk, Dawid                                 return;
394c5b2abe0SLewanczyk, Dawid                             }
395002d39b4SEd Tanous                             BMCWEB_LOG_DEBUG << "Got " << properties.size()
396c5b2abe0SLewanczyk, Dawid                                              << " UUID properties.";
397168e20c1SEd Tanous                             for (const std::pair<
398168e20c1SEd Tanous                                      std::string,
399002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& property :
400002d39b4SEd Tanous                                  properties)
4011abe55efSEd Tanous                             {
40204a258f4SEd Tanous                                 if (property.first == "UUID")
4031abe55efSEd Tanous                                 {
404c5b2abe0SLewanczyk, Dawid                                     const std::string* value =
4058d78b7a9SPatrick Williams                                         std::get_if<std::string>(
4061b6b96c5SEd Tanous                                             &property.second);
40704a258f4SEd Tanous 
4081abe55efSEd Tanous                                     if (value != nullptr)
4091abe55efSEd Tanous                                     {
410029573d4SEd Tanous                                         std::string valueStr = *value;
41104a258f4SEd Tanous                                         if (valueStr.size() == 32)
4121abe55efSEd Tanous                                         {
413029573d4SEd Tanous                                             valueStr.insert(8, 1, '-');
414029573d4SEd Tanous                                             valueStr.insert(13, 1, '-');
415029573d4SEd Tanous                                             valueStr.insert(18, 1, '-');
416029573d4SEd Tanous                                             valueStr.insert(23, 1, '-');
41704a258f4SEd Tanous                                         }
418029573d4SEd Tanous                                         BMCWEB_LOG_DEBUG << "UUID = "
41904a258f4SEd Tanous                                                          << valueStr;
420002d39b4SEd Tanous                                         aResp->res.jsonValue["UUID"] = valueStr;
421c5b2abe0SLewanczyk, Dawid                                     }
422c5b2abe0SLewanczyk, Dawid                                 }
423c5b2abe0SLewanczyk, Dawid                             }
424c5b2abe0SLewanczyk, Dawid                             },
42504a258f4SEd Tanous                             connection.first, path,
4266c34de48SEd Tanous                             "org.freedesktop.DBus.Properties", "GetAll",
4271abe55efSEd Tanous                             "xyz.openbmc_project.Common.UUID");
428c5b2abe0SLewanczyk, Dawid                     }
429029573d4SEd Tanous                     else if (interfaceName ==
430029573d4SEd Tanous                              "xyz.openbmc_project.Inventory.Item.System")
4311abe55efSEd Tanous                     {
432029573d4SEd Tanous                         crow::connections::systemBus->async_method_call(
433168e20c1SEd Tanous                             [aResp](const boost::system::error_code ec2,
434b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
4351214b7e7SGunnar Mills                                         propertiesList) {
436cb13a392SEd Tanous                             if (ec2)
437029573d4SEd Tanous                             {
438e4a4b9a9SJames Feist                                 // doesn't have to include this
439e4a4b9a9SJames Feist                                 // interface
440029573d4SEd Tanous                                 return;
441029573d4SEd Tanous                             }
442002d39b4SEd Tanous                             BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
443029573d4SEd Tanous                                              << " properties for system";
444168e20c1SEd Tanous                             for (const std::pair<
445168e20c1SEd Tanous                                      std::string,
446002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& property :
447002d39b4SEd Tanous                                  propertiesList)
448029573d4SEd Tanous                             {
449fc5afcf9Sbeccabroek                                 const std::string& propertyName =
450fc5afcf9Sbeccabroek                                     property.first;
451fc5afcf9Sbeccabroek                                 if ((propertyName == "PartNumber") ||
452fc5afcf9Sbeccabroek                                     (propertyName == "SerialNumber") ||
453fc5afcf9Sbeccabroek                                     (propertyName == "Manufacturer") ||
4545235d964SSunnySrivastava1984                                     (propertyName == "Model") ||
4555235d964SSunnySrivastava1984                                     (propertyName == "SubModel"))
456fc5afcf9Sbeccabroek                                 {
457029573d4SEd Tanous                                     const std::string* value =
458fc5afcf9Sbeccabroek                                         std::get_if<std::string>(
459029573d4SEd Tanous                                             &property.second);
460029573d4SEd Tanous                                     if (value != nullptr)
461029573d4SEd Tanous                                     {
462002d39b4SEd Tanous                                         aResp->res.jsonValue[propertyName] =
463029573d4SEd Tanous                                             *value;
464029573d4SEd Tanous                                     }
465029573d4SEd Tanous                                 }
466fc5afcf9Sbeccabroek                             }
467c1e236a6SGunnar Mills 
468cb7e1e7bSAndrew Geissler                             // Grab the bios version
469f97ddba7SGunnar Mills                             fw_util::populateFirmwareInformation(
470002d39b4SEd Tanous                                 aResp, fw_util::biosPurpose, "BiosVersion",
471002d39b4SEd Tanous                                 false);
472029573d4SEd Tanous                             },
473029573d4SEd Tanous                             connection.first, path,
474029573d4SEd Tanous                             "org.freedesktop.DBus.Properties", "GetAll",
4750fda0f12SGeorge Liu                             "xyz.openbmc_project.Inventory.Decorator.Asset");
476e4a4b9a9SJames Feist 
4771e1e598dSJonathan Doman                         sdbusplus::asio::getProperty<std::string>(
4781e1e598dSJonathan Doman                             *crow::connections::systemBus, connection.first,
4791e1e598dSJonathan Doman                             path,
4801e1e598dSJonathan Doman                             "xyz.openbmc_project.Inventory.Decorator."
4811e1e598dSJonathan Doman                             "AssetTag",
4821e1e598dSJonathan Doman                             "AssetTag",
483168e20c1SEd Tanous                             [aResp](const boost::system::error_code ec2,
4841e1e598dSJonathan Doman                                     const std::string& value) {
485cb13a392SEd Tanous                             if (ec2)
486e4a4b9a9SJames Feist                             {
487e4a4b9a9SJames Feist                                 // doesn't have to include this
488e4a4b9a9SJames Feist                                 // interface
489e4a4b9a9SJames Feist                                 return;
490e4a4b9a9SJames Feist                             }
491e4a4b9a9SJames Feist 
4921e1e598dSJonathan Doman                             aResp->res.jsonValue["AssetTag"] = value;
4931e1e598dSJonathan Doman                             });
494029573d4SEd Tanous                     }
495029573d4SEd Tanous                 }
496029573d4SEd Tanous             }
497c5b2abe0SLewanczyk, Dawid         }
498c5b2abe0SLewanczyk, Dawid         },
499c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper",
500c5b2abe0SLewanczyk, Dawid         "/xyz/openbmc_project/object_mapper",
501c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
5026617338dSEd Tanous         "/xyz/openbmc_project/inventory", int32_t(0),
5036617338dSEd Tanous         std::array<const char*, 5>{
5046617338dSEd Tanous             "xyz.openbmc_project.Inventory.Decorator.Asset",
5056617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Cpu",
5066617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Dimm",
5076617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.System",
5086617338dSEd Tanous             "xyz.openbmc_project.Common.UUID",
5096617338dSEd Tanous         });
510c5b2abe0SLewanczyk, Dawid }
511c5b2abe0SLewanczyk, Dawid 
512c5b2abe0SLewanczyk, Dawid /**
513c5b2abe0SLewanczyk, Dawid  * @brief Retrieves host state properties over dbus
514c5b2abe0SLewanczyk, Dawid  *
515c5b2abe0SLewanczyk, Dawid  * @param[in] aResp     Shared pointer for completing asynchronous calls.
516c5b2abe0SLewanczyk, Dawid  *
517c5b2abe0SLewanczyk, Dawid  * @return None.
518c5b2abe0SLewanczyk, Dawid  */
5198d1b46d7Szhanghch05 inline void getHostState(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
5201abe55efSEd Tanous {
52155c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get host information.";
5221e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
5231e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
5241e1e598dSJonathan Doman         "/xyz/openbmc_project/state/host0", "xyz.openbmc_project.State.Host",
5251e1e598dSJonathan Doman         "CurrentHostState",
526c5d03ff4SJennifer Lee         [aResp](const boost::system::error_code ec,
5271e1e598dSJonathan Doman                 const std::string& hostState) {
5281abe55efSEd Tanous         if (ec)
5291abe55efSEd Tanous         {
53022228c28SAndrew Geissler             if (ec == boost::system::errc::host_unreachable)
53122228c28SAndrew Geissler             {
53222228c28SAndrew Geissler                 // Service not available, no error, just don't return
53322228c28SAndrew Geissler                 // host state info
53422228c28SAndrew Geissler                 BMCWEB_LOG_DEBUG << "Service not available " << ec;
53522228c28SAndrew Geissler                 return;
53622228c28SAndrew Geissler             }
53722228c28SAndrew Geissler             BMCWEB_LOG_ERROR << "DBUS response error " << ec;
538f12894f8SJason M. Bills             messages::internalError(aResp->res);
539c5b2abe0SLewanczyk, Dawid             return;
540c5b2abe0SLewanczyk, Dawid         }
5416617338dSEd Tanous 
5421e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Host state: " << hostState;
543c5b2abe0SLewanczyk, Dawid         // Verify Host State
5441e1e598dSJonathan Doman         if (hostState == "xyz.openbmc_project.State.Host.HostState.Running")
5451abe55efSEd Tanous         {
54655c7b7a2SEd Tanous             aResp->res.jsonValue["PowerState"] = "On";
5476617338dSEd Tanous             aResp->res.jsonValue["Status"]["State"] = "Enabled";
5481abe55efSEd Tanous         }
5491e1e598dSJonathan Doman         else if (hostState ==
5500fda0f12SGeorge Liu                  "xyz.openbmc_project.State.Host.HostState.Quiesced")
5518c888608SGunnar Mills         {
5528c888608SGunnar Mills             aResp->res.jsonValue["PowerState"] = "On";
5538c888608SGunnar Mills             aResp->res.jsonValue["Status"]["State"] = "Quiesced";
5548c888608SGunnar Mills         }
5551e1e598dSJonathan Doman         else if (hostState ==
5560fda0f12SGeorge Liu                  "xyz.openbmc_project.State.Host.HostState.DiagnosticMode")
55783935af9SAndrew Geissler         {
55883935af9SAndrew Geissler             aResp->res.jsonValue["PowerState"] = "On";
55983935af9SAndrew Geissler             aResp->res.jsonValue["Status"]["State"] = "InTest";
56083935af9SAndrew Geissler         }
5610fda0f12SGeorge Liu         else if (
5621e1e598dSJonathan Doman             hostState ==
5630fda0f12SGeorge Liu             "xyz.openbmc_project.State.Host.HostState.TransitioningToRunning")
5641a2a1437SAndrew Geissler         {
5651a2a1437SAndrew Geissler             aResp->res.jsonValue["PowerState"] = "PoweringOn";
56615c27bf8SNoah Brewer             aResp->res.jsonValue["Status"]["State"] = "Starting";
5671a2a1437SAndrew Geissler         }
568002d39b4SEd Tanous         else if (hostState ==
5690fda0f12SGeorge Liu                  "xyz.openbmc_project.State.Host.HostState.TransitioningToOff")
5701a2a1437SAndrew Geissler         {
5711a2a1437SAndrew Geissler             aResp->res.jsonValue["PowerState"] = "PoweringOff";
5721a2a1437SAndrew Geissler             aResp->res.jsonValue["Status"]["State"] = "Disabled";
5731a2a1437SAndrew Geissler         }
5741abe55efSEd Tanous         else
5751abe55efSEd Tanous         {
57655c7b7a2SEd Tanous             aResp->res.jsonValue["PowerState"] = "Off";
5776617338dSEd Tanous             aResp->res.jsonValue["Status"]["State"] = "Disabled";
578c5b2abe0SLewanczyk, Dawid         }
5791e1e598dSJonathan Doman         });
580c5b2abe0SLewanczyk, Dawid }
581c5b2abe0SLewanczyk, Dawid 
582c5b2abe0SLewanczyk, Dawid /**
583786d0f60SGunnar Mills  * @brief Translates boot source DBUS property value to redfish.
584491d8ee7SSantosh Puranik  *
585491d8ee7SSantosh Puranik  * @param[in] dbusSource    The boot source in DBUS speak.
586491d8ee7SSantosh Puranik  *
587491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source in Redfish terms. If translation
588491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
589491d8ee7SSantosh Puranik  */
59023a21a1cSEd Tanous inline std::string dbusToRfBootSource(const std::string& dbusSource)
591491d8ee7SSantosh Puranik {
592491d8ee7SSantosh Puranik     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
593491d8ee7SSantosh Puranik     {
594491d8ee7SSantosh Puranik         return "None";
595491d8ee7SSantosh Puranik     }
5963174e4dfSEd Tanous     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Disk")
597491d8ee7SSantosh Puranik     {
598491d8ee7SSantosh Puranik         return "Hdd";
599491d8ee7SSantosh Puranik     }
6003174e4dfSEd Tanous     if (dbusSource ==
601a71dc0b7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia")
602491d8ee7SSantosh Puranik     {
603491d8ee7SSantosh Puranik         return "Cd";
604491d8ee7SSantosh Puranik     }
6053174e4dfSEd Tanous     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Network")
606491d8ee7SSantosh Puranik     {
607491d8ee7SSantosh Puranik         return "Pxe";
608491d8ee7SSantosh Puranik     }
6093174e4dfSEd Tanous     if (dbusSource ==
610944ffaf9SJohnathan Mantey         "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia")
6119f16b2c1SJennifer Lee     {
6129f16b2c1SJennifer Lee         return "Usb";
6139f16b2c1SJennifer Lee     }
614491d8ee7SSantosh Puranik     return "";
615491d8ee7SSantosh Puranik }
616491d8ee7SSantosh Puranik 
617491d8ee7SSantosh Puranik /**
618cd9a4666SKonstantin Aladyshev  * @brief Translates boot type DBUS property value to redfish.
619cd9a4666SKonstantin Aladyshev  *
620cd9a4666SKonstantin Aladyshev  * @param[in] dbusType    The boot type in DBUS speak.
621cd9a4666SKonstantin Aladyshev  *
622cd9a4666SKonstantin Aladyshev  * @return Returns as a string, the boot type in Redfish terms. If translation
623cd9a4666SKonstantin Aladyshev  * cannot be done, returns an empty string.
624cd9a4666SKonstantin Aladyshev  */
625cd9a4666SKonstantin Aladyshev inline std::string dbusToRfBootType(const std::string& dbusType)
626cd9a4666SKonstantin Aladyshev {
627cd9a4666SKonstantin Aladyshev     if (dbusType == "xyz.openbmc_project.Control.Boot.Type.Types.Legacy")
628cd9a4666SKonstantin Aladyshev     {
629cd9a4666SKonstantin Aladyshev         return "Legacy";
630cd9a4666SKonstantin Aladyshev     }
631cd9a4666SKonstantin Aladyshev     if (dbusType == "xyz.openbmc_project.Control.Boot.Type.Types.EFI")
632cd9a4666SKonstantin Aladyshev     {
633cd9a4666SKonstantin Aladyshev         return "UEFI";
634cd9a4666SKonstantin Aladyshev     }
635cd9a4666SKonstantin Aladyshev     return "";
636cd9a4666SKonstantin Aladyshev }
637cd9a4666SKonstantin Aladyshev 
638cd9a4666SKonstantin Aladyshev /**
639786d0f60SGunnar Mills  * @brief Translates boot mode DBUS property value to redfish.
640491d8ee7SSantosh Puranik  *
641491d8ee7SSantosh Puranik  * @param[in] dbusMode    The boot mode in DBUS speak.
642491d8ee7SSantosh Puranik  *
643491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode in Redfish terms. If translation
644491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
645491d8ee7SSantosh Puranik  */
64623a21a1cSEd Tanous inline std::string dbusToRfBootMode(const std::string& dbusMode)
647491d8ee7SSantosh Puranik {
648491d8ee7SSantosh Puranik     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
649491d8ee7SSantosh Puranik     {
650491d8ee7SSantosh Puranik         return "None";
651491d8ee7SSantosh Puranik     }
6523174e4dfSEd Tanous     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe")
653491d8ee7SSantosh Puranik     {
654491d8ee7SSantosh Puranik         return "Diags";
655491d8ee7SSantosh Puranik     }
6563174e4dfSEd Tanous     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup")
657491d8ee7SSantosh Puranik     {
658491d8ee7SSantosh Puranik         return "BiosSetup";
659491d8ee7SSantosh Puranik     }
660491d8ee7SSantosh Puranik     return "";
661491d8ee7SSantosh Puranik }
662491d8ee7SSantosh Puranik 
663491d8ee7SSantosh Puranik /**
664e43914b3SAndrew Geissler  * @brief Translates boot progress DBUS property value to redfish.
665e43914b3SAndrew Geissler  *
666e43914b3SAndrew Geissler  * @param[in] dbusBootProgress    The boot progress in DBUS speak.
667e43914b3SAndrew Geissler  *
668e43914b3SAndrew Geissler  * @return Returns as a string, the boot progress in Redfish terms. If
669e43914b3SAndrew Geissler  *         translation cannot be done, returns "None".
670e43914b3SAndrew Geissler  */
671e43914b3SAndrew Geissler inline std::string dbusToRfBootProgress(const std::string& dbusBootProgress)
672e43914b3SAndrew Geissler {
673e43914b3SAndrew Geissler     // Now convert the D-Bus BootProgress to the appropriate Redfish
674e43914b3SAndrew Geissler     // enum
675e43914b3SAndrew Geissler     std::string rfBpLastState = "None";
676e43914b3SAndrew Geissler     if (dbusBootProgress == "xyz.openbmc_project.State.Boot.Progress."
677e43914b3SAndrew Geissler                             "ProgressStages.Unspecified")
678e43914b3SAndrew Geissler     {
679e43914b3SAndrew Geissler         rfBpLastState = "None";
680e43914b3SAndrew Geissler     }
681e43914b3SAndrew Geissler     else if (dbusBootProgress ==
682e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
683e43914b3SAndrew Geissler              "PrimaryProcInit")
684e43914b3SAndrew Geissler     {
685e43914b3SAndrew Geissler         rfBpLastState = "PrimaryProcessorInitializationStarted";
686e43914b3SAndrew Geissler     }
687e43914b3SAndrew Geissler     else if (dbusBootProgress ==
688e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
689e43914b3SAndrew Geissler              "BusInit")
690e43914b3SAndrew Geissler     {
691e43914b3SAndrew Geissler         rfBpLastState = "BusInitializationStarted";
692e43914b3SAndrew Geissler     }
693e43914b3SAndrew Geissler     else if (dbusBootProgress ==
694e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
695e43914b3SAndrew Geissler              "MemoryInit")
696e43914b3SAndrew Geissler     {
697e43914b3SAndrew Geissler         rfBpLastState = "MemoryInitializationStarted";
698e43914b3SAndrew Geissler     }
699e43914b3SAndrew Geissler     else if (dbusBootProgress ==
700e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
701e43914b3SAndrew Geissler              "SecondaryProcInit")
702e43914b3SAndrew Geissler     {
703e43914b3SAndrew Geissler         rfBpLastState = "SecondaryProcessorInitializationStarted";
704e43914b3SAndrew Geissler     }
705e43914b3SAndrew Geissler     else if (dbusBootProgress ==
706e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
707e43914b3SAndrew Geissler              "PCIInit")
708e43914b3SAndrew Geissler     {
709e43914b3SAndrew Geissler         rfBpLastState = "PCIResourceConfigStarted";
710e43914b3SAndrew Geissler     }
711e43914b3SAndrew Geissler     else if (dbusBootProgress ==
712e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
713e43914b3SAndrew Geissler              "SystemSetup")
714e43914b3SAndrew Geissler     {
715e43914b3SAndrew Geissler         rfBpLastState = "SetupEntered";
716e43914b3SAndrew Geissler     }
717e43914b3SAndrew Geissler     else if (dbusBootProgress ==
718e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
719e43914b3SAndrew Geissler              "SystemInitComplete")
720e43914b3SAndrew Geissler     {
721e43914b3SAndrew Geissler         rfBpLastState = "SystemHardwareInitializationComplete";
722e43914b3SAndrew Geissler     }
723e43914b3SAndrew Geissler     else if (dbusBootProgress ==
724e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
725e43914b3SAndrew Geissler              "OSStart")
726e43914b3SAndrew Geissler     {
727e43914b3SAndrew Geissler         rfBpLastState = "OSBootStarted";
728e43914b3SAndrew Geissler     }
729e43914b3SAndrew Geissler     else if (dbusBootProgress ==
730e43914b3SAndrew Geissler              "xyz.openbmc_project.State.Boot.Progress.ProgressStages."
731e43914b3SAndrew Geissler              "OSRunning")
732e43914b3SAndrew Geissler     {
733e43914b3SAndrew Geissler         rfBpLastState = "OSRunning";
734e43914b3SAndrew Geissler     }
735e43914b3SAndrew Geissler     else
736e43914b3SAndrew Geissler     {
737e43914b3SAndrew Geissler         BMCWEB_LOG_DEBUG << "Unsupported D-Bus BootProgress "
738e43914b3SAndrew Geissler                          << dbusBootProgress;
739e43914b3SAndrew Geissler         // Just return the default
740e43914b3SAndrew Geissler     }
741e43914b3SAndrew Geissler     return rfBpLastState;
742e43914b3SAndrew Geissler }
743e43914b3SAndrew Geissler 
744e43914b3SAndrew Geissler /**
745786d0f60SGunnar Mills  * @brief Translates boot source from Redfish to the DBus boot paths.
746491d8ee7SSantosh Puranik  *
747491d8ee7SSantosh Puranik  * @param[in] rfSource    The boot source in Redfish.
748944ffaf9SJohnathan Mantey  * @param[out] bootSource The DBus source
749944ffaf9SJohnathan Mantey  * @param[out] bootMode   the DBus boot mode
750491d8ee7SSantosh Puranik  *
751944ffaf9SJohnathan Mantey  * @return Integer error code.
752491d8ee7SSantosh Puranik  */
7538d1b46d7Szhanghch05 inline int assignBootParameters(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
754944ffaf9SJohnathan Mantey                                 const std::string& rfSource,
755944ffaf9SJohnathan Mantey                                 std::string& bootSource, std::string& bootMode)
756491d8ee7SSantosh Puranik {
757c21865c4SKonstantin Aladyshev     bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
758c21865c4SKonstantin Aladyshev     bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
759944ffaf9SJohnathan Mantey 
760491d8ee7SSantosh Puranik     if (rfSource == "None")
761491d8ee7SSantosh Puranik     {
762944ffaf9SJohnathan Mantey         return 0;
763491d8ee7SSantosh Puranik     }
7643174e4dfSEd Tanous     if (rfSource == "Pxe")
765491d8ee7SSantosh Puranik     {
766944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Network";
767944ffaf9SJohnathan Mantey     }
768944ffaf9SJohnathan Mantey     else if (rfSource == "Hdd")
769944ffaf9SJohnathan Mantey     {
770944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Disk";
771944ffaf9SJohnathan Mantey     }
772944ffaf9SJohnathan Mantey     else if (rfSource == "Diags")
773944ffaf9SJohnathan Mantey     {
774944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe";
775944ffaf9SJohnathan Mantey     }
776944ffaf9SJohnathan Mantey     else if (rfSource == "Cd")
777944ffaf9SJohnathan Mantey     {
778944ffaf9SJohnathan Mantey         bootSource =
779944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia";
780944ffaf9SJohnathan Mantey     }
781944ffaf9SJohnathan Mantey     else if (rfSource == "BiosSetup")
782944ffaf9SJohnathan Mantey     {
783944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup";
784491d8ee7SSantosh Puranik     }
7859f16b2c1SJennifer Lee     else if (rfSource == "Usb")
7869f16b2c1SJennifer Lee     {
787944ffaf9SJohnathan Mantey         bootSource =
788944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia";
7899f16b2c1SJennifer Lee     }
790491d8ee7SSantosh Puranik     else
791491d8ee7SSantosh Puranik     {
7920fda0f12SGeorge Liu         BMCWEB_LOG_DEBUG
7930fda0f12SGeorge Liu             << "Invalid property value for BootSourceOverrideTarget: "
794944ffaf9SJohnathan Mantey             << bootSource;
795944ffaf9SJohnathan Mantey         messages::propertyValueNotInList(aResp->res, rfSource,
796944ffaf9SJohnathan Mantey                                          "BootSourceTargetOverride");
797944ffaf9SJohnathan Mantey         return -1;
798491d8ee7SSantosh Puranik     }
799944ffaf9SJohnathan Mantey     return 0;
800491d8ee7SSantosh Puranik }
8011981771bSAli Ahmed 
802978b8803SAndrew Geissler /**
803978b8803SAndrew Geissler  * @brief Retrieves boot progress of the system
804978b8803SAndrew Geissler  *
805978b8803SAndrew Geissler  * @param[in] aResp  Shared pointer for generating response message.
806978b8803SAndrew Geissler  *
807978b8803SAndrew Geissler  * @return None.
808978b8803SAndrew Geissler  */
8098d1b46d7Szhanghch05 inline void getBootProgress(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
810978b8803SAndrew Geissler {
8111e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
8121e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
8131e1e598dSJonathan Doman         "/xyz/openbmc_project/state/host0",
8141e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.Progress", "BootProgress",
815978b8803SAndrew Geissler         [aResp](const boost::system::error_code ec,
8161e1e598dSJonathan Doman                 const std::string& bootProgressStr) {
817978b8803SAndrew Geissler         if (ec)
818978b8803SAndrew Geissler         {
819978b8803SAndrew Geissler             // BootProgress is an optional object so just do nothing if
820978b8803SAndrew Geissler             // not found
821978b8803SAndrew Geissler             return;
822978b8803SAndrew Geissler         }
823978b8803SAndrew Geissler 
8241e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Boot Progress: " << bootProgressStr;
825978b8803SAndrew Geissler 
826e43914b3SAndrew Geissler         aResp->res.jsonValue["BootProgress"]["LastState"] =
827e43914b3SAndrew Geissler             dbusToRfBootProgress(bootProgressStr);
8281e1e598dSJonathan Doman         });
829978b8803SAndrew Geissler }
830491d8ee7SSantosh Puranik 
831491d8ee7SSantosh Puranik /**
832c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override type over DBUS and fills out the response
833cd9a4666SKonstantin Aladyshev  *
834cd9a4666SKonstantin Aladyshev  * @param[in] aResp         Shared pointer for generating response message.
835cd9a4666SKonstantin Aladyshev  *
836cd9a4666SKonstantin Aladyshev  * @return None.
837cd9a4666SKonstantin Aladyshev  */
838cd9a4666SKonstantin Aladyshev 
839c21865c4SKonstantin Aladyshev inline void getBootOverrideType(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
840cd9a4666SKonstantin Aladyshev {
8411e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
8421e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
8431e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
8441e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Type", "BootType",
845cd9a4666SKonstantin Aladyshev         [aResp](const boost::system::error_code ec,
8461e1e598dSJonathan Doman                 const std::string& bootType) {
847cd9a4666SKonstantin Aladyshev         if (ec)
848cd9a4666SKonstantin Aladyshev         {
849cd9a4666SKonstantin Aladyshev             // not an error, don't have to have the interface
850cd9a4666SKonstantin Aladyshev             return;
851cd9a4666SKonstantin Aladyshev         }
852cd9a4666SKonstantin Aladyshev 
8531e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Boot type: " << bootType;
854cd9a4666SKonstantin Aladyshev 
855002d39b4SEd Tanous         aResp->res.jsonValue["Boot"]
856002d39b4SEd Tanous                             ["BootSourceOverrideMode@Redfish.AllowableValues"] =
857002d39b4SEd Tanous             {"Legacy", "UEFI"};
858cd9a4666SKonstantin Aladyshev 
8591e1e598dSJonathan Doman         auto rfType = dbusToRfBootType(bootType);
860cd9a4666SKonstantin Aladyshev         if (rfType.empty())
861cd9a4666SKonstantin Aladyshev         {
862cd9a4666SKonstantin Aladyshev             messages::internalError(aResp->res);
863cd9a4666SKonstantin Aladyshev             return;
864cd9a4666SKonstantin Aladyshev         }
865cd9a4666SKonstantin Aladyshev 
866cd9a4666SKonstantin Aladyshev         aResp->res.jsonValue["Boot"]["BootSourceOverrideMode"] = rfType;
8671e1e598dSJonathan Doman         });
868cd9a4666SKonstantin Aladyshev }
869cd9a4666SKonstantin Aladyshev 
870cd9a4666SKonstantin Aladyshev /**
871c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override mode over DBUS and fills out the response
872491d8ee7SSantosh Puranik  *
873491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
874491d8ee7SSantosh Puranik  *
875491d8ee7SSantosh Puranik  * @return None.
876491d8ee7SSantosh Puranik  */
877c21865c4SKonstantin Aladyshev 
878c21865c4SKonstantin Aladyshev inline void getBootOverrideMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
879491d8ee7SSantosh Puranik {
8801e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
8811e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
8821e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
8831e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
884c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec,
8851e1e598dSJonathan Doman                 const std::string& bootModeStr) {
886491d8ee7SSantosh Puranik         if (ec)
887491d8ee7SSantosh Puranik         {
888491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
889491d8ee7SSantosh Puranik             messages::internalError(aResp->res);
890491d8ee7SSantosh Puranik             return;
891491d8ee7SSantosh Puranik         }
892491d8ee7SSantosh Puranik 
8931e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Boot mode: " << bootModeStr;
894491d8ee7SSantosh Puranik 
8950fda0f12SGeorge Liu         aResp->res
8960fda0f12SGeorge Liu             .jsonValue["Boot"]
897002d39b4SEd Tanous                       ["BootSourceOverrideTarget@Redfish.AllowableValues"] = {
898002d39b4SEd Tanous             "None", "Pxe", "Hdd", "Cd", "Diags", "BiosSetup", "Usb"};
899491d8ee7SSantosh Puranik 
9001e1e598dSJonathan Doman         if (bootModeStr !=
901491d8ee7SSantosh Puranik             "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
902491d8ee7SSantosh Puranik         {
9031e1e598dSJonathan Doman             auto rfMode = dbusToRfBootMode(bootModeStr);
904491d8ee7SSantosh Puranik             if (!rfMode.empty())
905491d8ee7SSantosh Puranik             {
906491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
907491d8ee7SSantosh Puranik                     rfMode;
908491d8ee7SSantosh Puranik             }
909491d8ee7SSantosh Puranik         }
9101e1e598dSJonathan Doman         });
911491d8ee7SSantosh Puranik }
912491d8ee7SSantosh Puranik 
913491d8ee7SSantosh Puranik /**
914c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override source over DBUS
915491d8ee7SSantosh Puranik  *
916491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
917491d8ee7SSantosh Puranik  *
918491d8ee7SSantosh Puranik  * @return None.
919491d8ee7SSantosh Puranik  */
920c21865c4SKonstantin Aladyshev 
921c21865c4SKonstantin Aladyshev inline void
922c21865c4SKonstantin Aladyshev     getBootOverrideSource(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
923491d8ee7SSantosh Puranik {
9241e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
9251e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
9261e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
9271e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.Source", "BootSource",
928c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec,
9291e1e598dSJonathan Doman                 const std::string& bootSourceStr) {
930491d8ee7SSantosh Puranik         if (ec)
931491d8ee7SSantosh Puranik         {
932491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
933491d8ee7SSantosh Puranik             messages::internalError(aResp->res);
934491d8ee7SSantosh Puranik             return;
935491d8ee7SSantosh Puranik         }
936491d8ee7SSantosh Puranik 
9371e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Boot source: " << bootSourceStr;
938491d8ee7SSantosh Puranik 
9391e1e598dSJonathan Doman         auto rfSource = dbusToRfBootSource(bootSourceStr);
940491d8ee7SSantosh Puranik         if (!rfSource.empty())
941491d8ee7SSantosh Puranik         {
942002d39b4SEd Tanous             aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] = rfSource;
943491d8ee7SSantosh Puranik         }
944cd9a4666SKonstantin Aladyshev 
945cd9a4666SKonstantin Aladyshev         // Get BootMode as BootSourceOverrideTarget is constructed
946cd9a4666SKonstantin Aladyshev         // from both BootSource and BootMode
947c21865c4SKonstantin Aladyshev         getBootOverrideMode(aResp);
9481e1e598dSJonathan Doman         });
949491d8ee7SSantosh Puranik }
950491d8ee7SSantosh Puranik 
951491d8ee7SSantosh Puranik /**
952c21865c4SKonstantin Aladyshev  * @brief This functions abstracts all the logic behind getting a
953c21865c4SKonstantin Aladyshev  * "BootSourceOverrideEnabled" property from an overall boot override enable
954c21865c4SKonstantin Aladyshev  * state
955491d8ee7SSantosh Puranik  *
956491d8ee7SSantosh Puranik  * @param[in] aResp     Shared pointer for generating response message.
957491d8ee7SSantosh Puranik  *
958491d8ee7SSantosh Puranik  * @return None.
959491d8ee7SSantosh Puranik  */
960491d8ee7SSantosh Puranik 
961c21865c4SKonstantin Aladyshev inline void
962c21865c4SKonstantin Aladyshev     processBootOverrideEnable(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
963c21865c4SKonstantin Aladyshev                               const bool bootOverrideEnableSetting)
964c21865c4SKonstantin Aladyshev {
965c21865c4SKonstantin Aladyshev     if (!bootOverrideEnableSetting)
966c21865c4SKonstantin Aladyshev     {
967c21865c4SKonstantin Aladyshev         aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] = "Disabled";
968c21865c4SKonstantin Aladyshev         return;
969c21865c4SKonstantin Aladyshev     }
970c21865c4SKonstantin Aladyshev 
971c21865c4SKonstantin Aladyshev     // If boot source override is enabled, we need to check 'one_time'
972c21865c4SKonstantin Aladyshev     // property to set a correct value for the "BootSourceOverrideEnabled"
9731e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
9741e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
9751e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot/one_time",
9761e1e598dSJonathan Doman         "xyz.openbmc_project.Object.Enable", "Enabled",
9771e1e598dSJonathan Doman         [aResp](const boost::system::error_code ec, bool oneTimeSetting) {
978491d8ee7SSantosh Puranik         if (ec)
979491d8ee7SSantosh Puranik         {
980491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
981c21865c4SKonstantin Aladyshev             messages::internalError(aResp->res);
982491d8ee7SSantosh Puranik             return;
983491d8ee7SSantosh Puranik         }
984491d8ee7SSantosh Puranik 
985c21865c4SKonstantin Aladyshev         if (oneTimeSetting)
986c21865c4SKonstantin Aladyshev         {
987002d39b4SEd Tanous             aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] = "Once";
988c21865c4SKonstantin Aladyshev         }
989c21865c4SKonstantin Aladyshev         else
990c21865c4SKonstantin Aladyshev         {
991c21865c4SKonstantin Aladyshev             aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
992c21865c4SKonstantin Aladyshev                 "Continuous";
993c21865c4SKonstantin Aladyshev         }
9941e1e598dSJonathan Doman         });
995491d8ee7SSantosh Puranik }
996491d8ee7SSantosh Puranik 
997491d8ee7SSantosh Puranik /**
998c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override enable over DBUS
999c21865c4SKonstantin Aladyshev  *
1000c21865c4SKonstantin Aladyshev  * @param[in] aResp     Shared pointer for generating response message.
1001c21865c4SKonstantin Aladyshev  *
1002c21865c4SKonstantin Aladyshev  * @return None.
1003c21865c4SKonstantin Aladyshev  */
1004c21865c4SKonstantin Aladyshev 
1005c21865c4SKonstantin Aladyshev inline void
1006c21865c4SKonstantin Aladyshev     getBootOverrideEnable(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1007c21865c4SKonstantin Aladyshev {
10081e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
10091e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
10101e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
10111e1e598dSJonathan Doman         "xyz.openbmc_project.Object.Enable", "Enabled",
1012c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec,
10131e1e598dSJonathan Doman                 const bool bootOverrideEnable) {
1014c21865c4SKonstantin Aladyshev         if (ec)
1015c21865c4SKonstantin Aladyshev         {
1016c21865c4SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1017c21865c4SKonstantin Aladyshev             messages::internalError(aResp->res);
1018c21865c4SKonstantin Aladyshev             return;
1019c21865c4SKonstantin Aladyshev         }
1020c21865c4SKonstantin Aladyshev 
10211e1e598dSJonathan Doman         processBootOverrideEnable(aResp, bootOverrideEnable);
10221e1e598dSJonathan Doman         });
1023c21865c4SKonstantin Aladyshev }
1024c21865c4SKonstantin Aladyshev 
1025c21865c4SKonstantin Aladyshev /**
1026c21865c4SKonstantin Aladyshev  * @brief Retrieves boot source override properties
1027c21865c4SKonstantin Aladyshev  *
1028c21865c4SKonstantin Aladyshev  * @param[in] aResp     Shared pointer for generating response message.
1029c21865c4SKonstantin Aladyshev  *
1030c21865c4SKonstantin Aladyshev  * @return None.
1031c21865c4SKonstantin Aladyshev  */
1032c21865c4SKonstantin Aladyshev inline void getBootProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1033c21865c4SKonstantin Aladyshev {
1034c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Get boot information.";
1035c21865c4SKonstantin Aladyshev 
1036c21865c4SKonstantin Aladyshev     getBootOverrideSource(aResp);
1037c21865c4SKonstantin Aladyshev     getBootOverrideType(aResp);
1038c21865c4SKonstantin Aladyshev     getBootOverrideEnable(aResp);
1039c21865c4SKonstantin Aladyshev }
1040c21865c4SKonstantin Aladyshev 
1041c21865c4SKonstantin Aladyshev /**
1042c0557e1aSGunnar Mills  * @brief Retrieves the Last Reset Time
1043c0557e1aSGunnar Mills  *
1044c0557e1aSGunnar Mills  * "Reset" is an overloaded term in Redfish, "Reset" includes power on
1045c0557e1aSGunnar Mills  * and power off. Even though this is the "system" Redfish object look at the
1046c0557e1aSGunnar Mills  * chassis D-Bus interface for the LastStateChangeTime since this has the
1047c0557e1aSGunnar Mills  * last power operation time.
1048c0557e1aSGunnar Mills  *
1049c0557e1aSGunnar Mills  * @param[in] aResp     Shared pointer for generating response message.
1050c0557e1aSGunnar Mills  *
1051c0557e1aSGunnar Mills  * @return None.
1052c0557e1aSGunnar Mills  */
10538d1b46d7Szhanghch05 inline void getLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1054c0557e1aSGunnar Mills {
1055c0557e1aSGunnar Mills     BMCWEB_LOG_DEBUG << "Getting System Last Reset Time";
1056c0557e1aSGunnar Mills 
10571e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
10581e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Chassis",
10591e1e598dSJonathan Doman         "/xyz/openbmc_project/state/chassis0",
10601e1e598dSJonathan Doman         "xyz.openbmc_project.State.Chassis", "LastStateChangeTime",
10611e1e598dSJonathan Doman         [aResp](const boost::system::error_code ec, uint64_t lastResetTime) {
1062c0557e1aSGunnar Mills         if (ec)
1063c0557e1aSGunnar Mills         {
1064c0557e1aSGunnar Mills             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
1065c0557e1aSGunnar Mills             return;
1066c0557e1aSGunnar Mills         }
1067c0557e1aSGunnar Mills 
1068c0557e1aSGunnar Mills         // LastStateChangeTime is epoch time, in milliseconds
1069c0557e1aSGunnar Mills         // https://github.com/openbmc/phosphor-dbus-interfaces/blob/33e8e1dd64da53a66e888d33dc82001305cd0bf9/xyz/openbmc_project/State/Chassis.interface.yaml#L19
10701e1e598dSJonathan Doman         uint64_t lastResetTimeStamp = lastResetTime / 1000;
1071c0557e1aSGunnar Mills 
1072c0557e1aSGunnar Mills         // Convert to ISO 8601 standard
1073c0557e1aSGunnar Mills         aResp->res.jsonValue["LastResetTime"] =
10741d8782e7SNan Zhou             crow::utility::getDateTimeUint(lastResetTimeStamp);
10751e1e598dSJonathan Doman         });
1076c0557e1aSGunnar Mills }
1077c0557e1aSGunnar Mills 
1078c0557e1aSGunnar Mills /**
10796bd5a8d2SGunnar Mills  * @brief Retrieves Automatic Retry properties. Known on D-Bus as AutoReboot.
10806bd5a8d2SGunnar Mills  *
10816bd5a8d2SGunnar Mills  * @param[in] aResp     Shared pointer for generating response message.
10826bd5a8d2SGunnar Mills  *
10836bd5a8d2SGunnar Mills  * @return None.
10846bd5a8d2SGunnar Mills  */
10858d1b46d7Szhanghch05 inline void getAutomaticRetry(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
10866bd5a8d2SGunnar Mills {
10876bd5a8d2SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Automatic Retry policy";
10886bd5a8d2SGunnar Mills 
10891e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
10901e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
10911e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/auto_reboot",
10921e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
10931e1e598dSJonathan Doman         [aResp](const boost::system::error_code ec, bool autoRebootEnabled) {
10946bd5a8d2SGunnar Mills         if (ec)
10956bd5a8d2SGunnar Mills         {
10966bd5a8d2SGunnar Mills             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
10976bd5a8d2SGunnar Mills             return;
10986bd5a8d2SGunnar Mills         }
10996bd5a8d2SGunnar Mills 
11001e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Auto Reboot: " << autoRebootEnabled;
1101e05aec50SEd Tanous         if (autoRebootEnabled)
11026bd5a8d2SGunnar Mills         {
11036bd5a8d2SGunnar Mills             aResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
11046bd5a8d2SGunnar Mills                 "RetryAttempts";
11056bd5a8d2SGunnar Mills             // If AutomaticRetry (AutoReboot) is enabled see how many
11066bd5a8d2SGunnar Mills             // attempts are left
11071e1e598dSJonathan Doman             sdbusplus::asio::getProperty<uint32_t>(
1108002d39b4SEd Tanous                 *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
11091e1e598dSJonathan Doman                 "/xyz/openbmc_project/state/host0",
11101e1e598dSJonathan Doman                 "xyz.openbmc_project.Control.Boot.RebootAttempts",
11111e1e598dSJonathan Doman                 "AttemptsLeft",
1112cb13a392SEd Tanous                 [aResp](const boost::system::error_code ec2,
1113914e2d5dSEd Tanous                         const uint32_t autoRebootAttemptsLeft) {
1114cb13a392SEd Tanous                 if (ec2)
11156bd5a8d2SGunnar Mills                 {
1116cb13a392SEd Tanous                     BMCWEB_LOG_DEBUG << "D-BUS response error " << ec2;
11176bd5a8d2SGunnar Mills                     return;
11186bd5a8d2SGunnar Mills                 }
11196bd5a8d2SGunnar Mills 
11206bd5a8d2SGunnar Mills                 BMCWEB_LOG_DEBUG << "Auto Reboot Attempts Left: "
11211e1e598dSJonathan Doman                                  << autoRebootAttemptsLeft;
11226bd5a8d2SGunnar Mills 
11236bd5a8d2SGunnar Mills                 aResp->res
1124002d39b4SEd Tanous                     .jsonValue["Boot"]["RemainingAutomaticRetryAttempts"] =
11251e1e598dSJonathan Doman                     autoRebootAttemptsLeft;
11261e1e598dSJonathan Doman                 });
11276bd5a8d2SGunnar Mills         }
11286bd5a8d2SGunnar Mills         else
11296bd5a8d2SGunnar Mills         {
1130002d39b4SEd Tanous             aResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] = "Disabled";
11316bd5a8d2SGunnar Mills         }
11326bd5a8d2SGunnar Mills 
11336bd5a8d2SGunnar Mills         // Not on D-Bus. Hardcoded here:
11346bd5a8d2SGunnar Mills         // https://github.com/openbmc/phosphor-state-manager/blob/1dbbef42675e94fb1f78edb87d6b11380260535a/meson_options.txt#L71
11356bd5a8d2SGunnar Mills         aResp->res.jsonValue["Boot"]["AutomaticRetryAttempts"] = 3;
113669f35306SGunnar Mills 
113769f35306SGunnar Mills         // "AutomaticRetryConfig" can be 3 values, Disabled, RetryAlways,
113869f35306SGunnar Mills         // and RetryAttempts. OpenBMC only supports Disabled and
113969f35306SGunnar Mills         // RetryAttempts.
1140002d39b4SEd Tanous         aResp->res.jsonValue["Boot"]
11410fda0f12SGeorge Liu                             ["AutomaticRetryConfig@Redfish.AllowableValues"] = {
11420fda0f12SGeorge Liu             "Disabled", "RetryAttempts"};
11431e1e598dSJonathan Doman         });
11446bd5a8d2SGunnar Mills }
11456bd5a8d2SGunnar Mills 
11466bd5a8d2SGunnar Mills /**
1147c6a620f2SGeorge Liu  * @brief Retrieves power restore policy over DBUS.
1148c6a620f2SGeorge Liu  *
1149c6a620f2SGeorge Liu  * @param[in] aResp     Shared pointer for generating response message.
1150c6a620f2SGeorge Liu  *
1151c6a620f2SGeorge Liu  * @return None.
1152c6a620f2SGeorge Liu  */
11538d1b46d7Szhanghch05 inline void
11548d1b46d7Szhanghch05     getPowerRestorePolicy(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1155c6a620f2SGeorge Liu {
1156c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Get power restore policy";
1157c6a620f2SGeorge Liu 
11581e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
11591e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
11601e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/power_restore_policy",
11611e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
11621e1e598dSJonathan Doman         [aResp](const boost::system::error_code ec, const std::string& policy) {
1163c6a620f2SGeorge Liu         if (ec)
1164c6a620f2SGeorge Liu         {
1165c6a620f2SGeorge Liu             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1166c6a620f2SGeorge Liu             return;
1167c6a620f2SGeorge Liu         }
1168c6a620f2SGeorge Liu 
11690fda0f12SGeorge Liu         const boost::container::flat_map<std::string, std::string> policyMaps = {
11700fda0f12SGeorge Liu             {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn",
1171c6a620f2SGeorge Liu              "AlwaysOn"},
11720fda0f12SGeorge Liu             {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff",
1173c6a620f2SGeorge Liu              "AlwaysOff"},
11740fda0f12SGeorge Liu             {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore",
11754ed47cb8SMatthew Barth              "LastState"},
11764ed47cb8SMatthew Barth             // Return `AlwaysOff` when power restore policy set to "None"
11774ed47cb8SMatthew Barth             {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.None",
11784ed47cb8SMatthew Barth              "AlwaysOff"}};
1179c6a620f2SGeorge Liu 
11801e1e598dSJonathan Doman         auto policyMapsIt = policyMaps.find(policy);
1181c6a620f2SGeorge Liu         if (policyMapsIt == policyMaps.end())
1182c6a620f2SGeorge Liu         {
1183c6a620f2SGeorge Liu             messages::internalError(aResp->res);
1184c6a620f2SGeorge Liu             return;
1185c6a620f2SGeorge Liu         }
1186c6a620f2SGeorge Liu 
1187c6a620f2SGeorge Liu         aResp->res.jsonValue["PowerRestorePolicy"] = policyMapsIt->second;
11881e1e598dSJonathan Doman         });
1189c6a620f2SGeorge Liu }
1190c6a620f2SGeorge Liu 
1191c6a620f2SGeorge Liu /**
11921981771bSAli Ahmed  * @brief Get TrustedModuleRequiredToBoot property. Determines whether or not
11931981771bSAli Ahmed  * TPM is required for booting the host.
11941981771bSAli Ahmed  *
11951981771bSAli Ahmed  * @param[in] aResp     Shared pointer for generating response message.
11961981771bSAli Ahmed  *
11971981771bSAli Ahmed  * @return None.
11981981771bSAli Ahmed  */
11991981771bSAli Ahmed inline void getTrustedModuleRequiredToBoot(
12001981771bSAli Ahmed     const std::shared_ptr<bmcweb::AsyncResp>& aResp)
12011981771bSAli Ahmed {
12021981771bSAli Ahmed     BMCWEB_LOG_DEBUG << "Get TPM required to boot.";
12031981771bSAli Ahmed 
12041981771bSAli Ahmed     crow::connections::systemBus->async_method_call(
1205b9d36b47SEd Tanous         [aResp](const boost::system::error_code ec,
1206b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
12071981771bSAli Ahmed         if (ec)
12081981771bSAli Ahmed         {
1209002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on TPM.Policy GetSubTree"
1210002d39b4SEd Tanous                              << ec;
12111981771bSAli Ahmed             // This is an optional D-Bus object so just return if
12121981771bSAli Ahmed             // error occurs
12131981771bSAli Ahmed             return;
12141981771bSAli Ahmed         }
121526f6976fSEd Tanous         if (subtree.empty())
12161981771bSAli Ahmed         {
12171981771bSAli Ahmed             // As noted above, this is an optional interface so just return
12181981771bSAli Ahmed             // if there is no instance found
12191981771bSAli Ahmed             return;
12201981771bSAli Ahmed         }
12211981771bSAli Ahmed 
12221981771bSAli Ahmed         /* When there is more than one TPMEnable object... */
12231981771bSAli Ahmed         if (subtree.size() > 1)
12241981771bSAli Ahmed         {
12251981771bSAli Ahmed             BMCWEB_LOG_DEBUG
12261981771bSAli Ahmed                 << "DBUS response has more than 1 TPM Enable object:"
12271981771bSAli Ahmed                 << subtree.size();
12281981771bSAli Ahmed             // Throw an internal Error and return
12291981771bSAli Ahmed             messages::internalError(aResp->res);
12301981771bSAli Ahmed             return;
12311981771bSAli Ahmed         }
12321981771bSAli Ahmed 
12331981771bSAli Ahmed         // Make sure the Dbus response map has a service and objectPath
12341981771bSAli Ahmed         // field
12351981771bSAli Ahmed         if (subtree[0].first.empty() || subtree[0].second.size() != 1)
12361981771bSAli Ahmed         {
12371981771bSAli Ahmed             BMCWEB_LOG_DEBUG << "TPM.Policy mapper error!";
12381981771bSAli Ahmed             messages::internalError(aResp->res);
12391981771bSAli Ahmed             return;
12401981771bSAli Ahmed         }
12411981771bSAli Ahmed 
12421981771bSAli Ahmed         const std::string& path = subtree[0].first;
12431981771bSAli Ahmed         const std::string& serv = subtree[0].second.begin()->first;
12441981771bSAli Ahmed 
12451981771bSAli Ahmed         // Valid TPM Enable object found, now reading the current value
12461e1e598dSJonathan Doman         sdbusplus::asio::getProperty<bool>(
12471e1e598dSJonathan Doman             *crow::connections::systemBus, serv, path,
12481e1e598dSJonathan Doman             "xyz.openbmc_project.Control.TPM.Policy", "TPMEnable",
12491e1e598dSJonathan Doman             [aResp](const boost::system::error_code ec, bool tpmRequired) {
12501981771bSAli Ahmed             if (ec)
12511981771bSAli Ahmed             {
1252002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "D-BUS response error on TPM.Policy Get"
1253002d39b4SEd Tanous                                  << ec;
12541981771bSAli Ahmed                 messages::internalError(aResp->res);
12551981771bSAli Ahmed                 return;
12561981771bSAli Ahmed             }
12571981771bSAli Ahmed 
12581e1e598dSJonathan Doman             if (tpmRequired)
12591981771bSAli Ahmed             {
1260002d39b4SEd Tanous                 aResp->res.jsonValue["Boot"]["TrustedModuleRequiredToBoot"] =
12611981771bSAli Ahmed                     "Required";
12621981771bSAli Ahmed             }
12631981771bSAli Ahmed             else
12641981771bSAli Ahmed             {
1265002d39b4SEd Tanous                 aResp->res.jsonValue["Boot"]["TrustedModuleRequiredToBoot"] =
12661981771bSAli Ahmed                     "Disabled";
12671981771bSAli Ahmed             }
12681e1e598dSJonathan Doman             });
12691981771bSAli Ahmed         },
12701981771bSAli Ahmed         "xyz.openbmc_project.ObjectMapper",
12711981771bSAli Ahmed         "/xyz/openbmc_project/object_mapper",
12721981771bSAli Ahmed         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
12731981771bSAli Ahmed         std::array<const char*, 1>{"xyz.openbmc_project.Control.TPM.Policy"});
12741981771bSAli Ahmed }
12751981771bSAli Ahmed 
12761981771bSAli Ahmed /**
12771c05dae3SAli Ahmed  * @brief Set TrustedModuleRequiredToBoot property. Determines whether or not
12781c05dae3SAli Ahmed  * TPM is required for booting the host.
12791c05dae3SAli Ahmed  *
12801c05dae3SAli Ahmed  * @param[in] aResp         Shared pointer for generating response message.
12811c05dae3SAli Ahmed  * @param[in] tpmRequired   Value to set TPM Required To Boot property to.
12821c05dae3SAli Ahmed  *
12831c05dae3SAli Ahmed  * @return None.
12841c05dae3SAli Ahmed  */
12851c05dae3SAli Ahmed inline void setTrustedModuleRequiredToBoot(
12861c05dae3SAli Ahmed     const std::shared_ptr<bmcweb::AsyncResp>& aResp, const bool tpmRequired)
12871c05dae3SAli Ahmed {
12881c05dae3SAli Ahmed     BMCWEB_LOG_DEBUG << "Set TrustedModuleRequiredToBoot.";
12891c05dae3SAli Ahmed 
12901c05dae3SAli Ahmed     crow::connections::systemBus->async_method_call(
1291b9d36b47SEd Tanous         [aResp, tpmRequired](const boost::system::error_code ec,
1292b9d36b47SEd Tanous                              dbus::utility::MapperGetSubTreeResponse& subtree) {
12931c05dae3SAli Ahmed         if (ec)
12941c05dae3SAli Ahmed         {
1295002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on TPM.Policy GetSubTree"
1296002d39b4SEd Tanous                              << ec;
12971c05dae3SAli Ahmed             messages::internalError(aResp->res);
12981c05dae3SAli Ahmed             return;
12991c05dae3SAli Ahmed         }
130026f6976fSEd Tanous         if (subtree.empty())
13011c05dae3SAli Ahmed         {
13021c05dae3SAli Ahmed             messages::propertyValueNotInList(aResp->res, "ComputerSystem",
13031c05dae3SAli Ahmed                                              "TrustedModuleRequiredToBoot");
13041c05dae3SAli Ahmed             return;
13051c05dae3SAli Ahmed         }
13061c05dae3SAli Ahmed 
13071c05dae3SAli Ahmed         /* When there is more than one TPMEnable object... */
13081c05dae3SAli Ahmed         if (subtree.size() > 1)
13091c05dae3SAli Ahmed         {
13101c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG
13111c05dae3SAli Ahmed                 << "DBUS response has more than 1 TPM Enable object:"
13121c05dae3SAli Ahmed                 << subtree.size();
13131c05dae3SAli Ahmed             // Throw an internal Error and return
13141c05dae3SAli Ahmed             messages::internalError(aResp->res);
13151c05dae3SAli Ahmed             return;
13161c05dae3SAli Ahmed         }
13171c05dae3SAli Ahmed 
13181c05dae3SAli Ahmed         // Make sure the Dbus response map has a service and objectPath
13191c05dae3SAli Ahmed         // field
13201c05dae3SAli Ahmed         if (subtree[0].first.empty() || subtree[0].second.size() != 1)
13211c05dae3SAli Ahmed         {
13221c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG << "TPM.Policy mapper error!";
13231c05dae3SAli Ahmed             messages::internalError(aResp->res);
13241c05dae3SAli Ahmed             return;
13251c05dae3SAli Ahmed         }
13261c05dae3SAli Ahmed 
13271c05dae3SAli Ahmed         const std::string& path = subtree[0].first;
13281c05dae3SAli Ahmed         const std::string& serv = subtree[0].second.begin()->first;
13291c05dae3SAli Ahmed 
13301c05dae3SAli Ahmed         if (serv.empty())
13311c05dae3SAli Ahmed         {
13321c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG << "TPM.Policy service mapper error!";
13331c05dae3SAli Ahmed             messages::internalError(aResp->res);
13341c05dae3SAli Ahmed             return;
13351c05dae3SAli Ahmed         }
13361c05dae3SAli Ahmed 
13371c05dae3SAli Ahmed         // Valid TPM Enable object found, now setting the value
13381c05dae3SAli Ahmed         crow::connections::systemBus->async_method_call(
13391c05dae3SAli Ahmed             [aResp](const boost::system::error_code ec) {
13401c05dae3SAli Ahmed             if (ec)
13411c05dae3SAli Ahmed             {
13420fda0f12SGeorge Liu                 BMCWEB_LOG_DEBUG
13430fda0f12SGeorge Liu                     << "DBUS response error: Set TrustedModuleRequiredToBoot"
13441c05dae3SAli Ahmed                     << ec;
13451c05dae3SAli Ahmed                 messages::internalError(aResp->res);
13461c05dae3SAli Ahmed                 return;
13471c05dae3SAli Ahmed             }
13481c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG << "Set TrustedModuleRequiredToBoot done.";
13491c05dae3SAli Ahmed             },
13501c05dae3SAli Ahmed             serv, path, "org.freedesktop.DBus.Properties", "Set",
13511c05dae3SAli Ahmed             "xyz.openbmc_project.Control.TPM.Policy", "TPMEnable",
1352168e20c1SEd Tanous             dbus::utility::DbusVariantType(tpmRequired));
13531c05dae3SAli Ahmed         },
13541c05dae3SAli Ahmed         "xyz.openbmc_project.ObjectMapper",
13551c05dae3SAli Ahmed         "/xyz/openbmc_project/object_mapper",
13561c05dae3SAli Ahmed         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
13571c05dae3SAli Ahmed         std::array<const char*, 1>{"xyz.openbmc_project.Control.TPM.Policy"});
13581c05dae3SAli Ahmed }
13591c05dae3SAli Ahmed 
13601c05dae3SAli Ahmed /**
1361491d8ee7SSantosh Puranik  * @brief Sets boot properties into DBUS object(s).
1362491d8ee7SSantosh Puranik  *
1363491d8ee7SSantosh Puranik  * @param[in] aResp           Shared pointer for generating response message.
1364cd9a4666SKonstantin Aladyshev  * @param[in] bootType        The boot type to set.
1365cd9a4666SKonstantin Aladyshev  * @return Integer error code.
1366cd9a4666SKonstantin Aladyshev  */
1367cd9a4666SKonstantin Aladyshev inline void setBootType(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1368cd9a4666SKonstantin Aladyshev                         const std::optional<std::string>& bootType)
1369cd9a4666SKonstantin Aladyshev {
1370c21865c4SKonstantin Aladyshev     std::string bootTypeStr;
1371cd9a4666SKonstantin Aladyshev 
1372c21865c4SKonstantin Aladyshev     if (!bootType)
1373cd9a4666SKonstantin Aladyshev     {
1374c21865c4SKonstantin Aladyshev         return;
1375c21865c4SKonstantin Aladyshev     }
1376c21865c4SKonstantin Aladyshev 
1377cd9a4666SKonstantin Aladyshev     // Source target specified
1378cd9a4666SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Boot type: " << *bootType;
1379cd9a4666SKonstantin Aladyshev     // Figure out which DBUS interface and property to use
1380cd9a4666SKonstantin Aladyshev     if (*bootType == "Legacy")
1381cd9a4666SKonstantin Aladyshev     {
1382cd9a4666SKonstantin Aladyshev         bootTypeStr = "xyz.openbmc_project.Control.Boot.Type.Types.Legacy";
1383cd9a4666SKonstantin Aladyshev     }
1384cd9a4666SKonstantin Aladyshev     else if (*bootType == "UEFI")
1385cd9a4666SKonstantin Aladyshev     {
1386cd9a4666SKonstantin Aladyshev         bootTypeStr = "xyz.openbmc_project.Control.Boot.Type.Types.EFI";
1387cd9a4666SKonstantin Aladyshev     }
1388cd9a4666SKonstantin Aladyshev     else
1389cd9a4666SKonstantin Aladyshev     {
1390cd9a4666SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Invalid property value for "
1391cd9a4666SKonstantin Aladyshev                             "BootSourceOverrideMode: "
1392cd9a4666SKonstantin Aladyshev                          << *bootType;
1393cd9a4666SKonstantin Aladyshev         messages::propertyValueNotInList(aResp->res, *bootType,
1394cd9a4666SKonstantin Aladyshev                                          "BootSourceOverrideMode");
1395cd9a4666SKonstantin Aladyshev         return;
1396cd9a4666SKonstantin Aladyshev     }
1397cd9a4666SKonstantin Aladyshev 
1398cd9a4666SKonstantin Aladyshev     // Act on validated parameters
1399cd9a4666SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot type: " << bootTypeStr;
1400cd9a4666SKonstantin Aladyshev 
1401cd9a4666SKonstantin Aladyshev     crow::connections::systemBus->async_method_call(
1402c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec) {
1403cd9a4666SKonstantin Aladyshev         if (ec)
1404cd9a4666SKonstantin Aladyshev         {
1405cd9a4666SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1406cd9a4666SKonstantin Aladyshev             if (ec.value() == boost::asio::error::host_unreachable)
1407cd9a4666SKonstantin Aladyshev             {
1408cd9a4666SKonstantin Aladyshev                 messages::resourceNotFound(aResp->res, "Set", "BootType");
1409cd9a4666SKonstantin Aladyshev                 return;
1410cd9a4666SKonstantin Aladyshev             }
1411cd9a4666SKonstantin Aladyshev             messages::internalError(aResp->res);
1412cd9a4666SKonstantin Aladyshev             return;
1413cd9a4666SKonstantin Aladyshev         }
1414cd9a4666SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Boot type update done.";
1415cd9a4666SKonstantin Aladyshev         },
1416c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1417c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1418cd9a4666SKonstantin Aladyshev         "org.freedesktop.DBus.Properties", "Set",
1419cd9a4666SKonstantin Aladyshev         "xyz.openbmc_project.Control.Boot.Type", "BootType",
1420168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootTypeStr));
1421cd9a4666SKonstantin Aladyshev }
1422cd9a4666SKonstantin Aladyshev 
1423cd9a4666SKonstantin Aladyshev /**
1424cd9a4666SKonstantin Aladyshev  * @brief Sets boot properties into DBUS object(s).
1425cd9a4666SKonstantin Aladyshev  *
1426cd9a4666SKonstantin Aladyshev  * @param[in] aResp           Shared pointer for generating response message.
1427c21865c4SKonstantin Aladyshev  * @param[in] bootType        The boot type to set.
1428c21865c4SKonstantin Aladyshev  * @return Integer error code.
1429c21865c4SKonstantin Aladyshev  */
1430c21865c4SKonstantin Aladyshev inline void setBootEnable(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1431c21865c4SKonstantin Aladyshev                           const std::optional<std::string>& bootEnable)
1432c21865c4SKonstantin Aladyshev {
1433c21865c4SKonstantin Aladyshev     if (!bootEnable)
1434c21865c4SKonstantin Aladyshev     {
1435c21865c4SKonstantin Aladyshev         return;
1436c21865c4SKonstantin Aladyshev     }
1437c21865c4SKonstantin Aladyshev     // Source target specified
1438c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Boot enable: " << *bootEnable;
1439c21865c4SKonstantin Aladyshev 
1440c21865c4SKonstantin Aladyshev     bool bootOverrideEnable = false;
1441c21865c4SKonstantin Aladyshev     bool bootOverridePersistent = false;
1442c21865c4SKonstantin Aladyshev     // Figure out which DBUS interface and property to use
1443c21865c4SKonstantin Aladyshev     if (*bootEnable == "Disabled")
1444c21865c4SKonstantin Aladyshev     {
1445c21865c4SKonstantin Aladyshev         bootOverrideEnable = false;
1446c21865c4SKonstantin Aladyshev     }
1447c21865c4SKonstantin Aladyshev     else if (*bootEnable == "Once")
1448c21865c4SKonstantin Aladyshev     {
1449c21865c4SKonstantin Aladyshev         bootOverrideEnable = true;
1450c21865c4SKonstantin Aladyshev         bootOverridePersistent = false;
1451c21865c4SKonstantin Aladyshev     }
1452c21865c4SKonstantin Aladyshev     else if (*bootEnable == "Continuous")
1453c21865c4SKonstantin Aladyshev     {
1454c21865c4SKonstantin Aladyshev         bootOverrideEnable = true;
1455c21865c4SKonstantin Aladyshev         bootOverridePersistent = true;
1456c21865c4SKonstantin Aladyshev     }
1457c21865c4SKonstantin Aladyshev     else
1458c21865c4SKonstantin Aladyshev     {
14590fda0f12SGeorge Liu         BMCWEB_LOG_DEBUG
14600fda0f12SGeorge Liu             << "Invalid property value for BootSourceOverrideEnabled: "
1461c21865c4SKonstantin Aladyshev             << *bootEnable;
1462c21865c4SKonstantin Aladyshev         messages::propertyValueNotInList(aResp->res, *bootEnable,
1463c21865c4SKonstantin Aladyshev                                          "BootSourceOverrideEnabled");
1464c21865c4SKonstantin Aladyshev         return;
1465c21865c4SKonstantin Aladyshev     }
1466c21865c4SKonstantin Aladyshev 
1467c21865c4SKonstantin Aladyshev     // Act on validated parameters
1468c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot override enable: " << bootOverrideEnable;
1469c21865c4SKonstantin Aladyshev 
1470c21865c4SKonstantin Aladyshev     crow::connections::systemBus->async_method_call(
1471c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec) {
1472c21865c4SKonstantin Aladyshev         if (ec)
1473c21865c4SKonstantin Aladyshev         {
1474c21865c4SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1475c21865c4SKonstantin Aladyshev             messages::internalError(aResp->res);
1476c21865c4SKonstantin Aladyshev             return;
1477c21865c4SKonstantin Aladyshev         }
1478c21865c4SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Boot override enable update done.";
1479c21865c4SKonstantin Aladyshev         },
1480c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1481c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1482c21865c4SKonstantin Aladyshev         "org.freedesktop.DBus.Properties", "Set",
1483c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Object.Enable", "Enabled",
1484168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootOverrideEnable));
1485c21865c4SKonstantin Aladyshev 
1486c21865c4SKonstantin Aladyshev     if (!bootOverrideEnable)
1487c21865c4SKonstantin Aladyshev     {
1488c21865c4SKonstantin Aladyshev         return;
1489c21865c4SKonstantin Aladyshev     }
1490c21865c4SKonstantin Aladyshev 
1491c21865c4SKonstantin Aladyshev     // In case boot override is enabled we need to set correct value for the
1492c21865c4SKonstantin Aladyshev     // 'one_time' enable DBus interface
1493c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot override persistent: "
1494c21865c4SKonstantin Aladyshev                      << bootOverridePersistent;
1495c21865c4SKonstantin Aladyshev 
1496c21865c4SKonstantin Aladyshev     crow::connections::systemBus->async_method_call(
1497c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec) {
1498c21865c4SKonstantin Aladyshev         if (ec)
1499c21865c4SKonstantin Aladyshev         {
1500c21865c4SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1501c21865c4SKonstantin Aladyshev             messages::internalError(aResp->res);
1502c21865c4SKonstantin Aladyshev             return;
1503c21865c4SKonstantin Aladyshev         }
1504c21865c4SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Boot one_time update done.";
1505c21865c4SKonstantin Aladyshev         },
1506c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1507c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot/one_time",
1508c21865c4SKonstantin Aladyshev         "org.freedesktop.DBus.Properties", "Set",
1509c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Object.Enable", "Enabled",
1510168e20c1SEd Tanous         dbus::utility::DbusVariantType(!bootOverridePersistent));
1511c21865c4SKonstantin Aladyshev }
1512c21865c4SKonstantin Aladyshev 
1513c21865c4SKonstantin Aladyshev /**
1514c21865c4SKonstantin Aladyshev  * @brief Sets boot properties into DBUS object(s).
1515c21865c4SKonstantin Aladyshev  *
1516c21865c4SKonstantin Aladyshev  * @param[in] aResp           Shared pointer for generating response message.
1517491d8ee7SSantosh Puranik  * @param[in] bootSource      The boot source to set.
1518491d8ee7SSantosh Puranik  *
1519265c1602SJohnathan Mantey  * @return Integer error code.
1520491d8ee7SSantosh Puranik  */
1521cd9a4666SKonstantin Aladyshev inline void setBootModeOrSource(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1522cd9a4666SKonstantin Aladyshev                                 const std::optional<std::string>& bootSource)
1523491d8ee7SSantosh Puranik {
1524c21865c4SKonstantin Aladyshev     std::string bootSourceStr;
1525c21865c4SKonstantin Aladyshev     std::string bootModeStr;
1526944ffaf9SJohnathan Mantey 
1527c21865c4SKonstantin Aladyshev     if (!bootSource)
1528491d8ee7SSantosh Puranik     {
1529c21865c4SKonstantin Aladyshev         return;
1530c21865c4SKonstantin Aladyshev     }
1531c21865c4SKonstantin Aladyshev 
1532491d8ee7SSantosh Puranik     // Source target specified
1533491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Boot source: " << *bootSource;
1534491d8ee7SSantosh Puranik     // Figure out which DBUS interface and property to use
1535e662eae8SEd Tanous     if (assignBootParameters(aResp, *bootSource, bootSourceStr, bootModeStr) !=
1536e662eae8SEd Tanous         0)
1537491d8ee7SSantosh Puranik     {
1538944ffaf9SJohnathan Mantey         BMCWEB_LOG_DEBUG
1539944ffaf9SJohnathan Mantey             << "Invalid property value for BootSourceOverrideTarget: "
1540491d8ee7SSantosh Puranik             << *bootSource;
1541491d8ee7SSantosh Puranik         messages::propertyValueNotInList(aResp->res, *bootSource,
1542491d8ee7SSantosh Puranik                                          "BootSourceTargetOverride");
1543491d8ee7SSantosh Puranik         return;
1544491d8ee7SSantosh Puranik     }
1545491d8ee7SSantosh Puranik 
1546944ffaf9SJohnathan Mantey     // Act on validated parameters
1547944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
1548944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
1549944ffaf9SJohnathan Mantey 
1550491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1551491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec) {
1552491d8ee7SSantosh Puranik         if (ec)
1553491d8ee7SSantosh Puranik         {
1554491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1555491d8ee7SSantosh Puranik             messages::internalError(aResp->res);
1556491d8ee7SSantosh Puranik             return;
1557491d8ee7SSantosh Puranik         }
1558491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot source update done.";
1559491d8ee7SSantosh Puranik         },
1560c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1561c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1562491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1563491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource",
1564168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootSourceStr));
1565944ffaf9SJohnathan Mantey 
1566491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1567491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec) {
1568491d8ee7SSantosh Puranik         if (ec)
1569491d8ee7SSantosh Puranik         {
1570491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1571491d8ee7SSantosh Puranik             messages::internalError(aResp->res);
1572491d8ee7SSantosh Puranik             return;
1573491d8ee7SSantosh Puranik         }
1574491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot mode update done.";
1575491d8ee7SSantosh Puranik         },
1576c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1577c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1578491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1579491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
1580168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootModeStr));
1581cd9a4666SKonstantin Aladyshev }
1582944ffaf9SJohnathan Mantey 
1583cd9a4666SKonstantin Aladyshev /**
1584c21865c4SKonstantin Aladyshev  * @brief Sets Boot source override properties.
1585491d8ee7SSantosh Puranik  *
1586491d8ee7SSantosh Puranik  * @param[in] aResp      Shared pointer for generating response message.
1587491d8ee7SSantosh Puranik  * @param[in] bootSource The boot source from incoming RF request.
1588cd9a4666SKonstantin Aladyshev  * @param[in] bootType   The boot type from incoming RF request.
1589491d8ee7SSantosh Puranik  * @param[in] bootEnable The boot override enable from incoming RF request.
1590491d8ee7SSantosh Puranik  *
1591265c1602SJohnathan Mantey  * @return Integer error code.
1592491d8ee7SSantosh Puranik  */
1593c21865c4SKonstantin Aladyshev 
1594c21865c4SKonstantin Aladyshev inline void setBootProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1595c21865c4SKonstantin Aladyshev                               const std::optional<std::string>& bootSource,
1596c21865c4SKonstantin Aladyshev                               const std::optional<std::string>& bootType,
1597c21865c4SKonstantin Aladyshev                               const std::optional<std::string>& bootEnable)
1598491d8ee7SSantosh Puranik {
1599491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set boot information.";
1600491d8ee7SSantosh Puranik 
1601c21865c4SKonstantin Aladyshev     setBootModeOrSource(aResp, bootSource);
1602c21865c4SKonstantin Aladyshev     setBootType(aResp, bootType);
1603c21865c4SKonstantin Aladyshev     setBootEnable(aResp, bootEnable);
1604491d8ee7SSantosh Puranik }
1605491d8ee7SSantosh Puranik 
1606c6a620f2SGeorge Liu /**
160798e386ecSGunnar Mills  * @brief Sets AssetTag
160898e386ecSGunnar Mills  *
160998e386ecSGunnar Mills  * @param[in] aResp   Shared pointer for generating response message.
161098e386ecSGunnar Mills  * @param[in] assetTag  "AssetTag" from request.
161198e386ecSGunnar Mills  *
161298e386ecSGunnar Mills  * @return None.
161398e386ecSGunnar Mills  */
16148d1b46d7Szhanghch05 inline void setAssetTag(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
161598e386ecSGunnar Mills                         const std::string& assetTag)
161698e386ecSGunnar Mills {
161798e386ecSGunnar Mills     crow::connections::systemBus->async_method_call(
1618b9d36b47SEd Tanous         [aResp,
1619b9d36b47SEd Tanous          assetTag](const boost::system::error_code ec,
1620b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
162198e386ecSGunnar Mills         if (ec)
162298e386ecSGunnar Mills         {
162398e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " << ec;
162498e386ecSGunnar Mills             messages::internalError(aResp->res);
162598e386ecSGunnar Mills             return;
162698e386ecSGunnar Mills         }
162726f6976fSEd Tanous         if (subtree.empty())
162898e386ecSGunnar Mills         {
162998e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find system D-Bus object!";
163098e386ecSGunnar Mills             messages::internalError(aResp->res);
163198e386ecSGunnar Mills             return;
163298e386ecSGunnar Mills         }
163398e386ecSGunnar Mills         // Assume only 1 system D-Bus object
163498e386ecSGunnar Mills         // Throw an error if there is more than 1
163598e386ecSGunnar Mills         if (subtree.size() > 1)
163698e386ecSGunnar Mills         {
163798e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Found more than 1 system D-Bus object!";
163898e386ecSGunnar Mills             messages::internalError(aResp->res);
163998e386ecSGunnar Mills             return;
164098e386ecSGunnar Mills         }
164198e386ecSGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.size() != 1)
164298e386ecSGunnar Mills         {
164398e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Asset Tag Set mapper error!";
164498e386ecSGunnar Mills             messages::internalError(aResp->res);
164598e386ecSGunnar Mills             return;
164698e386ecSGunnar Mills         }
164798e386ecSGunnar Mills 
164898e386ecSGunnar Mills         const std::string& path = subtree[0].first;
164998e386ecSGunnar Mills         const std::string& service = subtree[0].second.begin()->first;
165098e386ecSGunnar Mills 
165198e386ecSGunnar Mills         if (service.empty())
165298e386ecSGunnar Mills         {
165398e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Asset Tag Set service mapper error!";
165498e386ecSGunnar Mills             messages::internalError(aResp->res);
165598e386ecSGunnar Mills             return;
165698e386ecSGunnar Mills         }
165798e386ecSGunnar Mills 
165898e386ecSGunnar Mills         crow::connections::systemBus->async_method_call(
165998e386ecSGunnar Mills             [aResp](const boost::system::error_code ec2) {
166098e386ecSGunnar Mills             if (ec2)
166198e386ecSGunnar Mills             {
1662002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "D-Bus response error on AssetTag Set "
1663002d39b4SEd Tanous                                  << ec2;
166498e386ecSGunnar Mills                 messages::internalError(aResp->res);
166598e386ecSGunnar Mills                 return;
166698e386ecSGunnar Mills             }
166798e386ecSGunnar Mills             },
166898e386ecSGunnar Mills             service, path, "org.freedesktop.DBus.Properties", "Set",
166998e386ecSGunnar Mills             "xyz.openbmc_project.Inventory.Decorator.AssetTag", "AssetTag",
1670168e20c1SEd Tanous             dbus::utility::DbusVariantType(assetTag));
167198e386ecSGunnar Mills         },
167298e386ecSGunnar Mills         "xyz.openbmc_project.ObjectMapper",
167398e386ecSGunnar Mills         "/xyz/openbmc_project/object_mapper",
167498e386ecSGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
167598e386ecSGunnar Mills         "/xyz/openbmc_project/inventory", int32_t(0),
167698e386ecSGunnar Mills         std::array<const char*, 1>{
167798e386ecSGunnar Mills             "xyz.openbmc_project.Inventory.Item.System"});
167898e386ecSGunnar Mills }
167998e386ecSGunnar Mills 
168098e386ecSGunnar Mills /**
168169f35306SGunnar Mills  * @brief Sets automaticRetry (Auto Reboot)
168269f35306SGunnar Mills  *
168369f35306SGunnar Mills  * @param[in] aResp   Shared pointer for generating response message.
168469f35306SGunnar Mills  * @param[in] automaticRetryConfig  "AutomaticRetryConfig" from request.
168569f35306SGunnar Mills  *
168669f35306SGunnar Mills  * @return None.
168769f35306SGunnar Mills  */
16888d1b46d7Szhanghch05 inline void setAutomaticRetry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1689f23b7296SEd Tanous                               const std::string& automaticRetryConfig)
169069f35306SGunnar Mills {
169169f35306SGunnar Mills     BMCWEB_LOG_DEBUG << "Set Automatic Retry.";
169269f35306SGunnar Mills 
169369f35306SGunnar Mills     // OpenBMC only supports "Disabled" and "RetryAttempts".
1694543f4400SEd Tanous     bool autoRebootEnabled = false;
169569f35306SGunnar Mills 
169669f35306SGunnar Mills     if (automaticRetryConfig == "Disabled")
169769f35306SGunnar Mills     {
169869f35306SGunnar Mills         autoRebootEnabled = false;
169969f35306SGunnar Mills     }
170069f35306SGunnar Mills     else if (automaticRetryConfig == "RetryAttempts")
170169f35306SGunnar Mills     {
170269f35306SGunnar Mills         autoRebootEnabled = true;
170369f35306SGunnar Mills     }
170469f35306SGunnar Mills     else
170569f35306SGunnar Mills     {
17060fda0f12SGeorge Liu         BMCWEB_LOG_DEBUG << "Invalid property value for AutomaticRetryConfig: "
170769f35306SGunnar Mills                          << automaticRetryConfig;
170869f35306SGunnar Mills         messages::propertyValueNotInList(aResp->res, automaticRetryConfig,
170969f35306SGunnar Mills                                          "AutomaticRetryConfig");
171069f35306SGunnar Mills         return;
171169f35306SGunnar Mills     }
171269f35306SGunnar Mills 
171369f35306SGunnar Mills     crow::connections::systemBus->async_method_call(
171469f35306SGunnar Mills         [aResp](const boost::system::error_code ec) {
171569f35306SGunnar Mills         if (ec)
171669f35306SGunnar Mills         {
171769f35306SGunnar Mills             messages::internalError(aResp->res);
171869f35306SGunnar Mills             return;
171969f35306SGunnar Mills         }
172069f35306SGunnar Mills         },
172169f35306SGunnar Mills         "xyz.openbmc_project.Settings",
172269f35306SGunnar Mills         "/xyz/openbmc_project/control/host0/auto_reboot",
172369f35306SGunnar Mills         "org.freedesktop.DBus.Properties", "Set",
172469f35306SGunnar Mills         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
1725168e20c1SEd Tanous         dbus::utility::DbusVariantType(autoRebootEnabled));
172669f35306SGunnar Mills }
172769f35306SGunnar Mills 
172869f35306SGunnar Mills /**
1729c6a620f2SGeorge Liu  * @brief Sets power restore policy properties.
1730c6a620f2SGeorge Liu  *
1731c6a620f2SGeorge Liu  * @param[in] aResp   Shared pointer for generating response message.
1732c6a620f2SGeorge Liu  * @param[in] policy  power restore policy properties from request.
1733c6a620f2SGeorge Liu  *
1734c6a620f2SGeorge Liu  * @return None.
1735c6a620f2SGeorge Liu  */
17368d1b46d7Szhanghch05 inline void
17378d1b46d7Szhanghch05     setPowerRestorePolicy(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
17384e69c904SGunnar Mills                           const std::string& policy)
1739c6a620f2SGeorge Liu {
1740c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Set power restore policy.";
1741c6a620f2SGeorge Liu 
1742c6a620f2SGeorge Liu     const boost::container::flat_map<std::string, std::string> policyMaps = {
17430fda0f12SGeorge Liu         {"AlwaysOn",
17440fda0f12SGeorge Liu          "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn"},
17450fda0f12SGeorge Liu         {"AlwaysOff",
17460fda0f12SGeorge Liu          "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff"},
17470fda0f12SGeorge Liu         {"LastState",
17480fda0f12SGeorge Liu          "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore"}};
1749c6a620f2SGeorge Liu 
1750c6a620f2SGeorge Liu     std::string powerRestorPolicy;
1751c6a620f2SGeorge Liu 
17524e69c904SGunnar Mills     auto policyMapsIt = policyMaps.find(policy);
1753c6a620f2SGeorge Liu     if (policyMapsIt == policyMaps.end())
1754c6a620f2SGeorge Liu     {
17554e69c904SGunnar Mills         messages::propertyValueNotInList(aResp->res, policy,
17564e69c904SGunnar Mills                                          "PowerRestorePolicy");
1757c6a620f2SGeorge Liu         return;
1758c6a620f2SGeorge Liu     }
1759c6a620f2SGeorge Liu 
1760c6a620f2SGeorge Liu     powerRestorPolicy = policyMapsIt->second;
1761c6a620f2SGeorge Liu 
1762c6a620f2SGeorge Liu     crow::connections::systemBus->async_method_call(
1763c6a620f2SGeorge Liu         [aResp](const boost::system::error_code ec) {
1764c6a620f2SGeorge Liu         if (ec)
1765c6a620f2SGeorge Liu         {
1766c6a620f2SGeorge Liu             messages::internalError(aResp->res);
1767c6a620f2SGeorge Liu             return;
1768c6a620f2SGeorge Liu         }
1769c6a620f2SGeorge Liu         },
1770c6a620f2SGeorge Liu         "xyz.openbmc_project.Settings",
1771c6a620f2SGeorge Liu         "/xyz/openbmc_project/control/host0/power_restore_policy",
1772c6a620f2SGeorge Liu         "org.freedesktop.DBus.Properties", "Set",
1773c6a620f2SGeorge Liu         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
1774168e20c1SEd Tanous         dbus::utility::DbusVariantType(powerRestorPolicy));
1775c6a620f2SGeorge Liu }
1776c6a620f2SGeorge Liu 
1777a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
1778a6349918SAppaRao Puli /**
1779a6349918SAppaRao Puli  * @brief Retrieves provisioning status
1780a6349918SAppaRao Puli  *
1781a6349918SAppaRao Puli  * @param[in] aResp     Shared pointer for completing asynchronous calls.
1782a6349918SAppaRao Puli  *
1783a6349918SAppaRao Puli  * @return None.
1784a6349918SAppaRao Puli  */
17858d1b46d7Szhanghch05 inline void getProvisioningStatus(std::shared_ptr<bmcweb::AsyncResp> aResp)
1786a6349918SAppaRao Puli {
1787a6349918SAppaRao Puli     BMCWEB_LOG_DEBUG << "Get OEM information.";
1788a6349918SAppaRao Puli     crow::connections::systemBus->async_method_call(
1789a6349918SAppaRao Puli         [aResp](const boost::system::error_code ec,
1790b9d36b47SEd Tanous                 const dbus::utility::DBusPropertiesMap& propertiesList) {
1791b99fb1a9SAppaRao Puli         nlohmann::json& oemPFR =
1792b99fb1a9SAppaRao Puli             aResp->res.jsonValue["Oem"]["OpenBmc"]["FirmwareProvisioning"];
179350626f4fSJames Feist         aResp->res.jsonValue["Oem"]["OpenBmc"]["@odata.type"] =
179450626f4fSJames Feist             "#OemComputerSystem.OpenBmc";
179550626f4fSJames Feist         oemPFR["@odata.type"] = "#OemComputerSystem.FirmwareProvisioning";
179650626f4fSJames Feist 
1797a6349918SAppaRao Puli         if (ec)
1798a6349918SAppaRao Puli         {
1799a6349918SAppaRao Puli             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1800b99fb1a9SAppaRao Puli             // not an error, don't have to have the interface
1801b99fb1a9SAppaRao Puli             oemPFR["ProvisioningStatus"] = "NotProvisioned";
1802a6349918SAppaRao Puli             return;
1803a6349918SAppaRao Puli         }
1804a6349918SAppaRao Puli 
1805a6349918SAppaRao Puli         const bool* provState = nullptr;
1806a6349918SAppaRao Puli         const bool* lockState = nullptr;
18076e3b67ecSAppaRao Puli         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
18086e3b67ecSAppaRao Puli                  property : propertiesList)
1809a6349918SAppaRao Puli         {
1810a6349918SAppaRao Puli             if (property.first == "UfmProvisioned")
1811a6349918SAppaRao Puli             {
1812a6349918SAppaRao Puli                 provState = std::get_if<bool>(&property.second);
1813a6349918SAppaRao Puli             }
1814a6349918SAppaRao Puli             else if (property.first == "UfmLocked")
1815a6349918SAppaRao Puli             {
1816a6349918SAppaRao Puli                 lockState = std::get_if<bool>(&property.second);
1817a6349918SAppaRao Puli             }
1818a6349918SAppaRao Puli         }
1819a6349918SAppaRao Puli 
1820a6349918SAppaRao Puli         if ((provState == nullptr) || (lockState == nullptr))
1821a6349918SAppaRao Puli         {
1822a6349918SAppaRao Puli             BMCWEB_LOG_DEBUG << "Unable to get PFR attributes.";
1823a6349918SAppaRao Puli             messages::internalError(aResp->res);
1824a6349918SAppaRao Puli             return;
1825a6349918SAppaRao Puli         }
1826a6349918SAppaRao Puli 
1827a6349918SAppaRao Puli         if (*provState == true)
1828a6349918SAppaRao Puli         {
1829a6349918SAppaRao Puli             if (*lockState == true)
1830a6349918SAppaRao Puli             {
1831a6349918SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "ProvisionedAndLocked";
1832a6349918SAppaRao Puli             }
1833a6349918SAppaRao Puli             else
1834a6349918SAppaRao Puli             {
1835a6349918SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "ProvisionedButNotLocked";
1836a6349918SAppaRao Puli             }
1837a6349918SAppaRao Puli         }
1838a6349918SAppaRao Puli         else
1839a6349918SAppaRao Puli         {
1840a6349918SAppaRao Puli             oemPFR["ProvisioningStatus"] = "NotProvisioned";
1841a6349918SAppaRao Puli         }
1842a6349918SAppaRao Puli         },
1843a6349918SAppaRao Puli         "xyz.openbmc_project.PFR.Manager", "/xyz/openbmc_project/pfr",
1844a6349918SAppaRao Puli         "org.freedesktop.DBus.Properties", "GetAll",
1845a6349918SAppaRao Puli         "xyz.openbmc_project.PFR.Attributes");
1846a6349918SAppaRao Puli }
1847a6349918SAppaRao Puli #endif
1848a6349918SAppaRao Puli 
1849491d8ee7SSantosh Puranik /**
18503a2d0424SChris Cain  * @brief Translate the PowerMode to a response message.
18513a2d0424SChris Cain  *
18523a2d0424SChris Cain  * @param[in] aResp  Shared pointer for generating response message.
18533a2d0424SChris Cain  * @param[in] modeValue  PowerMode value to be translated
18543a2d0424SChris Cain  *
18553a2d0424SChris Cain  * @return None.
18563a2d0424SChris Cain  */
18573a2d0424SChris Cain inline void translatePowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
18583a2d0424SChris Cain                                const std::string& modeValue)
18593a2d0424SChris Cain {
18600fda0f12SGeorge Liu     if (modeValue == "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static")
18613a2d0424SChris Cain     {
18623a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "Static";
18633a2d0424SChris Cain     }
18640fda0f12SGeorge Liu     else if (
18650fda0f12SGeorge Liu         modeValue ==
18660fda0f12SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance")
18673a2d0424SChris Cain     {
18683a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "MaximumPerformance";
18693a2d0424SChris Cain     }
18700fda0f12SGeorge Liu     else if (modeValue ==
18710fda0f12SGeorge Liu              "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving")
18723a2d0424SChris Cain     {
18733a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "PowerSaving";
18743a2d0424SChris Cain     }
18750fda0f12SGeorge Liu     else if (modeValue ==
18760fda0f12SGeorge Liu              "xyz.openbmc_project.Control.Power.Mode.PowerMode.OEM")
18773a2d0424SChris Cain     {
18783a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "OEM";
18793a2d0424SChris Cain     }
18803a2d0424SChris Cain     else
18813a2d0424SChris Cain     {
18823a2d0424SChris Cain         // Any other values would be invalid
18833a2d0424SChris Cain         BMCWEB_LOG_DEBUG << "PowerMode value was not valid: " << modeValue;
18843a2d0424SChris Cain         messages::internalError(aResp->res);
18853a2d0424SChris Cain     }
18863a2d0424SChris Cain }
18873a2d0424SChris Cain 
18883a2d0424SChris Cain /**
18893a2d0424SChris Cain  * @brief Retrieves system power mode
18903a2d0424SChris Cain  *
18913a2d0424SChris Cain  * @param[in] aResp  Shared pointer for generating response message.
18923a2d0424SChris Cain  *
18933a2d0424SChris Cain  * @return None.
18943a2d0424SChris Cain  */
18953a2d0424SChris Cain inline void getPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
18963a2d0424SChris Cain {
18973a2d0424SChris Cain     BMCWEB_LOG_DEBUG << "Get power mode.";
18983a2d0424SChris Cain 
18993a2d0424SChris Cain     // Get Power Mode object path:
19003a2d0424SChris Cain     crow::connections::systemBus->async_method_call(
1901b9d36b47SEd Tanous         [aResp](const boost::system::error_code ec,
1902b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
19033a2d0424SChris Cain         if (ec)
19043a2d0424SChris Cain         {
1905002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on Power.Mode GetSubTree "
1906002d39b4SEd Tanous                              << ec;
19073a2d0424SChris Cain             // This is an optional D-Bus object so just return if
19083a2d0424SChris Cain             // error occurs
19093a2d0424SChris Cain             return;
19103a2d0424SChris Cain         }
19113a2d0424SChris Cain         if (subtree.empty())
19123a2d0424SChris Cain         {
19133a2d0424SChris Cain             // As noted above, this is an optional interface so just return
19143a2d0424SChris Cain             // if there is no instance found
19153a2d0424SChris Cain             return;
19163a2d0424SChris Cain         }
19173a2d0424SChris Cain         if (subtree.size() > 1)
19183a2d0424SChris Cain         {
19193a2d0424SChris Cain             // More then one PowerMode object is not supported and is an
19203a2d0424SChris Cain             // error
19213a2d0424SChris Cain             BMCWEB_LOG_DEBUG
19223a2d0424SChris Cain                 << "Found more than 1 system D-Bus Power.Mode objects: "
19233a2d0424SChris Cain                 << subtree.size();
19243a2d0424SChris Cain             messages::internalError(aResp->res);
19253a2d0424SChris Cain             return;
19263a2d0424SChris Cain         }
19273a2d0424SChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
19283a2d0424SChris Cain         {
19293a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode mapper error!";
19303a2d0424SChris Cain             messages::internalError(aResp->res);
19313a2d0424SChris Cain             return;
19323a2d0424SChris Cain         }
19333a2d0424SChris Cain         const std::string& path = subtree[0].first;
19343a2d0424SChris Cain         const std::string& service = subtree[0].second.begin()->first;
19353a2d0424SChris Cain         if (service.empty())
19363a2d0424SChris Cain         {
19373a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode service mapper error!";
19383a2d0424SChris Cain             messages::internalError(aResp->res);
19393a2d0424SChris Cain             return;
19403a2d0424SChris Cain         }
19413a2d0424SChris Cain         // Valid Power Mode object found, now read the current value
19421e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
19431e1e598dSJonathan Doman             *crow::connections::systemBus, service, path,
19441e1e598dSJonathan Doman             "xyz.openbmc_project.Control.Power.Mode", "PowerMode",
19453a2d0424SChris Cain             [aResp](const boost::system::error_code ec,
19461e1e598dSJonathan Doman                     const std::string& pmode) {
19473a2d0424SChris Cain             if (ec)
19483a2d0424SChris Cain             {
1949002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error on PowerMode Get: "
1950002d39b4SEd Tanous                                  << ec;
19513a2d0424SChris Cain                 messages::internalError(aResp->res);
19523a2d0424SChris Cain                 return;
19533a2d0424SChris Cain             }
19543a2d0424SChris Cain 
1955002d39b4SEd Tanous             aResp->res.jsonValue["PowerMode@Redfish.AllowableValues"] = {
1956002d39b4SEd Tanous                 "Static", "MaximumPerformance", "PowerSaving"};
19573a2d0424SChris Cain 
19581e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Current power mode: " << pmode;
19591e1e598dSJonathan Doman             translatePowerMode(aResp, pmode);
19601e1e598dSJonathan Doman             });
19613a2d0424SChris Cain         },
19623a2d0424SChris Cain         "xyz.openbmc_project.ObjectMapper",
19633a2d0424SChris Cain         "/xyz/openbmc_project/object_mapper",
19643a2d0424SChris Cain         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
19653a2d0424SChris Cain         std::array<const char*, 1>{"xyz.openbmc_project.Control.Power.Mode"});
19663a2d0424SChris Cain }
19673a2d0424SChris Cain 
19683a2d0424SChris Cain /**
19693a2d0424SChris Cain  * @brief Validate the specified mode is valid and return the PowerMode
19703a2d0424SChris Cain  * name associated with that string
19713a2d0424SChris Cain  *
19723a2d0424SChris Cain  * @param[in] aResp   Shared pointer for generating response message.
19733a2d0424SChris Cain  * @param[in] modeString  String representing the desired PowerMode
19743a2d0424SChris Cain  *
19753a2d0424SChris Cain  * @return PowerMode value or empty string if mode is not valid
19763a2d0424SChris Cain  */
19773a2d0424SChris Cain inline std::string
19783a2d0424SChris Cain     validatePowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
19793a2d0424SChris Cain                       const std::string& modeString)
19803a2d0424SChris Cain {
19813a2d0424SChris Cain     std::string mode;
19823a2d0424SChris Cain 
19833a2d0424SChris Cain     if (modeString == "Static")
19843a2d0424SChris Cain     {
19853a2d0424SChris Cain         mode = "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static";
19863a2d0424SChris Cain     }
19873a2d0424SChris Cain     else if (modeString == "MaximumPerformance")
19883a2d0424SChris Cain     {
19890fda0f12SGeorge Liu         mode =
19900fda0f12SGeorge Liu             "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance";
19913a2d0424SChris Cain     }
19923a2d0424SChris Cain     else if (modeString == "PowerSaving")
19933a2d0424SChris Cain     {
19943a2d0424SChris Cain         mode = "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving";
19953a2d0424SChris Cain     }
19963a2d0424SChris Cain     else
19973a2d0424SChris Cain     {
19983a2d0424SChris Cain         messages::propertyValueNotInList(aResp->res, modeString, "PowerMode");
19993a2d0424SChris Cain     }
20003a2d0424SChris Cain     return mode;
20013a2d0424SChris Cain }
20023a2d0424SChris Cain 
20033a2d0424SChris Cain /**
20043a2d0424SChris Cain  * @brief Sets system power mode.
20053a2d0424SChris Cain  *
20063a2d0424SChris Cain  * @param[in] aResp   Shared pointer for generating response message.
20073a2d0424SChris Cain  * @param[in] pmode   System power mode from request.
20083a2d0424SChris Cain  *
20093a2d0424SChris Cain  * @return None.
20103a2d0424SChris Cain  */
20113a2d0424SChris Cain inline void setPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
20123a2d0424SChris Cain                          const std::string& pmode)
20133a2d0424SChris Cain {
20143a2d0424SChris Cain     BMCWEB_LOG_DEBUG << "Set power mode.";
20153a2d0424SChris Cain 
20163a2d0424SChris Cain     std::string powerMode = validatePowerMode(aResp, pmode);
20173a2d0424SChris Cain     if (powerMode.empty())
20183a2d0424SChris Cain     {
20193a2d0424SChris Cain         return;
20203a2d0424SChris Cain     }
20213a2d0424SChris Cain 
20223a2d0424SChris Cain     // Get Power Mode object path:
20233a2d0424SChris Cain     crow::connections::systemBus->async_method_call(
2024b9d36b47SEd Tanous         [aResp,
2025b9d36b47SEd Tanous          powerMode](const boost::system::error_code ec,
2026b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
20273a2d0424SChris Cain         if (ec)
20283a2d0424SChris Cain         {
2029002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on Power.Mode GetSubTree "
2030002d39b4SEd Tanous                              << ec;
20313a2d0424SChris Cain             // This is an optional D-Bus object, but user attempted to patch
20323a2d0424SChris Cain             messages::internalError(aResp->res);
20333a2d0424SChris Cain             return;
20343a2d0424SChris Cain         }
20353a2d0424SChris Cain         if (subtree.empty())
20363a2d0424SChris Cain         {
20373a2d0424SChris Cain             // This is an optional D-Bus object, but user attempted to patch
20383a2d0424SChris Cain             messages::resourceNotFound(aResp->res, "ComputerSystem",
20393a2d0424SChris Cain                                        "PowerMode");
20403a2d0424SChris Cain             return;
20413a2d0424SChris Cain         }
20423a2d0424SChris Cain         if (subtree.size() > 1)
20433a2d0424SChris Cain         {
20443a2d0424SChris Cain             // More then one PowerMode object is not supported and is an
20453a2d0424SChris Cain             // error
20463a2d0424SChris Cain             BMCWEB_LOG_DEBUG
20473a2d0424SChris Cain                 << "Found more than 1 system D-Bus Power.Mode objects: "
20483a2d0424SChris Cain                 << subtree.size();
20493a2d0424SChris Cain             messages::internalError(aResp->res);
20503a2d0424SChris Cain             return;
20513a2d0424SChris Cain         }
20523a2d0424SChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
20533a2d0424SChris Cain         {
20543a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode mapper error!";
20553a2d0424SChris Cain             messages::internalError(aResp->res);
20563a2d0424SChris Cain             return;
20573a2d0424SChris Cain         }
20583a2d0424SChris Cain         const std::string& path = subtree[0].first;
20593a2d0424SChris Cain         const std::string& service = subtree[0].second.begin()->first;
20603a2d0424SChris Cain         if (service.empty())
20613a2d0424SChris Cain         {
20623a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode service mapper error!";
20633a2d0424SChris Cain             messages::internalError(aResp->res);
20643a2d0424SChris Cain             return;
20653a2d0424SChris Cain         }
20663a2d0424SChris Cain 
20673a2d0424SChris Cain         BMCWEB_LOG_DEBUG << "Setting power mode(" << powerMode << ") -> "
20683a2d0424SChris Cain                          << path;
20693a2d0424SChris Cain 
20703a2d0424SChris Cain         // Set the Power Mode property
20713a2d0424SChris Cain         crow::connections::systemBus->async_method_call(
20723a2d0424SChris Cain             [aResp](const boost::system::error_code ec) {
20733a2d0424SChris Cain             if (ec)
20743a2d0424SChris Cain             {
20753a2d0424SChris Cain                 messages::internalError(aResp->res);
20763a2d0424SChris Cain                 return;
20773a2d0424SChris Cain             }
20783a2d0424SChris Cain             },
20793a2d0424SChris Cain             service, path, "org.freedesktop.DBus.Properties", "Set",
20803a2d0424SChris Cain             "xyz.openbmc_project.Control.Power.Mode", "PowerMode",
2081168e20c1SEd Tanous             dbus::utility::DbusVariantType(powerMode));
20823a2d0424SChris Cain         },
20833a2d0424SChris Cain         "xyz.openbmc_project.ObjectMapper",
20843a2d0424SChris Cain         "/xyz/openbmc_project/object_mapper",
20853a2d0424SChris Cain         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
20863a2d0424SChris Cain         std::array<const char*, 1>{"xyz.openbmc_project.Control.Power.Mode"});
20873a2d0424SChris Cain }
20883a2d0424SChris Cain 
20893a2d0424SChris Cain /**
209051709ffdSYong Li  * @brief Translates watchdog timeout action DBUS property value to redfish.
209151709ffdSYong Li  *
209251709ffdSYong Li  * @param[in] dbusAction    The watchdog timeout action in D-BUS.
209351709ffdSYong Li  *
209451709ffdSYong Li  * @return Returns as a string, the timeout action in Redfish terms. If
209551709ffdSYong Li  * translation cannot be done, returns an empty string.
209651709ffdSYong Li  */
209723a21a1cSEd Tanous inline std::string dbusToRfWatchdogAction(const std::string& dbusAction)
209851709ffdSYong Li {
209951709ffdSYong Li     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.None")
210051709ffdSYong Li     {
210151709ffdSYong Li         return "None";
210251709ffdSYong Li     }
21033174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.HardReset")
210451709ffdSYong Li     {
210551709ffdSYong Li         return "ResetSystem";
210651709ffdSYong Li     }
21073174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerOff")
210851709ffdSYong Li     {
210951709ffdSYong Li         return "PowerDown";
211051709ffdSYong Li     }
21113174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerCycle")
211251709ffdSYong Li     {
211351709ffdSYong Li         return "PowerCycle";
211451709ffdSYong Li     }
211551709ffdSYong Li 
211651709ffdSYong Li     return "";
211751709ffdSYong Li }
211851709ffdSYong Li 
211951709ffdSYong Li /**
2120c45f0082SYong Li  *@brief Translates timeout action from Redfish to DBUS property value.
2121c45f0082SYong Li  *
2122c45f0082SYong Li  *@param[in] rfAction The timeout action in Redfish.
2123c45f0082SYong Li  *
2124c45f0082SYong Li  *@return Returns as a string, the time_out action as expected by DBUS.
2125c45f0082SYong Li  *If translation cannot be done, returns an empty string.
2126c45f0082SYong Li  */
2127c45f0082SYong Li 
212823a21a1cSEd Tanous inline std::string rfToDbusWDTTimeOutAct(const std::string& rfAction)
2129c45f0082SYong Li {
2130c45f0082SYong Li     if (rfAction == "None")
2131c45f0082SYong Li     {
2132c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.None";
2133c45f0082SYong Li     }
21343174e4dfSEd Tanous     if (rfAction == "PowerCycle")
2135c45f0082SYong Li     {
2136c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerCycle";
2137c45f0082SYong Li     }
21383174e4dfSEd Tanous     if (rfAction == "PowerDown")
2139c45f0082SYong Li     {
2140c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerOff";
2141c45f0082SYong Li     }
21423174e4dfSEd Tanous     if (rfAction == "ResetSystem")
2143c45f0082SYong Li     {
2144c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.HardReset";
2145c45f0082SYong Li     }
2146c45f0082SYong Li 
2147c45f0082SYong Li     return "";
2148c45f0082SYong Li }
2149c45f0082SYong Li 
2150c45f0082SYong Li /**
215151709ffdSYong Li  * @brief Retrieves host watchdog timer properties over DBUS
215251709ffdSYong Li  *
215351709ffdSYong Li  * @param[in] aResp     Shared pointer for completing asynchronous calls.
215451709ffdSYong Li  *
215551709ffdSYong Li  * @return None.
215651709ffdSYong Li  */
21578d1b46d7Szhanghch05 inline void
21588d1b46d7Szhanghch05     getHostWatchdogTimer(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
215951709ffdSYong Li {
216051709ffdSYong Li     BMCWEB_LOG_DEBUG << "Get host watchodg";
216151709ffdSYong Li     crow::connections::systemBus->async_method_call(
216251709ffdSYong Li         [aResp](const boost::system::error_code ec,
2163b9d36b47SEd Tanous                 const dbus::utility::DBusPropertiesMap& properties) {
216451709ffdSYong Li         if (ec)
216551709ffdSYong Li         {
216651709ffdSYong Li             // watchdog service is stopped
216751709ffdSYong Li             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
216851709ffdSYong Li             return;
216951709ffdSYong Li         }
217051709ffdSYong Li 
217151709ffdSYong Li         BMCWEB_LOG_DEBUG << "Got " << properties.size() << " wdt prop.";
217251709ffdSYong Li 
217351709ffdSYong Li         nlohmann::json& hostWatchdogTimer =
217451709ffdSYong Li             aResp->res.jsonValue["HostWatchdogTimer"];
217551709ffdSYong Li 
217651709ffdSYong Li         // watchdog service is running/enabled
217751709ffdSYong Li         hostWatchdogTimer["Status"]["State"] = "Enabled";
217851709ffdSYong Li 
217951709ffdSYong Li         for (const auto& property : properties)
218051709ffdSYong Li         {
218151709ffdSYong Li             BMCWEB_LOG_DEBUG << "prop=" << property.first;
218251709ffdSYong Li             if (property.first == "Enabled")
218351709ffdSYong Li             {
218451709ffdSYong Li                 const bool* state = std::get_if<bool>(&property.second);
218551709ffdSYong Li 
2186e662eae8SEd Tanous                 if (state == nullptr)
218751709ffdSYong Li                 {
218851709ffdSYong Li                     messages::internalError(aResp->res);
2189601af5edSChicago Duan                     return;
219051709ffdSYong Li                 }
219151709ffdSYong Li 
219251709ffdSYong Li                 hostWatchdogTimer["FunctionEnabled"] = *state;
219351709ffdSYong Li             }
219451709ffdSYong Li             else if (property.first == "ExpireAction")
219551709ffdSYong Li             {
219651709ffdSYong Li                 const std::string* s =
219751709ffdSYong Li                     std::get_if<std::string>(&property.second);
2198e662eae8SEd Tanous                 if (s == nullptr)
219951709ffdSYong Li                 {
220051709ffdSYong Li                     messages::internalError(aResp->res);
2201601af5edSChicago Duan                     return;
220251709ffdSYong Li                 }
220351709ffdSYong Li 
220451709ffdSYong Li                 std::string action = dbusToRfWatchdogAction(*s);
220551709ffdSYong Li                 if (action.empty())
220651709ffdSYong Li                 {
220751709ffdSYong Li                     messages::internalError(aResp->res);
2208601af5edSChicago Duan                     return;
220951709ffdSYong Li                 }
221051709ffdSYong Li                 hostWatchdogTimer["TimeoutAction"] = action;
221151709ffdSYong Li             }
221251709ffdSYong Li         }
221351709ffdSYong Li         },
221451709ffdSYong Li         "xyz.openbmc_project.Watchdog", "/xyz/openbmc_project/watchdog/host0",
221551709ffdSYong Li         "org.freedesktop.DBus.Properties", "GetAll",
221651709ffdSYong Li         "xyz.openbmc_project.State.Watchdog");
221751709ffdSYong Li }
221851709ffdSYong Li 
221951709ffdSYong Li /**
2220c45f0082SYong Li  * @brief Sets Host WatchDog Timer properties.
2221c45f0082SYong Li  *
2222c45f0082SYong Li  * @param[in] aResp      Shared pointer for generating response message.
2223c45f0082SYong Li  * @param[in] wdtEnable  The WDTimer Enable value (true/false) from incoming
2224c45f0082SYong Li  *                       RF request.
2225c45f0082SYong Li  * @param[in] wdtTimeOutAction The WDT Timeout action, from incoming RF request.
2226c45f0082SYong Li  *
2227c45f0082SYong Li  * @return None.
2228c45f0082SYong Li  */
22298d1b46d7Szhanghch05 inline void setWDTProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2230c45f0082SYong Li                              const std::optional<bool> wdtEnable,
2231c45f0082SYong Li                              const std::optional<std::string>& wdtTimeOutAction)
2232c45f0082SYong Li {
2233c45f0082SYong Li     BMCWEB_LOG_DEBUG << "Set host watchdog";
2234c45f0082SYong Li 
2235c45f0082SYong Li     if (wdtTimeOutAction)
2236c45f0082SYong Li     {
2237c45f0082SYong Li         std::string wdtTimeOutActStr = rfToDbusWDTTimeOutAct(*wdtTimeOutAction);
2238c45f0082SYong Li         // check if TimeOut Action is Valid
2239c45f0082SYong Li         if (wdtTimeOutActStr.empty())
2240c45f0082SYong Li         {
2241c45f0082SYong Li             BMCWEB_LOG_DEBUG << "Unsupported value for TimeoutAction: "
2242c45f0082SYong Li                              << *wdtTimeOutAction;
2243c45f0082SYong Li             messages::propertyValueNotInList(aResp->res, *wdtTimeOutAction,
2244c45f0082SYong Li                                              "TimeoutAction");
2245c45f0082SYong Li             return;
2246c45f0082SYong Li         }
2247c45f0082SYong Li 
2248c45f0082SYong Li         crow::connections::systemBus->async_method_call(
2249c45f0082SYong Li             [aResp](const boost::system::error_code ec) {
2250c45f0082SYong Li             if (ec)
2251c45f0082SYong Li             {
2252c45f0082SYong Li                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
2253c45f0082SYong Li                 messages::internalError(aResp->res);
2254c45f0082SYong Li                 return;
2255c45f0082SYong Li             }
2256c45f0082SYong Li             },
2257c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
2258c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
2259c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
2260c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "ExpireAction",
2261168e20c1SEd Tanous             dbus::utility::DbusVariantType(wdtTimeOutActStr));
2262c45f0082SYong Li     }
2263c45f0082SYong Li 
2264c45f0082SYong Li     if (wdtEnable)
2265c45f0082SYong Li     {
2266c45f0082SYong Li         crow::connections::systemBus->async_method_call(
2267c45f0082SYong Li             [aResp](const boost::system::error_code ec) {
2268c45f0082SYong Li             if (ec)
2269c45f0082SYong Li             {
2270c45f0082SYong Li                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
2271c45f0082SYong Li                 messages::internalError(aResp->res);
2272c45f0082SYong Li                 return;
2273c45f0082SYong Li             }
2274c45f0082SYong Li             },
2275c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
2276c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
2277c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
2278c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "Enabled",
2279168e20c1SEd Tanous             dbus::utility::DbusVariantType(*wdtEnable));
2280c45f0082SYong Li     }
2281c45f0082SYong Li }
2282c45f0082SYong Li 
228337bbf98cSChris Cain using ipsPropertiesType =
228437bbf98cSChris Cain     std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>;
228537bbf98cSChris Cain /**
228637bbf98cSChris Cain  * @brief Parse the Idle Power Saver properties into json
228737bbf98cSChris Cain  *
228837bbf98cSChris Cain  * @param[in] aResp     Shared pointer for completing asynchronous calls.
228937bbf98cSChris Cain  * @param[in] properties  IPS property data from DBus.
229037bbf98cSChris Cain  *
229137bbf98cSChris Cain  * @return true if successful
229237bbf98cSChris Cain  */
2293f6674220SEd Tanous inline bool parseIpsProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
229437bbf98cSChris Cain                                ipsPropertiesType& properties)
229537bbf98cSChris Cain {
229637bbf98cSChris Cain     for (const auto& property : properties)
229737bbf98cSChris Cain     {
229837bbf98cSChris Cain         if (property.first == "Enabled")
229937bbf98cSChris Cain         {
230037bbf98cSChris Cain             const bool* state = std::get_if<bool>(&property.second);
2301e662eae8SEd Tanous             if (state == nullptr)
230237bbf98cSChris Cain             {
230337bbf98cSChris Cain                 return false;
230437bbf98cSChris Cain             }
230537bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"][property.first] = *state;
230637bbf98cSChris Cain         }
230737bbf98cSChris Cain         else if (property.first == "EnterUtilizationPercent")
230837bbf98cSChris Cain         {
230937bbf98cSChris Cain             const uint8_t* util = std::get_if<uint8_t>(&property.second);
2310e662eae8SEd Tanous             if (util == nullptr)
231137bbf98cSChris Cain             {
231237bbf98cSChris Cain                 return false;
231337bbf98cSChris Cain             }
231437bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"][property.first] = *util;
231537bbf98cSChris Cain         }
231637bbf98cSChris Cain         else if (property.first == "EnterDwellTime")
231737bbf98cSChris Cain         {
231837bbf98cSChris Cain             // Convert Dbus time from milliseconds to seconds
231937bbf98cSChris Cain             const uint64_t* timeMilliseconds =
232037bbf98cSChris Cain                 std::get_if<uint64_t>(&property.second);
2321e662eae8SEd Tanous             if (timeMilliseconds == nullptr)
232237bbf98cSChris Cain             {
232337bbf98cSChris Cain                 return false;
232437bbf98cSChris Cain             }
232537bbf98cSChris Cain             const std::chrono::duration<uint64_t, std::milli> ms(
232637bbf98cSChris Cain                 *timeMilliseconds);
232737bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"]["EnterDwellTimeSeconds"] =
232837bbf98cSChris Cain                 std::chrono::duration_cast<std::chrono::duration<uint64_t>>(ms)
232937bbf98cSChris Cain                     .count();
233037bbf98cSChris Cain         }
233137bbf98cSChris Cain         else if (property.first == "ExitUtilizationPercent")
233237bbf98cSChris Cain         {
233337bbf98cSChris Cain             const uint8_t* util = std::get_if<uint8_t>(&property.second);
2334e662eae8SEd Tanous             if (util == nullptr)
233537bbf98cSChris Cain             {
233637bbf98cSChris Cain                 return false;
233737bbf98cSChris Cain             }
233837bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"][property.first] = *util;
233937bbf98cSChris Cain         }
234037bbf98cSChris Cain         else if (property.first == "ExitDwellTime")
234137bbf98cSChris Cain         {
234237bbf98cSChris Cain             // Convert Dbus time from milliseconds to seconds
234337bbf98cSChris Cain             const uint64_t* timeMilliseconds =
234437bbf98cSChris Cain                 std::get_if<uint64_t>(&property.second);
2345e662eae8SEd Tanous             if (timeMilliseconds == nullptr)
234637bbf98cSChris Cain             {
234737bbf98cSChris Cain                 return false;
234837bbf98cSChris Cain             }
234937bbf98cSChris Cain             const std::chrono::duration<uint64_t, std::milli> ms(
235037bbf98cSChris Cain                 *timeMilliseconds);
235137bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"]["ExitDwellTimeSeconds"] =
235237bbf98cSChris Cain                 std::chrono::duration_cast<std::chrono::duration<uint64_t>>(ms)
235337bbf98cSChris Cain                     .count();
235437bbf98cSChris Cain         }
235537bbf98cSChris Cain         else
235637bbf98cSChris Cain         {
235737bbf98cSChris Cain             BMCWEB_LOG_WARNING << "Unexpected IdlePowerSaver property: "
235837bbf98cSChris Cain                                << property.first;
235937bbf98cSChris Cain         }
236037bbf98cSChris Cain     }
236137bbf98cSChris Cain 
236237bbf98cSChris Cain     return true;
236337bbf98cSChris Cain }
236437bbf98cSChris Cain 
236537bbf98cSChris Cain /**
236637bbf98cSChris Cain  * @brief Retrieves host watchdog timer properties over DBUS
236737bbf98cSChris Cain  *
236837bbf98cSChris Cain  * @param[in] aResp     Shared pointer for completing asynchronous calls.
236937bbf98cSChris Cain  *
237037bbf98cSChris Cain  * @return None.
237137bbf98cSChris Cain  */
237237bbf98cSChris Cain inline void getIdlePowerSaver(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
237337bbf98cSChris Cain {
237437bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "Get idle power saver parameters";
237537bbf98cSChris Cain 
237637bbf98cSChris Cain     // Get IdlePowerSaver object path:
237737bbf98cSChris Cain     crow::connections::systemBus->async_method_call(
2378b9d36b47SEd Tanous         [aResp](const boost::system::error_code ec,
2379b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
238037bbf98cSChris Cain         if (ec)
238137bbf98cSChris Cain         {
238237bbf98cSChris Cain             BMCWEB_LOG_DEBUG
238337bbf98cSChris Cain                 << "DBUS response error on Power.IdlePowerSaver GetSubTree "
238437bbf98cSChris Cain                 << ec;
238537bbf98cSChris Cain             messages::internalError(aResp->res);
238637bbf98cSChris Cain             return;
238737bbf98cSChris Cain         }
238837bbf98cSChris Cain         if (subtree.empty())
238937bbf98cSChris Cain         {
239037bbf98cSChris Cain             // This is an optional interface so just return
239137bbf98cSChris Cain             // if there is no instance found
239237bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "No instances found";
239337bbf98cSChris Cain             return;
239437bbf98cSChris Cain         }
239537bbf98cSChris Cain         if (subtree.size() > 1)
239637bbf98cSChris Cain         {
239737bbf98cSChris Cain             // More then one PowerIdlePowerSaver object is not supported and
239837bbf98cSChris Cain             // is an error
239937bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "Found more than 1 system D-Bus "
240037bbf98cSChris Cain                                 "Power.IdlePowerSaver objects: "
240137bbf98cSChris Cain                              << subtree.size();
240237bbf98cSChris Cain             messages::internalError(aResp->res);
240337bbf98cSChris Cain             return;
240437bbf98cSChris Cain         }
240537bbf98cSChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
240637bbf98cSChris Cain         {
240737bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver mapper error!";
240837bbf98cSChris Cain             messages::internalError(aResp->res);
240937bbf98cSChris Cain             return;
241037bbf98cSChris Cain         }
241137bbf98cSChris Cain         const std::string& path = subtree[0].first;
241237bbf98cSChris Cain         const std::string& service = subtree[0].second.begin()->first;
241337bbf98cSChris Cain         if (service.empty())
241437bbf98cSChris Cain         {
2415002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver service mapper error!";
241637bbf98cSChris Cain             messages::internalError(aResp->res);
241737bbf98cSChris Cain             return;
241837bbf98cSChris Cain         }
241937bbf98cSChris Cain 
242037bbf98cSChris Cain         // Valid IdlePowerSaver object found, now read the current values
242137bbf98cSChris Cain         crow::connections::systemBus->async_method_call(
242237bbf98cSChris Cain             [aResp](const boost::system::error_code ec,
242337bbf98cSChris Cain                     ipsPropertiesType& properties) {
242437bbf98cSChris Cain             if (ec)
242537bbf98cSChris Cain             {
242637bbf98cSChris Cain                 BMCWEB_LOG_ERROR
2427002d39b4SEd Tanous                     << "DBUS response error on IdlePowerSaver GetAll: " << ec;
242837bbf98cSChris Cain                 messages::internalError(aResp->res);
242937bbf98cSChris Cain                 return;
243037bbf98cSChris Cain             }
243137bbf98cSChris Cain 
2432e05aec50SEd Tanous             if (!parseIpsProperties(aResp, properties))
243337bbf98cSChris Cain             {
243437bbf98cSChris Cain                 messages::internalError(aResp->res);
243537bbf98cSChris Cain                 return;
243637bbf98cSChris Cain             }
243737bbf98cSChris Cain             },
243837bbf98cSChris Cain             service, path, "org.freedesktop.DBus.Properties", "GetAll",
243937bbf98cSChris Cain             "xyz.openbmc_project.Control.Power.IdlePowerSaver");
244037bbf98cSChris Cain         },
244137bbf98cSChris Cain         "xyz.openbmc_project.ObjectMapper",
244237bbf98cSChris Cain         "/xyz/openbmc_project/object_mapper",
244337bbf98cSChris Cain         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
244437bbf98cSChris Cain         std::array<const char*, 1>{
244537bbf98cSChris Cain             "xyz.openbmc_project.Control.Power.IdlePowerSaver"});
244637bbf98cSChris Cain 
244737bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "EXIT: Get idle power saver parameters";
244837bbf98cSChris Cain }
244937bbf98cSChris Cain 
245037bbf98cSChris Cain /**
245137bbf98cSChris Cain  * @brief Sets Idle Power Saver properties.
245237bbf98cSChris Cain  *
245337bbf98cSChris Cain  * @param[in] aResp      Shared pointer for generating response message.
245437bbf98cSChris Cain  * @param[in] ipsEnable  The IPS Enable value (true/false) from incoming
245537bbf98cSChris Cain  *                       RF request.
245637bbf98cSChris Cain  * @param[in] ipsEnterUtil The utilization limit to enter idle state.
245737bbf98cSChris Cain  * @param[in] ipsEnterTime The time the utilization must be below ipsEnterUtil
245837bbf98cSChris Cain  * before entering idle state.
245937bbf98cSChris Cain  * @param[in] ipsExitUtil The utilization limit when exiting idle state.
246037bbf98cSChris Cain  * @param[in] ipsExitTime The time the utilization must be above ipsExutUtil
246137bbf98cSChris Cain  * before exiting idle state
246237bbf98cSChris Cain  *
246337bbf98cSChris Cain  * @return None.
246437bbf98cSChris Cain  */
246537bbf98cSChris Cain inline void setIdlePowerSaver(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
246637bbf98cSChris Cain                               const std::optional<bool> ipsEnable,
246737bbf98cSChris Cain                               const std::optional<uint8_t> ipsEnterUtil,
246837bbf98cSChris Cain                               const std::optional<uint64_t> ipsEnterTime,
246937bbf98cSChris Cain                               const std::optional<uint8_t> ipsExitUtil,
247037bbf98cSChris Cain                               const std::optional<uint64_t> ipsExitTime)
247137bbf98cSChris Cain {
247237bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "Set idle power saver properties";
247337bbf98cSChris Cain 
247437bbf98cSChris Cain     // Get IdlePowerSaver object path:
247537bbf98cSChris Cain     crow::connections::systemBus->async_method_call(
247637bbf98cSChris Cain         [aResp, ipsEnable, ipsEnterUtil, ipsEnterTime, ipsExitUtil,
2477b9d36b47SEd Tanous          ipsExitTime](const boost::system::error_code ec,
2478b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
247937bbf98cSChris Cain         if (ec)
248037bbf98cSChris Cain         {
248137bbf98cSChris Cain             BMCWEB_LOG_DEBUG
248237bbf98cSChris Cain                 << "DBUS response error on Power.IdlePowerSaver GetSubTree "
248337bbf98cSChris Cain                 << ec;
248437bbf98cSChris Cain             messages::internalError(aResp->res);
248537bbf98cSChris Cain             return;
248637bbf98cSChris Cain         }
248737bbf98cSChris Cain         if (subtree.empty())
248837bbf98cSChris Cain         {
248937bbf98cSChris Cain             // This is an optional D-Bus object, but user attempted to patch
249037bbf98cSChris Cain             messages::resourceNotFound(aResp->res, "ComputerSystem",
249137bbf98cSChris Cain                                        "IdlePowerSaver");
249237bbf98cSChris Cain             return;
249337bbf98cSChris Cain         }
249437bbf98cSChris Cain         if (subtree.size() > 1)
249537bbf98cSChris Cain         {
249637bbf98cSChris Cain             // More then one PowerIdlePowerSaver object is not supported and
249737bbf98cSChris Cain             // is an error
24980fda0f12SGeorge Liu             BMCWEB_LOG_DEBUG
24990fda0f12SGeorge Liu                 << "Found more than 1 system D-Bus Power.IdlePowerSaver objects: "
250037bbf98cSChris Cain                 << subtree.size();
250137bbf98cSChris Cain             messages::internalError(aResp->res);
250237bbf98cSChris Cain             return;
250337bbf98cSChris Cain         }
250437bbf98cSChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
250537bbf98cSChris Cain         {
250637bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver mapper error!";
250737bbf98cSChris Cain             messages::internalError(aResp->res);
250837bbf98cSChris Cain             return;
250937bbf98cSChris Cain         }
251037bbf98cSChris Cain         const std::string& path = subtree[0].first;
251137bbf98cSChris Cain         const std::string& service = subtree[0].second.begin()->first;
251237bbf98cSChris Cain         if (service.empty())
251337bbf98cSChris Cain         {
2514002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver service mapper error!";
251537bbf98cSChris Cain             messages::internalError(aResp->res);
251637bbf98cSChris Cain             return;
251737bbf98cSChris Cain         }
251837bbf98cSChris Cain 
251937bbf98cSChris Cain         // Valid Power IdlePowerSaver object found, now set any values that
252037bbf98cSChris Cain         // need to be updated
252137bbf98cSChris Cain 
252237bbf98cSChris Cain         if (ipsEnable)
252337bbf98cSChris Cain         {
252437bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
252537bbf98cSChris Cain                 [aResp](const boost::system::error_code ec) {
252637bbf98cSChris Cain                 if (ec)
252737bbf98cSChris Cain                 {
252837bbf98cSChris Cain                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
252937bbf98cSChris Cain                     messages::internalError(aResp->res);
253037bbf98cSChris Cain                     return;
253137bbf98cSChris Cain                 }
253237bbf98cSChris Cain                 },
253337bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
2534002d39b4SEd Tanous                 "xyz.openbmc_project.Control.Power.IdlePowerSaver", "Enabled",
2535002d39b4SEd Tanous                 dbus::utility::DbusVariantType(*ipsEnable));
253637bbf98cSChris Cain         }
253737bbf98cSChris Cain         if (ipsEnterUtil)
253837bbf98cSChris Cain         {
253937bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
254037bbf98cSChris Cain                 [aResp](const boost::system::error_code ec) {
254137bbf98cSChris Cain                 if (ec)
254237bbf98cSChris Cain                 {
254337bbf98cSChris Cain                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
254437bbf98cSChris Cain                     messages::internalError(aResp->res);
254537bbf98cSChris Cain                     return;
254637bbf98cSChris Cain                 }
254737bbf98cSChris Cain                 },
254837bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
254937bbf98cSChris Cain                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
255037bbf98cSChris Cain                 "EnterUtilizationPercent",
2551168e20c1SEd Tanous                 dbus::utility::DbusVariantType(*ipsEnterUtil));
255237bbf98cSChris Cain         }
255337bbf98cSChris Cain         if (ipsEnterTime)
255437bbf98cSChris Cain         {
255537bbf98cSChris Cain             // Convert from seconds into milliseconds for DBus
255637bbf98cSChris Cain             const uint64_t timeMilliseconds = *ipsEnterTime * 1000;
255737bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
255837bbf98cSChris Cain                 [aResp](const boost::system::error_code ec) {
255937bbf98cSChris Cain                 if (ec)
256037bbf98cSChris Cain                 {
256137bbf98cSChris Cain                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
256237bbf98cSChris Cain                     messages::internalError(aResp->res);
256337bbf98cSChris Cain                     return;
256437bbf98cSChris Cain                 }
256537bbf98cSChris Cain                 },
256637bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
256737bbf98cSChris Cain                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2568168e20c1SEd Tanous                 "EnterDwellTime",
2569168e20c1SEd Tanous                 dbus::utility::DbusVariantType(timeMilliseconds));
257037bbf98cSChris Cain         }
257137bbf98cSChris Cain         if (ipsExitUtil)
257237bbf98cSChris Cain         {
257337bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
257437bbf98cSChris Cain                 [aResp](const boost::system::error_code ec) {
257537bbf98cSChris Cain                 if (ec)
257637bbf98cSChris Cain                 {
257737bbf98cSChris Cain                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
257837bbf98cSChris Cain                     messages::internalError(aResp->res);
257937bbf98cSChris Cain                     return;
258037bbf98cSChris Cain                 }
258137bbf98cSChris Cain                 },
258237bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
258337bbf98cSChris Cain                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
258437bbf98cSChris Cain                 "ExitUtilizationPercent",
2585168e20c1SEd Tanous                 dbus::utility::DbusVariantType(*ipsExitUtil));
258637bbf98cSChris Cain         }
258737bbf98cSChris Cain         if (ipsExitTime)
258837bbf98cSChris Cain         {
258937bbf98cSChris Cain             // Convert from seconds into milliseconds for DBus
259037bbf98cSChris Cain             const uint64_t timeMilliseconds = *ipsExitTime * 1000;
259137bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
259237bbf98cSChris Cain                 [aResp](const boost::system::error_code ec) {
259337bbf98cSChris Cain                 if (ec)
259437bbf98cSChris Cain                 {
259537bbf98cSChris Cain                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
259637bbf98cSChris Cain                     messages::internalError(aResp->res);
259737bbf98cSChris Cain                     return;
259837bbf98cSChris Cain                 }
259937bbf98cSChris Cain                 },
260037bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
260137bbf98cSChris Cain                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2602168e20c1SEd Tanous                 "ExitDwellTime",
2603168e20c1SEd Tanous                 dbus::utility::DbusVariantType(timeMilliseconds));
260437bbf98cSChris Cain         }
260537bbf98cSChris Cain         },
260637bbf98cSChris Cain         "xyz.openbmc_project.ObjectMapper",
260737bbf98cSChris Cain         "/xyz/openbmc_project/object_mapper",
260837bbf98cSChris Cain         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
260937bbf98cSChris Cain         std::array<const char*, 1>{
261037bbf98cSChris Cain             "xyz.openbmc_project.Control.Power.IdlePowerSaver"});
261137bbf98cSChris Cain 
261237bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "EXIT: Set idle power saver parameters";
261337bbf98cSChris Cain }
261437bbf98cSChris Cain 
2615c45f0082SYong Li /**
2616c5b2abe0SLewanczyk, Dawid  * SystemsCollection derived class for delivering ComputerSystems Collection
2617c5b2abe0SLewanczyk, Dawid  * Schema
2618c5b2abe0SLewanczyk, Dawid  */
26197e860f15SJohn Edward Broadbent inline void requestRoutesSystemsCollection(App& app)
26201abe55efSEd Tanous {
26217e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/")
2622ed398213SEd Tanous         .privileges(redfish::privileges::getComputerSystemCollection)
26237e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2624f4c99e70SEd Tanous             [&app](const crow::Request& req,
26257e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2626*3ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2627f4c99e70SEd Tanous         {
2628f4c99e70SEd Tanous             return;
2629f4c99e70SEd Tanous         }
26308d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
26310f74e643SEd Tanous             "#ComputerSystemCollection.ComputerSystemCollection";
26328d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Systems";
26338d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Computer System Collection";
2634462023adSSunitha Harish 
26351e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
2636002d39b4SEd Tanous             *crow::connections::systemBus, "xyz.openbmc_project.Settings",
26371e1e598dSJonathan Doman             "/xyz/openbmc_project/network/hypervisor",
2638002d39b4SEd Tanous             "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
26391e1e598dSJonathan Doman             [asyncResp](const boost::system::error_code ec,
26401e1e598dSJonathan Doman                         const std::string& /*hostName*/) {
2641002d39b4SEd Tanous             nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
26422c70f800SEd Tanous             ifaceArray = nlohmann::json::array();
2643002d39b4SEd Tanous             auto& count = asyncResp->res.jsonValue["Members@odata.count"];
26441476687dSEd Tanous 
26451476687dSEd Tanous             nlohmann::json::object_t system;
26461476687dSEd Tanous             system["@odata.id"] = "/redfish/v1/Systems/system";
26471476687dSEd Tanous             ifaceArray.push_back(std::move(system));
264894bda602STim Lee             count = ifaceArray.size();
2649cb13a392SEd Tanous             if (!ec)
2650462023adSSunitha Harish             {
2651462023adSSunitha Harish                 BMCWEB_LOG_DEBUG << "Hypervisor is available";
26521476687dSEd Tanous                 nlohmann::json::object_t hypervisor;
2653002d39b4SEd Tanous                 hypervisor["@odata.id"] = "/redfish/v1/Systems/hypervisor";
26541476687dSEd Tanous                 ifaceArray.push_back(std::move(hypervisor));
26552c70f800SEd Tanous                 count = ifaceArray.size();
2656cb13a392SEd Tanous             }
26571e1e598dSJonathan Doman             });
26587e860f15SJohn Edward Broadbent         });
2659c5b2abe0SLewanczyk, Dawid }
26607e860f15SJohn Edward Broadbent 
26617e860f15SJohn Edward Broadbent /**
26627e860f15SJohn Edward Broadbent  * Function transceives data with dbus directly.
26637e860f15SJohn Edward Broadbent  */
26644f48d5f6SEd Tanous inline void doNMI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
26657e860f15SJohn Edward Broadbent {
26667e860f15SJohn Edward Broadbent     constexpr char const* serviceName = "xyz.openbmc_project.Control.Host.NMI";
26677e860f15SJohn Edward Broadbent     constexpr char const* objectPath = "/xyz/openbmc_project/control/host0/nmi";
26687e860f15SJohn Edward Broadbent     constexpr char const* interfaceName =
26697e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Control.Host.NMI";
26707e860f15SJohn Edward Broadbent     constexpr char const* method = "NMI";
26717e860f15SJohn Edward Broadbent 
26727e860f15SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
26737e860f15SJohn Edward Broadbent         [asyncResp](const boost::system::error_code ec) {
26747e860f15SJohn Edward Broadbent         if (ec)
26757e860f15SJohn Edward Broadbent         {
26767e860f15SJohn Edward Broadbent             BMCWEB_LOG_ERROR << " Bad D-Bus request error: " << ec;
26777e860f15SJohn Edward Broadbent             messages::internalError(asyncResp->res);
26787e860f15SJohn Edward Broadbent             return;
26797e860f15SJohn Edward Broadbent         }
26807e860f15SJohn Edward Broadbent         messages::success(asyncResp->res);
26817e860f15SJohn Edward Broadbent         },
26827e860f15SJohn Edward Broadbent         serviceName, objectPath, interfaceName, method);
26837e860f15SJohn Edward Broadbent }
2684c5b2abe0SLewanczyk, Dawid 
2685c5b2abe0SLewanczyk, Dawid /**
2686cc340dd9SEd Tanous  * SystemActionsReset class supports handle POST method for Reset action.
2687cc340dd9SEd Tanous  * The class retrieves and sends data directly to D-Bus.
2688cc340dd9SEd Tanous  */
26897e860f15SJohn Edward Broadbent inline void requestRoutesSystemActionsReset(App& app)
2690cc340dd9SEd Tanous {
2691cc340dd9SEd Tanous     /**
2692cc340dd9SEd Tanous      * Function handles POST method request.
2693cc340dd9SEd Tanous      * Analyzes POST body message before sends Reset request data to D-Bus.
2694cc340dd9SEd Tanous      */
26957e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
26967e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset/")
2697ed398213SEd Tanous         .privileges(redfish::privileges::postComputerSystem)
2698002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
2699002d39b4SEd Tanous             [&app](const crow::Request& req,
27007e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2701*3ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
270245ca1b86SEd Tanous         {
270345ca1b86SEd Tanous             return;
270445ca1b86SEd Tanous         }
27059712f8acSEd Tanous         std::string resetType;
270615ed6780SWilly Tu         if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
27077e860f15SJohn Edward Broadbent                                        resetType))
2708cc340dd9SEd Tanous         {
2709cc340dd9SEd Tanous             return;
2710cc340dd9SEd Tanous         }
2711cc340dd9SEd Tanous 
2712d22c8396SJason M. Bills         // Get the command and host vs. chassis
2713cc340dd9SEd Tanous         std::string command;
2714543f4400SEd Tanous         bool hostCommand = true;
2715d4d25793SEd Tanous         if ((resetType == "On") || (resetType == "ForceOn"))
2716cc340dd9SEd Tanous         {
2717cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.On";
2718d22c8396SJason M. Bills             hostCommand = true;
2719d22c8396SJason M. Bills         }
2720d22c8396SJason M. Bills         else if (resetType == "ForceOff")
2721d22c8396SJason M. Bills         {
2722d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Chassis.Transition.Off";
2723d22c8396SJason M. Bills             hostCommand = false;
2724d22c8396SJason M. Bills         }
2725d22c8396SJason M. Bills         else if (resetType == "ForceRestart")
2726d22c8396SJason M. Bills         {
272786a0851aSJason M. Bills             command =
272886a0851aSJason M. Bills                 "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot";
272986a0851aSJason M. Bills             hostCommand = true;
2730cc340dd9SEd Tanous         }
27319712f8acSEd Tanous         else if (resetType == "GracefulShutdown")
2732cc340dd9SEd Tanous         {
2733cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.Off";
2734d22c8396SJason M. Bills             hostCommand = true;
2735cc340dd9SEd Tanous         }
27369712f8acSEd Tanous         else if (resetType == "GracefulRestart")
2737cc340dd9SEd Tanous         {
27380fda0f12SGeorge Liu             command =
27390fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot";
2740d22c8396SJason M. Bills             hostCommand = true;
2741d22c8396SJason M. Bills         }
2742d22c8396SJason M. Bills         else if (resetType == "PowerCycle")
2743d22c8396SJason M. Bills         {
274486a0851aSJason M. Bills             command = "xyz.openbmc_project.State.Host.Transition.Reboot";
274586a0851aSJason M. Bills             hostCommand = true;
2746cc340dd9SEd Tanous         }
2747bfd5b826SLakshminarayana R. Kammath         else if (resetType == "Nmi")
2748bfd5b826SLakshminarayana R. Kammath         {
2749bfd5b826SLakshminarayana R. Kammath             doNMI(asyncResp);
2750bfd5b826SLakshminarayana R. Kammath             return;
2751bfd5b826SLakshminarayana R. Kammath         }
2752cc340dd9SEd Tanous         else
2753cc340dd9SEd Tanous         {
27548d1b46d7Szhanghch05             messages::actionParameterUnknown(asyncResp->res, "Reset",
27558d1b46d7Szhanghch05                                              resetType);
2756cc340dd9SEd Tanous             return;
2757cc340dd9SEd Tanous         }
2758cc340dd9SEd Tanous 
2759d22c8396SJason M. Bills         if (hostCommand)
2760d22c8396SJason M. Bills         {
2761cc340dd9SEd Tanous             crow::connections::systemBus->async_method_call(
2762d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
2763cc340dd9SEd Tanous                 if (ec)
2764cc340dd9SEd Tanous                 {
2765cc340dd9SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
2766002d39b4SEd Tanous                     if (ec.value() == boost::asio::error::invalid_argument)
2767d22c8396SJason M. Bills                     {
2768d22c8396SJason M. Bills                         messages::actionParameterNotSupported(
2769d22c8396SJason M. Bills                             asyncResp->res, resetType, "Reset");
2770d22c8396SJason M. Bills                     }
2771d22c8396SJason M. Bills                     else
2772d22c8396SJason M. Bills                     {
2773f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2774d22c8396SJason M. Bills                     }
2775cc340dd9SEd Tanous                     return;
2776cc340dd9SEd Tanous                 }
2777f12894f8SJason M. Bills                 messages::success(asyncResp->res);
2778cc340dd9SEd Tanous                 },
2779cc340dd9SEd Tanous                 "xyz.openbmc_project.State.Host",
2780cc340dd9SEd Tanous                 "/xyz/openbmc_project/state/host0",
2781cc340dd9SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
27829712f8acSEd Tanous                 "xyz.openbmc_project.State.Host", "RequestedHostTransition",
2783168e20c1SEd Tanous                 dbus::utility::DbusVariantType{command});
2784cc340dd9SEd Tanous         }
2785d22c8396SJason M. Bills         else
2786d22c8396SJason M. Bills         {
2787d22c8396SJason M. Bills             crow::connections::systemBus->async_method_call(
2788d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
2789d22c8396SJason M. Bills                 if (ec)
2790d22c8396SJason M. Bills                 {
2791d22c8396SJason M. Bills                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
2792002d39b4SEd Tanous                     if (ec.value() == boost::asio::error::invalid_argument)
2793d22c8396SJason M. Bills                     {
2794d22c8396SJason M. Bills                         messages::actionParameterNotSupported(
2795d22c8396SJason M. Bills                             asyncResp->res, resetType, "Reset");
2796d22c8396SJason M. Bills                     }
2797d22c8396SJason M. Bills                     else
2798d22c8396SJason M. Bills                     {
2799d22c8396SJason M. Bills                         messages::internalError(asyncResp->res);
2800d22c8396SJason M. Bills                     }
2801d22c8396SJason M. Bills                     return;
2802d22c8396SJason M. Bills                 }
2803d22c8396SJason M. Bills                 messages::success(asyncResp->res);
2804d22c8396SJason M. Bills                 },
2805d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis",
2806d22c8396SJason M. Bills                 "/xyz/openbmc_project/state/chassis0",
2807d22c8396SJason M. Bills                 "org.freedesktop.DBus.Properties", "Set",
2808002d39b4SEd Tanous                 "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition",
2809168e20c1SEd Tanous                 dbus::utility::DbusVariantType{command});
2810d22c8396SJason M. Bills         }
28117e860f15SJohn Edward Broadbent         });
2812d22c8396SJason M. Bills }
2813cc340dd9SEd Tanous 
2814cc340dd9SEd Tanous /**
28156617338dSEd Tanous  * Systems derived class for delivering Computer Systems Schema.
2816c5b2abe0SLewanczyk, Dawid  */
28177e860f15SJohn Edward Broadbent inline void requestRoutesSystems(App& app)
28181abe55efSEd Tanous {
2819c5b2abe0SLewanczyk, Dawid 
2820c5b2abe0SLewanczyk, Dawid     /**
2821c5b2abe0SLewanczyk, Dawid      * Functions triggers appropriate requests on DBus
2822c5b2abe0SLewanczyk, Dawid      */
28237e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/")
2824ed398213SEd Tanous         .privileges(redfish::privileges::getComputerSystem)
2825002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2826002d39b4SEd Tanous             [&app](const crow::Request& req,
2827002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2828*3ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
282945ca1b86SEd Tanous         {
283045ca1b86SEd Tanous             return;
283145ca1b86SEd Tanous         }
28328d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
283337bbf98cSChris Cain             "#ComputerSystem.v1_16_0.ComputerSystem";
28348d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "system";
28358d1b46d7Szhanghch05         asyncResp->res.jsonValue["Id"] = "system";
28368d1b46d7Szhanghch05         asyncResp->res.jsonValue["SystemType"] = "Physical";
28378d1b46d7Szhanghch05         asyncResp->res.jsonValue["Description"] = "Computer System";
28388d1b46d7Szhanghch05         asyncResp->res.jsonValue["ProcessorSummary"]["Count"] = 0;
28398d1b46d7Szhanghch05         asyncResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
28408d1b46d7Szhanghch05             "Disabled";
28418d1b46d7Szhanghch05         asyncResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] =
28428d1b46d7Szhanghch05             uint64_t(0);
28438d1b46d7Szhanghch05         asyncResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
28448d1b46d7Szhanghch05             "Disabled";
2845002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Systems/system";
284604a258f4SEd Tanous 
28471476687dSEd Tanous         asyncResp->res.jsonValue["Processors"]["@odata.id"] =
28481476687dSEd Tanous             "/redfish/v1/Systems/system/Processors";
28491476687dSEd Tanous         asyncResp->res.jsonValue["Memory"]["@odata.id"] =
28501476687dSEd Tanous             "/redfish/v1/Systems/system/Memory";
28511476687dSEd Tanous         asyncResp->res.jsonValue["Storage"]["@odata.id"] =
28521476687dSEd Tanous             "/redfish/v1/Systems/system/Storage";
2853029573d4SEd Tanous 
2854002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"]["target"] =
28551476687dSEd Tanous             "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset";
28561476687dSEd Tanous         asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"]
28571476687dSEd Tanous                                 ["@Redfish.ActionInfo"] =
28581476687dSEd Tanous             "/redfish/v1/Systems/system/ResetActionInfo";
2859c5b2abe0SLewanczyk, Dawid 
28601476687dSEd Tanous         asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
28611476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices";
28621476687dSEd Tanous         asyncResp->res.jsonValue["Bios"]["@odata.id"] =
28631476687dSEd Tanous             "/redfish/v1/Systems/system/Bios";
2864c4bf6374SJason M. Bills 
28651476687dSEd Tanous         nlohmann::json::array_t managedBy;
28661476687dSEd Tanous         nlohmann::json& manager = managedBy.emplace_back();
28671476687dSEd Tanous         manager["@odata.id"] = "/redfish/v1/Managers/bmc";
2868002d39b4SEd Tanous         asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy);
28691476687dSEd Tanous         asyncResp->res.jsonValue["Status"]["Health"] = "OK";
28701476687dSEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
28710e8ac5e7SGunnar Mills 
28720e8ac5e7SGunnar Mills         // Fill in SerialConsole info
2873002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
2874002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["IPMI"]["ServiceEnabled"] =
2875002d39b4SEd Tanous             true;
28761476687dSEd Tanous 
28770e8ac5e7SGunnar Mills         // TODO (Gunnar): Should look for obmc-console-ssh@2200.service
28781476687dSEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["SSH"]["ServiceEnabled"] =
28791476687dSEd Tanous             true;
28801476687dSEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["SSH"]["Port"] = 2200;
28811476687dSEd Tanous         asyncResp->res
28821476687dSEd Tanous             .jsonValue["SerialConsole"]["SSH"]["HotKeySequenceDisplay"] =
28831476687dSEd Tanous             "Press ~. to exit console";
28840e8ac5e7SGunnar Mills 
28850e8ac5e7SGunnar Mills #ifdef BMCWEB_ENABLE_KVM
28860e8ac5e7SGunnar Mills         // Fill in GraphicalConsole info
2887002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
2888002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
2889002d39b4SEd Tanous             4;
28901476687dSEd Tanous         asyncResp->res
2891002d39b4SEd Tanous             .jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
28921476687dSEd Tanous 
28930e8ac5e7SGunnar Mills #endif // BMCWEB_ENABLE_KVM
2894e284a7c1SJames Feist         constexpr const std::array<const char*, 4> inventoryForSystems = {
2895b49ac873SJames Feist             "xyz.openbmc_project.Inventory.Item.Dimm",
28962ad9c2f6SJames Feist             "xyz.openbmc_project.Inventory.Item.Cpu",
2897e284a7c1SJames Feist             "xyz.openbmc_project.Inventory.Item.Drive",
2898e284a7c1SJames Feist             "xyz.openbmc_project.Inventory.Item.StorageController"};
2899b49ac873SJames Feist 
2900b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
2901b49ac873SJames Feist         crow::connections::systemBus->async_method_call(
2902b49ac873SJames Feist             [health](const boost::system::error_code ec,
2903914e2d5dSEd Tanous                      const std::vector<std::string>& resp) {
2904b49ac873SJames Feist             if (ec)
2905b49ac873SJames Feist             {
2906b49ac873SJames Feist                 // no inventory
2907b49ac873SJames Feist                 return;
2908b49ac873SJames Feist             }
2909b49ac873SJames Feist 
2910914e2d5dSEd Tanous             health->inventory = resp;
2911b49ac873SJames Feist             },
2912b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper",
2913b49ac873SJames Feist             "/xyz/openbmc_project/object_mapper",
2914b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
2915b49ac873SJames Feist             int32_t(0), inventoryForSystems);
2916b49ac873SJames Feist 
2917b49ac873SJames Feist         health->populate();
2918b49ac873SJames Feist 
2919002d39b4SEd Tanous         getMainChassisId(asyncResp,
2920002d39b4SEd Tanous                          [](const std::string& chassisId,
29218d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2922b2c7e208SEd Tanous             nlohmann::json::array_t chassisArray;
2923b2c7e208SEd Tanous             nlohmann::json& chassis = chassisArray.emplace_back();
2924b2c7e208SEd Tanous             chassis["@odata.id"] = "/redfish/v1/Chassis/" + chassisId;
2925002d39b4SEd Tanous             aRsp->res.jsonValue["Links"]["Chassis"] = std::move(chassisArray);
2926c5d03ff4SJennifer Lee         });
2927a3002228SAppaRao Puli 
29289f8bfa7cSGunnar Mills         getLocationIndicatorActive(asyncResp);
29299f8bfa7cSGunnar Mills         // TODO (Gunnar): Remove IndicatorLED after enough time has passed
2930a3002228SAppaRao Puli         getIndicatorLedState(asyncResp);
29315bc2dc8eSJames Feist         getComputerSystem(asyncResp, health);
29326c34de48SEd Tanous         getHostState(asyncResp);
2933491d8ee7SSantosh Puranik         getBootProperties(asyncResp);
2934978b8803SAndrew Geissler         getBootProgress(asyncResp);
2935adbe192aSJason M. Bills         getPCIeDeviceList(asyncResp, "PCIeDevices");
293651709ffdSYong Li         getHostWatchdogTimer(asyncResp);
2937c6a620f2SGeorge Liu         getPowerRestorePolicy(asyncResp);
29386bd5a8d2SGunnar Mills         getAutomaticRetry(asyncResp);
2939c0557e1aSGunnar Mills         getLastResetTime(asyncResp);
2940a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
2941a6349918SAppaRao Puli         getProvisioningStatus(asyncResp);
2942a6349918SAppaRao Puli #endif
29431981771bSAli Ahmed         getTrustedModuleRequiredToBoot(asyncResp);
29443a2d0424SChris Cain         getPowerMode(asyncResp);
294537bbf98cSChris Cain         getIdlePowerSaver(asyncResp);
29467e860f15SJohn Edward Broadbent         });
2947550a6bf8SJiaqing Zhao 
29487e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/")
2949ed398213SEd Tanous         .privileges(redfish::privileges::patchComputerSystem)
29507e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
295145ca1b86SEd Tanous             [&app](const crow::Request& req,
29527e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2953*3ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
295445ca1b86SEd Tanous         {
295545ca1b86SEd Tanous             return;
295645ca1b86SEd Tanous         }
29579f8bfa7cSGunnar Mills         std::optional<bool> locationIndicatorActive;
2958cde19e5fSSantosh Puranik         std::optional<std::string> indicatorLed;
295998e386ecSGunnar Mills         std::optional<std::string> assetTag;
2960c6a620f2SGeorge Liu         std::optional<std::string> powerRestorePolicy;
29613a2d0424SChris Cain         std::optional<std::string> powerMode;
2962550a6bf8SJiaqing Zhao         std::optional<bool> wdtEnable;
2963550a6bf8SJiaqing Zhao         std::optional<std::string> wdtTimeOutAction;
2964550a6bf8SJiaqing Zhao         std::optional<std::string> bootSource;
2965550a6bf8SJiaqing Zhao         std::optional<std::string> bootType;
2966550a6bf8SJiaqing Zhao         std::optional<std::string> bootEnable;
2967550a6bf8SJiaqing Zhao         std::optional<std::string> bootAutomaticRetry;
2968550a6bf8SJiaqing Zhao         std::optional<bool> bootTrustedModuleRequired;
2969550a6bf8SJiaqing Zhao         std::optional<bool> ipsEnable;
2970550a6bf8SJiaqing Zhao         std::optional<uint8_t> ipsEnterUtil;
2971550a6bf8SJiaqing Zhao         std::optional<uint64_t> ipsEnterTime;
2972550a6bf8SJiaqing Zhao         std::optional<uint8_t> ipsExitUtil;
2973550a6bf8SJiaqing Zhao         std::optional<uint64_t> ipsExitTime;
2974550a6bf8SJiaqing Zhao 
2975550a6bf8SJiaqing Zhao         // clang-format off
297615ed6780SWilly Tu         if (!json_util::readJsonPatch(
2977550a6bf8SJiaqing Zhao                 req, asyncResp->res,
2978550a6bf8SJiaqing Zhao                 "IndicatorLED", indicatorLed,
29797e860f15SJohn Edward Broadbent                 "LocationIndicatorActive", locationIndicatorActive,
2980550a6bf8SJiaqing Zhao                 "AssetTag", assetTag,
2981550a6bf8SJiaqing Zhao                 "PowerRestorePolicy", powerRestorePolicy,
2982550a6bf8SJiaqing Zhao                 "PowerMode", powerMode,
2983550a6bf8SJiaqing Zhao                 "HostWatchdogTimer/FunctionEnabled", wdtEnable,
2984550a6bf8SJiaqing Zhao                 "HostWatchdogTimer/TimeoutAction", wdtTimeOutAction,
2985550a6bf8SJiaqing Zhao                 "Boot/BootSourceOverrideTarget", bootSource,
2986550a6bf8SJiaqing Zhao                 "Boot/BootSourceOverrideMode", bootType,
2987550a6bf8SJiaqing Zhao                 "Boot/BootSourceOverrideEnabled", bootEnable,
2988550a6bf8SJiaqing Zhao                 "Boot/AutomaticRetryConfig", bootAutomaticRetry,
2989550a6bf8SJiaqing Zhao                 "Boot/TrustedModuleRequiredToBoot", bootTrustedModuleRequired,
2990550a6bf8SJiaqing Zhao                 "IdlePowerSaver/Enabled", ipsEnable,
2991550a6bf8SJiaqing Zhao                 "IdlePowerSaver/EnterUtilizationPercent", ipsEnterUtil,
2992550a6bf8SJiaqing Zhao                 "IdlePowerSaver/EnterDwellTimeSeconds", ipsEnterTime,
2993550a6bf8SJiaqing Zhao                 "IdlePowerSaver/ExitUtilizationPercent", ipsExitUtil,
2994550a6bf8SJiaqing Zhao                 "IdlePowerSaver/ExitDwellTimeSeconds", ipsExitTime))
29956617338dSEd Tanous         {
29966617338dSEd Tanous             return;
29976617338dSEd Tanous         }
2998550a6bf8SJiaqing Zhao         // clang-format on
2999491d8ee7SSantosh Puranik 
30008d1b46d7Szhanghch05         asyncResp->res.result(boost::beast::http::status::no_content);
3001c45f0082SYong Li 
300298e386ecSGunnar Mills         if (assetTag)
300398e386ecSGunnar Mills         {
300498e386ecSGunnar Mills             setAssetTag(asyncResp, *assetTag);
300598e386ecSGunnar Mills         }
300698e386ecSGunnar Mills 
3007550a6bf8SJiaqing Zhao         if (wdtEnable || wdtTimeOutAction)
3008c45f0082SYong Li         {
3009f23b7296SEd Tanous             setWDTProperties(asyncResp, wdtEnable, wdtTimeOutAction);
3010c45f0082SYong Li         }
3011c45f0082SYong Li 
3012cd9a4666SKonstantin Aladyshev         if (bootSource || bootType || bootEnable)
301369f35306SGunnar Mills         {
3014002d39b4SEd Tanous             setBootProperties(asyncResp, bootSource, bootType, bootEnable);
3015491d8ee7SSantosh Puranik         }
3016550a6bf8SJiaqing Zhao         if (bootAutomaticRetry)
301769f35306SGunnar Mills         {
3018550a6bf8SJiaqing Zhao             setAutomaticRetry(asyncResp, *bootAutomaticRetry);
301969f35306SGunnar Mills         }
3020ac7e1e0bSAli Ahmed 
3021550a6bf8SJiaqing Zhao         if (bootTrustedModuleRequired)
3022ac7e1e0bSAli Ahmed         {
3023550a6bf8SJiaqing Zhao             setTrustedModuleRequiredToBoot(asyncResp,
3024550a6bf8SJiaqing Zhao                                            *bootTrustedModuleRequired);
302569f35306SGunnar Mills         }
3026265c1602SJohnathan Mantey 
30279f8bfa7cSGunnar Mills         if (locationIndicatorActive)
30289f8bfa7cSGunnar Mills         {
3029002d39b4SEd Tanous             setLocationIndicatorActive(asyncResp, *locationIndicatorActive);
30309f8bfa7cSGunnar Mills         }
30319f8bfa7cSGunnar Mills 
30327e860f15SJohn Edward Broadbent         // TODO (Gunnar): Remove IndicatorLED after enough time has
30337e860f15SJohn Edward Broadbent         // passed
30349712f8acSEd Tanous         if (indicatorLed)
30356617338dSEd Tanous         {
3036f23b7296SEd Tanous             setIndicatorLedState(asyncResp, *indicatorLed);
3037002d39b4SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::warning,
3038d6aa0093SGunnar Mills                                      "299 - \"IndicatorLED is deprecated. Use "
3039d6aa0093SGunnar Mills                                      "LocationIndicatorActive instead.\"");
30406617338dSEd Tanous         }
3041c6a620f2SGeorge Liu 
3042c6a620f2SGeorge Liu         if (powerRestorePolicy)
3043c6a620f2SGeorge Liu         {
30444e69c904SGunnar Mills             setPowerRestorePolicy(asyncResp, *powerRestorePolicy);
3045c6a620f2SGeorge Liu         }
30463a2d0424SChris Cain 
30473a2d0424SChris Cain         if (powerMode)
30483a2d0424SChris Cain         {
30493a2d0424SChris Cain             setPowerMode(asyncResp, *powerMode);
30503a2d0424SChris Cain         }
305137bbf98cSChris Cain 
3052550a6bf8SJiaqing Zhao         if (ipsEnable || ipsEnterUtil || ipsEnterTime || ipsExitUtil ||
3053550a6bf8SJiaqing Zhao             ipsExitTime)
305437bbf98cSChris Cain         {
3055002d39b4SEd Tanous             setIdlePowerSaver(asyncResp, ipsEnable, ipsEnterUtil, ipsEnterTime,
3056002d39b4SEd Tanous                               ipsExitUtil, ipsExitTime);
305737bbf98cSChris Cain         }
30587e860f15SJohn Edward Broadbent         });
3059c5b2abe0SLewanczyk, Dawid }
30601cb1a9e6SAppaRao Puli 
30611cb1a9e6SAppaRao Puli /**
30621cb1a9e6SAppaRao Puli  * SystemResetActionInfo derived class for delivering Computer Systems
30631cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
30641cb1a9e6SAppaRao Puli  */
30657e860f15SJohn Edward Broadbent inline void requestRoutesSystemResetActionInfo(App& app)
30661cb1a9e6SAppaRao Puli {
30671cb1a9e6SAppaRao Puli     /**
30681cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
30691cb1a9e6SAppaRao Puli      */
30707e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/ResetActionInfo/")
3071ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
30727e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
307345ca1b86SEd Tanous             [&app](const crow::Request& req,
30747e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3075*3ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
307645ca1b86SEd Tanous         {
307745ca1b86SEd Tanous             return;
307845ca1b86SEd Tanous         }
30791476687dSEd Tanous 
30801476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
30811476687dSEd Tanous             "/redfish/v1/Systems/system/ResetActionInfo";
30821476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
30831476687dSEd Tanous             "#ActionInfo.v1_1_2.ActionInfo";
30841476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "Reset Action Info";
30851476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
30863215e700SNan Zhou 
30873215e700SNan Zhou         nlohmann::json::array_t parameters;
30883215e700SNan Zhou         nlohmann::json::object_t parameter;
30893215e700SNan Zhou 
30903215e700SNan Zhou         parameter["Name"] = "ResetType";
30913215e700SNan Zhou         parameter["Required"] = true;
30923215e700SNan Zhou         parameter["DataType"] = "String";
30933215e700SNan Zhou         nlohmann::json::array_t allowableValues;
30943215e700SNan Zhou         allowableValues.emplace_back("On");
30953215e700SNan Zhou         allowableValues.emplace_back("ForceOff");
30963215e700SNan Zhou         allowableValues.emplace_back("ForceOn");
30973215e700SNan Zhou         allowableValues.emplace_back("ForceRestart");
30983215e700SNan Zhou         allowableValues.emplace_back("GracefulRestart");
30993215e700SNan Zhou         allowableValues.emplace_back("GracefulShutdown");
31003215e700SNan Zhou         allowableValues.emplace_back("PowerCycle");
31013215e700SNan Zhou         allowableValues.emplace_back("Nmi");
31023215e700SNan Zhou         parameter["AllowableValues"] = std::move(allowableValues);
31033215e700SNan Zhou         parameters.emplace_back(std::move(parameter));
31043215e700SNan Zhou 
31053215e700SNan Zhou         asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
31067e860f15SJohn Edward Broadbent         });
31071cb1a9e6SAppaRao Puli }
3108c5b2abe0SLewanczyk, Dawid } // namespace redfish
3109