xref: /openbmc/bmcweb/features/redfish/lib/systems.hpp (revision 02cad96e3a83b0c9a01dbc87219c1b8b6681fa67)
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>
30c5b2abe0SLewanczyk, Dawid #include <utils/json_utils.hpp>
31eee0013eSWilly Tu #include <utils/sw_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.
55*02cad96eSEd Tanous     const 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 
110*02cad96eSEd Tanous     const 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"];
329*02cad96eSEd Tanous                                     const uint64_t* preValue =
330*02cad96eSEd Tanous                                         totalMemory.get_ptr<const 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
469eee0013eSWilly Tu                             sw_util::populateSoftwareInformation(
470eee0013eSWilly Tu                                 aResp, sw_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;
9335ef735c8SNan Zhou             if (ec.value() == boost::asio::error::host_unreachable)
9345ef735c8SNan Zhou             {
9355ef735c8SNan Zhou                 return;
9365ef735c8SNan Zhou             }
937491d8ee7SSantosh Puranik             messages::internalError(aResp->res);
938491d8ee7SSantosh Puranik             return;
939491d8ee7SSantosh Puranik         }
940491d8ee7SSantosh Puranik 
9411e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Boot source: " << bootSourceStr;
942491d8ee7SSantosh Puranik 
9431e1e598dSJonathan Doman         auto rfSource = dbusToRfBootSource(bootSourceStr);
944491d8ee7SSantosh Puranik         if (!rfSource.empty())
945491d8ee7SSantosh Puranik         {
946002d39b4SEd Tanous             aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] = rfSource;
947491d8ee7SSantosh Puranik         }
948cd9a4666SKonstantin Aladyshev 
949cd9a4666SKonstantin Aladyshev         // Get BootMode as BootSourceOverrideTarget is constructed
950cd9a4666SKonstantin Aladyshev         // from both BootSource and BootMode
951c21865c4SKonstantin Aladyshev         getBootOverrideMode(aResp);
9521e1e598dSJonathan Doman         });
953491d8ee7SSantosh Puranik }
954491d8ee7SSantosh Puranik 
955491d8ee7SSantosh Puranik /**
956c21865c4SKonstantin Aladyshev  * @brief This functions abstracts all the logic behind getting a
957c21865c4SKonstantin Aladyshev  * "BootSourceOverrideEnabled" property from an overall boot override enable
958c21865c4SKonstantin Aladyshev  * state
959491d8ee7SSantosh Puranik  *
960491d8ee7SSantosh Puranik  * @param[in] aResp     Shared pointer for generating response message.
961491d8ee7SSantosh Puranik  *
962491d8ee7SSantosh Puranik  * @return None.
963491d8ee7SSantosh Puranik  */
964491d8ee7SSantosh Puranik 
965c21865c4SKonstantin Aladyshev inline void
966c21865c4SKonstantin Aladyshev     processBootOverrideEnable(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
967c21865c4SKonstantin Aladyshev                               const bool bootOverrideEnableSetting)
968c21865c4SKonstantin Aladyshev {
969c21865c4SKonstantin Aladyshev     if (!bootOverrideEnableSetting)
970c21865c4SKonstantin Aladyshev     {
971c21865c4SKonstantin Aladyshev         aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] = "Disabled";
972c21865c4SKonstantin Aladyshev         return;
973c21865c4SKonstantin Aladyshev     }
974c21865c4SKonstantin Aladyshev 
975c21865c4SKonstantin Aladyshev     // If boot source override is enabled, we need to check 'one_time'
976c21865c4SKonstantin Aladyshev     // property to set a correct value for the "BootSourceOverrideEnabled"
9771e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
9781e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
9791e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot/one_time",
9801e1e598dSJonathan Doman         "xyz.openbmc_project.Object.Enable", "Enabled",
9811e1e598dSJonathan Doman         [aResp](const boost::system::error_code ec, bool oneTimeSetting) {
982491d8ee7SSantosh Puranik         if (ec)
983491d8ee7SSantosh Puranik         {
984491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
985c21865c4SKonstantin Aladyshev             messages::internalError(aResp->res);
986491d8ee7SSantosh Puranik             return;
987491d8ee7SSantosh Puranik         }
988491d8ee7SSantosh Puranik 
989c21865c4SKonstantin Aladyshev         if (oneTimeSetting)
990c21865c4SKonstantin Aladyshev         {
991002d39b4SEd Tanous             aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] = "Once";
992c21865c4SKonstantin Aladyshev         }
993c21865c4SKonstantin Aladyshev         else
994c21865c4SKonstantin Aladyshev         {
995c21865c4SKonstantin Aladyshev             aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
996c21865c4SKonstantin Aladyshev                 "Continuous";
997c21865c4SKonstantin Aladyshev         }
9981e1e598dSJonathan Doman         });
999491d8ee7SSantosh Puranik }
1000491d8ee7SSantosh Puranik 
1001491d8ee7SSantosh Puranik /**
1002c21865c4SKonstantin Aladyshev  * @brief Retrieves boot override enable over DBUS
1003c21865c4SKonstantin Aladyshev  *
1004c21865c4SKonstantin Aladyshev  * @param[in] aResp     Shared pointer for generating response message.
1005c21865c4SKonstantin Aladyshev  *
1006c21865c4SKonstantin Aladyshev  * @return None.
1007c21865c4SKonstantin Aladyshev  */
1008c21865c4SKonstantin Aladyshev 
1009c21865c4SKonstantin Aladyshev inline void
1010c21865c4SKonstantin Aladyshev     getBootOverrideEnable(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1011c21865c4SKonstantin Aladyshev {
10121e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
10131e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
10141e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/boot",
10151e1e598dSJonathan Doman         "xyz.openbmc_project.Object.Enable", "Enabled",
1016c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec,
10171e1e598dSJonathan Doman                 const bool bootOverrideEnable) {
1018c21865c4SKonstantin Aladyshev         if (ec)
1019c21865c4SKonstantin Aladyshev         {
1020c21865c4SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
10215ef735c8SNan Zhou             if (ec.value() == boost::asio::error::host_unreachable)
10225ef735c8SNan Zhou             {
10235ef735c8SNan Zhou                 return;
10245ef735c8SNan Zhou             }
1025c21865c4SKonstantin Aladyshev             messages::internalError(aResp->res);
1026c21865c4SKonstantin Aladyshev             return;
1027c21865c4SKonstantin Aladyshev         }
1028c21865c4SKonstantin Aladyshev 
10291e1e598dSJonathan Doman         processBootOverrideEnable(aResp, bootOverrideEnable);
10301e1e598dSJonathan Doman         });
1031c21865c4SKonstantin Aladyshev }
1032c21865c4SKonstantin Aladyshev 
1033c21865c4SKonstantin Aladyshev /**
1034c21865c4SKonstantin Aladyshev  * @brief Retrieves boot source override properties
1035c21865c4SKonstantin Aladyshev  *
1036c21865c4SKonstantin Aladyshev  * @param[in] aResp     Shared pointer for generating response message.
1037c21865c4SKonstantin Aladyshev  *
1038c21865c4SKonstantin Aladyshev  * @return None.
1039c21865c4SKonstantin Aladyshev  */
1040c21865c4SKonstantin Aladyshev inline void getBootProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1041c21865c4SKonstantin Aladyshev {
1042c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Get boot information.";
1043c21865c4SKonstantin Aladyshev 
1044c21865c4SKonstantin Aladyshev     getBootOverrideSource(aResp);
1045c21865c4SKonstantin Aladyshev     getBootOverrideType(aResp);
1046c21865c4SKonstantin Aladyshev     getBootOverrideEnable(aResp);
1047c21865c4SKonstantin Aladyshev }
1048c21865c4SKonstantin Aladyshev 
1049c21865c4SKonstantin Aladyshev /**
1050c0557e1aSGunnar Mills  * @brief Retrieves the Last Reset Time
1051c0557e1aSGunnar Mills  *
1052c0557e1aSGunnar Mills  * "Reset" is an overloaded term in Redfish, "Reset" includes power on
1053c0557e1aSGunnar Mills  * and power off. Even though this is the "system" Redfish object look at the
1054c0557e1aSGunnar Mills  * chassis D-Bus interface for the LastStateChangeTime since this has the
1055c0557e1aSGunnar Mills  * last power operation time.
1056c0557e1aSGunnar Mills  *
1057c0557e1aSGunnar Mills  * @param[in] aResp     Shared pointer for generating response message.
1058c0557e1aSGunnar Mills  *
1059c0557e1aSGunnar Mills  * @return None.
1060c0557e1aSGunnar Mills  */
10618d1b46d7Szhanghch05 inline void getLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1062c0557e1aSGunnar Mills {
1063c0557e1aSGunnar Mills     BMCWEB_LOG_DEBUG << "Getting System Last Reset Time";
1064c0557e1aSGunnar Mills 
10651e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
10661e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Chassis",
10671e1e598dSJonathan Doman         "/xyz/openbmc_project/state/chassis0",
10681e1e598dSJonathan Doman         "xyz.openbmc_project.State.Chassis", "LastStateChangeTime",
10691e1e598dSJonathan Doman         [aResp](const boost::system::error_code ec, uint64_t lastResetTime) {
1070c0557e1aSGunnar Mills         if (ec)
1071c0557e1aSGunnar Mills         {
1072c0557e1aSGunnar Mills             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
1073c0557e1aSGunnar Mills             return;
1074c0557e1aSGunnar Mills         }
1075c0557e1aSGunnar Mills 
1076c0557e1aSGunnar Mills         // LastStateChangeTime is epoch time, in milliseconds
1077c0557e1aSGunnar Mills         // https://github.com/openbmc/phosphor-dbus-interfaces/blob/33e8e1dd64da53a66e888d33dc82001305cd0bf9/xyz/openbmc_project/State/Chassis.interface.yaml#L19
10781e1e598dSJonathan Doman         uint64_t lastResetTimeStamp = lastResetTime / 1000;
1079c0557e1aSGunnar Mills 
1080c0557e1aSGunnar Mills         // Convert to ISO 8601 standard
1081c0557e1aSGunnar Mills         aResp->res.jsonValue["LastResetTime"] =
10821d8782e7SNan Zhou             crow::utility::getDateTimeUint(lastResetTimeStamp);
10831e1e598dSJonathan Doman         });
1084c0557e1aSGunnar Mills }
1085c0557e1aSGunnar Mills 
1086c0557e1aSGunnar Mills /**
10876bd5a8d2SGunnar Mills  * @brief Retrieves Automatic Retry properties. Known on D-Bus as AutoReboot.
10886bd5a8d2SGunnar Mills  *
10896bd5a8d2SGunnar Mills  * @param[in] aResp     Shared pointer for generating response message.
10906bd5a8d2SGunnar Mills  *
10916bd5a8d2SGunnar Mills  * @return None.
10926bd5a8d2SGunnar Mills  */
10938d1b46d7Szhanghch05 inline void getAutomaticRetry(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
10946bd5a8d2SGunnar Mills {
10956bd5a8d2SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Automatic Retry policy";
10966bd5a8d2SGunnar Mills 
10971e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
10981e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
10991e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/auto_reboot",
11001e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
11011e1e598dSJonathan Doman         [aResp](const boost::system::error_code ec, bool autoRebootEnabled) {
11026bd5a8d2SGunnar Mills         if (ec)
11036bd5a8d2SGunnar Mills         {
11046bd5a8d2SGunnar Mills             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
11056bd5a8d2SGunnar Mills             return;
11066bd5a8d2SGunnar Mills         }
11076bd5a8d2SGunnar Mills 
11081e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Auto Reboot: " << autoRebootEnabled;
1109e05aec50SEd Tanous         if (autoRebootEnabled)
11106bd5a8d2SGunnar Mills         {
11116bd5a8d2SGunnar Mills             aResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
11126bd5a8d2SGunnar Mills                 "RetryAttempts";
11136bd5a8d2SGunnar Mills             // If AutomaticRetry (AutoReboot) is enabled see how many
11146bd5a8d2SGunnar Mills             // attempts are left
11151e1e598dSJonathan Doman             sdbusplus::asio::getProperty<uint32_t>(
1116002d39b4SEd Tanous                 *crow::connections::systemBus, "xyz.openbmc_project.State.Host",
11171e1e598dSJonathan Doman                 "/xyz/openbmc_project/state/host0",
11181e1e598dSJonathan Doman                 "xyz.openbmc_project.Control.Boot.RebootAttempts",
11191e1e598dSJonathan Doman                 "AttemptsLeft",
1120cb13a392SEd Tanous                 [aResp](const boost::system::error_code ec2,
1121914e2d5dSEd Tanous                         const uint32_t autoRebootAttemptsLeft) {
1122cb13a392SEd Tanous                 if (ec2)
11236bd5a8d2SGunnar Mills                 {
1124cb13a392SEd Tanous                     BMCWEB_LOG_DEBUG << "D-BUS response error " << ec2;
11256bd5a8d2SGunnar Mills                     return;
11266bd5a8d2SGunnar Mills                 }
11276bd5a8d2SGunnar Mills 
11286bd5a8d2SGunnar Mills                 BMCWEB_LOG_DEBUG << "Auto Reboot Attempts Left: "
11291e1e598dSJonathan Doman                                  << autoRebootAttemptsLeft;
11306bd5a8d2SGunnar Mills 
11316bd5a8d2SGunnar Mills                 aResp->res
1132002d39b4SEd Tanous                     .jsonValue["Boot"]["RemainingAutomaticRetryAttempts"] =
11331e1e598dSJonathan Doman                     autoRebootAttemptsLeft;
11341e1e598dSJonathan Doman                 });
11356bd5a8d2SGunnar Mills         }
11366bd5a8d2SGunnar Mills         else
11376bd5a8d2SGunnar Mills         {
1138002d39b4SEd Tanous             aResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] = "Disabled";
11396bd5a8d2SGunnar Mills         }
11406bd5a8d2SGunnar Mills 
11416bd5a8d2SGunnar Mills         // Not on D-Bus. Hardcoded here:
11426bd5a8d2SGunnar Mills         // https://github.com/openbmc/phosphor-state-manager/blob/1dbbef42675e94fb1f78edb87d6b11380260535a/meson_options.txt#L71
11436bd5a8d2SGunnar Mills         aResp->res.jsonValue["Boot"]["AutomaticRetryAttempts"] = 3;
114469f35306SGunnar Mills 
114569f35306SGunnar Mills         // "AutomaticRetryConfig" can be 3 values, Disabled, RetryAlways,
114669f35306SGunnar Mills         // and RetryAttempts. OpenBMC only supports Disabled and
114769f35306SGunnar Mills         // RetryAttempts.
1148002d39b4SEd Tanous         aResp->res.jsonValue["Boot"]
11490fda0f12SGeorge Liu                             ["AutomaticRetryConfig@Redfish.AllowableValues"] = {
11500fda0f12SGeorge Liu             "Disabled", "RetryAttempts"};
11511e1e598dSJonathan Doman         });
11526bd5a8d2SGunnar Mills }
11536bd5a8d2SGunnar Mills 
11546bd5a8d2SGunnar Mills /**
1155c6a620f2SGeorge Liu  * @brief Retrieves power restore policy over DBUS.
1156c6a620f2SGeorge Liu  *
1157c6a620f2SGeorge Liu  * @param[in] aResp     Shared pointer for generating response message.
1158c6a620f2SGeorge Liu  *
1159c6a620f2SGeorge Liu  * @return None.
1160c6a620f2SGeorge Liu  */
11618d1b46d7Szhanghch05 inline void
11628d1b46d7Szhanghch05     getPowerRestorePolicy(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1163c6a620f2SGeorge Liu {
1164c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Get power restore policy";
1165c6a620f2SGeorge Liu 
11661e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
11671e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
11681e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/power_restore_policy",
11691e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
11701e1e598dSJonathan Doman         [aResp](const boost::system::error_code ec, const std::string& policy) {
1171c6a620f2SGeorge Liu         if (ec)
1172c6a620f2SGeorge Liu         {
1173c6a620f2SGeorge Liu             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1174c6a620f2SGeorge Liu             return;
1175c6a620f2SGeorge Liu         }
1176c6a620f2SGeorge Liu 
11770fda0f12SGeorge Liu         const boost::container::flat_map<std::string, std::string> policyMaps = {
11780fda0f12SGeorge Liu             {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn",
1179c6a620f2SGeorge Liu              "AlwaysOn"},
11800fda0f12SGeorge Liu             {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff",
1181c6a620f2SGeorge Liu              "AlwaysOff"},
11820fda0f12SGeorge Liu             {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore",
11834ed47cb8SMatthew Barth              "LastState"},
11844ed47cb8SMatthew Barth             // Return `AlwaysOff` when power restore policy set to "None"
11854ed47cb8SMatthew Barth             {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy.None",
11864ed47cb8SMatthew Barth              "AlwaysOff"}};
1187c6a620f2SGeorge Liu 
11881e1e598dSJonathan Doman         auto policyMapsIt = policyMaps.find(policy);
1189c6a620f2SGeorge Liu         if (policyMapsIt == policyMaps.end())
1190c6a620f2SGeorge Liu         {
1191c6a620f2SGeorge Liu             messages::internalError(aResp->res);
1192c6a620f2SGeorge Liu             return;
1193c6a620f2SGeorge Liu         }
1194c6a620f2SGeorge Liu 
1195c6a620f2SGeorge Liu         aResp->res.jsonValue["PowerRestorePolicy"] = policyMapsIt->second;
11961e1e598dSJonathan Doman         });
1197c6a620f2SGeorge Liu }
1198c6a620f2SGeorge Liu 
1199c6a620f2SGeorge Liu /**
12001981771bSAli Ahmed  * @brief Get TrustedModuleRequiredToBoot property. Determines whether or not
12011981771bSAli Ahmed  * TPM is required for booting the host.
12021981771bSAli Ahmed  *
12031981771bSAli Ahmed  * @param[in] aResp     Shared pointer for generating response message.
12041981771bSAli Ahmed  *
12051981771bSAli Ahmed  * @return None.
12061981771bSAli Ahmed  */
12071981771bSAli Ahmed inline void getTrustedModuleRequiredToBoot(
12081981771bSAli Ahmed     const std::shared_ptr<bmcweb::AsyncResp>& aResp)
12091981771bSAli Ahmed {
12101981771bSAli Ahmed     BMCWEB_LOG_DEBUG << "Get TPM required to boot.";
12111981771bSAli Ahmed 
12121981771bSAli Ahmed     crow::connections::systemBus->async_method_call(
1213b9d36b47SEd Tanous         [aResp](const boost::system::error_code ec,
1214b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
12151981771bSAli Ahmed         if (ec)
12161981771bSAli Ahmed         {
1217002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on TPM.Policy GetSubTree"
1218002d39b4SEd Tanous                              << ec;
12191981771bSAli Ahmed             // This is an optional D-Bus object so just return if
12201981771bSAli Ahmed             // error occurs
12211981771bSAli Ahmed             return;
12221981771bSAli Ahmed         }
122326f6976fSEd Tanous         if (subtree.empty())
12241981771bSAli Ahmed         {
12251981771bSAli Ahmed             // As noted above, this is an optional interface so just return
12261981771bSAli Ahmed             // if there is no instance found
12271981771bSAli Ahmed             return;
12281981771bSAli Ahmed         }
12291981771bSAli Ahmed 
12301981771bSAli Ahmed         /* When there is more than one TPMEnable object... */
12311981771bSAli Ahmed         if (subtree.size() > 1)
12321981771bSAli Ahmed         {
12331981771bSAli Ahmed             BMCWEB_LOG_DEBUG
12341981771bSAli Ahmed                 << "DBUS response has more than 1 TPM Enable object:"
12351981771bSAli Ahmed                 << subtree.size();
12361981771bSAli Ahmed             // Throw an internal Error and return
12371981771bSAli Ahmed             messages::internalError(aResp->res);
12381981771bSAli Ahmed             return;
12391981771bSAli Ahmed         }
12401981771bSAli Ahmed 
12411981771bSAli Ahmed         // Make sure the Dbus response map has a service and objectPath
12421981771bSAli Ahmed         // field
12431981771bSAli Ahmed         if (subtree[0].first.empty() || subtree[0].second.size() != 1)
12441981771bSAli Ahmed         {
12451981771bSAli Ahmed             BMCWEB_LOG_DEBUG << "TPM.Policy mapper error!";
12461981771bSAli Ahmed             messages::internalError(aResp->res);
12471981771bSAli Ahmed             return;
12481981771bSAli Ahmed         }
12491981771bSAli Ahmed 
12501981771bSAli Ahmed         const std::string& path = subtree[0].first;
12511981771bSAli Ahmed         const std::string& serv = subtree[0].second.begin()->first;
12521981771bSAli Ahmed 
12531981771bSAli Ahmed         // Valid TPM Enable object found, now reading the current value
12541e1e598dSJonathan Doman         sdbusplus::asio::getProperty<bool>(
12551e1e598dSJonathan Doman             *crow::connections::systemBus, serv, path,
12561e1e598dSJonathan Doman             "xyz.openbmc_project.Control.TPM.Policy", "TPMEnable",
12578a592810SEd Tanous             [aResp](const boost::system::error_code ec2, bool tpmRequired) {
12588a592810SEd Tanous             if (ec2)
12591981771bSAli Ahmed             {
1260002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "D-BUS response error on TPM.Policy Get"
12618a592810SEd Tanous                                  << ec2;
12621981771bSAli Ahmed                 messages::internalError(aResp->res);
12631981771bSAli Ahmed                 return;
12641981771bSAli Ahmed             }
12651981771bSAli Ahmed 
12661e1e598dSJonathan Doman             if (tpmRequired)
12671981771bSAli Ahmed             {
1268002d39b4SEd Tanous                 aResp->res.jsonValue["Boot"]["TrustedModuleRequiredToBoot"] =
12691981771bSAli Ahmed                     "Required";
12701981771bSAli Ahmed             }
12711981771bSAli Ahmed             else
12721981771bSAli Ahmed             {
1273002d39b4SEd Tanous                 aResp->res.jsonValue["Boot"]["TrustedModuleRequiredToBoot"] =
12741981771bSAli Ahmed                     "Disabled";
12751981771bSAli Ahmed             }
12761e1e598dSJonathan Doman             });
12771981771bSAli Ahmed         },
12781981771bSAli Ahmed         "xyz.openbmc_project.ObjectMapper",
12791981771bSAli Ahmed         "/xyz/openbmc_project/object_mapper",
12801981771bSAli Ahmed         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
12811981771bSAli Ahmed         std::array<const char*, 1>{"xyz.openbmc_project.Control.TPM.Policy"});
12821981771bSAli Ahmed }
12831981771bSAli Ahmed 
12841981771bSAli Ahmed /**
12851c05dae3SAli Ahmed  * @brief Set TrustedModuleRequiredToBoot property. Determines whether or not
12861c05dae3SAli Ahmed  * TPM is required for booting the host.
12871c05dae3SAli Ahmed  *
12881c05dae3SAli Ahmed  * @param[in] aResp         Shared pointer for generating response message.
12891c05dae3SAli Ahmed  * @param[in] tpmRequired   Value to set TPM Required To Boot property to.
12901c05dae3SAli Ahmed  *
12911c05dae3SAli Ahmed  * @return None.
12921c05dae3SAli Ahmed  */
12931c05dae3SAli Ahmed inline void setTrustedModuleRequiredToBoot(
12941c05dae3SAli Ahmed     const std::shared_ptr<bmcweb::AsyncResp>& aResp, const bool tpmRequired)
12951c05dae3SAli Ahmed {
12961c05dae3SAli Ahmed     BMCWEB_LOG_DEBUG << "Set TrustedModuleRequiredToBoot.";
12971c05dae3SAli Ahmed 
12981c05dae3SAli Ahmed     crow::connections::systemBus->async_method_call(
1299b9d36b47SEd Tanous         [aResp, tpmRequired](const boost::system::error_code ec,
1300b9d36b47SEd Tanous                              dbus::utility::MapperGetSubTreeResponse& subtree) {
13011c05dae3SAli Ahmed         if (ec)
13021c05dae3SAli Ahmed         {
1303002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on TPM.Policy GetSubTree"
1304002d39b4SEd Tanous                              << ec;
13051c05dae3SAli Ahmed             messages::internalError(aResp->res);
13061c05dae3SAli Ahmed             return;
13071c05dae3SAli Ahmed         }
130826f6976fSEd Tanous         if (subtree.empty())
13091c05dae3SAli Ahmed         {
13101c05dae3SAli Ahmed             messages::propertyValueNotInList(aResp->res, "ComputerSystem",
13111c05dae3SAli Ahmed                                              "TrustedModuleRequiredToBoot");
13121c05dae3SAli Ahmed             return;
13131c05dae3SAli Ahmed         }
13141c05dae3SAli Ahmed 
13151c05dae3SAli Ahmed         /* When there is more than one TPMEnable object... */
13161c05dae3SAli Ahmed         if (subtree.size() > 1)
13171c05dae3SAli Ahmed         {
13181c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG
13191c05dae3SAli Ahmed                 << "DBUS response has more than 1 TPM Enable object:"
13201c05dae3SAli Ahmed                 << subtree.size();
13211c05dae3SAli Ahmed             // Throw an internal Error and return
13221c05dae3SAli Ahmed             messages::internalError(aResp->res);
13231c05dae3SAli Ahmed             return;
13241c05dae3SAli Ahmed         }
13251c05dae3SAli Ahmed 
13261c05dae3SAli Ahmed         // Make sure the Dbus response map has a service and objectPath
13271c05dae3SAli Ahmed         // field
13281c05dae3SAli Ahmed         if (subtree[0].first.empty() || subtree[0].second.size() != 1)
13291c05dae3SAli Ahmed         {
13301c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG << "TPM.Policy mapper error!";
13311c05dae3SAli Ahmed             messages::internalError(aResp->res);
13321c05dae3SAli Ahmed             return;
13331c05dae3SAli Ahmed         }
13341c05dae3SAli Ahmed 
13351c05dae3SAli Ahmed         const std::string& path = subtree[0].first;
13361c05dae3SAli Ahmed         const std::string& serv = subtree[0].second.begin()->first;
13371c05dae3SAli Ahmed 
13381c05dae3SAli Ahmed         if (serv.empty())
13391c05dae3SAli Ahmed         {
13401c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG << "TPM.Policy service mapper error!";
13411c05dae3SAli Ahmed             messages::internalError(aResp->res);
13421c05dae3SAli Ahmed             return;
13431c05dae3SAli Ahmed         }
13441c05dae3SAli Ahmed 
13451c05dae3SAli Ahmed         // Valid TPM Enable object found, now setting the value
13461c05dae3SAli Ahmed         crow::connections::systemBus->async_method_call(
13478a592810SEd Tanous             [aResp](const boost::system::error_code ec2) {
13488a592810SEd Tanous             if (ec2)
13491c05dae3SAli Ahmed             {
13500fda0f12SGeorge Liu                 BMCWEB_LOG_DEBUG
13510fda0f12SGeorge Liu                     << "DBUS response error: Set TrustedModuleRequiredToBoot"
13528a592810SEd Tanous                     << ec2;
13531c05dae3SAli Ahmed                 messages::internalError(aResp->res);
13541c05dae3SAli Ahmed                 return;
13551c05dae3SAli Ahmed             }
13561c05dae3SAli Ahmed             BMCWEB_LOG_DEBUG << "Set TrustedModuleRequiredToBoot done.";
13571c05dae3SAli Ahmed             },
13581c05dae3SAli Ahmed             serv, path, "org.freedesktop.DBus.Properties", "Set",
13591c05dae3SAli Ahmed             "xyz.openbmc_project.Control.TPM.Policy", "TPMEnable",
1360168e20c1SEd Tanous             dbus::utility::DbusVariantType(tpmRequired));
13611c05dae3SAli Ahmed         },
13621c05dae3SAli Ahmed         "xyz.openbmc_project.ObjectMapper",
13631c05dae3SAli Ahmed         "/xyz/openbmc_project/object_mapper",
13641c05dae3SAli Ahmed         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
13651c05dae3SAli Ahmed         std::array<const char*, 1>{"xyz.openbmc_project.Control.TPM.Policy"});
13661c05dae3SAli Ahmed }
13671c05dae3SAli Ahmed 
13681c05dae3SAli Ahmed /**
1369491d8ee7SSantosh Puranik  * @brief Sets boot properties into DBUS object(s).
1370491d8ee7SSantosh Puranik  *
1371491d8ee7SSantosh Puranik  * @param[in] aResp           Shared pointer for generating response message.
1372cd9a4666SKonstantin Aladyshev  * @param[in] bootType        The boot type to set.
1373cd9a4666SKonstantin Aladyshev  * @return Integer error code.
1374cd9a4666SKonstantin Aladyshev  */
1375cd9a4666SKonstantin Aladyshev inline void setBootType(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1376cd9a4666SKonstantin Aladyshev                         const std::optional<std::string>& bootType)
1377cd9a4666SKonstantin Aladyshev {
1378c21865c4SKonstantin Aladyshev     std::string bootTypeStr;
1379cd9a4666SKonstantin Aladyshev 
1380c21865c4SKonstantin Aladyshev     if (!bootType)
1381cd9a4666SKonstantin Aladyshev     {
1382c21865c4SKonstantin Aladyshev         return;
1383c21865c4SKonstantin Aladyshev     }
1384c21865c4SKonstantin Aladyshev 
1385cd9a4666SKonstantin Aladyshev     // Source target specified
1386cd9a4666SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Boot type: " << *bootType;
1387cd9a4666SKonstantin Aladyshev     // Figure out which DBUS interface and property to use
1388cd9a4666SKonstantin Aladyshev     if (*bootType == "Legacy")
1389cd9a4666SKonstantin Aladyshev     {
1390cd9a4666SKonstantin Aladyshev         bootTypeStr = "xyz.openbmc_project.Control.Boot.Type.Types.Legacy";
1391cd9a4666SKonstantin Aladyshev     }
1392cd9a4666SKonstantin Aladyshev     else if (*bootType == "UEFI")
1393cd9a4666SKonstantin Aladyshev     {
1394cd9a4666SKonstantin Aladyshev         bootTypeStr = "xyz.openbmc_project.Control.Boot.Type.Types.EFI";
1395cd9a4666SKonstantin Aladyshev     }
1396cd9a4666SKonstantin Aladyshev     else
1397cd9a4666SKonstantin Aladyshev     {
1398cd9a4666SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Invalid property value for "
1399cd9a4666SKonstantin Aladyshev                             "BootSourceOverrideMode: "
1400cd9a4666SKonstantin Aladyshev                          << *bootType;
1401cd9a4666SKonstantin Aladyshev         messages::propertyValueNotInList(aResp->res, *bootType,
1402cd9a4666SKonstantin Aladyshev                                          "BootSourceOverrideMode");
1403cd9a4666SKonstantin Aladyshev         return;
1404cd9a4666SKonstantin Aladyshev     }
1405cd9a4666SKonstantin Aladyshev 
1406cd9a4666SKonstantin Aladyshev     // Act on validated parameters
1407cd9a4666SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot type: " << bootTypeStr;
1408cd9a4666SKonstantin Aladyshev 
1409cd9a4666SKonstantin Aladyshev     crow::connections::systemBus->async_method_call(
1410c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec) {
1411cd9a4666SKonstantin Aladyshev         if (ec)
1412cd9a4666SKonstantin Aladyshev         {
1413cd9a4666SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1414cd9a4666SKonstantin Aladyshev             if (ec.value() == boost::asio::error::host_unreachable)
1415cd9a4666SKonstantin Aladyshev             {
1416cd9a4666SKonstantin Aladyshev                 messages::resourceNotFound(aResp->res, "Set", "BootType");
1417cd9a4666SKonstantin Aladyshev                 return;
1418cd9a4666SKonstantin Aladyshev             }
1419cd9a4666SKonstantin Aladyshev             messages::internalError(aResp->res);
1420cd9a4666SKonstantin Aladyshev             return;
1421cd9a4666SKonstantin Aladyshev         }
1422cd9a4666SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Boot type update done.";
1423cd9a4666SKonstantin Aladyshev         },
1424c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1425c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1426cd9a4666SKonstantin Aladyshev         "org.freedesktop.DBus.Properties", "Set",
1427cd9a4666SKonstantin Aladyshev         "xyz.openbmc_project.Control.Boot.Type", "BootType",
1428168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootTypeStr));
1429cd9a4666SKonstantin Aladyshev }
1430cd9a4666SKonstantin Aladyshev 
1431cd9a4666SKonstantin Aladyshev /**
1432cd9a4666SKonstantin Aladyshev  * @brief Sets boot properties into DBUS object(s).
1433cd9a4666SKonstantin Aladyshev  *
1434cd9a4666SKonstantin Aladyshev  * @param[in] aResp           Shared pointer for generating response message.
1435c21865c4SKonstantin Aladyshev  * @param[in] bootType        The boot type to set.
1436c21865c4SKonstantin Aladyshev  * @return Integer error code.
1437c21865c4SKonstantin Aladyshev  */
1438c21865c4SKonstantin Aladyshev inline void setBootEnable(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1439c21865c4SKonstantin Aladyshev                           const std::optional<std::string>& bootEnable)
1440c21865c4SKonstantin Aladyshev {
1441c21865c4SKonstantin Aladyshev     if (!bootEnable)
1442c21865c4SKonstantin Aladyshev     {
1443c21865c4SKonstantin Aladyshev         return;
1444c21865c4SKonstantin Aladyshev     }
1445c21865c4SKonstantin Aladyshev     // Source target specified
1446c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "Boot enable: " << *bootEnable;
1447c21865c4SKonstantin Aladyshev 
1448c21865c4SKonstantin Aladyshev     bool bootOverrideEnable = false;
1449c21865c4SKonstantin Aladyshev     bool bootOverridePersistent = false;
1450c21865c4SKonstantin Aladyshev     // Figure out which DBUS interface and property to use
1451c21865c4SKonstantin Aladyshev     if (*bootEnable == "Disabled")
1452c21865c4SKonstantin Aladyshev     {
1453c21865c4SKonstantin Aladyshev         bootOverrideEnable = false;
1454c21865c4SKonstantin Aladyshev     }
1455c21865c4SKonstantin Aladyshev     else if (*bootEnable == "Once")
1456c21865c4SKonstantin Aladyshev     {
1457c21865c4SKonstantin Aladyshev         bootOverrideEnable = true;
1458c21865c4SKonstantin Aladyshev         bootOverridePersistent = false;
1459c21865c4SKonstantin Aladyshev     }
1460c21865c4SKonstantin Aladyshev     else if (*bootEnable == "Continuous")
1461c21865c4SKonstantin Aladyshev     {
1462c21865c4SKonstantin Aladyshev         bootOverrideEnable = true;
1463c21865c4SKonstantin Aladyshev         bootOverridePersistent = true;
1464c21865c4SKonstantin Aladyshev     }
1465c21865c4SKonstantin Aladyshev     else
1466c21865c4SKonstantin Aladyshev     {
14670fda0f12SGeorge Liu         BMCWEB_LOG_DEBUG
14680fda0f12SGeorge Liu             << "Invalid property value for BootSourceOverrideEnabled: "
1469c21865c4SKonstantin Aladyshev             << *bootEnable;
1470c21865c4SKonstantin Aladyshev         messages::propertyValueNotInList(aResp->res, *bootEnable,
1471c21865c4SKonstantin Aladyshev                                          "BootSourceOverrideEnabled");
1472c21865c4SKonstantin Aladyshev         return;
1473c21865c4SKonstantin Aladyshev     }
1474c21865c4SKonstantin Aladyshev 
1475c21865c4SKonstantin Aladyshev     // Act on validated parameters
1476c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot override enable: " << bootOverrideEnable;
1477c21865c4SKonstantin Aladyshev 
1478c21865c4SKonstantin Aladyshev     crow::connections::systemBus->async_method_call(
14798a592810SEd Tanous         [aResp](const boost::system::error_code ec2) {
14808a592810SEd Tanous         if (ec2)
1481c21865c4SKonstantin Aladyshev         {
14828a592810SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
1483c21865c4SKonstantin Aladyshev             messages::internalError(aResp->res);
1484c21865c4SKonstantin Aladyshev             return;
1485c21865c4SKonstantin Aladyshev         }
1486c21865c4SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Boot override enable update done.";
1487c21865c4SKonstantin Aladyshev         },
1488c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1489c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1490c21865c4SKonstantin Aladyshev         "org.freedesktop.DBus.Properties", "Set",
1491c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Object.Enable", "Enabled",
1492168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootOverrideEnable));
1493c21865c4SKonstantin Aladyshev 
1494c21865c4SKonstantin Aladyshev     if (!bootOverrideEnable)
1495c21865c4SKonstantin Aladyshev     {
1496c21865c4SKonstantin Aladyshev         return;
1497c21865c4SKonstantin Aladyshev     }
1498c21865c4SKonstantin Aladyshev 
1499c21865c4SKonstantin Aladyshev     // In case boot override is enabled we need to set correct value for the
1500c21865c4SKonstantin Aladyshev     // 'one_time' enable DBus interface
1501c21865c4SKonstantin Aladyshev     BMCWEB_LOG_DEBUG << "DBUS boot override persistent: "
1502c21865c4SKonstantin Aladyshev                      << bootOverridePersistent;
1503c21865c4SKonstantin Aladyshev 
1504c21865c4SKonstantin Aladyshev     crow::connections::systemBus->async_method_call(
1505c21865c4SKonstantin Aladyshev         [aResp](const boost::system::error_code ec) {
1506c21865c4SKonstantin Aladyshev         if (ec)
1507c21865c4SKonstantin Aladyshev         {
1508c21865c4SKonstantin Aladyshev             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1509c21865c4SKonstantin Aladyshev             messages::internalError(aResp->res);
1510c21865c4SKonstantin Aladyshev             return;
1511c21865c4SKonstantin Aladyshev         }
1512c21865c4SKonstantin Aladyshev         BMCWEB_LOG_DEBUG << "Boot one_time update done.";
1513c21865c4SKonstantin Aladyshev         },
1514c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1515c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot/one_time",
1516c21865c4SKonstantin Aladyshev         "org.freedesktop.DBus.Properties", "Set",
1517c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Object.Enable", "Enabled",
1518168e20c1SEd Tanous         dbus::utility::DbusVariantType(!bootOverridePersistent));
1519c21865c4SKonstantin Aladyshev }
1520c21865c4SKonstantin Aladyshev 
1521c21865c4SKonstantin Aladyshev /**
1522c21865c4SKonstantin Aladyshev  * @brief Sets boot properties into DBUS object(s).
1523c21865c4SKonstantin Aladyshev  *
1524c21865c4SKonstantin Aladyshev  * @param[in] aResp           Shared pointer for generating response message.
1525491d8ee7SSantosh Puranik  * @param[in] bootSource      The boot source to set.
1526491d8ee7SSantosh Puranik  *
1527265c1602SJohnathan Mantey  * @return Integer error code.
1528491d8ee7SSantosh Puranik  */
1529cd9a4666SKonstantin Aladyshev inline void setBootModeOrSource(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1530cd9a4666SKonstantin Aladyshev                                 const std::optional<std::string>& bootSource)
1531491d8ee7SSantosh Puranik {
1532c21865c4SKonstantin Aladyshev     std::string bootSourceStr;
1533c21865c4SKonstantin Aladyshev     std::string bootModeStr;
1534944ffaf9SJohnathan Mantey 
1535c21865c4SKonstantin Aladyshev     if (!bootSource)
1536491d8ee7SSantosh Puranik     {
1537c21865c4SKonstantin Aladyshev         return;
1538c21865c4SKonstantin Aladyshev     }
1539c21865c4SKonstantin Aladyshev 
1540491d8ee7SSantosh Puranik     // Source target specified
1541491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Boot source: " << *bootSource;
1542491d8ee7SSantosh Puranik     // Figure out which DBUS interface and property to use
1543e662eae8SEd Tanous     if (assignBootParameters(aResp, *bootSource, bootSourceStr, bootModeStr) !=
1544e662eae8SEd Tanous         0)
1545491d8ee7SSantosh Puranik     {
1546944ffaf9SJohnathan Mantey         BMCWEB_LOG_DEBUG
1547944ffaf9SJohnathan Mantey             << "Invalid property value for BootSourceOverrideTarget: "
1548491d8ee7SSantosh Puranik             << *bootSource;
1549491d8ee7SSantosh Puranik         messages::propertyValueNotInList(aResp->res, *bootSource,
1550491d8ee7SSantosh Puranik                                          "BootSourceTargetOverride");
1551491d8ee7SSantosh Puranik         return;
1552491d8ee7SSantosh Puranik     }
1553491d8ee7SSantosh Puranik 
1554944ffaf9SJohnathan Mantey     // Act on validated parameters
1555944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
1556944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
1557944ffaf9SJohnathan Mantey 
1558491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1559491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec) {
1560491d8ee7SSantosh Puranik         if (ec)
1561491d8ee7SSantosh Puranik         {
1562491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1563491d8ee7SSantosh Puranik             messages::internalError(aResp->res);
1564491d8ee7SSantosh Puranik             return;
1565491d8ee7SSantosh Puranik         }
1566491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot source update done.";
1567491d8ee7SSantosh Puranik         },
1568c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1569c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1570491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1571491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource",
1572168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootSourceStr));
1573944ffaf9SJohnathan Mantey 
1574491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1575491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec) {
1576491d8ee7SSantosh Puranik         if (ec)
1577491d8ee7SSantosh Puranik         {
1578491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1579491d8ee7SSantosh Puranik             messages::internalError(aResp->res);
1580491d8ee7SSantosh Puranik             return;
1581491d8ee7SSantosh Puranik         }
1582491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot mode update done.";
1583491d8ee7SSantosh Puranik         },
1584c21865c4SKonstantin Aladyshev         "xyz.openbmc_project.Settings",
1585c21865c4SKonstantin Aladyshev         "/xyz/openbmc_project/control/host0/boot",
1586491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1587491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
1588168e20c1SEd Tanous         dbus::utility::DbusVariantType(bootModeStr));
1589cd9a4666SKonstantin Aladyshev }
1590944ffaf9SJohnathan Mantey 
1591cd9a4666SKonstantin Aladyshev /**
1592c21865c4SKonstantin Aladyshev  * @brief Sets Boot source override properties.
1593491d8ee7SSantosh Puranik  *
1594491d8ee7SSantosh Puranik  * @param[in] aResp      Shared pointer for generating response message.
1595491d8ee7SSantosh Puranik  * @param[in] bootSource The boot source from incoming RF request.
1596cd9a4666SKonstantin Aladyshev  * @param[in] bootType   The boot type from incoming RF request.
1597491d8ee7SSantosh Puranik  * @param[in] bootEnable The boot override enable from incoming RF request.
1598491d8ee7SSantosh Puranik  *
1599265c1602SJohnathan Mantey  * @return Integer error code.
1600491d8ee7SSantosh Puranik  */
1601c21865c4SKonstantin Aladyshev 
1602c21865c4SKonstantin Aladyshev inline void setBootProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1603c21865c4SKonstantin Aladyshev                               const std::optional<std::string>& bootSource,
1604c21865c4SKonstantin Aladyshev                               const std::optional<std::string>& bootType,
1605c21865c4SKonstantin Aladyshev                               const std::optional<std::string>& bootEnable)
1606491d8ee7SSantosh Puranik {
1607491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set boot information.";
1608491d8ee7SSantosh Puranik 
1609c21865c4SKonstantin Aladyshev     setBootModeOrSource(aResp, bootSource);
1610c21865c4SKonstantin Aladyshev     setBootType(aResp, bootType);
1611c21865c4SKonstantin Aladyshev     setBootEnable(aResp, bootEnable);
1612491d8ee7SSantosh Puranik }
1613491d8ee7SSantosh Puranik 
1614c6a620f2SGeorge Liu /**
161598e386ecSGunnar Mills  * @brief Sets AssetTag
161698e386ecSGunnar Mills  *
161798e386ecSGunnar Mills  * @param[in] aResp   Shared pointer for generating response message.
161898e386ecSGunnar Mills  * @param[in] assetTag  "AssetTag" from request.
161998e386ecSGunnar Mills  *
162098e386ecSGunnar Mills  * @return None.
162198e386ecSGunnar Mills  */
16228d1b46d7Szhanghch05 inline void setAssetTag(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
162398e386ecSGunnar Mills                         const std::string& assetTag)
162498e386ecSGunnar Mills {
162598e386ecSGunnar Mills     crow::connections::systemBus->async_method_call(
1626b9d36b47SEd Tanous         [aResp,
1627b9d36b47SEd Tanous          assetTag](const boost::system::error_code ec,
1628b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
162998e386ecSGunnar Mills         if (ec)
163098e386ecSGunnar Mills         {
163198e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " << ec;
163298e386ecSGunnar Mills             messages::internalError(aResp->res);
163398e386ecSGunnar Mills             return;
163498e386ecSGunnar Mills         }
163526f6976fSEd Tanous         if (subtree.empty())
163698e386ecSGunnar Mills         {
163798e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find system D-Bus object!";
163898e386ecSGunnar Mills             messages::internalError(aResp->res);
163998e386ecSGunnar Mills             return;
164098e386ecSGunnar Mills         }
164198e386ecSGunnar Mills         // Assume only 1 system D-Bus object
164298e386ecSGunnar Mills         // Throw an error if there is more than 1
164398e386ecSGunnar Mills         if (subtree.size() > 1)
164498e386ecSGunnar Mills         {
164598e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Found more than 1 system D-Bus object!";
164698e386ecSGunnar Mills             messages::internalError(aResp->res);
164798e386ecSGunnar Mills             return;
164898e386ecSGunnar Mills         }
164998e386ecSGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.size() != 1)
165098e386ecSGunnar Mills         {
165198e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Asset Tag Set mapper error!";
165298e386ecSGunnar Mills             messages::internalError(aResp->res);
165398e386ecSGunnar Mills             return;
165498e386ecSGunnar Mills         }
165598e386ecSGunnar Mills 
165698e386ecSGunnar Mills         const std::string& path = subtree[0].first;
165798e386ecSGunnar Mills         const std::string& service = subtree[0].second.begin()->first;
165898e386ecSGunnar Mills 
165998e386ecSGunnar Mills         if (service.empty())
166098e386ecSGunnar Mills         {
166198e386ecSGunnar Mills             BMCWEB_LOG_DEBUG << "Asset Tag Set service mapper error!";
166298e386ecSGunnar Mills             messages::internalError(aResp->res);
166398e386ecSGunnar Mills             return;
166498e386ecSGunnar Mills         }
166598e386ecSGunnar Mills 
166698e386ecSGunnar Mills         crow::connections::systemBus->async_method_call(
166798e386ecSGunnar Mills             [aResp](const boost::system::error_code ec2) {
166898e386ecSGunnar Mills             if (ec2)
166998e386ecSGunnar Mills             {
1670002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "D-Bus response error on AssetTag Set "
1671002d39b4SEd Tanous                                  << ec2;
167298e386ecSGunnar Mills                 messages::internalError(aResp->res);
167398e386ecSGunnar Mills                 return;
167498e386ecSGunnar Mills             }
167598e386ecSGunnar Mills             },
167698e386ecSGunnar Mills             service, path, "org.freedesktop.DBus.Properties", "Set",
167798e386ecSGunnar Mills             "xyz.openbmc_project.Inventory.Decorator.AssetTag", "AssetTag",
1678168e20c1SEd Tanous             dbus::utility::DbusVariantType(assetTag));
167998e386ecSGunnar Mills         },
168098e386ecSGunnar Mills         "xyz.openbmc_project.ObjectMapper",
168198e386ecSGunnar Mills         "/xyz/openbmc_project/object_mapper",
168298e386ecSGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
168398e386ecSGunnar Mills         "/xyz/openbmc_project/inventory", int32_t(0),
168498e386ecSGunnar Mills         std::array<const char*, 1>{
168598e386ecSGunnar Mills             "xyz.openbmc_project.Inventory.Item.System"});
168698e386ecSGunnar Mills }
168798e386ecSGunnar Mills 
168898e386ecSGunnar Mills /**
168969f35306SGunnar Mills  * @brief Sets automaticRetry (Auto Reboot)
169069f35306SGunnar Mills  *
169169f35306SGunnar Mills  * @param[in] aResp   Shared pointer for generating response message.
169269f35306SGunnar Mills  * @param[in] automaticRetryConfig  "AutomaticRetryConfig" from request.
169369f35306SGunnar Mills  *
169469f35306SGunnar Mills  * @return None.
169569f35306SGunnar Mills  */
16968d1b46d7Szhanghch05 inline void setAutomaticRetry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1697f23b7296SEd Tanous                               const std::string& automaticRetryConfig)
169869f35306SGunnar Mills {
169969f35306SGunnar Mills     BMCWEB_LOG_DEBUG << "Set Automatic Retry.";
170069f35306SGunnar Mills 
170169f35306SGunnar Mills     // OpenBMC only supports "Disabled" and "RetryAttempts".
1702543f4400SEd Tanous     bool autoRebootEnabled = false;
170369f35306SGunnar Mills 
170469f35306SGunnar Mills     if (automaticRetryConfig == "Disabled")
170569f35306SGunnar Mills     {
170669f35306SGunnar Mills         autoRebootEnabled = false;
170769f35306SGunnar Mills     }
170869f35306SGunnar Mills     else if (automaticRetryConfig == "RetryAttempts")
170969f35306SGunnar Mills     {
171069f35306SGunnar Mills         autoRebootEnabled = true;
171169f35306SGunnar Mills     }
171269f35306SGunnar Mills     else
171369f35306SGunnar Mills     {
17140fda0f12SGeorge Liu         BMCWEB_LOG_DEBUG << "Invalid property value for AutomaticRetryConfig: "
171569f35306SGunnar Mills                          << automaticRetryConfig;
171669f35306SGunnar Mills         messages::propertyValueNotInList(aResp->res, automaticRetryConfig,
171769f35306SGunnar Mills                                          "AutomaticRetryConfig");
171869f35306SGunnar Mills         return;
171969f35306SGunnar Mills     }
172069f35306SGunnar Mills 
172169f35306SGunnar Mills     crow::connections::systemBus->async_method_call(
172269f35306SGunnar Mills         [aResp](const boost::system::error_code ec) {
172369f35306SGunnar Mills         if (ec)
172469f35306SGunnar Mills         {
172569f35306SGunnar Mills             messages::internalError(aResp->res);
172669f35306SGunnar Mills             return;
172769f35306SGunnar Mills         }
172869f35306SGunnar Mills         },
172969f35306SGunnar Mills         "xyz.openbmc_project.Settings",
173069f35306SGunnar Mills         "/xyz/openbmc_project/control/host0/auto_reboot",
173169f35306SGunnar Mills         "org.freedesktop.DBus.Properties", "Set",
173269f35306SGunnar Mills         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
1733168e20c1SEd Tanous         dbus::utility::DbusVariantType(autoRebootEnabled));
173469f35306SGunnar Mills }
173569f35306SGunnar Mills 
173669f35306SGunnar Mills /**
1737c6a620f2SGeorge Liu  * @brief Sets power restore policy properties.
1738c6a620f2SGeorge Liu  *
1739c6a620f2SGeorge Liu  * @param[in] aResp   Shared pointer for generating response message.
1740c6a620f2SGeorge Liu  * @param[in] policy  power restore policy properties from request.
1741c6a620f2SGeorge Liu  *
1742c6a620f2SGeorge Liu  * @return None.
1743c6a620f2SGeorge Liu  */
17448d1b46d7Szhanghch05 inline void
17458d1b46d7Szhanghch05     setPowerRestorePolicy(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
17464e69c904SGunnar Mills                           const std::string& policy)
1747c6a620f2SGeorge Liu {
1748c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Set power restore policy.";
1749c6a620f2SGeorge Liu 
1750c6a620f2SGeorge Liu     const boost::container::flat_map<std::string, std::string> policyMaps = {
17510fda0f12SGeorge Liu         {"AlwaysOn",
17520fda0f12SGeorge Liu          "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn"},
17530fda0f12SGeorge Liu         {"AlwaysOff",
17540fda0f12SGeorge Liu          "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff"},
17550fda0f12SGeorge Liu         {"LastState",
17560fda0f12SGeorge Liu          "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore"}};
1757c6a620f2SGeorge Liu 
1758c6a620f2SGeorge Liu     std::string powerRestorPolicy;
1759c6a620f2SGeorge Liu 
17604e69c904SGunnar Mills     auto policyMapsIt = policyMaps.find(policy);
1761c6a620f2SGeorge Liu     if (policyMapsIt == policyMaps.end())
1762c6a620f2SGeorge Liu     {
17634e69c904SGunnar Mills         messages::propertyValueNotInList(aResp->res, policy,
17644e69c904SGunnar Mills                                          "PowerRestorePolicy");
1765c6a620f2SGeorge Liu         return;
1766c6a620f2SGeorge Liu     }
1767c6a620f2SGeorge Liu 
1768c6a620f2SGeorge Liu     powerRestorPolicy = policyMapsIt->second;
1769c6a620f2SGeorge Liu 
1770c6a620f2SGeorge Liu     crow::connections::systemBus->async_method_call(
1771c6a620f2SGeorge Liu         [aResp](const boost::system::error_code ec) {
1772c6a620f2SGeorge Liu         if (ec)
1773c6a620f2SGeorge Liu         {
1774c6a620f2SGeorge Liu             messages::internalError(aResp->res);
1775c6a620f2SGeorge Liu             return;
1776c6a620f2SGeorge Liu         }
1777c6a620f2SGeorge Liu         },
1778c6a620f2SGeorge Liu         "xyz.openbmc_project.Settings",
1779c6a620f2SGeorge Liu         "/xyz/openbmc_project/control/host0/power_restore_policy",
1780c6a620f2SGeorge Liu         "org.freedesktop.DBus.Properties", "Set",
1781c6a620f2SGeorge Liu         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
1782168e20c1SEd Tanous         dbus::utility::DbusVariantType(powerRestorPolicy));
1783c6a620f2SGeorge Liu }
1784c6a620f2SGeorge Liu 
1785a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
1786a6349918SAppaRao Puli /**
1787a6349918SAppaRao Puli  * @brief Retrieves provisioning status
1788a6349918SAppaRao Puli  *
1789a6349918SAppaRao Puli  * @param[in] aResp     Shared pointer for completing asynchronous calls.
1790a6349918SAppaRao Puli  *
1791a6349918SAppaRao Puli  * @return None.
1792a6349918SAppaRao Puli  */
17938d1b46d7Szhanghch05 inline void getProvisioningStatus(std::shared_ptr<bmcweb::AsyncResp> aResp)
1794a6349918SAppaRao Puli {
1795a6349918SAppaRao Puli     BMCWEB_LOG_DEBUG << "Get OEM information.";
1796a6349918SAppaRao Puli     crow::connections::systemBus->async_method_call(
1797a6349918SAppaRao Puli         [aResp](const boost::system::error_code ec,
1798b9d36b47SEd Tanous                 const dbus::utility::DBusPropertiesMap& propertiesList) {
1799b99fb1a9SAppaRao Puli         nlohmann::json& oemPFR =
1800b99fb1a9SAppaRao Puli             aResp->res.jsonValue["Oem"]["OpenBmc"]["FirmwareProvisioning"];
180150626f4fSJames Feist         aResp->res.jsonValue["Oem"]["OpenBmc"]["@odata.type"] =
180250626f4fSJames Feist             "#OemComputerSystem.OpenBmc";
180350626f4fSJames Feist         oemPFR["@odata.type"] = "#OemComputerSystem.FirmwareProvisioning";
180450626f4fSJames Feist 
1805a6349918SAppaRao Puli         if (ec)
1806a6349918SAppaRao Puli         {
1807a6349918SAppaRao Puli             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1808b99fb1a9SAppaRao Puli             // not an error, don't have to have the interface
1809b99fb1a9SAppaRao Puli             oemPFR["ProvisioningStatus"] = "NotProvisioned";
1810a6349918SAppaRao Puli             return;
1811a6349918SAppaRao Puli         }
1812a6349918SAppaRao Puli 
1813a6349918SAppaRao Puli         const bool* provState = nullptr;
1814a6349918SAppaRao Puli         const bool* lockState = nullptr;
18156e3b67ecSAppaRao Puli         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
18166e3b67ecSAppaRao Puli                  property : propertiesList)
1817a6349918SAppaRao Puli         {
1818a6349918SAppaRao Puli             if (property.first == "UfmProvisioned")
1819a6349918SAppaRao Puli             {
1820a6349918SAppaRao Puli                 provState = std::get_if<bool>(&property.second);
1821a6349918SAppaRao Puli             }
1822a6349918SAppaRao Puli             else if (property.first == "UfmLocked")
1823a6349918SAppaRao Puli             {
1824a6349918SAppaRao Puli                 lockState = std::get_if<bool>(&property.second);
1825a6349918SAppaRao Puli             }
1826a6349918SAppaRao Puli         }
1827a6349918SAppaRao Puli 
1828a6349918SAppaRao Puli         if ((provState == nullptr) || (lockState == nullptr))
1829a6349918SAppaRao Puli         {
1830a6349918SAppaRao Puli             BMCWEB_LOG_DEBUG << "Unable to get PFR attributes.";
1831a6349918SAppaRao Puli             messages::internalError(aResp->res);
1832a6349918SAppaRao Puli             return;
1833a6349918SAppaRao Puli         }
1834a6349918SAppaRao Puli 
1835a6349918SAppaRao Puli         if (*provState == true)
1836a6349918SAppaRao Puli         {
1837a6349918SAppaRao Puli             if (*lockState == true)
1838a6349918SAppaRao Puli             {
1839a6349918SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "ProvisionedAndLocked";
1840a6349918SAppaRao Puli             }
1841a6349918SAppaRao Puli             else
1842a6349918SAppaRao Puli             {
1843a6349918SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "ProvisionedButNotLocked";
1844a6349918SAppaRao Puli             }
1845a6349918SAppaRao Puli         }
1846a6349918SAppaRao Puli         else
1847a6349918SAppaRao Puli         {
1848a6349918SAppaRao Puli             oemPFR["ProvisioningStatus"] = "NotProvisioned";
1849a6349918SAppaRao Puli         }
1850a6349918SAppaRao Puli         },
1851a6349918SAppaRao Puli         "xyz.openbmc_project.PFR.Manager", "/xyz/openbmc_project/pfr",
1852a6349918SAppaRao Puli         "org.freedesktop.DBus.Properties", "GetAll",
1853a6349918SAppaRao Puli         "xyz.openbmc_project.PFR.Attributes");
1854a6349918SAppaRao Puli }
1855a6349918SAppaRao Puli #endif
1856a6349918SAppaRao Puli 
1857491d8ee7SSantosh Puranik /**
18583a2d0424SChris Cain  * @brief Translate the PowerMode to a response message.
18593a2d0424SChris Cain  *
18603a2d0424SChris Cain  * @param[in] aResp  Shared pointer for generating response message.
18613a2d0424SChris Cain  * @param[in] modeValue  PowerMode value to be translated
18623a2d0424SChris Cain  *
18633a2d0424SChris Cain  * @return None.
18643a2d0424SChris Cain  */
18653a2d0424SChris Cain inline void translatePowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
18663a2d0424SChris Cain                                const std::string& modeValue)
18673a2d0424SChris Cain {
18680fda0f12SGeorge Liu     if (modeValue == "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static")
18693a2d0424SChris Cain     {
18703a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "Static";
18713a2d0424SChris Cain     }
18720fda0f12SGeorge Liu     else if (
18730fda0f12SGeorge Liu         modeValue ==
18740fda0f12SGeorge Liu         "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance")
18753a2d0424SChris Cain     {
18763a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "MaximumPerformance";
18773a2d0424SChris Cain     }
18780fda0f12SGeorge Liu     else if (modeValue ==
18790fda0f12SGeorge Liu              "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving")
18803a2d0424SChris Cain     {
18813a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "PowerSaving";
18823a2d0424SChris Cain     }
18830fda0f12SGeorge Liu     else if (modeValue ==
18840fda0f12SGeorge Liu              "xyz.openbmc_project.Control.Power.Mode.PowerMode.OEM")
18853a2d0424SChris Cain     {
18863a2d0424SChris Cain         aResp->res.jsonValue["PowerMode"] = "OEM";
18873a2d0424SChris Cain     }
18883a2d0424SChris Cain     else
18893a2d0424SChris Cain     {
18903a2d0424SChris Cain         // Any other values would be invalid
18913a2d0424SChris Cain         BMCWEB_LOG_DEBUG << "PowerMode value was not valid: " << modeValue;
18923a2d0424SChris Cain         messages::internalError(aResp->res);
18933a2d0424SChris Cain     }
18943a2d0424SChris Cain }
18953a2d0424SChris Cain 
18963a2d0424SChris Cain /**
18973a2d0424SChris Cain  * @brief Retrieves system power mode
18983a2d0424SChris Cain  *
18993a2d0424SChris Cain  * @param[in] aResp  Shared pointer for generating response message.
19003a2d0424SChris Cain  *
19013a2d0424SChris Cain  * @return None.
19023a2d0424SChris Cain  */
19033a2d0424SChris Cain inline void getPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
19043a2d0424SChris Cain {
19053a2d0424SChris Cain     BMCWEB_LOG_DEBUG << "Get power mode.";
19063a2d0424SChris Cain 
19073a2d0424SChris Cain     // Get Power Mode object path:
19083a2d0424SChris Cain     crow::connections::systemBus->async_method_call(
1909b9d36b47SEd Tanous         [aResp](const boost::system::error_code ec,
1910b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
19113a2d0424SChris Cain         if (ec)
19123a2d0424SChris Cain         {
1913002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on Power.Mode GetSubTree "
1914002d39b4SEd Tanous                              << ec;
19153a2d0424SChris Cain             // This is an optional D-Bus object so just return if
19163a2d0424SChris Cain             // error occurs
19173a2d0424SChris Cain             return;
19183a2d0424SChris Cain         }
19193a2d0424SChris Cain         if (subtree.empty())
19203a2d0424SChris Cain         {
19213a2d0424SChris Cain             // As noted above, this is an optional interface so just return
19223a2d0424SChris Cain             // if there is no instance found
19233a2d0424SChris Cain             return;
19243a2d0424SChris Cain         }
19253a2d0424SChris Cain         if (subtree.size() > 1)
19263a2d0424SChris Cain         {
19273a2d0424SChris Cain             // More then one PowerMode object is not supported and is an
19283a2d0424SChris Cain             // error
19293a2d0424SChris Cain             BMCWEB_LOG_DEBUG
19303a2d0424SChris Cain                 << "Found more than 1 system D-Bus Power.Mode objects: "
19313a2d0424SChris Cain                 << subtree.size();
19323a2d0424SChris Cain             messages::internalError(aResp->res);
19333a2d0424SChris Cain             return;
19343a2d0424SChris Cain         }
19353a2d0424SChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
19363a2d0424SChris Cain         {
19373a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode mapper error!";
19383a2d0424SChris Cain             messages::internalError(aResp->res);
19393a2d0424SChris Cain             return;
19403a2d0424SChris Cain         }
19413a2d0424SChris Cain         const std::string& path = subtree[0].first;
19423a2d0424SChris Cain         const std::string& service = subtree[0].second.begin()->first;
19433a2d0424SChris Cain         if (service.empty())
19443a2d0424SChris Cain         {
19453a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode service mapper error!";
19463a2d0424SChris Cain             messages::internalError(aResp->res);
19473a2d0424SChris Cain             return;
19483a2d0424SChris Cain         }
19493a2d0424SChris Cain         // Valid Power Mode object found, now read the current value
19501e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
19511e1e598dSJonathan Doman             *crow::connections::systemBus, service, path,
19521e1e598dSJonathan Doman             "xyz.openbmc_project.Control.Power.Mode", "PowerMode",
19538a592810SEd Tanous             [aResp](const boost::system::error_code ec2,
19541e1e598dSJonathan Doman                     const std::string& pmode) {
19558a592810SEd Tanous             if (ec2)
19563a2d0424SChris Cain             {
1957002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error on PowerMode Get: "
19588a592810SEd Tanous                                  << ec2;
19593a2d0424SChris Cain                 messages::internalError(aResp->res);
19603a2d0424SChris Cain                 return;
19613a2d0424SChris Cain             }
19623a2d0424SChris Cain 
1963002d39b4SEd Tanous             aResp->res.jsonValue["PowerMode@Redfish.AllowableValues"] = {
1964002d39b4SEd Tanous                 "Static", "MaximumPerformance", "PowerSaving"};
19653a2d0424SChris Cain 
19661e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Current power mode: " << pmode;
19671e1e598dSJonathan Doman             translatePowerMode(aResp, pmode);
19681e1e598dSJonathan Doman             });
19693a2d0424SChris Cain         },
19703a2d0424SChris Cain         "xyz.openbmc_project.ObjectMapper",
19713a2d0424SChris Cain         "/xyz/openbmc_project/object_mapper",
19723a2d0424SChris Cain         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
19733a2d0424SChris Cain         std::array<const char*, 1>{"xyz.openbmc_project.Control.Power.Mode"});
19743a2d0424SChris Cain }
19753a2d0424SChris Cain 
19763a2d0424SChris Cain /**
19773a2d0424SChris Cain  * @brief Validate the specified mode is valid and return the PowerMode
19783a2d0424SChris Cain  * name associated with that string
19793a2d0424SChris Cain  *
19803a2d0424SChris Cain  * @param[in] aResp   Shared pointer for generating response message.
19813a2d0424SChris Cain  * @param[in] modeString  String representing the desired PowerMode
19823a2d0424SChris Cain  *
19833a2d0424SChris Cain  * @return PowerMode value or empty string if mode is not valid
19843a2d0424SChris Cain  */
19853a2d0424SChris Cain inline std::string
19863a2d0424SChris Cain     validatePowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
19873a2d0424SChris Cain                       const std::string& modeString)
19883a2d0424SChris Cain {
19893a2d0424SChris Cain     std::string mode;
19903a2d0424SChris Cain 
19913a2d0424SChris Cain     if (modeString == "Static")
19923a2d0424SChris Cain     {
19933a2d0424SChris Cain         mode = "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static";
19943a2d0424SChris Cain     }
19953a2d0424SChris Cain     else if (modeString == "MaximumPerformance")
19963a2d0424SChris Cain     {
19970fda0f12SGeorge Liu         mode =
19980fda0f12SGeorge Liu             "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance";
19993a2d0424SChris Cain     }
20003a2d0424SChris Cain     else if (modeString == "PowerSaving")
20013a2d0424SChris Cain     {
20023a2d0424SChris Cain         mode = "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving";
20033a2d0424SChris Cain     }
20043a2d0424SChris Cain     else
20053a2d0424SChris Cain     {
20063a2d0424SChris Cain         messages::propertyValueNotInList(aResp->res, modeString, "PowerMode");
20073a2d0424SChris Cain     }
20083a2d0424SChris Cain     return mode;
20093a2d0424SChris Cain }
20103a2d0424SChris Cain 
20113a2d0424SChris Cain /**
20123a2d0424SChris Cain  * @brief Sets system power mode.
20133a2d0424SChris Cain  *
20143a2d0424SChris Cain  * @param[in] aResp   Shared pointer for generating response message.
20153a2d0424SChris Cain  * @param[in] pmode   System power mode from request.
20163a2d0424SChris Cain  *
20173a2d0424SChris Cain  * @return None.
20183a2d0424SChris Cain  */
20193a2d0424SChris Cain inline void setPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
20203a2d0424SChris Cain                          const std::string& pmode)
20213a2d0424SChris Cain {
20223a2d0424SChris Cain     BMCWEB_LOG_DEBUG << "Set power mode.";
20233a2d0424SChris Cain 
20243a2d0424SChris Cain     std::string powerMode = validatePowerMode(aResp, pmode);
20253a2d0424SChris Cain     if (powerMode.empty())
20263a2d0424SChris Cain     {
20273a2d0424SChris Cain         return;
20283a2d0424SChris Cain     }
20293a2d0424SChris Cain 
20303a2d0424SChris Cain     // Get Power Mode object path:
20313a2d0424SChris Cain     crow::connections::systemBus->async_method_call(
2032b9d36b47SEd Tanous         [aResp,
2033b9d36b47SEd Tanous          powerMode](const boost::system::error_code ec,
2034b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
20353a2d0424SChris Cain         if (ec)
20363a2d0424SChris Cain         {
2037002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error on Power.Mode GetSubTree "
2038002d39b4SEd Tanous                              << ec;
20393a2d0424SChris Cain             // This is an optional D-Bus object, but user attempted to patch
20403a2d0424SChris Cain             messages::internalError(aResp->res);
20413a2d0424SChris Cain             return;
20423a2d0424SChris Cain         }
20433a2d0424SChris Cain         if (subtree.empty())
20443a2d0424SChris Cain         {
20453a2d0424SChris Cain             // This is an optional D-Bus object, but user attempted to patch
20463a2d0424SChris Cain             messages::resourceNotFound(aResp->res, "ComputerSystem",
20473a2d0424SChris Cain                                        "PowerMode");
20483a2d0424SChris Cain             return;
20493a2d0424SChris Cain         }
20503a2d0424SChris Cain         if (subtree.size() > 1)
20513a2d0424SChris Cain         {
20523a2d0424SChris Cain             // More then one PowerMode object is not supported and is an
20533a2d0424SChris Cain             // error
20543a2d0424SChris Cain             BMCWEB_LOG_DEBUG
20553a2d0424SChris Cain                 << "Found more than 1 system D-Bus Power.Mode objects: "
20563a2d0424SChris Cain                 << subtree.size();
20573a2d0424SChris Cain             messages::internalError(aResp->res);
20583a2d0424SChris Cain             return;
20593a2d0424SChris Cain         }
20603a2d0424SChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
20613a2d0424SChris Cain         {
20623a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode mapper error!";
20633a2d0424SChris Cain             messages::internalError(aResp->res);
20643a2d0424SChris Cain             return;
20653a2d0424SChris Cain         }
20663a2d0424SChris Cain         const std::string& path = subtree[0].first;
20673a2d0424SChris Cain         const std::string& service = subtree[0].second.begin()->first;
20683a2d0424SChris Cain         if (service.empty())
20693a2d0424SChris Cain         {
20703a2d0424SChris Cain             BMCWEB_LOG_DEBUG << "Power.Mode service mapper error!";
20713a2d0424SChris Cain             messages::internalError(aResp->res);
20723a2d0424SChris Cain             return;
20733a2d0424SChris Cain         }
20743a2d0424SChris Cain 
20753a2d0424SChris Cain         BMCWEB_LOG_DEBUG << "Setting power mode(" << powerMode << ") -> "
20763a2d0424SChris Cain                          << path;
20773a2d0424SChris Cain 
20783a2d0424SChris Cain         // Set the Power Mode property
20793a2d0424SChris Cain         crow::connections::systemBus->async_method_call(
20808a592810SEd Tanous             [aResp](const boost::system::error_code ec2) {
20818a592810SEd Tanous             if (ec2)
20823a2d0424SChris Cain             {
20833a2d0424SChris Cain                 messages::internalError(aResp->res);
20843a2d0424SChris Cain                 return;
20853a2d0424SChris Cain             }
20863a2d0424SChris Cain             },
20873a2d0424SChris Cain             service, path, "org.freedesktop.DBus.Properties", "Set",
20883a2d0424SChris Cain             "xyz.openbmc_project.Control.Power.Mode", "PowerMode",
2089168e20c1SEd Tanous             dbus::utility::DbusVariantType(powerMode));
20903a2d0424SChris Cain         },
20913a2d0424SChris Cain         "xyz.openbmc_project.ObjectMapper",
20923a2d0424SChris Cain         "/xyz/openbmc_project/object_mapper",
20933a2d0424SChris Cain         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
20943a2d0424SChris Cain         std::array<const char*, 1>{"xyz.openbmc_project.Control.Power.Mode"});
20953a2d0424SChris Cain }
20963a2d0424SChris Cain 
20973a2d0424SChris Cain /**
209851709ffdSYong Li  * @brief Translates watchdog timeout action DBUS property value to redfish.
209951709ffdSYong Li  *
210051709ffdSYong Li  * @param[in] dbusAction    The watchdog timeout action in D-BUS.
210151709ffdSYong Li  *
210251709ffdSYong Li  * @return Returns as a string, the timeout action in Redfish terms. If
210351709ffdSYong Li  * translation cannot be done, returns an empty string.
210451709ffdSYong Li  */
210523a21a1cSEd Tanous inline std::string dbusToRfWatchdogAction(const std::string& dbusAction)
210651709ffdSYong Li {
210751709ffdSYong Li     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.None")
210851709ffdSYong Li     {
210951709ffdSYong Li         return "None";
211051709ffdSYong Li     }
21113174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.HardReset")
211251709ffdSYong Li     {
211351709ffdSYong Li         return "ResetSystem";
211451709ffdSYong Li     }
21153174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerOff")
211651709ffdSYong Li     {
211751709ffdSYong Li         return "PowerDown";
211851709ffdSYong Li     }
21193174e4dfSEd Tanous     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerCycle")
212051709ffdSYong Li     {
212151709ffdSYong Li         return "PowerCycle";
212251709ffdSYong Li     }
212351709ffdSYong Li 
212451709ffdSYong Li     return "";
212551709ffdSYong Li }
212651709ffdSYong Li 
212751709ffdSYong Li /**
2128c45f0082SYong Li  *@brief Translates timeout action from Redfish to DBUS property value.
2129c45f0082SYong Li  *
2130c45f0082SYong Li  *@param[in] rfAction The timeout action in Redfish.
2131c45f0082SYong Li  *
2132c45f0082SYong Li  *@return Returns as a string, the time_out action as expected by DBUS.
2133c45f0082SYong Li  *If translation cannot be done, returns an empty string.
2134c45f0082SYong Li  */
2135c45f0082SYong Li 
213623a21a1cSEd Tanous inline std::string rfToDbusWDTTimeOutAct(const std::string& rfAction)
2137c45f0082SYong Li {
2138c45f0082SYong Li     if (rfAction == "None")
2139c45f0082SYong Li     {
2140c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.None";
2141c45f0082SYong Li     }
21423174e4dfSEd Tanous     if (rfAction == "PowerCycle")
2143c45f0082SYong Li     {
2144c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerCycle";
2145c45f0082SYong Li     }
21463174e4dfSEd Tanous     if (rfAction == "PowerDown")
2147c45f0082SYong Li     {
2148c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerOff";
2149c45f0082SYong Li     }
21503174e4dfSEd Tanous     if (rfAction == "ResetSystem")
2151c45f0082SYong Li     {
2152c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.HardReset";
2153c45f0082SYong Li     }
2154c45f0082SYong Li 
2155c45f0082SYong Li     return "";
2156c45f0082SYong Li }
2157c45f0082SYong Li 
2158c45f0082SYong Li /**
215951709ffdSYong Li  * @brief Retrieves host watchdog timer properties over DBUS
216051709ffdSYong Li  *
216151709ffdSYong Li  * @param[in] aResp     Shared pointer for completing asynchronous calls.
216251709ffdSYong Li  *
216351709ffdSYong Li  * @return None.
216451709ffdSYong Li  */
21658d1b46d7Szhanghch05 inline void
21668d1b46d7Szhanghch05     getHostWatchdogTimer(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
216751709ffdSYong Li {
216851709ffdSYong Li     BMCWEB_LOG_DEBUG << "Get host watchodg";
216951709ffdSYong Li     crow::connections::systemBus->async_method_call(
217051709ffdSYong Li         [aResp](const boost::system::error_code ec,
2171b9d36b47SEd Tanous                 const dbus::utility::DBusPropertiesMap& properties) {
217251709ffdSYong Li         if (ec)
217351709ffdSYong Li         {
217451709ffdSYong Li             // watchdog service is stopped
217551709ffdSYong Li             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
217651709ffdSYong Li             return;
217751709ffdSYong Li         }
217851709ffdSYong Li 
217951709ffdSYong Li         BMCWEB_LOG_DEBUG << "Got " << properties.size() << " wdt prop.";
218051709ffdSYong Li 
218151709ffdSYong Li         nlohmann::json& hostWatchdogTimer =
218251709ffdSYong Li             aResp->res.jsonValue["HostWatchdogTimer"];
218351709ffdSYong Li 
218451709ffdSYong Li         // watchdog service is running/enabled
218551709ffdSYong Li         hostWatchdogTimer["Status"]["State"] = "Enabled";
218651709ffdSYong Li 
218751709ffdSYong Li         for (const auto& property : properties)
218851709ffdSYong Li         {
218951709ffdSYong Li             BMCWEB_LOG_DEBUG << "prop=" << property.first;
219051709ffdSYong Li             if (property.first == "Enabled")
219151709ffdSYong Li             {
219251709ffdSYong Li                 const bool* state = std::get_if<bool>(&property.second);
219351709ffdSYong Li 
2194e662eae8SEd Tanous                 if (state == nullptr)
219551709ffdSYong Li                 {
219651709ffdSYong Li                     messages::internalError(aResp->res);
2197601af5edSChicago Duan                     return;
219851709ffdSYong Li                 }
219951709ffdSYong Li 
220051709ffdSYong Li                 hostWatchdogTimer["FunctionEnabled"] = *state;
220151709ffdSYong Li             }
220251709ffdSYong Li             else if (property.first == "ExpireAction")
220351709ffdSYong Li             {
220451709ffdSYong Li                 const std::string* s =
220551709ffdSYong Li                     std::get_if<std::string>(&property.second);
2206e662eae8SEd Tanous                 if (s == nullptr)
220751709ffdSYong Li                 {
220851709ffdSYong Li                     messages::internalError(aResp->res);
2209601af5edSChicago Duan                     return;
221051709ffdSYong Li                 }
221151709ffdSYong Li 
221251709ffdSYong Li                 std::string action = dbusToRfWatchdogAction(*s);
221351709ffdSYong Li                 if (action.empty())
221451709ffdSYong Li                 {
221551709ffdSYong Li                     messages::internalError(aResp->res);
2216601af5edSChicago Duan                     return;
221751709ffdSYong Li                 }
221851709ffdSYong Li                 hostWatchdogTimer["TimeoutAction"] = action;
221951709ffdSYong Li             }
222051709ffdSYong Li         }
222151709ffdSYong Li         },
222251709ffdSYong Li         "xyz.openbmc_project.Watchdog", "/xyz/openbmc_project/watchdog/host0",
222351709ffdSYong Li         "org.freedesktop.DBus.Properties", "GetAll",
222451709ffdSYong Li         "xyz.openbmc_project.State.Watchdog");
222551709ffdSYong Li }
222651709ffdSYong Li 
222751709ffdSYong Li /**
2228c45f0082SYong Li  * @brief Sets Host WatchDog Timer properties.
2229c45f0082SYong Li  *
2230c45f0082SYong Li  * @param[in] aResp      Shared pointer for generating response message.
2231c45f0082SYong Li  * @param[in] wdtEnable  The WDTimer Enable value (true/false) from incoming
2232c45f0082SYong Li  *                       RF request.
2233c45f0082SYong Li  * @param[in] wdtTimeOutAction The WDT Timeout action, from incoming RF request.
2234c45f0082SYong Li  *
2235c45f0082SYong Li  * @return None.
2236c45f0082SYong Li  */
22378d1b46d7Szhanghch05 inline void setWDTProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2238c45f0082SYong Li                              const std::optional<bool> wdtEnable,
2239c45f0082SYong Li                              const std::optional<std::string>& wdtTimeOutAction)
2240c45f0082SYong Li {
2241c45f0082SYong Li     BMCWEB_LOG_DEBUG << "Set host watchdog";
2242c45f0082SYong Li 
2243c45f0082SYong Li     if (wdtTimeOutAction)
2244c45f0082SYong Li     {
2245c45f0082SYong Li         std::string wdtTimeOutActStr = rfToDbusWDTTimeOutAct(*wdtTimeOutAction);
2246c45f0082SYong Li         // check if TimeOut Action is Valid
2247c45f0082SYong Li         if (wdtTimeOutActStr.empty())
2248c45f0082SYong Li         {
2249c45f0082SYong Li             BMCWEB_LOG_DEBUG << "Unsupported value for TimeoutAction: "
2250c45f0082SYong Li                              << *wdtTimeOutAction;
2251c45f0082SYong Li             messages::propertyValueNotInList(aResp->res, *wdtTimeOutAction,
2252c45f0082SYong Li                                              "TimeoutAction");
2253c45f0082SYong Li             return;
2254c45f0082SYong Li         }
2255c45f0082SYong Li 
2256c45f0082SYong Li         crow::connections::systemBus->async_method_call(
2257c45f0082SYong Li             [aResp](const boost::system::error_code ec) {
2258c45f0082SYong Li             if (ec)
2259c45f0082SYong Li             {
2260c45f0082SYong Li                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
2261c45f0082SYong Li                 messages::internalError(aResp->res);
2262c45f0082SYong Li                 return;
2263c45f0082SYong Li             }
2264c45f0082SYong Li             },
2265c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
2266c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
2267c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
2268c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "ExpireAction",
2269168e20c1SEd Tanous             dbus::utility::DbusVariantType(wdtTimeOutActStr));
2270c45f0082SYong Li     }
2271c45f0082SYong Li 
2272c45f0082SYong Li     if (wdtEnable)
2273c45f0082SYong Li     {
2274c45f0082SYong Li         crow::connections::systemBus->async_method_call(
2275c45f0082SYong Li             [aResp](const boost::system::error_code ec) {
2276c45f0082SYong Li             if (ec)
2277c45f0082SYong Li             {
2278c45f0082SYong Li                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
2279c45f0082SYong Li                 messages::internalError(aResp->res);
2280c45f0082SYong Li                 return;
2281c45f0082SYong Li             }
2282c45f0082SYong Li             },
2283c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
2284c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
2285c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
2286c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "Enabled",
2287168e20c1SEd Tanous             dbus::utility::DbusVariantType(*wdtEnable));
2288c45f0082SYong Li     }
2289c45f0082SYong Li }
2290c45f0082SYong Li 
229137bbf98cSChris Cain using ipsPropertiesType =
229237bbf98cSChris Cain     std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>;
229337bbf98cSChris Cain /**
229437bbf98cSChris Cain  * @brief Parse the Idle Power Saver properties into json
229537bbf98cSChris Cain  *
229637bbf98cSChris Cain  * @param[in] aResp     Shared pointer for completing asynchronous calls.
229737bbf98cSChris Cain  * @param[in] properties  IPS property data from DBus.
229837bbf98cSChris Cain  *
229937bbf98cSChris Cain  * @return true if successful
230037bbf98cSChris Cain  */
2301f6674220SEd Tanous inline bool parseIpsProperties(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2302*02cad96eSEd Tanous                                const ipsPropertiesType& properties)
230337bbf98cSChris Cain {
230437bbf98cSChris Cain     for (const auto& property : properties)
230537bbf98cSChris Cain     {
230637bbf98cSChris Cain         if (property.first == "Enabled")
230737bbf98cSChris Cain         {
230837bbf98cSChris Cain             const bool* state = std::get_if<bool>(&property.second);
2309e662eae8SEd Tanous             if (state == nullptr)
231037bbf98cSChris Cain             {
231137bbf98cSChris Cain                 return false;
231237bbf98cSChris Cain             }
231337bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"][property.first] = *state;
231437bbf98cSChris Cain         }
231537bbf98cSChris Cain         else if (property.first == "EnterUtilizationPercent")
231637bbf98cSChris Cain         {
231737bbf98cSChris Cain             const uint8_t* util = std::get_if<uint8_t>(&property.second);
2318e662eae8SEd Tanous             if (util == nullptr)
231937bbf98cSChris Cain             {
232037bbf98cSChris Cain                 return false;
232137bbf98cSChris Cain             }
232237bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"][property.first] = *util;
232337bbf98cSChris Cain         }
232437bbf98cSChris Cain         else if (property.first == "EnterDwellTime")
232537bbf98cSChris Cain         {
232637bbf98cSChris Cain             // Convert Dbus time from milliseconds to seconds
232737bbf98cSChris Cain             const uint64_t* timeMilliseconds =
232837bbf98cSChris Cain                 std::get_if<uint64_t>(&property.second);
2329e662eae8SEd Tanous             if (timeMilliseconds == nullptr)
233037bbf98cSChris Cain             {
233137bbf98cSChris Cain                 return false;
233237bbf98cSChris Cain             }
233337bbf98cSChris Cain             const std::chrono::duration<uint64_t, std::milli> ms(
233437bbf98cSChris Cain                 *timeMilliseconds);
233537bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"]["EnterDwellTimeSeconds"] =
233637bbf98cSChris Cain                 std::chrono::duration_cast<std::chrono::duration<uint64_t>>(ms)
233737bbf98cSChris Cain                     .count();
233837bbf98cSChris Cain         }
233937bbf98cSChris Cain         else if (property.first == "ExitUtilizationPercent")
234037bbf98cSChris Cain         {
234137bbf98cSChris Cain             const uint8_t* util = std::get_if<uint8_t>(&property.second);
2342e662eae8SEd Tanous             if (util == nullptr)
234337bbf98cSChris Cain             {
234437bbf98cSChris Cain                 return false;
234537bbf98cSChris Cain             }
234637bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"][property.first] = *util;
234737bbf98cSChris Cain         }
234837bbf98cSChris Cain         else if (property.first == "ExitDwellTime")
234937bbf98cSChris Cain         {
235037bbf98cSChris Cain             // Convert Dbus time from milliseconds to seconds
235137bbf98cSChris Cain             const uint64_t* timeMilliseconds =
235237bbf98cSChris Cain                 std::get_if<uint64_t>(&property.second);
2353e662eae8SEd Tanous             if (timeMilliseconds == nullptr)
235437bbf98cSChris Cain             {
235537bbf98cSChris Cain                 return false;
235637bbf98cSChris Cain             }
235737bbf98cSChris Cain             const std::chrono::duration<uint64_t, std::milli> ms(
235837bbf98cSChris Cain                 *timeMilliseconds);
235937bbf98cSChris Cain             aResp->res.jsonValue["IdlePowerSaver"]["ExitDwellTimeSeconds"] =
236037bbf98cSChris Cain                 std::chrono::duration_cast<std::chrono::duration<uint64_t>>(ms)
236137bbf98cSChris Cain                     .count();
236237bbf98cSChris Cain         }
236337bbf98cSChris Cain         else
236437bbf98cSChris Cain         {
236537bbf98cSChris Cain             BMCWEB_LOG_WARNING << "Unexpected IdlePowerSaver property: "
236637bbf98cSChris Cain                                << property.first;
236737bbf98cSChris Cain         }
236837bbf98cSChris Cain     }
236937bbf98cSChris Cain 
237037bbf98cSChris Cain     return true;
237137bbf98cSChris Cain }
237237bbf98cSChris Cain 
237337bbf98cSChris Cain /**
237437bbf98cSChris Cain  * @brief Retrieves host watchdog timer properties over DBUS
237537bbf98cSChris Cain  *
237637bbf98cSChris Cain  * @param[in] aResp     Shared pointer for completing asynchronous calls.
237737bbf98cSChris Cain  *
237837bbf98cSChris Cain  * @return None.
237937bbf98cSChris Cain  */
238037bbf98cSChris Cain inline void getIdlePowerSaver(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
238137bbf98cSChris Cain {
238237bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "Get idle power saver parameters";
238337bbf98cSChris Cain 
238437bbf98cSChris Cain     // Get IdlePowerSaver object path:
238537bbf98cSChris Cain     crow::connections::systemBus->async_method_call(
2386b9d36b47SEd Tanous         [aResp](const boost::system::error_code ec,
2387b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
238837bbf98cSChris Cain         if (ec)
238937bbf98cSChris Cain         {
239037bbf98cSChris Cain             BMCWEB_LOG_DEBUG
239137bbf98cSChris Cain                 << "DBUS response error on Power.IdlePowerSaver GetSubTree "
239237bbf98cSChris Cain                 << ec;
239337bbf98cSChris Cain             messages::internalError(aResp->res);
239437bbf98cSChris Cain             return;
239537bbf98cSChris Cain         }
239637bbf98cSChris Cain         if (subtree.empty())
239737bbf98cSChris Cain         {
239837bbf98cSChris Cain             // This is an optional interface so just return
239937bbf98cSChris Cain             // if there is no instance found
240037bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "No instances found";
240137bbf98cSChris Cain             return;
240237bbf98cSChris Cain         }
240337bbf98cSChris Cain         if (subtree.size() > 1)
240437bbf98cSChris Cain         {
240537bbf98cSChris Cain             // More then one PowerIdlePowerSaver object is not supported and
240637bbf98cSChris Cain             // is an error
240737bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "Found more than 1 system D-Bus "
240837bbf98cSChris Cain                                 "Power.IdlePowerSaver objects: "
240937bbf98cSChris Cain                              << subtree.size();
241037bbf98cSChris Cain             messages::internalError(aResp->res);
241137bbf98cSChris Cain             return;
241237bbf98cSChris Cain         }
241337bbf98cSChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
241437bbf98cSChris Cain         {
241537bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver mapper error!";
241637bbf98cSChris Cain             messages::internalError(aResp->res);
241737bbf98cSChris Cain             return;
241837bbf98cSChris Cain         }
241937bbf98cSChris Cain         const std::string& path = subtree[0].first;
242037bbf98cSChris Cain         const std::string& service = subtree[0].second.begin()->first;
242137bbf98cSChris Cain         if (service.empty())
242237bbf98cSChris Cain         {
2423002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver service mapper error!";
242437bbf98cSChris Cain             messages::internalError(aResp->res);
242537bbf98cSChris Cain             return;
242637bbf98cSChris Cain         }
242737bbf98cSChris Cain 
242837bbf98cSChris Cain         // Valid IdlePowerSaver object found, now read the current values
242937bbf98cSChris Cain         crow::connections::systemBus->async_method_call(
24308a592810SEd Tanous             [aResp](const boost::system::error_code ec2,
2431*02cad96eSEd Tanous                     const ipsPropertiesType& properties) {
24328a592810SEd Tanous             if (ec2)
243337bbf98cSChris Cain             {
243437bbf98cSChris Cain                 BMCWEB_LOG_ERROR
24358a592810SEd Tanous                     << "DBUS response error on IdlePowerSaver GetAll: " << ec2;
243637bbf98cSChris Cain                 messages::internalError(aResp->res);
243737bbf98cSChris Cain                 return;
243837bbf98cSChris Cain             }
243937bbf98cSChris Cain 
2440e05aec50SEd Tanous             if (!parseIpsProperties(aResp, properties))
244137bbf98cSChris Cain             {
244237bbf98cSChris Cain                 messages::internalError(aResp->res);
244337bbf98cSChris Cain                 return;
244437bbf98cSChris Cain             }
244537bbf98cSChris Cain             },
244637bbf98cSChris Cain             service, path, "org.freedesktop.DBus.Properties", "GetAll",
244737bbf98cSChris Cain             "xyz.openbmc_project.Control.Power.IdlePowerSaver");
244837bbf98cSChris Cain         },
244937bbf98cSChris Cain         "xyz.openbmc_project.ObjectMapper",
245037bbf98cSChris Cain         "/xyz/openbmc_project/object_mapper",
245137bbf98cSChris Cain         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
245237bbf98cSChris Cain         std::array<const char*, 1>{
245337bbf98cSChris Cain             "xyz.openbmc_project.Control.Power.IdlePowerSaver"});
245437bbf98cSChris Cain 
245537bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "EXIT: Get idle power saver parameters";
245637bbf98cSChris Cain }
245737bbf98cSChris Cain 
245837bbf98cSChris Cain /**
245937bbf98cSChris Cain  * @brief Sets Idle Power Saver properties.
246037bbf98cSChris Cain  *
246137bbf98cSChris Cain  * @param[in] aResp      Shared pointer for generating response message.
246237bbf98cSChris Cain  * @param[in] ipsEnable  The IPS Enable value (true/false) from incoming
246337bbf98cSChris Cain  *                       RF request.
246437bbf98cSChris Cain  * @param[in] ipsEnterUtil The utilization limit to enter idle state.
246537bbf98cSChris Cain  * @param[in] ipsEnterTime The time the utilization must be below ipsEnterUtil
246637bbf98cSChris Cain  * before entering idle state.
246737bbf98cSChris Cain  * @param[in] ipsExitUtil The utilization limit when exiting idle state.
246837bbf98cSChris Cain  * @param[in] ipsExitTime The time the utilization must be above ipsExutUtil
246937bbf98cSChris Cain  * before exiting idle state
247037bbf98cSChris Cain  *
247137bbf98cSChris Cain  * @return None.
247237bbf98cSChris Cain  */
247337bbf98cSChris Cain inline void setIdlePowerSaver(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
247437bbf98cSChris Cain                               const std::optional<bool> ipsEnable,
247537bbf98cSChris Cain                               const std::optional<uint8_t> ipsEnterUtil,
247637bbf98cSChris Cain                               const std::optional<uint64_t> ipsEnterTime,
247737bbf98cSChris Cain                               const std::optional<uint8_t> ipsExitUtil,
247837bbf98cSChris Cain                               const std::optional<uint64_t> ipsExitTime)
247937bbf98cSChris Cain {
248037bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "Set idle power saver properties";
248137bbf98cSChris Cain 
248237bbf98cSChris Cain     // Get IdlePowerSaver object path:
248337bbf98cSChris Cain     crow::connections::systemBus->async_method_call(
248437bbf98cSChris Cain         [aResp, ipsEnable, ipsEnterUtil, ipsEnterTime, ipsExitUtil,
2485b9d36b47SEd Tanous          ipsExitTime](const boost::system::error_code ec,
2486b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
248737bbf98cSChris Cain         if (ec)
248837bbf98cSChris Cain         {
248937bbf98cSChris Cain             BMCWEB_LOG_DEBUG
249037bbf98cSChris Cain                 << "DBUS response error on Power.IdlePowerSaver GetSubTree "
249137bbf98cSChris Cain                 << ec;
249237bbf98cSChris Cain             messages::internalError(aResp->res);
249337bbf98cSChris Cain             return;
249437bbf98cSChris Cain         }
249537bbf98cSChris Cain         if (subtree.empty())
249637bbf98cSChris Cain         {
249737bbf98cSChris Cain             // This is an optional D-Bus object, but user attempted to patch
249837bbf98cSChris Cain             messages::resourceNotFound(aResp->res, "ComputerSystem",
249937bbf98cSChris Cain                                        "IdlePowerSaver");
250037bbf98cSChris Cain             return;
250137bbf98cSChris Cain         }
250237bbf98cSChris Cain         if (subtree.size() > 1)
250337bbf98cSChris Cain         {
250437bbf98cSChris Cain             // More then one PowerIdlePowerSaver object is not supported and
250537bbf98cSChris Cain             // is an error
25060fda0f12SGeorge Liu             BMCWEB_LOG_DEBUG
25070fda0f12SGeorge Liu                 << "Found more than 1 system D-Bus Power.IdlePowerSaver objects: "
250837bbf98cSChris Cain                 << subtree.size();
250937bbf98cSChris Cain             messages::internalError(aResp->res);
251037bbf98cSChris Cain             return;
251137bbf98cSChris Cain         }
251237bbf98cSChris Cain         if ((subtree[0].first.empty()) || (subtree[0].second.size() != 1))
251337bbf98cSChris Cain         {
251437bbf98cSChris Cain             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver mapper error!";
251537bbf98cSChris Cain             messages::internalError(aResp->res);
251637bbf98cSChris Cain             return;
251737bbf98cSChris Cain         }
251837bbf98cSChris Cain         const std::string& path = subtree[0].first;
251937bbf98cSChris Cain         const std::string& service = subtree[0].second.begin()->first;
252037bbf98cSChris Cain         if (service.empty())
252137bbf98cSChris Cain         {
2522002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Power.IdlePowerSaver service mapper error!";
252337bbf98cSChris Cain             messages::internalError(aResp->res);
252437bbf98cSChris Cain             return;
252537bbf98cSChris Cain         }
252637bbf98cSChris Cain 
252737bbf98cSChris Cain         // Valid Power IdlePowerSaver object found, now set any values that
252837bbf98cSChris Cain         // need to be updated
252937bbf98cSChris Cain 
253037bbf98cSChris Cain         if (ipsEnable)
253137bbf98cSChris Cain         {
253237bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
25338a592810SEd Tanous                 [aResp](const boost::system::error_code ec2) {
25348a592810SEd Tanous                 if (ec2)
253537bbf98cSChris Cain                 {
25368a592810SEd Tanous                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
253737bbf98cSChris Cain                     messages::internalError(aResp->res);
253837bbf98cSChris Cain                     return;
253937bbf98cSChris Cain                 }
254037bbf98cSChris Cain                 },
254137bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
2542002d39b4SEd Tanous                 "xyz.openbmc_project.Control.Power.IdlePowerSaver", "Enabled",
2543002d39b4SEd Tanous                 dbus::utility::DbusVariantType(*ipsEnable));
254437bbf98cSChris Cain         }
254537bbf98cSChris Cain         if (ipsEnterUtil)
254637bbf98cSChris Cain         {
254737bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
25488a592810SEd Tanous                 [aResp](const boost::system::error_code ec2) {
25498a592810SEd Tanous                 if (ec2)
255037bbf98cSChris Cain                 {
25518a592810SEd Tanous                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
255237bbf98cSChris Cain                     messages::internalError(aResp->res);
255337bbf98cSChris Cain                     return;
255437bbf98cSChris Cain                 }
255537bbf98cSChris Cain                 },
255637bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
255737bbf98cSChris Cain                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
255837bbf98cSChris Cain                 "EnterUtilizationPercent",
2559168e20c1SEd Tanous                 dbus::utility::DbusVariantType(*ipsEnterUtil));
256037bbf98cSChris Cain         }
256137bbf98cSChris Cain         if (ipsEnterTime)
256237bbf98cSChris Cain         {
256337bbf98cSChris Cain             // Convert from seconds into milliseconds for DBus
256437bbf98cSChris Cain             const uint64_t timeMilliseconds = *ipsEnterTime * 1000;
256537bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
25668a592810SEd Tanous                 [aResp](const boost::system::error_code ec2) {
25678a592810SEd Tanous                 if (ec2)
256837bbf98cSChris Cain                 {
25698a592810SEd Tanous                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
257037bbf98cSChris Cain                     messages::internalError(aResp->res);
257137bbf98cSChris Cain                     return;
257237bbf98cSChris Cain                 }
257337bbf98cSChris Cain                 },
257437bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
257537bbf98cSChris Cain                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2576168e20c1SEd Tanous                 "EnterDwellTime",
2577168e20c1SEd Tanous                 dbus::utility::DbusVariantType(timeMilliseconds));
257837bbf98cSChris Cain         }
257937bbf98cSChris Cain         if (ipsExitUtil)
258037bbf98cSChris Cain         {
258137bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
25828a592810SEd Tanous                 [aResp](const boost::system::error_code ec2) {
25838a592810SEd Tanous                 if (ec2)
258437bbf98cSChris Cain                 {
25858a592810SEd Tanous                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
258637bbf98cSChris Cain                     messages::internalError(aResp->res);
258737bbf98cSChris Cain                     return;
258837bbf98cSChris Cain                 }
258937bbf98cSChris Cain                 },
259037bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
259137bbf98cSChris Cain                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
259237bbf98cSChris Cain                 "ExitUtilizationPercent",
2593168e20c1SEd Tanous                 dbus::utility::DbusVariantType(*ipsExitUtil));
259437bbf98cSChris Cain         }
259537bbf98cSChris Cain         if (ipsExitTime)
259637bbf98cSChris Cain         {
259737bbf98cSChris Cain             // Convert from seconds into milliseconds for DBus
259837bbf98cSChris Cain             const uint64_t timeMilliseconds = *ipsExitTime * 1000;
259937bbf98cSChris Cain             crow::connections::systemBus->async_method_call(
26008a592810SEd Tanous                 [aResp](const boost::system::error_code ec2) {
26018a592810SEd Tanous                 if (ec2)
260237bbf98cSChris Cain                 {
26038a592810SEd Tanous                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
260437bbf98cSChris Cain                     messages::internalError(aResp->res);
260537bbf98cSChris Cain                     return;
260637bbf98cSChris Cain                 }
260737bbf98cSChris Cain                 },
260837bbf98cSChris Cain                 service, path, "org.freedesktop.DBus.Properties", "Set",
260937bbf98cSChris Cain                 "xyz.openbmc_project.Control.Power.IdlePowerSaver",
2610168e20c1SEd Tanous                 "ExitDwellTime",
2611168e20c1SEd Tanous                 dbus::utility::DbusVariantType(timeMilliseconds));
261237bbf98cSChris Cain         }
261337bbf98cSChris Cain         },
261437bbf98cSChris Cain         "xyz.openbmc_project.ObjectMapper",
261537bbf98cSChris Cain         "/xyz/openbmc_project/object_mapper",
261637bbf98cSChris Cain         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
261737bbf98cSChris Cain         std::array<const char*, 1>{
261837bbf98cSChris Cain             "xyz.openbmc_project.Control.Power.IdlePowerSaver"});
261937bbf98cSChris Cain 
262037bbf98cSChris Cain     BMCWEB_LOG_DEBUG << "EXIT: Set idle power saver parameters";
262137bbf98cSChris Cain }
262237bbf98cSChris Cain 
2623c45f0082SYong Li /**
2624c5b2abe0SLewanczyk, Dawid  * SystemsCollection derived class for delivering ComputerSystems Collection
2625c5b2abe0SLewanczyk, Dawid  * Schema
2626c5b2abe0SLewanczyk, Dawid  */
26277e860f15SJohn Edward Broadbent inline void requestRoutesSystemsCollection(App& app)
26281abe55efSEd Tanous {
26297e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/")
2630ed398213SEd Tanous         .privileges(redfish::privileges::getComputerSystemCollection)
26317e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2632f4c99e70SEd Tanous             [&app](const crow::Request& req,
26337e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26343ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2635f4c99e70SEd Tanous         {
2636f4c99e70SEd Tanous             return;
2637f4c99e70SEd Tanous         }
26388d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
26390f74e643SEd Tanous             "#ComputerSystemCollection.ComputerSystemCollection";
26408d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Systems";
26418d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Computer System Collection";
2642462023adSSunitha Harish 
26431e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
2644002d39b4SEd Tanous             *crow::connections::systemBus, "xyz.openbmc_project.Settings",
26451e1e598dSJonathan Doman             "/xyz/openbmc_project/network/hypervisor",
2646002d39b4SEd Tanous             "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
26478a592810SEd Tanous             [asyncResp](const boost::system::error_code ec2,
26481e1e598dSJonathan Doman                         const std::string& /*hostName*/) {
2649002d39b4SEd Tanous             nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
26502c70f800SEd Tanous             ifaceArray = nlohmann::json::array();
2651002d39b4SEd Tanous             auto& count = asyncResp->res.jsonValue["Members@odata.count"];
26521476687dSEd Tanous 
26531476687dSEd Tanous             nlohmann::json::object_t system;
26541476687dSEd Tanous             system["@odata.id"] = "/redfish/v1/Systems/system";
26551476687dSEd Tanous             ifaceArray.push_back(std::move(system));
265694bda602STim Lee             count = ifaceArray.size();
26578a592810SEd Tanous             if (!ec2)
2658462023adSSunitha Harish             {
2659462023adSSunitha Harish                 BMCWEB_LOG_DEBUG << "Hypervisor is available";
26601476687dSEd Tanous                 nlohmann::json::object_t hypervisor;
2661002d39b4SEd Tanous                 hypervisor["@odata.id"] = "/redfish/v1/Systems/hypervisor";
26621476687dSEd Tanous                 ifaceArray.push_back(std::move(hypervisor));
26632c70f800SEd Tanous                 count = ifaceArray.size();
2664cb13a392SEd Tanous             }
26651e1e598dSJonathan Doman             });
26667e860f15SJohn Edward Broadbent         });
2667c5b2abe0SLewanczyk, Dawid }
26687e860f15SJohn Edward Broadbent 
26697e860f15SJohn Edward Broadbent /**
26707e860f15SJohn Edward Broadbent  * Function transceives data with dbus directly.
26717e860f15SJohn Edward Broadbent  */
26724f48d5f6SEd Tanous inline void doNMI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
26737e860f15SJohn Edward Broadbent {
26747e860f15SJohn Edward Broadbent     constexpr char const* serviceName = "xyz.openbmc_project.Control.Host.NMI";
26757e860f15SJohn Edward Broadbent     constexpr char const* objectPath = "/xyz/openbmc_project/control/host0/nmi";
26767e860f15SJohn Edward Broadbent     constexpr char const* interfaceName =
26777e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Control.Host.NMI";
26787e860f15SJohn Edward Broadbent     constexpr char const* method = "NMI";
26797e860f15SJohn Edward Broadbent 
26807e860f15SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
26817e860f15SJohn Edward Broadbent         [asyncResp](const boost::system::error_code ec) {
26827e860f15SJohn Edward Broadbent         if (ec)
26837e860f15SJohn Edward Broadbent         {
26847e860f15SJohn Edward Broadbent             BMCWEB_LOG_ERROR << " Bad D-Bus request error: " << ec;
26857e860f15SJohn Edward Broadbent             messages::internalError(asyncResp->res);
26867e860f15SJohn Edward Broadbent             return;
26877e860f15SJohn Edward Broadbent         }
26887e860f15SJohn Edward Broadbent         messages::success(asyncResp->res);
26897e860f15SJohn Edward Broadbent         },
26907e860f15SJohn Edward Broadbent         serviceName, objectPath, interfaceName, method);
26917e860f15SJohn Edward Broadbent }
2692c5b2abe0SLewanczyk, Dawid 
2693c5b2abe0SLewanczyk, Dawid /**
2694cc340dd9SEd Tanous  * SystemActionsReset class supports handle POST method for Reset action.
2695cc340dd9SEd Tanous  * The class retrieves and sends data directly to D-Bus.
2696cc340dd9SEd Tanous  */
26977e860f15SJohn Edward Broadbent inline void requestRoutesSystemActionsReset(App& app)
2698cc340dd9SEd Tanous {
2699cc340dd9SEd Tanous     /**
2700cc340dd9SEd Tanous      * Function handles POST method request.
2701cc340dd9SEd Tanous      * Analyzes POST body message before sends Reset request data to D-Bus.
2702cc340dd9SEd Tanous      */
27037e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
27047e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset/")
2705ed398213SEd Tanous         .privileges(redfish::privileges::postComputerSystem)
2706002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
2707002d39b4SEd Tanous             [&app](const crow::Request& req,
27087e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
27093ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
271045ca1b86SEd Tanous         {
271145ca1b86SEd Tanous             return;
271245ca1b86SEd Tanous         }
27139712f8acSEd Tanous         std::string resetType;
271415ed6780SWilly Tu         if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
27157e860f15SJohn Edward Broadbent                                        resetType))
2716cc340dd9SEd Tanous         {
2717cc340dd9SEd Tanous             return;
2718cc340dd9SEd Tanous         }
2719cc340dd9SEd Tanous 
2720d22c8396SJason M. Bills         // Get the command and host vs. chassis
2721cc340dd9SEd Tanous         std::string command;
2722543f4400SEd Tanous         bool hostCommand = true;
2723d4d25793SEd Tanous         if ((resetType == "On") || (resetType == "ForceOn"))
2724cc340dd9SEd Tanous         {
2725cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.On";
2726d22c8396SJason M. Bills             hostCommand = true;
2727d22c8396SJason M. Bills         }
2728d22c8396SJason M. Bills         else if (resetType == "ForceOff")
2729d22c8396SJason M. Bills         {
2730d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Chassis.Transition.Off";
2731d22c8396SJason M. Bills             hostCommand = false;
2732d22c8396SJason M. Bills         }
2733d22c8396SJason M. Bills         else if (resetType == "ForceRestart")
2734d22c8396SJason M. Bills         {
273586a0851aSJason M. Bills             command =
273686a0851aSJason M. Bills                 "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot";
273786a0851aSJason M. Bills             hostCommand = true;
2738cc340dd9SEd Tanous         }
27399712f8acSEd Tanous         else if (resetType == "GracefulShutdown")
2740cc340dd9SEd Tanous         {
2741cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.Off";
2742d22c8396SJason M. Bills             hostCommand = true;
2743cc340dd9SEd Tanous         }
27449712f8acSEd Tanous         else if (resetType == "GracefulRestart")
2745cc340dd9SEd Tanous         {
27460fda0f12SGeorge Liu             command =
27470fda0f12SGeorge Liu                 "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot";
2748d22c8396SJason M. Bills             hostCommand = true;
2749d22c8396SJason M. Bills         }
2750d22c8396SJason M. Bills         else if (resetType == "PowerCycle")
2751d22c8396SJason M. Bills         {
275286a0851aSJason M. Bills             command = "xyz.openbmc_project.State.Host.Transition.Reboot";
275386a0851aSJason M. Bills             hostCommand = true;
2754cc340dd9SEd Tanous         }
2755bfd5b826SLakshminarayana R. Kammath         else if (resetType == "Nmi")
2756bfd5b826SLakshminarayana R. Kammath         {
2757bfd5b826SLakshminarayana R. Kammath             doNMI(asyncResp);
2758bfd5b826SLakshminarayana R. Kammath             return;
2759bfd5b826SLakshminarayana R. Kammath         }
2760cc340dd9SEd Tanous         else
2761cc340dd9SEd Tanous         {
27628d1b46d7Szhanghch05             messages::actionParameterUnknown(asyncResp->res, "Reset",
27638d1b46d7Szhanghch05                                              resetType);
2764cc340dd9SEd Tanous             return;
2765cc340dd9SEd Tanous         }
2766cc340dd9SEd Tanous 
2767d22c8396SJason M. Bills         if (hostCommand)
2768d22c8396SJason M. Bills         {
2769cc340dd9SEd Tanous             crow::connections::systemBus->async_method_call(
2770d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
2771cc340dd9SEd Tanous                 if (ec)
2772cc340dd9SEd Tanous                 {
2773cc340dd9SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
2774002d39b4SEd Tanous                     if (ec.value() == boost::asio::error::invalid_argument)
2775d22c8396SJason M. Bills                     {
2776d22c8396SJason M. Bills                         messages::actionParameterNotSupported(
2777d22c8396SJason M. Bills                             asyncResp->res, resetType, "Reset");
2778d22c8396SJason M. Bills                     }
2779d22c8396SJason M. Bills                     else
2780d22c8396SJason M. Bills                     {
2781f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2782d22c8396SJason M. Bills                     }
2783cc340dd9SEd Tanous                     return;
2784cc340dd9SEd Tanous                 }
2785f12894f8SJason M. Bills                 messages::success(asyncResp->res);
2786cc340dd9SEd Tanous                 },
2787cc340dd9SEd Tanous                 "xyz.openbmc_project.State.Host",
2788cc340dd9SEd Tanous                 "/xyz/openbmc_project/state/host0",
2789cc340dd9SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
27909712f8acSEd Tanous                 "xyz.openbmc_project.State.Host", "RequestedHostTransition",
2791168e20c1SEd Tanous                 dbus::utility::DbusVariantType{command});
2792cc340dd9SEd Tanous         }
2793d22c8396SJason M. Bills         else
2794d22c8396SJason M. Bills         {
2795d22c8396SJason M. Bills             crow::connections::systemBus->async_method_call(
2796d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
2797d22c8396SJason M. Bills                 if (ec)
2798d22c8396SJason M. Bills                 {
2799d22c8396SJason M. Bills                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
2800002d39b4SEd Tanous                     if (ec.value() == boost::asio::error::invalid_argument)
2801d22c8396SJason M. Bills                     {
2802d22c8396SJason M. Bills                         messages::actionParameterNotSupported(
2803d22c8396SJason M. Bills                             asyncResp->res, resetType, "Reset");
2804d22c8396SJason M. Bills                     }
2805d22c8396SJason M. Bills                     else
2806d22c8396SJason M. Bills                     {
2807d22c8396SJason M. Bills                         messages::internalError(asyncResp->res);
2808d22c8396SJason M. Bills                     }
2809d22c8396SJason M. Bills                     return;
2810d22c8396SJason M. Bills                 }
2811d22c8396SJason M. Bills                 messages::success(asyncResp->res);
2812d22c8396SJason M. Bills                 },
2813d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis",
2814d22c8396SJason M. Bills                 "/xyz/openbmc_project/state/chassis0",
2815d22c8396SJason M. Bills                 "org.freedesktop.DBus.Properties", "Set",
2816002d39b4SEd Tanous                 "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition",
2817168e20c1SEd Tanous                 dbus::utility::DbusVariantType{command});
2818d22c8396SJason M. Bills         }
28197e860f15SJohn Edward Broadbent         });
2820d22c8396SJason M. Bills }
2821cc340dd9SEd Tanous 
2822cc340dd9SEd Tanous /**
28236617338dSEd Tanous  * Systems derived class for delivering Computer Systems Schema.
2824c5b2abe0SLewanczyk, Dawid  */
28257e860f15SJohn Edward Broadbent inline void requestRoutesSystems(App& app)
28261abe55efSEd Tanous {
2827c5b2abe0SLewanczyk, Dawid 
2828c5b2abe0SLewanczyk, Dawid     /**
2829c5b2abe0SLewanczyk, Dawid      * Functions triggers appropriate requests on DBus
2830c5b2abe0SLewanczyk, Dawid      */
28317e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/")
2832ed398213SEd Tanous         .privileges(redfish::privileges::getComputerSystem)
2833002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2834002d39b4SEd Tanous             [&app](const crow::Request& req,
2835002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
28363ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
283745ca1b86SEd Tanous         {
283845ca1b86SEd Tanous             return;
283945ca1b86SEd Tanous         }
28408d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
284137bbf98cSChris Cain             "#ComputerSystem.v1_16_0.ComputerSystem";
28428d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "system";
28438d1b46d7Szhanghch05         asyncResp->res.jsonValue["Id"] = "system";
28448d1b46d7Szhanghch05         asyncResp->res.jsonValue["SystemType"] = "Physical";
28458d1b46d7Szhanghch05         asyncResp->res.jsonValue["Description"] = "Computer System";
28468d1b46d7Szhanghch05         asyncResp->res.jsonValue["ProcessorSummary"]["Count"] = 0;
28478d1b46d7Szhanghch05         asyncResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
28488d1b46d7Szhanghch05             "Disabled";
28498d1b46d7Szhanghch05         asyncResp->res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] =
28508d1b46d7Szhanghch05             uint64_t(0);
28518d1b46d7Szhanghch05         asyncResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
28528d1b46d7Szhanghch05             "Disabled";
2853002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Systems/system";
285404a258f4SEd Tanous 
28551476687dSEd Tanous         asyncResp->res.jsonValue["Processors"]["@odata.id"] =
28561476687dSEd Tanous             "/redfish/v1/Systems/system/Processors";
28571476687dSEd Tanous         asyncResp->res.jsonValue["Memory"]["@odata.id"] =
28581476687dSEd Tanous             "/redfish/v1/Systems/system/Memory";
28591476687dSEd Tanous         asyncResp->res.jsonValue["Storage"]["@odata.id"] =
28601476687dSEd Tanous             "/redfish/v1/Systems/system/Storage";
2861029573d4SEd Tanous 
2862002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"]["target"] =
28631476687dSEd Tanous             "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset";
28641476687dSEd Tanous         asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"]
28651476687dSEd Tanous                                 ["@Redfish.ActionInfo"] =
28661476687dSEd Tanous             "/redfish/v1/Systems/system/ResetActionInfo";
2867c5b2abe0SLewanczyk, Dawid 
28681476687dSEd Tanous         asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
28691476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices";
28701476687dSEd Tanous         asyncResp->res.jsonValue["Bios"]["@odata.id"] =
28711476687dSEd Tanous             "/redfish/v1/Systems/system/Bios";
2872c4bf6374SJason M. Bills 
28731476687dSEd Tanous         nlohmann::json::array_t managedBy;
28741476687dSEd Tanous         nlohmann::json& manager = managedBy.emplace_back();
28751476687dSEd Tanous         manager["@odata.id"] = "/redfish/v1/Managers/bmc";
2876002d39b4SEd Tanous         asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy);
28771476687dSEd Tanous         asyncResp->res.jsonValue["Status"]["Health"] = "OK";
28781476687dSEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
28790e8ac5e7SGunnar Mills 
28800e8ac5e7SGunnar Mills         // Fill in SerialConsole info
2881002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
2882002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["IPMI"]["ServiceEnabled"] =
2883002d39b4SEd Tanous             true;
28841476687dSEd Tanous 
28850e8ac5e7SGunnar Mills         // TODO (Gunnar): Should look for obmc-console-ssh@2200.service
28861476687dSEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["SSH"]["ServiceEnabled"] =
28871476687dSEd Tanous             true;
28881476687dSEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["SSH"]["Port"] = 2200;
28891476687dSEd Tanous         asyncResp->res
28901476687dSEd Tanous             .jsonValue["SerialConsole"]["SSH"]["HotKeySequenceDisplay"] =
28911476687dSEd Tanous             "Press ~. to exit console";
28920e8ac5e7SGunnar Mills 
28930e8ac5e7SGunnar Mills #ifdef BMCWEB_ENABLE_KVM
28940e8ac5e7SGunnar Mills         // Fill in GraphicalConsole info
2895002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
2896002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
2897002d39b4SEd Tanous             4;
28981476687dSEd Tanous         asyncResp->res
2899002d39b4SEd Tanous             .jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
29001476687dSEd Tanous 
29010e8ac5e7SGunnar Mills #endif // BMCWEB_ENABLE_KVM
2902e284a7c1SJames Feist         constexpr const std::array<const char*, 4> inventoryForSystems = {
2903b49ac873SJames Feist             "xyz.openbmc_project.Inventory.Item.Dimm",
29042ad9c2f6SJames Feist             "xyz.openbmc_project.Inventory.Item.Cpu",
2905e284a7c1SJames Feist             "xyz.openbmc_project.Inventory.Item.Drive",
2906e284a7c1SJames Feist             "xyz.openbmc_project.Inventory.Item.StorageController"};
2907b49ac873SJames Feist 
2908b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
2909b49ac873SJames Feist         crow::connections::systemBus->async_method_call(
2910b49ac873SJames Feist             [health](const boost::system::error_code ec,
2911914e2d5dSEd Tanous                      const std::vector<std::string>& resp) {
2912b49ac873SJames Feist             if (ec)
2913b49ac873SJames Feist             {
2914b49ac873SJames Feist                 // no inventory
2915b49ac873SJames Feist                 return;
2916b49ac873SJames Feist             }
2917b49ac873SJames Feist 
2918914e2d5dSEd Tanous             health->inventory = resp;
2919b49ac873SJames Feist             },
2920b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper",
2921b49ac873SJames Feist             "/xyz/openbmc_project/object_mapper",
2922b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
2923b49ac873SJames Feist             int32_t(0), inventoryForSystems);
2924b49ac873SJames Feist 
2925b49ac873SJames Feist         health->populate();
2926b49ac873SJames Feist 
2927002d39b4SEd Tanous         getMainChassisId(asyncResp,
2928002d39b4SEd Tanous                          [](const std::string& chassisId,
29298d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2930b2c7e208SEd Tanous             nlohmann::json::array_t chassisArray;
2931b2c7e208SEd Tanous             nlohmann::json& chassis = chassisArray.emplace_back();
2932b2c7e208SEd Tanous             chassis["@odata.id"] = "/redfish/v1/Chassis/" + chassisId;
2933002d39b4SEd Tanous             aRsp->res.jsonValue["Links"]["Chassis"] = std::move(chassisArray);
2934c5d03ff4SJennifer Lee         });
2935a3002228SAppaRao Puli 
29369f8bfa7cSGunnar Mills         getLocationIndicatorActive(asyncResp);
29379f8bfa7cSGunnar Mills         // TODO (Gunnar): Remove IndicatorLED after enough time has passed
2938a3002228SAppaRao Puli         getIndicatorLedState(asyncResp);
29395bc2dc8eSJames Feist         getComputerSystem(asyncResp, health);
29406c34de48SEd Tanous         getHostState(asyncResp);
2941491d8ee7SSantosh Puranik         getBootProperties(asyncResp);
2942978b8803SAndrew Geissler         getBootProgress(asyncResp);
2943adbe192aSJason M. Bills         getPCIeDeviceList(asyncResp, "PCIeDevices");
294451709ffdSYong Li         getHostWatchdogTimer(asyncResp);
2945c6a620f2SGeorge Liu         getPowerRestorePolicy(asyncResp);
29466bd5a8d2SGunnar Mills         getAutomaticRetry(asyncResp);
2947c0557e1aSGunnar Mills         getLastResetTime(asyncResp);
2948a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
2949a6349918SAppaRao Puli         getProvisioningStatus(asyncResp);
2950a6349918SAppaRao Puli #endif
29511981771bSAli Ahmed         getTrustedModuleRequiredToBoot(asyncResp);
29523a2d0424SChris Cain         getPowerMode(asyncResp);
295337bbf98cSChris Cain         getIdlePowerSaver(asyncResp);
29547e860f15SJohn Edward Broadbent         });
2955550a6bf8SJiaqing Zhao 
29567e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/")
2957ed398213SEd Tanous         .privileges(redfish::privileges::patchComputerSystem)
29587e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
295945ca1b86SEd Tanous             [&app](const crow::Request& req,
29607e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
29613ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
296245ca1b86SEd Tanous         {
296345ca1b86SEd Tanous             return;
296445ca1b86SEd Tanous         }
29659f8bfa7cSGunnar Mills         std::optional<bool> locationIndicatorActive;
2966cde19e5fSSantosh Puranik         std::optional<std::string> indicatorLed;
296798e386ecSGunnar Mills         std::optional<std::string> assetTag;
2968c6a620f2SGeorge Liu         std::optional<std::string> powerRestorePolicy;
29693a2d0424SChris Cain         std::optional<std::string> powerMode;
2970550a6bf8SJiaqing Zhao         std::optional<bool> wdtEnable;
2971550a6bf8SJiaqing Zhao         std::optional<std::string> wdtTimeOutAction;
2972550a6bf8SJiaqing Zhao         std::optional<std::string> bootSource;
2973550a6bf8SJiaqing Zhao         std::optional<std::string> bootType;
2974550a6bf8SJiaqing Zhao         std::optional<std::string> bootEnable;
2975550a6bf8SJiaqing Zhao         std::optional<std::string> bootAutomaticRetry;
2976550a6bf8SJiaqing Zhao         std::optional<bool> bootTrustedModuleRequired;
2977550a6bf8SJiaqing Zhao         std::optional<bool> ipsEnable;
2978550a6bf8SJiaqing Zhao         std::optional<uint8_t> ipsEnterUtil;
2979550a6bf8SJiaqing Zhao         std::optional<uint64_t> ipsEnterTime;
2980550a6bf8SJiaqing Zhao         std::optional<uint8_t> ipsExitUtil;
2981550a6bf8SJiaqing Zhao         std::optional<uint64_t> ipsExitTime;
2982550a6bf8SJiaqing Zhao 
2983550a6bf8SJiaqing Zhao         // clang-format off
298415ed6780SWilly Tu         if (!json_util::readJsonPatch(
2985550a6bf8SJiaqing Zhao                 req, asyncResp->res,
2986550a6bf8SJiaqing Zhao                 "IndicatorLED", indicatorLed,
29877e860f15SJohn Edward Broadbent                 "LocationIndicatorActive", locationIndicatorActive,
2988550a6bf8SJiaqing Zhao                 "AssetTag", assetTag,
2989550a6bf8SJiaqing Zhao                 "PowerRestorePolicy", powerRestorePolicy,
2990550a6bf8SJiaqing Zhao                 "PowerMode", powerMode,
2991550a6bf8SJiaqing Zhao                 "HostWatchdogTimer/FunctionEnabled", wdtEnable,
2992550a6bf8SJiaqing Zhao                 "HostWatchdogTimer/TimeoutAction", wdtTimeOutAction,
2993550a6bf8SJiaqing Zhao                 "Boot/BootSourceOverrideTarget", bootSource,
2994550a6bf8SJiaqing Zhao                 "Boot/BootSourceOverrideMode", bootType,
2995550a6bf8SJiaqing Zhao                 "Boot/BootSourceOverrideEnabled", bootEnable,
2996550a6bf8SJiaqing Zhao                 "Boot/AutomaticRetryConfig", bootAutomaticRetry,
2997550a6bf8SJiaqing Zhao                 "Boot/TrustedModuleRequiredToBoot", bootTrustedModuleRequired,
2998550a6bf8SJiaqing Zhao                 "IdlePowerSaver/Enabled", ipsEnable,
2999550a6bf8SJiaqing Zhao                 "IdlePowerSaver/EnterUtilizationPercent", ipsEnterUtil,
3000550a6bf8SJiaqing Zhao                 "IdlePowerSaver/EnterDwellTimeSeconds", ipsEnterTime,
3001550a6bf8SJiaqing Zhao                 "IdlePowerSaver/ExitUtilizationPercent", ipsExitUtil,
3002550a6bf8SJiaqing Zhao                 "IdlePowerSaver/ExitDwellTimeSeconds", ipsExitTime))
30036617338dSEd Tanous         {
30046617338dSEd Tanous             return;
30056617338dSEd Tanous         }
3006550a6bf8SJiaqing Zhao         // clang-format on
3007491d8ee7SSantosh Puranik 
30088d1b46d7Szhanghch05         asyncResp->res.result(boost::beast::http::status::no_content);
3009c45f0082SYong Li 
301098e386ecSGunnar Mills         if (assetTag)
301198e386ecSGunnar Mills         {
301298e386ecSGunnar Mills             setAssetTag(asyncResp, *assetTag);
301398e386ecSGunnar Mills         }
301498e386ecSGunnar Mills 
3015550a6bf8SJiaqing Zhao         if (wdtEnable || wdtTimeOutAction)
3016c45f0082SYong Li         {
3017f23b7296SEd Tanous             setWDTProperties(asyncResp, wdtEnable, wdtTimeOutAction);
3018c45f0082SYong Li         }
3019c45f0082SYong Li 
3020cd9a4666SKonstantin Aladyshev         if (bootSource || bootType || bootEnable)
302169f35306SGunnar Mills         {
3022002d39b4SEd Tanous             setBootProperties(asyncResp, bootSource, bootType, bootEnable);
3023491d8ee7SSantosh Puranik         }
3024550a6bf8SJiaqing Zhao         if (bootAutomaticRetry)
302569f35306SGunnar Mills         {
3026550a6bf8SJiaqing Zhao             setAutomaticRetry(asyncResp, *bootAutomaticRetry);
302769f35306SGunnar Mills         }
3028ac7e1e0bSAli Ahmed 
3029550a6bf8SJiaqing Zhao         if (bootTrustedModuleRequired)
3030ac7e1e0bSAli Ahmed         {
3031550a6bf8SJiaqing Zhao             setTrustedModuleRequiredToBoot(asyncResp,
3032550a6bf8SJiaqing Zhao                                            *bootTrustedModuleRequired);
303369f35306SGunnar Mills         }
3034265c1602SJohnathan Mantey 
30359f8bfa7cSGunnar Mills         if (locationIndicatorActive)
30369f8bfa7cSGunnar Mills         {
3037002d39b4SEd Tanous             setLocationIndicatorActive(asyncResp, *locationIndicatorActive);
30389f8bfa7cSGunnar Mills         }
30399f8bfa7cSGunnar Mills 
30407e860f15SJohn Edward Broadbent         // TODO (Gunnar): Remove IndicatorLED after enough time has
30417e860f15SJohn Edward Broadbent         // passed
30429712f8acSEd Tanous         if (indicatorLed)
30436617338dSEd Tanous         {
3044f23b7296SEd Tanous             setIndicatorLedState(asyncResp, *indicatorLed);
3045002d39b4SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::warning,
3046d6aa0093SGunnar Mills                                      "299 - \"IndicatorLED is deprecated. Use "
3047d6aa0093SGunnar Mills                                      "LocationIndicatorActive instead.\"");
30486617338dSEd Tanous         }
3049c6a620f2SGeorge Liu 
3050c6a620f2SGeorge Liu         if (powerRestorePolicy)
3051c6a620f2SGeorge Liu         {
30524e69c904SGunnar Mills             setPowerRestorePolicy(asyncResp, *powerRestorePolicy);
3053c6a620f2SGeorge Liu         }
30543a2d0424SChris Cain 
30553a2d0424SChris Cain         if (powerMode)
30563a2d0424SChris Cain         {
30573a2d0424SChris Cain             setPowerMode(asyncResp, *powerMode);
30583a2d0424SChris Cain         }
305937bbf98cSChris Cain 
3060550a6bf8SJiaqing Zhao         if (ipsEnable || ipsEnterUtil || ipsEnterTime || ipsExitUtil ||
3061550a6bf8SJiaqing Zhao             ipsExitTime)
306237bbf98cSChris Cain         {
3063002d39b4SEd Tanous             setIdlePowerSaver(asyncResp, ipsEnable, ipsEnterUtil, ipsEnterTime,
3064002d39b4SEd Tanous                               ipsExitUtil, ipsExitTime);
306537bbf98cSChris Cain         }
30667e860f15SJohn Edward Broadbent         });
3067c5b2abe0SLewanczyk, Dawid }
30681cb1a9e6SAppaRao Puli 
30691cb1a9e6SAppaRao Puli /**
30701cb1a9e6SAppaRao Puli  * SystemResetActionInfo derived class for delivering Computer Systems
30711cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
30721cb1a9e6SAppaRao Puli  */
30737e860f15SJohn Edward Broadbent inline void requestRoutesSystemResetActionInfo(App& app)
30741cb1a9e6SAppaRao Puli {
30751cb1a9e6SAppaRao Puli     /**
30761cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
30771cb1a9e6SAppaRao Puli      */
30787e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/ResetActionInfo/")
3079ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
30807e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
308145ca1b86SEd Tanous             [&app](const crow::Request& req,
30827e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
30833ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
308445ca1b86SEd Tanous         {
308545ca1b86SEd Tanous             return;
308645ca1b86SEd Tanous         }
30871476687dSEd Tanous 
30881476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
30891476687dSEd Tanous             "/redfish/v1/Systems/system/ResetActionInfo";
30901476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
30911476687dSEd Tanous             "#ActionInfo.v1_1_2.ActionInfo";
30921476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "Reset Action Info";
30931476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
30943215e700SNan Zhou 
30953215e700SNan Zhou         nlohmann::json::array_t parameters;
30963215e700SNan Zhou         nlohmann::json::object_t parameter;
30973215e700SNan Zhou 
30983215e700SNan Zhou         parameter["Name"] = "ResetType";
30993215e700SNan Zhou         parameter["Required"] = true;
31003215e700SNan Zhou         parameter["DataType"] = "String";
31013215e700SNan Zhou         nlohmann::json::array_t allowableValues;
31023215e700SNan Zhou         allowableValues.emplace_back("On");
31033215e700SNan Zhou         allowableValues.emplace_back("ForceOff");
31043215e700SNan Zhou         allowableValues.emplace_back("ForceOn");
31053215e700SNan Zhou         allowableValues.emplace_back("ForceRestart");
31063215e700SNan Zhou         allowableValues.emplace_back("GracefulRestart");
31073215e700SNan Zhou         allowableValues.emplace_back("GracefulShutdown");
31083215e700SNan Zhou         allowableValues.emplace_back("PowerCycle");
31093215e700SNan Zhou         allowableValues.emplace_back("Nmi");
31103215e700SNan Zhou         parameter["AllowableValues"] = std::move(allowableValues);
31113215e700SNan Zhou         parameters.emplace_back(std::move(parameter));
31123215e700SNan Zhou 
31133215e700SNan Zhou         asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
31147e860f15SJohn Edward Broadbent         });
31151cb1a9e6SAppaRao Puli }
3116c5b2abe0SLewanczyk, Dawid } // namespace redfish
3117