xref: /openbmc/bmcweb/features/redfish/lib/systems.hpp (revision 69f353069256230827258dc35e9059cc51982cf2)
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>
27abf2add6SEd Tanous #include <variant>
28c5b2abe0SLewanczyk, Dawid 
291abe55efSEd Tanous namespace redfish
301abe55efSEd Tanous {
31c5b2abe0SLewanczyk, Dawid 
329d3ae10eSAlpana Kumari /**
339d3ae10eSAlpana Kumari  * @brief Updates the Functional State of DIMMs
349d3ae10eSAlpana Kumari  *
359d3ae10eSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
369d3ae10eSAlpana Kumari  * @param[in] dimmState Dimm's Functional state, true/false
379d3ae10eSAlpana Kumari  *
389d3ae10eSAlpana Kumari  * @return None.
399d3ae10eSAlpana Kumari  */
409d3ae10eSAlpana Kumari void updateDimmProperties(std::shared_ptr<AsyncResp> aResp,
419d3ae10eSAlpana Kumari                           const std::variant<bool> &dimmState)
429d3ae10eSAlpana Kumari {
439d3ae10eSAlpana Kumari     const bool *isDimmFunctional = std::get_if<bool>(&dimmState);
449d3ae10eSAlpana Kumari     if (isDimmFunctional == nullptr)
459d3ae10eSAlpana Kumari     {
469d3ae10eSAlpana Kumari         messages::internalError(aResp->res);
479d3ae10eSAlpana Kumari         return;
489d3ae10eSAlpana Kumari     }
499d3ae10eSAlpana Kumari     BMCWEB_LOG_DEBUG << "Dimm Functional: " << *isDimmFunctional;
509d3ae10eSAlpana Kumari 
519d3ae10eSAlpana Kumari     // Set it as Enabled if atleast one DIMM is functional
529d3ae10eSAlpana Kumari     // Update STATE only if previous State was DISABLED and current Dimm is
539d3ae10eSAlpana Kumari     // ENABLED.
549d3ae10eSAlpana Kumari     nlohmann::json &prevMemSummary =
559d3ae10eSAlpana Kumari         aResp->res.jsonValue["MemorySummary"]["Status"]["State"];
569d3ae10eSAlpana Kumari     if (prevMemSummary == "Disabled")
579d3ae10eSAlpana Kumari     {
589d3ae10eSAlpana Kumari         if (*isDimmFunctional == true)
599d3ae10eSAlpana Kumari         {
609d3ae10eSAlpana Kumari             aResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
619d3ae10eSAlpana Kumari                 "Enabled";
629d3ae10eSAlpana Kumari         }
639d3ae10eSAlpana Kumari     }
649d3ae10eSAlpana Kumari }
659d3ae10eSAlpana Kumari 
6657e8c9beSAlpana Kumari /*
6757e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Count" based on Cpu PresenceState
6857e8c9beSAlpana Kumari  *
6957e8c9beSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
7057e8c9beSAlpana Kumari  * @param[in] cpuPresenceState CPU present or not
7157e8c9beSAlpana Kumari  *
7257e8c9beSAlpana Kumari  * @return None.
7357e8c9beSAlpana Kumari  */
7457e8c9beSAlpana Kumari void modifyCpuPresenceState(std::shared_ptr<AsyncResp> aResp,
7557e8c9beSAlpana Kumari                             const std::variant<bool> &cpuPresenceState)
7657e8c9beSAlpana Kumari {
7757e8c9beSAlpana Kumari     const bool *isCpuPresent = std::get_if<bool>(&cpuPresenceState);
7857e8c9beSAlpana Kumari 
7957e8c9beSAlpana Kumari     if (isCpuPresent == nullptr)
8057e8c9beSAlpana Kumari     {
8157e8c9beSAlpana Kumari         messages::internalError(aResp->res);
8257e8c9beSAlpana Kumari         return;
8357e8c9beSAlpana Kumari     }
8457e8c9beSAlpana Kumari     BMCWEB_LOG_DEBUG << "Cpu Present: " << *isCpuPresent;
8557e8c9beSAlpana Kumari 
8657e8c9beSAlpana Kumari     if (*isCpuPresent == true)
8757e8c9beSAlpana Kumari     {
88b4b9595aSJames Feist         nlohmann::json &procCount =
89b4b9595aSJames Feist             aResp->res.jsonValue["ProcessorSummary"]["Count"];
90b4b9595aSJames Feist         auto procCountPtr =
91b4b9595aSJames Feist             procCount.get_ptr<nlohmann::json::number_integer_t *>();
92b4b9595aSJames Feist         if (procCountPtr != nullptr)
93b4b9595aSJames Feist         {
94b4b9595aSJames Feist             // shouldn't be possible to be nullptr
95b4b9595aSJames Feist             *procCountPtr += 1;
9657e8c9beSAlpana Kumari         }
97b4b9595aSJames Feist     }
9857e8c9beSAlpana Kumari }
9957e8c9beSAlpana Kumari 
10057e8c9beSAlpana Kumari /*
10157e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Status" "State" based on
10257e8c9beSAlpana Kumari  *        CPU Functional State
10357e8c9beSAlpana Kumari  *
10457e8c9beSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
10557e8c9beSAlpana Kumari  * @param[in] cpuFunctionalState is CPU functional true/false
10657e8c9beSAlpana Kumari  *
10757e8c9beSAlpana Kumari  * @return None.
10857e8c9beSAlpana Kumari  */
10957e8c9beSAlpana Kumari void modifyCpuFunctionalState(std::shared_ptr<AsyncResp> aResp,
11057e8c9beSAlpana Kumari                               const std::variant<bool> &cpuFunctionalState)
11157e8c9beSAlpana Kumari {
11257e8c9beSAlpana Kumari     const bool *isCpuFunctional = std::get_if<bool>(&cpuFunctionalState);
11357e8c9beSAlpana Kumari 
11457e8c9beSAlpana Kumari     if (isCpuFunctional == nullptr)
11557e8c9beSAlpana Kumari     {
11657e8c9beSAlpana Kumari         messages::internalError(aResp->res);
11757e8c9beSAlpana Kumari         return;
11857e8c9beSAlpana Kumari     }
11957e8c9beSAlpana Kumari     BMCWEB_LOG_DEBUG << "Cpu Functional: " << *isCpuFunctional;
12057e8c9beSAlpana Kumari 
12157e8c9beSAlpana Kumari     nlohmann::json &prevProcState =
12257e8c9beSAlpana Kumari         aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"];
12357e8c9beSAlpana Kumari 
12457e8c9beSAlpana Kumari     // Set it as Enabled if atleast one CPU is functional
12557e8c9beSAlpana Kumari     // Update STATE only if previous State was Non_Functional and current CPU is
12657e8c9beSAlpana Kumari     // Functional.
12757e8c9beSAlpana Kumari     if (prevProcState == "Disabled")
12857e8c9beSAlpana Kumari     {
12957e8c9beSAlpana Kumari         if (*isCpuFunctional == true)
13057e8c9beSAlpana Kumari         {
13157e8c9beSAlpana Kumari             aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
13257e8c9beSAlpana Kumari                 "Enabled";
13357e8c9beSAlpana Kumari         }
13457e8c9beSAlpana Kumari     }
13557e8c9beSAlpana Kumari }
13657e8c9beSAlpana Kumari 
13757e8c9beSAlpana Kumari /*
138c5b2abe0SLewanczyk, Dawid  * @brief Retrieves computer system properties over dbus
139c5b2abe0SLewanczyk, Dawid  *
140c5b2abe0SLewanczyk, Dawid  * @param[in] aResp Shared pointer for completing asynchronous calls
141c5b2abe0SLewanczyk, Dawid  * @param[in] name  Computer system name from request
142c5b2abe0SLewanczyk, Dawid  *
143c5b2abe0SLewanczyk, Dawid  * @return None.
144c5b2abe0SLewanczyk, Dawid  */
1455bc2dc8eSJames Feist void getComputerSystem(std::shared_ptr<AsyncResp> aResp,
1465bc2dc8eSJames Feist                        std::shared_ptr<HealthPopulate> systemHealth)
1471abe55efSEd Tanous {
14855c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get available system components.";
1499d3ae10eSAlpana Kumari 
15055c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
1515bc2dc8eSJames Feist         [aResp, systemHealth](
152c5b2abe0SLewanczyk, Dawid             const boost::system::error_code ec,
153c5b2abe0SLewanczyk, Dawid             const std::vector<std::pair<
1546c34de48SEd Tanous                 std::string,
1556c34de48SEd Tanous                 std::vector<std::pair<std::string, std::vector<std::string>>>>>
156c5b2abe0SLewanczyk, Dawid                 &subtree) {
1571abe55efSEd Tanous             if (ec)
1581abe55efSEd Tanous             {
15955c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error";
160f12894f8SJason M. Bills                 messages::internalError(aResp->res);
161c5b2abe0SLewanczyk, Dawid                 return;
162c5b2abe0SLewanczyk, Dawid             }
163c5b2abe0SLewanczyk, Dawid             // Iterate over all retrieved ObjectPaths.
1646c34de48SEd Tanous             for (const std::pair<std::string,
1656c34de48SEd Tanous                                  std::vector<std::pair<
1666c34de48SEd Tanous                                      std::string, std::vector<std::string>>>>
1671abe55efSEd Tanous                      &object : subtree)
1681abe55efSEd Tanous             {
169c5b2abe0SLewanczyk, Dawid                 const std::string &path = object.first;
17055c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "Got path: " << path;
1711abe55efSEd Tanous                 const std::vector<
1721abe55efSEd Tanous                     std::pair<std::string, std::vector<std::string>>>
173c5b2abe0SLewanczyk, Dawid                     &connectionNames = object.second;
1741abe55efSEd Tanous                 if (connectionNames.size() < 1)
1751abe55efSEd Tanous                 {
176c5b2abe0SLewanczyk, Dawid                     continue;
177c5b2abe0SLewanczyk, Dawid                 }
178029573d4SEd Tanous 
1795bc2dc8eSJames Feist                 auto memoryHealth = std::make_shared<HealthPopulate>(
1805bc2dc8eSJames Feist                     aResp, aResp->res.jsonValue["MemorySummary"]["Status"]);
1815bc2dc8eSJames Feist 
1825bc2dc8eSJames Feist                 auto cpuHealth = std::make_shared<HealthPopulate>(
1835bc2dc8eSJames Feist                     aResp, aResp->res.jsonValue["ProcessorSummary"]["Status"]);
1845bc2dc8eSJames Feist 
1855bc2dc8eSJames Feist                 systemHealth->children.emplace_back(memoryHealth);
1865bc2dc8eSJames Feist                 systemHealth->children.emplace_back(cpuHealth);
1875bc2dc8eSJames Feist 
1886c34de48SEd Tanous                 // This is not system, so check if it's cpu, dimm, UUID or
1896c34de48SEd Tanous                 // BiosVer
19004a258f4SEd Tanous                 for (const auto &connection : connectionNames)
1911abe55efSEd Tanous                 {
19204a258f4SEd Tanous                     for (const auto &interfaceName : connection.second)
1931abe55efSEd Tanous                     {
19404a258f4SEd Tanous                         if (interfaceName ==
19504a258f4SEd Tanous                             "xyz.openbmc_project.Inventory.Item.Dimm")
1961abe55efSEd Tanous                         {
1971abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
19804a258f4SEd Tanous                                 << "Found Dimm, now get its properties.";
1999d3ae10eSAlpana Kumari 
20055c7b7a2SEd Tanous                             crow::connections::systemBus->async_method_call(
2019d3ae10eSAlpana Kumari                                 [aResp, service{connection.first},
2029d3ae10eSAlpana Kumari                                  path(std::move(path))](
2039d3ae10eSAlpana Kumari                                     const boost::system::error_code ec,
2046c34de48SEd Tanous                                     const std::vector<
2056c34de48SEd Tanous                                         std::pair<std::string, VariantType>>
2061abe55efSEd Tanous                                         &properties) {
2071abe55efSEd Tanous                                     if (ec)
2081abe55efSEd Tanous                                     {
2091abe55efSEd Tanous                                         BMCWEB_LOG_ERROR
2106c34de48SEd Tanous                                             << "DBUS response error " << ec;
211f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
212c5b2abe0SLewanczyk, Dawid                                         return;
213c5b2abe0SLewanczyk, Dawid                                     }
2146c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
2156c34de48SEd Tanous                                                      << properties.size()
216c5b2abe0SLewanczyk, Dawid                                                      << " Dimm properties.";
2179d3ae10eSAlpana Kumari 
2189d3ae10eSAlpana Kumari                                     if (properties.size() > 0)
2199d3ae10eSAlpana Kumari                                     {
22004a258f4SEd Tanous                                         for (const std::pair<std::string,
22104a258f4SEd Tanous                                                              VariantType>
22204a258f4SEd Tanous                                                  &property : properties)
2231abe55efSEd Tanous                                         {
2245fd7ba65SCheng C Yang                                             if (property.first !=
2255fd7ba65SCheng C Yang                                                 "MemorySizeInKB")
2261abe55efSEd Tanous                                             {
2275fd7ba65SCheng C Yang                                                 continue;
2285fd7ba65SCheng C Yang                                             }
2295fd7ba65SCheng C Yang                                             const uint32_t *value =
2308d78b7a9SPatrick Williams                                                 std::get_if<uint32_t>(
2311b6b96c5SEd Tanous                                                     &property.second);
2325fd7ba65SCheng C Yang                                             if (value == nullptr)
2331abe55efSEd Tanous                                             {
2345fd7ba65SCheng C Yang                                                 BMCWEB_LOG_DEBUG
2355fd7ba65SCheng C Yang                                                     << "Find incorrect type of "
2365fd7ba65SCheng C Yang                                                        "MemorySize";
2375fd7ba65SCheng C Yang                                                 continue;
2385fd7ba65SCheng C Yang                                             }
2395fd7ba65SCheng C Yang                                             nlohmann::json &totalMemory =
2405fd7ba65SCheng C Yang                                                 aResp->res
2415fd7ba65SCheng C Yang                                                     .jsonValue["MemorySummar"
2425fd7ba65SCheng C Yang                                                                "y"]
2435fd7ba65SCheng C Yang                                                               ["TotalSystemMe"
2445fd7ba65SCheng C Yang                                                                "moryGiB"];
2455fd7ba65SCheng C Yang                                             uint64_t *preValue =
2465fd7ba65SCheng C Yang                                                 totalMemory
2475fd7ba65SCheng C Yang                                                     .get_ptr<uint64_t *>();
2485fd7ba65SCheng C Yang                                             if (preValue == nullptr)
2495fd7ba65SCheng C Yang                                             {
2505fd7ba65SCheng C Yang                                                 continue;
2515fd7ba65SCheng C Yang                                             }
2525fd7ba65SCheng C Yang                                             aResp->res
2535fd7ba65SCheng C Yang                                                 .jsonValue["MemorySummary"]
2546c34de48SEd Tanous                                                           ["TotalSystemMemoryGi"
2555fd7ba65SCheng C Yang                                                            "B"] =
2565fd7ba65SCheng C Yang                                                 *value / (1024 * 1024) +
2575fd7ba65SCheng C Yang                                                 *preValue;
2585fd7ba65SCheng C Yang                                             aResp->res
2595fd7ba65SCheng C Yang                                                 .jsonValue["MemorySummary"]
2609d3ae10eSAlpana Kumari                                                           ["Status"]["State"] =
2611abe55efSEd Tanous                                                 "Enabled";
262c5b2abe0SLewanczyk, Dawid                                         }
263c5b2abe0SLewanczyk, Dawid                                     }
2649d3ae10eSAlpana Kumari                                     else
2659d3ae10eSAlpana Kumari                                     {
2669d3ae10eSAlpana Kumari                                         auto getDimmProperties =
2679d3ae10eSAlpana Kumari                                             [aResp](
2689d3ae10eSAlpana Kumari                                                 const boost::system::error_code
2699d3ae10eSAlpana Kumari                                                     ec,
2709d3ae10eSAlpana Kumari                                                 const std::variant<bool>
2719d3ae10eSAlpana Kumari                                                     &dimmState) {
2729d3ae10eSAlpana Kumari                                                 if (ec)
2739d3ae10eSAlpana Kumari                                                 {
2749d3ae10eSAlpana Kumari                                                     BMCWEB_LOG_ERROR
2759d3ae10eSAlpana Kumari                                                         << "DBUS response "
2769d3ae10eSAlpana Kumari                                                            "error "
2779d3ae10eSAlpana Kumari                                                         << ec;
2789d3ae10eSAlpana Kumari                                                     return;
2799d3ae10eSAlpana Kumari                                                 }
2809d3ae10eSAlpana Kumari                                                 updateDimmProperties(aResp,
2819d3ae10eSAlpana Kumari                                                                      dimmState);
2829d3ae10eSAlpana Kumari                                             };
2839d3ae10eSAlpana Kumari                                         crow::connections::systemBus
2849d3ae10eSAlpana Kumari                                             ->async_method_call(
2859d3ae10eSAlpana Kumari                                                 std::move(getDimmProperties),
2869d3ae10eSAlpana Kumari                                                 service, path,
2879d3ae10eSAlpana Kumari                                                 "org.freedesktop.DBus."
2889d3ae10eSAlpana Kumari                                                 "Properties",
2899d3ae10eSAlpana Kumari                                                 "Get",
2909d3ae10eSAlpana Kumari                                                 "xyz.openbmc_project.State."
2919d3ae10eSAlpana Kumari                                                 "Decorator.OperationalStatus",
2929d3ae10eSAlpana Kumari                                                 "Functional");
2939d3ae10eSAlpana Kumari                                     }
294c5b2abe0SLewanczyk, Dawid                                 },
29504a258f4SEd Tanous                                 connection.first, path,
2966c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
2976c34de48SEd Tanous                                 "xyz.openbmc_project.Inventory.Item.Dimm");
2985bc2dc8eSJames Feist 
2995bc2dc8eSJames Feist                             memoryHealth->inventory.emplace_back(path);
3001abe55efSEd Tanous                         }
30104a258f4SEd Tanous                         else if (interfaceName ==
30204a258f4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.Cpu")
3031abe55efSEd Tanous                         {
3041abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
30504a258f4SEd Tanous                                 << "Found Cpu, now get its properties.";
30657e8c9beSAlpana Kumari 
307a0803efaSEd Tanous                             crow::connections::systemBus->async_method_call(
30857e8c9beSAlpana Kumari                                 [aResp, service{connection.first},
30957e8c9beSAlpana Kumari                                  path(std::move(path))](
31057e8c9beSAlpana Kumari                                     const boost::system::error_code ec,
3116c34de48SEd Tanous                                     const std::vector<
3126c34de48SEd Tanous                                         std::pair<std::string, VariantType>>
3131abe55efSEd Tanous                                         &properties) {
3141abe55efSEd Tanous                                     if (ec)
3151abe55efSEd Tanous                                     {
3161abe55efSEd Tanous                                         BMCWEB_LOG_ERROR
3176c34de48SEd Tanous                                             << "DBUS response error " << ec;
318f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
319c5b2abe0SLewanczyk, Dawid                                         return;
320c5b2abe0SLewanczyk, Dawid                                     }
3216c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
3226c34de48SEd Tanous                                                      << properties.size()
323c5b2abe0SLewanczyk, Dawid                                                      << " Cpu properties.";
32457e8c9beSAlpana Kumari 
32557e8c9beSAlpana Kumari                                     if (properties.size() > 0)
32657e8c9beSAlpana Kumari                                     {
32704a258f4SEd Tanous                                         for (const auto &property : properties)
3281abe55efSEd Tanous                                         {
32957e8c9beSAlpana Kumari                                             if (property.first ==
33057e8c9beSAlpana Kumari                                                 "ProcessorFamily")
3311abe55efSEd Tanous                                             {
332a0803efaSEd Tanous                                                 const std::string *value =
3338d78b7a9SPatrick Williams                                                     std::get_if<std::string>(
3341b6b96c5SEd Tanous                                                         &property.second);
3351abe55efSEd Tanous                                                 if (value != nullptr)
3361abe55efSEd Tanous                                                 {
33757e8c9beSAlpana Kumari                                                     nlohmann::json
33857e8c9beSAlpana Kumari                                                         &procSummary =
3391abe55efSEd Tanous                                                             aResp->res.jsonValue
3406c34de48SEd Tanous                                                                 ["ProcessorSumm"
34104a258f4SEd Tanous                                                                  "ary"];
34204a258f4SEd Tanous                                                     nlohmann::json &procCount =
34304a258f4SEd Tanous                                                         procSummary["Count"];
344b4b9595aSJames Feist 
345b4b9595aSJames Feist                                                     auto procCountPtr =
346b4b9595aSJames Feist                                                         procCount.get_ptr<
347b4b9595aSJames Feist                                                             nlohmann::json::
348b4b9595aSJames Feist                                                                 number_integer_t
349b4b9595aSJames Feist                                                                     *>();
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,
37157e8c9beSAlpana Kumari                                                 const std::variant<bool>
37257e8c9beSAlpana Kumari                                                     &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,
38957e8c9beSAlpana Kumari                                                 const std::variant<bool>
39057e8c9beSAlpana Kumari                                                     &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(
446029573d4SEd Tanous                                 [aResp](const boost::system::error_code ec,
4476c34de48SEd Tanous                                         const std::vector<
4486c34de48SEd Tanous                                             std::pair<std::string, VariantType>>
4491abe55efSEd Tanous                                             &properties) {
4501abe55efSEd Tanous                                     if (ec)
4511abe55efSEd Tanous                                     {
4521abe55efSEd Tanous                                         BMCWEB_LOG_DEBUG
4536c34de48SEd Tanous                                             << "DBUS response error " << ec;
454f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
455c5b2abe0SLewanczyk, Dawid                                         return;
456c5b2abe0SLewanczyk, Dawid                                     }
4576c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
4586c34de48SEd Tanous                                                      << properties.size()
459c5b2abe0SLewanczyk, Dawid                                                      << " UUID properties.";
4601abe55efSEd Tanous                                     for (const std::pair<std::string,
46104a258f4SEd Tanous                                                          VariantType>
46204a258f4SEd Tanous                                              &property : properties)
4631abe55efSEd Tanous                                     {
46404a258f4SEd Tanous                                         if (property.first == "UUID")
4651abe55efSEd Tanous                                         {
466c5b2abe0SLewanczyk, Dawid                                             const std::string *value =
4678d78b7a9SPatrick Williams                                                 std::get_if<std::string>(
4681b6b96c5SEd Tanous                                                     &property.second);
46904a258f4SEd Tanous 
4701abe55efSEd Tanous                                             if (value != nullptr)
4711abe55efSEd Tanous                                             {
472029573d4SEd Tanous                                                 std::string valueStr = *value;
47304a258f4SEd Tanous                                                 if (valueStr.size() == 32)
4741abe55efSEd Tanous                                                 {
475029573d4SEd Tanous                                                     valueStr.insert(8, 1, '-');
476029573d4SEd Tanous                                                     valueStr.insert(13, 1, '-');
477029573d4SEd Tanous                                                     valueStr.insert(18, 1, '-');
478029573d4SEd Tanous                                                     valueStr.insert(23, 1, '-');
47904a258f4SEd Tanous                                                 }
480029573d4SEd Tanous                                                 BMCWEB_LOG_DEBUG << "UUID = "
48104a258f4SEd Tanous                                                                  << valueStr;
482029573d4SEd Tanous                                                 aResp->res.jsonValue["UUID"] =
48304a258f4SEd Tanous                                                     valueStr;
484c5b2abe0SLewanczyk, Dawid                                             }
485c5b2abe0SLewanczyk, Dawid                                         }
486c5b2abe0SLewanczyk, Dawid                                     }
487c5b2abe0SLewanczyk, Dawid                                 },
48804a258f4SEd Tanous                                 connection.first, path,
4896c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
4901abe55efSEd Tanous                                 "xyz.openbmc_project.Common.UUID");
491c5b2abe0SLewanczyk, Dawid                         }
492029573d4SEd Tanous                         else if (interfaceName ==
493029573d4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.System")
4941abe55efSEd Tanous                         {
495029573d4SEd Tanous                             crow::connections::systemBus->async_method_call(
496029573d4SEd Tanous                                 [aResp](const boost::system::error_code ec,
497029573d4SEd Tanous                                         const std::vector<
498029573d4SEd Tanous                                             std::pair<std::string, VariantType>>
499029573d4SEd Tanous                                             &propertiesList) {
500029573d4SEd Tanous                                     if (ec)
501029573d4SEd Tanous                                     {
502e4a4b9a9SJames Feist                                         // doesn't have to include this
503e4a4b9a9SJames Feist                                         // interface
504029573d4SEd Tanous                                         return;
505029573d4SEd Tanous                                     }
506698654b6SGunnar Mills                                     BMCWEB_LOG_DEBUG
507698654b6SGunnar Mills                                         << "Got " << propertiesList.size()
508029573d4SEd Tanous                                         << " properties for system";
509029573d4SEd Tanous                                     for (const std::pair<std::string,
510029573d4SEd Tanous                                                          VariantType>
511029573d4SEd Tanous                                              &property : propertiesList)
512029573d4SEd Tanous                                     {
513fc5afcf9Sbeccabroek                                         const std::string &propertyName =
514fc5afcf9Sbeccabroek                                             property.first;
515fc5afcf9Sbeccabroek                                         if ((propertyName == "PartNumber") ||
516fc5afcf9Sbeccabroek                                             (propertyName == "SerialNumber") ||
517fc5afcf9Sbeccabroek                                             (propertyName == "Manufacturer") ||
518fc5afcf9Sbeccabroek                                             (propertyName == "Model"))
519fc5afcf9Sbeccabroek                                         {
520029573d4SEd Tanous                                             const std::string *value =
521fc5afcf9Sbeccabroek                                                 std::get_if<std::string>(
522029573d4SEd Tanous                                                     &property.second);
523029573d4SEd Tanous                                             if (value != nullptr)
524029573d4SEd Tanous                                             {
525029573d4SEd Tanous                                                 aResp->res
526fc5afcf9Sbeccabroek                                                     .jsonValue[propertyName] =
527029573d4SEd Tanous                                                     *value;
528029573d4SEd Tanous                                             }
529029573d4SEd Tanous                                         }
530fc5afcf9Sbeccabroek                                     }
531c1e236a6SGunnar Mills 
532cb7e1e7bSAndrew Geissler                                     // Grab the bios version
533cb7e1e7bSAndrew Geissler                                     fw_util::getActiveFwVersion(
534cb7e1e7bSAndrew Geissler                                         aResp, fw_util::biosPurpose,
535cb7e1e7bSAndrew Geissler                                         "BiosVersion");
536029573d4SEd Tanous                                 },
537029573d4SEd Tanous                                 connection.first, path,
538029573d4SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
539029573d4SEd Tanous                                 "xyz.openbmc_project.Inventory.Decorator."
540029573d4SEd Tanous                                 "Asset");
541e4a4b9a9SJames Feist 
542e4a4b9a9SJames Feist                             crow::connections::systemBus->async_method_call(
543e4a4b9a9SJames Feist                                 [aResp](
544e4a4b9a9SJames Feist                                     const boost::system::error_code ec,
545e4a4b9a9SJames Feist                                     const std::variant<std::string> &property) {
546e4a4b9a9SJames Feist                                     if (ec)
547e4a4b9a9SJames Feist                                     {
548e4a4b9a9SJames Feist                                         // doesn't have to include this
549e4a4b9a9SJames Feist                                         // interface
550e4a4b9a9SJames Feist                                         return;
551e4a4b9a9SJames Feist                                     }
552e4a4b9a9SJames Feist 
553e4a4b9a9SJames Feist                                     const std::string *value =
554e4a4b9a9SJames Feist                                         std::get_if<std::string>(&property);
555e4a4b9a9SJames Feist                                     if (value != nullptr)
556e4a4b9a9SJames Feist                                     {
557e4a4b9a9SJames Feist                                         aResp->res.jsonValue["AssetTag"] =
558e4a4b9a9SJames Feist                                             *value;
559e4a4b9a9SJames Feist                                     }
560e4a4b9a9SJames Feist                                 },
561e4a4b9a9SJames Feist                                 connection.first, path,
562e4a4b9a9SJames Feist                                 "org.freedesktop.DBus.Properties", "Get",
563e4a4b9a9SJames Feist                                 "xyz.openbmc_project.Inventory.Decorator."
564e4a4b9a9SJames Feist                                 "AssetTag",
565e4a4b9a9SJames Feist                                 "AssetTag");
566029573d4SEd Tanous                         }
567029573d4SEd Tanous                     }
568029573d4SEd Tanous                 }
569c5b2abe0SLewanczyk, Dawid             }
570c5b2abe0SLewanczyk, Dawid         },
571c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper",
572c5b2abe0SLewanczyk, Dawid         "/xyz/openbmc_project/object_mapper",
573c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
5746617338dSEd Tanous         "/xyz/openbmc_project/inventory", int32_t(0),
5756617338dSEd Tanous         std::array<const char *, 5>{
5766617338dSEd Tanous             "xyz.openbmc_project.Inventory.Decorator.Asset",
5776617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Cpu",
5786617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Dimm",
5796617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.System",
5806617338dSEd Tanous             "xyz.openbmc_project.Common.UUID",
5816617338dSEd Tanous         });
582c5b2abe0SLewanczyk, Dawid }
583c5b2abe0SLewanczyk, Dawid 
584c5b2abe0SLewanczyk, Dawid /**
585c5b2abe0SLewanczyk, Dawid  * @brief Retrieves host state properties over dbus
586c5b2abe0SLewanczyk, Dawid  *
587c5b2abe0SLewanczyk, Dawid  * @param[in] aResp     Shared pointer for completing asynchronous calls.
588c5b2abe0SLewanczyk, Dawid  *
589c5b2abe0SLewanczyk, Dawid  * @return None.
590c5b2abe0SLewanczyk, Dawid  */
591a0803efaSEd Tanous void getHostState(std::shared_ptr<AsyncResp> aResp)
5921abe55efSEd Tanous {
59355c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get host information.";
59455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
595c5d03ff4SJennifer Lee         [aResp](const boost::system::error_code ec,
596abf2add6SEd Tanous                 const std::variant<std::string> &hostState) {
5971abe55efSEd Tanous             if (ec)
5981abe55efSEd Tanous             {
59955c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
600f12894f8SJason M. Bills                 messages::internalError(aResp->res);
601c5b2abe0SLewanczyk, Dawid                 return;
602c5b2abe0SLewanczyk, Dawid             }
6036617338dSEd Tanous 
604abf2add6SEd Tanous             const std::string *s = std::get_if<std::string>(&hostState);
60555c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Host state: " << *s;
6066617338dSEd Tanous             if (s != nullptr)
6071abe55efSEd Tanous             {
608c5b2abe0SLewanczyk, Dawid                 // Verify Host State
60994732661SAndrew Geissler                 if (*s == "xyz.openbmc_project.State.Host.HostState.Running")
6101abe55efSEd Tanous                 {
61155c7b7a2SEd Tanous                     aResp->res.jsonValue["PowerState"] = "On";
6126617338dSEd Tanous                     aResp->res.jsonValue["Status"]["State"] = "Enabled";
6131abe55efSEd Tanous                 }
61483935af9SAndrew Geissler                 else if (*s == "xyz.openbmc_project.State.Host.HostState."
6158c888608SGunnar Mills                                "Quiesced")
6168c888608SGunnar Mills                 {
6178c888608SGunnar Mills                     aResp->res.jsonValue["PowerState"] = "On";
6188c888608SGunnar Mills                     aResp->res.jsonValue["Status"]["State"] = "Quiesced";
6198c888608SGunnar Mills                 }
6208c888608SGunnar Mills                 else if (*s == "xyz.openbmc_project.State.Host.HostState."
62183935af9SAndrew Geissler                                "DiagnosticMode")
62283935af9SAndrew Geissler                 {
62383935af9SAndrew Geissler                     aResp->res.jsonValue["PowerState"] = "On";
62483935af9SAndrew Geissler                     aResp->res.jsonValue["Status"]["State"] = "InTest";
62583935af9SAndrew Geissler                 }
6261abe55efSEd Tanous                 else
6271abe55efSEd Tanous                 {
62855c7b7a2SEd Tanous                     aResp->res.jsonValue["PowerState"] = "Off";
6296617338dSEd Tanous                     aResp->res.jsonValue["Status"]["State"] = "Disabled";
630c5b2abe0SLewanczyk, Dawid                 }
631c5b2abe0SLewanczyk, Dawid             }
632c5b2abe0SLewanczyk, Dawid         },
6336c34de48SEd Tanous         "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
6346617338dSEd Tanous         "org.freedesktop.DBus.Properties", "Get",
6356617338dSEd Tanous         "xyz.openbmc_project.State.Host", "CurrentHostState");
636c5b2abe0SLewanczyk, Dawid }
637c5b2abe0SLewanczyk, Dawid 
638c5b2abe0SLewanczyk, Dawid /**
639491d8ee7SSantosh Puranik  * @brief Traslates boot source DBUS property value to redfish.
640491d8ee7SSantosh Puranik  *
641491d8ee7SSantosh Puranik  * @param[in] dbusSource    The boot source in DBUS speak.
642491d8ee7SSantosh Puranik  *
643491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source in Redfish terms. If translation
644491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
645491d8ee7SSantosh Puranik  */
646491d8ee7SSantosh Puranik static std::string dbusToRfBootSource(const std::string &dbusSource)
647491d8ee7SSantosh Puranik {
648491d8ee7SSantosh Puranik     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
649491d8ee7SSantosh Puranik     {
650491d8ee7SSantosh Puranik         return "None";
651491d8ee7SSantosh Puranik     }
652491d8ee7SSantosh Puranik     else if (dbusSource ==
653491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.Disk")
654491d8ee7SSantosh Puranik     {
655491d8ee7SSantosh Puranik         return "Hdd";
656491d8ee7SSantosh Puranik     }
657491d8ee7SSantosh Puranik     else if (dbusSource ==
658a71dc0b7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia")
659491d8ee7SSantosh Puranik     {
660491d8ee7SSantosh Puranik         return "Cd";
661491d8ee7SSantosh Puranik     }
662491d8ee7SSantosh Puranik     else if (dbusSource ==
663491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.Network")
664491d8ee7SSantosh Puranik     {
665491d8ee7SSantosh Puranik         return "Pxe";
666491d8ee7SSantosh Puranik     }
6679f16b2c1SJennifer Lee     else if (dbusSource ==
668944ffaf9SJohnathan Mantey              "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia")
6699f16b2c1SJennifer Lee     {
6709f16b2c1SJennifer Lee         return "Usb";
6719f16b2c1SJennifer Lee     }
672491d8ee7SSantosh Puranik     else
673491d8ee7SSantosh Puranik     {
674491d8ee7SSantosh Puranik         return "";
675491d8ee7SSantosh Puranik     }
676491d8ee7SSantosh Puranik }
677491d8ee7SSantosh Puranik 
678491d8ee7SSantosh Puranik /**
679491d8ee7SSantosh Puranik  * @brief Traslates boot mode DBUS property value to redfish.
680491d8ee7SSantosh Puranik  *
681491d8ee7SSantosh Puranik  * @param[in] dbusMode    The boot mode in DBUS speak.
682491d8ee7SSantosh Puranik  *
683491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode in Redfish terms. If translation
684491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
685491d8ee7SSantosh Puranik  */
686491d8ee7SSantosh Puranik static std::string dbusToRfBootMode(const std::string &dbusMode)
687491d8ee7SSantosh Puranik {
688491d8ee7SSantosh Puranik     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
689491d8ee7SSantosh Puranik     {
690491d8ee7SSantosh Puranik         return "None";
691491d8ee7SSantosh Puranik     }
692491d8ee7SSantosh Puranik     else if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe")
693491d8ee7SSantosh Puranik     {
694491d8ee7SSantosh Puranik         return "Diags";
695491d8ee7SSantosh Puranik     }
696491d8ee7SSantosh Puranik     else if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup")
697491d8ee7SSantosh Puranik     {
698491d8ee7SSantosh Puranik         return "BiosSetup";
699491d8ee7SSantosh Puranik     }
700491d8ee7SSantosh Puranik     else
701491d8ee7SSantosh Puranik     {
702491d8ee7SSantosh Puranik         return "";
703491d8ee7SSantosh Puranik     }
704491d8ee7SSantosh Puranik }
705491d8ee7SSantosh Puranik 
706491d8ee7SSantosh Puranik /**
707944ffaf9SJohnathan Mantey  * @brief Traslates boot source from Redfish to the DBus boot paths.
708491d8ee7SSantosh Puranik  *
709491d8ee7SSantosh Puranik  * @param[in] rfSource    The boot source in Redfish.
710944ffaf9SJohnathan Mantey  * @param[out] bootSource The DBus source
711944ffaf9SJohnathan Mantey  * @param[out] bootMode   the DBus boot mode
712491d8ee7SSantosh Puranik  *
713944ffaf9SJohnathan Mantey  * @return Integer error code.
714491d8ee7SSantosh Puranik  */
715944ffaf9SJohnathan Mantey static int assignBootParameters(std::shared_ptr<AsyncResp> aResp,
716944ffaf9SJohnathan Mantey                                 const std::string &rfSource,
717944ffaf9SJohnathan Mantey                                 std::string &bootSource, std::string &bootMode)
718491d8ee7SSantosh Puranik {
719944ffaf9SJohnathan Mantey     // The caller has initialized the bootSource and bootMode to:
720944ffaf9SJohnathan Mantey     // bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
721944ffaf9SJohnathan Mantey     // bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
722944ffaf9SJohnathan Mantey     // Only modify the bootSource/bootMode variable needed to achieve the
723944ffaf9SJohnathan Mantey     // desired boot action.
724944ffaf9SJohnathan Mantey 
725491d8ee7SSantosh Puranik     if (rfSource == "None")
726491d8ee7SSantosh Puranik     {
727944ffaf9SJohnathan Mantey         return 0;
728491d8ee7SSantosh Puranik     }
729491d8ee7SSantosh Puranik     else if (rfSource == "Pxe")
730491d8ee7SSantosh Puranik     {
731944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Network";
732944ffaf9SJohnathan Mantey     }
733944ffaf9SJohnathan Mantey     else if (rfSource == "Hdd")
734944ffaf9SJohnathan Mantey     {
735944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Disk";
736944ffaf9SJohnathan Mantey     }
737944ffaf9SJohnathan Mantey     else if (rfSource == "Diags")
738944ffaf9SJohnathan Mantey     {
739944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe";
740944ffaf9SJohnathan Mantey     }
741944ffaf9SJohnathan Mantey     else if (rfSource == "Cd")
742944ffaf9SJohnathan Mantey     {
743944ffaf9SJohnathan Mantey         bootSource =
744944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia";
745944ffaf9SJohnathan Mantey     }
746944ffaf9SJohnathan Mantey     else if (rfSource == "BiosSetup")
747944ffaf9SJohnathan Mantey     {
748944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup";
749491d8ee7SSantosh Puranik     }
7509f16b2c1SJennifer Lee     else if (rfSource == "Usb")
7519f16b2c1SJennifer Lee     {
752944ffaf9SJohnathan Mantey         bootSource =
753944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia";
7549f16b2c1SJennifer Lee     }
755491d8ee7SSantosh Puranik     else
756491d8ee7SSantosh Puranik     {
757944ffaf9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "Invalid property value for "
758944ffaf9SJohnathan Mantey                             "BootSourceOverrideTarget: "
759944ffaf9SJohnathan Mantey                          << bootSource;
760944ffaf9SJohnathan Mantey         messages::propertyValueNotInList(aResp->res, rfSource,
761944ffaf9SJohnathan Mantey                                          "BootSourceTargetOverride");
762944ffaf9SJohnathan Mantey         return -1;
763491d8ee7SSantosh Puranik     }
764944ffaf9SJohnathan Mantey     return 0;
765491d8ee7SSantosh Puranik }
766491d8ee7SSantosh Puranik 
767491d8ee7SSantosh Puranik /**
768491d8ee7SSantosh Puranik  * @brief Retrieves boot mode over DBUS and fills out the response
769491d8ee7SSantosh Puranik  *
770491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
771491d8ee7SSantosh Puranik  * @param[in] bootDbusObj   The dbus object to query for boot properties.
772491d8ee7SSantosh Puranik  *
773491d8ee7SSantosh Puranik  * @return None.
774491d8ee7SSantosh Puranik  */
775491d8ee7SSantosh Puranik static void getBootMode(std::shared_ptr<AsyncResp> aResp,
776491d8ee7SSantosh Puranik                         std::string bootDbusObj)
777491d8ee7SSantosh Puranik {
778491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
779491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec,
780491d8ee7SSantosh Puranik                 const std::variant<std::string> &bootMode) {
781491d8ee7SSantosh Puranik             if (ec)
782491d8ee7SSantosh Puranik             {
783491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
784491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
785491d8ee7SSantosh Puranik                 return;
786491d8ee7SSantosh Puranik             }
787491d8ee7SSantosh Puranik 
788491d8ee7SSantosh Puranik             const std::string *bootModeStr =
789491d8ee7SSantosh Puranik                 std::get_if<std::string>(&bootMode);
790491d8ee7SSantosh Puranik 
791491d8ee7SSantosh Puranik             if (!bootModeStr)
792491d8ee7SSantosh Puranik             {
793491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
794491d8ee7SSantosh Puranik                 return;
795491d8ee7SSantosh Puranik             }
796491d8ee7SSantosh Puranik 
797491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot mode: " << *bootModeStr;
798491d8ee7SSantosh Puranik 
799491d8ee7SSantosh Puranik             // TODO (Santosh): Do we need to support override mode?
800491d8ee7SSantosh Puranik             aResp->res.jsonValue["Boot"]["BootSourceOverrideMode"] = "Legacy";
801491d8ee7SSantosh Puranik             aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget@Redfish."
802491d8ee7SSantosh Puranik                                          "AllowableValues"] = {
803944ffaf9SJohnathan Mantey                 "None", "Pxe", "Hdd", "Cd", "Diags", "BiosSetup", "Usb"};
804491d8ee7SSantosh Puranik 
805491d8ee7SSantosh Puranik             if (*bootModeStr !=
806491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
807491d8ee7SSantosh Puranik             {
808491d8ee7SSantosh Puranik                 auto rfMode = dbusToRfBootMode(*bootModeStr);
809491d8ee7SSantosh Puranik                 if (!rfMode.empty())
810491d8ee7SSantosh Puranik                 {
811491d8ee7SSantosh Puranik                     aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
812491d8ee7SSantosh Puranik                         rfMode;
813491d8ee7SSantosh Puranik                 }
814491d8ee7SSantosh Puranik             }
815491d8ee7SSantosh Puranik 
816491d8ee7SSantosh Puranik             // If the BootSourceOverrideTarget is still "None" at the end,
817491d8ee7SSantosh Puranik             // reset the BootSourceOverrideEnabled to indicate that
818491d8ee7SSantosh Puranik             // overrides are disabled
819491d8ee7SSantosh Puranik             if (aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] ==
820491d8ee7SSantosh Puranik                 "None")
821491d8ee7SSantosh Puranik             {
822491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
823491d8ee7SSantosh Puranik                     "Disabled";
824491d8ee7SSantosh Puranik             }
825491d8ee7SSantosh Puranik         },
826491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootDbusObj,
827491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
828491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode");
829491d8ee7SSantosh Puranik }
830491d8ee7SSantosh Puranik 
831491d8ee7SSantosh Puranik /**
832491d8ee7SSantosh Puranik  * @brief Retrieves boot source over DBUS
833491d8ee7SSantosh Puranik  *
834491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
835491d8ee7SSantosh Puranik  * @param[in] oneTimeEnable Boolean to indicate boot properties are one-time.
836491d8ee7SSantosh Puranik  *
837491d8ee7SSantosh Puranik  * @return None.
838491d8ee7SSantosh Puranik  */
839491d8ee7SSantosh Puranik static void getBootSource(std::shared_ptr<AsyncResp> aResp, bool oneTimeEnabled)
840491d8ee7SSantosh Puranik {
841491d8ee7SSantosh Puranik     std::string bootDbusObj =
842491d8ee7SSantosh Puranik         oneTimeEnabled ? "/xyz/openbmc_project/control/host0/boot/one_time"
843491d8ee7SSantosh Puranik                        : "/xyz/openbmc_project/control/host0/boot";
844491d8ee7SSantosh Puranik 
845491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Is one time: " << oneTimeEnabled;
846491d8ee7SSantosh Puranik     aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
847491d8ee7SSantosh Puranik         (oneTimeEnabled) ? "Once" : "Continuous";
848491d8ee7SSantosh Puranik 
849491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
850491d8ee7SSantosh Puranik         [aResp, bootDbusObj](const boost::system::error_code ec,
851491d8ee7SSantosh Puranik                              const std::variant<std::string> &bootSource) {
852491d8ee7SSantosh Puranik             if (ec)
853491d8ee7SSantosh Puranik             {
854491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
855491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
856491d8ee7SSantosh Puranik                 return;
857491d8ee7SSantosh Puranik             }
858491d8ee7SSantosh Puranik 
859491d8ee7SSantosh Puranik             const std::string *bootSourceStr =
860491d8ee7SSantosh Puranik                 std::get_if<std::string>(&bootSource);
861491d8ee7SSantosh Puranik 
862491d8ee7SSantosh Puranik             if (!bootSourceStr)
863491d8ee7SSantosh Puranik             {
864491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
865491d8ee7SSantosh Puranik                 return;
866491d8ee7SSantosh Puranik             }
867491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot source: " << *bootSourceStr;
868491d8ee7SSantosh Puranik 
869491d8ee7SSantosh Puranik             auto rfSource = dbusToRfBootSource(*bootSourceStr);
870491d8ee7SSantosh Puranik             if (!rfSource.empty())
871491d8ee7SSantosh Puranik             {
872491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
873491d8ee7SSantosh Puranik                     rfSource;
874491d8ee7SSantosh Puranik             }
875491d8ee7SSantosh Puranik         },
876491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootDbusObj,
877491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
878491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource");
879491d8ee7SSantosh Puranik     getBootMode(std::move(aResp), std::move(bootDbusObj));
880491d8ee7SSantosh Puranik }
881491d8ee7SSantosh Puranik 
882491d8ee7SSantosh Puranik /**
883491d8ee7SSantosh Puranik  * @brief Retrieves "One time" enabled setting over DBUS and calls function to
884491d8ee7SSantosh Puranik  * get boot source and boot mode.
885491d8ee7SSantosh Puranik  *
886491d8ee7SSantosh Puranik  * @param[in] aResp     Shared pointer for generating response message.
887491d8ee7SSantosh Puranik  *
888491d8ee7SSantosh Puranik  * @return None.
889491d8ee7SSantosh Puranik  */
890491d8ee7SSantosh Puranik static void getBootProperties(std::shared_ptr<AsyncResp> aResp)
891491d8ee7SSantosh Puranik {
892491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Get boot information.";
893491d8ee7SSantosh Puranik 
894491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
895c5d03ff4SJennifer Lee         [aResp](const boost::system::error_code ec,
89619bd78d9SPatrick Williams                 const std::variant<bool> &oneTime) {
897491d8ee7SSantosh Puranik             if (ec)
898491d8ee7SSantosh Puranik             {
899491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
9002a833c77SJames Feist                 // not an error, don't have to have the interface
901491d8ee7SSantosh Puranik                 return;
902491d8ee7SSantosh Puranik             }
903491d8ee7SSantosh Puranik 
904491d8ee7SSantosh Puranik             const bool *oneTimePtr = std::get_if<bool>(&oneTime);
905491d8ee7SSantosh Puranik 
906491d8ee7SSantosh Puranik             if (!oneTimePtr)
907491d8ee7SSantosh Puranik             {
908491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
909491d8ee7SSantosh Puranik                 return;
910491d8ee7SSantosh Puranik             }
911491d8ee7SSantosh Puranik             getBootSource(aResp, *oneTimePtr);
912491d8ee7SSantosh Puranik         },
913491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
914491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
915491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
916491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled");
917491d8ee7SSantosh Puranik }
918491d8ee7SSantosh Puranik 
919491d8ee7SSantosh Puranik /**
9206bd5a8d2SGunnar Mills  * @brief Retrieves Automatic Retry properties. Known on D-Bus as AutoReboot.
9216bd5a8d2SGunnar Mills  *
9226bd5a8d2SGunnar Mills  * @param[in] aResp     Shared pointer for generating response message.
9236bd5a8d2SGunnar Mills  *
9246bd5a8d2SGunnar Mills  * @return None.
9256bd5a8d2SGunnar Mills  */
9266bd5a8d2SGunnar Mills void getAutomaticRetry(std::shared_ptr<AsyncResp> aResp)
9276bd5a8d2SGunnar Mills {
9286bd5a8d2SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Automatic Retry policy";
9296bd5a8d2SGunnar Mills 
9306bd5a8d2SGunnar Mills     crow::connections::systemBus->async_method_call(
9316bd5a8d2SGunnar Mills         [aResp](const boost::system::error_code ec,
9326bd5a8d2SGunnar Mills                 std::variant<bool> &autoRebootEnabled) {
9336bd5a8d2SGunnar Mills             if (ec)
9346bd5a8d2SGunnar Mills             {
9356bd5a8d2SGunnar Mills                 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
9366bd5a8d2SGunnar Mills                 return;
9376bd5a8d2SGunnar Mills             }
9386bd5a8d2SGunnar Mills 
9396bd5a8d2SGunnar Mills             const bool *autoRebootEnabledPtr =
9406bd5a8d2SGunnar Mills                 std::get_if<bool>(&autoRebootEnabled);
9416bd5a8d2SGunnar Mills 
9426bd5a8d2SGunnar Mills             if (!autoRebootEnabledPtr)
9436bd5a8d2SGunnar Mills             {
9446bd5a8d2SGunnar Mills                 messages::internalError(aResp->res);
9456bd5a8d2SGunnar Mills                 return;
9466bd5a8d2SGunnar Mills             }
9476bd5a8d2SGunnar Mills 
9486bd5a8d2SGunnar Mills             BMCWEB_LOG_DEBUG << "Auto Reboot: " << *autoRebootEnabledPtr;
9496bd5a8d2SGunnar Mills             if (*autoRebootEnabledPtr == true)
9506bd5a8d2SGunnar Mills             {
9516bd5a8d2SGunnar Mills                 aResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
9526bd5a8d2SGunnar Mills                     "RetryAttempts";
9536bd5a8d2SGunnar Mills                 // If AutomaticRetry (AutoReboot) is enabled see how many
9546bd5a8d2SGunnar Mills                 // attempts are left
9556bd5a8d2SGunnar Mills                 crow::connections::systemBus->async_method_call(
9566bd5a8d2SGunnar Mills                     [aResp](const boost::system::error_code ec,
9576bd5a8d2SGunnar Mills                             std::variant<uint32_t> &autoRebootAttemptsLeft) {
9586bd5a8d2SGunnar Mills                         if (ec)
9596bd5a8d2SGunnar Mills                         {
9606bd5a8d2SGunnar Mills                             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
9616bd5a8d2SGunnar Mills                             return;
9626bd5a8d2SGunnar Mills                         }
9636bd5a8d2SGunnar Mills 
9646bd5a8d2SGunnar Mills                         const uint32_t *autoRebootAttemptsLeftPtr =
9656bd5a8d2SGunnar Mills                             std::get_if<uint32_t>(&autoRebootAttemptsLeft);
9666bd5a8d2SGunnar Mills 
9676bd5a8d2SGunnar Mills                         if (!autoRebootAttemptsLeftPtr)
9686bd5a8d2SGunnar Mills                         {
9696bd5a8d2SGunnar Mills                             messages::internalError(aResp->res);
9706bd5a8d2SGunnar Mills                             return;
9716bd5a8d2SGunnar Mills                         }
9726bd5a8d2SGunnar Mills 
9736bd5a8d2SGunnar Mills                         BMCWEB_LOG_DEBUG << "Auto Reboot Attempts Left: "
9746bd5a8d2SGunnar Mills                                          << *autoRebootAttemptsLeftPtr;
9756bd5a8d2SGunnar Mills 
9766bd5a8d2SGunnar Mills                         aResp->res
9776bd5a8d2SGunnar Mills                             .jsonValue["Boot"]
9786bd5a8d2SGunnar Mills                                       ["RemainingAutomaticRetryAttempts"] =
9796bd5a8d2SGunnar Mills                             *autoRebootAttemptsLeftPtr;
9806bd5a8d2SGunnar Mills                     },
9816bd5a8d2SGunnar Mills                     "xyz.openbmc_project.State.Host",
9826bd5a8d2SGunnar Mills                     "/xyz/openbmc_project/state/host0",
9836bd5a8d2SGunnar Mills                     "org.freedesktop.DBus.Properties", "Get",
9846bd5a8d2SGunnar Mills                     "xyz.openbmc_project.Control.Boot.RebootAttempts",
9856bd5a8d2SGunnar Mills                     "AttemptsLeft");
9866bd5a8d2SGunnar Mills             }
9876bd5a8d2SGunnar Mills             else
9886bd5a8d2SGunnar Mills             {
9896bd5a8d2SGunnar Mills                 aResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
9906bd5a8d2SGunnar Mills                     "Disabled";
9916bd5a8d2SGunnar Mills             }
9926bd5a8d2SGunnar Mills 
9936bd5a8d2SGunnar Mills             // Not on D-Bus. Hardcoded here:
9946bd5a8d2SGunnar Mills             // https://github.com/openbmc/phosphor-state-manager/blob/1dbbef42675e94fb1f78edb87d6b11380260535a/meson_options.txt#L71
9956bd5a8d2SGunnar Mills             aResp->res.jsonValue["Boot"]["AutomaticRetryAttempts"] = 3;
996*69f35306SGunnar Mills 
997*69f35306SGunnar Mills             // "AutomaticRetryConfig" can be 3 values, Disabled, RetryAlways,
998*69f35306SGunnar Mills             // and RetryAttempts. OpenBMC only supports Disabled and
999*69f35306SGunnar Mills             // RetryAttempts.
1000*69f35306SGunnar Mills             aResp->res.jsonValue["Boot"]["AutomaticRetryConfig@Redfish."
1001*69f35306SGunnar Mills                                          "AllowableValues"] = {"Disabled",
1002*69f35306SGunnar Mills                                                                "RetryAttempts"};
10036bd5a8d2SGunnar Mills         },
10046bd5a8d2SGunnar Mills         "xyz.openbmc_project.Settings",
10056bd5a8d2SGunnar Mills         "/xyz/openbmc_project/control/host0/auto_reboot",
10066bd5a8d2SGunnar Mills         "org.freedesktop.DBus.Properties", "Get",
10076bd5a8d2SGunnar Mills         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot");
10086bd5a8d2SGunnar Mills }
10096bd5a8d2SGunnar Mills 
10106bd5a8d2SGunnar Mills /**
1011c6a620f2SGeorge Liu  * @brief Retrieves power restore policy over DBUS.
1012c6a620f2SGeorge Liu  *
1013c6a620f2SGeorge Liu  * @param[in] aResp     Shared pointer for generating response message.
1014c6a620f2SGeorge Liu  *
1015c6a620f2SGeorge Liu  * @return None.
1016c6a620f2SGeorge Liu  */
1017c6a620f2SGeorge Liu void getPowerRestorePolicy(std::shared_ptr<AsyncResp> aResp)
1018c6a620f2SGeorge Liu {
1019c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Get power restore policy";
1020c6a620f2SGeorge Liu 
1021c6a620f2SGeorge Liu     crow::connections::systemBus->async_method_call(
1022c6a620f2SGeorge Liu         [aResp](const boost::system::error_code ec,
102319bd78d9SPatrick Williams                 std::variant<std::string> &policy) {
1024c6a620f2SGeorge Liu             if (ec)
1025c6a620f2SGeorge Liu             {
1026c6a620f2SGeorge Liu                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1027c6a620f2SGeorge Liu                 return;
1028c6a620f2SGeorge Liu             }
1029c6a620f2SGeorge Liu 
1030c6a620f2SGeorge Liu             const boost::container::flat_map<std::string, std::string>
1031c6a620f2SGeorge Liu                 policyMaps = {
1032c6a620f2SGeorge Liu                     {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1033c6a620f2SGeorge Liu                      "AlwaysOn",
1034c6a620f2SGeorge Liu                      "AlwaysOn"},
1035c6a620f2SGeorge Liu                     {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1036c6a620f2SGeorge Liu                      "AlwaysOff",
1037c6a620f2SGeorge Liu                      "AlwaysOff"},
1038c6a620f2SGeorge Liu                     {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1039c6a620f2SGeorge Liu                      "LastState",
1040c6a620f2SGeorge Liu                      "LastState"}};
1041c6a620f2SGeorge Liu 
1042c6a620f2SGeorge Liu             const std::string *policyPtr = std::get_if<std::string>(&policy);
1043c6a620f2SGeorge Liu 
1044c6a620f2SGeorge Liu             if (!policyPtr)
1045c6a620f2SGeorge Liu             {
1046c6a620f2SGeorge Liu                 messages::internalError(aResp->res);
1047c6a620f2SGeorge Liu                 return;
1048c6a620f2SGeorge Liu             }
1049c6a620f2SGeorge Liu 
1050c6a620f2SGeorge Liu             auto policyMapsIt = policyMaps.find(*policyPtr);
1051c6a620f2SGeorge Liu             if (policyMapsIt == policyMaps.end())
1052c6a620f2SGeorge Liu             {
1053c6a620f2SGeorge Liu                 messages::internalError(aResp->res);
1054c6a620f2SGeorge Liu                 return;
1055c6a620f2SGeorge Liu             }
1056c6a620f2SGeorge Liu 
1057c6a620f2SGeorge Liu             aResp->res.jsonValue["PowerRestorePolicy"] = policyMapsIt->second;
1058c6a620f2SGeorge Liu         },
1059c6a620f2SGeorge Liu         "xyz.openbmc_project.Settings",
1060c6a620f2SGeorge Liu         "/xyz/openbmc_project/control/host0/power_restore_policy",
1061c6a620f2SGeorge Liu         "org.freedesktop.DBus.Properties", "Get",
1062c6a620f2SGeorge Liu         "xyz.openbmc_project.Control.Power.RestorePolicy",
1063c6a620f2SGeorge Liu         "PowerRestorePolicy");
1064c6a620f2SGeorge Liu }
1065c6a620f2SGeorge Liu 
1066c6a620f2SGeorge Liu /**
1067491d8ee7SSantosh Puranik  * @brief Sets boot properties into DBUS object(s).
1068491d8ee7SSantosh Puranik  *
1069491d8ee7SSantosh Puranik  * @param[in] aResp           Shared pointer for generating response message.
1070491d8ee7SSantosh Puranik  * @param[in] oneTimeEnabled  Is "one-time" setting already enabled.
1071491d8ee7SSantosh Puranik  * @param[in] bootSource      The boot source to set.
1072491d8ee7SSantosh Puranik  * @param[in] bootEnable      The source override "enable" to set.
1073491d8ee7SSantosh Puranik  *
1074265c1602SJohnathan Mantey  * @return Integer error code.
1075491d8ee7SSantosh Puranik  */
1076491d8ee7SSantosh Puranik static void setBootModeOrSource(std::shared_ptr<AsyncResp> aResp,
1077491d8ee7SSantosh Puranik                                 bool oneTimeEnabled,
1078491d8ee7SSantosh Puranik                                 std::optional<std::string> bootSource,
1079491d8ee7SSantosh Puranik                                 std::optional<std::string> bootEnable)
1080491d8ee7SSantosh Puranik {
1081944ffaf9SJohnathan Mantey     std::string bootSourceStr =
1082944ffaf9SJohnathan Mantey         "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
1083944ffaf9SJohnathan Mantey     std::string bootModeStr =
1084944ffaf9SJohnathan Mantey         "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
1085491d8ee7SSantosh Puranik     bool oneTimeSetting = oneTimeEnabled;
1086944ffaf9SJohnathan Mantey     bool useBootSource = true;
1087944ffaf9SJohnathan Mantey 
1088491d8ee7SSantosh Puranik     // Validate incoming parameters
1089491d8ee7SSantosh Puranik     if (bootEnable)
1090491d8ee7SSantosh Puranik     {
1091491d8ee7SSantosh Puranik         if (*bootEnable == "Once")
1092491d8ee7SSantosh Puranik         {
1093491d8ee7SSantosh Puranik             oneTimeSetting = true;
1094491d8ee7SSantosh Puranik         }
1095491d8ee7SSantosh Puranik         else if (*bootEnable == "Continuous")
1096491d8ee7SSantosh Puranik         {
1097491d8ee7SSantosh Puranik             oneTimeSetting = false;
1098491d8ee7SSantosh Puranik         }
1099491d8ee7SSantosh Puranik         else if (*bootEnable == "Disabled")
1100491d8ee7SSantosh Puranik         {
1101944ffaf9SJohnathan Mantey             BMCWEB_LOG_DEBUG << "Boot source override will be disabled";
1102491d8ee7SSantosh Puranik             oneTimeSetting = false;
1103944ffaf9SJohnathan Mantey             useBootSource = false;
1104491d8ee7SSantosh Puranik         }
1105491d8ee7SSantosh Puranik         else
1106491d8ee7SSantosh Puranik         {
1107491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Unsupported value for "
1108491d8ee7SSantosh Puranik                                 "BootSourceOverrideEnabled: "
1109491d8ee7SSantosh Puranik                              << *bootEnable;
1110491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootEnable,
1111491d8ee7SSantosh Puranik                                              "BootSourceOverrideEnabled");
1112491d8ee7SSantosh Puranik             return;
1113491d8ee7SSantosh Puranik         }
1114491d8ee7SSantosh Puranik     }
1115491d8ee7SSantosh Puranik 
1116944ffaf9SJohnathan Mantey     if (bootSource && useBootSource)
1117491d8ee7SSantosh Puranik     {
1118491d8ee7SSantosh Puranik         // Source target specified
1119491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot source: " << *bootSource;
1120491d8ee7SSantosh Puranik         // Figure out which DBUS interface and property to use
1121944ffaf9SJohnathan Mantey         if (assignBootParameters(aResp, *bootSource, bootSourceStr,
1122944ffaf9SJohnathan Mantey                                  bootModeStr))
1123491d8ee7SSantosh Puranik         {
1124944ffaf9SJohnathan Mantey             BMCWEB_LOG_DEBUG
1125944ffaf9SJohnathan Mantey                 << "Invalid property value for BootSourceOverrideTarget: "
1126491d8ee7SSantosh Puranik                 << *bootSource;
1127491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootSource,
1128491d8ee7SSantosh Puranik                                              "BootSourceTargetOverride");
1129491d8ee7SSantosh Puranik             return;
1130491d8ee7SSantosh Puranik         }
1131944ffaf9SJohnathan Mantey     }
1132491d8ee7SSantosh Puranik 
1133944ffaf9SJohnathan Mantey     // Act on validated parameters
1134944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
1135944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
1136944ffaf9SJohnathan Mantey     const char *bootObj =
1137944ffaf9SJohnathan Mantey         oneTimeSetting ? "/xyz/openbmc_project/control/host0/boot/one_time"
1138944ffaf9SJohnathan Mantey                        : "/xyz/openbmc_project/control/host0/boot";
1139944ffaf9SJohnathan Mantey 
1140491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1141491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec) {
1142491d8ee7SSantosh Puranik             if (ec)
1143491d8ee7SSantosh Puranik             {
1144491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1145491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1146491d8ee7SSantosh Puranik                 return;
1147491d8ee7SSantosh Puranik             }
1148491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot source update done.";
1149491d8ee7SSantosh Puranik         },
1150491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootObj,
1151491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1152491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource",
1153491d8ee7SSantosh Puranik         std::variant<std::string>(bootSourceStr));
1154944ffaf9SJohnathan Mantey 
1155491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1156491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec) {
1157491d8ee7SSantosh Puranik             if (ec)
1158491d8ee7SSantosh Puranik             {
1159491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1160491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1161491d8ee7SSantosh Puranik                 return;
1162491d8ee7SSantosh Puranik             }
1163491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot mode update done.";
1164491d8ee7SSantosh Puranik         },
1165491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootObj,
1166491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1167491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
1168491d8ee7SSantosh Puranik         std::variant<std::string>(bootModeStr));
1169944ffaf9SJohnathan Mantey 
1170491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1171491d8ee7SSantosh Puranik         [aResp{std::move(aResp)}](const boost::system::error_code ec) {
1172491d8ee7SSantosh Puranik             if (ec)
1173491d8ee7SSantosh Puranik             {
1174491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1175491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1176491d8ee7SSantosh Puranik                 return;
1177491d8ee7SSantosh Puranik             }
1178491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot enable update done.";
1179491d8ee7SSantosh Puranik         },
1180491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
1181491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
1182491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1183491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled",
1184491d8ee7SSantosh Puranik         std::variant<bool>(oneTimeSetting));
1185491d8ee7SSantosh Puranik }
1186491d8ee7SSantosh Puranik 
1187491d8ee7SSantosh Puranik /**
1188491d8ee7SSantosh Puranik  * @brief Retrieves "One time" enabled setting over DBUS and calls function to
1189491d8ee7SSantosh Puranik  * set boot source/boot mode properties.
1190491d8ee7SSantosh Puranik  *
1191491d8ee7SSantosh Puranik  * @param[in] aResp      Shared pointer for generating response message.
1192491d8ee7SSantosh Puranik  * @param[in] bootSource The boot source from incoming RF request.
1193491d8ee7SSantosh Puranik  * @param[in] bootEnable The boot override enable from incoming RF request.
1194491d8ee7SSantosh Puranik  *
1195265c1602SJohnathan Mantey  * @return Integer error code.
1196491d8ee7SSantosh Puranik  */
1197*69f35306SGunnar Mills static void setBootSourceProperties(std::shared_ptr<AsyncResp> aResp,
1198491d8ee7SSantosh Puranik                                     std::optional<std::string> bootSource,
1199491d8ee7SSantosh Puranik                                     std::optional<std::string> bootEnable)
1200491d8ee7SSantosh Puranik {
1201491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set boot information.";
1202491d8ee7SSantosh Puranik 
1203491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1204265c1602SJohnathan Mantey         [aResp, bootSource{std::move(bootSource)},
120519bd78d9SPatrick Williams          bootEnable{std::move(bootEnable)}](const boost::system::error_code ec,
120619bd78d9SPatrick Williams                                             const std::variant<bool> &oneTime) {
1207491d8ee7SSantosh Puranik             if (ec)
1208491d8ee7SSantosh Puranik             {
1209491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1210491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1211491d8ee7SSantosh Puranik                 return;
1212491d8ee7SSantosh Puranik             }
1213491d8ee7SSantosh Puranik 
1214491d8ee7SSantosh Puranik             const bool *oneTimePtr = std::get_if<bool>(&oneTime);
1215491d8ee7SSantosh Puranik 
1216491d8ee7SSantosh Puranik             if (!oneTimePtr)
1217491d8ee7SSantosh Puranik             {
1218491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1219491d8ee7SSantosh Puranik                 return;
1220491d8ee7SSantosh Puranik             }
1221491d8ee7SSantosh Puranik 
1222491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Got one time: " << *oneTimePtr;
1223491d8ee7SSantosh Puranik 
1224491d8ee7SSantosh Puranik             setBootModeOrSource(aResp, *oneTimePtr, std::move(bootSource),
1225491d8ee7SSantosh Puranik                                 std::move(bootEnable));
1226491d8ee7SSantosh Puranik         },
1227491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
1228491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
1229491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
1230491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled");
1231491d8ee7SSantosh Puranik }
1232491d8ee7SSantosh Puranik 
1233c6a620f2SGeorge Liu /**
1234*69f35306SGunnar Mills  * @brief Sets automaticRetry (Auto Reboot)
1235*69f35306SGunnar Mills  *
1236*69f35306SGunnar Mills  * @param[in] aResp   Shared pointer for generating response message.
1237*69f35306SGunnar Mills  * @param[in] automaticRetryConfig  "AutomaticRetryConfig" from request.
1238*69f35306SGunnar Mills  *
1239*69f35306SGunnar Mills  * @return None.
1240*69f35306SGunnar Mills  */
1241*69f35306SGunnar Mills static void setAutomaticRetry(std::shared_ptr<AsyncResp> aResp,
1242*69f35306SGunnar Mills                               const std::string &&automaticRetryConfig)
1243*69f35306SGunnar Mills {
1244*69f35306SGunnar Mills     BMCWEB_LOG_DEBUG << "Set Automatic Retry.";
1245*69f35306SGunnar Mills 
1246*69f35306SGunnar Mills     // OpenBMC only supports "Disabled" and "RetryAttempts".
1247*69f35306SGunnar Mills     bool autoRebootEnabled;
1248*69f35306SGunnar Mills 
1249*69f35306SGunnar Mills     if (automaticRetryConfig == "Disabled")
1250*69f35306SGunnar Mills     {
1251*69f35306SGunnar Mills         autoRebootEnabled = false;
1252*69f35306SGunnar Mills     }
1253*69f35306SGunnar Mills     else if (automaticRetryConfig == "RetryAttempts")
1254*69f35306SGunnar Mills     {
1255*69f35306SGunnar Mills         autoRebootEnabled = true;
1256*69f35306SGunnar Mills     }
1257*69f35306SGunnar Mills     else
1258*69f35306SGunnar Mills     {
1259*69f35306SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid property value for "
1260*69f35306SGunnar Mills                             "AutomaticRetryConfig: "
1261*69f35306SGunnar Mills                          << automaticRetryConfig;
1262*69f35306SGunnar Mills         messages::propertyValueNotInList(aResp->res, automaticRetryConfig,
1263*69f35306SGunnar Mills                                          "AutomaticRetryConfig");
1264*69f35306SGunnar Mills         return;
1265*69f35306SGunnar Mills     }
1266*69f35306SGunnar Mills 
1267*69f35306SGunnar Mills     crow::connections::systemBus->async_method_call(
1268*69f35306SGunnar Mills         [aResp](const boost::system::error_code ec) {
1269*69f35306SGunnar Mills             if (ec)
1270*69f35306SGunnar Mills             {
1271*69f35306SGunnar Mills                 messages::internalError(aResp->res);
1272*69f35306SGunnar Mills                 return;
1273*69f35306SGunnar Mills             }
1274*69f35306SGunnar Mills         },
1275*69f35306SGunnar Mills         "xyz.openbmc_project.Settings",
1276*69f35306SGunnar Mills         "/xyz/openbmc_project/control/host0/auto_reboot",
1277*69f35306SGunnar Mills         "org.freedesktop.DBus.Properties", "Set",
1278*69f35306SGunnar Mills         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
1279*69f35306SGunnar Mills         std::variant<bool>(autoRebootEnabled));
1280*69f35306SGunnar Mills }
1281*69f35306SGunnar Mills 
1282*69f35306SGunnar Mills /**
1283c6a620f2SGeorge Liu  * @brief Sets power restore policy properties.
1284c6a620f2SGeorge Liu  *
1285c6a620f2SGeorge Liu  * @param[in] aResp   Shared pointer for generating response message.
1286c6a620f2SGeorge Liu  * @param[in] policy  power restore policy properties from request.
1287c6a620f2SGeorge Liu  *
1288c6a620f2SGeorge Liu  * @return None.
1289c6a620f2SGeorge Liu  */
1290c6a620f2SGeorge Liu static void setPowerRestorePolicy(std::shared_ptr<AsyncResp> aResp,
1291c6a620f2SGeorge Liu                                   std::optional<std::string> policy)
1292c6a620f2SGeorge Liu {
1293c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Set power restore policy.";
1294c6a620f2SGeorge Liu 
1295c6a620f2SGeorge Liu     const boost::container::flat_map<std::string, std::string> policyMaps = {
1296c6a620f2SGeorge Liu         {"AlwaysOn", "xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1297c6a620f2SGeorge Liu                      "AlwaysOn"},
1298c6a620f2SGeorge Liu         {"AlwaysOff", "xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1299c6a620f2SGeorge Liu                       "AlwaysOff"},
1300c6a620f2SGeorge Liu         {"LastState", "xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1301c6a620f2SGeorge Liu                       "LastState"}};
1302c6a620f2SGeorge Liu 
1303c6a620f2SGeorge Liu     std::string powerRestorPolicy;
1304c6a620f2SGeorge Liu 
1305c6a620f2SGeorge Liu     auto policyMapsIt = policyMaps.find(*policy);
1306c6a620f2SGeorge Liu     if (policyMapsIt == policyMaps.end())
1307c6a620f2SGeorge Liu     {
1308c6a620f2SGeorge Liu         messages::internalError(aResp->res);
1309c6a620f2SGeorge Liu         return;
1310c6a620f2SGeorge Liu     }
1311c6a620f2SGeorge Liu 
1312c6a620f2SGeorge Liu     powerRestorPolicy = policyMapsIt->second;
1313c6a620f2SGeorge Liu 
1314c6a620f2SGeorge Liu     crow::connections::systemBus->async_method_call(
1315c6a620f2SGeorge Liu         [aResp](const boost::system::error_code ec) {
1316c6a620f2SGeorge Liu             if (ec)
1317c6a620f2SGeorge Liu             {
1318c6a620f2SGeorge Liu                 messages::internalError(aResp->res);
1319c6a620f2SGeorge Liu                 return;
1320c6a620f2SGeorge Liu             }
1321c6a620f2SGeorge Liu         },
1322c6a620f2SGeorge Liu         "xyz.openbmc_project.Settings",
1323c6a620f2SGeorge Liu         "/xyz/openbmc_project/control/host0/power_restore_policy",
1324c6a620f2SGeorge Liu         "org.freedesktop.DBus.Properties", "Set",
1325c6a620f2SGeorge Liu         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
1326c6a620f2SGeorge Liu         std::variant<std::string>(powerRestorPolicy));
1327c6a620f2SGeorge Liu }
1328c6a620f2SGeorge Liu 
1329a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
1330a6349918SAppaRao Puli /**
1331a6349918SAppaRao Puli  * @brief Retrieves provisioning status
1332a6349918SAppaRao Puli  *
1333a6349918SAppaRao Puli  * @param[in] aResp     Shared pointer for completing asynchronous calls.
1334a6349918SAppaRao Puli  *
1335a6349918SAppaRao Puli  * @return None.
1336a6349918SAppaRao Puli  */
1337a6349918SAppaRao Puli void getProvisioningStatus(std::shared_ptr<AsyncResp> aResp)
1338a6349918SAppaRao Puli {
1339a6349918SAppaRao Puli     BMCWEB_LOG_DEBUG << "Get OEM information.";
1340a6349918SAppaRao Puli     crow::connections::systemBus->async_method_call(
1341a6349918SAppaRao Puli         [aResp](const boost::system::error_code ec,
1342a6349918SAppaRao Puli                 const std::vector<std::pair<std::string, VariantType>>
1343a6349918SAppaRao Puli                     &propertiesList) {
1344a6349918SAppaRao Puli             if (ec)
1345a6349918SAppaRao Puli             {
1346a6349918SAppaRao Puli                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1347a6349918SAppaRao Puli                 messages::internalError(aResp->res);
1348a6349918SAppaRao Puli                 return;
1349a6349918SAppaRao Puli             }
1350a6349918SAppaRao Puli 
1351a6349918SAppaRao Puli             const bool *provState = nullptr;
1352a6349918SAppaRao Puli             const bool *lockState = nullptr;
1353a6349918SAppaRao Puli             for (const std::pair<std::string, VariantType> &property :
1354a6349918SAppaRao Puli                  propertiesList)
1355a6349918SAppaRao Puli             {
1356a6349918SAppaRao Puli                 if (property.first == "UfmProvisioned")
1357a6349918SAppaRao Puli                 {
1358a6349918SAppaRao Puli                     provState = std::get_if<bool>(&property.second);
1359a6349918SAppaRao Puli                 }
1360a6349918SAppaRao Puli                 else if (property.first == "UfmLocked")
1361a6349918SAppaRao Puli                 {
1362a6349918SAppaRao Puli                     lockState = std::get_if<bool>(&property.second);
1363a6349918SAppaRao Puli                 }
1364a6349918SAppaRao Puli             }
1365a6349918SAppaRao Puli 
1366a6349918SAppaRao Puli             if ((provState == nullptr) || (lockState == nullptr))
1367a6349918SAppaRao Puli             {
1368a6349918SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Unable to get PFR attributes.";
1369a6349918SAppaRao Puli                 messages::internalError(aResp->res);
1370a6349918SAppaRao Puli                 return;
1371a6349918SAppaRao Puli             }
1372a6349918SAppaRao Puli 
1373a6349918SAppaRao Puli             nlohmann::json &oemPFR =
1374a6349918SAppaRao Puli                 aResp->res.jsonValue["Oem"]["OpenBmc"]["FirmwareProvisioning"];
1375a6349918SAppaRao Puli             if (*provState == true)
1376a6349918SAppaRao Puli             {
1377a6349918SAppaRao Puli                 if (*lockState == true)
1378a6349918SAppaRao Puli                 {
1379a6349918SAppaRao Puli                     oemPFR["ProvisioningStatus"] = "ProvisionedAndLocked";
1380a6349918SAppaRao Puli                 }
1381a6349918SAppaRao Puli                 else
1382a6349918SAppaRao Puli                 {
1383a6349918SAppaRao Puli                     oemPFR["ProvisioningStatus"] = "ProvisionedButNotLocked";
1384a6349918SAppaRao Puli                 }
1385a6349918SAppaRao Puli             }
1386a6349918SAppaRao Puli             else
1387a6349918SAppaRao Puli             {
1388a6349918SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "NotProvisioned";
1389a6349918SAppaRao Puli             }
1390a6349918SAppaRao Puli         },
1391a6349918SAppaRao Puli         "xyz.openbmc_project.PFR.Manager", "/xyz/openbmc_project/pfr",
1392a6349918SAppaRao Puli         "org.freedesktop.DBus.Properties", "GetAll",
1393a6349918SAppaRao Puli         "xyz.openbmc_project.PFR.Attributes");
1394a6349918SAppaRao Puli }
1395a6349918SAppaRao Puli #endif
1396a6349918SAppaRao Puli 
1397491d8ee7SSantosh Puranik /**
139851709ffdSYong Li  * @brief Translates watchdog timeout action DBUS property value to redfish.
139951709ffdSYong Li  *
140051709ffdSYong Li  * @param[in] dbusAction    The watchdog timeout action in D-BUS.
140151709ffdSYong Li  *
140251709ffdSYong Li  * @return Returns as a string, the timeout action in Redfish terms. If
140351709ffdSYong Li  * translation cannot be done, returns an empty string.
140451709ffdSYong Li  */
140551709ffdSYong Li static std::string dbusToRfWatchdogAction(const std::string &dbusAction)
140651709ffdSYong Li {
140751709ffdSYong Li     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.None")
140851709ffdSYong Li     {
140951709ffdSYong Li         return "None";
141051709ffdSYong Li     }
141151709ffdSYong Li     else if (dbusAction ==
141251709ffdSYong Li              "xyz.openbmc_project.State.Watchdog.Action.HardReset")
141351709ffdSYong Li     {
141451709ffdSYong Li         return "ResetSystem";
141551709ffdSYong Li     }
141651709ffdSYong Li     else if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerOff")
141751709ffdSYong Li     {
141851709ffdSYong Li         return "PowerDown";
141951709ffdSYong Li     }
142051709ffdSYong Li     else if (dbusAction ==
142151709ffdSYong Li              "xyz.openbmc_project.State.Watchdog.Action.PowerCycle")
142251709ffdSYong Li     {
142351709ffdSYong Li         return "PowerCycle";
142451709ffdSYong Li     }
142551709ffdSYong Li 
142651709ffdSYong Li     return "";
142751709ffdSYong Li }
142851709ffdSYong Li 
142951709ffdSYong Li /**
1430c45f0082SYong Li  *@brief Translates timeout action from Redfish to DBUS property value.
1431c45f0082SYong Li  *
1432c45f0082SYong Li  *@param[in] rfAction The timeout action in Redfish.
1433c45f0082SYong Li  *
1434c45f0082SYong Li  *@return Returns as a string, the time_out action as expected by DBUS.
1435c45f0082SYong Li  *If translation cannot be done, returns an empty string.
1436c45f0082SYong Li  */
1437c45f0082SYong Li 
1438c45f0082SYong Li static std::string rfToDbusWDTTimeOutAct(const std::string &rfAction)
1439c45f0082SYong Li {
1440c45f0082SYong Li     if (rfAction == "None")
1441c45f0082SYong Li     {
1442c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.None";
1443c45f0082SYong Li     }
1444c45f0082SYong Li     else if (rfAction == "PowerCycle")
1445c45f0082SYong Li     {
1446c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerCycle";
1447c45f0082SYong Li     }
1448c45f0082SYong Li     else if (rfAction == "PowerDown")
1449c45f0082SYong Li     {
1450c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerOff";
1451c45f0082SYong Li     }
1452c45f0082SYong Li     else if (rfAction == "ResetSystem")
1453c45f0082SYong Li     {
1454c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.HardReset";
1455c45f0082SYong Li     }
1456c45f0082SYong Li 
1457c45f0082SYong Li     return "";
1458c45f0082SYong Li }
1459c45f0082SYong Li 
1460c45f0082SYong Li /**
146151709ffdSYong Li  * @brief Retrieves host watchdog timer properties over DBUS
146251709ffdSYong Li  *
146351709ffdSYong Li  * @param[in] aResp     Shared pointer for completing asynchronous calls.
146451709ffdSYong Li  *
146551709ffdSYong Li  * @return None.
146651709ffdSYong Li  */
146751709ffdSYong Li void getHostWatchdogTimer(std::shared_ptr<AsyncResp> aResp)
146851709ffdSYong Li {
146951709ffdSYong Li     BMCWEB_LOG_DEBUG << "Get host watchodg";
147051709ffdSYong Li     crow::connections::systemBus->async_method_call(
147151709ffdSYong Li         [aResp](const boost::system::error_code ec,
147251709ffdSYong Li                 PropertiesType &properties) {
147351709ffdSYong Li             if (ec)
147451709ffdSYong Li             {
147551709ffdSYong Li                 // watchdog service is stopped
147651709ffdSYong Li                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
147751709ffdSYong Li                 return;
147851709ffdSYong Li             }
147951709ffdSYong Li 
148051709ffdSYong Li             BMCWEB_LOG_DEBUG << "Got " << properties.size() << " wdt prop.";
148151709ffdSYong Li 
148251709ffdSYong Li             nlohmann::json &hostWatchdogTimer =
148351709ffdSYong Li                 aResp->res.jsonValue["HostWatchdogTimer"];
148451709ffdSYong Li 
148551709ffdSYong Li             // watchdog service is running/enabled
148651709ffdSYong Li             hostWatchdogTimer["Status"]["State"] = "Enabled";
148751709ffdSYong Li 
148851709ffdSYong Li             for (const auto &property : properties)
148951709ffdSYong Li             {
149051709ffdSYong Li                 BMCWEB_LOG_DEBUG << "prop=" << property.first;
149151709ffdSYong Li                 if (property.first == "Enabled")
149251709ffdSYong Li                 {
149351709ffdSYong Li                     const bool *state = std::get_if<bool>(&property.second);
149451709ffdSYong Li 
149551709ffdSYong Li                     if (!state)
149651709ffdSYong Li                     {
149751709ffdSYong Li                         messages::internalError(aResp->res);
149851709ffdSYong Li                         continue;
149951709ffdSYong Li                     }
150051709ffdSYong Li 
150151709ffdSYong Li                     hostWatchdogTimer["FunctionEnabled"] = *state;
150251709ffdSYong Li                 }
150351709ffdSYong Li                 else if (property.first == "ExpireAction")
150451709ffdSYong Li                 {
150551709ffdSYong Li                     const std::string *s =
150651709ffdSYong Li                         std::get_if<std::string>(&property.second);
150751709ffdSYong Li                     if (!s)
150851709ffdSYong Li                     {
150951709ffdSYong Li                         messages::internalError(aResp->res);
151051709ffdSYong Li                         continue;
151151709ffdSYong Li                     }
151251709ffdSYong Li 
151351709ffdSYong Li                     std::string action = dbusToRfWatchdogAction(*s);
151451709ffdSYong Li                     if (action.empty())
151551709ffdSYong Li                     {
151651709ffdSYong Li                         messages::internalError(aResp->res);
151751709ffdSYong Li                         continue;
151851709ffdSYong Li                     }
151951709ffdSYong Li                     hostWatchdogTimer["TimeoutAction"] = action;
152051709ffdSYong Li                 }
152151709ffdSYong Li             }
152251709ffdSYong Li         },
152351709ffdSYong Li         "xyz.openbmc_project.Watchdog", "/xyz/openbmc_project/watchdog/host0",
152451709ffdSYong Li         "org.freedesktop.DBus.Properties", "GetAll",
152551709ffdSYong Li         "xyz.openbmc_project.State.Watchdog");
152651709ffdSYong Li }
152751709ffdSYong Li 
152851709ffdSYong Li /**
1529c45f0082SYong Li  * @brief Sets Host WatchDog Timer properties.
1530c45f0082SYong Li  *
1531c45f0082SYong Li  * @param[in] aResp      Shared pointer for generating response message.
1532c45f0082SYong Li  * @param[in] wdtEnable  The WDTimer Enable value (true/false) from incoming
1533c45f0082SYong Li  *                       RF request.
1534c45f0082SYong Li  * @param[in] wdtTimeOutAction The WDT Timeout action, from incoming RF request.
1535c45f0082SYong Li  *
1536c45f0082SYong Li  * @return None.
1537c45f0082SYong Li  */
1538c45f0082SYong Li static void setWDTProperties(std::shared_ptr<AsyncResp> aResp,
1539c45f0082SYong Li                              const std::optional<bool> wdtEnable,
1540c45f0082SYong Li                              const std::optional<std::string> &wdtTimeOutAction)
1541c45f0082SYong Li {
1542c45f0082SYong Li     BMCWEB_LOG_DEBUG << "Set host watchdog";
1543c45f0082SYong Li 
1544c45f0082SYong Li     if (wdtTimeOutAction)
1545c45f0082SYong Li     {
1546c45f0082SYong Li         std::string wdtTimeOutActStr = rfToDbusWDTTimeOutAct(*wdtTimeOutAction);
1547c45f0082SYong Li         // check if TimeOut Action is Valid
1548c45f0082SYong Li         if (wdtTimeOutActStr.empty())
1549c45f0082SYong Li         {
1550c45f0082SYong Li             BMCWEB_LOG_DEBUG << "Unsupported value for TimeoutAction: "
1551c45f0082SYong Li                              << *wdtTimeOutAction;
1552c45f0082SYong Li             messages::propertyValueNotInList(aResp->res, *wdtTimeOutAction,
1553c45f0082SYong Li                                              "TimeoutAction");
1554c45f0082SYong Li             return;
1555c45f0082SYong Li         }
1556c45f0082SYong Li 
1557c45f0082SYong Li         crow::connections::systemBus->async_method_call(
1558c45f0082SYong Li             [aResp](const boost::system::error_code ec) {
1559c45f0082SYong Li                 if (ec)
1560c45f0082SYong Li                 {
1561c45f0082SYong Li                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1562c45f0082SYong Li                     messages::internalError(aResp->res);
1563c45f0082SYong Li                     return;
1564c45f0082SYong Li                 }
1565c45f0082SYong Li             },
1566c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
1567c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
1568c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
1569c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "ExpireAction",
1570c45f0082SYong Li             std::variant<std::string>(wdtTimeOutActStr));
1571c45f0082SYong Li     }
1572c45f0082SYong Li 
1573c45f0082SYong Li     if (wdtEnable)
1574c45f0082SYong Li     {
1575c45f0082SYong Li         crow::connections::systemBus->async_method_call(
1576c45f0082SYong Li             [aResp](const boost::system::error_code ec) {
1577c45f0082SYong Li                 if (ec)
1578c45f0082SYong Li                 {
1579c45f0082SYong Li                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1580c45f0082SYong Li                     messages::internalError(aResp->res);
1581c45f0082SYong Li                     return;
1582c45f0082SYong Li                 }
1583c45f0082SYong Li             },
1584c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
1585c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
1586c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
1587c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "Enabled",
1588c45f0082SYong Li             std::variant<bool>(*wdtEnable));
1589c45f0082SYong Li     }
1590c45f0082SYong Li }
1591c45f0082SYong Li 
1592c45f0082SYong Li /**
1593c5b2abe0SLewanczyk, Dawid  * SystemsCollection derived class for delivering ComputerSystems Collection
1594c5b2abe0SLewanczyk, Dawid  * Schema
1595c5b2abe0SLewanczyk, Dawid  */
15961abe55efSEd Tanous class SystemsCollection : public Node
15971abe55efSEd Tanous {
1598c5b2abe0SLewanczyk, Dawid   public:
15991abe55efSEd Tanous     SystemsCollection(CrowApp &app) : Node(app, "/redfish/v1/Systems/")
16001abe55efSEd Tanous     {
1601c5b2abe0SLewanczyk, Dawid         entityPrivileges = {
1602c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::get, {{"Login"}}},
1603c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::head, {{"Login"}}},
1604c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1605c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1606c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1607c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1608c5b2abe0SLewanczyk, Dawid     }
1609c5b2abe0SLewanczyk, Dawid 
1610c5b2abe0SLewanczyk, Dawid   private:
161155c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
16121abe55efSEd Tanous                const std::vector<std::string> &params) override
16131abe55efSEd Tanous     {
1614462023adSSunitha Harish         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
16150f74e643SEd Tanous         res.jsonValue["@odata.type"] =
16160f74e643SEd Tanous             "#ComputerSystemCollection.ComputerSystemCollection";
16170f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Systems";
16180f74e643SEd Tanous         res.jsonValue["Name"] = "Computer System Collection";
1619462023adSSunitha Harish 
1620462023adSSunitha Harish         crow::connections::systemBus->async_method_call(
1621462023adSSunitha Harish             [asyncResp](const boost::system::error_code ec,
1622462023adSSunitha Harish                         const std::variant<std::string> &hostName) {
1623462023adSSunitha Harish                 nlohmann::json &iface_array =
1624462023adSSunitha Harish                     asyncResp->res.jsonValue["Members"];
1625462023adSSunitha Harish                 iface_array = nlohmann::json::array();
1626462023adSSunitha Harish                 auto &count = asyncResp->res.jsonValue["Members@odata.count"];
1627462023adSSunitha Harish                 count = 0;
1628462023adSSunitha Harish                 if (ec)
1629462023adSSunitha Harish                 {
1630462023adSSunitha Harish                     iface_array.push_back(
1631462023adSSunitha Harish                         {{"@odata.id", "/redfish/v1/Systems/system"}});
1632462023adSSunitha Harish                     count = iface_array.size();
1633462023adSSunitha Harish                     return;
1634462023adSSunitha Harish                 }
1635462023adSSunitha Harish                 BMCWEB_LOG_DEBUG << "Hypervisor is available";
1636462023adSSunitha Harish                 iface_array.push_back(
1637462023adSSunitha Harish                     {{"@odata.id", "/redfish/v1/Systems/system"}});
1638462023adSSunitha Harish                 iface_array.push_back(
1639462023adSSunitha Harish                     {{"@odata.id", "/redfish/v1/Systems/hypervisor"}});
1640462023adSSunitha Harish                 count = iface_array.size();
1641462023adSSunitha Harish             },
1642462023adSSunitha Harish             "xyz.openbmc_project.Settings", "/xyz/openbmc_project/network/vmi",
1643462023adSSunitha Harish             "org.freedesktop.DBus.Properties", "Get",
1644462023adSSunitha Harish             "xyz.openbmc_project.Network.SystemConfiguration", "HostName");
1645c5b2abe0SLewanczyk, Dawid     }
1646c5b2abe0SLewanczyk, Dawid };
1647c5b2abe0SLewanczyk, Dawid 
1648c5b2abe0SLewanczyk, Dawid /**
1649cc340dd9SEd Tanous  * SystemActionsReset class supports handle POST method for Reset action.
1650cc340dd9SEd Tanous  * The class retrieves and sends data directly to D-Bus.
1651cc340dd9SEd Tanous  */
1652cc340dd9SEd Tanous class SystemActionsReset : public Node
1653cc340dd9SEd Tanous {
1654cc340dd9SEd Tanous   public:
1655cc340dd9SEd Tanous     SystemActionsReset(CrowApp &app) :
1656029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset/")
1657cc340dd9SEd Tanous     {
1658cc340dd9SEd Tanous         entityPrivileges = {
1659cc340dd9SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1660cc340dd9SEd Tanous     }
1661cc340dd9SEd Tanous 
1662cc340dd9SEd Tanous   private:
1663cc340dd9SEd Tanous     /**
1664cc340dd9SEd Tanous      * Function handles POST method request.
1665cc340dd9SEd Tanous      * Analyzes POST body message before sends Reset request data to D-Bus.
1666cc340dd9SEd Tanous      */
1667cc340dd9SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
1668cc340dd9SEd Tanous                 const std::vector<std::string> &params) override
1669cc340dd9SEd Tanous     {
1670cc340dd9SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1671cc340dd9SEd Tanous 
16729712f8acSEd Tanous         std::string resetType;
16739712f8acSEd Tanous         if (!json_util::readJson(req, res, "ResetType", resetType))
1674cc340dd9SEd Tanous         {
1675cc340dd9SEd Tanous             return;
1676cc340dd9SEd Tanous         }
1677cc340dd9SEd Tanous 
1678d22c8396SJason M. Bills         // Get the command and host vs. chassis
1679cc340dd9SEd Tanous         std::string command;
1680d22c8396SJason M. Bills         bool hostCommand;
16819712f8acSEd Tanous         if (resetType == "On")
1682cc340dd9SEd Tanous         {
1683cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.On";
1684d22c8396SJason M. Bills             hostCommand = true;
1685d22c8396SJason M. Bills         }
1686d22c8396SJason M. Bills         else if (resetType == "ForceOff")
1687d22c8396SJason M. Bills         {
1688d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Chassis.Transition.Off";
1689d22c8396SJason M. Bills             hostCommand = false;
1690d22c8396SJason M. Bills         }
1691d22c8396SJason M. Bills         else if (resetType == "ForceOn")
1692d22c8396SJason M. Bills         {
1693d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Host.Transition.On";
1694d22c8396SJason M. Bills             hostCommand = true;
1695d22c8396SJason M. Bills         }
1696d22c8396SJason M. Bills         else if (resetType == "ForceRestart")
1697d22c8396SJason M. Bills         {
169886a0851aSJason M. Bills             command =
169986a0851aSJason M. Bills                 "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot";
170086a0851aSJason M. Bills             hostCommand = true;
1701cc340dd9SEd Tanous         }
17029712f8acSEd Tanous         else if (resetType == "GracefulShutdown")
1703cc340dd9SEd Tanous         {
1704cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.Off";
1705d22c8396SJason M. Bills             hostCommand = true;
1706cc340dd9SEd Tanous         }
17079712f8acSEd Tanous         else if (resetType == "GracefulRestart")
1708cc340dd9SEd Tanous         {
170986a0851aSJason M. Bills             command =
171086a0851aSJason M. Bills                 "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot";
1711d22c8396SJason M. Bills             hostCommand = true;
1712d22c8396SJason M. Bills         }
1713d22c8396SJason M. Bills         else if (resetType == "PowerCycle")
1714d22c8396SJason M. Bills         {
171586a0851aSJason M. Bills             command = "xyz.openbmc_project.State.Host.Transition.Reboot";
171686a0851aSJason M. Bills             hostCommand = true;
1717cc340dd9SEd Tanous         }
1718bfd5b826SLakshminarayana R. Kammath         else if (resetType == "Nmi")
1719bfd5b826SLakshminarayana R. Kammath         {
1720bfd5b826SLakshminarayana R. Kammath             doNMI(asyncResp);
1721bfd5b826SLakshminarayana R. Kammath             return;
1722bfd5b826SLakshminarayana R. Kammath         }
1723cc340dd9SEd Tanous         else
1724cc340dd9SEd Tanous         {
1725f12894f8SJason M. Bills             messages::actionParameterUnknown(res, "Reset", resetType);
1726cc340dd9SEd Tanous             return;
1727cc340dd9SEd Tanous         }
1728cc340dd9SEd Tanous 
1729d22c8396SJason M. Bills         if (hostCommand)
1730d22c8396SJason M. Bills         {
1731cc340dd9SEd Tanous             crow::connections::systemBus->async_method_call(
1732d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
1733cc340dd9SEd Tanous                     if (ec)
1734cc340dd9SEd Tanous                     {
1735cc340dd9SEd Tanous                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1736d22c8396SJason M. Bills                         if (ec.value() == boost::asio::error::invalid_argument)
1737d22c8396SJason M. Bills                         {
1738d22c8396SJason M. Bills                             messages::actionParameterNotSupported(
1739d22c8396SJason M. Bills                                 asyncResp->res, resetType, "Reset");
1740d22c8396SJason M. Bills                         }
1741d22c8396SJason M. Bills                         else
1742d22c8396SJason M. Bills                         {
1743f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
1744d22c8396SJason M. Bills                         }
1745cc340dd9SEd Tanous                         return;
1746cc340dd9SEd Tanous                     }
1747f12894f8SJason M. Bills                     messages::success(asyncResp->res);
1748cc340dd9SEd Tanous                 },
1749cc340dd9SEd Tanous                 "xyz.openbmc_project.State.Host",
1750cc340dd9SEd Tanous                 "/xyz/openbmc_project/state/host0",
1751cc340dd9SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
17529712f8acSEd Tanous                 "xyz.openbmc_project.State.Host", "RequestedHostTransition",
1753abf2add6SEd Tanous                 std::variant<std::string>{command});
1754cc340dd9SEd Tanous         }
1755d22c8396SJason M. Bills         else
1756d22c8396SJason M. Bills         {
1757d22c8396SJason M. Bills             crow::connections::systemBus->async_method_call(
1758d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
1759d22c8396SJason M. Bills                     if (ec)
1760d22c8396SJason M. Bills                     {
1761d22c8396SJason M. Bills                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1762d22c8396SJason M. Bills                         if (ec.value() == boost::asio::error::invalid_argument)
1763d22c8396SJason M. Bills                         {
1764d22c8396SJason M. Bills                             messages::actionParameterNotSupported(
1765d22c8396SJason M. Bills                                 asyncResp->res, resetType, "Reset");
1766d22c8396SJason M. Bills                         }
1767d22c8396SJason M. Bills                         else
1768d22c8396SJason M. Bills                         {
1769d22c8396SJason M. Bills                             messages::internalError(asyncResp->res);
1770d22c8396SJason M. Bills                         }
1771d22c8396SJason M. Bills                         return;
1772d22c8396SJason M. Bills                     }
1773d22c8396SJason M. Bills                     messages::success(asyncResp->res);
1774d22c8396SJason M. Bills                 },
1775d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis",
1776d22c8396SJason M. Bills                 "/xyz/openbmc_project/state/chassis0",
1777d22c8396SJason M. Bills                 "org.freedesktop.DBus.Properties", "Set",
1778d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition",
1779d22c8396SJason M. Bills                 std::variant<std::string>{command});
1780d22c8396SJason M. Bills         }
1781d22c8396SJason M. Bills     }
1782bfd5b826SLakshminarayana R. Kammath     /**
1783bfd5b826SLakshminarayana R. Kammath      * Function transceives data with dbus directly.
1784bfd5b826SLakshminarayana R. Kammath      */
1785bfd5b826SLakshminarayana R. Kammath     void doNMI(const std::shared_ptr<AsyncResp> &asyncResp)
1786bfd5b826SLakshminarayana R. Kammath     {
1787bfd5b826SLakshminarayana R. Kammath         constexpr char const *serviceName =
1788bfd5b826SLakshminarayana R. Kammath             "xyz.openbmc_project.Control.Host.NMI";
1789bfd5b826SLakshminarayana R. Kammath         constexpr char const *objectPath =
1790bfd5b826SLakshminarayana R. Kammath             "/xyz/openbmc_project/control/host0/nmi";
1791bfd5b826SLakshminarayana R. Kammath         constexpr char const *interfaceName =
1792bfd5b826SLakshminarayana R. Kammath             "xyz.openbmc_project.Control.Host.NMI";
1793bfd5b826SLakshminarayana R. Kammath         constexpr char const *method = "NMI";
1794bfd5b826SLakshminarayana R. Kammath 
1795bfd5b826SLakshminarayana R. Kammath         crow::connections::systemBus->async_method_call(
1796bfd5b826SLakshminarayana R. Kammath             [asyncResp](const boost::system::error_code ec) {
1797bfd5b826SLakshminarayana R. Kammath                 if (ec)
1798bfd5b826SLakshminarayana R. Kammath                 {
1799bfd5b826SLakshminarayana R. Kammath                     BMCWEB_LOG_ERROR << " Bad D-Bus request error: " << ec;
1800bfd5b826SLakshminarayana R. Kammath                     messages::internalError(asyncResp->res);
1801bfd5b826SLakshminarayana R. Kammath                     return;
1802bfd5b826SLakshminarayana R. Kammath                 }
1803bfd5b826SLakshminarayana R. Kammath                 messages::success(asyncResp->res);
1804bfd5b826SLakshminarayana R. Kammath             },
1805bfd5b826SLakshminarayana R. Kammath             serviceName, objectPath, interfaceName, method);
1806bfd5b826SLakshminarayana R. Kammath     }
1807cc340dd9SEd Tanous };
1808cc340dd9SEd Tanous 
1809cc340dd9SEd Tanous /**
18106617338dSEd Tanous  * Systems derived class for delivering Computer Systems Schema.
1811c5b2abe0SLewanczyk, Dawid  */
18121abe55efSEd Tanous class Systems : public Node
18131abe55efSEd Tanous {
1814c5b2abe0SLewanczyk, Dawid   public:
1815c5b2abe0SLewanczyk, Dawid     /*
1816c5b2abe0SLewanczyk, Dawid      * Default Constructor
1817c5b2abe0SLewanczyk, Dawid      */
1818029573d4SEd Tanous     Systems(CrowApp &app) : Node(app, "/redfish/v1/Systems/system/")
18191abe55efSEd Tanous     {
1820c5b2abe0SLewanczyk, Dawid         entityPrivileges = {
1821c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::get, {{"Login"}}},
1822c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::head, {{"Login"}}},
1823c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1824c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1825c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1826c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1827c5b2abe0SLewanczyk, Dawid     }
1828c5b2abe0SLewanczyk, Dawid 
1829c5b2abe0SLewanczyk, Dawid   private:
1830c5b2abe0SLewanczyk, Dawid     /**
1831c5b2abe0SLewanczyk, Dawid      * Functions triggers appropriate requests on DBus
1832c5b2abe0SLewanczyk, Dawid      */
183355c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
18341abe55efSEd Tanous                const std::vector<std::string> &params) override
18351abe55efSEd Tanous     {
18366bd5a8d2SGunnar Mills         res.jsonValue["@odata.type"] = "#ComputerSystem.v1_11_0.ComputerSystem";
1837450a25cbSGunnar Mills         res.jsonValue["Name"] = "system";
1838029573d4SEd Tanous         res.jsonValue["Id"] = "system";
18390f74e643SEd Tanous         res.jsonValue["SystemType"] = "Physical";
18400f74e643SEd Tanous         res.jsonValue["Description"] = "Computer System";
18410f74e643SEd Tanous         res.jsonValue["ProcessorSummary"]["Count"] = 0;
18420f74e643SEd Tanous         res.jsonValue["ProcessorSummary"]["Status"]["State"] = "Disabled";
18435fd7ba65SCheng C Yang         res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] = uint64_t(0);
18440f74e643SEd Tanous         res.jsonValue["MemorySummary"]["Status"]["State"] = "Disabled";
1845029573d4SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Systems/system";
184604a258f4SEd Tanous 
1847443c2934SRapkiewicz, Pawel         res.jsonValue["Processors"] = {
1848029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/Processors"}};
1849443c2934SRapkiewicz, Pawel         res.jsonValue["Memory"] = {
1850029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/Memory"}};
1851a25aeccfSNikhil Potade         res.jsonValue["Storage"] = {
1852a25aeccfSNikhil Potade             {"@odata.id", "/redfish/v1/Systems/system/Storage"}};
1853029573d4SEd Tanous 
1854cc340dd9SEd Tanous         // TODO Need to support ForceRestart.
1855cc340dd9SEd Tanous         res.jsonValue["Actions"]["#ComputerSystem.Reset"] = {
1856cc340dd9SEd Tanous             {"target",
1857029573d4SEd Tanous              "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset"},
1858cc340dd9SEd Tanous             {"ResetType@Redfish.AllowableValues",
1859d22c8396SJason M. Bills              {"On", "ForceOff", "ForceOn", "ForceRestart", "GracefulRestart",
1860bfd5b826SLakshminarayana R. Kammath               "GracefulShutdown", "PowerCycle", "Nmi"}}};
1861c5b2abe0SLewanczyk, Dawid 
1862c4bf6374SJason M. Bills         res.jsonValue["LogServices"] = {
1863029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/LogServices"}};
1864c4bf6374SJason M. Bills 
1865d82a3acdSCarol Wang         res.jsonValue["Bios"] = {
1866d82a3acdSCarol Wang             {"@odata.id", "/redfish/v1/Systems/system/Bios"}};
1867d82a3acdSCarol Wang 
1868c5d03ff4SJennifer Lee         res.jsonValue["Links"]["ManagedBy"] = {
1869c5d03ff4SJennifer Lee             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
1870c5d03ff4SJennifer Lee 
1871c5d03ff4SJennifer Lee         res.jsonValue["Status"] = {
1872c5d03ff4SJennifer Lee             {"Health", "OK"},
1873c5d03ff4SJennifer Lee             {"State", "Enabled"},
1874c5d03ff4SJennifer Lee         };
1875a0803efaSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1876c5b2abe0SLewanczyk, Dawid 
1877e284a7c1SJames Feist         constexpr const std::array<const char *, 4> inventoryForSystems = {
1878b49ac873SJames Feist             "xyz.openbmc_project.Inventory.Item.Dimm",
18792ad9c2f6SJames Feist             "xyz.openbmc_project.Inventory.Item.Cpu",
1880e284a7c1SJames Feist             "xyz.openbmc_project.Inventory.Item.Drive",
1881e284a7c1SJames Feist             "xyz.openbmc_project.Inventory.Item.StorageController"};
1882b49ac873SJames Feist 
1883b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
1884b49ac873SJames Feist         crow::connections::systemBus->async_method_call(
1885b49ac873SJames Feist             [health](const boost::system::error_code ec,
1886b49ac873SJames Feist                      std::vector<std::string> &resp) {
1887b49ac873SJames Feist                 if (ec)
1888b49ac873SJames Feist                 {
1889b49ac873SJames Feist                     // no inventory
1890b49ac873SJames Feist                     return;
1891b49ac873SJames Feist                 }
1892b49ac873SJames Feist 
1893b49ac873SJames Feist                 health->inventory = std::move(resp);
1894b49ac873SJames Feist             },
1895b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper",
1896b49ac873SJames Feist             "/xyz/openbmc_project/object_mapper",
1897b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1898b49ac873SJames Feist             int32_t(0), inventoryForSystems);
1899b49ac873SJames Feist 
1900b49ac873SJames Feist         health->populate();
1901b49ac873SJames Feist 
1902c5d03ff4SJennifer Lee         getMainChassisId(asyncResp, [](const std::string &chassisId,
1903c5d03ff4SJennifer Lee                                        std::shared_ptr<AsyncResp> aRsp) {
1904c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["Chassis"] = {
1905c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
1906c5d03ff4SJennifer Lee         });
1907a3002228SAppaRao Puli 
1908a3002228SAppaRao Puli         getIndicatorLedState(asyncResp);
19095bc2dc8eSJames Feist         getComputerSystem(asyncResp, health);
19106c34de48SEd Tanous         getHostState(asyncResp);
1911491d8ee7SSantosh Puranik         getBootProperties(asyncResp);
1912adbe192aSJason M. Bills         getPCIeDeviceList(asyncResp, "PCIeDevices");
191351709ffdSYong Li         getHostWatchdogTimer(asyncResp);
1914c6a620f2SGeorge Liu         getPowerRestorePolicy(asyncResp);
19156bd5a8d2SGunnar Mills         getAutomaticRetry(asyncResp);
1916a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
1917a6349918SAppaRao Puli         getProvisioningStatus(asyncResp);
1918a6349918SAppaRao Puli #endif
1919c5b2abe0SLewanczyk, Dawid     }
1920c5b2abe0SLewanczyk, Dawid 
192155c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
19221abe55efSEd Tanous                  const std::vector<std::string> &params) override
19231abe55efSEd Tanous     {
1924cde19e5fSSantosh Puranik         std::optional<std::string> indicatorLed;
1925491d8ee7SSantosh Puranik         std::optional<nlohmann::json> bootProps;
1926c45f0082SYong Li         std::optional<nlohmann::json> wdtTimerProps;
1927c6a620f2SGeorge Liu         std::optional<std::string> powerRestorePolicy;
192841352c24SSantosh Puranik         auto asyncResp = std::make_shared<AsyncResp>(res);
192941352c24SSantosh Puranik 
1930944ffaf9SJohnathan Mantey         if (!json_util::readJson(req, res, "IndicatorLED", indicatorLed, "Boot",
1931c6a620f2SGeorge Liu                                  bootProps, "WatchdogTimer", wdtTimerProps,
1932c6a620f2SGeorge Liu                                  "PowerRestorePolicy", powerRestorePolicy))
19336617338dSEd Tanous         {
19346617338dSEd Tanous             return;
19356617338dSEd Tanous         }
1936491d8ee7SSantosh Puranik 
1937944ffaf9SJohnathan Mantey         res.result(boost::beast::http::status::no_content);
1938c45f0082SYong Li 
1939c45f0082SYong Li         if (wdtTimerProps)
1940c45f0082SYong Li         {
1941c45f0082SYong Li             std::optional<bool> wdtEnable;
1942c45f0082SYong Li             std::optional<std::string> wdtTimeOutAction;
1943c45f0082SYong Li 
1944c45f0082SYong Li             if (!json_util::readJson(*wdtTimerProps, asyncResp->res,
1945c45f0082SYong Li                                      "FunctionEnabled", wdtEnable,
1946c45f0082SYong Li                                      "TimeoutAction", wdtTimeOutAction))
1947c45f0082SYong Li             {
1948c45f0082SYong Li                 return;
1949c45f0082SYong Li             }
1950c45f0082SYong Li             setWDTProperties(asyncResp, std::move(wdtEnable),
1951c45f0082SYong Li                              std::move(wdtTimeOutAction));
1952c45f0082SYong Li         }
1953c45f0082SYong Li 
1954491d8ee7SSantosh Puranik         if (bootProps)
1955491d8ee7SSantosh Puranik         {
1956491d8ee7SSantosh Puranik             std::optional<std::string> bootSource;
1957491d8ee7SSantosh Puranik             std::optional<std::string> bootEnable;
1958*69f35306SGunnar Mills             std::optional<std::string> automaticRetryConfig;
1959491d8ee7SSantosh Puranik 
1960*69f35306SGunnar Mills             if (!json_util::readJson(
1961*69f35306SGunnar Mills                     *bootProps, asyncResp->res, "BootSourceOverrideTarget",
1962*69f35306SGunnar Mills                     bootSource, "BootSourceOverrideEnabled", bootEnable,
1963*69f35306SGunnar Mills                     "AutomaticRetryConfig", automaticRetryConfig))
1964491d8ee7SSantosh Puranik             {
1965491d8ee7SSantosh Puranik                 return;
1966491d8ee7SSantosh Puranik             }
1967*69f35306SGunnar Mills             if (bootSource || bootEnable)
1968*69f35306SGunnar Mills             {
1969*69f35306SGunnar Mills                 setBootSourceProperties(asyncResp, std::move(bootSource),
1970491d8ee7SSantosh Puranik                                         std::move(bootEnable));
1971491d8ee7SSantosh Puranik             }
1972*69f35306SGunnar Mills             if (automaticRetryConfig)
1973*69f35306SGunnar Mills             {
1974*69f35306SGunnar Mills                 setAutomaticRetry(asyncResp, std::move(*automaticRetryConfig));
1975*69f35306SGunnar Mills             }
1976*69f35306SGunnar Mills         }
1977265c1602SJohnathan Mantey 
19789712f8acSEd Tanous         if (indicatorLed)
19796617338dSEd Tanous         {
1980a3002228SAppaRao Puli             setIndicatorLedState(asyncResp, std::move(*indicatorLed));
19816617338dSEd Tanous         }
1982c6a620f2SGeorge Liu 
1983c6a620f2SGeorge Liu         if (powerRestorePolicy)
1984c6a620f2SGeorge Liu         {
1985c6a620f2SGeorge Liu             setPowerRestorePolicy(asyncResp, std::move(*powerRestorePolicy));
1986c6a620f2SGeorge Liu         }
1987c5b2abe0SLewanczyk, Dawid     }
1988c5b2abe0SLewanczyk, Dawid };
1989c5b2abe0SLewanczyk, Dawid } // namespace redfish
1990