xref: /openbmc/bmcweb/features/redfish/lib/systems.hpp (revision 9cf21522554fb4db984ea3291bb512c9737b0a0d)
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>
271214b7e7SGunnar Mills 
28abf2add6SEd Tanous #include <variant>
29c5b2abe0SLewanczyk, Dawid 
301abe55efSEd Tanous namespace redfish
311abe55efSEd Tanous {
32c5b2abe0SLewanczyk, Dawid 
339d3ae10eSAlpana Kumari /**
349d3ae10eSAlpana Kumari  * @brief Updates the Functional State of DIMMs
359d3ae10eSAlpana Kumari  *
369d3ae10eSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
379d3ae10eSAlpana Kumari  * @param[in] dimmState Dimm's Functional state, true/false
389d3ae10eSAlpana Kumari  *
399d3ae10eSAlpana Kumari  * @return None.
409d3ae10eSAlpana Kumari  */
4123a21a1cSEd Tanous inline void updateDimmProperties(std::shared_ptr<AsyncResp> aResp,
429d3ae10eSAlpana Kumari                                  const std::variant<bool>& dimmState)
439d3ae10eSAlpana Kumari {
449d3ae10eSAlpana Kumari     const bool* isDimmFunctional = std::get_if<bool>(&dimmState);
459d3ae10eSAlpana Kumari     if (isDimmFunctional == nullptr)
469d3ae10eSAlpana Kumari     {
479d3ae10eSAlpana Kumari         messages::internalError(aResp->res);
489d3ae10eSAlpana Kumari         return;
499d3ae10eSAlpana Kumari     }
509d3ae10eSAlpana Kumari     BMCWEB_LOG_DEBUG << "Dimm Functional: " << *isDimmFunctional;
519d3ae10eSAlpana Kumari 
529d3ae10eSAlpana Kumari     // Set it as Enabled if at least one DIMM is functional
539d3ae10eSAlpana Kumari     // Update STATE only if previous State was DISABLED and current Dimm is
549d3ae10eSAlpana Kumari     // ENABLED.
559d3ae10eSAlpana Kumari     nlohmann::json& prevMemSummary =
569d3ae10eSAlpana Kumari         aResp->res.jsonValue["MemorySummary"]["Status"]["State"];
579d3ae10eSAlpana Kumari     if (prevMemSummary == "Disabled")
589d3ae10eSAlpana Kumari     {
599d3ae10eSAlpana Kumari         if (*isDimmFunctional == true)
609d3ae10eSAlpana Kumari         {
619d3ae10eSAlpana Kumari             aResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
629d3ae10eSAlpana Kumari                 "Enabled";
639d3ae10eSAlpana Kumari         }
649d3ae10eSAlpana Kumari     }
659d3ae10eSAlpana Kumari }
669d3ae10eSAlpana Kumari 
6757e8c9beSAlpana Kumari /*
6857e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Count" based on Cpu PresenceState
6957e8c9beSAlpana Kumari  *
7057e8c9beSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
7157e8c9beSAlpana Kumari  * @param[in] cpuPresenceState CPU present or not
7257e8c9beSAlpana Kumari  *
7357e8c9beSAlpana Kumari  * @return None.
7457e8c9beSAlpana Kumari  */
7523a21a1cSEd Tanous inline void modifyCpuPresenceState(std::shared_ptr<AsyncResp> aResp,
7657e8c9beSAlpana Kumari                                    const std::variant<bool>& cpuPresenceState)
7757e8c9beSAlpana Kumari {
7857e8c9beSAlpana Kumari     const bool* isCpuPresent = std::get_if<bool>(&cpuPresenceState);
7957e8c9beSAlpana Kumari 
8057e8c9beSAlpana Kumari     if (isCpuPresent == nullptr)
8157e8c9beSAlpana Kumari     {
8257e8c9beSAlpana Kumari         messages::internalError(aResp->res);
8357e8c9beSAlpana Kumari         return;
8457e8c9beSAlpana Kumari     }
8557e8c9beSAlpana Kumari     BMCWEB_LOG_DEBUG << "Cpu Present: " << *isCpuPresent;
8657e8c9beSAlpana Kumari 
8757e8c9beSAlpana Kumari     if (*isCpuPresent == true)
8857e8c9beSAlpana Kumari     {
89b4b9595aSJames Feist         nlohmann::json& procCount =
90b4b9595aSJames Feist             aResp->res.jsonValue["ProcessorSummary"]["Count"];
91b4b9595aSJames Feist         auto procCountPtr =
92b4b9595aSJames Feist             procCount.get_ptr<nlohmann::json::number_integer_t*>();
93b4b9595aSJames Feist         if (procCountPtr != nullptr)
94b4b9595aSJames Feist         {
95b4b9595aSJames Feist             // shouldn't be possible to be nullptr
96b4b9595aSJames Feist             *procCountPtr += 1;
9757e8c9beSAlpana Kumari         }
98b4b9595aSJames Feist     }
9957e8c9beSAlpana Kumari }
10057e8c9beSAlpana Kumari 
10157e8c9beSAlpana Kumari /*
10257e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Status" "State" based on
10357e8c9beSAlpana Kumari  *        CPU Functional State
10457e8c9beSAlpana Kumari  *
10557e8c9beSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
10657e8c9beSAlpana Kumari  * @param[in] cpuFunctionalState is CPU functional true/false
10757e8c9beSAlpana Kumari  *
10857e8c9beSAlpana Kumari  * @return None.
10957e8c9beSAlpana Kumari  */
11023a21a1cSEd Tanous inline void
11123a21a1cSEd Tanous     modifyCpuFunctionalState(std::shared_ptr<AsyncResp> aResp,
11257e8c9beSAlpana Kumari                              const std::variant<bool>& cpuFunctionalState)
11357e8c9beSAlpana Kumari {
11457e8c9beSAlpana Kumari     const bool* isCpuFunctional = std::get_if<bool>(&cpuFunctionalState);
11557e8c9beSAlpana Kumari 
11657e8c9beSAlpana Kumari     if (isCpuFunctional == nullptr)
11757e8c9beSAlpana Kumari     {
11857e8c9beSAlpana Kumari         messages::internalError(aResp->res);
11957e8c9beSAlpana Kumari         return;
12057e8c9beSAlpana Kumari     }
12157e8c9beSAlpana Kumari     BMCWEB_LOG_DEBUG << "Cpu Functional: " << *isCpuFunctional;
12257e8c9beSAlpana Kumari 
12357e8c9beSAlpana Kumari     nlohmann::json& prevProcState =
12457e8c9beSAlpana Kumari         aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"];
12557e8c9beSAlpana Kumari 
12657e8c9beSAlpana Kumari     // Set it as Enabled if at least one CPU is functional
12757e8c9beSAlpana Kumari     // Update STATE only if previous State was Non_Functional and current CPU is
12857e8c9beSAlpana Kumari     // Functional.
12957e8c9beSAlpana Kumari     if (prevProcState == "Disabled")
13057e8c9beSAlpana Kumari     {
13157e8c9beSAlpana Kumari         if (*isCpuFunctional == true)
13257e8c9beSAlpana Kumari         {
13357e8c9beSAlpana Kumari             aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
13457e8c9beSAlpana Kumari                 "Enabled";
13557e8c9beSAlpana Kumari         }
13657e8c9beSAlpana Kumari     }
13757e8c9beSAlpana Kumari }
13857e8c9beSAlpana Kumari 
13957e8c9beSAlpana Kumari /*
140c5b2abe0SLewanczyk, Dawid  * @brief Retrieves computer system properties over dbus
141c5b2abe0SLewanczyk, Dawid  *
142c5b2abe0SLewanczyk, Dawid  * @param[in] aResp Shared pointer for completing asynchronous calls
143c5b2abe0SLewanczyk, Dawid  * @param[in] name  Computer system name from request
144c5b2abe0SLewanczyk, Dawid  *
145c5b2abe0SLewanczyk, Dawid  * @return None.
146c5b2abe0SLewanczyk, Dawid  */
14723a21a1cSEd Tanous inline void getComputerSystem(std::shared_ptr<AsyncResp> aResp,
1485bc2dc8eSJames Feist                               std::shared_ptr<HealthPopulate> systemHealth)
1491abe55efSEd Tanous {
15055c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get available system components.";
1519d3ae10eSAlpana Kumari 
15255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
1535bc2dc8eSJames Feist         [aResp, systemHealth](
154c5b2abe0SLewanczyk, Dawid             const boost::system::error_code ec,
155c5b2abe0SLewanczyk, Dawid             const std::vector<std::pair<
1566c34de48SEd Tanous                 std::string,
1571214b7e7SGunnar Mills                 std::vector<std::pair<std::string, std::vector<std::string>>>>>&
1581214b7e7SGunnar Mills                 subtree) {
1591abe55efSEd Tanous             if (ec)
1601abe55efSEd Tanous             {
16155c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error";
162f12894f8SJason M. Bills                 messages::internalError(aResp->res);
163c5b2abe0SLewanczyk, Dawid                 return;
164c5b2abe0SLewanczyk, Dawid             }
165c5b2abe0SLewanczyk, Dawid             // Iterate over all retrieved ObjectPaths.
1666c34de48SEd Tanous             for (const std::pair<std::string,
1676c34de48SEd Tanous                                  std::vector<std::pair<
1681214b7e7SGunnar Mills                                      std::string, std::vector<std::string>>>>&
1691214b7e7SGunnar Mills                      object : subtree)
1701abe55efSEd Tanous             {
171c5b2abe0SLewanczyk, Dawid                 const std::string& path = object.first;
17255c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "Got path: " << path;
1731abe55efSEd Tanous                 const std::vector<
1741214b7e7SGunnar Mills                     std::pair<std::string, std::vector<std::string>>>&
1751214b7e7SGunnar Mills                     connectionNames = object.second;
1761abe55efSEd Tanous                 if (connectionNames.size() < 1)
1771abe55efSEd Tanous                 {
178c5b2abe0SLewanczyk, Dawid                     continue;
179c5b2abe0SLewanczyk, Dawid                 }
180029573d4SEd Tanous 
1815bc2dc8eSJames Feist                 auto memoryHealth = std::make_shared<HealthPopulate>(
1825bc2dc8eSJames Feist                     aResp, aResp->res.jsonValue["MemorySummary"]["Status"]);
1835bc2dc8eSJames Feist 
1845bc2dc8eSJames Feist                 auto cpuHealth = std::make_shared<HealthPopulate>(
1855bc2dc8eSJames Feist                     aResp, aResp->res.jsonValue["ProcessorSummary"]["Status"]);
1865bc2dc8eSJames Feist 
1875bc2dc8eSJames Feist                 systemHealth->children.emplace_back(memoryHealth);
1885bc2dc8eSJames Feist                 systemHealth->children.emplace_back(cpuHealth);
1895bc2dc8eSJames Feist 
1906c34de48SEd Tanous                 // This is not system, so check if it's cpu, dimm, UUID or
1916c34de48SEd Tanous                 // BiosVer
19204a258f4SEd Tanous                 for (const auto& connection : connectionNames)
1931abe55efSEd Tanous                 {
19404a258f4SEd Tanous                     for (const auto& interfaceName : connection.second)
1951abe55efSEd Tanous                     {
19604a258f4SEd Tanous                         if (interfaceName ==
19704a258f4SEd Tanous                             "xyz.openbmc_project.Inventory.Item.Dimm")
1981abe55efSEd Tanous                         {
1991abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
20004a258f4SEd Tanous                                 << "Found Dimm, now get its properties.";
2019d3ae10eSAlpana Kumari 
20255c7b7a2SEd Tanous                             crow::connections::systemBus->async_method_call(
2039d3ae10eSAlpana Kumari                                 [aResp, service{connection.first},
2049d3ae10eSAlpana Kumari                                  path(std::move(path))](
205cb13a392SEd Tanous                                     const boost::system::error_code ec2,
2066c34de48SEd Tanous                                     const std::vector<
2071214b7e7SGunnar Mills                                         std::pair<std::string, VariantType>>&
2081214b7e7SGunnar Mills                                         properties) {
209cb13a392SEd Tanous                                     if (ec2)
2101abe55efSEd Tanous                                     {
2111abe55efSEd Tanous                                         BMCWEB_LOG_ERROR
212cb13a392SEd Tanous                                             << "DBUS response error " << ec2;
213f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
214c5b2abe0SLewanczyk, Dawid                                         return;
215c5b2abe0SLewanczyk, Dawid                                     }
2166c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
2176c34de48SEd Tanous                                                      << properties.size()
218c5b2abe0SLewanczyk, Dawid                                                      << " Dimm properties.";
2199d3ae10eSAlpana Kumari 
2209d3ae10eSAlpana Kumari                                     if (properties.size() > 0)
2219d3ae10eSAlpana Kumari                                     {
22204a258f4SEd Tanous                                         for (const std::pair<std::string,
2231214b7e7SGunnar Mills                                                              VariantType>&
2241214b7e7SGunnar Mills                                                  property : properties)
2251abe55efSEd Tanous                                         {
2265fd7ba65SCheng C Yang                                             if (property.first !=
2275fd7ba65SCheng C Yang                                                 "MemorySizeInKB")
2281abe55efSEd Tanous                                             {
2295fd7ba65SCheng C Yang                                                 continue;
2305fd7ba65SCheng C Yang                                             }
2315fd7ba65SCheng C Yang                                             const uint32_t* value =
2328d78b7a9SPatrick Williams                                                 std::get_if<uint32_t>(
2331b6b96c5SEd Tanous                                                     &property.second);
2345fd7ba65SCheng C Yang                                             if (value == nullptr)
2351abe55efSEd Tanous                                             {
2365fd7ba65SCheng C Yang                                                 BMCWEB_LOG_DEBUG
2375fd7ba65SCheng C Yang                                                     << "Find incorrect type of "
2385fd7ba65SCheng C Yang                                                        "MemorySize";
2395fd7ba65SCheng C Yang                                                 continue;
2405fd7ba65SCheng C Yang                                             }
2415fd7ba65SCheng C Yang                                             nlohmann::json& totalMemory =
2425fd7ba65SCheng C Yang                                                 aResp->res
2435fd7ba65SCheng C Yang                                                     .jsonValue["MemorySummar"
2445fd7ba65SCheng C Yang                                                                "y"]
2455fd7ba65SCheng C Yang                                                               ["TotalSystemMe"
2465fd7ba65SCheng C Yang                                                                "moryGiB"];
2475fd7ba65SCheng C Yang                                             uint64_t* preValue =
2485fd7ba65SCheng C Yang                                                 totalMemory
2495fd7ba65SCheng C Yang                                                     .get_ptr<uint64_t*>();
2505fd7ba65SCheng C Yang                                             if (preValue == nullptr)
2515fd7ba65SCheng C Yang                                             {
2525fd7ba65SCheng C Yang                                                 continue;
2535fd7ba65SCheng C Yang                                             }
2545fd7ba65SCheng C Yang                                             aResp->res
2555fd7ba65SCheng C Yang                                                 .jsonValue["MemorySummary"]
2566c34de48SEd Tanous                                                           ["TotalSystemMemoryGi"
2575fd7ba65SCheng C Yang                                                            "B"] =
2585fd7ba65SCheng C Yang                                                 *value / (1024 * 1024) +
2595fd7ba65SCheng C Yang                                                 *preValue;
2605fd7ba65SCheng C Yang                                             aResp->res
2615fd7ba65SCheng C Yang                                                 .jsonValue["MemorySummary"]
2629d3ae10eSAlpana Kumari                                                           ["Status"]["State"] =
2631abe55efSEd Tanous                                                 "Enabled";
264c5b2abe0SLewanczyk, Dawid                                         }
265c5b2abe0SLewanczyk, Dawid                                     }
2669d3ae10eSAlpana Kumari                                     else
2679d3ae10eSAlpana Kumari                                     {
2689d3ae10eSAlpana Kumari                                         auto getDimmProperties =
2699d3ae10eSAlpana Kumari                                             [aResp](
2709d3ae10eSAlpana Kumari                                                 const boost::system::error_code
271cb13a392SEd Tanous                                                     ec3,
2721214b7e7SGunnar Mills                                                 const std::variant<bool>&
2731214b7e7SGunnar Mills                                                     dimmState) {
274cb13a392SEd Tanous                                                 if (ec3)
2759d3ae10eSAlpana Kumari                                                 {
2769d3ae10eSAlpana Kumari                                                     BMCWEB_LOG_ERROR
2779d3ae10eSAlpana Kumari                                                         << "DBUS response "
2789d3ae10eSAlpana Kumari                                                            "error "
279cb13a392SEd Tanous                                                         << ec3;
2809d3ae10eSAlpana Kumari                                                     return;
2819d3ae10eSAlpana Kumari                                                 }
2829d3ae10eSAlpana Kumari                                                 updateDimmProperties(aResp,
2839d3ae10eSAlpana Kumari                                                                      dimmState);
2849d3ae10eSAlpana Kumari                                             };
2859d3ae10eSAlpana Kumari                                         crow::connections::systemBus
2869d3ae10eSAlpana Kumari                                             ->async_method_call(
2879d3ae10eSAlpana Kumari                                                 std::move(getDimmProperties),
2889d3ae10eSAlpana Kumari                                                 service, path,
2899d3ae10eSAlpana Kumari                                                 "org.freedesktop.DBus."
2909d3ae10eSAlpana Kumari                                                 "Properties",
2919d3ae10eSAlpana Kumari                                                 "Get",
2929d3ae10eSAlpana Kumari                                                 "xyz.openbmc_project.State."
2939d3ae10eSAlpana Kumari                                                 "Decorator.OperationalStatus",
2949d3ae10eSAlpana Kumari                                                 "Functional");
2959d3ae10eSAlpana Kumari                                     }
296c5b2abe0SLewanczyk, Dawid                                 },
29704a258f4SEd Tanous                                 connection.first, path,
2986c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
2996c34de48SEd Tanous                                 "xyz.openbmc_project.Inventory.Item.Dimm");
3005bc2dc8eSJames Feist 
3015bc2dc8eSJames Feist                             memoryHealth->inventory.emplace_back(path);
3021abe55efSEd Tanous                         }
30304a258f4SEd Tanous                         else if (interfaceName ==
30404a258f4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.Cpu")
3051abe55efSEd Tanous                         {
3061abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
30704a258f4SEd Tanous                                 << "Found Cpu, now get its properties.";
30857e8c9beSAlpana Kumari 
309a0803efaSEd Tanous                             crow::connections::systemBus->async_method_call(
31057e8c9beSAlpana Kumari                                 [aResp, service{connection.first},
31157e8c9beSAlpana Kumari                                  path(std::move(path))](
312cb13a392SEd Tanous                                     const boost::system::error_code ec2,
3136c34de48SEd Tanous                                     const std::vector<
3141214b7e7SGunnar Mills                                         std::pair<std::string, VariantType>>&
3151214b7e7SGunnar Mills                                         properties) {
316cb13a392SEd Tanous                                     if (ec2)
3171abe55efSEd Tanous                                     {
3181abe55efSEd Tanous                                         BMCWEB_LOG_ERROR
319cb13a392SEd Tanous                                             << "DBUS response error " << ec2;
320f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
321c5b2abe0SLewanczyk, Dawid                                         return;
322c5b2abe0SLewanczyk, Dawid                                     }
3236c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
3246c34de48SEd Tanous                                                      << properties.size()
325c5b2abe0SLewanczyk, Dawid                                                      << " Cpu properties.";
32657e8c9beSAlpana Kumari 
32757e8c9beSAlpana Kumari                                     if (properties.size() > 0)
32857e8c9beSAlpana Kumari                                     {
329*9cf21522SZhikui Ren                                         const uint64_t* processorId = nullptr;
330029cc1f4SZhikui Ren                                         const std::string* procFamily = nullptr;
331029cc1f4SZhikui Ren                                         nlohmann::json& procSummary =
332029cc1f4SZhikui Ren                                             aResp->res.jsonValue["ProcessorSumm"
33304a258f4SEd Tanous                                                                  "ary"];
33404a258f4SEd Tanous                                         nlohmann::json& procCount =
33504a258f4SEd Tanous                                             procSummary["Count"];
336b4b9595aSJames Feist 
337029cc1f4SZhikui Ren                                         auto procCountPtr = procCount.get_ptr<
338b4b9595aSJames Feist                                             nlohmann::json::
3391214b7e7SGunnar Mills                                                 number_integer_t*>();
340029cc1f4SZhikui Ren                                         if (procCountPtr == nullptr)
341b4b9595aSJames Feist                                         {
342029cc1f4SZhikui Ren                                             messages::internalError(aResp->res);
343029cc1f4SZhikui Ren                                             return;
344029cc1f4SZhikui Ren                                         }
345029cc1f4SZhikui Ren                                         for (const auto& property : properties)
346029cc1f4SZhikui Ren                                         {
347029cc1f4SZhikui Ren 
348*9cf21522SZhikui Ren                                             if (property.first == "Id")
349029cc1f4SZhikui Ren                                             {
350029cc1f4SZhikui Ren                                                 processorId =
351*9cf21522SZhikui Ren                                                     std::get_if<uint64_t>(
352029cc1f4SZhikui Ren                                                         &property.second);
353029cc1f4SZhikui Ren                                                 if (nullptr != procFamily)
354029cc1f4SZhikui Ren                                                     break;
355029cc1f4SZhikui Ren                                                 continue;
356029cc1f4SZhikui Ren                                             }
357029cc1f4SZhikui Ren 
358*9cf21522SZhikui Ren                                             if (property.first == "Family")
359029cc1f4SZhikui Ren                                             {
360029cc1f4SZhikui Ren                                                 procFamily =
361029cc1f4SZhikui Ren                                                     std::get_if<std::string>(
362029cc1f4SZhikui Ren                                                         &property.second);
363029cc1f4SZhikui Ren                                                 if (nullptr != processorId)
364029cc1f4SZhikui Ren                                                     break;
365029cc1f4SZhikui Ren                                                 continue;
366029cc1f4SZhikui Ren                                             }
367029cc1f4SZhikui Ren                                         }
368029cc1f4SZhikui Ren 
369029cc1f4SZhikui Ren                                         if (procFamily != nullptr &&
370029cc1f4SZhikui Ren                                             processorId != nullptr)
371029cc1f4SZhikui Ren                                         {
372029cc1f4SZhikui Ren                                             if (procCountPtr != nullptr &&
373029cc1f4SZhikui Ren                                                 *processorId != 0)
374029cc1f4SZhikui Ren                                             {
375b4b9595aSJames Feist                                                 *procCountPtr += 1;
376029cc1f4SZhikui Ren                                                 procSummary["Status"]["State"] =
377c5b2abe0SLewanczyk, Dawid                                                     "Enabled";
378029cc1f4SZhikui Ren 
37957e8c9beSAlpana Kumari                                                 procSummary["Model"] =
380029cc1f4SZhikui Ren                                                     *procFamily;
381c5b2abe0SLewanczyk, Dawid                                             }
382c5b2abe0SLewanczyk, Dawid                                         }
38357e8c9beSAlpana Kumari                                     }
38457e8c9beSAlpana Kumari                                     else
38557e8c9beSAlpana Kumari                                     {
38657e8c9beSAlpana Kumari                                         auto getCpuPresenceState =
38757e8c9beSAlpana Kumari                                             [aResp](
38857e8c9beSAlpana Kumari                                                 const boost::system::error_code
389cb13a392SEd Tanous                                                     ec3,
3901214b7e7SGunnar Mills                                                 const std::variant<bool>&
3911214b7e7SGunnar Mills                                                     cpuPresenceCheck) {
392cb13a392SEd Tanous                                                 if (ec3)
39357e8c9beSAlpana Kumari                                                 {
39457e8c9beSAlpana Kumari                                                     BMCWEB_LOG_ERROR
39557e8c9beSAlpana Kumari                                                         << "DBUS response "
39657e8c9beSAlpana Kumari                                                            "error "
397cb13a392SEd Tanous                                                         << ec3;
39857e8c9beSAlpana Kumari                                                     return;
39957e8c9beSAlpana Kumari                                                 }
40057e8c9beSAlpana Kumari                                                 modifyCpuPresenceState(
40157e8c9beSAlpana Kumari                                                     aResp, cpuPresenceCheck);
40257e8c9beSAlpana Kumari                                             };
40357e8c9beSAlpana Kumari 
40457e8c9beSAlpana Kumari                                         auto getCpuFunctionalState =
40557e8c9beSAlpana Kumari                                             [aResp](
40657e8c9beSAlpana Kumari                                                 const boost::system::error_code
407cb13a392SEd Tanous                                                     ec3,
4081214b7e7SGunnar Mills                                                 const std::variant<bool>&
4091214b7e7SGunnar Mills                                                     cpuFunctionalCheck) {
410cb13a392SEd Tanous                                                 if (ec3)
41157e8c9beSAlpana Kumari                                                 {
41257e8c9beSAlpana Kumari                                                     BMCWEB_LOG_ERROR
41357e8c9beSAlpana Kumari                                                         << "DBUS response "
41457e8c9beSAlpana Kumari                                                            "error "
415cb13a392SEd Tanous                                                         << ec3;
41657e8c9beSAlpana Kumari                                                     return;
41757e8c9beSAlpana Kumari                                                 }
41857e8c9beSAlpana Kumari                                                 modifyCpuFunctionalState(
41957e8c9beSAlpana Kumari                                                     aResp, cpuFunctionalCheck);
42057e8c9beSAlpana Kumari                                             };
42157e8c9beSAlpana Kumari                                         // Get the Presence of CPU
42257e8c9beSAlpana Kumari                                         crow::connections::systemBus
42357e8c9beSAlpana Kumari                                             ->async_method_call(
42457e8c9beSAlpana Kumari                                                 std::move(getCpuPresenceState),
42557e8c9beSAlpana Kumari                                                 service, path,
42657e8c9beSAlpana Kumari                                                 "org.freedesktop.DBus."
42757e8c9beSAlpana Kumari                                                 "Properties",
42857e8c9beSAlpana Kumari                                                 "Get",
42957e8c9beSAlpana Kumari                                                 "xyz.openbmc_project.Inventory."
43057e8c9beSAlpana Kumari                                                 "Item",
43157e8c9beSAlpana Kumari                                                 "Present");
43257e8c9beSAlpana Kumari 
43357e8c9beSAlpana Kumari                                         // Get the Functional State
43457e8c9beSAlpana Kumari                                         crow::connections::systemBus
43557e8c9beSAlpana Kumari                                             ->async_method_call(
43657e8c9beSAlpana Kumari                                                 std::move(
43757e8c9beSAlpana Kumari                                                     getCpuFunctionalState),
43857e8c9beSAlpana Kumari                                                 service, path,
43957e8c9beSAlpana Kumari                                                 "org.freedesktop.DBus."
44057e8c9beSAlpana Kumari                                                 "Properties",
44157e8c9beSAlpana Kumari                                                 "Get",
44257e8c9beSAlpana Kumari                                                 "xyz.openbmc_project.State."
44357e8c9beSAlpana Kumari                                                 "Decorator."
44457e8c9beSAlpana Kumari                                                 "OperationalStatus",
44557e8c9beSAlpana Kumari                                                 "Functional");
44657e8c9beSAlpana Kumari 
44757e8c9beSAlpana Kumari                                         // Get the MODEL from
44857e8c9beSAlpana Kumari                                         // xyz.openbmc_project.Inventory.Decorator.Asset
44957e8c9beSAlpana Kumari                                         // support it later as Model  is Empty
45057e8c9beSAlpana Kumari                                         // currently.
45157e8c9beSAlpana Kumari                                     }
452c5b2abe0SLewanczyk, Dawid                                 },
45304a258f4SEd Tanous                                 connection.first, path,
4546c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
4556c34de48SEd Tanous                                 "xyz.openbmc_project.Inventory.Item.Cpu");
4565bc2dc8eSJames Feist 
4575bc2dc8eSJames Feist                             cpuHealth->inventory.emplace_back(path);
4581abe55efSEd Tanous                         }
45904a258f4SEd Tanous                         else if (interfaceName ==
46004a258f4SEd Tanous                                  "xyz.openbmc_project.Common.UUID")
4611abe55efSEd Tanous                         {
4621abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
46304a258f4SEd Tanous                                 << "Found UUID, now get its properties.";
46455c7b7a2SEd Tanous                             crow::connections::systemBus->async_method_call(
4651214b7e7SGunnar Mills                                 [aResp](
466cb13a392SEd Tanous                                     const boost::system::error_code ec3,
4676c34de48SEd Tanous                                     const std::vector<
4681214b7e7SGunnar Mills                                         std::pair<std::string, VariantType>>&
4691214b7e7SGunnar Mills                                         properties) {
470cb13a392SEd Tanous                                     if (ec3)
4711abe55efSEd Tanous                                     {
4721abe55efSEd Tanous                                         BMCWEB_LOG_DEBUG
473cb13a392SEd Tanous                                             << "DBUS response error " << ec3;
474f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
475c5b2abe0SLewanczyk, Dawid                                         return;
476c5b2abe0SLewanczyk, Dawid                                     }
4776c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
4786c34de48SEd Tanous                                                      << properties.size()
479c5b2abe0SLewanczyk, Dawid                                                      << " UUID properties.";
4801abe55efSEd Tanous                                     for (const std::pair<std::string,
4811214b7e7SGunnar Mills                                                          VariantType>&
4821214b7e7SGunnar Mills                                              property : properties)
4831abe55efSEd Tanous                                     {
48404a258f4SEd Tanous                                         if (property.first == "UUID")
4851abe55efSEd Tanous                                         {
486c5b2abe0SLewanczyk, Dawid                                             const std::string* value =
4878d78b7a9SPatrick Williams                                                 std::get_if<std::string>(
4881b6b96c5SEd Tanous                                                     &property.second);
48904a258f4SEd Tanous 
4901abe55efSEd Tanous                                             if (value != nullptr)
4911abe55efSEd Tanous                                             {
492029573d4SEd Tanous                                                 std::string valueStr = *value;
49304a258f4SEd Tanous                                                 if (valueStr.size() == 32)
4941abe55efSEd Tanous                                                 {
495029573d4SEd Tanous                                                     valueStr.insert(8, 1, '-');
496029573d4SEd Tanous                                                     valueStr.insert(13, 1, '-');
497029573d4SEd Tanous                                                     valueStr.insert(18, 1, '-');
498029573d4SEd Tanous                                                     valueStr.insert(23, 1, '-');
49904a258f4SEd Tanous                                                 }
500029573d4SEd Tanous                                                 BMCWEB_LOG_DEBUG << "UUID = "
50104a258f4SEd Tanous                                                                  << valueStr;
502029573d4SEd Tanous                                                 aResp->res.jsonValue["UUID"] =
50304a258f4SEd Tanous                                                     valueStr;
504c5b2abe0SLewanczyk, Dawid                                             }
505c5b2abe0SLewanczyk, Dawid                                         }
506c5b2abe0SLewanczyk, Dawid                                     }
507c5b2abe0SLewanczyk, Dawid                                 },
50804a258f4SEd Tanous                                 connection.first, path,
5096c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
5101abe55efSEd Tanous                                 "xyz.openbmc_project.Common.UUID");
511c5b2abe0SLewanczyk, Dawid                         }
512029573d4SEd Tanous                         else if (interfaceName ==
513029573d4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.System")
5141abe55efSEd Tanous                         {
515029573d4SEd Tanous                             crow::connections::systemBus->async_method_call(
5161214b7e7SGunnar Mills                                 [aResp](
517cb13a392SEd Tanous                                     const boost::system::error_code ec2,
518029573d4SEd Tanous                                     const std::vector<
5191214b7e7SGunnar Mills                                         std::pair<std::string, VariantType>>&
5201214b7e7SGunnar Mills                                         propertiesList) {
521cb13a392SEd Tanous                                     if (ec2)
522029573d4SEd Tanous                                     {
523e4a4b9a9SJames Feist                                         // doesn't have to include this
524e4a4b9a9SJames Feist                                         // interface
525029573d4SEd Tanous                                         return;
526029573d4SEd Tanous                                     }
527698654b6SGunnar Mills                                     BMCWEB_LOG_DEBUG
528698654b6SGunnar Mills                                         << "Got " << propertiesList.size()
529029573d4SEd Tanous                                         << " properties for system";
530029573d4SEd Tanous                                     for (const std::pair<std::string,
5311214b7e7SGunnar Mills                                                          VariantType>&
5321214b7e7SGunnar Mills                                              property : propertiesList)
533029573d4SEd Tanous                                     {
534fc5afcf9Sbeccabroek                                         const std::string& propertyName =
535fc5afcf9Sbeccabroek                                             property.first;
536fc5afcf9Sbeccabroek                                         if ((propertyName == "PartNumber") ||
537fc5afcf9Sbeccabroek                                             (propertyName == "SerialNumber") ||
538fc5afcf9Sbeccabroek                                             (propertyName == "Manufacturer") ||
539fc5afcf9Sbeccabroek                                             (propertyName == "Model"))
540fc5afcf9Sbeccabroek                                         {
541029573d4SEd Tanous                                             const std::string* value =
542fc5afcf9Sbeccabroek                                                 std::get_if<std::string>(
543029573d4SEd Tanous                                                     &property.second);
544029573d4SEd Tanous                                             if (value != nullptr)
545029573d4SEd Tanous                                             {
546029573d4SEd Tanous                                                 aResp->res
547fc5afcf9Sbeccabroek                                                     .jsonValue[propertyName] =
548029573d4SEd Tanous                                                     *value;
549029573d4SEd Tanous                                             }
550029573d4SEd Tanous                                         }
551fc5afcf9Sbeccabroek                                     }
552c1e236a6SGunnar Mills 
553cb7e1e7bSAndrew Geissler                                     // Grab the bios version
554f97ddba7SGunnar Mills                                     fw_util::populateFirmwareInformation(
555cb7e1e7bSAndrew Geissler                                         aResp, fw_util::biosPurpose,
55672d566d9SGunnar Mills                                         "BiosVersion", false);
557029573d4SEd Tanous                                 },
558029573d4SEd Tanous                                 connection.first, path,
559029573d4SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
560029573d4SEd Tanous                                 "xyz.openbmc_project.Inventory.Decorator."
561029573d4SEd Tanous                                 "Asset");
562e4a4b9a9SJames Feist 
563e4a4b9a9SJames Feist                             crow::connections::systemBus->async_method_call(
564e4a4b9a9SJames Feist                                 [aResp](
565cb13a392SEd Tanous                                     const boost::system::error_code ec2,
566e4a4b9a9SJames Feist                                     const std::variant<std::string>& property) {
567cb13a392SEd Tanous                                     if (ec2)
568e4a4b9a9SJames Feist                                     {
569e4a4b9a9SJames Feist                                         // doesn't have to include this
570e4a4b9a9SJames Feist                                         // interface
571e4a4b9a9SJames Feist                                         return;
572e4a4b9a9SJames Feist                                     }
573e4a4b9a9SJames Feist 
574e4a4b9a9SJames Feist                                     const std::string* value =
575e4a4b9a9SJames Feist                                         std::get_if<std::string>(&property);
576e4a4b9a9SJames Feist                                     if (value != nullptr)
577e4a4b9a9SJames Feist                                     {
578e4a4b9a9SJames Feist                                         aResp->res.jsonValue["AssetTag"] =
579e4a4b9a9SJames Feist                                             *value;
580e4a4b9a9SJames Feist                                     }
581e4a4b9a9SJames Feist                                 },
582e4a4b9a9SJames Feist                                 connection.first, path,
583e4a4b9a9SJames Feist                                 "org.freedesktop.DBus.Properties", "Get",
584e4a4b9a9SJames Feist                                 "xyz.openbmc_project.Inventory.Decorator."
585e4a4b9a9SJames Feist                                 "AssetTag",
586e4a4b9a9SJames Feist                                 "AssetTag");
587029573d4SEd Tanous                         }
588029573d4SEd Tanous                     }
589029573d4SEd Tanous                 }
590c5b2abe0SLewanczyk, Dawid             }
591c5b2abe0SLewanczyk, Dawid         },
592c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper",
593c5b2abe0SLewanczyk, Dawid         "/xyz/openbmc_project/object_mapper",
594c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
5956617338dSEd Tanous         "/xyz/openbmc_project/inventory", int32_t(0),
5966617338dSEd Tanous         std::array<const char*, 5>{
5976617338dSEd Tanous             "xyz.openbmc_project.Inventory.Decorator.Asset",
5986617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Cpu",
5996617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Dimm",
6006617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.System",
6016617338dSEd Tanous             "xyz.openbmc_project.Common.UUID",
6026617338dSEd Tanous         });
603c5b2abe0SLewanczyk, Dawid }
604c5b2abe0SLewanczyk, Dawid 
605c5b2abe0SLewanczyk, Dawid /**
606c5b2abe0SLewanczyk, Dawid  * @brief Retrieves host state properties over dbus
607c5b2abe0SLewanczyk, Dawid  *
608c5b2abe0SLewanczyk, Dawid  * @param[in] aResp     Shared pointer for completing asynchronous calls.
609c5b2abe0SLewanczyk, Dawid  *
610c5b2abe0SLewanczyk, Dawid  * @return None.
611c5b2abe0SLewanczyk, Dawid  */
61223a21a1cSEd Tanous inline void getHostState(std::shared_ptr<AsyncResp> aResp)
6131abe55efSEd Tanous {
61455c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get host information.";
61555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
616c5d03ff4SJennifer Lee         [aResp](const boost::system::error_code ec,
617abf2add6SEd Tanous                 const std::variant<std::string>& hostState) {
6181abe55efSEd Tanous             if (ec)
6191abe55efSEd Tanous             {
62055c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
621f12894f8SJason M. Bills                 messages::internalError(aResp->res);
622c5b2abe0SLewanczyk, Dawid                 return;
623c5b2abe0SLewanczyk, Dawid             }
6246617338dSEd Tanous 
625abf2add6SEd Tanous             const std::string* s = std::get_if<std::string>(&hostState);
62655c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Host state: " << *s;
6276617338dSEd Tanous             if (s != nullptr)
6281abe55efSEd Tanous             {
629c5b2abe0SLewanczyk, Dawid                 // Verify Host State
63094732661SAndrew Geissler                 if (*s == "xyz.openbmc_project.State.Host.HostState.Running")
6311abe55efSEd Tanous                 {
63255c7b7a2SEd Tanous                     aResp->res.jsonValue["PowerState"] = "On";
6336617338dSEd Tanous                     aResp->res.jsonValue["Status"]["State"] = "Enabled";
6341abe55efSEd Tanous                 }
63583935af9SAndrew Geissler                 else if (*s == "xyz.openbmc_project.State.Host.HostState."
6368c888608SGunnar Mills                                "Quiesced")
6378c888608SGunnar Mills                 {
6388c888608SGunnar Mills                     aResp->res.jsonValue["PowerState"] = "On";
6398c888608SGunnar Mills                     aResp->res.jsonValue["Status"]["State"] = "Quiesced";
6408c888608SGunnar Mills                 }
6418c888608SGunnar Mills                 else if (*s == "xyz.openbmc_project.State.Host.HostState."
64283935af9SAndrew Geissler                                "DiagnosticMode")
64383935af9SAndrew Geissler                 {
64483935af9SAndrew Geissler                     aResp->res.jsonValue["PowerState"] = "On";
64583935af9SAndrew Geissler                     aResp->res.jsonValue["Status"]["State"] = "InTest";
64683935af9SAndrew Geissler                 }
6471abe55efSEd Tanous                 else
6481abe55efSEd Tanous                 {
64955c7b7a2SEd Tanous                     aResp->res.jsonValue["PowerState"] = "Off";
6506617338dSEd Tanous                     aResp->res.jsonValue["Status"]["State"] = "Disabled";
651c5b2abe0SLewanczyk, Dawid                 }
652c5b2abe0SLewanczyk, Dawid             }
653c5b2abe0SLewanczyk, Dawid         },
6546c34de48SEd Tanous         "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
6556617338dSEd Tanous         "org.freedesktop.DBus.Properties", "Get",
6566617338dSEd Tanous         "xyz.openbmc_project.State.Host", "CurrentHostState");
657c5b2abe0SLewanczyk, Dawid }
658c5b2abe0SLewanczyk, Dawid 
659c5b2abe0SLewanczyk, Dawid /**
660786d0f60SGunnar Mills  * @brief Translates boot source DBUS property value to redfish.
661491d8ee7SSantosh Puranik  *
662491d8ee7SSantosh Puranik  * @param[in] dbusSource    The boot source in DBUS speak.
663491d8ee7SSantosh Puranik  *
664491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source in Redfish terms. If translation
665491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
666491d8ee7SSantosh Puranik  */
66723a21a1cSEd Tanous inline std::string dbusToRfBootSource(const std::string& dbusSource)
668491d8ee7SSantosh Puranik {
669491d8ee7SSantosh Puranik     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
670491d8ee7SSantosh Puranik     {
671491d8ee7SSantosh Puranik         return "None";
672491d8ee7SSantosh Puranik     }
673491d8ee7SSantosh Puranik     else if (dbusSource ==
674491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.Disk")
675491d8ee7SSantosh Puranik     {
676491d8ee7SSantosh Puranik         return "Hdd";
677491d8ee7SSantosh Puranik     }
678491d8ee7SSantosh Puranik     else if (dbusSource ==
679a71dc0b7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia")
680491d8ee7SSantosh Puranik     {
681491d8ee7SSantosh Puranik         return "Cd";
682491d8ee7SSantosh Puranik     }
683491d8ee7SSantosh Puranik     else if (dbusSource ==
684491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.Network")
685491d8ee7SSantosh Puranik     {
686491d8ee7SSantosh Puranik         return "Pxe";
687491d8ee7SSantosh Puranik     }
6889f16b2c1SJennifer Lee     else if (dbusSource ==
689944ffaf9SJohnathan Mantey              "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia")
6909f16b2c1SJennifer Lee     {
6919f16b2c1SJennifer Lee         return "Usb";
6929f16b2c1SJennifer Lee     }
693491d8ee7SSantosh Puranik     else
694491d8ee7SSantosh Puranik     {
695491d8ee7SSantosh Puranik         return "";
696491d8ee7SSantosh Puranik     }
697491d8ee7SSantosh Puranik }
698491d8ee7SSantosh Puranik 
699491d8ee7SSantosh Puranik /**
700786d0f60SGunnar Mills  * @brief Translates boot mode DBUS property value to redfish.
701491d8ee7SSantosh Puranik  *
702491d8ee7SSantosh Puranik  * @param[in] dbusMode    The boot mode in DBUS speak.
703491d8ee7SSantosh Puranik  *
704491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode in Redfish terms. If translation
705491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
706491d8ee7SSantosh Puranik  */
70723a21a1cSEd Tanous inline std::string dbusToRfBootMode(const std::string& dbusMode)
708491d8ee7SSantosh Puranik {
709491d8ee7SSantosh Puranik     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
710491d8ee7SSantosh Puranik     {
711491d8ee7SSantosh Puranik         return "None";
712491d8ee7SSantosh Puranik     }
713491d8ee7SSantosh Puranik     else if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe")
714491d8ee7SSantosh Puranik     {
715491d8ee7SSantosh Puranik         return "Diags";
716491d8ee7SSantosh Puranik     }
717491d8ee7SSantosh Puranik     else if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup")
718491d8ee7SSantosh Puranik     {
719491d8ee7SSantosh Puranik         return "BiosSetup";
720491d8ee7SSantosh Puranik     }
721491d8ee7SSantosh Puranik     else
722491d8ee7SSantosh Puranik     {
723491d8ee7SSantosh Puranik         return "";
724491d8ee7SSantosh Puranik     }
725491d8ee7SSantosh Puranik }
726491d8ee7SSantosh Puranik 
727491d8ee7SSantosh Puranik /**
728786d0f60SGunnar Mills  * @brief Translates boot source from Redfish to the DBus boot paths.
729491d8ee7SSantosh Puranik  *
730491d8ee7SSantosh Puranik  * @param[in] rfSource    The boot source in Redfish.
731944ffaf9SJohnathan Mantey  * @param[out] bootSource The DBus source
732944ffaf9SJohnathan Mantey  * @param[out] bootMode   the DBus boot mode
733491d8ee7SSantosh Puranik  *
734944ffaf9SJohnathan Mantey  * @return Integer error code.
735491d8ee7SSantosh Puranik  */
73623a21a1cSEd Tanous inline int assignBootParameters(std::shared_ptr<AsyncResp> aResp,
737944ffaf9SJohnathan Mantey                                 const std::string& rfSource,
738944ffaf9SJohnathan Mantey                                 std::string& bootSource, std::string& bootMode)
739491d8ee7SSantosh Puranik {
740944ffaf9SJohnathan Mantey     // The caller has initialized the bootSource and bootMode to:
741944ffaf9SJohnathan Mantey     // bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
742944ffaf9SJohnathan Mantey     // bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
743944ffaf9SJohnathan Mantey     // Only modify the bootSource/bootMode variable needed to achieve the
744944ffaf9SJohnathan Mantey     // desired boot action.
745944ffaf9SJohnathan Mantey 
746491d8ee7SSantosh Puranik     if (rfSource == "None")
747491d8ee7SSantosh Puranik     {
748944ffaf9SJohnathan Mantey         return 0;
749491d8ee7SSantosh Puranik     }
750491d8ee7SSantosh Puranik     else if (rfSource == "Pxe")
751491d8ee7SSantosh Puranik     {
752944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Network";
753944ffaf9SJohnathan Mantey     }
754944ffaf9SJohnathan Mantey     else if (rfSource == "Hdd")
755944ffaf9SJohnathan Mantey     {
756944ffaf9SJohnathan Mantey         bootSource = "xyz.openbmc_project.Control.Boot.Source.Sources.Disk";
757944ffaf9SJohnathan Mantey     }
758944ffaf9SJohnathan Mantey     else if (rfSource == "Diags")
759944ffaf9SJohnathan Mantey     {
760944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe";
761944ffaf9SJohnathan Mantey     }
762944ffaf9SJohnathan Mantey     else if (rfSource == "Cd")
763944ffaf9SJohnathan Mantey     {
764944ffaf9SJohnathan Mantey         bootSource =
765944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia";
766944ffaf9SJohnathan Mantey     }
767944ffaf9SJohnathan Mantey     else if (rfSource == "BiosSetup")
768944ffaf9SJohnathan Mantey     {
769944ffaf9SJohnathan Mantey         bootMode = "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup";
770491d8ee7SSantosh Puranik     }
7719f16b2c1SJennifer Lee     else if (rfSource == "Usb")
7729f16b2c1SJennifer Lee     {
773944ffaf9SJohnathan Mantey         bootSource =
774944ffaf9SJohnathan Mantey             "xyz.openbmc_project.Control.Boot.Source.Sources.RemovableMedia";
7759f16b2c1SJennifer Lee     }
776491d8ee7SSantosh Puranik     else
777491d8ee7SSantosh Puranik     {
778944ffaf9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "Invalid property value for "
779944ffaf9SJohnathan Mantey                             "BootSourceOverrideTarget: "
780944ffaf9SJohnathan Mantey                          << bootSource;
781944ffaf9SJohnathan Mantey         messages::propertyValueNotInList(aResp->res, rfSource,
782944ffaf9SJohnathan Mantey                                          "BootSourceTargetOverride");
783944ffaf9SJohnathan Mantey         return -1;
784491d8ee7SSantosh Puranik     }
785944ffaf9SJohnathan Mantey     return 0;
786491d8ee7SSantosh Puranik }
787491d8ee7SSantosh Puranik 
788491d8ee7SSantosh Puranik /**
789491d8ee7SSantosh Puranik  * @brief Retrieves boot mode over DBUS and fills out the response
790491d8ee7SSantosh Puranik  *
791491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
792491d8ee7SSantosh Puranik  * @param[in] bootDbusObj   The dbus object to query for boot properties.
793491d8ee7SSantosh Puranik  *
794491d8ee7SSantosh Puranik  * @return None.
795491d8ee7SSantosh Puranik  */
79623a21a1cSEd Tanous inline void getBootMode(std::shared_ptr<AsyncResp> aResp,
797491d8ee7SSantosh Puranik                         std::string bootDbusObj)
798491d8ee7SSantosh Puranik {
799491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
800491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec,
801491d8ee7SSantosh Puranik                 const std::variant<std::string>& bootMode) {
802491d8ee7SSantosh Puranik             if (ec)
803491d8ee7SSantosh Puranik             {
804491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
805491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
806491d8ee7SSantosh Puranik                 return;
807491d8ee7SSantosh Puranik             }
808491d8ee7SSantosh Puranik 
809491d8ee7SSantosh Puranik             const std::string* bootModeStr =
810491d8ee7SSantosh Puranik                 std::get_if<std::string>(&bootMode);
811491d8ee7SSantosh Puranik 
812491d8ee7SSantosh Puranik             if (!bootModeStr)
813491d8ee7SSantosh Puranik             {
814491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
815491d8ee7SSantosh Puranik                 return;
816491d8ee7SSantosh Puranik             }
817491d8ee7SSantosh Puranik 
818491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot mode: " << *bootModeStr;
819491d8ee7SSantosh Puranik 
820491d8ee7SSantosh Puranik             // TODO (Santosh): Do we need to support override mode?
821491d8ee7SSantosh Puranik             aResp->res.jsonValue["Boot"]["BootSourceOverrideMode"] = "Legacy";
822491d8ee7SSantosh Puranik             aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget@Redfish."
823491d8ee7SSantosh Puranik                                          "AllowableValues"] = {
824944ffaf9SJohnathan Mantey                 "None", "Pxe", "Hdd", "Cd", "Diags", "BiosSetup", "Usb"};
825491d8ee7SSantosh Puranik 
826491d8ee7SSantosh Puranik             if (*bootModeStr !=
827491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
828491d8ee7SSantosh Puranik             {
829491d8ee7SSantosh Puranik                 auto rfMode = dbusToRfBootMode(*bootModeStr);
830491d8ee7SSantosh Puranik                 if (!rfMode.empty())
831491d8ee7SSantosh Puranik                 {
832491d8ee7SSantosh Puranik                     aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
833491d8ee7SSantosh Puranik                         rfMode;
834491d8ee7SSantosh Puranik                 }
835491d8ee7SSantosh Puranik             }
836491d8ee7SSantosh Puranik 
837491d8ee7SSantosh Puranik             // If the BootSourceOverrideTarget is still "None" at the end,
838491d8ee7SSantosh Puranik             // reset the BootSourceOverrideEnabled to indicate that
839491d8ee7SSantosh Puranik             // overrides are disabled
840491d8ee7SSantosh Puranik             if (aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] ==
841491d8ee7SSantosh Puranik                 "None")
842491d8ee7SSantosh Puranik             {
843491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
844491d8ee7SSantosh Puranik                     "Disabled";
845491d8ee7SSantosh Puranik             }
846491d8ee7SSantosh Puranik         },
847491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootDbusObj,
848491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
849491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode");
850491d8ee7SSantosh Puranik }
851491d8ee7SSantosh Puranik 
852491d8ee7SSantosh Puranik /**
853491d8ee7SSantosh Puranik  * @brief Retrieves boot source over DBUS
854491d8ee7SSantosh Puranik  *
855491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
856491d8ee7SSantosh Puranik  * @param[in] oneTimeEnable Boolean to indicate boot properties are one-time.
857491d8ee7SSantosh Puranik  *
858491d8ee7SSantosh Puranik  * @return None.
859491d8ee7SSantosh Puranik  */
86023a21a1cSEd Tanous inline void getBootSource(std::shared_ptr<AsyncResp> aResp, bool oneTimeEnabled)
861491d8ee7SSantosh Puranik {
862491d8ee7SSantosh Puranik     std::string bootDbusObj =
863491d8ee7SSantosh Puranik         oneTimeEnabled ? "/xyz/openbmc_project/control/host0/boot/one_time"
864491d8ee7SSantosh Puranik                        : "/xyz/openbmc_project/control/host0/boot";
865491d8ee7SSantosh Puranik 
866491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Is one time: " << oneTimeEnabled;
867491d8ee7SSantosh Puranik     aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
868491d8ee7SSantosh Puranik         (oneTimeEnabled) ? "Once" : "Continuous";
869491d8ee7SSantosh Puranik 
870491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
871491d8ee7SSantosh Puranik         [aResp, bootDbusObj](const boost::system::error_code ec,
872491d8ee7SSantosh Puranik                              const std::variant<std::string>& bootSource) {
873491d8ee7SSantosh Puranik             if (ec)
874491d8ee7SSantosh Puranik             {
875491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
876491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
877491d8ee7SSantosh Puranik                 return;
878491d8ee7SSantosh Puranik             }
879491d8ee7SSantosh Puranik 
880491d8ee7SSantosh Puranik             const std::string* bootSourceStr =
881491d8ee7SSantosh Puranik                 std::get_if<std::string>(&bootSource);
882491d8ee7SSantosh Puranik 
883491d8ee7SSantosh Puranik             if (!bootSourceStr)
884491d8ee7SSantosh Puranik             {
885491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
886491d8ee7SSantosh Puranik                 return;
887491d8ee7SSantosh Puranik             }
888491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot source: " << *bootSourceStr;
889491d8ee7SSantosh Puranik 
890491d8ee7SSantosh Puranik             auto rfSource = dbusToRfBootSource(*bootSourceStr);
891491d8ee7SSantosh Puranik             if (!rfSource.empty())
892491d8ee7SSantosh Puranik             {
893491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
894491d8ee7SSantosh Puranik                     rfSource;
895491d8ee7SSantosh Puranik             }
896491d8ee7SSantosh Puranik         },
897491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootDbusObj,
898491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
899491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource");
900491d8ee7SSantosh Puranik     getBootMode(std::move(aResp), std::move(bootDbusObj));
901491d8ee7SSantosh Puranik }
902491d8ee7SSantosh Puranik 
903491d8ee7SSantosh Puranik /**
904491d8ee7SSantosh Puranik  * @brief Retrieves "One time" enabled setting over DBUS and calls function to
905491d8ee7SSantosh Puranik  * get boot source and boot mode.
906491d8ee7SSantosh Puranik  *
907491d8ee7SSantosh Puranik  * @param[in] aResp     Shared pointer for generating response message.
908491d8ee7SSantosh Puranik  *
909491d8ee7SSantosh Puranik  * @return None.
910491d8ee7SSantosh Puranik  */
91123a21a1cSEd Tanous inline void getBootProperties(std::shared_ptr<AsyncResp> aResp)
912491d8ee7SSantosh Puranik {
913491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Get boot information.";
914491d8ee7SSantosh Puranik 
915491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
916c5d03ff4SJennifer Lee         [aResp](const boost::system::error_code ec,
91719bd78d9SPatrick Williams                 const std::variant<bool>& oneTime) {
918491d8ee7SSantosh Puranik             if (ec)
919491d8ee7SSantosh Puranik             {
920491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
9212a833c77SJames Feist                 // not an error, don't have to have the interface
922491d8ee7SSantosh Puranik                 return;
923491d8ee7SSantosh Puranik             }
924491d8ee7SSantosh Puranik 
925491d8ee7SSantosh Puranik             const bool* oneTimePtr = std::get_if<bool>(&oneTime);
926491d8ee7SSantosh Puranik 
927491d8ee7SSantosh Puranik             if (!oneTimePtr)
928491d8ee7SSantosh Puranik             {
929491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
930491d8ee7SSantosh Puranik                 return;
931491d8ee7SSantosh Puranik             }
932491d8ee7SSantosh Puranik             getBootSource(aResp, *oneTimePtr);
933491d8ee7SSantosh Puranik         },
934491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
935491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
936491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
937491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled");
938491d8ee7SSantosh Puranik }
939491d8ee7SSantosh Puranik 
940491d8ee7SSantosh Puranik /**
941c0557e1aSGunnar Mills  * @brief Retrieves the Last Reset Time
942c0557e1aSGunnar Mills  *
943c0557e1aSGunnar Mills  * "Reset" is an overloaded term in Redfish, "Reset" includes power on
944c0557e1aSGunnar Mills  * and power off. Even though this is the "system" Redfish object look at the
945c0557e1aSGunnar Mills  * chassis D-Bus interface for the LastStateChangeTime since this has the
946c0557e1aSGunnar Mills  * last power operation time.
947c0557e1aSGunnar Mills  *
948c0557e1aSGunnar Mills  * @param[in] aResp     Shared pointer for generating response message.
949c0557e1aSGunnar Mills  *
950c0557e1aSGunnar Mills  * @return None.
951c0557e1aSGunnar Mills  */
95223a21a1cSEd Tanous inline void getLastResetTime(std::shared_ptr<AsyncResp> aResp)
953c0557e1aSGunnar Mills {
954c0557e1aSGunnar Mills     BMCWEB_LOG_DEBUG << "Getting System Last Reset Time";
955c0557e1aSGunnar Mills 
956c0557e1aSGunnar Mills     crow::connections::systemBus->async_method_call(
957c0557e1aSGunnar Mills         [aResp](const boost::system::error_code ec,
958c0557e1aSGunnar Mills                 std::variant<uint64_t>& lastResetTime) {
959c0557e1aSGunnar Mills             if (ec)
960c0557e1aSGunnar Mills             {
961c0557e1aSGunnar Mills                 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
962c0557e1aSGunnar Mills                 return;
963c0557e1aSGunnar Mills             }
964c0557e1aSGunnar Mills 
965c0557e1aSGunnar Mills             const uint64_t* lastResetTimePtr =
966c0557e1aSGunnar Mills                 std::get_if<uint64_t>(&lastResetTime);
967c0557e1aSGunnar Mills 
968c0557e1aSGunnar Mills             if (!lastResetTimePtr)
969c0557e1aSGunnar Mills             {
970c0557e1aSGunnar Mills                 messages::internalError(aResp->res);
971c0557e1aSGunnar Mills                 return;
972c0557e1aSGunnar Mills             }
973c0557e1aSGunnar Mills             // LastStateChangeTime is epoch time, in milliseconds
974c0557e1aSGunnar Mills             // https://github.com/openbmc/phosphor-dbus-interfaces/blob/33e8e1dd64da53a66e888d33dc82001305cd0bf9/xyz/openbmc_project/State/Chassis.interface.yaml#L19
975c0557e1aSGunnar Mills             time_t lastResetTimeStamp =
976c0557e1aSGunnar Mills                 static_cast<time_t>(*lastResetTimePtr / 1000);
977c0557e1aSGunnar Mills 
978c0557e1aSGunnar Mills             // Convert to ISO 8601 standard
979c0557e1aSGunnar Mills             aResp->res.jsonValue["LastResetTime"] =
980c0557e1aSGunnar Mills                 crow::utility::getDateTime(lastResetTimeStamp);
981c0557e1aSGunnar Mills         },
982c0557e1aSGunnar Mills         "xyz.openbmc_project.State.Chassis",
983c0557e1aSGunnar Mills         "/xyz/openbmc_project/state/chassis0",
984c0557e1aSGunnar Mills         "org.freedesktop.DBus.Properties", "Get",
985c0557e1aSGunnar Mills         "xyz.openbmc_project.State.Chassis", "LastStateChangeTime");
986c0557e1aSGunnar Mills }
987c0557e1aSGunnar Mills 
988c0557e1aSGunnar Mills /**
9896bd5a8d2SGunnar Mills  * @brief Retrieves Automatic Retry properties. Known on D-Bus as AutoReboot.
9906bd5a8d2SGunnar Mills  *
9916bd5a8d2SGunnar Mills  * @param[in] aResp     Shared pointer for generating response message.
9926bd5a8d2SGunnar Mills  *
9936bd5a8d2SGunnar Mills  * @return None.
9946bd5a8d2SGunnar Mills  */
99523a21a1cSEd Tanous inline void getAutomaticRetry(std::shared_ptr<AsyncResp> aResp)
9966bd5a8d2SGunnar Mills {
9976bd5a8d2SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Automatic Retry policy";
9986bd5a8d2SGunnar Mills 
9996bd5a8d2SGunnar Mills     crow::connections::systemBus->async_method_call(
10006bd5a8d2SGunnar Mills         [aResp](const boost::system::error_code ec,
10016bd5a8d2SGunnar Mills                 std::variant<bool>& autoRebootEnabled) {
10026bd5a8d2SGunnar Mills             if (ec)
10036bd5a8d2SGunnar Mills             {
10046bd5a8d2SGunnar Mills                 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
10056bd5a8d2SGunnar Mills                 return;
10066bd5a8d2SGunnar Mills             }
10076bd5a8d2SGunnar Mills 
10086bd5a8d2SGunnar Mills             const bool* autoRebootEnabledPtr =
10096bd5a8d2SGunnar Mills                 std::get_if<bool>(&autoRebootEnabled);
10106bd5a8d2SGunnar Mills 
10116bd5a8d2SGunnar Mills             if (!autoRebootEnabledPtr)
10126bd5a8d2SGunnar Mills             {
10136bd5a8d2SGunnar Mills                 messages::internalError(aResp->res);
10146bd5a8d2SGunnar Mills                 return;
10156bd5a8d2SGunnar Mills             }
10166bd5a8d2SGunnar Mills 
10176bd5a8d2SGunnar Mills             BMCWEB_LOG_DEBUG << "Auto Reboot: " << *autoRebootEnabledPtr;
10186bd5a8d2SGunnar Mills             if (*autoRebootEnabledPtr == true)
10196bd5a8d2SGunnar Mills             {
10206bd5a8d2SGunnar Mills                 aResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
10216bd5a8d2SGunnar Mills                     "RetryAttempts";
10226bd5a8d2SGunnar Mills                 // If AutomaticRetry (AutoReboot) is enabled see how many
10236bd5a8d2SGunnar Mills                 // attempts are left
10246bd5a8d2SGunnar Mills                 crow::connections::systemBus->async_method_call(
1025cb13a392SEd Tanous                     [aResp](const boost::system::error_code ec2,
10266bd5a8d2SGunnar Mills                             std::variant<uint32_t>& autoRebootAttemptsLeft) {
1027cb13a392SEd Tanous                         if (ec2)
10286bd5a8d2SGunnar Mills                         {
1029cb13a392SEd Tanous                             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec2;
10306bd5a8d2SGunnar Mills                             return;
10316bd5a8d2SGunnar Mills                         }
10326bd5a8d2SGunnar Mills 
10336bd5a8d2SGunnar Mills                         const uint32_t* autoRebootAttemptsLeftPtr =
10346bd5a8d2SGunnar Mills                             std::get_if<uint32_t>(&autoRebootAttemptsLeft);
10356bd5a8d2SGunnar Mills 
10366bd5a8d2SGunnar Mills                         if (!autoRebootAttemptsLeftPtr)
10376bd5a8d2SGunnar Mills                         {
10386bd5a8d2SGunnar Mills                             messages::internalError(aResp->res);
10396bd5a8d2SGunnar Mills                             return;
10406bd5a8d2SGunnar Mills                         }
10416bd5a8d2SGunnar Mills 
10426bd5a8d2SGunnar Mills                         BMCWEB_LOG_DEBUG << "Auto Reboot Attempts Left: "
10436bd5a8d2SGunnar Mills                                          << *autoRebootAttemptsLeftPtr;
10446bd5a8d2SGunnar Mills 
10456bd5a8d2SGunnar Mills                         aResp->res
10466bd5a8d2SGunnar Mills                             .jsonValue["Boot"]
10476bd5a8d2SGunnar Mills                                       ["RemainingAutomaticRetryAttempts"] =
10486bd5a8d2SGunnar Mills                             *autoRebootAttemptsLeftPtr;
10496bd5a8d2SGunnar Mills                     },
10506bd5a8d2SGunnar Mills                     "xyz.openbmc_project.State.Host",
10516bd5a8d2SGunnar Mills                     "/xyz/openbmc_project/state/host0",
10526bd5a8d2SGunnar Mills                     "org.freedesktop.DBus.Properties", "Get",
10536bd5a8d2SGunnar Mills                     "xyz.openbmc_project.Control.Boot.RebootAttempts",
10546bd5a8d2SGunnar Mills                     "AttemptsLeft");
10556bd5a8d2SGunnar Mills             }
10566bd5a8d2SGunnar Mills             else
10576bd5a8d2SGunnar Mills             {
10586bd5a8d2SGunnar Mills                 aResp->res.jsonValue["Boot"]["AutomaticRetryConfig"] =
10596bd5a8d2SGunnar Mills                     "Disabled";
10606bd5a8d2SGunnar Mills             }
10616bd5a8d2SGunnar Mills 
10626bd5a8d2SGunnar Mills             // Not on D-Bus. Hardcoded here:
10636bd5a8d2SGunnar Mills             // https://github.com/openbmc/phosphor-state-manager/blob/1dbbef42675e94fb1f78edb87d6b11380260535a/meson_options.txt#L71
10646bd5a8d2SGunnar Mills             aResp->res.jsonValue["Boot"]["AutomaticRetryAttempts"] = 3;
106569f35306SGunnar Mills 
106669f35306SGunnar Mills             // "AutomaticRetryConfig" can be 3 values, Disabled, RetryAlways,
106769f35306SGunnar Mills             // and RetryAttempts. OpenBMC only supports Disabled and
106869f35306SGunnar Mills             // RetryAttempts.
106969f35306SGunnar Mills             aResp->res.jsonValue["Boot"]["AutomaticRetryConfig@Redfish."
107069f35306SGunnar Mills                                          "AllowableValues"] = {"Disabled",
107169f35306SGunnar Mills                                                                "RetryAttempts"};
10726bd5a8d2SGunnar Mills         },
10736bd5a8d2SGunnar Mills         "xyz.openbmc_project.Settings",
10746bd5a8d2SGunnar Mills         "/xyz/openbmc_project/control/host0/auto_reboot",
10756bd5a8d2SGunnar Mills         "org.freedesktop.DBus.Properties", "Get",
10766bd5a8d2SGunnar Mills         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot");
10776bd5a8d2SGunnar Mills }
10786bd5a8d2SGunnar Mills 
10796bd5a8d2SGunnar Mills /**
1080c6a620f2SGeorge Liu  * @brief Retrieves power restore policy over DBUS.
1081c6a620f2SGeorge Liu  *
1082c6a620f2SGeorge Liu  * @param[in] aResp     Shared pointer for generating response message.
1083c6a620f2SGeorge Liu  *
1084c6a620f2SGeorge Liu  * @return None.
1085c6a620f2SGeorge Liu  */
108623a21a1cSEd Tanous inline void getPowerRestorePolicy(std::shared_ptr<AsyncResp> aResp)
1087c6a620f2SGeorge Liu {
1088c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Get power restore policy";
1089c6a620f2SGeorge Liu 
1090c6a620f2SGeorge Liu     crow::connections::systemBus->async_method_call(
1091c6a620f2SGeorge Liu         [aResp](const boost::system::error_code ec,
109219bd78d9SPatrick Williams                 std::variant<std::string>& policy) {
1093c6a620f2SGeorge Liu             if (ec)
1094c6a620f2SGeorge Liu             {
1095c6a620f2SGeorge Liu                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1096c6a620f2SGeorge Liu                 return;
1097c6a620f2SGeorge Liu             }
1098c6a620f2SGeorge Liu 
1099c6a620f2SGeorge Liu             const boost::container::flat_map<std::string, std::string>
1100c6a620f2SGeorge Liu                 policyMaps = {
1101c6a620f2SGeorge Liu                     {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1102c6a620f2SGeorge Liu                      "AlwaysOn",
1103c6a620f2SGeorge Liu                      "AlwaysOn"},
1104c6a620f2SGeorge Liu                     {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1105c6a620f2SGeorge Liu                      "AlwaysOff",
1106c6a620f2SGeorge Liu                      "AlwaysOff"},
1107c6a620f2SGeorge Liu                     {"xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1108c6a620f2SGeorge Liu                      "LastState",
1109c6a620f2SGeorge Liu                      "LastState"}};
1110c6a620f2SGeorge Liu 
1111c6a620f2SGeorge Liu             const std::string* policyPtr = std::get_if<std::string>(&policy);
1112c6a620f2SGeorge Liu 
1113c6a620f2SGeorge Liu             if (!policyPtr)
1114c6a620f2SGeorge Liu             {
1115c6a620f2SGeorge Liu                 messages::internalError(aResp->res);
1116c6a620f2SGeorge Liu                 return;
1117c6a620f2SGeorge Liu             }
1118c6a620f2SGeorge Liu 
1119c6a620f2SGeorge Liu             auto policyMapsIt = policyMaps.find(*policyPtr);
1120c6a620f2SGeorge Liu             if (policyMapsIt == policyMaps.end())
1121c6a620f2SGeorge Liu             {
1122c6a620f2SGeorge Liu                 messages::internalError(aResp->res);
1123c6a620f2SGeorge Liu                 return;
1124c6a620f2SGeorge Liu             }
1125c6a620f2SGeorge Liu 
1126c6a620f2SGeorge Liu             aResp->res.jsonValue["PowerRestorePolicy"] = policyMapsIt->second;
1127c6a620f2SGeorge Liu         },
1128c6a620f2SGeorge Liu         "xyz.openbmc_project.Settings",
1129c6a620f2SGeorge Liu         "/xyz/openbmc_project/control/host0/power_restore_policy",
1130c6a620f2SGeorge Liu         "org.freedesktop.DBus.Properties", "Get",
1131c6a620f2SGeorge Liu         "xyz.openbmc_project.Control.Power.RestorePolicy",
1132c6a620f2SGeorge Liu         "PowerRestorePolicy");
1133c6a620f2SGeorge Liu }
1134c6a620f2SGeorge Liu 
1135c6a620f2SGeorge Liu /**
1136491d8ee7SSantosh Puranik  * @brief Sets boot properties into DBUS object(s).
1137491d8ee7SSantosh Puranik  *
1138491d8ee7SSantosh Puranik  * @param[in] aResp           Shared pointer for generating response message.
1139491d8ee7SSantosh Puranik  * @param[in] oneTimeEnabled  Is "one-time" setting already enabled.
1140491d8ee7SSantosh Puranik  * @param[in] bootSource      The boot source to set.
1141491d8ee7SSantosh Puranik  * @param[in] bootEnable      The source override "enable" to set.
1142491d8ee7SSantosh Puranik  *
1143265c1602SJohnathan Mantey  * @return Integer error code.
1144491d8ee7SSantosh Puranik  */
114523a21a1cSEd Tanous inline void setBootModeOrSource(std::shared_ptr<AsyncResp> aResp,
1146491d8ee7SSantosh Puranik                                 bool oneTimeEnabled,
1147491d8ee7SSantosh Puranik                                 std::optional<std::string> bootSource,
1148491d8ee7SSantosh Puranik                                 std::optional<std::string> bootEnable)
1149491d8ee7SSantosh Puranik {
1150944ffaf9SJohnathan Mantey     std::string bootSourceStr =
1151944ffaf9SJohnathan Mantey         "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
1152944ffaf9SJohnathan Mantey     std::string bootModeStr =
1153944ffaf9SJohnathan Mantey         "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
1154491d8ee7SSantosh Puranik     bool oneTimeSetting = oneTimeEnabled;
1155944ffaf9SJohnathan Mantey     bool useBootSource = true;
1156944ffaf9SJohnathan Mantey 
1157491d8ee7SSantosh Puranik     // Validate incoming parameters
1158491d8ee7SSantosh Puranik     if (bootEnable)
1159491d8ee7SSantosh Puranik     {
1160491d8ee7SSantosh Puranik         if (*bootEnable == "Once")
1161491d8ee7SSantosh Puranik         {
1162491d8ee7SSantosh Puranik             oneTimeSetting = true;
1163491d8ee7SSantosh Puranik         }
1164491d8ee7SSantosh Puranik         else if (*bootEnable == "Continuous")
1165491d8ee7SSantosh Puranik         {
1166491d8ee7SSantosh Puranik             oneTimeSetting = false;
1167491d8ee7SSantosh Puranik         }
1168491d8ee7SSantosh Puranik         else if (*bootEnable == "Disabled")
1169491d8ee7SSantosh Puranik         {
1170944ffaf9SJohnathan Mantey             BMCWEB_LOG_DEBUG << "Boot source override will be disabled";
1171491d8ee7SSantosh Puranik             oneTimeSetting = false;
1172944ffaf9SJohnathan Mantey             useBootSource = false;
1173491d8ee7SSantosh Puranik         }
1174491d8ee7SSantosh Puranik         else
1175491d8ee7SSantosh Puranik         {
1176491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Unsupported value for "
1177491d8ee7SSantosh Puranik                                 "BootSourceOverrideEnabled: "
1178491d8ee7SSantosh Puranik                              << *bootEnable;
1179491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootEnable,
1180491d8ee7SSantosh Puranik                                              "BootSourceOverrideEnabled");
1181491d8ee7SSantosh Puranik             return;
1182491d8ee7SSantosh Puranik         }
1183491d8ee7SSantosh Puranik     }
1184491d8ee7SSantosh Puranik 
1185944ffaf9SJohnathan Mantey     if (bootSource && useBootSource)
1186491d8ee7SSantosh Puranik     {
1187491d8ee7SSantosh Puranik         // Source target specified
1188491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot source: " << *bootSource;
1189491d8ee7SSantosh Puranik         // Figure out which DBUS interface and property to use
1190944ffaf9SJohnathan Mantey         if (assignBootParameters(aResp, *bootSource, bootSourceStr,
1191944ffaf9SJohnathan Mantey                                  bootModeStr))
1192491d8ee7SSantosh Puranik         {
1193944ffaf9SJohnathan Mantey             BMCWEB_LOG_DEBUG
1194944ffaf9SJohnathan Mantey                 << "Invalid property value for BootSourceOverrideTarget: "
1195491d8ee7SSantosh Puranik                 << *bootSource;
1196491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootSource,
1197491d8ee7SSantosh Puranik                                              "BootSourceTargetOverride");
1198491d8ee7SSantosh Puranik             return;
1199491d8ee7SSantosh Puranik         }
1200944ffaf9SJohnathan Mantey     }
1201491d8ee7SSantosh Puranik 
1202944ffaf9SJohnathan Mantey     // Act on validated parameters
1203944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
1204944ffaf9SJohnathan Mantey     BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
1205944ffaf9SJohnathan Mantey     const char* bootObj =
1206944ffaf9SJohnathan Mantey         oneTimeSetting ? "/xyz/openbmc_project/control/host0/boot/one_time"
1207944ffaf9SJohnathan Mantey                        : "/xyz/openbmc_project/control/host0/boot";
1208944ffaf9SJohnathan Mantey 
1209491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1210491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec) {
1211491d8ee7SSantosh Puranik             if (ec)
1212491d8ee7SSantosh Puranik             {
1213491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1214491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1215491d8ee7SSantosh Puranik                 return;
1216491d8ee7SSantosh Puranik             }
1217491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot source update done.";
1218491d8ee7SSantosh Puranik         },
1219491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootObj,
1220491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1221491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource",
1222491d8ee7SSantosh Puranik         std::variant<std::string>(bootSourceStr));
1223944ffaf9SJohnathan Mantey 
1224491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1225491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec) {
1226491d8ee7SSantosh Puranik             if (ec)
1227491d8ee7SSantosh Puranik             {
1228491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1229491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1230491d8ee7SSantosh Puranik                 return;
1231491d8ee7SSantosh Puranik             }
1232491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot mode update done.";
1233491d8ee7SSantosh Puranik         },
1234491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootObj,
1235491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1236491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
1237491d8ee7SSantosh Puranik         std::variant<std::string>(bootModeStr));
1238944ffaf9SJohnathan Mantey 
1239491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1240491d8ee7SSantosh Puranik         [aResp{std::move(aResp)}](const boost::system::error_code ec) {
1241491d8ee7SSantosh Puranik             if (ec)
1242491d8ee7SSantosh Puranik             {
1243491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1244491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1245491d8ee7SSantosh Puranik                 return;
1246491d8ee7SSantosh Puranik             }
1247491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot enable update done.";
1248491d8ee7SSantosh Puranik         },
1249491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
1250491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
1251491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1252491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled",
1253491d8ee7SSantosh Puranik         std::variant<bool>(oneTimeSetting));
1254491d8ee7SSantosh Puranik }
1255491d8ee7SSantosh Puranik 
1256491d8ee7SSantosh Puranik /**
1257491d8ee7SSantosh Puranik  * @brief Retrieves "One time" enabled setting over DBUS and calls function to
1258491d8ee7SSantosh Puranik  * set boot source/boot mode properties.
1259491d8ee7SSantosh Puranik  *
1260491d8ee7SSantosh Puranik  * @param[in] aResp      Shared pointer for generating response message.
1261491d8ee7SSantosh Puranik  * @param[in] bootSource The boot source from incoming RF request.
1262491d8ee7SSantosh Puranik  * @param[in] bootEnable The boot override enable from incoming RF request.
1263491d8ee7SSantosh Puranik  *
1264265c1602SJohnathan Mantey  * @return Integer error code.
1265491d8ee7SSantosh Puranik  */
126623a21a1cSEd Tanous inline void setBootSourceProperties(std::shared_ptr<AsyncResp> aResp,
1267491d8ee7SSantosh Puranik                                     std::optional<std::string> bootSource,
1268491d8ee7SSantosh Puranik                                     std::optional<std::string> bootEnable)
1269491d8ee7SSantosh Puranik {
1270491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set boot information.";
1271491d8ee7SSantosh Puranik 
1272491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1273265c1602SJohnathan Mantey         [aResp, bootSource{std::move(bootSource)},
127419bd78d9SPatrick Williams          bootEnable{std::move(bootEnable)}](const boost::system::error_code ec,
127519bd78d9SPatrick Williams                                             const std::variant<bool>& oneTime) {
1276491d8ee7SSantosh Puranik             if (ec)
1277491d8ee7SSantosh Puranik             {
1278491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1279491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1280491d8ee7SSantosh Puranik                 return;
1281491d8ee7SSantosh Puranik             }
1282491d8ee7SSantosh Puranik 
1283491d8ee7SSantosh Puranik             const bool* oneTimePtr = std::get_if<bool>(&oneTime);
1284491d8ee7SSantosh Puranik 
1285491d8ee7SSantosh Puranik             if (!oneTimePtr)
1286491d8ee7SSantosh Puranik             {
1287491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1288491d8ee7SSantosh Puranik                 return;
1289491d8ee7SSantosh Puranik             }
1290491d8ee7SSantosh Puranik 
1291491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Got one time: " << *oneTimePtr;
1292491d8ee7SSantosh Puranik 
1293491d8ee7SSantosh Puranik             setBootModeOrSource(aResp, *oneTimePtr, std::move(bootSource),
1294491d8ee7SSantosh Puranik                                 std::move(bootEnable));
1295491d8ee7SSantosh Puranik         },
1296491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
1297491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
1298491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
1299491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled");
1300491d8ee7SSantosh Puranik }
1301491d8ee7SSantosh Puranik 
1302c6a620f2SGeorge Liu /**
130369f35306SGunnar Mills  * @brief Sets automaticRetry (Auto Reboot)
130469f35306SGunnar Mills  *
130569f35306SGunnar Mills  * @param[in] aResp   Shared pointer for generating response message.
130669f35306SGunnar Mills  * @param[in] automaticRetryConfig  "AutomaticRetryConfig" from request.
130769f35306SGunnar Mills  *
130869f35306SGunnar Mills  * @return None.
130969f35306SGunnar Mills  */
131023a21a1cSEd Tanous inline void setAutomaticRetry(std::shared_ptr<AsyncResp> aResp,
131169f35306SGunnar Mills                               const std::string&& automaticRetryConfig)
131269f35306SGunnar Mills {
131369f35306SGunnar Mills     BMCWEB_LOG_DEBUG << "Set Automatic Retry.";
131469f35306SGunnar Mills 
131569f35306SGunnar Mills     // OpenBMC only supports "Disabled" and "RetryAttempts".
131669f35306SGunnar Mills     bool autoRebootEnabled;
131769f35306SGunnar Mills 
131869f35306SGunnar Mills     if (automaticRetryConfig == "Disabled")
131969f35306SGunnar Mills     {
132069f35306SGunnar Mills         autoRebootEnabled = false;
132169f35306SGunnar Mills     }
132269f35306SGunnar Mills     else if (automaticRetryConfig == "RetryAttempts")
132369f35306SGunnar Mills     {
132469f35306SGunnar Mills         autoRebootEnabled = true;
132569f35306SGunnar Mills     }
132669f35306SGunnar Mills     else
132769f35306SGunnar Mills     {
132869f35306SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid property value for "
132969f35306SGunnar Mills                             "AutomaticRetryConfig: "
133069f35306SGunnar Mills                          << automaticRetryConfig;
133169f35306SGunnar Mills         messages::propertyValueNotInList(aResp->res, automaticRetryConfig,
133269f35306SGunnar Mills                                          "AutomaticRetryConfig");
133369f35306SGunnar Mills         return;
133469f35306SGunnar Mills     }
133569f35306SGunnar Mills 
133669f35306SGunnar Mills     crow::connections::systemBus->async_method_call(
133769f35306SGunnar Mills         [aResp](const boost::system::error_code ec) {
133869f35306SGunnar Mills             if (ec)
133969f35306SGunnar Mills             {
134069f35306SGunnar Mills                 messages::internalError(aResp->res);
134169f35306SGunnar Mills                 return;
134269f35306SGunnar Mills             }
134369f35306SGunnar Mills         },
134469f35306SGunnar Mills         "xyz.openbmc_project.Settings",
134569f35306SGunnar Mills         "/xyz/openbmc_project/control/host0/auto_reboot",
134669f35306SGunnar Mills         "org.freedesktop.DBus.Properties", "Set",
134769f35306SGunnar Mills         "xyz.openbmc_project.Control.Boot.RebootPolicy", "AutoReboot",
134869f35306SGunnar Mills         std::variant<bool>(autoRebootEnabled));
134969f35306SGunnar Mills }
135069f35306SGunnar Mills 
135169f35306SGunnar Mills /**
1352c6a620f2SGeorge Liu  * @brief Sets power restore policy properties.
1353c6a620f2SGeorge Liu  *
1354c6a620f2SGeorge Liu  * @param[in] aResp   Shared pointer for generating response message.
1355c6a620f2SGeorge Liu  * @param[in] policy  power restore policy properties from request.
1356c6a620f2SGeorge Liu  *
1357c6a620f2SGeorge Liu  * @return None.
1358c6a620f2SGeorge Liu  */
135923a21a1cSEd Tanous inline void setPowerRestorePolicy(std::shared_ptr<AsyncResp> aResp,
1360c6a620f2SGeorge Liu                                   std::optional<std::string> policy)
1361c6a620f2SGeorge Liu {
1362c6a620f2SGeorge Liu     BMCWEB_LOG_DEBUG << "Set power restore policy.";
1363c6a620f2SGeorge Liu 
1364c6a620f2SGeorge Liu     const boost::container::flat_map<std::string, std::string> policyMaps = {
1365c6a620f2SGeorge Liu         {"AlwaysOn", "xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1366c6a620f2SGeorge Liu                      "AlwaysOn"},
1367c6a620f2SGeorge Liu         {"AlwaysOff", "xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1368c6a620f2SGeorge Liu                       "AlwaysOff"},
1369c6a620f2SGeorge Liu         {"LastState", "xyz.openbmc_project.Control.Power.RestorePolicy.Policy."
1370c6a620f2SGeorge Liu                       "LastState"}};
1371c6a620f2SGeorge Liu 
1372c6a620f2SGeorge Liu     std::string powerRestorPolicy;
1373c6a620f2SGeorge Liu 
1374c6a620f2SGeorge Liu     auto policyMapsIt = policyMaps.find(*policy);
1375c6a620f2SGeorge Liu     if (policyMapsIt == policyMaps.end())
1376c6a620f2SGeorge Liu     {
1377c6a620f2SGeorge Liu         messages::internalError(aResp->res);
1378c6a620f2SGeorge Liu         return;
1379c6a620f2SGeorge Liu     }
1380c6a620f2SGeorge Liu 
1381c6a620f2SGeorge Liu     powerRestorPolicy = policyMapsIt->second;
1382c6a620f2SGeorge Liu 
1383c6a620f2SGeorge Liu     crow::connections::systemBus->async_method_call(
1384c6a620f2SGeorge Liu         [aResp](const boost::system::error_code ec) {
1385c6a620f2SGeorge Liu             if (ec)
1386c6a620f2SGeorge Liu             {
1387c6a620f2SGeorge Liu                 messages::internalError(aResp->res);
1388c6a620f2SGeorge Liu                 return;
1389c6a620f2SGeorge Liu             }
1390c6a620f2SGeorge Liu         },
1391c6a620f2SGeorge Liu         "xyz.openbmc_project.Settings",
1392c6a620f2SGeorge Liu         "/xyz/openbmc_project/control/host0/power_restore_policy",
1393c6a620f2SGeorge Liu         "org.freedesktop.DBus.Properties", "Set",
1394c6a620f2SGeorge Liu         "xyz.openbmc_project.Control.Power.RestorePolicy", "PowerRestorePolicy",
1395c6a620f2SGeorge Liu         std::variant<std::string>(powerRestorPolicy));
1396c6a620f2SGeorge Liu }
1397c6a620f2SGeorge Liu 
1398a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
1399a6349918SAppaRao Puli /**
1400a6349918SAppaRao Puli  * @brief Retrieves provisioning status
1401a6349918SAppaRao Puli  *
1402a6349918SAppaRao Puli  * @param[in] aResp     Shared pointer for completing asynchronous calls.
1403a6349918SAppaRao Puli  *
1404a6349918SAppaRao Puli  * @return None.
1405a6349918SAppaRao Puli  */
140623a21a1cSEd Tanous inline void getProvisioningStatus(std::shared_ptr<AsyncResp> aResp)
1407a6349918SAppaRao Puli {
1408a6349918SAppaRao Puli     BMCWEB_LOG_DEBUG << "Get OEM information.";
1409a6349918SAppaRao Puli     crow::connections::systemBus->async_method_call(
1410a6349918SAppaRao Puli         [aResp](const boost::system::error_code ec,
14111214b7e7SGunnar Mills                 const std::vector<std::pair<std::string, VariantType>>&
14121214b7e7SGunnar Mills                     propertiesList) {
1413b99fb1a9SAppaRao Puli             nlohmann::json& oemPFR =
1414b99fb1a9SAppaRao Puli                 aResp->res.jsonValue["Oem"]["OpenBmc"]["FirmwareProvisioning"];
1415a6349918SAppaRao Puli             if (ec)
1416a6349918SAppaRao Puli             {
1417a6349918SAppaRao Puli                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1418b99fb1a9SAppaRao Puli                 // not an error, don't have to have the interface
1419b99fb1a9SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "NotProvisioned";
1420a6349918SAppaRao Puli                 return;
1421a6349918SAppaRao Puli             }
1422a6349918SAppaRao Puli 
1423a6349918SAppaRao Puli             const bool* provState = nullptr;
1424a6349918SAppaRao Puli             const bool* lockState = nullptr;
1425a6349918SAppaRao Puli             for (const std::pair<std::string, VariantType>& property :
1426a6349918SAppaRao Puli                  propertiesList)
1427a6349918SAppaRao Puli             {
1428a6349918SAppaRao Puli                 if (property.first == "UfmProvisioned")
1429a6349918SAppaRao Puli                 {
1430a6349918SAppaRao Puli                     provState = std::get_if<bool>(&property.second);
1431a6349918SAppaRao Puli                 }
1432a6349918SAppaRao Puli                 else if (property.first == "UfmLocked")
1433a6349918SAppaRao Puli                 {
1434a6349918SAppaRao Puli                     lockState = std::get_if<bool>(&property.second);
1435a6349918SAppaRao Puli                 }
1436a6349918SAppaRao Puli             }
1437a6349918SAppaRao Puli 
1438a6349918SAppaRao Puli             if ((provState == nullptr) || (lockState == nullptr))
1439a6349918SAppaRao Puli             {
1440a6349918SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Unable to get PFR attributes.";
1441a6349918SAppaRao Puli                 messages::internalError(aResp->res);
1442a6349918SAppaRao Puli                 return;
1443a6349918SAppaRao Puli             }
1444a6349918SAppaRao Puli 
1445a6349918SAppaRao Puli             if (*provState == true)
1446a6349918SAppaRao Puli             {
1447a6349918SAppaRao Puli                 if (*lockState == true)
1448a6349918SAppaRao Puli                 {
1449a6349918SAppaRao Puli                     oemPFR["ProvisioningStatus"] = "ProvisionedAndLocked";
1450a6349918SAppaRao Puli                 }
1451a6349918SAppaRao Puli                 else
1452a6349918SAppaRao Puli                 {
1453a6349918SAppaRao Puli                     oemPFR["ProvisioningStatus"] = "ProvisionedButNotLocked";
1454a6349918SAppaRao Puli                 }
1455a6349918SAppaRao Puli             }
1456a6349918SAppaRao Puli             else
1457a6349918SAppaRao Puli             {
1458a6349918SAppaRao Puli                 oemPFR["ProvisioningStatus"] = "NotProvisioned";
1459a6349918SAppaRao Puli             }
1460a6349918SAppaRao Puli         },
1461a6349918SAppaRao Puli         "xyz.openbmc_project.PFR.Manager", "/xyz/openbmc_project/pfr",
1462a6349918SAppaRao Puli         "org.freedesktop.DBus.Properties", "GetAll",
1463a6349918SAppaRao Puli         "xyz.openbmc_project.PFR.Attributes");
1464a6349918SAppaRao Puli }
1465a6349918SAppaRao Puli #endif
1466a6349918SAppaRao Puli 
1467491d8ee7SSantosh Puranik /**
146851709ffdSYong Li  * @brief Translates watchdog timeout action DBUS property value to redfish.
146951709ffdSYong Li  *
147051709ffdSYong Li  * @param[in] dbusAction    The watchdog timeout action in D-BUS.
147151709ffdSYong Li  *
147251709ffdSYong Li  * @return Returns as a string, the timeout action in Redfish terms. If
147351709ffdSYong Li  * translation cannot be done, returns an empty string.
147451709ffdSYong Li  */
147523a21a1cSEd Tanous inline std::string dbusToRfWatchdogAction(const std::string& dbusAction)
147651709ffdSYong Li {
147751709ffdSYong Li     if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.None")
147851709ffdSYong Li     {
147951709ffdSYong Li         return "None";
148051709ffdSYong Li     }
148151709ffdSYong Li     else if (dbusAction ==
148251709ffdSYong Li              "xyz.openbmc_project.State.Watchdog.Action.HardReset")
148351709ffdSYong Li     {
148451709ffdSYong Li         return "ResetSystem";
148551709ffdSYong Li     }
148651709ffdSYong Li     else if (dbusAction == "xyz.openbmc_project.State.Watchdog.Action.PowerOff")
148751709ffdSYong Li     {
148851709ffdSYong Li         return "PowerDown";
148951709ffdSYong Li     }
149051709ffdSYong Li     else if (dbusAction ==
149151709ffdSYong Li              "xyz.openbmc_project.State.Watchdog.Action.PowerCycle")
149251709ffdSYong Li     {
149351709ffdSYong Li         return "PowerCycle";
149451709ffdSYong Li     }
149551709ffdSYong Li 
149651709ffdSYong Li     return "";
149751709ffdSYong Li }
149851709ffdSYong Li 
149951709ffdSYong Li /**
1500c45f0082SYong Li  *@brief Translates timeout action from Redfish to DBUS property value.
1501c45f0082SYong Li  *
1502c45f0082SYong Li  *@param[in] rfAction The timeout action in Redfish.
1503c45f0082SYong Li  *
1504c45f0082SYong Li  *@return Returns as a string, the time_out action as expected by DBUS.
1505c45f0082SYong Li  *If translation cannot be done, returns an empty string.
1506c45f0082SYong Li  */
1507c45f0082SYong Li 
150823a21a1cSEd Tanous inline std::string rfToDbusWDTTimeOutAct(const std::string& rfAction)
1509c45f0082SYong Li {
1510c45f0082SYong Li     if (rfAction == "None")
1511c45f0082SYong Li     {
1512c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.None";
1513c45f0082SYong Li     }
1514c45f0082SYong Li     else if (rfAction == "PowerCycle")
1515c45f0082SYong Li     {
1516c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerCycle";
1517c45f0082SYong Li     }
1518c45f0082SYong Li     else if (rfAction == "PowerDown")
1519c45f0082SYong Li     {
1520c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.PowerOff";
1521c45f0082SYong Li     }
1522c45f0082SYong Li     else if (rfAction == "ResetSystem")
1523c45f0082SYong Li     {
1524c45f0082SYong Li         return "xyz.openbmc_project.State.Watchdog.Action.HardReset";
1525c45f0082SYong Li     }
1526c45f0082SYong Li 
1527c45f0082SYong Li     return "";
1528c45f0082SYong Li }
1529c45f0082SYong Li 
1530c45f0082SYong Li /**
153151709ffdSYong Li  * @brief Retrieves host watchdog timer properties over DBUS
153251709ffdSYong Li  *
153351709ffdSYong Li  * @param[in] aResp     Shared pointer for completing asynchronous calls.
153451709ffdSYong Li  *
153551709ffdSYong Li  * @return None.
153651709ffdSYong Li  */
153723a21a1cSEd Tanous inline void getHostWatchdogTimer(std::shared_ptr<AsyncResp> aResp)
153851709ffdSYong Li {
153951709ffdSYong Li     BMCWEB_LOG_DEBUG << "Get host watchodg";
154051709ffdSYong Li     crow::connections::systemBus->async_method_call(
154151709ffdSYong Li         [aResp](const boost::system::error_code ec,
154251709ffdSYong Li                 PropertiesType& properties) {
154351709ffdSYong Li             if (ec)
154451709ffdSYong Li             {
154551709ffdSYong Li                 // watchdog service is stopped
154651709ffdSYong Li                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
154751709ffdSYong Li                 return;
154851709ffdSYong Li             }
154951709ffdSYong Li 
155051709ffdSYong Li             BMCWEB_LOG_DEBUG << "Got " << properties.size() << " wdt prop.";
155151709ffdSYong Li 
155251709ffdSYong Li             nlohmann::json& hostWatchdogTimer =
155351709ffdSYong Li                 aResp->res.jsonValue["HostWatchdogTimer"];
155451709ffdSYong Li 
155551709ffdSYong Li             // watchdog service is running/enabled
155651709ffdSYong Li             hostWatchdogTimer["Status"]["State"] = "Enabled";
155751709ffdSYong Li 
155851709ffdSYong Li             for (const auto& property : properties)
155951709ffdSYong Li             {
156051709ffdSYong Li                 BMCWEB_LOG_DEBUG << "prop=" << property.first;
156151709ffdSYong Li                 if (property.first == "Enabled")
156251709ffdSYong Li                 {
156351709ffdSYong Li                     const bool* state = std::get_if<bool>(&property.second);
156451709ffdSYong Li 
156551709ffdSYong Li                     if (!state)
156651709ffdSYong Li                     {
156751709ffdSYong Li                         messages::internalError(aResp->res);
156851709ffdSYong Li                         continue;
156951709ffdSYong Li                     }
157051709ffdSYong Li 
157151709ffdSYong Li                     hostWatchdogTimer["FunctionEnabled"] = *state;
157251709ffdSYong Li                 }
157351709ffdSYong Li                 else if (property.first == "ExpireAction")
157451709ffdSYong Li                 {
157551709ffdSYong Li                     const std::string* s =
157651709ffdSYong Li                         std::get_if<std::string>(&property.second);
157751709ffdSYong Li                     if (!s)
157851709ffdSYong Li                     {
157951709ffdSYong Li                         messages::internalError(aResp->res);
158051709ffdSYong Li                         continue;
158151709ffdSYong Li                     }
158251709ffdSYong Li 
158351709ffdSYong Li                     std::string action = dbusToRfWatchdogAction(*s);
158451709ffdSYong Li                     if (action.empty())
158551709ffdSYong Li                     {
158651709ffdSYong Li                         messages::internalError(aResp->res);
158751709ffdSYong Li                         continue;
158851709ffdSYong Li                     }
158951709ffdSYong Li                     hostWatchdogTimer["TimeoutAction"] = action;
159051709ffdSYong Li                 }
159151709ffdSYong Li             }
159251709ffdSYong Li         },
159351709ffdSYong Li         "xyz.openbmc_project.Watchdog", "/xyz/openbmc_project/watchdog/host0",
159451709ffdSYong Li         "org.freedesktop.DBus.Properties", "GetAll",
159551709ffdSYong Li         "xyz.openbmc_project.State.Watchdog");
159651709ffdSYong Li }
159751709ffdSYong Li 
159851709ffdSYong Li /**
1599c45f0082SYong Li  * @brief Sets Host WatchDog Timer properties.
1600c45f0082SYong Li  *
1601c45f0082SYong Li  * @param[in] aResp      Shared pointer for generating response message.
1602c45f0082SYong Li  * @param[in] wdtEnable  The WDTimer Enable value (true/false) from incoming
1603c45f0082SYong Li  *                       RF request.
1604c45f0082SYong Li  * @param[in] wdtTimeOutAction The WDT Timeout action, from incoming RF request.
1605c45f0082SYong Li  *
1606c45f0082SYong Li  * @return None.
1607c45f0082SYong Li  */
160823a21a1cSEd Tanous inline void setWDTProperties(std::shared_ptr<AsyncResp> aResp,
1609c45f0082SYong Li                              const std::optional<bool> wdtEnable,
1610c45f0082SYong Li                              const std::optional<std::string>& wdtTimeOutAction)
1611c45f0082SYong Li {
1612c45f0082SYong Li     BMCWEB_LOG_DEBUG << "Set host watchdog";
1613c45f0082SYong Li 
1614c45f0082SYong Li     if (wdtTimeOutAction)
1615c45f0082SYong Li     {
1616c45f0082SYong Li         std::string wdtTimeOutActStr = rfToDbusWDTTimeOutAct(*wdtTimeOutAction);
1617c45f0082SYong Li         // check if TimeOut Action is Valid
1618c45f0082SYong Li         if (wdtTimeOutActStr.empty())
1619c45f0082SYong Li         {
1620c45f0082SYong Li             BMCWEB_LOG_DEBUG << "Unsupported value for TimeoutAction: "
1621c45f0082SYong Li                              << *wdtTimeOutAction;
1622c45f0082SYong Li             messages::propertyValueNotInList(aResp->res, *wdtTimeOutAction,
1623c45f0082SYong Li                                              "TimeoutAction");
1624c45f0082SYong Li             return;
1625c45f0082SYong Li         }
1626c45f0082SYong Li 
1627c45f0082SYong Li         crow::connections::systemBus->async_method_call(
1628c45f0082SYong Li             [aResp](const boost::system::error_code ec) {
1629c45f0082SYong Li                 if (ec)
1630c45f0082SYong Li                 {
1631c45f0082SYong Li                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1632c45f0082SYong Li                     messages::internalError(aResp->res);
1633c45f0082SYong Li                     return;
1634c45f0082SYong Li                 }
1635c45f0082SYong Li             },
1636c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
1637c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
1638c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
1639c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "ExpireAction",
1640c45f0082SYong Li             std::variant<std::string>(wdtTimeOutActStr));
1641c45f0082SYong Li     }
1642c45f0082SYong Li 
1643c45f0082SYong Li     if (wdtEnable)
1644c45f0082SYong Li     {
1645c45f0082SYong Li         crow::connections::systemBus->async_method_call(
1646c45f0082SYong Li             [aResp](const boost::system::error_code ec) {
1647c45f0082SYong Li                 if (ec)
1648c45f0082SYong Li                 {
1649c45f0082SYong Li                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1650c45f0082SYong Li                     messages::internalError(aResp->res);
1651c45f0082SYong Li                     return;
1652c45f0082SYong Li                 }
1653c45f0082SYong Li             },
1654c45f0082SYong Li             "xyz.openbmc_project.Watchdog",
1655c45f0082SYong Li             "/xyz/openbmc_project/watchdog/host0",
1656c45f0082SYong Li             "org.freedesktop.DBus.Properties", "Set",
1657c45f0082SYong Li             "xyz.openbmc_project.State.Watchdog", "Enabled",
1658c45f0082SYong Li             std::variant<bool>(*wdtEnable));
1659c45f0082SYong Li     }
1660c45f0082SYong Li }
1661c45f0082SYong Li 
1662c45f0082SYong Li /**
1663c5b2abe0SLewanczyk, Dawid  * SystemsCollection derived class for delivering ComputerSystems Collection
1664c5b2abe0SLewanczyk, Dawid  * Schema
1665c5b2abe0SLewanczyk, Dawid  */
16661abe55efSEd Tanous class SystemsCollection : public Node
16671abe55efSEd Tanous {
1668c5b2abe0SLewanczyk, Dawid   public:
166952cc112dSEd Tanous     SystemsCollection(App& app) : Node(app, "/redfish/v1/Systems/")
16701abe55efSEd Tanous     {
1671c5b2abe0SLewanczyk, Dawid         entityPrivileges = {
1672c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::get, {{"Login"}}},
1673c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::head, {{"Login"}}},
1674c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1675c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1676c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1677c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1678c5b2abe0SLewanczyk, Dawid     }
1679c5b2abe0SLewanczyk, Dawid 
1680c5b2abe0SLewanczyk, Dawid   private:
1681cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
1682cb13a392SEd Tanous                const std::vector<std::string>&) override
16831abe55efSEd Tanous     {
1684462023adSSunitha Harish         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
16850f74e643SEd Tanous         res.jsonValue["@odata.type"] =
16860f74e643SEd Tanous             "#ComputerSystemCollection.ComputerSystemCollection";
16870f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Systems";
16880f74e643SEd Tanous         res.jsonValue["Name"] = "Computer System Collection";
1689462023adSSunitha Harish 
1690462023adSSunitha Harish         crow::connections::systemBus->async_method_call(
1691462023adSSunitha Harish             [asyncResp](const boost::system::error_code ec,
1692cb13a392SEd Tanous                         const std::variant<std::string>& /*hostName*/) {
1693462023adSSunitha Harish                 nlohmann::json& iface_array =
1694462023adSSunitha Harish                     asyncResp->res.jsonValue["Members"];
1695462023adSSunitha Harish                 iface_array = nlohmann::json::array();
1696462023adSSunitha Harish                 auto& count = asyncResp->res.jsonValue["Members@odata.count"];
1697462023adSSunitha Harish                 count = 0;
1698cb13a392SEd Tanous                 iface_array.push_back(
1699cb13a392SEd Tanous                     {{"@odata.id", "/redfish/v1/Systems/system"}});
1700cb13a392SEd Tanous                 if (!ec)
1701462023adSSunitha Harish                 {
1702462023adSSunitha Harish                     BMCWEB_LOG_DEBUG << "Hypervisor is available";
1703462023adSSunitha Harish                     iface_array.push_back(
1704462023adSSunitha Harish                         {{"@odata.id", "/redfish/v1/Systems/hypervisor"}});
1705462023adSSunitha Harish                     count = iface_array.size();
1706cb13a392SEd Tanous                     return;
1707cb13a392SEd Tanous                 }
1708462023adSSunitha Harish             },
17098e651fbfSSunitha Harish             "xyz.openbmc_project.Settings",
17108e651fbfSSunitha Harish             "/xyz/openbmc_project/network/hypervisor",
1711462023adSSunitha Harish             "org.freedesktop.DBus.Properties", "Get",
1712462023adSSunitha Harish             "xyz.openbmc_project.Network.SystemConfiguration", "HostName");
1713c5b2abe0SLewanczyk, Dawid     }
1714c5b2abe0SLewanczyk, Dawid };
1715c5b2abe0SLewanczyk, Dawid 
1716c5b2abe0SLewanczyk, Dawid /**
1717cc340dd9SEd Tanous  * SystemActionsReset class supports handle POST method for Reset action.
1718cc340dd9SEd Tanous  * The class retrieves and sends data directly to D-Bus.
1719cc340dd9SEd Tanous  */
1720cc340dd9SEd Tanous class SystemActionsReset : public Node
1721cc340dd9SEd Tanous {
1722cc340dd9SEd Tanous   public:
172352cc112dSEd Tanous     SystemActionsReset(App& app) :
1724029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset/")
1725cc340dd9SEd Tanous     {
1726cc340dd9SEd Tanous         entityPrivileges = {
1727cc340dd9SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1728cc340dd9SEd Tanous     }
1729cc340dd9SEd Tanous 
1730cc340dd9SEd Tanous   private:
1731cc340dd9SEd Tanous     /**
1732cc340dd9SEd Tanous      * Function handles POST method request.
1733cc340dd9SEd Tanous      * Analyzes POST body message before sends Reset request data to D-Bus.
1734cc340dd9SEd Tanous      */
1735cc340dd9SEd Tanous     void doPost(crow::Response& res, const crow::Request& req,
1736cb13a392SEd Tanous                 const std::vector<std::string>&) override
1737cc340dd9SEd Tanous     {
1738cc340dd9SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1739cc340dd9SEd Tanous 
17409712f8acSEd Tanous         std::string resetType;
17419712f8acSEd Tanous         if (!json_util::readJson(req, res, "ResetType", resetType))
1742cc340dd9SEd Tanous         {
1743cc340dd9SEd Tanous             return;
1744cc340dd9SEd Tanous         }
1745cc340dd9SEd Tanous 
1746d22c8396SJason M. Bills         // Get the command and host vs. chassis
1747cc340dd9SEd Tanous         std::string command;
1748d22c8396SJason M. Bills         bool hostCommand;
17499712f8acSEd Tanous         if (resetType == "On")
1750cc340dd9SEd Tanous         {
1751cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.On";
1752d22c8396SJason M. Bills             hostCommand = true;
1753d22c8396SJason M. Bills         }
1754d22c8396SJason M. Bills         else if (resetType == "ForceOff")
1755d22c8396SJason M. Bills         {
1756d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Chassis.Transition.Off";
1757d22c8396SJason M. Bills             hostCommand = false;
1758d22c8396SJason M. Bills         }
1759d22c8396SJason M. Bills         else if (resetType == "ForceOn")
1760d22c8396SJason M. Bills         {
1761d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Host.Transition.On";
1762d22c8396SJason M. Bills             hostCommand = true;
1763d22c8396SJason M. Bills         }
1764d22c8396SJason M. Bills         else if (resetType == "ForceRestart")
1765d22c8396SJason M. Bills         {
176686a0851aSJason M. Bills             command =
176786a0851aSJason M. Bills                 "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot";
176886a0851aSJason M. Bills             hostCommand = true;
1769cc340dd9SEd Tanous         }
17709712f8acSEd Tanous         else if (resetType == "GracefulShutdown")
1771cc340dd9SEd Tanous         {
1772cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.Off";
1773d22c8396SJason M. Bills             hostCommand = true;
1774cc340dd9SEd Tanous         }
17759712f8acSEd Tanous         else if (resetType == "GracefulRestart")
1776cc340dd9SEd Tanous         {
177786a0851aSJason M. Bills             command =
177886a0851aSJason M. Bills                 "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot";
1779d22c8396SJason M. Bills             hostCommand = true;
1780d22c8396SJason M. Bills         }
1781d22c8396SJason M. Bills         else if (resetType == "PowerCycle")
1782d22c8396SJason M. Bills         {
178386a0851aSJason M. Bills             command = "xyz.openbmc_project.State.Host.Transition.Reboot";
178486a0851aSJason M. Bills             hostCommand = true;
1785cc340dd9SEd Tanous         }
1786bfd5b826SLakshminarayana R. Kammath         else if (resetType == "Nmi")
1787bfd5b826SLakshminarayana R. Kammath         {
1788bfd5b826SLakshminarayana R. Kammath             doNMI(asyncResp);
1789bfd5b826SLakshminarayana R. Kammath             return;
1790bfd5b826SLakshminarayana R. Kammath         }
1791cc340dd9SEd Tanous         else
1792cc340dd9SEd Tanous         {
1793f12894f8SJason M. Bills             messages::actionParameterUnknown(res, "Reset", resetType);
1794cc340dd9SEd Tanous             return;
1795cc340dd9SEd Tanous         }
1796cc340dd9SEd Tanous 
1797d22c8396SJason M. Bills         if (hostCommand)
1798d22c8396SJason M. Bills         {
1799cc340dd9SEd Tanous             crow::connections::systemBus->async_method_call(
1800d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
1801cc340dd9SEd Tanous                     if (ec)
1802cc340dd9SEd Tanous                     {
1803cc340dd9SEd Tanous                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1804d22c8396SJason M. Bills                         if (ec.value() == boost::asio::error::invalid_argument)
1805d22c8396SJason M. Bills                         {
1806d22c8396SJason M. Bills                             messages::actionParameterNotSupported(
1807d22c8396SJason M. Bills                                 asyncResp->res, resetType, "Reset");
1808d22c8396SJason M. Bills                         }
1809d22c8396SJason M. Bills                         else
1810d22c8396SJason M. Bills                         {
1811f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
1812d22c8396SJason M. Bills                         }
1813cc340dd9SEd Tanous                         return;
1814cc340dd9SEd Tanous                     }
1815f12894f8SJason M. Bills                     messages::success(asyncResp->res);
1816cc340dd9SEd Tanous                 },
1817cc340dd9SEd Tanous                 "xyz.openbmc_project.State.Host",
1818cc340dd9SEd Tanous                 "/xyz/openbmc_project/state/host0",
1819cc340dd9SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
18209712f8acSEd Tanous                 "xyz.openbmc_project.State.Host", "RequestedHostTransition",
1821abf2add6SEd Tanous                 std::variant<std::string>{command});
1822cc340dd9SEd Tanous         }
1823d22c8396SJason M. Bills         else
1824d22c8396SJason M. Bills         {
1825d22c8396SJason M. Bills             crow::connections::systemBus->async_method_call(
1826d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
1827d22c8396SJason M. Bills                     if (ec)
1828d22c8396SJason M. Bills                     {
1829d22c8396SJason M. Bills                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1830d22c8396SJason M. Bills                         if (ec.value() == boost::asio::error::invalid_argument)
1831d22c8396SJason M. Bills                         {
1832d22c8396SJason M. Bills                             messages::actionParameterNotSupported(
1833d22c8396SJason M. Bills                                 asyncResp->res, resetType, "Reset");
1834d22c8396SJason M. Bills                         }
1835d22c8396SJason M. Bills                         else
1836d22c8396SJason M. Bills                         {
1837d22c8396SJason M. Bills                             messages::internalError(asyncResp->res);
1838d22c8396SJason M. Bills                         }
1839d22c8396SJason M. Bills                         return;
1840d22c8396SJason M. Bills                     }
1841d22c8396SJason M. Bills                     messages::success(asyncResp->res);
1842d22c8396SJason M. Bills                 },
1843d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis",
1844d22c8396SJason M. Bills                 "/xyz/openbmc_project/state/chassis0",
1845d22c8396SJason M. Bills                 "org.freedesktop.DBus.Properties", "Set",
1846d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition",
1847d22c8396SJason M. Bills                 std::variant<std::string>{command});
1848d22c8396SJason M. Bills         }
1849d22c8396SJason M. Bills     }
1850bfd5b826SLakshminarayana R. Kammath     /**
1851bfd5b826SLakshminarayana R. Kammath      * Function transceives data with dbus directly.
1852bfd5b826SLakshminarayana R. Kammath      */
1853bfd5b826SLakshminarayana R. Kammath     void doNMI(const std::shared_ptr<AsyncResp>& asyncResp)
1854bfd5b826SLakshminarayana R. Kammath     {
1855bfd5b826SLakshminarayana R. Kammath         constexpr char const* serviceName =
1856bfd5b826SLakshminarayana R. Kammath             "xyz.openbmc_project.Control.Host.NMI";
1857bfd5b826SLakshminarayana R. Kammath         constexpr char const* objectPath =
1858bfd5b826SLakshminarayana R. Kammath             "/xyz/openbmc_project/control/host0/nmi";
1859bfd5b826SLakshminarayana R. Kammath         constexpr char const* interfaceName =
1860bfd5b826SLakshminarayana R. Kammath             "xyz.openbmc_project.Control.Host.NMI";
1861bfd5b826SLakshminarayana R. Kammath         constexpr char const* method = "NMI";
1862bfd5b826SLakshminarayana R. Kammath 
1863bfd5b826SLakshminarayana R. Kammath         crow::connections::systemBus->async_method_call(
1864bfd5b826SLakshminarayana R. Kammath             [asyncResp](const boost::system::error_code ec) {
1865bfd5b826SLakshminarayana R. Kammath                 if (ec)
1866bfd5b826SLakshminarayana R. Kammath                 {
1867bfd5b826SLakshminarayana R. Kammath                     BMCWEB_LOG_ERROR << " Bad D-Bus request error: " << ec;
1868bfd5b826SLakshminarayana R. Kammath                     messages::internalError(asyncResp->res);
1869bfd5b826SLakshminarayana R. Kammath                     return;
1870bfd5b826SLakshminarayana R. Kammath                 }
1871bfd5b826SLakshminarayana R. Kammath                 messages::success(asyncResp->res);
1872bfd5b826SLakshminarayana R. Kammath             },
1873bfd5b826SLakshminarayana R. Kammath             serviceName, objectPath, interfaceName, method);
1874bfd5b826SLakshminarayana R. Kammath     }
1875cc340dd9SEd Tanous };
1876cc340dd9SEd Tanous 
1877cc340dd9SEd Tanous /**
18786617338dSEd Tanous  * Systems derived class for delivering Computer Systems Schema.
1879c5b2abe0SLewanczyk, Dawid  */
18801abe55efSEd Tanous class Systems : public Node
18811abe55efSEd Tanous {
1882c5b2abe0SLewanczyk, Dawid   public:
1883c5b2abe0SLewanczyk, Dawid     /*
1884c5b2abe0SLewanczyk, Dawid      * Default Constructor
1885c5b2abe0SLewanczyk, Dawid      */
188652cc112dSEd Tanous     Systems(App& app) : Node(app, "/redfish/v1/Systems/system/")
18871abe55efSEd Tanous     {
1888c5b2abe0SLewanczyk, Dawid         entityPrivileges = {
1889c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::get, {{"Login"}}},
1890c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::head, {{"Login"}}},
1891c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1892c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1893c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1894c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1895c5b2abe0SLewanczyk, Dawid     }
1896c5b2abe0SLewanczyk, Dawid 
1897c5b2abe0SLewanczyk, Dawid   private:
1898c5b2abe0SLewanczyk, Dawid     /**
1899c5b2abe0SLewanczyk, Dawid      * Functions triggers appropriate requests on DBus
1900c5b2abe0SLewanczyk, Dawid      */
1901cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
1902cb13a392SEd Tanous                const std::vector<std::string>&) override
19031abe55efSEd Tanous     {
1904c0557e1aSGunnar Mills         res.jsonValue["@odata.type"] = "#ComputerSystem.v1_12_0.ComputerSystem";
1905450a25cbSGunnar Mills         res.jsonValue["Name"] = "system";
1906029573d4SEd Tanous         res.jsonValue["Id"] = "system";
19070f74e643SEd Tanous         res.jsonValue["SystemType"] = "Physical";
19080f74e643SEd Tanous         res.jsonValue["Description"] = "Computer System";
19090f74e643SEd Tanous         res.jsonValue["ProcessorSummary"]["Count"] = 0;
19100f74e643SEd Tanous         res.jsonValue["ProcessorSummary"]["Status"]["State"] = "Disabled";
19115fd7ba65SCheng C Yang         res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] = uint64_t(0);
19120f74e643SEd Tanous         res.jsonValue["MemorySummary"]["Status"]["State"] = "Disabled";
1913029573d4SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Systems/system";
191404a258f4SEd Tanous 
1915443c2934SRapkiewicz, Pawel         res.jsonValue["Processors"] = {
1916029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/Processors"}};
1917443c2934SRapkiewicz, Pawel         res.jsonValue["Memory"] = {
1918029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/Memory"}};
1919a25aeccfSNikhil Potade         res.jsonValue["Storage"] = {
1920a25aeccfSNikhil Potade             {"@odata.id", "/redfish/v1/Systems/system/Storage"}};
1921029573d4SEd Tanous 
1922cc340dd9SEd Tanous         res.jsonValue["Actions"]["#ComputerSystem.Reset"] = {
1923cc340dd9SEd Tanous             {"target",
1924029573d4SEd Tanous              "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset"},
19251cb1a9e6SAppaRao Puli             {"@Redfish.ActionInfo",
19261cb1a9e6SAppaRao Puli              "/redfish/v1/Systems/system/ResetActionInfo"}};
1927c5b2abe0SLewanczyk, Dawid 
1928c4bf6374SJason M. Bills         res.jsonValue["LogServices"] = {
1929029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/LogServices"}};
1930c4bf6374SJason M. Bills 
1931d82a3acdSCarol Wang         res.jsonValue["Bios"] = {
1932d82a3acdSCarol Wang             {"@odata.id", "/redfish/v1/Systems/system/Bios"}};
1933d82a3acdSCarol Wang 
1934c5d03ff4SJennifer Lee         res.jsonValue["Links"]["ManagedBy"] = {
1935c5d03ff4SJennifer Lee             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
1936c5d03ff4SJennifer Lee 
1937c5d03ff4SJennifer Lee         res.jsonValue["Status"] = {
1938c5d03ff4SJennifer Lee             {"Health", "OK"},
1939c5d03ff4SJennifer Lee             {"State", "Enabled"},
1940c5d03ff4SJennifer Lee         };
1941a0803efaSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1942c5b2abe0SLewanczyk, Dawid 
1943e284a7c1SJames Feist         constexpr const std::array<const char*, 4> inventoryForSystems = {
1944b49ac873SJames Feist             "xyz.openbmc_project.Inventory.Item.Dimm",
19452ad9c2f6SJames Feist             "xyz.openbmc_project.Inventory.Item.Cpu",
1946e284a7c1SJames Feist             "xyz.openbmc_project.Inventory.Item.Drive",
1947e284a7c1SJames Feist             "xyz.openbmc_project.Inventory.Item.StorageController"};
1948b49ac873SJames Feist 
1949b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
1950b49ac873SJames Feist         crow::connections::systemBus->async_method_call(
1951b49ac873SJames Feist             [health](const boost::system::error_code ec,
1952b49ac873SJames Feist                      std::vector<std::string>& resp) {
1953b49ac873SJames Feist                 if (ec)
1954b49ac873SJames Feist                 {
1955b49ac873SJames Feist                     // no inventory
1956b49ac873SJames Feist                     return;
1957b49ac873SJames Feist                 }
1958b49ac873SJames Feist 
1959b49ac873SJames Feist                 health->inventory = std::move(resp);
1960b49ac873SJames Feist             },
1961b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper",
1962b49ac873SJames Feist             "/xyz/openbmc_project/object_mapper",
1963b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1964b49ac873SJames Feist             int32_t(0), inventoryForSystems);
1965b49ac873SJames Feist 
1966b49ac873SJames Feist         health->populate();
1967b49ac873SJames Feist 
1968c5d03ff4SJennifer Lee         getMainChassisId(asyncResp, [](const std::string& chassisId,
1969c5d03ff4SJennifer Lee                                        std::shared_ptr<AsyncResp> aRsp) {
1970c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["Chassis"] = {
1971c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
1972c5d03ff4SJennifer Lee         });
1973a3002228SAppaRao Puli 
1974a3002228SAppaRao Puli         getIndicatorLedState(asyncResp);
19755bc2dc8eSJames Feist         getComputerSystem(asyncResp, health);
19766c34de48SEd Tanous         getHostState(asyncResp);
1977491d8ee7SSantosh Puranik         getBootProperties(asyncResp);
1978adbe192aSJason M. Bills         getPCIeDeviceList(asyncResp, "PCIeDevices");
197951709ffdSYong Li         getHostWatchdogTimer(asyncResp);
1980c6a620f2SGeorge Liu         getPowerRestorePolicy(asyncResp);
19816bd5a8d2SGunnar Mills         getAutomaticRetry(asyncResp);
1982c0557e1aSGunnar Mills         getLastResetTime(asyncResp);
1983a6349918SAppaRao Puli #ifdef BMCWEB_ENABLE_REDFISH_PROVISIONING_FEATURE
1984a6349918SAppaRao Puli         getProvisioningStatus(asyncResp);
1985a6349918SAppaRao Puli #endif
1986c5b2abe0SLewanczyk, Dawid     }
1987c5b2abe0SLewanczyk, Dawid 
198855c7b7a2SEd Tanous     void doPatch(crow::Response& res, const crow::Request& req,
1989cb13a392SEd Tanous                  const std::vector<std::string>&) override
19901abe55efSEd Tanous     {
1991cde19e5fSSantosh Puranik         std::optional<std::string> indicatorLed;
1992491d8ee7SSantosh Puranik         std::optional<nlohmann::json> bootProps;
1993c45f0082SYong Li         std::optional<nlohmann::json> wdtTimerProps;
1994c6a620f2SGeorge Liu         std::optional<std::string> powerRestorePolicy;
199541352c24SSantosh Puranik         auto asyncResp = std::make_shared<AsyncResp>(res);
199641352c24SSantosh Puranik 
1997944ffaf9SJohnathan Mantey         if (!json_util::readJson(req, res, "IndicatorLED", indicatorLed, "Boot",
1998c6a620f2SGeorge Liu                                  bootProps, "WatchdogTimer", wdtTimerProps,
1999c6a620f2SGeorge Liu                                  "PowerRestorePolicy", powerRestorePolicy))
20006617338dSEd Tanous         {
20016617338dSEd Tanous             return;
20026617338dSEd Tanous         }
2003491d8ee7SSantosh Puranik 
2004944ffaf9SJohnathan Mantey         res.result(boost::beast::http::status::no_content);
2005c45f0082SYong Li 
2006c45f0082SYong Li         if (wdtTimerProps)
2007c45f0082SYong Li         {
2008c45f0082SYong Li             std::optional<bool> wdtEnable;
2009c45f0082SYong Li             std::optional<std::string> wdtTimeOutAction;
2010c45f0082SYong Li 
2011c45f0082SYong Li             if (!json_util::readJson(*wdtTimerProps, asyncResp->res,
2012c45f0082SYong Li                                      "FunctionEnabled", wdtEnable,
2013c45f0082SYong Li                                      "TimeoutAction", wdtTimeOutAction))
2014c45f0082SYong Li             {
2015c45f0082SYong Li                 return;
2016c45f0082SYong Li             }
2017c45f0082SYong Li             setWDTProperties(asyncResp, std::move(wdtEnable),
2018c45f0082SYong Li                              std::move(wdtTimeOutAction));
2019c45f0082SYong Li         }
2020c45f0082SYong Li 
2021491d8ee7SSantosh Puranik         if (bootProps)
2022491d8ee7SSantosh Puranik         {
2023491d8ee7SSantosh Puranik             std::optional<std::string> bootSource;
2024491d8ee7SSantosh Puranik             std::optional<std::string> bootEnable;
202569f35306SGunnar Mills             std::optional<std::string> automaticRetryConfig;
2026491d8ee7SSantosh Puranik 
202769f35306SGunnar Mills             if (!json_util::readJson(
202869f35306SGunnar Mills                     *bootProps, asyncResp->res, "BootSourceOverrideTarget",
202969f35306SGunnar Mills                     bootSource, "BootSourceOverrideEnabled", bootEnable,
203069f35306SGunnar Mills                     "AutomaticRetryConfig", automaticRetryConfig))
2031491d8ee7SSantosh Puranik             {
2032491d8ee7SSantosh Puranik                 return;
2033491d8ee7SSantosh Puranik             }
203469f35306SGunnar Mills             if (bootSource || bootEnable)
203569f35306SGunnar Mills             {
203669f35306SGunnar Mills                 setBootSourceProperties(asyncResp, std::move(bootSource),
2037491d8ee7SSantosh Puranik                                         std::move(bootEnable));
2038491d8ee7SSantosh Puranik             }
203969f35306SGunnar Mills             if (automaticRetryConfig)
204069f35306SGunnar Mills             {
204169f35306SGunnar Mills                 setAutomaticRetry(asyncResp, std::move(*automaticRetryConfig));
204269f35306SGunnar Mills             }
204369f35306SGunnar Mills         }
2044265c1602SJohnathan Mantey 
20459712f8acSEd Tanous         if (indicatorLed)
20466617338dSEd Tanous         {
2047a3002228SAppaRao Puli             setIndicatorLedState(asyncResp, std::move(*indicatorLed));
20486617338dSEd Tanous         }
2049c6a620f2SGeorge Liu 
2050c6a620f2SGeorge Liu         if (powerRestorePolicy)
2051c6a620f2SGeorge Liu         {
2052c6a620f2SGeorge Liu             setPowerRestorePolicy(asyncResp, std::move(*powerRestorePolicy));
2053c6a620f2SGeorge Liu         }
2054c5b2abe0SLewanczyk, Dawid     }
2055c5b2abe0SLewanczyk, Dawid };
20561cb1a9e6SAppaRao Puli 
20571cb1a9e6SAppaRao Puli /**
20581cb1a9e6SAppaRao Puli  * SystemResetActionInfo derived class for delivering Computer Systems
20591cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
20601cb1a9e6SAppaRao Puli  */
20611cb1a9e6SAppaRao Puli class SystemResetActionInfo : public Node
20621cb1a9e6SAppaRao Puli {
20631cb1a9e6SAppaRao Puli   public:
20641cb1a9e6SAppaRao Puli     /*
20651cb1a9e6SAppaRao Puli      * Default Constructor
20661cb1a9e6SAppaRao Puli      */
206752cc112dSEd Tanous     SystemResetActionInfo(App& app) :
20681cb1a9e6SAppaRao Puli         Node(app, "/redfish/v1/Systems/system/ResetActionInfo/")
20691cb1a9e6SAppaRao Puli     {
20701cb1a9e6SAppaRao Puli         entityPrivileges = {
20711cb1a9e6SAppaRao Puli             {boost::beast::http::verb::get, {{"Login"}}},
20721cb1a9e6SAppaRao Puli             {boost::beast::http::verb::head, {{"Login"}}},
20731cb1a9e6SAppaRao Puli             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
20741cb1a9e6SAppaRao Puli             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
20751cb1a9e6SAppaRao Puli             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
20761cb1a9e6SAppaRao Puli             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
20771cb1a9e6SAppaRao Puli     }
20781cb1a9e6SAppaRao Puli 
20791cb1a9e6SAppaRao Puli   private:
20801cb1a9e6SAppaRao Puli     /**
20811cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
20821cb1a9e6SAppaRao Puli      */
2083cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
2084cb13a392SEd Tanous                const std::vector<std::string>&) override
20851cb1a9e6SAppaRao Puli     {
20861cb1a9e6SAppaRao Puli         res.jsonValue = {
20871cb1a9e6SAppaRao Puli             {"@odata.type", "#ActionInfo.v1_1_2.ActionInfo"},
20881cb1a9e6SAppaRao Puli             {"@odata.id", "/redfish/v1/Systems/system/ResetActionInfo"},
20891cb1a9e6SAppaRao Puli             {"Name", "Reset Action Info"},
20901cb1a9e6SAppaRao Puli             {"Id", "ResetActionInfo"},
20911cb1a9e6SAppaRao Puli             {"Parameters",
20921cb1a9e6SAppaRao Puli              {{{"Name", "ResetType"},
20931cb1a9e6SAppaRao Puli                {"Required", true},
20941cb1a9e6SAppaRao Puli                {"DataType", "String"},
20951cb1a9e6SAppaRao Puli                {"AllowableValues",
20961cb1a9e6SAppaRao Puli                 {"On", "ForceOff", "ForceOn", "ForceRestart", "GracefulRestart",
20971cb1a9e6SAppaRao Puli                  "GracefulShutdown", "PowerCycle", "Nmi"}}}}}};
20981cb1a9e6SAppaRao Puli         res.end();
20991cb1a9e6SAppaRao Puli     }
21001cb1a9e6SAppaRao Puli };
2101c5b2abe0SLewanczyk, Dawid } // namespace redfish
2102