xref: /openbmc/bmcweb/features/redfish/lib/systems.hpp (revision 450a25cbaf88b640ba55435bbb5bfa8901798c5e)
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 =
2305fd7ba65SCheng C Yang                                                 sdbusplus::message::variant_ns::
2315fd7ba65SCheng C Yang                                                     get_if<uint32_t>(
2321b6b96c5SEd Tanous                                                         &property.second);
2335fd7ba65SCheng C Yang                                             if (value == nullptr)
2341abe55efSEd Tanous                                             {
2355fd7ba65SCheng C Yang                                                 BMCWEB_LOG_DEBUG
2365fd7ba65SCheng C Yang                                                     << "Find incorrect type of "
2375fd7ba65SCheng C Yang                                                        "MemorySize";
2385fd7ba65SCheng C Yang                                                 continue;
2395fd7ba65SCheng C Yang                                             }
2405fd7ba65SCheng C Yang                                             nlohmann::json &totalMemory =
2415fd7ba65SCheng C Yang                                                 aResp->res
2425fd7ba65SCheng C Yang                                                     .jsonValue["MemorySummar"
2435fd7ba65SCheng C Yang                                                                "y"]
2445fd7ba65SCheng C Yang                                                               ["TotalSystemMe"
2455fd7ba65SCheng C Yang                                                                "moryGiB"];
2465fd7ba65SCheng C Yang                                             uint64_t *preValue =
2475fd7ba65SCheng C Yang                                                 totalMemory
2485fd7ba65SCheng C Yang                                                     .get_ptr<uint64_t *>();
2495fd7ba65SCheng C Yang                                             if (preValue == nullptr)
2505fd7ba65SCheng C Yang                                             {
2515fd7ba65SCheng C Yang                                                 continue;
2525fd7ba65SCheng C Yang                                             }
2535fd7ba65SCheng C Yang                                             aResp->res
2545fd7ba65SCheng C Yang                                                 .jsonValue["MemorySummary"]
2556c34de48SEd Tanous                                                           ["TotalSystemMemoryGi"
2565fd7ba65SCheng C Yang                                                            "B"] =
2575fd7ba65SCheng C Yang                                                 *value / (1024 * 1024) +
2585fd7ba65SCheng C Yang                                                 *preValue;
2595fd7ba65SCheng C Yang                                             aResp->res
2605fd7ba65SCheng C Yang                                                 .jsonValue["MemorySummary"]
2619d3ae10eSAlpana Kumari                                                           ["Status"]["State"] =
2621abe55efSEd Tanous                                                 "Enabled";
263c5b2abe0SLewanczyk, Dawid                                         }
264c5b2abe0SLewanczyk, Dawid                                     }
2659d3ae10eSAlpana Kumari                                     else
2669d3ae10eSAlpana Kumari                                     {
2679d3ae10eSAlpana Kumari                                         auto getDimmProperties =
2689d3ae10eSAlpana Kumari                                             [aResp](
2699d3ae10eSAlpana Kumari                                                 const boost::system::error_code
2709d3ae10eSAlpana Kumari                                                     ec,
2719d3ae10eSAlpana Kumari                                                 const std::variant<bool>
2729d3ae10eSAlpana Kumari                                                     &dimmState) {
2739d3ae10eSAlpana Kumari                                                 if (ec)
2749d3ae10eSAlpana Kumari                                                 {
2759d3ae10eSAlpana Kumari                                                     BMCWEB_LOG_ERROR
2769d3ae10eSAlpana Kumari                                                         << "DBUS response "
2779d3ae10eSAlpana Kumari                                                            "error "
2789d3ae10eSAlpana Kumari                                                         << ec;
2799d3ae10eSAlpana Kumari                                                     return;
2809d3ae10eSAlpana Kumari                                                 }
2819d3ae10eSAlpana Kumari                                                 updateDimmProperties(aResp,
2829d3ae10eSAlpana Kumari                                                                      dimmState);
2839d3ae10eSAlpana Kumari                                             };
2849d3ae10eSAlpana Kumari                                         crow::connections::systemBus
2859d3ae10eSAlpana Kumari                                             ->async_method_call(
2869d3ae10eSAlpana Kumari                                                 std::move(getDimmProperties),
2879d3ae10eSAlpana Kumari                                                 service, path,
2889d3ae10eSAlpana Kumari                                                 "org.freedesktop.DBus."
2899d3ae10eSAlpana Kumari                                                 "Properties",
2909d3ae10eSAlpana Kumari                                                 "Get",
2919d3ae10eSAlpana Kumari                                                 "xyz.openbmc_project.State."
2929d3ae10eSAlpana Kumari                                                 "Decorator.OperationalStatus",
2939d3ae10eSAlpana Kumari                                                 "Functional");
2949d3ae10eSAlpana Kumari                                     }
295c5b2abe0SLewanczyk, Dawid                                 },
29604a258f4SEd Tanous                                 connection.first, path,
2976c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
2986c34de48SEd Tanous                                 "xyz.openbmc_project.Inventory.Item.Dimm");
2995bc2dc8eSJames Feist 
3005bc2dc8eSJames Feist                             memoryHealth->inventory.emplace_back(path);
3011abe55efSEd Tanous                         }
30204a258f4SEd Tanous                         else if (interfaceName ==
30304a258f4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.Cpu")
3041abe55efSEd Tanous                         {
3051abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
30604a258f4SEd Tanous                                 << "Found Cpu, now get its properties.";
30757e8c9beSAlpana Kumari 
308a0803efaSEd Tanous                             crow::connections::systemBus->async_method_call(
30957e8c9beSAlpana Kumari                                 [aResp, service{connection.first},
31057e8c9beSAlpana Kumari                                  path(std::move(path))](
31157e8c9beSAlpana Kumari                                     const boost::system::error_code ec,
3126c34de48SEd Tanous                                     const std::vector<
3136c34de48SEd Tanous                                         std::pair<std::string, VariantType>>
3141abe55efSEd Tanous                                         &properties) {
3151abe55efSEd Tanous                                     if (ec)
3161abe55efSEd Tanous                                     {
3171abe55efSEd Tanous                                         BMCWEB_LOG_ERROR
3186c34de48SEd Tanous                                             << "DBUS response error " << ec;
319f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
320c5b2abe0SLewanczyk, Dawid                                         return;
321c5b2abe0SLewanczyk, Dawid                                     }
3226c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
3236c34de48SEd Tanous                                                      << properties.size()
324c5b2abe0SLewanczyk, Dawid                                                      << " Cpu properties.";
32557e8c9beSAlpana Kumari 
32657e8c9beSAlpana Kumari                                     if (properties.size() > 0)
32757e8c9beSAlpana Kumari                                     {
32804a258f4SEd Tanous                                         for (const auto &property : properties)
3291abe55efSEd Tanous                                         {
33057e8c9beSAlpana Kumari                                             if (property.first ==
33157e8c9beSAlpana Kumari                                                 "ProcessorFamily")
3321abe55efSEd Tanous                                             {
333a0803efaSEd Tanous                                                 const std::string *value =
33457e8c9beSAlpana Kumari                                                     sdbusplus::message::
33557e8c9beSAlpana Kumari                                                         variant_ns::get_if<
33657e8c9beSAlpana Kumari                                                             std::string>(
3371b6b96c5SEd Tanous                                                             &property.second);
3381abe55efSEd Tanous                                                 if (value != nullptr)
3391abe55efSEd Tanous                                                 {
34057e8c9beSAlpana Kumari                                                     nlohmann::json
34157e8c9beSAlpana Kumari                                                         &procSummary =
3421abe55efSEd Tanous                                                             aResp->res.jsonValue
3436c34de48SEd Tanous                                                                 ["ProcessorSumm"
34404a258f4SEd Tanous                                                                  "ary"];
34504a258f4SEd Tanous                                                     nlohmann::json &procCount =
34604a258f4SEd Tanous                                                         procSummary["Count"];
347b4b9595aSJames Feist 
348b4b9595aSJames Feist                                                     auto procCountPtr =
349b4b9595aSJames Feist                                                         procCount.get_ptr<
350b4b9595aSJames Feist                                                             nlohmann::json::
351b4b9595aSJames Feist                                                                 number_integer_t
352b4b9595aSJames Feist                                                                     *>();
353b4b9595aSJames Feist                                                     if (procCountPtr != nullptr)
354b4b9595aSJames Feist                                                     {
355b4b9595aSJames Feist                                                         // shouldn't be possible
356b4b9595aSJames Feist                                                         // to be nullptr
357b4b9595aSJames Feist                                                         *procCountPtr += 1;
358b4b9595aSJames Feist                                                     }
35957e8c9beSAlpana Kumari                                                     procSummary["Status"]
36057e8c9beSAlpana Kumari                                                                ["State"] =
361c5b2abe0SLewanczyk, Dawid                                                                    "Enabled";
36257e8c9beSAlpana Kumari                                                     procSummary["Model"] =
36357e8c9beSAlpana Kumari                                                         *value;
364c5b2abe0SLewanczyk, Dawid                                                 }
365c5b2abe0SLewanczyk, Dawid                                             }
366c5b2abe0SLewanczyk, Dawid                                         }
36757e8c9beSAlpana Kumari                                     }
36857e8c9beSAlpana Kumari                                     else
36957e8c9beSAlpana Kumari                                     {
37057e8c9beSAlpana Kumari                                         auto getCpuPresenceState =
37157e8c9beSAlpana Kumari                                             [aResp](
37257e8c9beSAlpana Kumari                                                 const boost::system::error_code
37357e8c9beSAlpana Kumari                                                     ec,
37457e8c9beSAlpana Kumari                                                 const std::variant<bool>
37557e8c9beSAlpana Kumari                                                     &cpuPresenceCheck) {
37657e8c9beSAlpana Kumari                                                 if (ec)
37757e8c9beSAlpana Kumari                                                 {
37857e8c9beSAlpana Kumari                                                     BMCWEB_LOG_ERROR
37957e8c9beSAlpana Kumari                                                         << "DBUS response "
38057e8c9beSAlpana Kumari                                                            "error "
38157e8c9beSAlpana Kumari                                                         << ec;
38257e8c9beSAlpana Kumari                                                     return;
38357e8c9beSAlpana Kumari                                                 }
38457e8c9beSAlpana Kumari                                                 modifyCpuPresenceState(
38557e8c9beSAlpana Kumari                                                     aResp, cpuPresenceCheck);
38657e8c9beSAlpana Kumari                                             };
38757e8c9beSAlpana Kumari 
38857e8c9beSAlpana Kumari                                         auto getCpuFunctionalState =
38957e8c9beSAlpana Kumari                                             [aResp](
39057e8c9beSAlpana Kumari                                                 const boost::system::error_code
39157e8c9beSAlpana Kumari                                                     ec,
39257e8c9beSAlpana Kumari                                                 const std::variant<bool>
39357e8c9beSAlpana Kumari                                                     &cpuFunctionalCheck) {
39457e8c9beSAlpana Kumari                                                 if (ec)
39557e8c9beSAlpana Kumari                                                 {
39657e8c9beSAlpana Kumari                                                     BMCWEB_LOG_ERROR
39757e8c9beSAlpana Kumari                                                         << "DBUS response "
39857e8c9beSAlpana Kumari                                                            "error "
39957e8c9beSAlpana Kumari                                                         << ec;
40057e8c9beSAlpana Kumari                                                     return;
40157e8c9beSAlpana Kumari                                                 }
40257e8c9beSAlpana Kumari                                                 modifyCpuFunctionalState(
40357e8c9beSAlpana Kumari                                                     aResp, cpuFunctionalCheck);
40457e8c9beSAlpana Kumari                                             };
40557e8c9beSAlpana Kumari                                         // Get the Presence of CPU
40657e8c9beSAlpana Kumari                                         crow::connections::systemBus
40757e8c9beSAlpana Kumari                                             ->async_method_call(
40857e8c9beSAlpana Kumari                                                 std::move(getCpuPresenceState),
40957e8c9beSAlpana Kumari                                                 service, path,
41057e8c9beSAlpana Kumari                                                 "org.freedesktop.DBus."
41157e8c9beSAlpana Kumari                                                 "Properties",
41257e8c9beSAlpana Kumari                                                 "Get",
41357e8c9beSAlpana Kumari                                                 "xyz.openbmc_project.Inventory."
41457e8c9beSAlpana Kumari                                                 "Item",
41557e8c9beSAlpana Kumari                                                 "Present");
41657e8c9beSAlpana Kumari 
41757e8c9beSAlpana Kumari                                         // Get the Functional State
41857e8c9beSAlpana Kumari                                         crow::connections::systemBus
41957e8c9beSAlpana Kumari                                             ->async_method_call(
42057e8c9beSAlpana Kumari                                                 std::move(
42157e8c9beSAlpana Kumari                                                     getCpuFunctionalState),
42257e8c9beSAlpana Kumari                                                 service, path,
42357e8c9beSAlpana Kumari                                                 "org.freedesktop.DBus."
42457e8c9beSAlpana Kumari                                                 "Properties",
42557e8c9beSAlpana Kumari                                                 "Get",
42657e8c9beSAlpana Kumari                                                 "xyz.openbmc_project.State."
42757e8c9beSAlpana Kumari                                                 "Decorator."
42857e8c9beSAlpana Kumari                                                 "OperationalStatus",
42957e8c9beSAlpana Kumari                                                 "Functional");
43057e8c9beSAlpana Kumari 
43157e8c9beSAlpana Kumari                                         // Get the MODEL from
43257e8c9beSAlpana Kumari                                         // xyz.openbmc_project.Inventory.Decorator.Asset
43357e8c9beSAlpana Kumari                                         // support it later as Model  is Empty
43457e8c9beSAlpana Kumari                                         // currently.
43557e8c9beSAlpana Kumari                                     }
436c5b2abe0SLewanczyk, Dawid                                 },
43704a258f4SEd Tanous                                 connection.first, path,
4386c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
4396c34de48SEd Tanous                                 "xyz.openbmc_project.Inventory.Item.Cpu");
4405bc2dc8eSJames Feist 
4415bc2dc8eSJames Feist                             cpuHealth->inventory.emplace_back(path);
4421abe55efSEd Tanous                         }
44304a258f4SEd Tanous                         else if (interfaceName ==
44404a258f4SEd Tanous                                  "xyz.openbmc_project.Common.UUID")
4451abe55efSEd Tanous                         {
4461abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
44704a258f4SEd Tanous                                 << "Found UUID, now get its properties.";
44855c7b7a2SEd Tanous                             crow::connections::systemBus->async_method_call(
449029573d4SEd Tanous                                 [aResp](const boost::system::error_code ec,
4506c34de48SEd Tanous                                         const std::vector<
4516c34de48SEd Tanous                                             std::pair<std::string, VariantType>>
4521abe55efSEd Tanous                                             &properties) {
4531abe55efSEd Tanous                                     if (ec)
4541abe55efSEd Tanous                                     {
4551abe55efSEd Tanous                                         BMCWEB_LOG_DEBUG
4566c34de48SEd Tanous                                             << "DBUS response error " << ec;
457f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
458c5b2abe0SLewanczyk, Dawid                                         return;
459c5b2abe0SLewanczyk, Dawid                                     }
4606c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
4616c34de48SEd Tanous                                                      << properties.size()
462c5b2abe0SLewanczyk, Dawid                                                      << " UUID properties.";
4631abe55efSEd Tanous                                     for (const std::pair<std::string,
46404a258f4SEd Tanous                                                          VariantType>
46504a258f4SEd Tanous                                              &property : properties)
4661abe55efSEd Tanous                                     {
46704a258f4SEd Tanous                                         if (property.first == "UUID")
4681abe55efSEd Tanous                                         {
469c5b2abe0SLewanczyk, Dawid                                             const std::string *value =
470029573d4SEd Tanous                                                 sdbusplus::message::variant_ns::
471029573d4SEd Tanous                                                     get_if<std::string>(
4721b6b96c5SEd Tanous                                                         &property.second);
47304a258f4SEd Tanous 
4741abe55efSEd Tanous                                             if (value != nullptr)
4751abe55efSEd Tanous                                             {
476029573d4SEd Tanous                                                 std::string valueStr = *value;
47704a258f4SEd Tanous                                                 if (valueStr.size() == 32)
4781abe55efSEd Tanous                                                 {
479029573d4SEd Tanous                                                     valueStr.insert(8, 1, '-');
480029573d4SEd Tanous                                                     valueStr.insert(13, 1, '-');
481029573d4SEd Tanous                                                     valueStr.insert(18, 1, '-');
482029573d4SEd Tanous                                                     valueStr.insert(23, 1, '-');
48304a258f4SEd Tanous                                                 }
484029573d4SEd Tanous                                                 BMCWEB_LOG_DEBUG << "UUID = "
48504a258f4SEd Tanous                                                                  << valueStr;
486029573d4SEd Tanous                                                 aResp->res.jsonValue["UUID"] =
48704a258f4SEd Tanous                                                     valueStr;
488c5b2abe0SLewanczyk, Dawid                                             }
489c5b2abe0SLewanczyk, Dawid                                         }
490c5b2abe0SLewanczyk, Dawid                                     }
491c5b2abe0SLewanczyk, Dawid                                 },
49204a258f4SEd Tanous                                 connection.first, path,
4936c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
4941abe55efSEd Tanous                                 "xyz.openbmc_project.Common.UUID");
495c5b2abe0SLewanczyk, Dawid                         }
496029573d4SEd Tanous                         else if (interfaceName ==
497029573d4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.System")
4981abe55efSEd Tanous                         {
499029573d4SEd Tanous                             crow::connections::systemBus->async_method_call(
500029573d4SEd Tanous                                 [aResp](const boost::system::error_code ec,
501029573d4SEd Tanous                                         const std::vector<
502029573d4SEd Tanous                                             std::pair<std::string, VariantType>>
503029573d4SEd Tanous                                             &propertiesList) {
504029573d4SEd Tanous                                     if (ec)
505029573d4SEd Tanous                                     {
506e4a4b9a9SJames Feist                                         // doesn't have to include this
507e4a4b9a9SJames Feist                                         // interface
508029573d4SEd Tanous                                         return;
509029573d4SEd Tanous                                     }
510698654b6SGunnar Mills                                     BMCWEB_LOG_DEBUG
511698654b6SGunnar Mills                                         << "Got " << propertiesList.size()
512029573d4SEd Tanous                                         << " properties for system";
513029573d4SEd Tanous                                     for (const std::pair<std::string,
514029573d4SEd Tanous                                                          VariantType>
515029573d4SEd Tanous                                              &property : propertiesList)
516029573d4SEd Tanous                                     {
517fc5afcf9Sbeccabroek                                         const std::string &propertyName =
518fc5afcf9Sbeccabroek                                             property.first;
519fc5afcf9Sbeccabroek                                         if ((propertyName == "PartNumber") ||
520fc5afcf9Sbeccabroek                                             (propertyName == "SerialNumber") ||
521fc5afcf9Sbeccabroek                                             (propertyName == "Manufacturer") ||
522fc5afcf9Sbeccabroek                                             (propertyName == "Model"))
523fc5afcf9Sbeccabroek                                         {
524029573d4SEd Tanous                                             const std::string *value =
525fc5afcf9Sbeccabroek                                                 std::get_if<std::string>(
526029573d4SEd Tanous                                                     &property.second);
527029573d4SEd Tanous                                             if (value != nullptr)
528029573d4SEd Tanous                                             {
529029573d4SEd Tanous                                                 aResp->res
530fc5afcf9Sbeccabroek                                                     .jsonValue[propertyName] =
531029573d4SEd Tanous                                                     *value;
532029573d4SEd Tanous                                             }
533029573d4SEd Tanous                                         }
534fc5afcf9Sbeccabroek                                     }
535029573d4SEd Tanous                                     aResp->res.jsonValue["Id"] =
536029573d4SEd Tanous                                         aResp->res.jsonValue["SerialNumber"];
537cb7e1e7bSAndrew Geissler                                     // Grab the bios version
538cb7e1e7bSAndrew Geissler                                     fw_util::getActiveFwVersion(
539cb7e1e7bSAndrew Geissler                                         aResp, fw_util::biosPurpose,
540cb7e1e7bSAndrew Geissler                                         "BiosVersion");
541029573d4SEd Tanous                                 },
542029573d4SEd Tanous                                 connection.first, path,
543029573d4SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
544029573d4SEd Tanous                                 "xyz.openbmc_project.Inventory.Decorator."
545029573d4SEd Tanous                                 "Asset");
546e4a4b9a9SJames Feist 
547e4a4b9a9SJames Feist                             crow::connections::systemBus->async_method_call(
548e4a4b9a9SJames Feist                                 [aResp](
549e4a4b9a9SJames Feist                                     const boost::system::error_code ec,
550e4a4b9a9SJames Feist                                     const std::variant<std::string> &property) {
551e4a4b9a9SJames Feist                                     if (ec)
552e4a4b9a9SJames Feist                                     {
553e4a4b9a9SJames Feist                                         // doesn't have to include this
554e4a4b9a9SJames Feist                                         // interface
555e4a4b9a9SJames Feist                                         return;
556e4a4b9a9SJames Feist                                     }
557e4a4b9a9SJames Feist 
558e4a4b9a9SJames Feist                                     const std::string *value =
559e4a4b9a9SJames Feist                                         std::get_if<std::string>(&property);
560e4a4b9a9SJames Feist                                     if (value != nullptr)
561e4a4b9a9SJames Feist                                     {
562e4a4b9a9SJames Feist                                         aResp->res.jsonValue["AssetTag"] =
563e4a4b9a9SJames Feist                                             *value;
564e4a4b9a9SJames Feist                                     }
565e4a4b9a9SJames Feist                                 },
566e4a4b9a9SJames Feist                                 connection.first, path,
567e4a4b9a9SJames Feist                                 "org.freedesktop.DBus.Properties", "Get",
568e4a4b9a9SJames Feist                                 "xyz.openbmc_project.Inventory.Decorator."
569e4a4b9a9SJames Feist                                 "AssetTag",
570e4a4b9a9SJames Feist                                 "AssetTag");
571029573d4SEd Tanous                         }
572029573d4SEd Tanous                     }
573029573d4SEd Tanous                 }
574c5b2abe0SLewanczyk, Dawid             }
575c5b2abe0SLewanczyk, Dawid         },
576c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper",
577c5b2abe0SLewanczyk, Dawid         "/xyz/openbmc_project/object_mapper",
578c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
5796617338dSEd Tanous         "/xyz/openbmc_project/inventory", int32_t(0),
5806617338dSEd Tanous         std::array<const char *, 5>{
5816617338dSEd Tanous             "xyz.openbmc_project.Inventory.Decorator.Asset",
5826617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Cpu",
5836617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Dimm",
5846617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.System",
5856617338dSEd Tanous             "xyz.openbmc_project.Common.UUID",
5866617338dSEd Tanous         });
587c5b2abe0SLewanczyk, Dawid }
588c5b2abe0SLewanczyk, Dawid 
589c5b2abe0SLewanczyk, Dawid /**
590c5b2abe0SLewanczyk, Dawid  * @brief Retrieves host state properties over dbus
591c5b2abe0SLewanczyk, Dawid  *
592c5b2abe0SLewanczyk, Dawid  * @param[in] aResp     Shared pointer for completing asynchronous calls.
593c5b2abe0SLewanczyk, Dawid  *
594c5b2abe0SLewanczyk, Dawid  * @return None.
595c5b2abe0SLewanczyk, Dawid  */
596a0803efaSEd Tanous void getHostState(std::shared_ptr<AsyncResp> aResp)
5971abe55efSEd Tanous {
59855c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get host information.";
59955c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
600c5d03ff4SJennifer Lee         [aResp](const boost::system::error_code ec,
601abf2add6SEd Tanous                 const std::variant<std::string> &hostState) {
6021abe55efSEd Tanous             if (ec)
6031abe55efSEd Tanous             {
60455c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
605f12894f8SJason M. Bills                 messages::internalError(aResp->res);
606c5b2abe0SLewanczyk, Dawid                 return;
607c5b2abe0SLewanczyk, Dawid             }
6086617338dSEd Tanous 
609abf2add6SEd Tanous             const std::string *s = std::get_if<std::string>(&hostState);
61055c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Host state: " << *s;
6116617338dSEd Tanous             if (s != nullptr)
6121abe55efSEd Tanous             {
613c5b2abe0SLewanczyk, Dawid                 // Verify Host State
61494732661SAndrew Geissler                 if (*s == "xyz.openbmc_project.State.Host.HostState.Running")
6151abe55efSEd Tanous                 {
61655c7b7a2SEd Tanous                     aResp->res.jsonValue["PowerState"] = "On";
6176617338dSEd Tanous                     aResp->res.jsonValue["Status"]["State"] = "Enabled";
6181abe55efSEd Tanous                 }
61983935af9SAndrew Geissler                 else if (*s == "xyz.openbmc_project.State.Host.HostState."
62083935af9SAndrew Geissler                                "DiagnosticMode")
62183935af9SAndrew Geissler                 {
62283935af9SAndrew Geissler                     aResp->res.jsonValue["PowerState"] = "On";
62383935af9SAndrew Geissler                     aResp->res.jsonValue["Status"]["State"] = "InTest";
62483935af9SAndrew Geissler                 }
6251abe55efSEd Tanous                 else
6261abe55efSEd Tanous                 {
62755c7b7a2SEd Tanous                     aResp->res.jsonValue["PowerState"] = "Off";
6286617338dSEd Tanous                     aResp->res.jsonValue["Status"]["State"] = "Disabled";
629c5b2abe0SLewanczyk, Dawid                 }
630c5b2abe0SLewanczyk, Dawid             }
631c5b2abe0SLewanczyk, Dawid         },
6326c34de48SEd Tanous         "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
6336617338dSEd Tanous         "org.freedesktop.DBus.Properties", "Get",
6346617338dSEd Tanous         "xyz.openbmc_project.State.Host", "CurrentHostState");
635c5b2abe0SLewanczyk, Dawid }
636c5b2abe0SLewanczyk, Dawid 
637c5b2abe0SLewanczyk, Dawid /**
638491d8ee7SSantosh Puranik  * @brief Traslates boot source DBUS property value to redfish.
639491d8ee7SSantosh Puranik  *
640491d8ee7SSantosh Puranik  * @param[in] dbusSource    The boot source in DBUS speak.
641491d8ee7SSantosh Puranik  *
642491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source in Redfish terms. If translation
643491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
644491d8ee7SSantosh Puranik  */
645491d8ee7SSantosh Puranik static std::string dbusToRfBootSource(const std::string &dbusSource)
646491d8ee7SSantosh Puranik {
647491d8ee7SSantosh Puranik     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
648491d8ee7SSantosh Puranik     {
649491d8ee7SSantosh Puranik         return "None";
650491d8ee7SSantosh Puranik     }
651491d8ee7SSantosh Puranik     else if (dbusSource ==
652491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.Disk")
653491d8ee7SSantosh Puranik     {
654491d8ee7SSantosh Puranik         return "Hdd";
655491d8ee7SSantosh Puranik     }
656491d8ee7SSantosh Puranik     else if (dbusSource ==
657a71dc0b7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia")
658491d8ee7SSantosh Puranik     {
659491d8ee7SSantosh Puranik         return "Cd";
660491d8ee7SSantosh Puranik     }
661491d8ee7SSantosh Puranik     else if (dbusSource ==
662491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.Network")
663491d8ee7SSantosh Puranik     {
664491d8ee7SSantosh Puranik         return "Pxe";
665491d8ee7SSantosh Puranik     }
6669f16b2c1SJennifer Lee     else if (dbusSource ==
667944ffaf9SJohnathan Mantey              "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia")
6689f16b2c1SJennifer Lee     {
6699f16b2c1SJennifer Lee         return "Usb";
6709f16b2c1SJennifer Lee     }
671491d8ee7SSantosh Puranik     else
672491d8ee7SSantosh Puranik     {
673491d8ee7SSantosh Puranik         return "";
674491d8ee7SSantosh Puranik     }
675491d8ee7SSantosh Puranik }
676491d8ee7SSantosh Puranik 
677491d8ee7SSantosh Puranik /**
678491d8ee7SSantosh Puranik  * @brief Traslates boot mode DBUS property value to redfish.
679491d8ee7SSantosh Puranik  *
680491d8ee7SSantosh Puranik  * @param[in] dbusMode    The boot mode in DBUS speak.
681491d8ee7SSantosh Puranik  *
682491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode in Redfish terms. If translation
683491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
684491d8ee7SSantosh Puranik  */
685491d8ee7SSantosh Puranik static std::string dbusToRfBootMode(const std::string &dbusMode)
686491d8ee7SSantosh Puranik {
687491d8ee7SSantosh Puranik     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
688491d8ee7SSantosh Puranik     {
689491d8ee7SSantosh Puranik         return "None";
690491d8ee7SSantosh Puranik     }
691491d8ee7SSantosh Puranik     else if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe")
692491d8ee7SSantosh Puranik     {
693491d8ee7SSantosh Puranik         return "Diags";
694491d8ee7SSantosh Puranik     }
695491d8ee7SSantosh Puranik     else if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup")
696491d8ee7SSantosh Puranik     {
697491d8ee7SSantosh Puranik         return "BiosSetup";
698491d8ee7SSantosh Puranik     }
699491d8ee7SSantosh Puranik     else
700491d8ee7SSantosh Puranik     {
701491d8ee7SSantosh Puranik         return "";
702491d8ee7SSantosh Puranik     }
703491d8ee7SSantosh Puranik }
704491d8ee7SSantosh Puranik 
705491d8ee7SSantosh Puranik /**
706944ffaf9SJohnathan Mantey  * @brief Traslates boot source from Redfish to the DBus boot paths.
707491d8ee7SSantosh Puranik  *
708491d8ee7SSantosh Puranik  * @param[in] rfSource    The boot source in Redfish.
709944ffaf9SJohnathan Mantey  * @param[out] bootSource The DBus source
710944ffaf9SJohnathan Mantey  * @param[out] bootMode   the DBus boot mode
711491d8ee7SSantosh Puranik  *
712944ffaf9SJohnathan Mantey  * @return Integer error code.
713491d8ee7SSantosh Puranik  */
714944ffaf9SJohnathan Mantey static int assignBootParameters(std::shared_ptr<AsyncResp> aResp,
715944ffaf9SJohnathan Mantey                                 const std::string &rfSource,
716944ffaf9SJohnathan Mantey                                 std::string &bootSource, std::string &bootMode)
717491d8ee7SSantosh Puranik {
718944ffaf9SJohnathan Mantey     // The caller has initialized the bootSource and bootMode to:
719944ffaf9SJohnathan Mantey     // bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
720944ffaf9SJohnathan Mantey     // bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
721944ffaf9SJohnathan Mantey     // Only modify the bootSource/bootMode variable needed to achieve the
722944ffaf9SJohnathan Mantey     // desired boot action.
723944ffaf9SJohnathan Mantey 
724491d8ee7SSantosh Puranik     if (rfSource == "None")
725491d8ee7SSantosh Puranik     {
726944ffaf9SJohnathan Mantey         return 0;
727491d8ee7SSantosh Puranik     }
728491d8ee7SSantosh Puranik     else if (rfSource == "Pxe")
729491d8ee7SSantosh Puranik     {
730944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Network";
731944ffaf9SJohnathan Mantey     }
732944ffaf9SJohnathan Mantey     else if (rfSource == "Hdd")
733944ffaf9SJohnathan Mantey     {
734944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Disk";
735944ffaf9SJohnathan Mantey     }
736944ffaf9SJohnathan Mantey     else if (rfSource == "Diags")
737944ffaf9SJohnathan Mantey     {
738944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe";
739944ffaf9SJohnathan Mantey     }
740944ffaf9SJohnathan Mantey     else if (rfSource == "Cd")
741944ffaf9SJohnathan Mantey     {
742944ffaf9SJohnathan Mantey         bootSource =
743944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia";
744944ffaf9SJohnathan Mantey     }
745944ffaf9SJohnathan Mantey     else if (rfSource == "BiosSetup")
746944ffaf9SJohnathan Mantey     {
747944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup";
748491d8ee7SSantosh Puranik     }
7499f16b2c1SJennifer Lee     else if (rfSource == "Usb")
7509f16b2c1SJennifer Lee     {
751944ffaf9SJohnathan Mantey         bootSource =
752944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia";
7539f16b2c1SJennifer Lee     }
754491d8ee7SSantosh Puranik     else
755491d8ee7SSantosh Puranik     {
756944ffaf9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "Invalid property value for "
757944ffaf9SJohnathan Mantey                             "BootSourceOverrideTarget: "
758944ffaf9SJohnathan Mantey                          << bootSource;
759944ffaf9SJohnathan Mantey         messages::propertyValueNotInList(aResp->res, rfSource,
760944ffaf9SJohnathan Mantey                                          "BootSourceTargetOverride");
761944ffaf9SJohnathan Mantey         return -1;
762491d8ee7SSantosh Puranik     }
763944ffaf9SJohnathan Mantey     return 0;
764491d8ee7SSantosh Puranik }
765491d8ee7SSantosh Puranik 
766491d8ee7SSantosh Puranik /**
767491d8ee7SSantosh Puranik  * @brief Retrieves boot mode over DBUS and fills out the response
768491d8ee7SSantosh Puranik  *
769491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
770491d8ee7SSantosh Puranik  * @param[in] bootDbusObj   The dbus object to query for boot properties.
771491d8ee7SSantosh Puranik  *
772491d8ee7SSantosh Puranik  * @return None.
773491d8ee7SSantosh Puranik  */
774491d8ee7SSantosh Puranik static void getBootMode(std::shared_ptr<AsyncResp> aResp,
775491d8ee7SSantosh Puranik                         std::string bootDbusObj)
776491d8ee7SSantosh Puranik {
777491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
778491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec,
779491d8ee7SSantosh Puranik                 const std::variant<std::string> &bootMode) {
780491d8ee7SSantosh Puranik             if (ec)
781491d8ee7SSantosh Puranik             {
782491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
783491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
784491d8ee7SSantosh Puranik                 return;
785491d8ee7SSantosh Puranik             }
786491d8ee7SSantosh Puranik 
787491d8ee7SSantosh Puranik             const std::string *bootModeStr =
788491d8ee7SSantosh Puranik                 std::get_if<std::string>(&bootMode);
789491d8ee7SSantosh Puranik 
790491d8ee7SSantosh Puranik             if (!bootModeStr)
791491d8ee7SSantosh Puranik             {
792491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
793491d8ee7SSantosh Puranik                 return;
794491d8ee7SSantosh Puranik             }
795491d8ee7SSantosh Puranik 
796491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot mode: " << *bootModeStr;
797491d8ee7SSantosh Puranik 
798491d8ee7SSantosh Puranik             // TODO (Santosh): Do we need to support override mode?
799491d8ee7SSantosh Puranik             aResp->res.jsonValue["Boot"]["BootSourceOverrideMode"] = "Legacy";
800491d8ee7SSantosh Puranik             aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget@Redfish."
801491d8ee7SSantosh Puranik                                          "AllowableValues"] = {
802944ffaf9SJohnathan Mantey                 "None", "Pxe", "Hdd", "Cd", "Diags", "BiosSetup", "Usb"};
803491d8ee7SSantosh Puranik 
804491d8ee7SSantosh Puranik             if (*bootModeStr !=
805491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
806491d8ee7SSantosh Puranik             {
807491d8ee7SSantosh Puranik                 auto rfMode = dbusToRfBootMode(*bootModeStr);
808491d8ee7SSantosh Puranik                 if (!rfMode.empty())
809491d8ee7SSantosh Puranik                 {
810491d8ee7SSantosh Puranik                     aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
811491d8ee7SSantosh Puranik                         rfMode;
812491d8ee7SSantosh Puranik                 }
813491d8ee7SSantosh Puranik             }
814491d8ee7SSantosh Puranik 
815491d8ee7SSantosh Puranik             // If the BootSourceOverrideTarget is still "None" at the end,
816491d8ee7SSantosh Puranik             // reset the BootSourceOverrideEnabled to indicate that
817491d8ee7SSantosh Puranik             // overrides are disabled
818491d8ee7SSantosh Puranik             if (aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] ==
819491d8ee7SSantosh Puranik                 "None")
820491d8ee7SSantosh Puranik             {
821491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
822491d8ee7SSantosh Puranik                     "Disabled";
823491d8ee7SSantosh Puranik             }
824491d8ee7SSantosh Puranik         },
825491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootDbusObj,
826491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
827491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode");
828491d8ee7SSantosh Puranik }
829491d8ee7SSantosh Puranik 
830491d8ee7SSantosh Puranik /**
831491d8ee7SSantosh Puranik  * @brief Retrieves boot source over DBUS
832491d8ee7SSantosh Puranik  *
833491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
834491d8ee7SSantosh Puranik  * @param[in] oneTimeEnable Boolean to indicate boot properties are one-time.
835491d8ee7SSantosh Puranik  *
836491d8ee7SSantosh Puranik  * @return None.
837491d8ee7SSantosh Puranik  */
838491d8ee7SSantosh Puranik static void getBootSource(std::shared_ptr<AsyncResp> aResp, bool oneTimeEnabled)
839491d8ee7SSantosh Puranik {
840491d8ee7SSantosh Puranik     std::string bootDbusObj =
841491d8ee7SSantosh Puranik         oneTimeEnabled ? "/xyz/openbmc_project/control/host0/boot/one_time"
842491d8ee7SSantosh Puranik                        : "/xyz/openbmc_project/control/host0/boot";
843491d8ee7SSantosh Puranik 
844491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Is one time: " << oneTimeEnabled;
845491d8ee7SSantosh Puranik     aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
846491d8ee7SSantosh Puranik         (oneTimeEnabled) ? "Once" : "Continuous";
847491d8ee7SSantosh Puranik 
848491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
849491d8ee7SSantosh Puranik         [aResp, bootDbusObj](const boost::system::error_code ec,
850491d8ee7SSantosh Puranik                              const std::variant<std::string> &bootSource) {
851491d8ee7SSantosh Puranik             if (ec)
852491d8ee7SSantosh Puranik             {
853491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
854491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
855491d8ee7SSantosh Puranik                 return;
856491d8ee7SSantosh Puranik             }
857491d8ee7SSantosh Puranik 
858491d8ee7SSantosh Puranik             const std::string *bootSourceStr =
859491d8ee7SSantosh Puranik                 std::get_if<std::string>(&bootSource);
860491d8ee7SSantosh Puranik 
861491d8ee7SSantosh Puranik             if (!bootSourceStr)
862491d8ee7SSantosh Puranik             {
863491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
864491d8ee7SSantosh Puranik                 return;
865491d8ee7SSantosh Puranik             }
866491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot source: " << *bootSourceStr;
867491d8ee7SSantosh Puranik 
868491d8ee7SSantosh Puranik             auto rfSource = dbusToRfBootSource(*bootSourceStr);
869491d8ee7SSantosh Puranik             if (!rfSource.empty())
870491d8ee7SSantosh Puranik             {
871491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
872491d8ee7SSantosh Puranik                     rfSource;
873491d8ee7SSantosh Puranik             }
874491d8ee7SSantosh Puranik         },
875491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootDbusObj,
876491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
877491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource");
878491d8ee7SSantosh Puranik     getBootMode(std::move(aResp), std::move(bootDbusObj));
879491d8ee7SSantosh Puranik }
880491d8ee7SSantosh Puranik 
881491d8ee7SSantosh Puranik /**
882491d8ee7SSantosh Puranik  * @brief Retrieves "One time" enabled setting over DBUS and calls function to
883491d8ee7SSantosh Puranik  * get boot source and boot mode.
884491d8ee7SSantosh Puranik  *
885491d8ee7SSantosh Puranik  * @param[in] aResp     Shared pointer for generating response message.
886491d8ee7SSantosh Puranik  *
887491d8ee7SSantosh Puranik  * @return None.
888491d8ee7SSantosh Puranik  */
889491d8ee7SSantosh Puranik static void getBootProperties(std::shared_ptr<AsyncResp> aResp)
890491d8ee7SSantosh Puranik {
891491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Get boot information.";
892491d8ee7SSantosh Puranik 
893491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
894c5d03ff4SJennifer Lee         [aResp](const boost::system::error_code ec,
895491d8ee7SSantosh Puranik                 const sdbusplus::message::variant<bool> &oneTime) {
896491d8ee7SSantosh Puranik             if (ec)
897491d8ee7SSantosh Puranik             {
898491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
8992a833c77SJames Feist                 // not an error, don't have to have the interface
900491d8ee7SSantosh Puranik                 return;
901491d8ee7SSantosh Puranik             }
902491d8ee7SSantosh Puranik 
903491d8ee7SSantosh Puranik             const bool *oneTimePtr = std::get_if<bool>(&oneTime);
904491d8ee7SSantosh Puranik 
905491d8ee7SSantosh Puranik             if (!oneTimePtr)
906491d8ee7SSantosh Puranik             {
907491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
908491d8ee7SSantosh Puranik                 return;
909491d8ee7SSantosh Puranik             }
910491d8ee7SSantosh Puranik             getBootSource(aResp, *oneTimePtr);
911491d8ee7SSantosh Puranik         },
912491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
913491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
914491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
915491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled");
916491d8ee7SSantosh Puranik }
917491d8ee7SSantosh Puranik 
918491d8ee7SSantosh Puranik /**
919491d8ee7SSantosh Puranik  * @brief Sets boot properties into DBUS object(s).
920491d8ee7SSantosh Puranik  *
921491d8ee7SSantosh Puranik  * @param[in] aResp           Shared pointer for generating response message.
922491d8ee7SSantosh Puranik  * @param[in] oneTimeEnabled  Is "one-time" setting already enabled.
923491d8ee7SSantosh Puranik  * @param[in] bootSource      The boot source to set.
924491d8ee7SSantosh Puranik  * @param[in] bootEnable      The source override "enable" to set.
925491d8ee7SSantosh Puranik  *
926265c1602SJohnathan Mantey  * @return Integer error code.
927491d8ee7SSantosh Puranik  */
928491d8ee7SSantosh Puranik static void setBootModeOrSource(std::shared_ptr<AsyncResp> aResp,
929491d8ee7SSantosh Puranik                                 bool oneTimeEnabled,
930491d8ee7SSantosh Puranik                                 std::optional<std::string> bootSource,
931491d8ee7SSantosh Puranik                                 std::optional<std::string> bootEnable)
932491d8ee7SSantosh Puranik {
933944ffaf9SJohnathan Mantey     std::string bootSourceStr =
934944ffaf9SJohnathan Mantey         "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
935944ffaf9SJohnathan Mantey     std::string bootModeStr =
936944ffaf9SJohnathan Mantey         "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
937491d8ee7SSantosh Puranik     bool oneTimeSetting = oneTimeEnabled;
938944ffaf9SJohnathan Mantey     bool useBootSource = true;
939944ffaf9SJohnathan Mantey 
940491d8ee7SSantosh Puranik     // Validate incoming parameters
941491d8ee7SSantosh Puranik     if (bootEnable)
942491d8ee7SSantosh Puranik     {
943491d8ee7SSantosh Puranik         if (*bootEnable == "Once")
944491d8ee7SSantosh Puranik         {
945491d8ee7SSantosh Puranik             oneTimeSetting = true;
946491d8ee7SSantosh Puranik         }
947491d8ee7SSantosh Puranik         else if (*bootEnable == "Continuous")
948491d8ee7SSantosh Puranik         {
949491d8ee7SSantosh Puranik             oneTimeSetting = false;
950491d8ee7SSantosh Puranik         }
951491d8ee7SSantosh Puranik         else if (*bootEnable == "Disabled")
952491d8ee7SSantosh Puranik         {
953944ffaf9SJohnathan Mantey             BMCWEB_LOG_DEBUG << "Boot source override will be disabled";
954491d8ee7SSantosh Puranik             oneTimeSetting = false;
955944ffaf9SJohnathan Mantey             useBootSource = false;
956491d8ee7SSantosh Puranik         }
957491d8ee7SSantosh Puranik         else
958491d8ee7SSantosh Puranik         {
959491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Unsupported value for "
960491d8ee7SSantosh Puranik                                 "BootSourceOverrideEnabled: "
961491d8ee7SSantosh Puranik                              << *bootEnable;
962491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootEnable,
963491d8ee7SSantosh Puranik                                              "BootSourceOverrideEnabled");
964491d8ee7SSantosh Puranik             return;
965491d8ee7SSantosh Puranik         }
966491d8ee7SSantosh Puranik     }
967491d8ee7SSantosh Puranik 
968944ffaf9SJohnathan Mantey     if (bootSource && useBootSource)
969491d8ee7SSantosh Puranik     {
970491d8ee7SSantosh Puranik         // Source target specified
971491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot source: " << *bootSource;
972491d8ee7SSantosh Puranik         // Figure out which DBUS interface and property to use
973944ffaf9SJohnathan Mantey         if (assignBootParameters(aResp, *bootSource, bootSourceStr,
974944ffaf9SJohnathan Mantey                                  bootModeStr))
975491d8ee7SSantosh Puranik         {
976944ffaf9SJohnathan Mantey             BMCWEB_LOG_DEBUG
977944ffaf9SJohnathan Mantey                 << "Invalid property value for BootSourceOverrideTarget: "
978491d8ee7SSantosh Puranik                 << *bootSource;
979491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootSource,
980491d8ee7SSantosh Puranik                                              "BootSourceTargetOverride");
981491d8ee7SSantosh Puranik             return;
982491d8ee7SSantosh Puranik         }
983944ffaf9SJohnathan Mantey     }
984491d8ee7SSantosh Puranik 
985944ffaf9SJohnathan Mantey     // Act on validated parameters
986944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
987944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
988944ffaf9SJohnathan Mantey     const char *bootObj =
989944ffaf9SJohnathan Mantey         oneTimeSetting ? "/xyz/openbmc_project/control/host0/boot/one_time"
990944ffaf9SJohnathan Mantey                        : "/xyz/openbmc_project/control/host0/boot";
991944ffaf9SJohnathan Mantey 
992491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
993491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec) {
994491d8ee7SSantosh Puranik             if (ec)
995491d8ee7SSantosh Puranik             {
996491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
997491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
998491d8ee7SSantosh Puranik                 return;
999491d8ee7SSantosh Puranik             }
1000491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot source update done.";
1001491d8ee7SSantosh Puranik         },
1002491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootObj,
1003491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1004491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource",
1005491d8ee7SSantosh Puranik         std::variant<std::string>(bootSourceStr));
1006944ffaf9SJohnathan Mantey 
1007491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1008491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec) {
1009491d8ee7SSantosh Puranik             if (ec)
1010491d8ee7SSantosh Puranik             {
1011491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1012491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1013491d8ee7SSantosh Puranik                 return;
1014491d8ee7SSantosh Puranik             }
1015491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot mode update done.";
1016491d8ee7SSantosh Puranik         },
1017491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootObj,
1018491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1019491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
1020491d8ee7SSantosh Puranik         std::variant<std::string>(bootModeStr));
1021944ffaf9SJohnathan Mantey 
1022491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1023491d8ee7SSantosh Puranik         [aResp{std::move(aResp)}](const boost::system::error_code ec) {
1024491d8ee7SSantosh Puranik             if (ec)
1025491d8ee7SSantosh Puranik             {
1026491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1027491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1028491d8ee7SSantosh Puranik                 return;
1029491d8ee7SSantosh Puranik             }
1030491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot enable update done.";
1031491d8ee7SSantosh Puranik         },
1032491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
1033491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
1034491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1035491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled",
1036491d8ee7SSantosh Puranik         std::variant<bool>(oneTimeSetting));
1037491d8ee7SSantosh Puranik }
1038491d8ee7SSantosh Puranik 
1039491d8ee7SSantosh Puranik /**
1040491d8ee7SSantosh Puranik  * @brief Retrieves "One time" enabled setting over DBUS and calls function to
1041491d8ee7SSantosh Puranik  * set boot source/boot mode properties.
1042491d8ee7SSantosh Puranik  *
1043491d8ee7SSantosh Puranik  * @param[in] aResp      Shared pointer for generating response message.
1044491d8ee7SSantosh Puranik  * @param[in] bootSource The boot source from incoming RF request.
1045491d8ee7SSantosh Puranik  * @param[in] bootEnable The boot override enable from incoming RF request.
1046491d8ee7SSantosh Puranik  *
1047265c1602SJohnathan Mantey  * @return Integer error code.
1048491d8ee7SSantosh Puranik  */
1049491d8ee7SSantosh Puranik static void setBootProperties(std::shared_ptr<AsyncResp> aResp,
1050491d8ee7SSantosh Puranik                               std::optional<std::string> bootSource,
1051491d8ee7SSantosh Puranik                               std::optional<std::string> bootEnable)
1052491d8ee7SSantosh Puranik {
1053491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set boot information.";
1054491d8ee7SSantosh Puranik 
1055491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1056265c1602SJohnathan Mantey         [aResp, bootSource{std::move(bootSource)},
1057491d8ee7SSantosh Puranik          bootEnable{std::move(bootEnable)}](
1058491d8ee7SSantosh Puranik             const boost::system::error_code ec,
1059491d8ee7SSantosh Puranik             const sdbusplus::message::variant<bool> &oneTime) {
1060491d8ee7SSantosh Puranik             if (ec)
1061491d8ee7SSantosh Puranik             {
1062491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1063491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1064491d8ee7SSantosh Puranik                 return;
1065491d8ee7SSantosh Puranik             }
1066491d8ee7SSantosh Puranik 
1067491d8ee7SSantosh Puranik             const bool *oneTimePtr = std::get_if<bool>(&oneTime);
1068491d8ee7SSantosh Puranik 
1069491d8ee7SSantosh Puranik             if (!oneTimePtr)
1070491d8ee7SSantosh Puranik             {
1071491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1072491d8ee7SSantosh Puranik                 return;
1073491d8ee7SSantosh Puranik             }
1074491d8ee7SSantosh Puranik 
1075491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Got one time: " << *oneTimePtr;
1076491d8ee7SSantosh Puranik 
1077491d8ee7SSantosh Puranik             setBootModeOrSource(aResp, *oneTimePtr, std::move(bootSource),
1078491d8ee7SSantosh Puranik                                 std::move(bootEnable));
1079491d8ee7SSantosh Puranik         },
1080491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
1081491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
1082491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
1083491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled");
1084491d8ee7SSantosh Puranik }
1085491d8ee7SSantosh Puranik 
1086a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
1087a6349918SAppaRao Puli /**
1088a6349918SAppaRao Puli  * @brief Retrieves provisioning status
1089a6349918SAppaRao Puli  *
1090a6349918SAppaRao Puli  * @param[in] aResp     Shared pointer for completing asynchronous calls.
1091a6349918SAppaRao Puli  *
1092a6349918SAppaRao Puli  * @return None.
1093a6349918SAppaRao Puli  */
1094a6349918SAppaRao Puli void getProvisioningStatus(std::shared_ptr<AsyncResp> aResp)
1095a6349918SAppaRao Puli {
1096a6349918SAppaRao Puli     BMCWEB_LOG_DEBUG << "Get OEM information.";
1097a6349918SAppaRao Puli     crow::connections::systemBus->async_method_call(
1098a6349918SAppaRao Puli         [aResp](const boost::system::error_code ec,
1099a6349918SAppaRao Puli                 const std::vector<std::pair<std::string, VariantType>>
1100a6349918SAppaRao Puli                     &propertiesList) {
1101a6349918SAppaRao Puli             if (ec)
1102a6349918SAppaRao Puli             {
1103a6349918SAppaRao Puli                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1104a6349918SAppaRao Puli                 messages::internalError(aResp->res);
1105a6349918SAppaRao Puli                 return;
1106a6349918SAppaRao Puli             }
1107a6349918SAppaRao Puli 
1108a6349918SAppaRao Puli             const bool *provState = nullptr;
1109a6349918SAppaRao Puli             const bool *lockState = nullptr;
1110a6349918SAppaRao Puli             for (const std::pair<std::string, VariantType> &property :
1111a6349918SAppaRao Puli                  propertiesList)
1112a6349918SAppaRao Puli             {
1113a6349918SAppaRao Puli                 if (property.first == "UfmProvisioned")
1114a6349918SAppaRao Puli                 {
1115a6349918SAppaRao Puli                     provState = std::get_if<bool>(&property.second);
1116a6349918SAppaRao Puli                 }
1117a6349918SAppaRao Puli                 else if (property.first == "UfmLocked")
1118a6349918SAppaRao Puli                 {
1119a6349918SAppaRao Puli                     lockState = std::get_if<bool>(&property.second);
1120a6349918SAppaRao Puli                 }
1121a6349918SAppaRao Puli             }
1122a6349918SAppaRao Puli 
1123a6349918SAppaRao Puli             if ((provState == nullptr) || (lockState == nullptr))
1124a6349918SAppaRao Puli             {
1125a6349918SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Unable to get PFR attributes.";
1126a6349918SAppaRao Puli                 messages::internalError(aResp->res);
1127a6349918SAppaRao Puli                 return;
1128a6349918SAppaRao Puli             }
1129a6349918SAppaRao Puli 
1130a6349918SAppaRao Puli             nlohmann::json &oemPFR =
1131a6349918SAppaRao Puli                 aResp->res.jsonValue["Oem"]["OpenBmc"]["FirmwareProvisioning"];
1132a6349918SAppaRao Puli             if (*provState == true)
1133a6349918SAppaRao Puli             {
1134a6349918SAppaRao Puli                 if (*lockState == true)
1135a6349918SAppaRao Puli                 {
1136a6349918SAppaRao Puli                     oemPFR["ProvisioningStatus"] = "ProvisionedAndLocked";
1137a6349918SAppaRao Puli                 }
1138a6349918SAppaRao Puli                 else
1139a6349918SAppaRao Puli                 {
1140a6349918SAppaRao Puli                     oemPFR["ProvisioningStatus"] = "ProvisionedButNotLocked";
1141a6349918SAppaRao Puli                 }
1142a6349918SAppaRao Puli             }
1143a6349918SAppaRao Puli             else
1144a6349918SAppaRao Puli             {
1145a6349918SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "NotProvisioned";
1146a6349918SAppaRao Puli             }
1147a6349918SAppaRao Puli         },
1148a6349918SAppaRao Puli         "xyz.openbmc_project.PFR.Manager", "/xyz/openbmc_project/pfr",
1149a6349918SAppaRao Puli         "org.freedesktop.DBus.Properties", "GetAll",
1150a6349918SAppaRao Puli         "xyz.openbmc_project.PFR.Attributes");
1151a6349918SAppaRao Puli }
1152a6349918SAppaRao Puli #endif
1153a6349918SAppaRao Puli 
1154491d8ee7SSantosh Puranik /**
115551709ffdSYong Li  * @brief Translates watchdog timeout action DBUS property value to redfish.
115651709ffdSYong Li  *
115751709ffdSYong Li  * @param[in] dbusAction    The watchdog timeout action in D-BUS.
115851709ffdSYong Li  *
115951709ffdSYong Li  * @return Returns as a string, the timeout action in Redfish terms. If
116051709ffdSYong Li  * translation cannot be done, returns an empty string.
116151709ffdSYong Li  */
116251709ffdSYong Li static std::string dbusToRfWatchdogAction(const std::string &dbusAction)
116351709ffdSYong Li {
116451709ffdSYong Li     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.None")
116551709ffdSYong Li     {
116651709ffdSYong Li         return "None";
116751709ffdSYong Li     }
116851709ffdSYong Li     else if (dbusAction ==
116951709ffdSYong Li              "xyz.openbmc_project.State.Watchdog.Action.HardReset")
117051709ffdSYong Li     {
117151709ffdSYong Li         return "ResetSystem";
117251709ffdSYong Li     }
117351709ffdSYong Li     else if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerOff")
117451709ffdSYong Li     {
117551709ffdSYong Li         return "PowerDown";
117651709ffdSYong Li     }
117751709ffdSYong Li     else if (dbusAction ==
117851709ffdSYong Li              "xyz.openbmc_project.State.Watchdog.Action.PowerCycle")
117951709ffdSYong Li     {
118051709ffdSYong Li         return "PowerCycle";
118151709ffdSYong Li     }
118251709ffdSYong Li 
118351709ffdSYong Li     return "";
118451709ffdSYong Li }
118551709ffdSYong Li 
118651709ffdSYong Li /**
1187c45f0082SYong Li  *@brief Translates timeout action from Redfish to DBUS property value.
1188c45f0082SYong Li  *
1189c45f0082SYong Li  *@param[in] rfAction The timeout action in Redfish.
1190c45f0082SYong Li  *
1191c45f0082SYong Li  *@return Returns as a string, the time_out action as expected by DBUS.
1192c45f0082SYong Li  *If translation cannot be done, returns an empty string.
1193c45f0082SYong Li  */
1194c45f0082SYong Li 
1195c45f0082SYong Li static std::string rfToDbusWDTTimeOutAct(const std::string &rfAction)
1196c45f0082SYong Li {
1197c45f0082SYong Li     if (rfAction == "None")
1198c45f0082SYong Li     {
1199c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.None";
1200c45f0082SYong Li     }
1201c45f0082SYong Li     else if (rfAction == "PowerCycle")
1202c45f0082SYong Li     {
1203c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerCycle";
1204c45f0082SYong Li     }
1205c45f0082SYong Li     else if (rfAction == "PowerDown")
1206c45f0082SYong Li     {
1207c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerOff";
1208c45f0082SYong Li     }
1209c45f0082SYong Li     else if (rfAction == "ResetSystem")
1210c45f0082SYong Li     {
1211c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.HardReset";
1212c45f0082SYong Li     }
1213c45f0082SYong Li 
1214c45f0082SYong Li     return "";
1215c45f0082SYong Li }
1216c45f0082SYong Li 
1217c45f0082SYong Li /**
121851709ffdSYong Li  * @brief Retrieves host watchdog timer properties over DBUS
121951709ffdSYong Li  *
122051709ffdSYong Li  * @param[in] aResp     Shared pointer for completing asynchronous calls.
122151709ffdSYong Li  *
122251709ffdSYong Li  * @return None.
122351709ffdSYong Li  */
122451709ffdSYong Li void getHostWatchdogTimer(std::shared_ptr<AsyncResp> aResp)
122551709ffdSYong Li {
122651709ffdSYong Li     BMCWEB_LOG_DEBUG << "Get host watchodg";
122751709ffdSYong Li     crow::connections::systemBus->async_method_call(
122851709ffdSYong Li         [aResp](const boost::system::error_code ec,
122951709ffdSYong Li                 PropertiesType &properties) {
123051709ffdSYong Li             if (ec)
123151709ffdSYong Li             {
123251709ffdSYong Li                 // watchdog service is stopped
123351709ffdSYong Li                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
123451709ffdSYong Li                 return;
123551709ffdSYong Li             }
123651709ffdSYong Li 
123751709ffdSYong Li             BMCWEB_LOG_DEBUG << "Got " << properties.size() << " wdt prop.";
123851709ffdSYong Li 
123951709ffdSYong Li             nlohmann::json &hostWatchdogTimer =
124051709ffdSYong Li                 aResp->res.jsonValue["HostWatchdogTimer"];
124151709ffdSYong Li 
124251709ffdSYong Li             // watchdog service is running/enabled
124351709ffdSYong Li             hostWatchdogTimer["Status"]["State"] = "Enabled";
124451709ffdSYong Li 
124551709ffdSYong Li             for (const auto &property : properties)
124651709ffdSYong Li             {
124751709ffdSYong Li                 BMCWEB_LOG_DEBUG << "prop=" << property.first;
124851709ffdSYong Li                 if (property.first == "Enabled")
124951709ffdSYong Li                 {
125051709ffdSYong Li                     const bool *state = std::get_if<bool>(&property.second);
125151709ffdSYong Li 
125251709ffdSYong Li                     if (!state)
125351709ffdSYong Li                     {
125451709ffdSYong Li                         messages::internalError(aResp->res);
125551709ffdSYong Li                         continue;
125651709ffdSYong Li                     }
125751709ffdSYong Li 
125851709ffdSYong Li                     hostWatchdogTimer["FunctionEnabled"] = *state;
125951709ffdSYong Li                 }
126051709ffdSYong Li                 else if (property.first == "ExpireAction")
126151709ffdSYong Li                 {
126251709ffdSYong Li                     const std::string *s =
126351709ffdSYong Li                         std::get_if<std::string>(&property.second);
126451709ffdSYong Li                     if (!s)
126551709ffdSYong Li                     {
126651709ffdSYong Li                         messages::internalError(aResp->res);
126751709ffdSYong Li                         continue;
126851709ffdSYong Li                     }
126951709ffdSYong Li 
127051709ffdSYong Li                     std::string action = dbusToRfWatchdogAction(*s);
127151709ffdSYong Li                     if (action.empty())
127251709ffdSYong Li                     {
127351709ffdSYong Li                         messages::internalError(aResp->res);
127451709ffdSYong Li                         continue;
127551709ffdSYong Li                     }
127651709ffdSYong Li                     hostWatchdogTimer["TimeoutAction"] = action;
127751709ffdSYong Li                 }
127851709ffdSYong Li             }
127951709ffdSYong Li         },
128051709ffdSYong Li         "xyz.openbmc_project.Watchdog", "/xyz/openbmc_project/watchdog/host0",
128151709ffdSYong Li         "org.freedesktop.DBus.Properties", "GetAll",
128251709ffdSYong Li         "xyz.openbmc_project.State.Watchdog");
128351709ffdSYong Li }
128451709ffdSYong Li 
128551709ffdSYong Li /**
1286c45f0082SYong Li  * @brief Sets Host WatchDog Timer properties.
1287c45f0082SYong Li  *
1288c45f0082SYong Li  * @param[in] aResp      Shared pointer for generating response message.
1289c45f0082SYong Li  * @param[in] wdtEnable  The WDTimer Enable value (true/false) from incoming
1290c45f0082SYong Li  *                       RF request.
1291c45f0082SYong Li  * @param[in] wdtTimeOutAction The WDT Timeout action, from incoming RF request.
1292c45f0082SYong Li  *
1293c45f0082SYong Li  * @return None.
1294c45f0082SYong Li  */
1295c45f0082SYong Li static void setWDTProperties(std::shared_ptr<AsyncResp> aResp,
1296c45f0082SYong Li                              const std::optional<bool> wdtEnable,
1297c45f0082SYong Li                              const std::optional<std::string> &wdtTimeOutAction)
1298c45f0082SYong Li {
1299c45f0082SYong Li     BMCWEB_LOG_DEBUG << "Set host watchdog";
1300c45f0082SYong Li 
1301c45f0082SYong Li     if (wdtTimeOutAction)
1302c45f0082SYong Li     {
1303c45f0082SYong Li         std::string wdtTimeOutActStr = rfToDbusWDTTimeOutAct(*wdtTimeOutAction);
1304c45f0082SYong Li         // check if TimeOut Action is Valid
1305c45f0082SYong Li         if (wdtTimeOutActStr.empty())
1306c45f0082SYong Li         {
1307c45f0082SYong Li             BMCWEB_LOG_DEBUG << "Unsupported value for TimeoutAction: "
1308c45f0082SYong Li                              << *wdtTimeOutAction;
1309c45f0082SYong Li             messages::propertyValueNotInList(aResp->res, *wdtTimeOutAction,
1310c45f0082SYong Li                                              "TimeoutAction");
1311c45f0082SYong Li             return;
1312c45f0082SYong Li         }
1313c45f0082SYong Li 
1314c45f0082SYong Li         crow::connections::systemBus->async_method_call(
1315c45f0082SYong Li             [aResp](const boost::system::error_code ec) {
1316c45f0082SYong Li                 if (ec)
1317c45f0082SYong Li                 {
1318c45f0082SYong Li                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1319c45f0082SYong Li                     messages::internalError(aResp->res);
1320c45f0082SYong Li                     return;
1321c45f0082SYong Li                 }
1322c45f0082SYong Li             },
1323c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
1324c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
1325c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
1326c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "ExpireAction",
1327c45f0082SYong Li             std::variant<std::string>(wdtTimeOutActStr));
1328c45f0082SYong Li     }
1329c45f0082SYong Li 
1330c45f0082SYong Li     if (wdtEnable)
1331c45f0082SYong Li     {
1332c45f0082SYong Li         crow::connections::systemBus->async_method_call(
1333c45f0082SYong Li             [aResp](const boost::system::error_code ec) {
1334c45f0082SYong Li                 if (ec)
1335c45f0082SYong Li                 {
1336c45f0082SYong Li                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1337c45f0082SYong Li                     messages::internalError(aResp->res);
1338c45f0082SYong Li                     return;
1339c45f0082SYong Li                 }
1340c45f0082SYong Li             },
1341c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
1342c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
1343c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
1344c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "Enabled",
1345c45f0082SYong Li             std::variant<bool>(*wdtEnable));
1346c45f0082SYong Li     }
1347c45f0082SYong Li }
1348c45f0082SYong Li 
1349c45f0082SYong Li /**
1350c5b2abe0SLewanczyk, Dawid  * SystemsCollection derived class for delivering ComputerSystems Collection
1351c5b2abe0SLewanczyk, Dawid  * Schema
1352c5b2abe0SLewanczyk, Dawid  */
13531abe55efSEd Tanous class SystemsCollection : public Node
13541abe55efSEd Tanous {
1355c5b2abe0SLewanczyk, Dawid   public:
13561abe55efSEd Tanous     SystemsCollection(CrowApp &app) : Node(app, "/redfish/v1/Systems/")
13571abe55efSEd Tanous     {
1358c5b2abe0SLewanczyk, Dawid         entityPrivileges = {
1359c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::get, {{"Login"}}},
1360c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::head, {{"Login"}}},
1361c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1362c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1363c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1364c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1365c5b2abe0SLewanczyk, Dawid     }
1366c5b2abe0SLewanczyk, Dawid 
1367c5b2abe0SLewanczyk, Dawid   private:
136855c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
13691abe55efSEd Tanous                const std::vector<std::string> &params) override
13701abe55efSEd Tanous     {
13710f74e643SEd Tanous         res.jsonValue["@odata.type"] =
13720f74e643SEd Tanous             "#ComputerSystemCollection.ComputerSystemCollection";
13730f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Systems";
13740f74e643SEd Tanous         res.jsonValue["Name"] = "Computer System Collection";
1375029573d4SEd Tanous         res.jsonValue["Members"] = {
1376029573d4SEd Tanous             {{"@odata.id", "/redfish/v1/Systems/system"}}};
1377029573d4SEd Tanous         res.jsonValue["Members@odata.count"] = 1;
1378029573d4SEd Tanous         res.end();
1379c5b2abe0SLewanczyk, Dawid     }
1380c5b2abe0SLewanczyk, Dawid };
1381c5b2abe0SLewanczyk, Dawid 
1382c5b2abe0SLewanczyk, Dawid /**
1383cc340dd9SEd Tanous  * SystemActionsReset class supports handle POST method for Reset action.
1384cc340dd9SEd Tanous  * The class retrieves and sends data directly to D-Bus.
1385cc340dd9SEd Tanous  */
1386cc340dd9SEd Tanous class SystemActionsReset : public Node
1387cc340dd9SEd Tanous {
1388cc340dd9SEd Tanous   public:
1389cc340dd9SEd Tanous     SystemActionsReset(CrowApp &app) :
1390029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset/")
1391cc340dd9SEd Tanous     {
1392cc340dd9SEd Tanous         entityPrivileges = {
1393cc340dd9SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1394cc340dd9SEd Tanous     }
1395cc340dd9SEd Tanous 
1396cc340dd9SEd Tanous   private:
1397cc340dd9SEd Tanous     /**
1398cc340dd9SEd Tanous      * Function handles POST method request.
1399cc340dd9SEd Tanous      * Analyzes POST body message before sends Reset request data to D-Bus.
1400cc340dd9SEd Tanous      */
1401cc340dd9SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
1402cc340dd9SEd Tanous                 const std::vector<std::string> &params) override
1403cc340dd9SEd Tanous     {
1404cc340dd9SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1405cc340dd9SEd Tanous 
14069712f8acSEd Tanous         std::string resetType;
14079712f8acSEd Tanous         if (!json_util::readJson(req, res, "ResetType", resetType))
1408cc340dd9SEd Tanous         {
1409cc340dd9SEd Tanous             return;
1410cc340dd9SEd Tanous         }
1411cc340dd9SEd Tanous 
1412d22c8396SJason M. Bills         // Get the command and host vs. chassis
1413cc340dd9SEd Tanous         std::string command;
1414d22c8396SJason M. Bills         bool hostCommand;
14159712f8acSEd Tanous         if (resetType == "On")
1416cc340dd9SEd Tanous         {
1417cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.On";
1418d22c8396SJason M. Bills             hostCommand = true;
1419d22c8396SJason M. Bills         }
1420d22c8396SJason M. Bills         else if (resetType == "ForceOff")
1421d22c8396SJason M. Bills         {
1422d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Chassis.Transition.Off";
1423d22c8396SJason M. Bills             hostCommand = false;
1424d22c8396SJason M. Bills         }
1425d22c8396SJason M. Bills         else if (resetType == "ForceOn")
1426d22c8396SJason M. Bills         {
1427d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Host.Transition.On";
1428d22c8396SJason M. Bills             hostCommand = true;
1429d22c8396SJason M. Bills         }
1430d22c8396SJason M. Bills         else if (resetType == "ForceRestart")
1431d22c8396SJason M. Bills         {
143286a0851aSJason M. Bills             command =
143386a0851aSJason M. Bills                 "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot";
143486a0851aSJason M. Bills             hostCommand = true;
1435cc340dd9SEd Tanous         }
14369712f8acSEd Tanous         else if (resetType == "GracefulShutdown")
1437cc340dd9SEd Tanous         {
1438cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.Off";
1439d22c8396SJason M. Bills             hostCommand = true;
1440cc340dd9SEd Tanous         }
14419712f8acSEd Tanous         else if (resetType == "GracefulRestart")
1442cc340dd9SEd Tanous         {
144386a0851aSJason M. Bills             command =
144486a0851aSJason M. Bills                 "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot";
1445d22c8396SJason M. Bills             hostCommand = true;
1446d22c8396SJason M. Bills         }
1447d22c8396SJason M. Bills         else if (resetType == "PowerCycle")
1448d22c8396SJason M. Bills         {
144986a0851aSJason M. Bills             command = "xyz.openbmc_project.State.Host.Transition.Reboot";
145086a0851aSJason M. Bills             hostCommand = true;
1451cc340dd9SEd Tanous         }
1452bfd5b826SLakshminarayana R. Kammath         else if (resetType == "Nmi")
1453bfd5b826SLakshminarayana R. Kammath         {
1454bfd5b826SLakshminarayana R. Kammath             doNMI(asyncResp);
1455bfd5b826SLakshminarayana R. Kammath             return;
1456bfd5b826SLakshminarayana R. Kammath         }
1457cc340dd9SEd Tanous         else
1458cc340dd9SEd Tanous         {
1459f12894f8SJason M. Bills             messages::actionParameterUnknown(res, "Reset", resetType);
1460cc340dd9SEd Tanous             return;
1461cc340dd9SEd Tanous         }
1462cc340dd9SEd Tanous 
1463d22c8396SJason M. Bills         if (hostCommand)
1464d22c8396SJason M. Bills         {
1465cc340dd9SEd Tanous             crow::connections::systemBus->async_method_call(
1466d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
1467cc340dd9SEd Tanous                     if (ec)
1468cc340dd9SEd Tanous                     {
1469cc340dd9SEd Tanous                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1470d22c8396SJason M. Bills                         if (ec.value() == boost::asio::error::invalid_argument)
1471d22c8396SJason M. Bills                         {
1472d22c8396SJason M. Bills                             messages::actionParameterNotSupported(
1473d22c8396SJason M. Bills                                 asyncResp->res, resetType, "Reset");
1474d22c8396SJason M. Bills                         }
1475d22c8396SJason M. Bills                         else
1476d22c8396SJason M. Bills                         {
1477f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
1478d22c8396SJason M. Bills                         }
1479cc340dd9SEd Tanous                         return;
1480cc340dd9SEd Tanous                     }
1481f12894f8SJason M. Bills                     messages::success(asyncResp->res);
1482cc340dd9SEd Tanous                 },
1483cc340dd9SEd Tanous                 "xyz.openbmc_project.State.Host",
1484cc340dd9SEd Tanous                 "/xyz/openbmc_project/state/host0",
1485cc340dd9SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
14869712f8acSEd Tanous                 "xyz.openbmc_project.State.Host", "RequestedHostTransition",
1487abf2add6SEd Tanous                 std::variant<std::string>{command});
1488cc340dd9SEd Tanous         }
1489d22c8396SJason M. Bills         else
1490d22c8396SJason M. Bills         {
1491d22c8396SJason M. Bills             crow::connections::systemBus->async_method_call(
1492d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
1493d22c8396SJason M. Bills                     if (ec)
1494d22c8396SJason M. Bills                     {
1495d22c8396SJason M. Bills                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1496d22c8396SJason M. Bills                         if (ec.value() == boost::asio::error::invalid_argument)
1497d22c8396SJason M. Bills                         {
1498d22c8396SJason M. Bills                             messages::actionParameterNotSupported(
1499d22c8396SJason M. Bills                                 asyncResp->res, resetType, "Reset");
1500d22c8396SJason M. Bills                         }
1501d22c8396SJason M. Bills                         else
1502d22c8396SJason M. Bills                         {
1503d22c8396SJason M. Bills                             messages::internalError(asyncResp->res);
1504d22c8396SJason M. Bills                         }
1505d22c8396SJason M. Bills                         return;
1506d22c8396SJason M. Bills                     }
1507d22c8396SJason M. Bills                     messages::success(asyncResp->res);
1508d22c8396SJason M. Bills                 },
1509d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis",
1510d22c8396SJason M. Bills                 "/xyz/openbmc_project/state/chassis0",
1511d22c8396SJason M. Bills                 "org.freedesktop.DBus.Properties", "Set",
1512d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition",
1513d22c8396SJason M. Bills                 std::variant<std::string>{command});
1514d22c8396SJason M. Bills         }
1515d22c8396SJason M. Bills     }
1516bfd5b826SLakshminarayana R. Kammath     /**
1517bfd5b826SLakshminarayana R. Kammath      * Function transceives data with dbus directly.
1518bfd5b826SLakshminarayana R. Kammath      */
1519bfd5b826SLakshminarayana R. Kammath     void doNMI(const std::shared_ptr<AsyncResp> &asyncResp)
1520bfd5b826SLakshminarayana R. Kammath     {
1521bfd5b826SLakshminarayana R. Kammath         constexpr char const *serviceName =
1522bfd5b826SLakshminarayana R. Kammath             "xyz.openbmc_project.Control.Host.NMI";
1523bfd5b826SLakshminarayana R. Kammath         constexpr char const *objectPath =
1524bfd5b826SLakshminarayana R. Kammath             "/xyz/openbmc_project/control/host0/nmi";
1525bfd5b826SLakshminarayana R. Kammath         constexpr char const *interfaceName =
1526bfd5b826SLakshminarayana R. Kammath             "xyz.openbmc_project.Control.Host.NMI";
1527bfd5b826SLakshminarayana R. Kammath         constexpr char const *method = "NMI";
1528bfd5b826SLakshminarayana R. Kammath 
1529bfd5b826SLakshminarayana R. Kammath         crow::connections::systemBus->async_method_call(
1530bfd5b826SLakshminarayana R. Kammath             [asyncResp](const boost::system::error_code ec) {
1531bfd5b826SLakshminarayana R. Kammath                 if (ec)
1532bfd5b826SLakshminarayana R. Kammath                 {
1533bfd5b826SLakshminarayana R. Kammath                     BMCWEB_LOG_ERROR << " Bad D-Bus request error: " << ec;
1534bfd5b826SLakshminarayana R. Kammath                     messages::internalError(asyncResp->res);
1535bfd5b826SLakshminarayana R. Kammath                     return;
1536bfd5b826SLakshminarayana R. Kammath                 }
1537bfd5b826SLakshminarayana R. Kammath                 messages::success(asyncResp->res);
1538bfd5b826SLakshminarayana R. Kammath             },
1539bfd5b826SLakshminarayana R. Kammath             serviceName, objectPath, interfaceName, method);
1540bfd5b826SLakshminarayana R. Kammath     }
1541cc340dd9SEd Tanous };
1542cc340dd9SEd Tanous 
1543cc340dd9SEd Tanous /**
15446617338dSEd Tanous  * Systems derived class for delivering Computer Systems Schema.
1545c5b2abe0SLewanczyk, Dawid  */
15461abe55efSEd Tanous class Systems : public Node
15471abe55efSEd Tanous {
1548c5b2abe0SLewanczyk, Dawid   public:
1549c5b2abe0SLewanczyk, Dawid     /*
1550c5b2abe0SLewanczyk, Dawid      * Default Constructor
1551c5b2abe0SLewanczyk, Dawid      */
1552029573d4SEd Tanous     Systems(CrowApp &app) : Node(app, "/redfish/v1/Systems/system/")
15531abe55efSEd Tanous     {
1554c5b2abe0SLewanczyk, Dawid         entityPrivileges = {
1555c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::get, {{"Login"}}},
1556c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::head, {{"Login"}}},
1557c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1558c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1559c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1560c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1561c5b2abe0SLewanczyk, Dawid     }
1562c5b2abe0SLewanczyk, Dawid 
1563c5b2abe0SLewanczyk, Dawid   private:
1564c5b2abe0SLewanczyk, Dawid     /**
1565c5b2abe0SLewanczyk, Dawid      * Functions triggers appropriate requests on DBus
1566c5b2abe0SLewanczyk, Dawid      */
156755c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
15681abe55efSEd Tanous                const std::vector<std::string> &params) override
15691abe55efSEd Tanous     {
1570491d8ee7SSantosh Puranik         res.jsonValue["@odata.type"] = "#ComputerSystem.v1_6_0.ComputerSystem";
1571*450a25cbSGunnar Mills         res.jsonValue["Name"] = "system";
1572029573d4SEd Tanous         res.jsonValue["Id"] = "system";
15730f74e643SEd Tanous         res.jsonValue["SystemType"] = "Physical";
15740f74e643SEd Tanous         res.jsonValue["Description"] = "Computer System";
15750f74e643SEd Tanous         res.jsonValue["ProcessorSummary"]["Count"] = 0;
15760f74e643SEd Tanous         res.jsonValue["ProcessorSummary"]["Status"]["State"] = "Disabled";
15775fd7ba65SCheng C Yang         res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] = uint64_t(0);
15780f74e643SEd Tanous         res.jsonValue["MemorySummary"]["Status"]["State"] = "Disabled";
1579029573d4SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Systems/system";
158004a258f4SEd Tanous 
1581443c2934SRapkiewicz, Pawel         res.jsonValue["Processors"] = {
1582029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/Processors"}};
1583443c2934SRapkiewicz, Pawel         res.jsonValue["Memory"] = {
1584029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/Memory"}};
1585a25aeccfSNikhil Potade         res.jsonValue["Storage"] = {
1586a25aeccfSNikhil Potade             {"@odata.id", "/redfish/v1/Systems/system/Storage"}};
1587029573d4SEd Tanous 
1588cc340dd9SEd Tanous         // TODO Need to support ForceRestart.
1589cc340dd9SEd Tanous         res.jsonValue["Actions"]["#ComputerSystem.Reset"] = {
1590cc340dd9SEd Tanous             {"target",
1591029573d4SEd Tanous              "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset"},
1592cc340dd9SEd Tanous             {"ResetType@Redfish.AllowableValues",
1593d22c8396SJason M. Bills              {"On", "ForceOff", "ForceOn", "ForceRestart", "GracefulRestart",
1594bfd5b826SLakshminarayana R. Kammath               "GracefulShutdown", "PowerCycle", "Nmi"}}};
1595c5b2abe0SLewanczyk, Dawid 
1596c4bf6374SJason M. Bills         res.jsonValue["LogServices"] = {
1597029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/LogServices"}};
1598c4bf6374SJason M. Bills 
1599d82a3acdSCarol Wang         res.jsonValue["Bios"] = {
1600d82a3acdSCarol Wang             {"@odata.id", "/redfish/v1/Systems/system/Bios"}};
1601d82a3acdSCarol Wang 
1602c5d03ff4SJennifer Lee         res.jsonValue["Links"]["ManagedBy"] = {
1603c5d03ff4SJennifer Lee             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
1604c5d03ff4SJennifer Lee 
1605c5d03ff4SJennifer Lee         res.jsonValue["Status"] = {
1606c5d03ff4SJennifer Lee             {"Health", "OK"},
1607c5d03ff4SJennifer Lee             {"State", "Enabled"},
1608c5d03ff4SJennifer Lee         };
1609a0803efaSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1610c5b2abe0SLewanczyk, Dawid 
1611e284a7c1SJames Feist         constexpr const std::array<const char *, 4> inventoryForSystems = {
1612b49ac873SJames Feist             "xyz.openbmc_project.Inventory.Item.Dimm",
16132ad9c2f6SJames Feist             "xyz.openbmc_project.Inventory.Item.Cpu",
1614e284a7c1SJames Feist             "xyz.openbmc_project.Inventory.Item.Drive",
1615e284a7c1SJames Feist             "xyz.openbmc_project.Inventory.Item.StorageController"};
1616b49ac873SJames Feist 
1617b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
1618b49ac873SJames Feist         crow::connections::systemBus->async_method_call(
1619b49ac873SJames Feist             [health](const boost::system::error_code ec,
1620b49ac873SJames Feist                      std::vector<std::string> &resp) {
1621b49ac873SJames Feist                 if (ec)
1622b49ac873SJames Feist                 {
1623b49ac873SJames Feist                     // no inventory
1624b49ac873SJames Feist                     return;
1625b49ac873SJames Feist                 }
1626b49ac873SJames Feist 
1627b49ac873SJames Feist                 health->inventory = std::move(resp);
1628b49ac873SJames Feist             },
1629b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper",
1630b49ac873SJames Feist             "/xyz/openbmc_project/object_mapper",
1631b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1632b49ac873SJames Feist             int32_t(0), inventoryForSystems);
1633b49ac873SJames Feist 
1634b49ac873SJames Feist         health->populate();
1635b49ac873SJames Feist 
1636c5d03ff4SJennifer Lee         getMainChassisId(asyncResp, [](const std::string &chassisId,
1637c5d03ff4SJennifer Lee                                        std::shared_ptr<AsyncResp> aRsp) {
1638c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["Chassis"] = {
1639c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
1640c5d03ff4SJennifer Lee         });
1641a3002228SAppaRao Puli 
1642a3002228SAppaRao Puli         getIndicatorLedState(asyncResp);
16435bc2dc8eSJames Feist         getComputerSystem(asyncResp, health);
16446c34de48SEd Tanous         getHostState(asyncResp);
1645491d8ee7SSantosh Puranik         getBootProperties(asyncResp);
1646adbe192aSJason M. Bills         getPCIeDeviceList(asyncResp, "PCIeDevices");
164751709ffdSYong Li         getHostWatchdogTimer(asyncResp);
1648a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
1649a6349918SAppaRao Puli         getProvisioningStatus(asyncResp);
1650a6349918SAppaRao Puli #endif
1651c5b2abe0SLewanczyk, Dawid     }
1652c5b2abe0SLewanczyk, Dawid 
165355c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
16541abe55efSEd Tanous                  const std::vector<std::string> &params) override
16551abe55efSEd Tanous     {
1656cde19e5fSSantosh Puranik         std::optional<std::string> indicatorLed;
1657491d8ee7SSantosh Puranik         std::optional<nlohmann::json> bootProps;
1658c45f0082SYong Li         std::optional<nlohmann::json> wdtTimerProps;
165941352c24SSantosh Puranik         auto asyncResp = std::make_shared<AsyncResp>(res);
166041352c24SSantosh Puranik 
1661944ffaf9SJohnathan Mantey         if (!json_util::readJson(req, res, "IndicatorLED", indicatorLed, "Boot",
1662c45f0082SYong Li                                  bootProps, "WatchdogTimer", wdtTimerProps))
16636617338dSEd Tanous         {
16646617338dSEd Tanous             return;
16656617338dSEd Tanous         }
1666491d8ee7SSantosh Puranik 
1667944ffaf9SJohnathan Mantey         res.result(boost::beast::http::status::no_content);
1668c45f0082SYong Li 
1669c45f0082SYong Li         if (wdtTimerProps)
1670c45f0082SYong Li         {
1671c45f0082SYong Li             std::optional<bool> wdtEnable;
1672c45f0082SYong Li             std::optional<std::string> wdtTimeOutAction;
1673c45f0082SYong Li 
1674c45f0082SYong Li             if (!json_util::readJson(*wdtTimerProps, asyncResp->res,
1675c45f0082SYong Li                                      "FunctionEnabled", wdtEnable,
1676c45f0082SYong Li                                      "TimeoutAction", wdtTimeOutAction))
1677c45f0082SYong Li             {
1678c45f0082SYong Li                 return;
1679c45f0082SYong Li             }
1680c45f0082SYong Li             setWDTProperties(asyncResp, std::move(wdtEnable),
1681c45f0082SYong Li                              std::move(wdtTimeOutAction));
1682c45f0082SYong Li         }
1683c45f0082SYong Li 
1684491d8ee7SSantosh Puranik         if (bootProps)
1685491d8ee7SSantosh Puranik         {
1686491d8ee7SSantosh Puranik             std::optional<std::string> bootSource;
1687491d8ee7SSantosh Puranik             std::optional<std::string> bootEnable;
1688491d8ee7SSantosh Puranik 
1689491d8ee7SSantosh Puranik             if (!json_util::readJson(*bootProps, asyncResp->res,
1690491d8ee7SSantosh Puranik                                      "BootSourceOverrideTarget", bootSource,
1691491d8ee7SSantosh Puranik                                      "BootSourceOverrideEnabled", bootEnable))
1692491d8ee7SSantosh Puranik             {
1693491d8ee7SSantosh Puranik                 return;
1694491d8ee7SSantosh Puranik             }
1695491d8ee7SSantosh Puranik             setBootProperties(asyncResp, std::move(bootSource),
1696491d8ee7SSantosh Puranik                               std::move(bootEnable));
1697491d8ee7SSantosh Puranik         }
1698265c1602SJohnathan Mantey 
16999712f8acSEd Tanous         if (indicatorLed)
17006617338dSEd Tanous         {
1701a3002228SAppaRao Puli             setIndicatorLedState(asyncResp, std::move(*indicatorLed));
17026617338dSEd Tanous         }
1703c5b2abe0SLewanczyk, Dawid     }
1704c5b2abe0SLewanczyk, Dawid };
1705c5b2abe0SLewanczyk, Dawid } // namespace redfish
1706