xref: /openbmc/bmcweb/features/redfish/lib/systems.hpp (revision 1214b7e7d921e331fb1480c7e5d579ffa5811cda)
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 
18b49ac873SJames Feist #include "health.hpp"
191c8fba97SJames Feist #include "led.hpp"
20f5c9f8bdSJason M. Bills #include "pcie.hpp"
21c5d03ff4SJennifer Lee #include "redfish_util.hpp"
22c5d03ff4SJennifer Lee 
239712f8acSEd Tanous #include <boost/container/flat_map.hpp>
249712f8acSEd Tanous #include <node.hpp>
25cb7e1e7bSAndrew Geissler #include <utils/fw_utils.hpp>
26c5b2abe0SLewanczyk, Dawid #include <utils/json_utils.hpp>
27*1214b7e7SGunnar Mills 
28abf2add6SEd Tanous #include <variant>
29c5b2abe0SLewanczyk, Dawid 
301abe55efSEd Tanous namespace redfish
311abe55efSEd Tanous {
32c5b2abe0SLewanczyk, Dawid 
339d3ae10eSAlpana Kumari /**
349d3ae10eSAlpana Kumari  * @brief Updates the Functional State of DIMMs
359d3ae10eSAlpana Kumari  *
369d3ae10eSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
379d3ae10eSAlpana Kumari  * @param[in] dimmState Dimm's Functional state, true/false
389d3ae10eSAlpana Kumari  *
399d3ae10eSAlpana Kumari  * @return None.
409d3ae10eSAlpana Kumari  */
419d3ae10eSAlpana Kumari void updateDimmProperties(std::shared_ptr<AsyncResp> aResp,
429d3ae10eSAlpana Kumari                           const std::variant<bool>& dimmState)
439d3ae10eSAlpana Kumari {
449d3ae10eSAlpana Kumari     const bool* isDimmFunctional = std::get_if<bool>(&dimmState);
459d3ae10eSAlpana Kumari     if (isDimmFunctional == nullptr)
469d3ae10eSAlpana Kumari     {
479d3ae10eSAlpana Kumari         messages::internalError(aResp->res);
489d3ae10eSAlpana Kumari         return;
499d3ae10eSAlpana Kumari     }
509d3ae10eSAlpana Kumari     BMCWEB_LOG_DEBUG << "Dimm Functional: " << *isDimmFunctional;
519d3ae10eSAlpana Kumari 
529d3ae10eSAlpana Kumari     // Set it as Enabled if atleast one DIMM is functional
539d3ae10eSAlpana Kumari     // Update STATE only if previous State was DISABLED and current Dimm is
549d3ae10eSAlpana Kumari     // ENABLED.
559d3ae10eSAlpana Kumari     nlohmann::json& prevMemSummary =
569d3ae10eSAlpana Kumari         aResp->res.jsonValue["MemorySummary"]["Status"]["State"];
579d3ae10eSAlpana Kumari     if (prevMemSummary == "Disabled")
589d3ae10eSAlpana Kumari     {
599d3ae10eSAlpana Kumari         if (*isDimmFunctional == true)
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  */
7557e8c9beSAlpana Kumari void modifyCpuPresenceState(std::shared_ptr<AsyncResp> aResp,
7657e8c9beSAlpana Kumari                             const std::variant<bool>& cpuPresenceState)
7757e8c9beSAlpana Kumari {
7857e8c9beSAlpana Kumari     const bool* isCpuPresent = std::get_if<bool>(&cpuPresenceState);
7957e8c9beSAlpana Kumari 
8057e8c9beSAlpana Kumari     if (isCpuPresent == nullptr)
8157e8c9beSAlpana Kumari     {
8257e8c9beSAlpana Kumari         messages::internalError(aResp->res);
8357e8c9beSAlpana Kumari         return;
8457e8c9beSAlpana Kumari     }
8557e8c9beSAlpana Kumari     BMCWEB_LOG_DEBUG << "Cpu Present: " << *isCpuPresent;
8657e8c9beSAlpana Kumari 
8757e8c9beSAlpana Kumari     if (*isCpuPresent == true)
8857e8c9beSAlpana Kumari     {
89b4b9595aSJames Feist         nlohmann::json& procCount =
90b4b9595aSJames Feist             aResp->res.jsonValue["ProcessorSummary"]["Count"];
91b4b9595aSJames Feist         auto procCountPtr =
92b4b9595aSJames Feist             procCount.get_ptr<nlohmann::json::number_integer_t*>();
93b4b9595aSJames Feist         if (procCountPtr != nullptr)
94b4b9595aSJames Feist         {
95b4b9595aSJames Feist             // shouldn't be possible to be nullptr
96b4b9595aSJames Feist             *procCountPtr += 1;
9757e8c9beSAlpana Kumari         }
98b4b9595aSJames Feist     }
9957e8c9beSAlpana Kumari }
10057e8c9beSAlpana Kumari 
10157e8c9beSAlpana Kumari /*
10257e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Status" "State" based on
10357e8c9beSAlpana Kumari  *        CPU Functional State
10457e8c9beSAlpana Kumari  *
10557e8c9beSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
10657e8c9beSAlpana Kumari  * @param[in] cpuFunctionalState is CPU functional true/false
10757e8c9beSAlpana Kumari  *
10857e8c9beSAlpana Kumari  * @return None.
10957e8c9beSAlpana Kumari  */
11057e8c9beSAlpana Kumari void modifyCpuFunctionalState(std::shared_ptr<AsyncResp> aResp,
11157e8c9beSAlpana Kumari                               const std::variant<bool>& cpuFunctionalState)
11257e8c9beSAlpana Kumari {
11357e8c9beSAlpana Kumari     const bool* isCpuFunctional = std::get_if<bool>(&cpuFunctionalState);
11457e8c9beSAlpana Kumari 
11557e8c9beSAlpana Kumari     if (isCpuFunctional == nullptr)
11657e8c9beSAlpana Kumari     {
11757e8c9beSAlpana Kumari         messages::internalError(aResp->res);
11857e8c9beSAlpana Kumari         return;
11957e8c9beSAlpana Kumari     }
12057e8c9beSAlpana Kumari     BMCWEB_LOG_DEBUG << "Cpu Functional: " << *isCpuFunctional;
12157e8c9beSAlpana Kumari 
12257e8c9beSAlpana Kumari     nlohmann::json& prevProcState =
12357e8c9beSAlpana Kumari         aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"];
12457e8c9beSAlpana Kumari 
12557e8c9beSAlpana Kumari     // Set it as Enabled if atleast one CPU is functional
12657e8c9beSAlpana Kumari     // Update STATE only if previous State was Non_Functional and current CPU is
12757e8c9beSAlpana Kumari     // Functional.
12857e8c9beSAlpana Kumari     if (prevProcState == "Disabled")
12957e8c9beSAlpana Kumari     {
13057e8c9beSAlpana Kumari         if (*isCpuFunctional == true)
13157e8c9beSAlpana Kumari         {
13257e8c9beSAlpana Kumari             aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
13357e8c9beSAlpana Kumari                 "Enabled";
13457e8c9beSAlpana Kumari         }
13557e8c9beSAlpana Kumari     }
13657e8c9beSAlpana Kumari }
13757e8c9beSAlpana Kumari 
13857e8c9beSAlpana Kumari /*
139c5b2abe0SLewanczyk, Dawid  * @brief Retrieves computer system properties over dbus
140c5b2abe0SLewanczyk, Dawid  *
141c5b2abe0SLewanczyk, Dawid  * @param[in] aResp Shared pointer for completing asynchronous calls
142c5b2abe0SLewanczyk, Dawid  * @param[in] name  Computer system name from request
143c5b2abe0SLewanczyk, Dawid  *
144c5b2abe0SLewanczyk, Dawid  * @return None.
145c5b2abe0SLewanczyk, Dawid  */
1465bc2dc8eSJames Feist void getComputerSystem(std::shared_ptr<AsyncResp> aResp,
1475bc2dc8eSJames Feist                        std::shared_ptr<HealthPopulate> systemHealth)
1481abe55efSEd Tanous {
14955c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get available system components.";
1509d3ae10eSAlpana Kumari 
15155c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
1525bc2dc8eSJames Feist         [aResp, systemHealth](
153c5b2abe0SLewanczyk, Dawid             const boost::system::error_code ec,
154c5b2abe0SLewanczyk, Dawid             const std::vector<std::pair<
1556c34de48SEd Tanous                 std::string,
156*1214b7e7SGunnar Mills                 std::vector<std::pair<std::string, std::vector<std::string>>>>>&
157*1214b7e7SGunnar Mills                 subtree) {
1581abe55efSEd Tanous             if (ec)
1591abe55efSEd Tanous             {
16055c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error";
161f12894f8SJason M. Bills                 messages::internalError(aResp->res);
162c5b2abe0SLewanczyk, Dawid                 return;
163c5b2abe0SLewanczyk, Dawid             }
164c5b2abe0SLewanczyk, Dawid             // Iterate over all retrieved ObjectPaths.
1656c34de48SEd Tanous             for (const std::pair<std::string,
1666c34de48SEd Tanous                                  std::vector<std::pair<
167*1214b7e7SGunnar Mills                                      std::string, std::vector<std::string>>>>&
168*1214b7e7SGunnar Mills                      object : subtree)
1691abe55efSEd Tanous             {
170c5b2abe0SLewanczyk, Dawid                 const std::string& path = object.first;
17155c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "Got path: " << path;
1721abe55efSEd Tanous                 const std::vector<
173*1214b7e7SGunnar Mills                     std::pair<std::string, std::vector<std::string>>>&
174*1214b7e7SGunnar Mills                     connectionNames = object.second;
1751abe55efSEd Tanous                 if (connectionNames.size() < 1)
1761abe55efSEd Tanous                 {
177c5b2abe0SLewanczyk, Dawid                     continue;
178c5b2abe0SLewanczyk, Dawid                 }
179029573d4SEd Tanous 
1805bc2dc8eSJames Feist                 auto memoryHealth = std::make_shared<HealthPopulate>(
1815bc2dc8eSJames Feist                     aResp, aResp->res.jsonValue["MemorySummary"]["Status"]);
1825bc2dc8eSJames Feist 
1835bc2dc8eSJames Feist                 auto cpuHealth = std::make_shared<HealthPopulate>(
1845bc2dc8eSJames Feist                     aResp, aResp->res.jsonValue["ProcessorSummary"]["Status"]);
1855bc2dc8eSJames Feist 
1865bc2dc8eSJames Feist                 systemHealth->children.emplace_back(memoryHealth);
1875bc2dc8eSJames Feist                 systemHealth->children.emplace_back(cpuHealth);
1885bc2dc8eSJames Feist 
1896c34de48SEd Tanous                 // This is not system, so check if it's cpu, dimm, UUID or
1906c34de48SEd Tanous                 // BiosVer
19104a258f4SEd Tanous                 for (const auto& connection : connectionNames)
1921abe55efSEd Tanous                 {
19304a258f4SEd Tanous                     for (const auto& interfaceName : connection.second)
1941abe55efSEd Tanous                     {
19504a258f4SEd Tanous                         if (interfaceName ==
19604a258f4SEd Tanous                             "xyz.openbmc_project.Inventory.Item.Dimm")
1971abe55efSEd Tanous                         {
1981abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
19904a258f4SEd Tanous                                 << "Found Dimm, now get its properties.";
2009d3ae10eSAlpana Kumari 
20155c7b7a2SEd Tanous                             crow::connections::systemBus->async_method_call(
2029d3ae10eSAlpana Kumari                                 [aResp, service{connection.first},
2039d3ae10eSAlpana Kumari                                  path(std::move(path))](
2049d3ae10eSAlpana Kumari                                     const boost::system::error_code ec,
2056c34de48SEd Tanous                                     const std::vector<
206*1214b7e7SGunnar Mills                                         std::pair<std::string, VariantType>>&
207*1214b7e7SGunnar Mills                                         properties) {
2081abe55efSEd Tanous                                     if (ec)
2091abe55efSEd Tanous                                     {
2101abe55efSEd Tanous                                         BMCWEB_LOG_ERROR
2116c34de48SEd Tanous                                             << "DBUS response error " << ec;
212f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
213c5b2abe0SLewanczyk, Dawid                                         return;
214c5b2abe0SLewanczyk, Dawid                                     }
2156c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
2166c34de48SEd Tanous                                                      << properties.size()
217c5b2abe0SLewanczyk, Dawid                                                      << " Dimm properties.";
2189d3ae10eSAlpana Kumari 
2199d3ae10eSAlpana Kumari                                     if (properties.size() > 0)
2209d3ae10eSAlpana Kumari                                     {
22104a258f4SEd Tanous                                         for (const std::pair<std::string,
222*1214b7e7SGunnar Mills                                                              VariantType>&
223*1214b7e7SGunnar Mills                                                  property : properties)
2241abe55efSEd Tanous                                         {
2255fd7ba65SCheng C Yang                                             if (property.first !=
2265fd7ba65SCheng C Yang                                                 "MemorySizeInKB")
2271abe55efSEd Tanous                                             {
2285fd7ba65SCheng C Yang                                                 continue;
2295fd7ba65SCheng C Yang                                             }
2305fd7ba65SCheng C Yang                                             const uint32_t* value =
2318d78b7a9SPatrick Williams                                                 std::get_if<uint32_t>(
2321b6b96c5SEd Tanous                                                     &property.second);
2335fd7ba65SCheng C Yang                                             if (value == nullptr)
2341abe55efSEd Tanous                                             {
2355fd7ba65SCheng C Yang                                                 BMCWEB_LOG_DEBUG
2365fd7ba65SCheng C Yang                                                     << "Find incorrect type of "
2375fd7ba65SCheng C Yang                                                        "MemorySize";
2385fd7ba65SCheng C Yang                                                 continue;
2395fd7ba65SCheng C Yang                                             }
2405fd7ba65SCheng C Yang                                             nlohmann::json& totalMemory =
2415fd7ba65SCheng C Yang                                                 aResp->res
2425fd7ba65SCheng C Yang                                                     .jsonValue["MemorySummar"
2435fd7ba65SCheng C Yang                                                                "y"]
2445fd7ba65SCheng C Yang                                                               ["TotalSystemMe"
2455fd7ba65SCheng C Yang                                                                "moryGiB"];
2465fd7ba65SCheng C Yang                                             uint64_t* preValue =
2475fd7ba65SCheng C Yang                                                 totalMemory
2485fd7ba65SCheng C Yang                                                     .get_ptr<uint64_t*>();
2495fd7ba65SCheng C Yang                                             if (preValue == nullptr)
2505fd7ba65SCheng C Yang                                             {
2515fd7ba65SCheng C Yang                                                 continue;
2525fd7ba65SCheng C Yang                                             }
2535fd7ba65SCheng C Yang                                             aResp->res
2545fd7ba65SCheng C Yang                                                 .jsonValue["MemorySummary"]
2556c34de48SEd Tanous                                                           ["TotalSystemMemoryGi"
2565fd7ba65SCheng C Yang                                                            "B"] =
2575fd7ba65SCheng C Yang                                                 *value / (1024 * 1024) +
2585fd7ba65SCheng C Yang                                                 *preValue;
2595fd7ba65SCheng C Yang                                             aResp->res
2605fd7ba65SCheng C Yang                                                 .jsonValue["MemorySummary"]
2619d3ae10eSAlpana Kumari                                                           ["Status"]["State"] =
2621abe55efSEd Tanous                                                 "Enabled";
263c5b2abe0SLewanczyk, Dawid                                         }
264c5b2abe0SLewanczyk, Dawid                                     }
2659d3ae10eSAlpana Kumari                                     else
2669d3ae10eSAlpana Kumari                                     {
2679d3ae10eSAlpana Kumari                                         auto getDimmProperties =
2689d3ae10eSAlpana Kumari                                             [aResp](
2699d3ae10eSAlpana Kumari                                                 const boost::system::error_code
2709d3ae10eSAlpana Kumari                                                     ec,
271*1214b7e7SGunnar Mills                                                 const std::variant<bool>&
272*1214b7e7SGunnar Mills                                                     dimmState) {
2739d3ae10eSAlpana Kumari                                                 if (ec)
2749d3ae10eSAlpana Kumari                                                 {
2759d3ae10eSAlpana Kumari                                                     BMCWEB_LOG_ERROR
2769d3ae10eSAlpana Kumari                                                         << "DBUS response "
2779d3ae10eSAlpana Kumari                                                            "error "
2789d3ae10eSAlpana Kumari                                                         << ec;
2799d3ae10eSAlpana Kumari                                                     return;
2809d3ae10eSAlpana Kumari                                                 }
2819d3ae10eSAlpana Kumari                                                 updateDimmProperties(aResp,
2829d3ae10eSAlpana Kumari                                                                      dimmState);
2839d3ae10eSAlpana Kumari                                             };
2849d3ae10eSAlpana Kumari                                         crow::connections::systemBus
2859d3ae10eSAlpana Kumari                                             ->async_method_call(
2869d3ae10eSAlpana Kumari                                                 std::move(getDimmProperties),
2879d3ae10eSAlpana Kumari                                                 service, path,
2889d3ae10eSAlpana Kumari                                                 "org.freedesktop.DBus."
2899d3ae10eSAlpana Kumari                                                 "Properties",
2909d3ae10eSAlpana Kumari                                                 "Get",
2919d3ae10eSAlpana Kumari                                                 "xyz.openbmc_project.State."
2929d3ae10eSAlpana Kumari                                                 "Decorator.OperationalStatus",
2939d3ae10eSAlpana Kumari                                                 "Functional");
2949d3ae10eSAlpana Kumari                                     }
295c5b2abe0SLewanczyk, Dawid                                 },
29604a258f4SEd Tanous                                 connection.first, path,
2976c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
2986c34de48SEd Tanous                                 "xyz.openbmc_project.Inventory.Item.Dimm");
2995bc2dc8eSJames Feist 
3005bc2dc8eSJames Feist                             memoryHealth->inventory.emplace_back(path);
3011abe55efSEd Tanous                         }
30204a258f4SEd Tanous                         else if (interfaceName ==
30304a258f4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.Cpu")
3041abe55efSEd Tanous                         {
3051abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
30604a258f4SEd Tanous                                 << "Found Cpu, now get its properties.";
30757e8c9beSAlpana Kumari 
308a0803efaSEd Tanous                             crow::connections::systemBus->async_method_call(
30957e8c9beSAlpana Kumari                                 [aResp, service{connection.first},
31057e8c9beSAlpana Kumari                                  path(std::move(path))](
31157e8c9beSAlpana Kumari                                     const boost::system::error_code ec,
3126c34de48SEd Tanous                                     const std::vector<
313*1214b7e7SGunnar Mills                                         std::pair<std::string, VariantType>>&
314*1214b7e7SGunnar Mills                                         properties) {
3151abe55efSEd Tanous                                     if (ec)
3161abe55efSEd Tanous                                     {
3171abe55efSEd Tanous                                         BMCWEB_LOG_ERROR
3186c34de48SEd Tanous                                             << "DBUS response error " << ec;
319f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
320c5b2abe0SLewanczyk, Dawid                                         return;
321c5b2abe0SLewanczyk, Dawid                                     }
3226c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
3236c34de48SEd Tanous                                                      << properties.size()
324c5b2abe0SLewanczyk, Dawid                                                      << " Cpu properties.";
32557e8c9beSAlpana Kumari 
32657e8c9beSAlpana Kumari                                     if (properties.size() > 0)
32757e8c9beSAlpana Kumari                                     {
32804a258f4SEd Tanous                                         for (const auto& property : properties)
3291abe55efSEd Tanous                                         {
33057e8c9beSAlpana Kumari                                             if (property.first ==
33157e8c9beSAlpana Kumari                                                 "ProcessorFamily")
3321abe55efSEd Tanous                                             {
333a0803efaSEd Tanous                                                 const std::string* value =
3348d78b7a9SPatrick Williams                                                     std::get_if<std::string>(
3351b6b96c5SEd Tanous                                                         &property.second);
3361abe55efSEd Tanous                                                 if (value != nullptr)
3371abe55efSEd Tanous                                                 {
338*1214b7e7SGunnar Mills                                                     nlohmann::json&
339*1214b7e7SGunnar Mills                                                         procSummary =
3401abe55efSEd Tanous                                                             aResp->res.jsonValue
3416c34de48SEd Tanous                                                                 ["ProcessorSumm"
34204a258f4SEd Tanous                                                                  "ary"];
34304a258f4SEd Tanous                                                     nlohmann::json& procCount =
34404a258f4SEd Tanous                                                         procSummary["Count"];
345b4b9595aSJames Feist 
346b4b9595aSJames Feist                                                     auto procCountPtr =
347b4b9595aSJames Feist                                                         procCount.get_ptr<
348b4b9595aSJames Feist                                                             nlohmann::json::
349*1214b7e7SGunnar Mills                                                                 number_integer_t*>();
350b4b9595aSJames Feist                                                     if (procCountPtr != nullptr)
351b4b9595aSJames Feist                                                     {
352b4b9595aSJames Feist                                                         // shouldn't be possible
353b4b9595aSJames Feist                                                         // to be nullptr
354b4b9595aSJames Feist                                                         *procCountPtr += 1;
355b4b9595aSJames Feist                                                     }
35657e8c9beSAlpana Kumari                                                     procSummary["Status"]
35757e8c9beSAlpana Kumari                                                                ["State"] =
358c5b2abe0SLewanczyk, Dawid                                                                    "Enabled";
35957e8c9beSAlpana Kumari                                                     procSummary["Model"] =
36057e8c9beSAlpana Kumari                                                         *value;
361c5b2abe0SLewanczyk, Dawid                                                 }
362c5b2abe0SLewanczyk, Dawid                                             }
363c5b2abe0SLewanczyk, Dawid                                         }
36457e8c9beSAlpana Kumari                                     }
36557e8c9beSAlpana Kumari                                     else
36657e8c9beSAlpana Kumari                                     {
36757e8c9beSAlpana Kumari                                         auto getCpuPresenceState =
36857e8c9beSAlpana Kumari                                             [aResp](
36957e8c9beSAlpana Kumari                                                 const boost::system::error_code
37057e8c9beSAlpana Kumari                                                     ec,
371*1214b7e7SGunnar Mills                                                 const std::variant<bool>&
372*1214b7e7SGunnar Mills                                                     cpuPresenceCheck) {
37357e8c9beSAlpana Kumari                                                 if (ec)
37457e8c9beSAlpana Kumari                                                 {
37557e8c9beSAlpana Kumari                                                     BMCWEB_LOG_ERROR
37657e8c9beSAlpana Kumari                                                         << "DBUS response "
37757e8c9beSAlpana Kumari                                                            "error "
37857e8c9beSAlpana Kumari                                                         << ec;
37957e8c9beSAlpana Kumari                                                     return;
38057e8c9beSAlpana Kumari                                                 }
38157e8c9beSAlpana Kumari                                                 modifyCpuPresenceState(
38257e8c9beSAlpana Kumari                                                     aResp, cpuPresenceCheck);
38357e8c9beSAlpana Kumari                                             };
38457e8c9beSAlpana Kumari 
38557e8c9beSAlpana Kumari                                         auto getCpuFunctionalState =
38657e8c9beSAlpana Kumari                                             [aResp](
38757e8c9beSAlpana Kumari                                                 const boost::system::error_code
38857e8c9beSAlpana Kumari                                                     ec,
389*1214b7e7SGunnar Mills                                                 const std::variant<bool>&
390*1214b7e7SGunnar Mills                                                     cpuFunctionalCheck) {
39157e8c9beSAlpana Kumari                                                 if (ec)
39257e8c9beSAlpana Kumari                                                 {
39357e8c9beSAlpana Kumari                                                     BMCWEB_LOG_ERROR
39457e8c9beSAlpana Kumari                                                         << "DBUS response "
39557e8c9beSAlpana Kumari                                                            "error "
39657e8c9beSAlpana Kumari                                                         << ec;
39757e8c9beSAlpana Kumari                                                     return;
39857e8c9beSAlpana Kumari                                                 }
39957e8c9beSAlpana Kumari                                                 modifyCpuFunctionalState(
40057e8c9beSAlpana Kumari                                                     aResp, cpuFunctionalCheck);
40157e8c9beSAlpana Kumari                                             };
40257e8c9beSAlpana Kumari                                         // Get the Presence of CPU
40357e8c9beSAlpana Kumari                                         crow::connections::systemBus
40457e8c9beSAlpana Kumari                                             ->async_method_call(
40557e8c9beSAlpana Kumari                                                 std::move(getCpuPresenceState),
40657e8c9beSAlpana Kumari                                                 service, path,
40757e8c9beSAlpana Kumari                                                 "org.freedesktop.DBus."
40857e8c9beSAlpana Kumari                                                 "Properties",
40957e8c9beSAlpana Kumari                                                 "Get",
41057e8c9beSAlpana Kumari                                                 "xyz.openbmc_project.Inventory."
41157e8c9beSAlpana Kumari                                                 "Item",
41257e8c9beSAlpana Kumari                                                 "Present");
41357e8c9beSAlpana Kumari 
41457e8c9beSAlpana Kumari                                         // Get the Functional State
41557e8c9beSAlpana Kumari                                         crow::connections::systemBus
41657e8c9beSAlpana Kumari                                             ->async_method_call(
41757e8c9beSAlpana Kumari                                                 std::move(
41857e8c9beSAlpana Kumari                                                     getCpuFunctionalState),
41957e8c9beSAlpana Kumari                                                 service, path,
42057e8c9beSAlpana Kumari                                                 "org.freedesktop.DBus."
42157e8c9beSAlpana Kumari                                                 "Properties",
42257e8c9beSAlpana Kumari                                                 "Get",
42357e8c9beSAlpana Kumari                                                 "xyz.openbmc_project.State."
42457e8c9beSAlpana Kumari                                                 "Decorator."
42557e8c9beSAlpana Kumari                                                 "OperationalStatus",
42657e8c9beSAlpana Kumari                                                 "Functional");
42757e8c9beSAlpana Kumari 
42857e8c9beSAlpana Kumari                                         // Get the MODEL from
42957e8c9beSAlpana Kumari                                         // xyz.openbmc_project.Inventory.Decorator.Asset
43057e8c9beSAlpana Kumari                                         // support it later as Model  is Empty
43157e8c9beSAlpana Kumari                                         // currently.
43257e8c9beSAlpana Kumari                                     }
433c5b2abe0SLewanczyk, Dawid                                 },
43404a258f4SEd Tanous                                 connection.first, path,
4356c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
4366c34de48SEd Tanous                                 "xyz.openbmc_project.Inventory.Item.Cpu");
4375bc2dc8eSJames Feist 
4385bc2dc8eSJames Feist                             cpuHealth->inventory.emplace_back(path);
4391abe55efSEd Tanous                         }
44004a258f4SEd Tanous                         else if (interfaceName ==
44104a258f4SEd Tanous                                  "xyz.openbmc_project.Common.UUID")
4421abe55efSEd Tanous                         {
4431abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
44404a258f4SEd Tanous                                 << "Found UUID, now get its properties.";
44555c7b7a2SEd Tanous                             crow::connections::systemBus->async_method_call(
446*1214b7e7SGunnar Mills                                 [aResp](
447*1214b7e7SGunnar Mills                                     const boost::system::error_code ec,
4486c34de48SEd Tanous                                     const std::vector<
449*1214b7e7SGunnar Mills                                         std::pair<std::string, VariantType>>&
450*1214b7e7SGunnar Mills                                         properties) {
4511abe55efSEd Tanous                                     if (ec)
4521abe55efSEd Tanous                                     {
4531abe55efSEd Tanous                                         BMCWEB_LOG_DEBUG
4546c34de48SEd Tanous                                             << "DBUS response error " << ec;
455f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
456c5b2abe0SLewanczyk, Dawid                                         return;
457c5b2abe0SLewanczyk, Dawid                                     }
4586c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
4596c34de48SEd Tanous                                                      << properties.size()
460c5b2abe0SLewanczyk, Dawid                                                      << " UUID properties.";
4611abe55efSEd Tanous                                     for (const std::pair<std::string,
462*1214b7e7SGunnar Mills                                                          VariantType>&
463*1214b7e7SGunnar Mills                                              property : properties)
4641abe55efSEd Tanous                                     {
46504a258f4SEd Tanous                                         if (property.first == "UUID")
4661abe55efSEd Tanous                                         {
467c5b2abe0SLewanczyk, Dawid                                             const std::string* value =
4688d78b7a9SPatrick Williams                                                 std::get_if<std::string>(
4691b6b96c5SEd Tanous                                                     &property.second);
47004a258f4SEd Tanous 
4711abe55efSEd Tanous                                             if (value != nullptr)
4721abe55efSEd Tanous                                             {
473029573d4SEd Tanous                                                 std::string valueStr = *value;
47404a258f4SEd Tanous                                                 if (valueStr.size() == 32)
4751abe55efSEd Tanous                                                 {
476029573d4SEd Tanous                                                     valueStr.insert(8, 1, '-');
477029573d4SEd Tanous                                                     valueStr.insert(13, 1, '-');
478029573d4SEd Tanous                                                     valueStr.insert(18, 1, '-');
479029573d4SEd Tanous                                                     valueStr.insert(23, 1, '-');
48004a258f4SEd Tanous                                                 }
481029573d4SEd Tanous                                                 BMCWEB_LOG_DEBUG << "UUID = "
48204a258f4SEd Tanous                                                                  << valueStr;
483029573d4SEd Tanous                                                 aResp->res.jsonValue["UUID"] =
48404a258f4SEd Tanous                                                     valueStr;
485c5b2abe0SLewanczyk, Dawid                                             }
486c5b2abe0SLewanczyk, Dawid                                         }
487c5b2abe0SLewanczyk, Dawid                                     }
488c5b2abe0SLewanczyk, Dawid                                 },
48904a258f4SEd Tanous                                 connection.first, path,
4906c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
4911abe55efSEd Tanous                                 "xyz.openbmc_project.Common.UUID");
492c5b2abe0SLewanczyk, Dawid                         }
493029573d4SEd Tanous                         else if (interfaceName ==
494029573d4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.System")
4951abe55efSEd Tanous                         {
496029573d4SEd Tanous                             crow::connections::systemBus->async_method_call(
497*1214b7e7SGunnar Mills                                 [aResp](
498*1214b7e7SGunnar Mills                                     const boost::system::error_code ec,
499029573d4SEd Tanous                                     const std::vector<
500*1214b7e7SGunnar Mills                                         std::pair<std::string, VariantType>>&
501*1214b7e7SGunnar Mills                                         propertiesList) {
502029573d4SEd Tanous                                     if (ec)
503029573d4SEd Tanous                                     {
504e4a4b9a9SJames Feist                                         // doesn't have to include this
505e4a4b9a9SJames Feist                                         // interface
506029573d4SEd Tanous                                         return;
507029573d4SEd Tanous                                     }
508698654b6SGunnar Mills                                     BMCWEB_LOG_DEBUG
509698654b6SGunnar Mills                                         << "Got " << propertiesList.size()
510029573d4SEd Tanous                                         << " properties for system";
511029573d4SEd Tanous                                     for (const std::pair<std::string,
512*1214b7e7SGunnar Mills                                                          VariantType>&
513*1214b7e7SGunnar Mills                                              property : propertiesList)
514029573d4SEd Tanous                                     {
515fc5afcf9Sbeccabroek                                         const std::string& propertyName =
516fc5afcf9Sbeccabroek                                             property.first;
517fc5afcf9Sbeccabroek                                         if ((propertyName == "PartNumber") ||
518fc5afcf9Sbeccabroek                                             (propertyName == "SerialNumber") ||
519fc5afcf9Sbeccabroek                                             (propertyName == "Manufacturer") ||
520fc5afcf9Sbeccabroek                                             (propertyName == "Model"))
521fc5afcf9Sbeccabroek                                         {
522029573d4SEd Tanous                                             const std::string* value =
523fc5afcf9Sbeccabroek                                                 std::get_if<std::string>(
524029573d4SEd Tanous                                                     &property.second);
525029573d4SEd Tanous                                             if (value != nullptr)
526029573d4SEd Tanous                                             {
527029573d4SEd Tanous                                                 aResp->res
528fc5afcf9Sbeccabroek                                                     .jsonValue[propertyName] =
529029573d4SEd Tanous                                                     *value;
530029573d4SEd Tanous                                             }
531029573d4SEd Tanous                                         }
532fc5afcf9Sbeccabroek                                     }
533c1e236a6SGunnar Mills 
534cb7e1e7bSAndrew Geissler                                     // Grab the bios version
535cb7e1e7bSAndrew Geissler                                     fw_util::getActiveFwVersion(
536cb7e1e7bSAndrew Geissler                                         aResp, fw_util::biosPurpose,
537cb7e1e7bSAndrew Geissler                                         "BiosVersion");
538029573d4SEd Tanous                                 },
539029573d4SEd Tanous                                 connection.first, path,
540029573d4SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
541029573d4SEd Tanous                                 "xyz.openbmc_project.Inventory.Decorator."
542029573d4SEd Tanous                                 "Asset");
543e4a4b9a9SJames Feist 
544e4a4b9a9SJames Feist                             crow::connections::systemBus->async_method_call(
545e4a4b9a9SJames Feist                                 [aResp](
546e4a4b9a9SJames Feist                                     const boost::system::error_code ec,
547e4a4b9a9SJames Feist                                     const std::variant<std::string>& property) {
548e4a4b9a9SJames Feist                                     if (ec)
549e4a4b9a9SJames Feist                                     {
550e4a4b9a9SJames Feist                                         // doesn't have to include this
551e4a4b9a9SJames Feist                                         // interface
552e4a4b9a9SJames Feist                                         return;
553e4a4b9a9SJames Feist                                     }
554e4a4b9a9SJames Feist 
555e4a4b9a9SJames Feist                                     const std::string* value =
556e4a4b9a9SJames Feist                                         std::get_if<std::string>(&property);
557e4a4b9a9SJames Feist                                     if (value != nullptr)
558e4a4b9a9SJames Feist                                     {
559e4a4b9a9SJames Feist                                         aResp->res.jsonValue["AssetTag"] =
560e4a4b9a9SJames Feist                                             *value;
561e4a4b9a9SJames Feist                                     }
562e4a4b9a9SJames Feist                                 },
563e4a4b9a9SJames Feist                                 connection.first, path,
564e4a4b9a9SJames Feist                                 "org.freedesktop.DBus.Properties", "Get",
565e4a4b9a9SJames Feist                                 "xyz.openbmc_project.Inventory.Decorator."
566e4a4b9a9SJames Feist                                 "AssetTag",
567e4a4b9a9SJames Feist                                 "AssetTag");
568029573d4SEd Tanous                         }
569029573d4SEd Tanous                     }
570029573d4SEd Tanous                 }
571c5b2abe0SLewanczyk, Dawid             }
572c5b2abe0SLewanczyk, Dawid         },
573c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper",
574c5b2abe0SLewanczyk, Dawid         "/xyz/openbmc_project/object_mapper",
575c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
5766617338dSEd Tanous         "/xyz/openbmc_project/inventory", int32_t(0),
5776617338dSEd Tanous         std::array<const char*, 5>{
5786617338dSEd Tanous             "xyz.openbmc_project.Inventory.Decorator.Asset",
5796617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Cpu",
5806617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Dimm",
5816617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.System",
5826617338dSEd Tanous             "xyz.openbmc_project.Common.UUID",
5836617338dSEd Tanous         });
584c5b2abe0SLewanczyk, Dawid }
585c5b2abe0SLewanczyk, Dawid 
586c5b2abe0SLewanczyk, Dawid /**
587c5b2abe0SLewanczyk, Dawid  * @brief Retrieves host state properties over dbus
588c5b2abe0SLewanczyk, Dawid  *
589c5b2abe0SLewanczyk, Dawid  * @param[in] aResp     Shared pointer for completing asynchronous calls.
590c5b2abe0SLewanczyk, Dawid  *
591c5b2abe0SLewanczyk, Dawid  * @return None.
592c5b2abe0SLewanczyk, Dawid  */
593a0803efaSEd Tanous void getHostState(std::shared_ptr<AsyncResp> aResp)
5941abe55efSEd Tanous {
59555c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get host information.";
59655c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
597c5d03ff4SJennifer Lee         [aResp](const boost::system::error_code ec,
598abf2add6SEd Tanous                 const std::variant<std::string>& hostState) {
5991abe55efSEd Tanous             if (ec)
6001abe55efSEd Tanous             {
60155c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
602f12894f8SJason M. Bills                 messages::internalError(aResp->res);
603c5b2abe0SLewanczyk, Dawid                 return;
604c5b2abe0SLewanczyk, Dawid             }
6056617338dSEd Tanous 
606abf2add6SEd Tanous             const std::string* s = std::get_if<std::string>(&hostState);
60755c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Host state: " << *s;
6086617338dSEd Tanous             if (s != nullptr)
6091abe55efSEd Tanous             {
610c5b2abe0SLewanczyk, Dawid                 // Verify Host State
61194732661SAndrew Geissler                 if (*s == "xyz.openbmc_project.State.Host.HostState.Running")
6121abe55efSEd Tanous                 {
61355c7b7a2SEd Tanous                     aResp->res.jsonValue["PowerState"] = "On";
6146617338dSEd Tanous                     aResp->res.jsonValue["Status"]["State"] = "Enabled";
6151abe55efSEd Tanous                 }
61683935af9SAndrew Geissler                 else if (*s == "xyz.openbmc_project.State.Host.HostState."
6178c888608SGunnar Mills                                "Quiesced")
6188c888608SGunnar Mills                 {
6198c888608SGunnar Mills                     aResp->res.jsonValue["PowerState"] = "On";
6208c888608SGunnar Mills                     aResp->res.jsonValue["Status"]["State"] = "Quiesced";
6218c888608SGunnar Mills                 }
6228c888608SGunnar Mills                 else if (*s == "xyz.openbmc_project.State.Host.HostState."
62383935af9SAndrew Geissler                                "DiagnosticMode")
62483935af9SAndrew Geissler                 {
62583935af9SAndrew Geissler                     aResp->res.jsonValue["PowerState"] = "On";
62683935af9SAndrew Geissler                     aResp->res.jsonValue["Status"]["State"] = "InTest";
62783935af9SAndrew Geissler                 }
6281abe55efSEd Tanous                 else
6291abe55efSEd Tanous                 {
63055c7b7a2SEd Tanous                     aResp->res.jsonValue["PowerState"] = "Off";
6316617338dSEd Tanous                     aResp->res.jsonValue["Status"]["State"] = "Disabled";
632c5b2abe0SLewanczyk, Dawid                 }
633c5b2abe0SLewanczyk, Dawid             }
634c5b2abe0SLewanczyk, Dawid         },
6356c34de48SEd Tanous         "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
6366617338dSEd Tanous         "org.freedesktop.DBus.Properties", "Get",
6376617338dSEd Tanous         "xyz.openbmc_project.State.Host", "CurrentHostState");
638c5b2abe0SLewanczyk, Dawid }
639c5b2abe0SLewanczyk, Dawid 
640c5b2abe0SLewanczyk, Dawid /**
641491d8ee7SSantosh Puranik  * @brief Traslates boot source DBUS property value to redfish.
642491d8ee7SSantosh Puranik  *
643491d8ee7SSantosh Puranik  * @param[in] dbusSource    The boot source in DBUS speak.
644491d8ee7SSantosh Puranik  *
645491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source in Redfish terms. If translation
646491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
647491d8ee7SSantosh Puranik  */
648491d8ee7SSantosh Puranik static std::string dbusToRfBootSource(const std::string& dbusSource)
649491d8ee7SSantosh Puranik {
650491d8ee7SSantosh Puranik     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
651491d8ee7SSantosh Puranik     {
652491d8ee7SSantosh Puranik         return "None";
653491d8ee7SSantosh Puranik     }
654491d8ee7SSantosh Puranik     else if (dbusSource ==
655491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.Disk")
656491d8ee7SSantosh Puranik     {
657491d8ee7SSantosh Puranik         return "Hdd";
658491d8ee7SSantosh Puranik     }
659491d8ee7SSantosh Puranik     else if (dbusSource ==
660a71dc0b7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia")
661491d8ee7SSantosh Puranik     {
662491d8ee7SSantosh Puranik         return "Cd";
663491d8ee7SSantosh Puranik     }
664491d8ee7SSantosh Puranik     else if (dbusSource ==
665491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.Network")
666491d8ee7SSantosh Puranik     {
667491d8ee7SSantosh Puranik         return "Pxe";
668491d8ee7SSantosh Puranik     }
6699f16b2c1SJennifer Lee     else if (dbusSource ==
670944ffaf9SJohnathan Mantey              "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia")
6719f16b2c1SJennifer Lee     {
6729f16b2c1SJennifer Lee         return "Usb";
6739f16b2c1SJennifer Lee     }
674491d8ee7SSantosh Puranik     else
675491d8ee7SSantosh Puranik     {
676491d8ee7SSantosh Puranik         return "";
677491d8ee7SSantosh Puranik     }
678491d8ee7SSantosh Puranik }
679491d8ee7SSantosh Puranik 
680491d8ee7SSantosh Puranik /**
681491d8ee7SSantosh Puranik  * @brief Traslates boot mode DBUS property value to redfish.
682491d8ee7SSantosh Puranik  *
683491d8ee7SSantosh Puranik  * @param[in] dbusMode    The boot mode in DBUS speak.
684491d8ee7SSantosh Puranik  *
685491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode in Redfish terms. If translation
686491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
687491d8ee7SSantosh Puranik  */
688491d8ee7SSantosh Puranik static std::string dbusToRfBootMode(const std::string& dbusMode)
689491d8ee7SSantosh Puranik {
690491d8ee7SSantosh Puranik     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
691491d8ee7SSantosh Puranik     {
692491d8ee7SSantosh Puranik         return "None";
693491d8ee7SSantosh Puranik     }
694491d8ee7SSantosh Puranik     else if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe")
695491d8ee7SSantosh Puranik     {
696491d8ee7SSantosh Puranik         return "Diags";
697491d8ee7SSantosh Puranik     }
698491d8ee7SSantosh Puranik     else if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup")
699491d8ee7SSantosh Puranik     {
700491d8ee7SSantosh Puranik         return "BiosSetup";
701491d8ee7SSantosh Puranik     }
702491d8ee7SSantosh Puranik     else
703491d8ee7SSantosh Puranik     {
704491d8ee7SSantosh Puranik         return "";
705491d8ee7SSantosh Puranik     }
706491d8ee7SSantosh Puranik }
707491d8ee7SSantosh Puranik 
708491d8ee7SSantosh Puranik /**
709944ffaf9SJohnathan Mantey  * @brief Traslates boot source from Redfish to the DBus boot paths.
710491d8ee7SSantosh Puranik  *
711491d8ee7SSantosh Puranik  * @param[in] rfSource    The boot source in Redfish.
712944ffaf9SJohnathan Mantey  * @param[out] bootSource The DBus source
713944ffaf9SJohnathan Mantey  * @param[out] bootMode   the DBus boot mode
714491d8ee7SSantosh Puranik  *
715944ffaf9SJohnathan Mantey  * @return Integer error code.
716491d8ee7SSantosh Puranik  */
717944ffaf9SJohnathan Mantey static int assignBootParameters(std::shared_ptr<AsyncResp> aResp,
718944ffaf9SJohnathan Mantey                                 const std::string& rfSource,
719944ffaf9SJohnathan Mantey                                 std::string& bootSource, std::string& bootMode)
720491d8ee7SSantosh Puranik {
721944ffaf9SJohnathan Mantey     // The caller has initialized the bootSource and bootMode to:
722944ffaf9SJohnathan Mantey     // bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
723944ffaf9SJohnathan Mantey     // bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
724944ffaf9SJohnathan Mantey     // Only modify the bootSource/bootMode variable needed to achieve the
725944ffaf9SJohnathan Mantey     // desired boot action.
726944ffaf9SJohnathan Mantey 
727491d8ee7SSantosh Puranik     if (rfSource == "None")
728491d8ee7SSantosh Puranik     {
729944ffaf9SJohnathan Mantey         return 0;
730491d8ee7SSantosh Puranik     }
731491d8ee7SSantosh Puranik     else if (rfSource == "Pxe")
732491d8ee7SSantosh Puranik     {
733944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Network";
734944ffaf9SJohnathan Mantey     }
735944ffaf9SJohnathan Mantey     else if (rfSource == "Hdd")
736944ffaf9SJohnathan Mantey     {
737944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Disk";
738944ffaf9SJohnathan Mantey     }
739944ffaf9SJohnathan Mantey     else if (rfSource == "Diags")
740944ffaf9SJohnathan Mantey     {
741944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe";
742944ffaf9SJohnathan Mantey     }
743944ffaf9SJohnathan Mantey     else if (rfSource == "Cd")
744944ffaf9SJohnathan Mantey     {
745944ffaf9SJohnathan Mantey         bootSource =
746944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia";
747944ffaf9SJohnathan Mantey     }
748944ffaf9SJohnathan Mantey     else if (rfSource == "BiosSetup")
749944ffaf9SJohnathan Mantey     {
750944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup";
751491d8ee7SSantosh Puranik     }
7529f16b2c1SJennifer Lee     else if (rfSource == "Usb")
7539f16b2c1SJennifer Lee     {
754944ffaf9SJohnathan Mantey         bootSource =
755944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia";
7569f16b2c1SJennifer Lee     }
757491d8ee7SSantosh Puranik     else
758491d8ee7SSantosh Puranik     {
759944ffaf9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "Invalid property value for "
760944ffaf9SJohnathan Mantey                             "BootSourceOverrideTarget: "
761944ffaf9SJohnathan Mantey                          << bootSource;
762944ffaf9SJohnathan Mantey         messages::propertyValueNotInList(aResp->res, rfSource,
763944ffaf9SJohnathan Mantey                                          "BootSourceTargetOverride");
764944ffaf9SJohnathan Mantey         return -1;
765491d8ee7SSantosh Puranik     }
766944ffaf9SJohnathan Mantey     return 0;
767491d8ee7SSantosh Puranik }
768491d8ee7SSantosh Puranik 
769491d8ee7SSantosh Puranik /**
770491d8ee7SSantosh Puranik  * @brief Retrieves boot mode over DBUS and fills out the response
771491d8ee7SSantosh Puranik  *
772491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
773491d8ee7SSantosh Puranik  * @param[in] bootDbusObj   The dbus object to query for boot properties.
774491d8ee7SSantosh Puranik  *
775491d8ee7SSantosh Puranik  * @return None.
776491d8ee7SSantosh Puranik  */
777491d8ee7SSantosh Puranik static void getBootMode(std::shared_ptr<AsyncResp> aResp,
778491d8ee7SSantosh Puranik                         std::string bootDbusObj)
779491d8ee7SSantosh Puranik {
780491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
781491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec,
782491d8ee7SSantosh Puranik                 const std::variant<std::string>& bootMode) {
783491d8ee7SSantosh Puranik             if (ec)
784491d8ee7SSantosh Puranik             {
785491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
786491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
787491d8ee7SSantosh Puranik                 return;
788491d8ee7SSantosh Puranik             }
789491d8ee7SSantosh Puranik 
790491d8ee7SSantosh Puranik             const std::string* bootModeStr =
791491d8ee7SSantosh Puranik                 std::get_if<std::string>(&bootMode);
792491d8ee7SSantosh Puranik 
793491d8ee7SSantosh Puranik             if (!bootModeStr)
794491d8ee7SSantosh Puranik             {
795491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
796491d8ee7SSantosh Puranik                 return;
797491d8ee7SSantosh Puranik             }
798491d8ee7SSantosh Puranik 
799491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot mode: " << *bootModeStr;
800491d8ee7SSantosh Puranik 
801491d8ee7SSantosh Puranik             // TODO (Santosh): Do we need to support override mode?
802491d8ee7SSantosh Puranik             aResp->res.jsonValue["Boot"]["BootSourceOverrideMode"] = "Legacy";
803491d8ee7SSantosh Puranik             aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget@Redfish."
804491d8ee7SSantosh Puranik                                          "AllowableValues"] = {
805944ffaf9SJohnathan Mantey                 "None", "Pxe", "Hdd", "Cd", "Diags", "BiosSetup", "Usb"};
806491d8ee7SSantosh Puranik 
807491d8ee7SSantosh Puranik             if (*bootModeStr !=
808491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
809491d8ee7SSantosh Puranik             {
810491d8ee7SSantosh Puranik                 auto rfMode = dbusToRfBootMode(*bootModeStr);
811491d8ee7SSantosh Puranik                 if (!rfMode.empty())
812491d8ee7SSantosh Puranik                 {
813491d8ee7SSantosh Puranik                     aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
814491d8ee7SSantosh Puranik                         rfMode;
815491d8ee7SSantosh Puranik                 }
816491d8ee7SSantosh Puranik             }
817491d8ee7SSantosh Puranik 
818491d8ee7SSantosh Puranik             // If the BootSourceOverrideTarget is still "None" at the end,
819491d8ee7SSantosh Puranik             // reset the BootSourceOverrideEnabled to indicate that
820491d8ee7SSantosh Puranik             // overrides are disabled
821491d8ee7SSantosh Puranik             if (aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] ==
822491d8ee7SSantosh Puranik                 "None")
823491d8ee7SSantosh Puranik             {
824491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
825491d8ee7SSantosh Puranik                     "Disabled";
826491d8ee7SSantosh Puranik             }
827491d8ee7SSantosh Puranik         },
828491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootDbusObj,
829491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
830491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode");
831491d8ee7SSantosh Puranik }
832491d8ee7SSantosh Puranik 
833491d8ee7SSantosh Puranik /**
834491d8ee7SSantosh Puranik  * @brief Retrieves boot source over DBUS
835491d8ee7SSantosh Puranik  *
836491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
837491d8ee7SSantosh Puranik  * @param[in] oneTimeEnable Boolean to indicate boot properties are one-time.
838491d8ee7SSantosh Puranik  *
839491d8ee7SSantosh Puranik  * @return None.
840491d8ee7SSantosh Puranik  */
841491d8ee7SSantosh Puranik static void getBootSource(std::shared_ptr<AsyncResp> aResp, bool oneTimeEnabled)
842491d8ee7SSantosh Puranik {
843491d8ee7SSantosh Puranik     std::string bootDbusObj =
844491d8ee7SSantosh Puranik         oneTimeEnabled ? "/xyz/openbmc_project/control/host0/boot/one_time"
845491d8ee7SSantosh Puranik                        : "/xyz/openbmc_project/control/host0/boot";
846491d8ee7SSantosh Puranik 
847491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Is one time: " << oneTimeEnabled;
848491d8ee7SSantosh Puranik     aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
849491d8ee7SSantosh Puranik         (oneTimeEnabled) ? "Once" : "Continuous";
850491d8ee7SSantosh Puranik 
851491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
852491d8ee7SSantosh Puranik         [aResp, bootDbusObj](const boost::system::error_code ec,
853491d8ee7SSantosh Puranik                              const std::variant<std::string>& bootSource) {
854491d8ee7SSantosh Puranik             if (ec)
855491d8ee7SSantosh Puranik             {
856491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
857491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
858491d8ee7SSantosh Puranik                 return;
859491d8ee7SSantosh Puranik             }
860491d8ee7SSantosh Puranik 
861491d8ee7SSantosh Puranik             const std::string* bootSourceStr =
862491d8ee7SSantosh Puranik                 std::get_if<std::string>(&bootSource);
863491d8ee7SSantosh Puranik 
864491d8ee7SSantosh Puranik             if (!bootSourceStr)
865491d8ee7SSantosh Puranik             {
866491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
867491d8ee7SSantosh Puranik                 return;
868491d8ee7SSantosh Puranik             }
869491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot source: " << *bootSourceStr;
870491d8ee7SSantosh Puranik 
871491d8ee7SSantosh Puranik             auto rfSource = dbusToRfBootSource(*bootSourceStr);
872491d8ee7SSantosh Puranik             if (!rfSource.empty())
873491d8ee7SSantosh Puranik             {
874491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
875491d8ee7SSantosh Puranik                     rfSource;
876491d8ee7SSantosh Puranik             }
877491d8ee7SSantosh Puranik         },
878491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootDbusObj,
879491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
880491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource");
881491d8ee7SSantosh Puranik     getBootMode(std::move(aResp), std::move(bootDbusObj));
882491d8ee7SSantosh Puranik }
883491d8ee7SSantosh Puranik 
884491d8ee7SSantosh Puranik /**
885491d8ee7SSantosh Puranik  * @brief Retrieves "One time" enabled setting over DBUS and calls function to
886491d8ee7SSantosh Puranik  * get boot source and boot mode.
887491d8ee7SSantosh Puranik  *
888491d8ee7SSantosh Puranik  * @param[in] aResp     Shared pointer for generating response message.
889491d8ee7SSantosh Puranik  *
890491d8ee7SSantosh Puranik  * @return None.
891491d8ee7SSantosh Puranik  */
892491d8ee7SSantosh Puranik static void getBootProperties(std::shared_ptr<AsyncResp> aResp)
893491d8ee7SSantosh Puranik {
894491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Get boot information.";
895491d8ee7SSantosh Puranik 
896491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
897c5d03ff4SJennifer Lee         [aResp](const boost::system::error_code ec,
89819bd78d9SPatrick Williams                 const std::variant<bool>& oneTime) {
899491d8ee7SSantosh Puranik             if (ec)
900491d8ee7SSantosh Puranik             {
901491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
9022a833c77SJames Feist                 // not an error, don't have to have the interface
903491d8ee7SSantosh Puranik                 return;
904491d8ee7SSantosh Puranik             }
905491d8ee7SSantosh Puranik 
906491d8ee7SSantosh Puranik             const bool* oneTimePtr = std::get_if<bool>(&oneTime);
907491d8ee7SSantosh Puranik 
908491d8ee7SSantosh Puranik             if (!oneTimePtr)
909491d8ee7SSantosh Puranik             {
910491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
911491d8ee7SSantosh Puranik                 return;
912491d8ee7SSantosh Puranik             }
913491d8ee7SSantosh Puranik             getBootSource(aResp, *oneTimePtr);
914491d8ee7SSantosh Puranik         },
915491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
916491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
917491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
918491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled");
919491d8ee7SSantosh Puranik }
920491d8ee7SSantosh Puranik 
921491d8ee7SSantosh Puranik /**
9226bd5a8d2SGunnar Mills  * @brief Retrieves Automatic Retry properties. Known on D-Bus as AutoReboot.
9236bd5a8d2SGunnar Mills  *
9246bd5a8d2SGunnar Mills  * @param[in] aResp     Shared pointer for generating response message.
9256bd5a8d2SGunnar Mills  *
9266bd5a8d2SGunnar Mills  * @return None.
9276bd5a8d2SGunnar Mills  */
9286bd5a8d2SGunnar Mills void getAutomaticRetry(std::shared_ptr<AsyncResp> aResp)
9296bd5a8d2SGunnar Mills {
9306bd5a8d2SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Automatic Retry policy";
9316bd5a8d2SGunnar Mills 
9326bd5a8d2SGunnar Mills     crow::connections::systemBus->async_method_call(
9336bd5a8d2SGunnar Mills         [aResp](const boost::system::error_code ec,
9346bd5a8d2SGunnar Mills                 std::variant<bool>& autoRebootEnabled) {
9356bd5a8d2SGunnar Mills             if (ec)
9366bd5a8d2SGunnar Mills             {
9376bd5a8d2SGunnar Mills                 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
9386bd5a8d2SGunnar Mills                 return;
9396bd5a8d2SGunnar Mills             }
9406bd5a8d2SGunnar Mills 
9416bd5a8d2SGunnar Mills             const bool* autoRebootEnabledPtr =
9426bd5a8d2SGunnar Mills                 std::get_if<bool>(&autoRebootEnabled);
9436bd5a8d2SGunnar Mills 
9446bd5a8d2SGunnar Mills             if (!autoRebootEnabledPtr)
9456bd5a8d2SGunnar Mills             {
9466bd5a8d2SGunnar Mills                 messages::internalError(aResp->res);
9476bd5a8d2SGunnar Mills                 return;
9486bd5a8d2SGunnar Mills             }
9496bd5a8d2SGunnar Mills 
9506bd5a8d2SGunnar Mills             BMCWEB_LOG_DEBUG << "Auto Reboot: " << *autoRebootEnabledPtr;
9516bd5a8d2SGunnar Mills             if (*autoRebootEnabledPtr == true)
9526bd5a8d2SGunnar Mills             {
9536bd5a8d2SGunnar Mills                 aResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
9546bd5a8d2SGunnar Mills                     "RetryAttempts";
9556bd5a8d2SGunnar Mills                 // If AutomaticRetry (AutoReboot) is enabled see how many
9566bd5a8d2SGunnar Mills                 // attempts are left
9576bd5a8d2SGunnar Mills                 crow::connections::systemBus->async_method_call(
9586bd5a8d2SGunnar Mills                     [aResp](const boost::system::error_code ec,
9596bd5a8d2SGunnar Mills                             std::variant<uint32_t>& autoRebootAttemptsLeft) {
9606bd5a8d2SGunnar Mills                         if (ec)
9616bd5a8d2SGunnar Mills                         {
9626bd5a8d2SGunnar Mills                             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
9636bd5a8d2SGunnar Mills                             return;
9646bd5a8d2SGunnar Mills                         }
9656bd5a8d2SGunnar Mills 
9666bd5a8d2SGunnar Mills                         const uint32_t* autoRebootAttemptsLeftPtr =
9676bd5a8d2SGunnar Mills                             std::get_if<uint32_t>(&autoRebootAttemptsLeft);
9686bd5a8d2SGunnar Mills 
9696bd5a8d2SGunnar Mills                         if (!autoRebootAttemptsLeftPtr)
9706bd5a8d2SGunnar Mills                         {
9716bd5a8d2SGunnar Mills                             messages::internalError(aResp->res);
9726bd5a8d2SGunnar Mills                             return;
9736bd5a8d2SGunnar Mills                         }
9746bd5a8d2SGunnar Mills 
9756bd5a8d2SGunnar Mills                         BMCWEB_LOG_DEBUG << "Auto Reboot Attempts Left: "
9766bd5a8d2SGunnar Mills                                          << *autoRebootAttemptsLeftPtr;
9776bd5a8d2SGunnar Mills 
9786bd5a8d2SGunnar Mills                         aResp->res
9796bd5a8d2SGunnar Mills                             .jsonValue["Boot"]
9806bd5a8d2SGunnar Mills                                       ["RemainingAutomaticRetryAttempts"] =
9816bd5a8d2SGunnar Mills                             *autoRebootAttemptsLeftPtr;
9826bd5a8d2SGunnar Mills                     },
9836bd5a8d2SGunnar Mills                     "xyz.openbmc_project.State.Host",
9846bd5a8d2SGunnar Mills                     "/xyz/openbmc_project/state/host0",
9856bd5a8d2SGunnar Mills                     "org.freedesktop.DBus.Properties", "Get",
9866bd5a8d2SGunnar Mills                     "xyz.openbmc_project.Control.Boot.RebootAttempts",
9876bd5a8d2SGunnar Mills                     "AttemptsLeft");
9886bd5a8d2SGunnar Mills             }
9896bd5a8d2SGunnar Mills             else
9906bd5a8d2SGunnar Mills             {
9916bd5a8d2SGunnar Mills                 aResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
9926bd5a8d2SGunnar Mills                     "Disabled";
9936bd5a8d2SGunnar Mills             }
9946bd5a8d2SGunnar Mills 
9956bd5a8d2SGunnar Mills             // Not on D-Bus. Hardcoded here:
9966bd5a8d2SGunnar Mills             // https://github.com/openbmc/phosphor-state-manager/blob/1dbbef42675e94fb1f78edb87d6b11380260535a/meson_options.txt#L71
9976bd5a8d2SGunnar Mills             aResp->res.jsonValue["Boot"]["AutomaticRetryAttempts"] = 3;
99869f35306SGunnar Mills 
99969f35306SGunnar Mills             // "AutomaticRetryConfig" can be 3 values, Disabled, RetryAlways,
100069f35306SGunnar Mills             // and RetryAttempts. OpenBMC only supports Disabled and
100169f35306SGunnar Mills             // RetryAttempts.
100269f35306SGunnar Mills             aResp->res.jsonValue["Boot"]["AutomaticRetryConfig@Redfish."
100369f35306SGunnar Mills                                          "AllowableValues"] = {"Disabled",
100469f35306SGunnar Mills                                                                "RetryAttempts"};
10056bd5a8d2SGunnar Mills         },
10066bd5a8d2SGunnar Mills         "xyz.openbmc_project.Settings",
10076bd5a8d2SGunnar Mills         "/xyz/openbmc_project/control/host0/auto_reboot",
10086bd5a8d2SGunnar Mills         "org.freedesktop.DBus.Properties", "Get",
10096bd5a8d2SGunnar Mills         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot");
10106bd5a8d2SGunnar Mills }
10116bd5a8d2SGunnar Mills 
10126bd5a8d2SGunnar Mills /**
1013c6a620f2SGeorge Liu  * @brief Retrieves power restore policy over DBUS.
1014c6a620f2SGeorge Liu  *
1015c6a620f2SGeorge Liu  * @param[in] aResp     Shared pointer for generating response message.
1016c6a620f2SGeorge Liu  *
1017c6a620f2SGeorge Liu  * @return None.
1018c6a620f2SGeorge Liu  */
1019c6a620f2SGeorge Liu void getPowerRestorePolicy(std::shared_ptr<AsyncResp> aResp)
1020c6a620f2SGeorge Liu {
1021c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Get power restore policy";
1022c6a620f2SGeorge Liu 
1023c6a620f2SGeorge Liu     crow::connections::systemBus->async_method_call(
1024c6a620f2SGeorge Liu         [aResp](const boost::system::error_code ec,
102519bd78d9SPatrick Williams                 std::variant<std::string>& policy) {
1026c6a620f2SGeorge Liu             if (ec)
1027c6a620f2SGeorge Liu             {
1028c6a620f2SGeorge Liu                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1029c6a620f2SGeorge Liu                 return;
1030c6a620f2SGeorge Liu             }
1031c6a620f2SGeorge Liu 
1032c6a620f2SGeorge Liu             const boost::container::flat_map<std::string, std::string>
1033c6a620f2SGeorge Liu                 policyMaps = {
1034c6a620f2SGeorge Liu                     {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1035c6a620f2SGeorge Liu                      "AlwaysOn",
1036c6a620f2SGeorge Liu                      "AlwaysOn"},
1037c6a620f2SGeorge Liu                     {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1038c6a620f2SGeorge Liu                      "AlwaysOff",
1039c6a620f2SGeorge Liu                      "AlwaysOff"},
1040c6a620f2SGeorge Liu                     {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1041c6a620f2SGeorge Liu                      "LastState",
1042c6a620f2SGeorge Liu                      "LastState"}};
1043c6a620f2SGeorge Liu 
1044c6a620f2SGeorge Liu             const std::string* policyPtr = std::get_if<std::string>(&policy);
1045c6a620f2SGeorge Liu 
1046c6a620f2SGeorge Liu             if (!policyPtr)
1047c6a620f2SGeorge Liu             {
1048c6a620f2SGeorge Liu                 messages::internalError(aResp->res);
1049c6a620f2SGeorge Liu                 return;
1050c6a620f2SGeorge Liu             }
1051c6a620f2SGeorge Liu 
1052c6a620f2SGeorge Liu             auto policyMapsIt = policyMaps.find(*policyPtr);
1053c6a620f2SGeorge Liu             if (policyMapsIt == policyMaps.end())
1054c6a620f2SGeorge Liu             {
1055c6a620f2SGeorge Liu                 messages::internalError(aResp->res);
1056c6a620f2SGeorge Liu                 return;
1057c6a620f2SGeorge Liu             }
1058c6a620f2SGeorge Liu 
1059c6a620f2SGeorge Liu             aResp->res.jsonValue["PowerRestorePolicy"] = policyMapsIt->second;
1060c6a620f2SGeorge Liu         },
1061c6a620f2SGeorge Liu         "xyz.openbmc_project.Settings",
1062c6a620f2SGeorge Liu         "/xyz/openbmc_project/control/host0/power_restore_policy",
1063c6a620f2SGeorge Liu         "org.freedesktop.DBus.Properties", "Get",
1064c6a620f2SGeorge Liu         "xyz.openbmc_project.Control.Power.RestorePolicy",
1065c6a620f2SGeorge Liu         "PowerRestorePolicy");
1066c6a620f2SGeorge Liu }
1067c6a620f2SGeorge Liu 
1068c6a620f2SGeorge Liu /**
1069491d8ee7SSantosh Puranik  * @brief Sets boot properties into DBUS object(s).
1070491d8ee7SSantosh Puranik  *
1071491d8ee7SSantosh Puranik  * @param[in] aResp           Shared pointer for generating response message.
1072491d8ee7SSantosh Puranik  * @param[in] oneTimeEnabled  Is "one-time" setting already enabled.
1073491d8ee7SSantosh Puranik  * @param[in] bootSource      The boot source to set.
1074491d8ee7SSantosh Puranik  * @param[in] bootEnable      The source override "enable" to set.
1075491d8ee7SSantosh Puranik  *
1076265c1602SJohnathan Mantey  * @return Integer error code.
1077491d8ee7SSantosh Puranik  */
1078491d8ee7SSantosh Puranik static void setBootModeOrSource(std::shared_ptr<AsyncResp> aResp,
1079491d8ee7SSantosh Puranik                                 bool oneTimeEnabled,
1080491d8ee7SSantosh Puranik                                 std::optional<std::string> bootSource,
1081491d8ee7SSantosh Puranik                                 std::optional<std::string> bootEnable)
1082491d8ee7SSantosh Puranik {
1083944ffaf9SJohnathan Mantey     std::string bootSourceStr =
1084944ffaf9SJohnathan Mantey         "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
1085944ffaf9SJohnathan Mantey     std::string bootModeStr =
1086944ffaf9SJohnathan Mantey         "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
1087491d8ee7SSantosh Puranik     bool oneTimeSetting = oneTimeEnabled;
1088944ffaf9SJohnathan Mantey     bool useBootSource = true;
1089944ffaf9SJohnathan Mantey 
1090491d8ee7SSantosh Puranik     // Validate incoming parameters
1091491d8ee7SSantosh Puranik     if (bootEnable)
1092491d8ee7SSantosh Puranik     {
1093491d8ee7SSantosh Puranik         if (*bootEnable == "Once")
1094491d8ee7SSantosh Puranik         {
1095491d8ee7SSantosh Puranik             oneTimeSetting = true;
1096491d8ee7SSantosh Puranik         }
1097491d8ee7SSantosh Puranik         else if (*bootEnable == "Continuous")
1098491d8ee7SSantosh Puranik         {
1099491d8ee7SSantosh Puranik             oneTimeSetting = false;
1100491d8ee7SSantosh Puranik         }
1101491d8ee7SSantosh Puranik         else if (*bootEnable == "Disabled")
1102491d8ee7SSantosh Puranik         {
1103944ffaf9SJohnathan Mantey             BMCWEB_LOG_DEBUG << "Boot source override will be disabled";
1104491d8ee7SSantosh Puranik             oneTimeSetting = false;
1105944ffaf9SJohnathan Mantey             useBootSource = false;
1106491d8ee7SSantosh Puranik         }
1107491d8ee7SSantosh Puranik         else
1108491d8ee7SSantosh Puranik         {
1109491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Unsupported value for "
1110491d8ee7SSantosh Puranik                                 "BootSourceOverrideEnabled: "
1111491d8ee7SSantosh Puranik                              << *bootEnable;
1112491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootEnable,
1113491d8ee7SSantosh Puranik                                              "BootSourceOverrideEnabled");
1114491d8ee7SSantosh Puranik             return;
1115491d8ee7SSantosh Puranik         }
1116491d8ee7SSantosh Puranik     }
1117491d8ee7SSantosh Puranik 
1118944ffaf9SJohnathan Mantey     if (bootSource && useBootSource)
1119491d8ee7SSantosh Puranik     {
1120491d8ee7SSantosh Puranik         // Source target specified
1121491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot source: " << *bootSource;
1122491d8ee7SSantosh Puranik         // Figure out which DBUS interface and property to use
1123944ffaf9SJohnathan Mantey         if (assignBootParameters(aResp, *bootSource, bootSourceStr,
1124944ffaf9SJohnathan Mantey                                  bootModeStr))
1125491d8ee7SSantosh Puranik         {
1126944ffaf9SJohnathan Mantey             BMCWEB_LOG_DEBUG
1127944ffaf9SJohnathan Mantey                 << "Invalid property value for BootSourceOverrideTarget: "
1128491d8ee7SSantosh Puranik                 << *bootSource;
1129491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootSource,
1130491d8ee7SSantosh Puranik                                              "BootSourceTargetOverride");
1131491d8ee7SSantosh Puranik             return;
1132491d8ee7SSantosh Puranik         }
1133944ffaf9SJohnathan Mantey     }
1134491d8ee7SSantosh Puranik 
1135944ffaf9SJohnathan Mantey     // Act on validated parameters
1136944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
1137944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
1138944ffaf9SJohnathan Mantey     const char* bootObj =
1139944ffaf9SJohnathan Mantey         oneTimeSetting ? "/xyz/openbmc_project/control/host0/boot/one_time"
1140944ffaf9SJohnathan Mantey                        : "/xyz/openbmc_project/control/host0/boot";
1141944ffaf9SJohnathan Mantey 
1142491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1143491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec) {
1144491d8ee7SSantosh Puranik             if (ec)
1145491d8ee7SSantosh Puranik             {
1146491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1147491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1148491d8ee7SSantosh Puranik                 return;
1149491d8ee7SSantosh Puranik             }
1150491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot source update done.";
1151491d8ee7SSantosh Puranik         },
1152491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootObj,
1153491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1154491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource",
1155491d8ee7SSantosh Puranik         std::variant<std::string>(bootSourceStr));
1156944ffaf9SJohnathan Mantey 
1157491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1158491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec) {
1159491d8ee7SSantosh Puranik             if (ec)
1160491d8ee7SSantosh Puranik             {
1161491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1162491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1163491d8ee7SSantosh Puranik                 return;
1164491d8ee7SSantosh Puranik             }
1165491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot mode update done.";
1166491d8ee7SSantosh Puranik         },
1167491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootObj,
1168491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1169491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
1170491d8ee7SSantosh Puranik         std::variant<std::string>(bootModeStr));
1171944ffaf9SJohnathan Mantey 
1172491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1173491d8ee7SSantosh Puranik         [aResp{std::move(aResp)}](const boost::system::error_code ec) {
1174491d8ee7SSantosh Puranik             if (ec)
1175491d8ee7SSantosh Puranik             {
1176491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1177491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1178491d8ee7SSantosh Puranik                 return;
1179491d8ee7SSantosh Puranik             }
1180491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot enable update done.";
1181491d8ee7SSantosh Puranik         },
1182491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
1183491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
1184491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1185491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled",
1186491d8ee7SSantosh Puranik         std::variant<bool>(oneTimeSetting));
1187491d8ee7SSantosh Puranik }
1188491d8ee7SSantosh Puranik 
1189491d8ee7SSantosh Puranik /**
1190491d8ee7SSantosh Puranik  * @brief Retrieves "One time" enabled setting over DBUS and calls function to
1191491d8ee7SSantosh Puranik  * set boot source/boot mode properties.
1192491d8ee7SSantosh Puranik  *
1193491d8ee7SSantosh Puranik  * @param[in] aResp      Shared pointer for generating response message.
1194491d8ee7SSantosh Puranik  * @param[in] bootSource The boot source from incoming RF request.
1195491d8ee7SSantosh Puranik  * @param[in] bootEnable The boot override enable from incoming RF request.
1196491d8ee7SSantosh Puranik  *
1197265c1602SJohnathan Mantey  * @return Integer error code.
1198491d8ee7SSantosh Puranik  */
119969f35306SGunnar Mills static void setBootSourceProperties(std::shared_ptr<AsyncResp> aResp,
1200491d8ee7SSantosh Puranik                                     std::optional<std::string> bootSource,
1201491d8ee7SSantosh Puranik                                     std::optional<std::string> bootEnable)
1202491d8ee7SSantosh Puranik {
1203491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set boot information.";
1204491d8ee7SSantosh Puranik 
1205491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1206265c1602SJohnathan Mantey         [aResp, bootSource{std::move(bootSource)},
120719bd78d9SPatrick Williams          bootEnable{std::move(bootEnable)}](const boost::system::error_code ec,
120819bd78d9SPatrick Williams                                             const std::variant<bool>& oneTime) {
1209491d8ee7SSantosh Puranik             if (ec)
1210491d8ee7SSantosh Puranik             {
1211491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1212491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1213491d8ee7SSantosh Puranik                 return;
1214491d8ee7SSantosh Puranik             }
1215491d8ee7SSantosh Puranik 
1216491d8ee7SSantosh Puranik             const bool* oneTimePtr = std::get_if<bool>(&oneTime);
1217491d8ee7SSantosh Puranik 
1218491d8ee7SSantosh Puranik             if (!oneTimePtr)
1219491d8ee7SSantosh Puranik             {
1220491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1221491d8ee7SSantosh Puranik                 return;
1222491d8ee7SSantosh Puranik             }
1223491d8ee7SSantosh Puranik 
1224491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Got one time: " << *oneTimePtr;
1225491d8ee7SSantosh Puranik 
1226491d8ee7SSantosh Puranik             setBootModeOrSource(aResp, *oneTimePtr, std::move(bootSource),
1227491d8ee7SSantosh Puranik                                 std::move(bootEnable));
1228491d8ee7SSantosh Puranik         },
1229491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
1230491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
1231491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
1232491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled");
1233491d8ee7SSantosh Puranik }
1234491d8ee7SSantosh Puranik 
1235c6a620f2SGeorge Liu /**
123669f35306SGunnar Mills  * @brief Sets automaticRetry (Auto Reboot)
123769f35306SGunnar Mills  *
123869f35306SGunnar Mills  * @param[in] aResp   Shared pointer for generating response message.
123969f35306SGunnar Mills  * @param[in] automaticRetryConfig  "AutomaticRetryConfig" from request.
124069f35306SGunnar Mills  *
124169f35306SGunnar Mills  * @return None.
124269f35306SGunnar Mills  */
124369f35306SGunnar Mills static void setAutomaticRetry(std::shared_ptr<AsyncResp> aResp,
124469f35306SGunnar Mills                               const std::string&& automaticRetryConfig)
124569f35306SGunnar Mills {
124669f35306SGunnar Mills     BMCWEB_LOG_DEBUG << "Set Automatic Retry.";
124769f35306SGunnar Mills 
124869f35306SGunnar Mills     // OpenBMC only supports "Disabled" and "RetryAttempts".
124969f35306SGunnar Mills     bool autoRebootEnabled;
125069f35306SGunnar Mills 
125169f35306SGunnar Mills     if (automaticRetryConfig == "Disabled")
125269f35306SGunnar Mills     {
125369f35306SGunnar Mills         autoRebootEnabled = false;
125469f35306SGunnar Mills     }
125569f35306SGunnar Mills     else if (automaticRetryConfig == "RetryAttempts")
125669f35306SGunnar Mills     {
125769f35306SGunnar Mills         autoRebootEnabled = true;
125869f35306SGunnar Mills     }
125969f35306SGunnar Mills     else
126069f35306SGunnar Mills     {
126169f35306SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid property value for "
126269f35306SGunnar Mills                             "AutomaticRetryConfig: "
126369f35306SGunnar Mills                          << automaticRetryConfig;
126469f35306SGunnar Mills         messages::propertyValueNotInList(aResp->res, automaticRetryConfig,
126569f35306SGunnar Mills                                          "AutomaticRetryConfig");
126669f35306SGunnar Mills         return;
126769f35306SGunnar Mills     }
126869f35306SGunnar Mills 
126969f35306SGunnar Mills     crow::connections::systemBus->async_method_call(
127069f35306SGunnar Mills         [aResp](const boost::system::error_code ec) {
127169f35306SGunnar Mills             if (ec)
127269f35306SGunnar Mills             {
127369f35306SGunnar Mills                 messages::internalError(aResp->res);
127469f35306SGunnar Mills                 return;
127569f35306SGunnar Mills             }
127669f35306SGunnar Mills         },
127769f35306SGunnar Mills         "xyz.openbmc_project.Settings",
127869f35306SGunnar Mills         "/xyz/openbmc_project/control/host0/auto_reboot",
127969f35306SGunnar Mills         "org.freedesktop.DBus.Properties", "Set",
128069f35306SGunnar Mills         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
128169f35306SGunnar Mills         std::variant<bool>(autoRebootEnabled));
128269f35306SGunnar Mills }
128369f35306SGunnar Mills 
128469f35306SGunnar Mills /**
1285c6a620f2SGeorge Liu  * @brief Sets power restore policy properties.
1286c6a620f2SGeorge Liu  *
1287c6a620f2SGeorge Liu  * @param[in] aResp   Shared pointer for generating response message.
1288c6a620f2SGeorge Liu  * @param[in] policy  power restore policy properties from request.
1289c6a620f2SGeorge Liu  *
1290c6a620f2SGeorge Liu  * @return None.
1291c6a620f2SGeorge Liu  */
1292c6a620f2SGeorge Liu static void setPowerRestorePolicy(std::shared_ptr<AsyncResp> aResp,
1293c6a620f2SGeorge Liu                                   std::optional<std::string> policy)
1294c6a620f2SGeorge Liu {
1295c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Set power restore policy.";
1296c6a620f2SGeorge Liu 
1297c6a620f2SGeorge Liu     const boost::container::flat_map<std::string, std::string> policyMaps = {
1298c6a620f2SGeorge Liu         {"AlwaysOn", "xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1299c6a620f2SGeorge Liu                      "AlwaysOn"},
1300c6a620f2SGeorge Liu         {"AlwaysOff", "xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1301c6a620f2SGeorge Liu                       "AlwaysOff"},
1302c6a620f2SGeorge Liu         {"LastState", "xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1303c6a620f2SGeorge Liu                       "LastState"}};
1304c6a620f2SGeorge Liu 
1305c6a620f2SGeorge Liu     std::string powerRestorPolicy;
1306c6a620f2SGeorge Liu 
1307c6a620f2SGeorge Liu     auto policyMapsIt = policyMaps.find(*policy);
1308c6a620f2SGeorge Liu     if (policyMapsIt == policyMaps.end())
1309c6a620f2SGeorge Liu     {
1310c6a620f2SGeorge Liu         messages::internalError(aResp->res);
1311c6a620f2SGeorge Liu         return;
1312c6a620f2SGeorge Liu     }
1313c6a620f2SGeorge Liu 
1314c6a620f2SGeorge Liu     powerRestorPolicy = policyMapsIt->second;
1315c6a620f2SGeorge Liu 
1316c6a620f2SGeorge Liu     crow::connections::systemBus->async_method_call(
1317c6a620f2SGeorge Liu         [aResp](const boost::system::error_code ec) {
1318c6a620f2SGeorge Liu             if (ec)
1319c6a620f2SGeorge Liu             {
1320c6a620f2SGeorge Liu                 messages::internalError(aResp->res);
1321c6a620f2SGeorge Liu                 return;
1322c6a620f2SGeorge Liu             }
1323c6a620f2SGeorge Liu         },
1324c6a620f2SGeorge Liu         "xyz.openbmc_project.Settings",
1325c6a620f2SGeorge Liu         "/xyz/openbmc_project/control/host0/power_restore_policy",
1326c6a620f2SGeorge Liu         "org.freedesktop.DBus.Properties", "Set",
1327c6a620f2SGeorge Liu         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
1328c6a620f2SGeorge Liu         std::variant<std::string>(powerRestorPolicy));
1329c6a620f2SGeorge Liu }
1330c6a620f2SGeorge Liu 
1331a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
1332a6349918SAppaRao Puli /**
1333a6349918SAppaRao Puli  * @brief Retrieves provisioning status
1334a6349918SAppaRao Puli  *
1335a6349918SAppaRao Puli  * @param[in] aResp     Shared pointer for completing asynchronous calls.
1336a6349918SAppaRao Puli  *
1337a6349918SAppaRao Puli  * @return None.
1338a6349918SAppaRao Puli  */
1339a6349918SAppaRao Puli void getProvisioningStatus(std::shared_ptr<AsyncResp> aResp)
1340a6349918SAppaRao Puli {
1341a6349918SAppaRao Puli     BMCWEB_LOG_DEBUG << "Get OEM information.";
1342a6349918SAppaRao Puli     crow::connections::systemBus->async_method_call(
1343a6349918SAppaRao Puli         [aResp](const boost::system::error_code ec,
1344*1214b7e7SGunnar Mills                 const std::vector<std::pair<std::string, VariantType>>&
1345*1214b7e7SGunnar Mills                     propertiesList) {
1346a6349918SAppaRao Puli             if (ec)
1347a6349918SAppaRao Puli             {
1348a6349918SAppaRao Puli                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1349a6349918SAppaRao Puli                 messages::internalError(aResp->res);
1350a6349918SAppaRao Puli                 return;
1351a6349918SAppaRao Puli             }
1352a6349918SAppaRao Puli 
1353a6349918SAppaRao Puli             const bool* provState = nullptr;
1354a6349918SAppaRao Puli             const bool* lockState = nullptr;
1355a6349918SAppaRao Puli             for (const std::pair<std::string, VariantType>& property :
1356a6349918SAppaRao Puli                  propertiesList)
1357a6349918SAppaRao Puli             {
1358a6349918SAppaRao Puli                 if (property.first == "UfmProvisioned")
1359a6349918SAppaRao Puli                 {
1360a6349918SAppaRao Puli                     provState = std::get_if<bool>(&property.second);
1361a6349918SAppaRao Puli                 }
1362a6349918SAppaRao Puli                 else if (property.first == "UfmLocked")
1363a6349918SAppaRao Puli                 {
1364a6349918SAppaRao Puli                     lockState = std::get_if<bool>(&property.second);
1365a6349918SAppaRao Puli                 }
1366a6349918SAppaRao Puli             }
1367a6349918SAppaRao Puli 
1368a6349918SAppaRao Puli             if ((provState == nullptr) || (lockState == nullptr))
1369a6349918SAppaRao Puli             {
1370a6349918SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Unable to get PFR attributes.";
1371a6349918SAppaRao Puli                 messages::internalError(aResp->res);
1372a6349918SAppaRao Puli                 return;
1373a6349918SAppaRao Puli             }
1374a6349918SAppaRao Puli 
1375a6349918SAppaRao Puli             nlohmann::json& oemPFR =
1376a6349918SAppaRao Puli                 aResp->res.jsonValue["Oem"]["OpenBmc"]["FirmwareProvisioning"];
1377a6349918SAppaRao Puli             if (*provState == true)
1378a6349918SAppaRao Puli             {
1379a6349918SAppaRao Puli                 if (*lockState == true)
1380a6349918SAppaRao Puli                 {
1381a6349918SAppaRao Puli                     oemPFR["ProvisioningStatus"] = "ProvisionedAndLocked";
1382a6349918SAppaRao Puli                 }
1383a6349918SAppaRao Puli                 else
1384a6349918SAppaRao Puli                 {
1385a6349918SAppaRao Puli                     oemPFR["ProvisioningStatus"] = "ProvisionedButNotLocked";
1386a6349918SAppaRao Puli                 }
1387a6349918SAppaRao Puli             }
1388a6349918SAppaRao Puli             else
1389a6349918SAppaRao Puli             {
1390a6349918SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "NotProvisioned";
1391a6349918SAppaRao Puli             }
1392a6349918SAppaRao Puli         },
1393a6349918SAppaRao Puli         "xyz.openbmc_project.PFR.Manager", "/xyz/openbmc_project/pfr",
1394a6349918SAppaRao Puli         "org.freedesktop.DBus.Properties", "GetAll",
1395a6349918SAppaRao Puli         "xyz.openbmc_project.PFR.Attributes");
1396a6349918SAppaRao Puli }
1397a6349918SAppaRao Puli #endif
1398a6349918SAppaRao Puli 
1399491d8ee7SSantosh Puranik /**
140051709ffdSYong Li  * @brief Translates watchdog timeout action DBUS property value to redfish.
140151709ffdSYong Li  *
140251709ffdSYong Li  * @param[in] dbusAction    The watchdog timeout action in D-BUS.
140351709ffdSYong Li  *
140451709ffdSYong Li  * @return Returns as a string, the timeout action in Redfish terms. If
140551709ffdSYong Li  * translation cannot be done, returns an empty string.
140651709ffdSYong Li  */
140751709ffdSYong Li static std::string dbusToRfWatchdogAction(const std::string& dbusAction)
140851709ffdSYong Li {
140951709ffdSYong Li     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.None")
141051709ffdSYong Li     {
141151709ffdSYong Li         return "None";
141251709ffdSYong Li     }
141351709ffdSYong Li     else if (dbusAction ==
141451709ffdSYong Li              "xyz.openbmc_project.State.Watchdog.Action.HardReset")
141551709ffdSYong Li     {
141651709ffdSYong Li         return "ResetSystem";
141751709ffdSYong Li     }
141851709ffdSYong Li     else if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerOff")
141951709ffdSYong Li     {
142051709ffdSYong Li         return "PowerDown";
142151709ffdSYong Li     }
142251709ffdSYong Li     else if (dbusAction ==
142351709ffdSYong Li              "xyz.openbmc_project.State.Watchdog.Action.PowerCycle")
142451709ffdSYong Li     {
142551709ffdSYong Li         return "PowerCycle";
142651709ffdSYong Li     }
142751709ffdSYong Li 
142851709ffdSYong Li     return "";
142951709ffdSYong Li }
143051709ffdSYong Li 
143151709ffdSYong Li /**
1432c45f0082SYong Li  *@brief Translates timeout action from Redfish to DBUS property value.
1433c45f0082SYong Li  *
1434c45f0082SYong Li  *@param[in] rfAction The timeout action in Redfish.
1435c45f0082SYong Li  *
1436c45f0082SYong Li  *@return Returns as a string, the time_out action as expected by DBUS.
1437c45f0082SYong Li  *If translation cannot be done, returns an empty string.
1438c45f0082SYong Li  */
1439c45f0082SYong Li 
1440c45f0082SYong Li static std::string rfToDbusWDTTimeOutAct(const std::string& rfAction)
1441c45f0082SYong Li {
1442c45f0082SYong Li     if (rfAction == "None")
1443c45f0082SYong Li     {
1444c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.None";
1445c45f0082SYong Li     }
1446c45f0082SYong Li     else if (rfAction == "PowerCycle")
1447c45f0082SYong Li     {
1448c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerCycle";
1449c45f0082SYong Li     }
1450c45f0082SYong Li     else if (rfAction == "PowerDown")
1451c45f0082SYong Li     {
1452c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerOff";
1453c45f0082SYong Li     }
1454c45f0082SYong Li     else if (rfAction == "ResetSystem")
1455c45f0082SYong Li     {
1456c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.HardReset";
1457c45f0082SYong Li     }
1458c45f0082SYong Li 
1459c45f0082SYong Li     return "";
1460c45f0082SYong Li }
1461c45f0082SYong Li 
1462c45f0082SYong Li /**
146351709ffdSYong Li  * @brief Retrieves host watchdog timer properties over DBUS
146451709ffdSYong Li  *
146551709ffdSYong Li  * @param[in] aResp     Shared pointer for completing asynchronous calls.
146651709ffdSYong Li  *
146751709ffdSYong Li  * @return None.
146851709ffdSYong Li  */
146951709ffdSYong Li void getHostWatchdogTimer(std::shared_ptr<AsyncResp> aResp)
147051709ffdSYong Li {
147151709ffdSYong Li     BMCWEB_LOG_DEBUG << "Get host watchodg";
147251709ffdSYong Li     crow::connections::systemBus->async_method_call(
147351709ffdSYong Li         [aResp](const boost::system::error_code ec,
147451709ffdSYong Li                 PropertiesType& properties) {
147551709ffdSYong Li             if (ec)
147651709ffdSYong Li             {
147751709ffdSYong Li                 // watchdog service is stopped
147851709ffdSYong Li                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
147951709ffdSYong Li                 return;
148051709ffdSYong Li             }
148151709ffdSYong Li 
148251709ffdSYong Li             BMCWEB_LOG_DEBUG << "Got " << properties.size() << " wdt prop.";
148351709ffdSYong Li 
148451709ffdSYong Li             nlohmann::json& hostWatchdogTimer =
148551709ffdSYong Li                 aResp->res.jsonValue["HostWatchdogTimer"];
148651709ffdSYong Li 
148751709ffdSYong Li             // watchdog service is running/enabled
148851709ffdSYong Li             hostWatchdogTimer["Status"]["State"] = "Enabled";
148951709ffdSYong Li 
149051709ffdSYong Li             for (const auto& property : properties)
149151709ffdSYong Li             {
149251709ffdSYong Li                 BMCWEB_LOG_DEBUG << "prop=" << property.first;
149351709ffdSYong Li                 if (property.first == "Enabled")
149451709ffdSYong Li                 {
149551709ffdSYong Li                     const bool* state = std::get_if<bool>(&property.second);
149651709ffdSYong Li 
149751709ffdSYong Li                     if (!state)
149851709ffdSYong Li                     {
149951709ffdSYong Li                         messages::internalError(aResp->res);
150051709ffdSYong Li                         continue;
150151709ffdSYong Li                     }
150251709ffdSYong Li 
150351709ffdSYong Li                     hostWatchdogTimer["FunctionEnabled"] = *state;
150451709ffdSYong Li                 }
150551709ffdSYong Li                 else if (property.first == "ExpireAction")
150651709ffdSYong Li                 {
150751709ffdSYong Li                     const std::string* s =
150851709ffdSYong Li                         std::get_if<std::string>(&property.second);
150951709ffdSYong Li                     if (!s)
151051709ffdSYong Li                     {
151151709ffdSYong Li                         messages::internalError(aResp->res);
151251709ffdSYong Li                         continue;
151351709ffdSYong Li                     }
151451709ffdSYong Li 
151551709ffdSYong Li                     std::string action = dbusToRfWatchdogAction(*s);
151651709ffdSYong Li                     if (action.empty())
151751709ffdSYong Li                     {
151851709ffdSYong Li                         messages::internalError(aResp->res);
151951709ffdSYong Li                         continue;
152051709ffdSYong Li                     }
152151709ffdSYong Li                     hostWatchdogTimer["TimeoutAction"] = action;
152251709ffdSYong Li                 }
152351709ffdSYong Li             }
152451709ffdSYong Li         },
152551709ffdSYong Li         "xyz.openbmc_project.Watchdog", "/xyz/openbmc_project/watchdog/host0",
152651709ffdSYong Li         "org.freedesktop.DBus.Properties", "GetAll",
152751709ffdSYong Li         "xyz.openbmc_project.State.Watchdog");
152851709ffdSYong Li }
152951709ffdSYong Li 
153051709ffdSYong Li /**
1531c45f0082SYong Li  * @brief Sets Host WatchDog Timer properties.
1532c45f0082SYong Li  *
1533c45f0082SYong Li  * @param[in] aResp      Shared pointer for generating response message.
1534c45f0082SYong Li  * @param[in] wdtEnable  The WDTimer Enable value (true/false) from incoming
1535c45f0082SYong Li  *                       RF request.
1536c45f0082SYong Li  * @param[in] wdtTimeOutAction The WDT Timeout action, from incoming RF request.
1537c45f0082SYong Li  *
1538c45f0082SYong Li  * @return None.
1539c45f0082SYong Li  */
1540c45f0082SYong Li static void setWDTProperties(std::shared_ptr<AsyncResp> aResp,
1541c45f0082SYong Li                              const std::optional<bool> wdtEnable,
1542c45f0082SYong Li                              const std::optional<std::string>& wdtTimeOutAction)
1543c45f0082SYong Li {
1544c45f0082SYong Li     BMCWEB_LOG_DEBUG << "Set host watchdog";
1545c45f0082SYong Li 
1546c45f0082SYong Li     if (wdtTimeOutAction)
1547c45f0082SYong Li     {
1548c45f0082SYong Li         std::string wdtTimeOutActStr = rfToDbusWDTTimeOutAct(*wdtTimeOutAction);
1549c45f0082SYong Li         // check if TimeOut Action is Valid
1550c45f0082SYong Li         if (wdtTimeOutActStr.empty())
1551c45f0082SYong Li         {
1552c45f0082SYong Li             BMCWEB_LOG_DEBUG << "Unsupported value for TimeoutAction: "
1553c45f0082SYong Li                              << *wdtTimeOutAction;
1554c45f0082SYong Li             messages::propertyValueNotInList(aResp->res, *wdtTimeOutAction,
1555c45f0082SYong Li                                              "TimeoutAction");
1556c45f0082SYong Li             return;
1557c45f0082SYong Li         }
1558c45f0082SYong Li 
1559c45f0082SYong Li         crow::connections::systemBus->async_method_call(
1560c45f0082SYong Li             [aResp](const boost::system::error_code ec) {
1561c45f0082SYong Li                 if (ec)
1562c45f0082SYong Li                 {
1563c45f0082SYong Li                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1564c45f0082SYong Li                     messages::internalError(aResp->res);
1565c45f0082SYong Li                     return;
1566c45f0082SYong Li                 }
1567c45f0082SYong Li             },
1568c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
1569c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
1570c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
1571c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "ExpireAction",
1572c45f0082SYong Li             std::variant<std::string>(wdtTimeOutActStr));
1573c45f0082SYong Li     }
1574c45f0082SYong Li 
1575c45f0082SYong Li     if (wdtEnable)
1576c45f0082SYong Li     {
1577c45f0082SYong Li         crow::connections::systemBus->async_method_call(
1578c45f0082SYong Li             [aResp](const boost::system::error_code ec) {
1579c45f0082SYong Li                 if (ec)
1580c45f0082SYong Li                 {
1581c45f0082SYong Li                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1582c45f0082SYong Li                     messages::internalError(aResp->res);
1583c45f0082SYong Li                     return;
1584c45f0082SYong Li                 }
1585c45f0082SYong Li             },
1586c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
1587c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
1588c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
1589c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "Enabled",
1590c45f0082SYong Li             std::variant<bool>(*wdtEnable));
1591c45f0082SYong Li     }
1592c45f0082SYong Li }
1593c45f0082SYong Li 
1594c45f0082SYong Li /**
1595c5b2abe0SLewanczyk, Dawid  * SystemsCollection derived class for delivering ComputerSystems Collection
1596c5b2abe0SLewanczyk, Dawid  * Schema
1597c5b2abe0SLewanczyk, Dawid  */
15981abe55efSEd Tanous class SystemsCollection : public Node
15991abe55efSEd Tanous {
1600c5b2abe0SLewanczyk, Dawid   public:
16011abe55efSEd Tanous     SystemsCollection(CrowApp& app) : Node(app, "/redfish/v1/Systems/")
16021abe55efSEd Tanous     {
1603c5b2abe0SLewanczyk, Dawid         entityPrivileges = {
1604c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::get, {{"Login"}}},
1605c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::head, {{"Login"}}},
1606c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1607c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1608c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1609c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1610c5b2abe0SLewanczyk, Dawid     }
1611c5b2abe0SLewanczyk, Dawid 
1612c5b2abe0SLewanczyk, Dawid   private:
161355c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
16141abe55efSEd Tanous                const std::vector<std::string>& params) override
16151abe55efSEd Tanous     {
1616462023adSSunitha Harish         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
16170f74e643SEd Tanous         res.jsonValue["@odata.type"] =
16180f74e643SEd Tanous             "#ComputerSystemCollection.ComputerSystemCollection";
16190f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Systems";
16200f74e643SEd Tanous         res.jsonValue["Name"] = "Computer System Collection";
1621462023adSSunitha Harish 
1622462023adSSunitha Harish         crow::connections::systemBus->async_method_call(
1623462023adSSunitha Harish             [asyncResp](const boost::system::error_code ec,
1624462023adSSunitha Harish                         const std::variant<std::string>& hostName) {
1625462023adSSunitha Harish                 nlohmann::json& iface_array =
1626462023adSSunitha Harish                     asyncResp->res.jsonValue["Members"];
1627462023adSSunitha Harish                 iface_array = nlohmann::json::array();
1628462023adSSunitha Harish                 auto& count = asyncResp->res.jsonValue["Members@odata.count"];
1629462023adSSunitha Harish                 count = 0;
1630462023adSSunitha Harish                 if (ec)
1631462023adSSunitha Harish                 {
1632462023adSSunitha Harish                     iface_array.push_back(
1633462023adSSunitha Harish                         {{"@odata.id", "/redfish/v1/Systems/system"}});
1634462023adSSunitha Harish                     count = iface_array.size();
1635462023adSSunitha Harish                     return;
1636462023adSSunitha Harish                 }
1637462023adSSunitha Harish                 BMCWEB_LOG_DEBUG << "Hypervisor is available";
1638462023adSSunitha Harish                 iface_array.push_back(
1639462023adSSunitha Harish                     {{"@odata.id", "/redfish/v1/Systems/system"}});
1640462023adSSunitha Harish                 iface_array.push_back(
1641462023adSSunitha Harish                     {{"@odata.id", "/redfish/v1/Systems/hypervisor"}});
1642462023adSSunitha Harish                 count = iface_array.size();
1643462023adSSunitha Harish             },
1644462023adSSunitha Harish             "xyz.openbmc_project.Settings", "/xyz/openbmc_project/network/vmi",
1645462023adSSunitha Harish             "org.freedesktop.DBus.Properties", "Get",
1646462023adSSunitha Harish             "xyz.openbmc_project.Network.SystemConfiguration", "HostName");
1647c5b2abe0SLewanczyk, Dawid     }
1648c5b2abe0SLewanczyk, Dawid };
1649c5b2abe0SLewanczyk, Dawid 
1650c5b2abe0SLewanczyk, Dawid /**
1651cc340dd9SEd Tanous  * SystemActionsReset class supports handle POST method for Reset action.
1652cc340dd9SEd Tanous  * The class retrieves and sends data directly to D-Bus.
1653cc340dd9SEd Tanous  */
1654cc340dd9SEd Tanous class SystemActionsReset : public Node
1655cc340dd9SEd Tanous {
1656cc340dd9SEd Tanous   public:
1657cc340dd9SEd Tanous     SystemActionsReset(CrowApp& app) :
1658029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset/")
1659cc340dd9SEd Tanous     {
1660cc340dd9SEd Tanous         entityPrivileges = {
1661cc340dd9SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1662cc340dd9SEd Tanous     }
1663cc340dd9SEd Tanous 
1664cc340dd9SEd Tanous   private:
1665cc340dd9SEd Tanous     /**
1666cc340dd9SEd Tanous      * Function handles POST method request.
1667cc340dd9SEd Tanous      * Analyzes POST body message before sends Reset request data to D-Bus.
1668cc340dd9SEd Tanous      */
1669cc340dd9SEd Tanous     void doPost(crow::Response& res, const crow::Request& req,
1670cc340dd9SEd Tanous                 const std::vector<std::string>& params) override
1671cc340dd9SEd Tanous     {
1672cc340dd9SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1673cc340dd9SEd Tanous 
16749712f8acSEd Tanous         std::string resetType;
16759712f8acSEd Tanous         if (!json_util::readJson(req, res, "ResetType", resetType))
1676cc340dd9SEd Tanous         {
1677cc340dd9SEd Tanous             return;
1678cc340dd9SEd Tanous         }
1679cc340dd9SEd Tanous 
1680d22c8396SJason M. Bills         // Get the command and host vs. chassis
1681cc340dd9SEd Tanous         std::string command;
1682d22c8396SJason M. Bills         bool hostCommand;
16839712f8acSEd Tanous         if (resetType == "On")
1684cc340dd9SEd Tanous         {
1685cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.On";
1686d22c8396SJason M. Bills             hostCommand = true;
1687d22c8396SJason M. Bills         }
1688d22c8396SJason M. Bills         else if (resetType == "ForceOff")
1689d22c8396SJason M. Bills         {
1690d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Chassis.Transition.Off";
1691d22c8396SJason M. Bills             hostCommand = false;
1692d22c8396SJason M. Bills         }
1693d22c8396SJason M. Bills         else if (resetType == "ForceOn")
1694d22c8396SJason M. Bills         {
1695d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Host.Transition.On";
1696d22c8396SJason M. Bills             hostCommand = true;
1697d22c8396SJason M. Bills         }
1698d22c8396SJason M. Bills         else if (resetType == "ForceRestart")
1699d22c8396SJason M. Bills         {
170086a0851aSJason M. Bills             command =
170186a0851aSJason M. Bills                 "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot";
170286a0851aSJason M. Bills             hostCommand = true;
1703cc340dd9SEd Tanous         }
17049712f8acSEd Tanous         else if (resetType == "GracefulShutdown")
1705cc340dd9SEd Tanous         {
1706cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.Off";
1707d22c8396SJason M. Bills             hostCommand = true;
1708cc340dd9SEd Tanous         }
17099712f8acSEd Tanous         else if (resetType == "GracefulRestart")
1710cc340dd9SEd Tanous         {
171186a0851aSJason M. Bills             command =
171286a0851aSJason M. Bills                 "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot";
1713d22c8396SJason M. Bills             hostCommand = true;
1714d22c8396SJason M. Bills         }
1715d22c8396SJason M. Bills         else if (resetType == "PowerCycle")
1716d22c8396SJason M. Bills         {
171786a0851aSJason M. Bills             command = "xyz.openbmc_project.State.Host.Transition.Reboot";
171886a0851aSJason M. Bills             hostCommand = true;
1719cc340dd9SEd Tanous         }
1720bfd5b826SLakshminarayana R. Kammath         else if (resetType == "Nmi")
1721bfd5b826SLakshminarayana R. Kammath         {
1722bfd5b826SLakshminarayana R. Kammath             doNMI(asyncResp);
1723bfd5b826SLakshminarayana R. Kammath             return;
1724bfd5b826SLakshminarayana R. Kammath         }
1725cc340dd9SEd Tanous         else
1726cc340dd9SEd Tanous         {
1727f12894f8SJason M. Bills             messages::actionParameterUnknown(res, "Reset", resetType);
1728cc340dd9SEd Tanous             return;
1729cc340dd9SEd Tanous         }
1730cc340dd9SEd Tanous 
1731d22c8396SJason M. Bills         if (hostCommand)
1732d22c8396SJason M. Bills         {
1733cc340dd9SEd Tanous             crow::connections::systemBus->async_method_call(
1734d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
1735cc340dd9SEd Tanous                     if (ec)
1736cc340dd9SEd Tanous                     {
1737cc340dd9SEd Tanous                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1738d22c8396SJason M. Bills                         if (ec.value() == boost::asio::error::invalid_argument)
1739d22c8396SJason M. Bills                         {
1740d22c8396SJason M. Bills                             messages::actionParameterNotSupported(
1741d22c8396SJason M. Bills                                 asyncResp->res, resetType, "Reset");
1742d22c8396SJason M. Bills                         }
1743d22c8396SJason M. Bills                         else
1744d22c8396SJason M. Bills                         {
1745f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
1746d22c8396SJason M. Bills                         }
1747cc340dd9SEd Tanous                         return;
1748cc340dd9SEd Tanous                     }
1749f12894f8SJason M. Bills                     messages::success(asyncResp->res);
1750cc340dd9SEd Tanous                 },
1751cc340dd9SEd Tanous                 "xyz.openbmc_project.State.Host",
1752cc340dd9SEd Tanous                 "/xyz/openbmc_project/state/host0",
1753cc340dd9SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
17549712f8acSEd Tanous                 "xyz.openbmc_project.State.Host", "RequestedHostTransition",
1755abf2add6SEd Tanous                 std::variant<std::string>{command});
1756cc340dd9SEd Tanous         }
1757d22c8396SJason M. Bills         else
1758d22c8396SJason M. Bills         {
1759d22c8396SJason M. Bills             crow::connections::systemBus->async_method_call(
1760d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
1761d22c8396SJason M. Bills                     if (ec)
1762d22c8396SJason M. Bills                     {
1763d22c8396SJason M. Bills                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1764d22c8396SJason M. Bills                         if (ec.value() == boost::asio::error::invalid_argument)
1765d22c8396SJason M. Bills                         {
1766d22c8396SJason M. Bills                             messages::actionParameterNotSupported(
1767d22c8396SJason M. Bills                                 asyncResp->res, resetType, "Reset");
1768d22c8396SJason M. Bills                         }
1769d22c8396SJason M. Bills                         else
1770d22c8396SJason M. Bills                         {
1771d22c8396SJason M. Bills                             messages::internalError(asyncResp->res);
1772d22c8396SJason M. Bills                         }
1773d22c8396SJason M. Bills                         return;
1774d22c8396SJason M. Bills                     }
1775d22c8396SJason M. Bills                     messages::success(asyncResp->res);
1776d22c8396SJason M. Bills                 },
1777d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis",
1778d22c8396SJason M. Bills                 "/xyz/openbmc_project/state/chassis0",
1779d22c8396SJason M. Bills                 "org.freedesktop.DBus.Properties", "Set",
1780d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition",
1781d22c8396SJason M. Bills                 std::variant<std::string>{command});
1782d22c8396SJason M. Bills         }
1783d22c8396SJason M. Bills     }
1784bfd5b826SLakshminarayana R. Kammath     /**
1785bfd5b826SLakshminarayana R. Kammath      * Function transceives data with dbus directly.
1786bfd5b826SLakshminarayana R. Kammath      */
1787bfd5b826SLakshminarayana R. Kammath     void doNMI(const std::shared_ptr<AsyncResp>& asyncResp)
1788bfd5b826SLakshminarayana R. Kammath     {
1789bfd5b826SLakshminarayana R. Kammath         constexpr char const* serviceName =
1790bfd5b826SLakshminarayana R. Kammath             "xyz.openbmc_project.Control.Host.NMI";
1791bfd5b826SLakshminarayana R. Kammath         constexpr char const* objectPath =
1792bfd5b826SLakshminarayana R. Kammath             "/xyz/openbmc_project/control/host0/nmi";
1793bfd5b826SLakshminarayana R. Kammath         constexpr char const* interfaceName =
1794bfd5b826SLakshminarayana R. Kammath             "xyz.openbmc_project.Control.Host.NMI";
1795bfd5b826SLakshminarayana R. Kammath         constexpr char const* method = "NMI";
1796bfd5b826SLakshminarayana R. Kammath 
1797bfd5b826SLakshminarayana R. Kammath         crow::connections::systemBus->async_method_call(
1798bfd5b826SLakshminarayana R. Kammath             [asyncResp](const boost::system::error_code ec) {
1799bfd5b826SLakshminarayana R. Kammath                 if (ec)
1800bfd5b826SLakshminarayana R. Kammath                 {
1801bfd5b826SLakshminarayana R. Kammath                     BMCWEB_LOG_ERROR << " Bad D-Bus request error: " << ec;
1802bfd5b826SLakshminarayana R. Kammath                     messages::internalError(asyncResp->res);
1803bfd5b826SLakshminarayana R. Kammath                     return;
1804bfd5b826SLakshminarayana R. Kammath                 }
1805bfd5b826SLakshminarayana R. Kammath                 messages::success(asyncResp->res);
1806bfd5b826SLakshminarayana R. Kammath             },
1807bfd5b826SLakshminarayana R. Kammath             serviceName, objectPath, interfaceName, method);
1808bfd5b826SLakshminarayana R. Kammath     }
1809cc340dd9SEd Tanous };
1810cc340dd9SEd Tanous 
1811cc340dd9SEd Tanous /**
18126617338dSEd Tanous  * Systems derived class for delivering Computer Systems Schema.
1813c5b2abe0SLewanczyk, Dawid  */
18141abe55efSEd Tanous class Systems : public Node
18151abe55efSEd Tanous {
1816c5b2abe0SLewanczyk, Dawid   public:
1817c5b2abe0SLewanczyk, Dawid     /*
1818c5b2abe0SLewanczyk, Dawid      * Default Constructor
1819c5b2abe0SLewanczyk, Dawid      */
1820029573d4SEd Tanous     Systems(CrowApp& app) : Node(app, "/redfish/v1/Systems/system/")
18211abe55efSEd Tanous     {
1822c5b2abe0SLewanczyk, Dawid         entityPrivileges = {
1823c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::get, {{"Login"}}},
1824c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::head, {{"Login"}}},
1825c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1826c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1827c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1828c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1829c5b2abe0SLewanczyk, Dawid     }
1830c5b2abe0SLewanczyk, Dawid 
1831c5b2abe0SLewanczyk, Dawid   private:
1832c5b2abe0SLewanczyk, Dawid     /**
1833c5b2abe0SLewanczyk, Dawid      * Functions triggers appropriate requests on DBus
1834c5b2abe0SLewanczyk, Dawid      */
183555c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
18361abe55efSEd Tanous                const std::vector<std::string>& params) override
18371abe55efSEd Tanous     {
18386bd5a8d2SGunnar Mills         res.jsonValue["@odata.type"] = "#ComputerSystem.v1_11_0.ComputerSystem";
1839450a25cbSGunnar Mills         res.jsonValue["Name"] = "system";
1840029573d4SEd Tanous         res.jsonValue["Id"] = "system";
18410f74e643SEd Tanous         res.jsonValue["SystemType"] = "Physical";
18420f74e643SEd Tanous         res.jsonValue["Description"] = "Computer System";
18430f74e643SEd Tanous         res.jsonValue["ProcessorSummary"]["Count"] = 0;
18440f74e643SEd Tanous         res.jsonValue["ProcessorSummary"]["Status"]["State"] = "Disabled";
18455fd7ba65SCheng C Yang         res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] = uint64_t(0);
18460f74e643SEd Tanous         res.jsonValue["MemorySummary"]["Status"]["State"] = "Disabled";
1847029573d4SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Systems/system";
184804a258f4SEd Tanous 
1849443c2934SRapkiewicz, Pawel         res.jsonValue["Processors"] = {
1850029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/Processors"}};
1851443c2934SRapkiewicz, Pawel         res.jsonValue["Memory"] = {
1852029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/Memory"}};
1853a25aeccfSNikhil Potade         res.jsonValue["Storage"] = {
1854a25aeccfSNikhil Potade             {"@odata.id", "/redfish/v1/Systems/system/Storage"}};
1855029573d4SEd Tanous 
1856cc340dd9SEd Tanous         // TODO Need to support ForceRestart.
1857cc340dd9SEd Tanous         res.jsonValue["Actions"]["#ComputerSystem.Reset"] = {
1858cc340dd9SEd Tanous             {"target",
1859029573d4SEd Tanous              "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset"},
1860cc340dd9SEd Tanous             {"ResetType@Redfish.AllowableValues",
1861d22c8396SJason M. Bills              {"On", "ForceOff", "ForceOn", "ForceRestart", "GracefulRestart",
1862bfd5b826SLakshminarayana R. Kammath               "GracefulShutdown", "PowerCycle", "Nmi"}}};
1863c5b2abe0SLewanczyk, Dawid 
1864c4bf6374SJason M. Bills         res.jsonValue["LogServices"] = {
1865029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/LogServices"}};
1866c4bf6374SJason M. Bills 
1867d82a3acdSCarol Wang         res.jsonValue["Bios"] = {
1868d82a3acdSCarol Wang             {"@odata.id", "/redfish/v1/Systems/system/Bios"}};
1869d82a3acdSCarol Wang 
1870c5d03ff4SJennifer Lee         res.jsonValue["Links"]["ManagedBy"] = {
1871c5d03ff4SJennifer Lee             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
1872c5d03ff4SJennifer Lee 
1873c5d03ff4SJennifer Lee         res.jsonValue["Status"] = {
1874c5d03ff4SJennifer Lee             {"Health", "OK"},
1875c5d03ff4SJennifer Lee             {"State", "Enabled"},
1876c5d03ff4SJennifer Lee         };
1877a0803efaSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1878c5b2abe0SLewanczyk, Dawid 
1879e284a7c1SJames Feist         constexpr const std::array<const char*, 4> inventoryForSystems = {
1880b49ac873SJames Feist             "xyz.openbmc_project.Inventory.Item.Dimm",
18812ad9c2f6SJames Feist             "xyz.openbmc_project.Inventory.Item.Cpu",
1882e284a7c1SJames Feist             "xyz.openbmc_project.Inventory.Item.Drive",
1883e284a7c1SJames Feist             "xyz.openbmc_project.Inventory.Item.StorageController"};
1884b49ac873SJames Feist 
1885b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
1886b49ac873SJames Feist         crow::connections::systemBus->async_method_call(
1887b49ac873SJames Feist             [health](const boost::system::error_code ec,
1888b49ac873SJames Feist                      std::vector<std::string>& resp) {
1889b49ac873SJames Feist                 if (ec)
1890b49ac873SJames Feist                 {
1891b49ac873SJames Feist                     // no inventory
1892b49ac873SJames Feist                     return;
1893b49ac873SJames Feist                 }
1894b49ac873SJames Feist 
1895b49ac873SJames Feist                 health->inventory = std::move(resp);
1896b49ac873SJames Feist             },
1897b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper",
1898b49ac873SJames Feist             "/xyz/openbmc_project/object_mapper",
1899b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1900b49ac873SJames Feist             int32_t(0), inventoryForSystems);
1901b49ac873SJames Feist 
1902b49ac873SJames Feist         health->populate();
1903b49ac873SJames Feist 
1904c5d03ff4SJennifer Lee         getMainChassisId(asyncResp, [](const std::string& chassisId,
1905c5d03ff4SJennifer Lee                                        std::shared_ptr<AsyncResp> aRsp) {
1906c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["Chassis"] = {
1907c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
1908c5d03ff4SJennifer Lee         });
1909a3002228SAppaRao Puli 
1910a3002228SAppaRao Puli         getIndicatorLedState(asyncResp);
19115bc2dc8eSJames Feist         getComputerSystem(asyncResp, health);
19126c34de48SEd Tanous         getHostState(asyncResp);
1913491d8ee7SSantosh Puranik         getBootProperties(asyncResp);
1914adbe192aSJason M. Bills         getPCIeDeviceList(asyncResp, "PCIeDevices");
191551709ffdSYong Li         getHostWatchdogTimer(asyncResp);
1916c6a620f2SGeorge Liu         getPowerRestorePolicy(asyncResp);
19176bd5a8d2SGunnar Mills         getAutomaticRetry(asyncResp);
1918a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
1919a6349918SAppaRao Puli         getProvisioningStatus(asyncResp);
1920a6349918SAppaRao Puli #endif
1921c5b2abe0SLewanczyk, Dawid     }
1922c5b2abe0SLewanczyk, Dawid 
192355c7b7a2SEd Tanous     void doPatch(crow::Response& res, const crow::Request& req,
19241abe55efSEd Tanous                  const std::vector<std::string>& params) override
19251abe55efSEd Tanous     {
1926cde19e5fSSantosh Puranik         std::optional<std::string> indicatorLed;
1927491d8ee7SSantosh Puranik         std::optional<nlohmann::json> bootProps;
1928c45f0082SYong Li         std::optional<nlohmann::json> wdtTimerProps;
1929c6a620f2SGeorge Liu         std::optional<std::string> powerRestorePolicy;
193041352c24SSantosh Puranik         auto asyncResp = std::make_shared<AsyncResp>(res);
193141352c24SSantosh Puranik 
1932944ffaf9SJohnathan Mantey         if (!json_util::readJson(req, res, "IndicatorLED", indicatorLed, "Boot",
1933c6a620f2SGeorge Liu                                  bootProps, "WatchdogTimer", wdtTimerProps,
1934c6a620f2SGeorge Liu                                  "PowerRestorePolicy", powerRestorePolicy))
19356617338dSEd Tanous         {
19366617338dSEd Tanous             return;
19376617338dSEd Tanous         }
1938491d8ee7SSantosh Puranik 
1939944ffaf9SJohnathan Mantey         res.result(boost::beast::http::status::no_content);
1940c45f0082SYong Li 
1941c45f0082SYong Li         if (wdtTimerProps)
1942c45f0082SYong Li         {
1943c45f0082SYong Li             std::optional<bool> wdtEnable;
1944c45f0082SYong Li             std::optional<std::string> wdtTimeOutAction;
1945c45f0082SYong Li 
1946c45f0082SYong Li             if (!json_util::readJson(*wdtTimerProps, asyncResp->res,
1947c45f0082SYong Li                                      "FunctionEnabled", wdtEnable,
1948c45f0082SYong Li                                      "TimeoutAction", wdtTimeOutAction))
1949c45f0082SYong Li             {
1950c45f0082SYong Li                 return;
1951c45f0082SYong Li             }
1952c45f0082SYong Li             setWDTProperties(asyncResp, std::move(wdtEnable),
1953c45f0082SYong Li                              std::move(wdtTimeOutAction));
1954c45f0082SYong Li         }
1955c45f0082SYong Li 
1956491d8ee7SSantosh Puranik         if (bootProps)
1957491d8ee7SSantosh Puranik         {
1958491d8ee7SSantosh Puranik             std::optional<std::string> bootSource;
1959491d8ee7SSantosh Puranik             std::optional<std::string> bootEnable;
196069f35306SGunnar Mills             std::optional<std::string> automaticRetryConfig;
1961491d8ee7SSantosh Puranik 
196269f35306SGunnar Mills             if (!json_util::readJson(
196369f35306SGunnar Mills                     *bootProps, asyncResp->res, "BootSourceOverrideTarget",
196469f35306SGunnar Mills                     bootSource, "BootSourceOverrideEnabled", bootEnable,
196569f35306SGunnar Mills                     "AutomaticRetryConfig", automaticRetryConfig))
1966491d8ee7SSantosh Puranik             {
1967491d8ee7SSantosh Puranik                 return;
1968491d8ee7SSantosh Puranik             }
196969f35306SGunnar Mills             if (bootSource || bootEnable)
197069f35306SGunnar Mills             {
197169f35306SGunnar Mills                 setBootSourceProperties(asyncResp, std::move(bootSource),
1972491d8ee7SSantosh Puranik                                         std::move(bootEnable));
1973491d8ee7SSantosh Puranik             }
197469f35306SGunnar Mills             if (automaticRetryConfig)
197569f35306SGunnar Mills             {
197669f35306SGunnar Mills                 setAutomaticRetry(asyncResp, std::move(*automaticRetryConfig));
197769f35306SGunnar Mills             }
197869f35306SGunnar Mills         }
1979265c1602SJohnathan Mantey 
19809712f8acSEd Tanous         if (indicatorLed)
19816617338dSEd Tanous         {
1982a3002228SAppaRao Puli             setIndicatorLedState(asyncResp, std::move(*indicatorLed));
19836617338dSEd Tanous         }
1984c6a620f2SGeorge Liu 
1985c6a620f2SGeorge Liu         if (powerRestorePolicy)
1986c6a620f2SGeorge Liu         {
1987c6a620f2SGeorge Liu             setPowerRestorePolicy(asyncResp, std::move(*powerRestorePolicy));
1988c6a620f2SGeorge Liu         }
1989c5b2abe0SLewanczyk, Dawid     }
1990c5b2abe0SLewanczyk, Dawid };
1991c5b2abe0SLewanczyk, Dawid } // namespace redfish
1992