xref: /openbmc/bmcweb/features/redfish/lib/systems.hpp (revision 9d3ae10e0f1b7a694678ef6b4b051a3d9836307f)
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"
19f5c9f8bdSJason M. Bills #include "pcie.hpp"
20c5d03ff4SJennifer Lee #include "redfish_util.hpp"
21c5d03ff4SJennifer Lee 
229712f8acSEd Tanous #include <boost/container/flat_map.hpp>
239712f8acSEd Tanous #include <node.hpp>
24cb7e1e7bSAndrew Geissler #include <utils/fw_utils.hpp>
25c5b2abe0SLewanczyk, Dawid #include <utils/json_utils.hpp>
26abf2add6SEd Tanous #include <variant>
27c5b2abe0SLewanczyk, Dawid 
281abe55efSEd Tanous namespace redfish
291abe55efSEd Tanous {
30c5b2abe0SLewanczyk, Dawid 
31*9d3ae10eSAlpana Kumari /**
32*9d3ae10eSAlpana Kumari  * @brief Updates the Functional State of DIMMs
33*9d3ae10eSAlpana Kumari  *
34*9d3ae10eSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
35*9d3ae10eSAlpana Kumari  * @param[in] dimmState Dimm's Functional state, true/false
36*9d3ae10eSAlpana Kumari  *
37*9d3ae10eSAlpana Kumari  * @return None.
38*9d3ae10eSAlpana Kumari  */
39*9d3ae10eSAlpana Kumari void updateDimmProperties(std::shared_ptr<AsyncResp> aResp,
40*9d3ae10eSAlpana Kumari                           const std::variant<bool> &dimmState)
41*9d3ae10eSAlpana Kumari {
42*9d3ae10eSAlpana Kumari     const bool *isDimmFunctional = std::get_if<bool>(&dimmState);
43*9d3ae10eSAlpana Kumari     if (isDimmFunctional == nullptr)
44*9d3ae10eSAlpana Kumari     {
45*9d3ae10eSAlpana Kumari         messages::internalError(aResp->res);
46*9d3ae10eSAlpana Kumari         return;
47*9d3ae10eSAlpana Kumari     }
48*9d3ae10eSAlpana Kumari     BMCWEB_LOG_DEBUG << "Dimm Functional:" << *isDimmFunctional;
49*9d3ae10eSAlpana Kumari 
50*9d3ae10eSAlpana Kumari     // Set it as Enabled if atleast one DIMM is functional
51*9d3ae10eSAlpana Kumari     // Update STATE only if previous State was DISABLED and current Dimm is
52*9d3ae10eSAlpana Kumari     // ENABLED.
53*9d3ae10eSAlpana Kumari     nlohmann::json &prevMemSummary =
54*9d3ae10eSAlpana Kumari         aResp->res.jsonValue["MemorySummary"]["Status"]["State"];
55*9d3ae10eSAlpana Kumari     if (prevMemSummary == "Disabled")
56*9d3ae10eSAlpana Kumari     {
57*9d3ae10eSAlpana Kumari         if (*isDimmFunctional == true)
58*9d3ae10eSAlpana Kumari         {
59*9d3ae10eSAlpana Kumari             aResp->res.jsonValue["MemorySummary"]["Status"]["State"] =
60*9d3ae10eSAlpana Kumari                 "Enabled";
61*9d3ae10eSAlpana Kumari         }
62*9d3ae10eSAlpana Kumari     }
63*9d3ae10eSAlpana Kumari }
64*9d3ae10eSAlpana Kumari 
6557e8c9beSAlpana Kumari /*
6657e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Count" based on Cpu PresenceState
6757e8c9beSAlpana Kumari  *
6857e8c9beSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
6957e8c9beSAlpana Kumari  * @param[in] cpuPresenceState CPU present or not
7057e8c9beSAlpana Kumari  *
7157e8c9beSAlpana Kumari  * @return None.
7257e8c9beSAlpana Kumari  */
7357e8c9beSAlpana Kumari void modifyCpuPresenceState(std::shared_ptr<AsyncResp> aResp,
7457e8c9beSAlpana Kumari                             const std::variant<bool> &cpuPresenceState)
7557e8c9beSAlpana Kumari {
7657e8c9beSAlpana Kumari     const bool *isCpuPresent = std::get_if<bool>(&cpuPresenceState);
7757e8c9beSAlpana Kumari 
7857e8c9beSAlpana Kumari     if (isCpuPresent == nullptr)
7957e8c9beSAlpana Kumari     {
8057e8c9beSAlpana Kumari         messages::internalError(aResp->res);
8157e8c9beSAlpana Kumari         return;
8257e8c9beSAlpana Kumari     }
8357e8c9beSAlpana Kumari     BMCWEB_LOG_DEBUG << "Cpu Present:" << *isCpuPresent;
8457e8c9beSAlpana Kumari 
8557e8c9beSAlpana Kumari     nlohmann::json &procCount =
8657e8c9beSAlpana Kumari         aResp->res.jsonValue["ProcessorSummary"]["Count"];
8757e8c9beSAlpana Kumari     if (*isCpuPresent == true)
8857e8c9beSAlpana Kumari     {
8957e8c9beSAlpana Kumari         procCount = procCount.get<int>() + 1;
9057e8c9beSAlpana Kumari     }
9157e8c9beSAlpana Kumari     aResp->res.jsonValue["ProcessorSummary"]["Count"] = procCount;
9257e8c9beSAlpana Kumari }
9357e8c9beSAlpana Kumari 
9457e8c9beSAlpana Kumari /*
9557e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Status" "State" based on
9657e8c9beSAlpana Kumari  *        CPU Functional State
9757e8c9beSAlpana Kumari  *
9857e8c9beSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
9957e8c9beSAlpana Kumari  * @param[in] cpuFunctionalState is CPU functional true/false
10057e8c9beSAlpana Kumari  *
10157e8c9beSAlpana Kumari  * @return None.
10257e8c9beSAlpana Kumari  */
10357e8c9beSAlpana Kumari void modifyCpuFunctionalState(std::shared_ptr<AsyncResp> aResp,
10457e8c9beSAlpana Kumari                               const std::variant<bool> &cpuFunctionalState)
10557e8c9beSAlpana Kumari {
10657e8c9beSAlpana Kumari     const bool *isCpuFunctional = std::get_if<bool>(&cpuFunctionalState);
10757e8c9beSAlpana Kumari 
10857e8c9beSAlpana Kumari     if (isCpuFunctional == nullptr)
10957e8c9beSAlpana Kumari     {
11057e8c9beSAlpana Kumari         messages::internalError(aResp->res);
11157e8c9beSAlpana Kumari         return;
11257e8c9beSAlpana Kumari     }
11357e8c9beSAlpana Kumari     BMCWEB_LOG_DEBUG << "Cpu Functional:" << *isCpuFunctional;
11457e8c9beSAlpana Kumari 
11557e8c9beSAlpana Kumari     nlohmann::json &prevProcState =
11657e8c9beSAlpana Kumari         aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"];
11757e8c9beSAlpana Kumari 
11857e8c9beSAlpana Kumari     // Set it as Enabled if atleast one CPU is functional
11957e8c9beSAlpana Kumari     // Update STATE only if previous State was Non_Functional and current CPU is
12057e8c9beSAlpana Kumari     // Functional.
12157e8c9beSAlpana Kumari     if (prevProcState == "Disabled")
12257e8c9beSAlpana Kumari     {
12357e8c9beSAlpana Kumari         if (*isCpuFunctional == true)
12457e8c9beSAlpana Kumari         {
12557e8c9beSAlpana Kumari             aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
12657e8c9beSAlpana Kumari                 "Enabled";
12757e8c9beSAlpana Kumari         }
12857e8c9beSAlpana Kumari     }
12957e8c9beSAlpana Kumari }
13057e8c9beSAlpana Kumari 
13157e8c9beSAlpana Kumari /*
132c5b2abe0SLewanczyk, Dawid  * @brief Retrieves computer system properties over dbus
133c5b2abe0SLewanczyk, Dawid  *
134c5b2abe0SLewanczyk, Dawid  * @param[in] aResp Shared pointer for completing asynchronous calls
135c5b2abe0SLewanczyk, Dawid  * @param[in] name  Computer system name from request
136c5b2abe0SLewanczyk, Dawid  *
137c5b2abe0SLewanczyk, Dawid  * @return None.
138c5b2abe0SLewanczyk, Dawid  */
139029573d4SEd Tanous void getComputerSystem(std::shared_ptr<AsyncResp> aResp)
1401abe55efSEd Tanous {
14155c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get available system components.";
142*9d3ae10eSAlpana Kumari 
14355c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
144c5d03ff4SJennifer Lee         [aResp](
145c5b2abe0SLewanczyk, Dawid             const boost::system::error_code ec,
146c5b2abe0SLewanczyk, Dawid             const std::vector<std::pair<
1476c34de48SEd Tanous                 std::string,
1486c34de48SEd Tanous                 std::vector<std::pair<std::string, std::vector<std::string>>>>>
149c5b2abe0SLewanczyk, Dawid                 &subtree) {
1501abe55efSEd Tanous             if (ec)
1511abe55efSEd Tanous             {
15255c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error";
153f12894f8SJason M. Bills                 messages::internalError(aResp->res);
154c5b2abe0SLewanczyk, Dawid                 return;
155c5b2abe0SLewanczyk, Dawid             }
156c5b2abe0SLewanczyk, Dawid             // Iterate over all retrieved ObjectPaths.
1576c34de48SEd Tanous             for (const std::pair<std::string,
1586c34de48SEd Tanous                                  std::vector<std::pair<
1596c34de48SEd Tanous                                      std::string, std::vector<std::string>>>>
1601abe55efSEd Tanous                      &object : subtree)
1611abe55efSEd Tanous             {
162c5b2abe0SLewanczyk, Dawid                 const std::string &path = object.first;
16355c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "Got path: " << path;
1641abe55efSEd Tanous                 const std::vector<
1651abe55efSEd Tanous                     std::pair<std::string, std::vector<std::string>>>
166c5b2abe0SLewanczyk, Dawid                     &connectionNames = object.second;
1671abe55efSEd Tanous                 if (connectionNames.size() < 1)
1681abe55efSEd Tanous                 {
169c5b2abe0SLewanczyk, Dawid                     continue;
170c5b2abe0SLewanczyk, Dawid                 }
171029573d4SEd Tanous 
1726c34de48SEd Tanous                 // This is not system, so check if it's cpu, dimm, UUID or
1736c34de48SEd Tanous                 // BiosVer
17404a258f4SEd Tanous                 for (const auto &connection : connectionNames)
1751abe55efSEd Tanous                 {
17604a258f4SEd Tanous                     for (const auto &interfaceName : connection.second)
1771abe55efSEd Tanous                     {
17804a258f4SEd Tanous                         if (interfaceName ==
17904a258f4SEd Tanous                             "xyz.openbmc_project.Inventory.Item.Dimm")
1801abe55efSEd Tanous                         {
1811abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
18204a258f4SEd Tanous                                 << "Found Dimm, now get its properties.";
183*9d3ae10eSAlpana Kumari 
18455c7b7a2SEd Tanous                             crow::connections::systemBus->async_method_call(
185*9d3ae10eSAlpana Kumari                                 [aResp, service{connection.first},
186*9d3ae10eSAlpana Kumari                                  path(std::move(path))](
187*9d3ae10eSAlpana Kumari                                     const boost::system::error_code ec,
1886c34de48SEd Tanous                                     const std::vector<
1896c34de48SEd Tanous                                         std::pair<std::string, VariantType>>
1901abe55efSEd Tanous                                         &properties) {
1911abe55efSEd Tanous                                     if (ec)
1921abe55efSEd Tanous                                     {
1931abe55efSEd Tanous                                         BMCWEB_LOG_ERROR
1946c34de48SEd Tanous                                             << "DBUS response error " << ec;
195f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
196c5b2abe0SLewanczyk, Dawid                                         return;
197c5b2abe0SLewanczyk, Dawid                                     }
1986c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
1996c34de48SEd Tanous                                                      << properties.size()
200c5b2abe0SLewanczyk, Dawid                                                      << "Dimm properties.";
201*9d3ae10eSAlpana Kumari 
202*9d3ae10eSAlpana Kumari                                     if (properties.size() > 0)
203*9d3ae10eSAlpana Kumari                                     {
20404a258f4SEd Tanous                                         for (const std::pair<std::string,
20504a258f4SEd Tanous                                                              VariantType>
20604a258f4SEd Tanous                                                  &property : properties)
2071abe55efSEd Tanous                                         {
208*9d3ae10eSAlpana Kumari                                             if (property.first ==
209*9d3ae10eSAlpana Kumari                                                 "MemorySizeInKb")
2101abe55efSEd Tanous                                             {
21104a258f4SEd Tanous                                                 const uint64_t *value =
212*9d3ae10eSAlpana Kumari                                                     sdbusplus::message::
213*9d3ae10eSAlpana Kumari                                                         variant_ns::get_if<
214*9d3ae10eSAlpana Kumari                                                             uint64_t>(
2151b6b96c5SEd Tanous                                                             &property.second);
21604a258f4SEd Tanous                                                 if (value != nullptr)
2171abe55efSEd Tanous                                                 {
2181abe55efSEd Tanous                                                     aResp->res.jsonValue
2196c34de48SEd Tanous                                                         ["TotalSystemMemoryGi"
2206c34de48SEd Tanous                                                          "B"] +=
22104a258f4SEd Tanous                                                         *value / (1024 * 1024);
222*9d3ae10eSAlpana Kumari                                                     aResp->res.jsonValue
223*9d3ae10eSAlpana Kumari                                                         ["MemorySummary"]
224*9d3ae10eSAlpana Kumari                                                         ["Status"]["State"] =
2251abe55efSEd Tanous                                                         "Enabled";
226c5b2abe0SLewanczyk, Dawid                                                 }
227c5b2abe0SLewanczyk, Dawid                                             }
228c5b2abe0SLewanczyk, Dawid                                         }
229*9d3ae10eSAlpana Kumari                                     }
230*9d3ae10eSAlpana Kumari                                     else
231*9d3ae10eSAlpana Kumari                                     {
232*9d3ae10eSAlpana Kumari                                         auto getDimmProperties =
233*9d3ae10eSAlpana Kumari                                             [aResp](
234*9d3ae10eSAlpana Kumari                                                 const boost::system::error_code
235*9d3ae10eSAlpana Kumari                                                     ec,
236*9d3ae10eSAlpana Kumari                                                 const std::variant<bool>
237*9d3ae10eSAlpana Kumari                                                     &dimmState) {
238*9d3ae10eSAlpana Kumari                                                 if (ec)
239*9d3ae10eSAlpana Kumari                                                 {
240*9d3ae10eSAlpana Kumari                                                     BMCWEB_LOG_ERROR
241*9d3ae10eSAlpana Kumari                                                         << "DBUS response "
242*9d3ae10eSAlpana Kumari                                                            "error "
243*9d3ae10eSAlpana Kumari                                                         << ec;
244*9d3ae10eSAlpana Kumari                                                     return;
245*9d3ae10eSAlpana Kumari                                                 }
246*9d3ae10eSAlpana Kumari                                                 updateDimmProperties(aResp,
247*9d3ae10eSAlpana Kumari                                                                      dimmState);
248*9d3ae10eSAlpana Kumari                                             };
249*9d3ae10eSAlpana Kumari                                         crow::connections::systemBus
250*9d3ae10eSAlpana Kumari                                             ->async_method_call(
251*9d3ae10eSAlpana Kumari                                                 std::move(getDimmProperties),
252*9d3ae10eSAlpana Kumari                                                 service, path,
253*9d3ae10eSAlpana Kumari                                                 "org.freedesktop.DBus."
254*9d3ae10eSAlpana Kumari                                                 "Properties",
255*9d3ae10eSAlpana Kumari                                                 "Get",
256*9d3ae10eSAlpana Kumari                                                 "xyz.openbmc_project.State."
257*9d3ae10eSAlpana Kumari                                                 "Decorator.OperationalStatus",
258*9d3ae10eSAlpana Kumari                                                 "Functional");
259*9d3ae10eSAlpana Kumari                                     }
260c5b2abe0SLewanczyk, Dawid                                 },
26104a258f4SEd Tanous                                 connection.first, path,
2626c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
2636c34de48SEd Tanous                                 "xyz.openbmc_project.Inventory.Item.Dimm");
2641abe55efSEd Tanous                         }
26504a258f4SEd Tanous                         else if (interfaceName ==
26604a258f4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.Cpu")
2671abe55efSEd Tanous                         {
2681abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
26904a258f4SEd Tanous                                 << "Found Cpu, now get its properties.";
27057e8c9beSAlpana Kumari 
271a0803efaSEd Tanous                             crow::connections::systemBus->async_method_call(
27257e8c9beSAlpana Kumari                                 [aResp, service{connection.first},
27357e8c9beSAlpana Kumari                                  path(std::move(path))](
27457e8c9beSAlpana Kumari                                     const boost::system::error_code ec,
2756c34de48SEd Tanous                                     const std::vector<
2766c34de48SEd Tanous                                         std::pair<std::string, VariantType>>
2771abe55efSEd Tanous                                         &properties) {
2781abe55efSEd Tanous                                     if (ec)
2791abe55efSEd Tanous                                     {
2801abe55efSEd Tanous                                         BMCWEB_LOG_ERROR
2816c34de48SEd Tanous                                             << "DBUS response error " << ec;
282f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
283c5b2abe0SLewanczyk, Dawid                                         return;
284c5b2abe0SLewanczyk, Dawid                                     }
2856c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
2866c34de48SEd Tanous                                                      << properties.size()
287c5b2abe0SLewanczyk, Dawid                                                      << "Cpu properties.";
28857e8c9beSAlpana Kumari 
28957e8c9beSAlpana Kumari                                     if (properties.size() > 0)
29057e8c9beSAlpana Kumari                                     {
29104a258f4SEd Tanous                                         for (const auto &property : properties)
2921abe55efSEd Tanous                                         {
29357e8c9beSAlpana Kumari                                             if (property.first ==
29457e8c9beSAlpana Kumari                                                 "ProcessorFamily")
2951abe55efSEd Tanous                                             {
296a0803efaSEd Tanous                                                 const std::string *value =
29757e8c9beSAlpana Kumari                                                     sdbusplus::message::
29857e8c9beSAlpana Kumari                                                         variant_ns::get_if<
29957e8c9beSAlpana Kumari                                                             std::string>(
3001b6b96c5SEd Tanous                                                             &property.second);
3011abe55efSEd Tanous                                                 if (value != nullptr)
3021abe55efSEd Tanous                                                 {
30357e8c9beSAlpana Kumari                                                     nlohmann::json
30457e8c9beSAlpana Kumari                                                         &procSummary =
3051abe55efSEd Tanous                                                             aResp->res.jsonValue
3066c34de48SEd Tanous                                                                 ["ProcessorSumm"
30704a258f4SEd Tanous                                                                  "ary"];
30804a258f4SEd Tanous                                                     nlohmann::json &procCount =
30904a258f4SEd Tanous                                                         procSummary["Count"];
31004a258f4SEd Tanous                                                     procCount =
31157e8c9beSAlpana Kumari                                                         procCount.get<int>() +
31257e8c9beSAlpana Kumari                                                         1;
31357e8c9beSAlpana Kumari                                                     procSummary["Status"]
31457e8c9beSAlpana Kumari                                                                ["State"] =
315c5b2abe0SLewanczyk, Dawid                                                                    "Enabled";
31657e8c9beSAlpana Kumari                                                     procSummary["Model"] =
31757e8c9beSAlpana Kumari                                                         *value;
318c5b2abe0SLewanczyk, Dawid                                                 }
319c5b2abe0SLewanczyk, Dawid                                             }
320c5b2abe0SLewanczyk, Dawid                                         }
32157e8c9beSAlpana Kumari                                     }
32257e8c9beSAlpana Kumari                                     else
32357e8c9beSAlpana Kumari                                     {
32457e8c9beSAlpana Kumari                                         auto getCpuPresenceState =
32557e8c9beSAlpana Kumari                                             [aResp](
32657e8c9beSAlpana Kumari                                                 const boost::system::error_code
32757e8c9beSAlpana Kumari                                                     ec,
32857e8c9beSAlpana Kumari                                                 const std::variant<bool>
32957e8c9beSAlpana Kumari                                                     &cpuPresenceCheck) {
33057e8c9beSAlpana Kumari                                                 if (ec)
33157e8c9beSAlpana Kumari                                                 {
33257e8c9beSAlpana Kumari                                                     BMCWEB_LOG_ERROR
33357e8c9beSAlpana Kumari                                                         << "DBUS response "
33457e8c9beSAlpana Kumari                                                            "error "
33557e8c9beSAlpana Kumari                                                         << ec;
33657e8c9beSAlpana Kumari                                                     return;
33757e8c9beSAlpana Kumari                                                 }
33857e8c9beSAlpana Kumari                                                 modifyCpuPresenceState(
33957e8c9beSAlpana Kumari                                                     aResp, cpuPresenceCheck);
34057e8c9beSAlpana Kumari                                             };
34157e8c9beSAlpana Kumari 
34257e8c9beSAlpana Kumari                                         auto getCpuFunctionalState =
34357e8c9beSAlpana Kumari                                             [aResp](
34457e8c9beSAlpana Kumari                                                 const boost::system::error_code
34557e8c9beSAlpana Kumari                                                     ec,
34657e8c9beSAlpana Kumari                                                 const std::variant<bool>
34757e8c9beSAlpana Kumari                                                     &cpuFunctionalCheck) {
34857e8c9beSAlpana Kumari                                                 if (ec)
34957e8c9beSAlpana Kumari                                                 {
35057e8c9beSAlpana Kumari                                                     BMCWEB_LOG_ERROR
35157e8c9beSAlpana Kumari                                                         << "DBUS response "
35257e8c9beSAlpana Kumari                                                            "error "
35357e8c9beSAlpana Kumari                                                         << ec;
35457e8c9beSAlpana Kumari                                                     return;
35557e8c9beSAlpana Kumari                                                 }
35657e8c9beSAlpana Kumari                                                 modifyCpuFunctionalState(
35757e8c9beSAlpana Kumari                                                     aResp, cpuFunctionalCheck);
35857e8c9beSAlpana Kumari                                             };
35957e8c9beSAlpana Kumari                                         // Get the Presence of CPU
36057e8c9beSAlpana Kumari                                         crow::connections::systemBus
36157e8c9beSAlpana Kumari                                             ->async_method_call(
36257e8c9beSAlpana Kumari                                                 std::move(getCpuPresenceState),
36357e8c9beSAlpana Kumari                                                 service, path,
36457e8c9beSAlpana Kumari                                                 "org.freedesktop.DBus."
36557e8c9beSAlpana Kumari                                                 "Properties",
36657e8c9beSAlpana Kumari                                                 "Get",
36757e8c9beSAlpana Kumari                                                 "xyz.openbmc_project.Inventory."
36857e8c9beSAlpana Kumari                                                 "Item",
36957e8c9beSAlpana Kumari                                                 "Present");
37057e8c9beSAlpana Kumari 
37157e8c9beSAlpana Kumari                                         // Get the Functional State
37257e8c9beSAlpana Kumari                                         crow::connections::systemBus
37357e8c9beSAlpana Kumari                                             ->async_method_call(
37457e8c9beSAlpana Kumari                                                 std::move(
37557e8c9beSAlpana Kumari                                                     getCpuFunctionalState),
37657e8c9beSAlpana Kumari                                                 service, path,
37757e8c9beSAlpana Kumari                                                 "org.freedesktop.DBus."
37857e8c9beSAlpana Kumari                                                 "Properties",
37957e8c9beSAlpana Kumari                                                 "Get",
38057e8c9beSAlpana Kumari                                                 "xyz.openbmc_project.State."
38157e8c9beSAlpana Kumari                                                 "Decorator."
38257e8c9beSAlpana Kumari                                                 "OperationalStatus",
38357e8c9beSAlpana Kumari                                                 "Functional");
38457e8c9beSAlpana Kumari 
38557e8c9beSAlpana Kumari                                         // Get the MODEL from
38657e8c9beSAlpana Kumari                                         // xyz.openbmc_project.Inventory.Decorator.Asset
38757e8c9beSAlpana Kumari                                         // support it later as Model  is Empty
38857e8c9beSAlpana Kumari                                         // currently.
38957e8c9beSAlpana Kumari                                     }
390c5b2abe0SLewanczyk, Dawid                                 },
39104a258f4SEd Tanous                                 connection.first, path,
3926c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
3936c34de48SEd Tanous                                 "xyz.openbmc_project.Inventory.Item.Cpu");
3941abe55efSEd Tanous                         }
39504a258f4SEd Tanous                         else if (interfaceName ==
39604a258f4SEd Tanous                                  "xyz.openbmc_project.Common.UUID")
3971abe55efSEd Tanous                         {
3981abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
39904a258f4SEd Tanous                                 << "Found UUID, now get its properties.";
40055c7b7a2SEd Tanous                             crow::connections::systemBus->async_method_call(
401029573d4SEd Tanous                                 [aResp](const boost::system::error_code ec,
4026c34de48SEd Tanous                                         const std::vector<
4036c34de48SEd Tanous                                             std::pair<std::string, VariantType>>
4041abe55efSEd Tanous                                             &properties) {
4051abe55efSEd Tanous                                     if (ec)
4061abe55efSEd Tanous                                     {
4071abe55efSEd Tanous                                         BMCWEB_LOG_DEBUG
4086c34de48SEd Tanous                                             << "DBUS response error " << ec;
409f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
410c5b2abe0SLewanczyk, Dawid                                         return;
411c5b2abe0SLewanczyk, Dawid                                     }
4126c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
4136c34de48SEd Tanous                                                      << properties.size()
414c5b2abe0SLewanczyk, Dawid                                                      << "UUID properties.";
4151abe55efSEd Tanous                                     for (const std::pair<std::string,
41604a258f4SEd Tanous                                                          VariantType>
41704a258f4SEd Tanous                                              &property : properties)
4181abe55efSEd Tanous                                     {
41904a258f4SEd Tanous                                         if (property.first == "UUID")
4201abe55efSEd Tanous                                         {
421c5b2abe0SLewanczyk, Dawid                                             const std::string *value =
422029573d4SEd Tanous                                                 sdbusplus::message::variant_ns::
423029573d4SEd Tanous                                                     get_if<std::string>(
4241b6b96c5SEd Tanous                                                         &property.second);
42504a258f4SEd Tanous 
4261abe55efSEd Tanous                                             if (value != nullptr)
4271abe55efSEd Tanous                                             {
428029573d4SEd Tanous                                                 std::string valueStr = *value;
42904a258f4SEd Tanous                                                 if (valueStr.size() == 32)
4301abe55efSEd Tanous                                                 {
431029573d4SEd Tanous                                                     valueStr.insert(8, 1, '-');
432029573d4SEd Tanous                                                     valueStr.insert(13, 1, '-');
433029573d4SEd Tanous                                                     valueStr.insert(18, 1, '-');
434029573d4SEd Tanous                                                     valueStr.insert(23, 1, '-');
43504a258f4SEd Tanous                                                 }
436029573d4SEd Tanous                                                 BMCWEB_LOG_DEBUG << "UUID = "
43704a258f4SEd Tanous                                                                  << valueStr;
438029573d4SEd Tanous                                                 aResp->res.jsonValue["UUID"] =
43904a258f4SEd Tanous                                                     valueStr;
440c5b2abe0SLewanczyk, Dawid                                             }
441c5b2abe0SLewanczyk, Dawid                                         }
442c5b2abe0SLewanczyk, Dawid                                     }
443c5b2abe0SLewanczyk, Dawid                                 },
44404a258f4SEd Tanous                                 connection.first, path,
4456c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
4461abe55efSEd Tanous                                 "xyz.openbmc_project.Common.UUID");
447c5b2abe0SLewanczyk, Dawid                         }
448029573d4SEd Tanous                         else if (interfaceName ==
449029573d4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.System")
4501abe55efSEd Tanous                         {
451029573d4SEd Tanous                             crow::connections::systemBus->async_method_call(
452029573d4SEd Tanous                                 [aResp](const boost::system::error_code ec,
453029573d4SEd Tanous                                         const std::vector<
454029573d4SEd Tanous                                             std::pair<std::string, VariantType>>
455029573d4SEd Tanous                                             &propertiesList) {
456029573d4SEd Tanous                                     if (ec)
457029573d4SEd Tanous                                     {
458e4a4b9a9SJames Feist                                         // doesn't have to include this
459e4a4b9a9SJames Feist                                         // interface
460029573d4SEd Tanous                                         return;
461029573d4SEd Tanous                                     }
462029573d4SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
463029573d4SEd Tanous                                                      << propertiesList.size()
464029573d4SEd Tanous                                                      << "properties for system";
465029573d4SEd Tanous                                     for (const std::pair<std::string,
466029573d4SEd Tanous                                                          VariantType>
467029573d4SEd Tanous                                              &property : propertiesList)
468029573d4SEd Tanous                                     {
469fc5afcf9Sbeccabroek                                         const std::string &propertyName =
470fc5afcf9Sbeccabroek                                             property.first;
471fc5afcf9Sbeccabroek                                         if ((propertyName == "PartNumber") ||
472fc5afcf9Sbeccabroek                                             (propertyName == "SerialNumber") ||
473fc5afcf9Sbeccabroek                                             (propertyName == "Manufacturer") ||
474fc5afcf9Sbeccabroek                                             (propertyName == "Model"))
475fc5afcf9Sbeccabroek                                         {
476029573d4SEd Tanous                                             const std::string *value =
477fc5afcf9Sbeccabroek                                                 std::get_if<std::string>(
478029573d4SEd Tanous                                                     &property.second);
479029573d4SEd Tanous                                             if (value != nullptr)
480029573d4SEd Tanous                                             {
481029573d4SEd Tanous                                                 aResp->res
482fc5afcf9Sbeccabroek                                                     .jsonValue[propertyName] =
483029573d4SEd Tanous                                                     *value;
484029573d4SEd Tanous                                             }
485029573d4SEd Tanous                                         }
486fc5afcf9Sbeccabroek                                     }
487029573d4SEd Tanous                                     aResp->res.jsonValue["Name"] = "system";
488029573d4SEd Tanous                                     aResp->res.jsonValue["Id"] =
489029573d4SEd Tanous                                         aResp->res.jsonValue["SerialNumber"];
490cb7e1e7bSAndrew Geissler                                     // Grab the bios version
491cb7e1e7bSAndrew Geissler                                     fw_util::getActiveFwVersion(
492cb7e1e7bSAndrew Geissler                                         aResp, fw_util::biosPurpose,
493cb7e1e7bSAndrew Geissler                                         "BiosVersion");
494029573d4SEd Tanous                                 },
495029573d4SEd Tanous                                 connection.first, path,
496029573d4SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
497029573d4SEd Tanous                                 "xyz.openbmc_project.Inventory.Decorator."
498029573d4SEd Tanous                                 "Asset");
499e4a4b9a9SJames Feist 
500e4a4b9a9SJames Feist                             crow::connections::systemBus->async_method_call(
501e4a4b9a9SJames Feist                                 [aResp](
502e4a4b9a9SJames Feist                                     const boost::system::error_code ec,
503e4a4b9a9SJames Feist                                     const std::variant<std::string> &property) {
504e4a4b9a9SJames Feist                                     if (ec)
505e4a4b9a9SJames Feist                                     {
506e4a4b9a9SJames Feist                                         // doesn't have to include this
507e4a4b9a9SJames Feist                                         // interface
508e4a4b9a9SJames Feist                                         return;
509e4a4b9a9SJames Feist                                     }
510e4a4b9a9SJames Feist 
511e4a4b9a9SJames Feist                                     const std::string *value =
512e4a4b9a9SJames Feist                                         std::get_if<std::string>(&property);
513e4a4b9a9SJames Feist                                     if (value != nullptr)
514e4a4b9a9SJames Feist                                     {
515e4a4b9a9SJames Feist                                         aResp->res.jsonValue["AssetTag"] =
516e4a4b9a9SJames Feist                                             *value;
517e4a4b9a9SJames Feist                                     }
518e4a4b9a9SJames Feist                                 },
519e4a4b9a9SJames Feist                                 connection.first, path,
520e4a4b9a9SJames Feist                                 "org.freedesktop.DBus.Properties", "Get",
521e4a4b9a9SJames Feist                                 "xyz.openbmc_project.Inventory.Decorator."
522e4a4b9a9SJames Feist                                 "AssetTag",
523e4a4b9a9SJames Feist                                 "AssetTag");
524029573d4SEd Tanous                         }
525029573d4SEd Tanous                     }
526029573d4SEd Tanous                 }
527c5b2abe0SLewanczyk, Dawid             }
528c5b2abe0SLewanczyk, Dawid         },
529c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper",
530c5b2abe0SLewanczyk, Dawid         "/xyz/openbmc_project/object_mapper",
531c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
5326617338dSEd Tanous         "/xyz/openbmc_project/inventory", int32_t(0),
5336617338dSEd Tanous         std::array<const char *, 5>{
5346617338dSEd Tanous             "xyz.openbmc_project.Inventory.Decorator.Asset",
5356617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Cpu",
5366617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Dimm",
5376617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.System",
5386617338dSEd Tanous             "xyz.openbmc_project.Common.UUID",
5396617338dSEd Tanous         });
540c5b2abe0SLewanczyk, Dawid }
541c5b2abe0SLewanczyk, Dawid 
542c5b2abe0SLewanczyk, Dawid /**
543c5b2abe0SLewanczyk, Dawid  * @brief Retrieves identify led group properties over dbus
544c5b2abe0SLewanczyk, Dawid  *
545491d8ee7SSantosh Puranik  * @param[in] aResp     Shared pointer for generating response message.
546c5b2abe0SLewanczyk, Dawid  * @param[in] callback  Callback for process retrieved data.
547c5b2abe0SLewanczyk, Dawid  *
548c5b2abe0SLewanczyk, Dawid  * @return None.
549c5b2abe0SLewanczyk, Dawid  */
550c5b2abe0SLewanczyk, Dawid template <typename CallbackFunc>
551a0803efaSEd Tanous void getLedGroupIdentify(std::shared_ptr<AsyncResp> aResp,
5521abe55efSEd Tanous                          CallbackFunc &&callback)
5531abe55efSEd Tanous {
55455c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get led groups";
55555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
556c5d03ff4SJennifer Lee         [aResp,
5576617338dSEd Tanous          callback{std::move(callback)}](const boost::system::error_code &ec,
5581abe55efSEd Tanous                                         const ManagedObjectsType &resp) {
5591abe55efSEd Tanous             if (ec)
5601abe55efSEd Tanous             {
56155c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
562f12894f8SJason M. Bills                 messages::internalError(aResp->res);
563c5b2abe0SLewanczyk, Dawid                 return;
564c5b2abe0SLewanczyk, Dawid             }
5656c34de48SEd Tanous             BMCWEB_LOG_DEBUG << "Got " << resp.size() << "led group objects.";
5661abe55efSEd Tanous             for (const auto &objPath : resp)
5671abe55efSEd Tanous             {
568c5b2abe0SLewanczyk, Dawid                 const std::string &path = objPath.first;
5691abe55efSEd Tanous                 if (path.rfind("enclosure_identify") != std::string::npos)
5701abe55efSEd Tanous                 {
5711abe55efSEd Tanous                     for (const auto &interface : objPath.second)
5721abe55efSEd Tanous                     {
5736c34de48SEd Tanous                         if (interface.first == "xyz.openbmc_project.Led.Group")
5741abe55efSEd Tanous                         {
5751abe55efSEd Tanous                             for (const auto &property : interface.second)
5761abe55efSEd Tanous                             {
5771abe55efSEd Tanous                                 if (property.first == "Asserted")
5781abe55efSEd Tanous                                 {
579c5b2abe0SLewanczyk, Dawid                                     const bool *asserted =
580abf2add6SEd Tanous                                         std::get_if<bool>(&property.second);
5811abe55efSEd Tanous                                     if (nullptr != asserted)
5821abe55efSEd Tanous                                     {
583c5b2abe0SLewanczyk, Dawid                                         callback(*asserted, aResp);
5841abe55efSEd Tanous                                     }
5851abe55efSEd Tanous                                     else
5861abe55efSEd Tanous                                     {
587c5b2abe0SLewanczyk, Dawid                                         callback(false, aResp);
588c5b2abe0SLewanczyk, Dawid                                     }
589c5b2abe0SLewanczyk, Dawid                                 }
590c5b2abe0SLewanczyk, Dawid                             }
591c5b2abe0SLewanczyk, Dawid                         }
592c5b2abe0SLewanczyk, Dawid                     }
593c5b2abe0SLewanczyk, Dawid                 }
594c5b2abe0SLewanczyk, Dawid             }
595c5b2abe0SLewanczyk, Dawid         },
596c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.LED.GroupManager",
5976c34de48SEd Tanous         "/xyz/openbmc_project/led/groups", "org.freedesktop.DBus.ObjectManager",
5986c34de48SEd Tanous         "GetManagedObjects");
599c5b2abe0SLewanczyk, Dawid }
600c5b2abe0SLewanczyk, Dawid 
601c5b2abe0SLewanczyk, Dawid template <typename CallbackFunc>
6026c34de48SEd Tanous void getLedIdentify(std::shared_ptr<AsyncResp> aResp, CallbackFunc &&callback)
6031abe55efSEd Tanous {
60455c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get identify led properties";
60555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
6066617338dSEd Tanous         [aResp,
6076617338dSEd Tanous          callback{std::move(callback)}](const boost::system::error_code ec,
608c5b2abe0SLewanczyk, Dawid                                         const PropertiesType &properties) {
6091abe55efSEd Tanous             if (ec)
6101abe55efSEd Tanous             {
61155c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
612f12894f8SJason M. Bills                 messages::internalError(aResp->res);
613c5b2abe0SLewanczyk, Dawid                 return;
614c5b2abe0SLewanczyk, Dawid             }
6151abe55efSEd Tanous             BMCWEB_LOG_DEBUG << "Got " << properties.size()
6161abe55efSEd Tanous                              << "led properties.";
617c5b2abe0SLewanczyk, Dawid             std::string output;
6181abe55efSEd Tanous             for (const auto &property : properties)
6191abe55efSEd Tanous             {
6201abe55efSEd Tanous                 if (property.first == "State")
6211abe55efSEd Tanous                 {
622c5b2abe0SLewanczyk, Dawid                     const std::string *s =
623abf2add6SEd Tanous                         std::get_if<std::string>(&property.second);
6241abe55efSEd Tanous                     if (nullptr != s)
6251abe55efSEd Tanous                     {
62655c7b7a2SEd Tanous                         BMCWEB_LOG_DEBUG << "Identify Led State: " << *s;
627c5b2abe0SLewanczyk, Dawid                         const auto pos = s->rfind('.');
6281abe55efSEd Tanous                         if (pos != std::string::npos)
6291abe55efSEd Tanous                         {
630c5b2abe0SLewanczyk, Dawid                             auto led = s->substr(pos + 1);
6311abe55efSEd Tanous                             for (const std::pair<const char *, const char *>
6321abe55efSEd Tanous                                      &p :
6331abe55efSEd Tanous                                  std::array<
6346c34de48SEd Tanous                                      std::pair<const char *, const char *>, 3>{
6356c34de48SEd Tanous                                      {{"On", "Lit"},
636c5b2abe0SLewanczyk, Dawid                                       {"Blink", "Blinking"},
6371abe55efSEd Tanous                                       {"Off", "Off"}}})
6381abe55efSEd Tanous                             {
6391abe55efSEd Tanous                                 if (led == p.first)
6401abe55efSEd Tanous                                 {
641c5b2abe0SLewanczyk, Dawid                                     output = p.second;
642c5b2abe0SLewanczyk, Dawid                                 }
643c5b2abe0SLewanczyk, Dawid                             }
644c5b2abe0SLewanczyk, Dawid                         }
645c5b2abe0SLewanczyk, Dawid                     }
646c5b2abe0SLewanczyk, Dawid                 }
647c5b2abe0SLewanczyk, Dawid             }
648c5b2abe0SLewanczyk, Dawid             callback(output, aResp);
649c5b2abe0SLewanczyk, Dawid         },
650c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.LED.Controller.identify",
651c5b2abe0SLewanczyk, Dawid         "/xyz/openbmc_project/led/physical/identify",
652c5b2abe0SLewanczyk, Dawid         "org.freedesktop.DBus.Properties", "GetAll",
653c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.Led.Physical");
654c5b2abe0SLewanczyk, Dawid }
655c5b2abe0SLewanczyk, Dawid /**
656c5b2abe0SLewanczyk, Dawid  * @brief Retrieves host state properties over dbus
657c5b2abe0SLewanczyk, Dawid  *
658c5b2abe0SLewanczyk, Dawid  * @param[in] aResp     Shared pointer for completing asynchronous calls.
659c5b2abe0SLewanczyk, Dawid  *
660c5b2abe0SLewanczyk, Dawid  * @return None.
661c5b2abe0SLewanczyk, Dawid  */
662a0803efaSEd Tanous void getHostState(std::shared_ptr<AsyncResp> aResp)
6631abe55efSEd Tanous {
66455c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get host information.";
66555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
666c5d03ff4SJennifer Lee         [aResp](const boost::system::error_code ec,
667abf2add6SEd Tanous                 const std::variant<std::string> &hostState) {
6681abe55efSEd Tanous             if (ec)
6691abe55efSEd Tanous             {
67055c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
671f12894f8SJason M. Bills                 messages::internalError(aResp->res);
672c5b2abe0SLewanczyk, Dawid                 return;
673c5b2abe0SLewanczyk, Dawid             }
6746617338dSEd Tanous 
675abf2add6SEd Tanous             const std::string *s = std::get_if<std::string>(&hostState);
67655c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Host state: " << *s;
6776617338dSEd Tanous             if (s != nullptr)
6781abe55efSEd Tanous             {
679c5b2abe0SLewanczyk, Dawid                 // Verify Host State
68094732661SAndrew Geissler                 if (*s == "xyz.openbmc_project.State.Host.HostState.Running")
6811abe55efSEd Tanous                 {
68255c7b7a2SEd Tanous                     aResp->res.jsonValue["PowerState"] = "On";
6836617338dSEd Tanous                     aResp->res.jsonValue["Status"]["State"] = "Enabled";
6841abe55efSEd Tanous                 }
6851abe55efSEd Tanous                 else
6861abe55efSEd Tanous                 {
68755c7b7a2SEd Tanous                     aResp->res.jsonValue["PowerState"] = "Off";
6886617338dSEd Tanous                     aResp->res.jsonValue["Status"]["State"] = "Disabled";
689c5b2abe0SLewanczyk, Dawid                 }
690c5b2abe0SLewanczyk, Dawid             }
691c5b2abe0SLewanczyk, Dawid         },
6926c34de48SEd Tanous         "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
6936617338dSEd Tanous         "org.freedesktop.DBus.Properties", "Get",
6946617338dSEd Tanous         "xyz.openbmc_project.State.Host", "CurrentHostState");
695c5b2abe0SLewanczyk, Dawid }
696c5b2abe0SLewanczyk, Dawid 
697c5b2abe0SLewanczyk, Dawid /**
698491d8ee7SSantosh Puranik  * @brief Traslates boot source DBUS property value to redfish.
699491d8ee7SSantosh Puranik  *
700491d8ee7SSantosh Puranik  * @param[in] dbusSource    The boot source in DBUS speak.
701491d8ee7SSantosh Puranik  *
702491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source in Redfish terms. If translation
703491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
704491d8ee7SSantosh Puranik  */
705491d8ee7SSantosh Puranik static std::string dbusToRfBootSource(const std::string &dbusSource)
706491d8ee7SSantosh Puranik {
707491d8ee7SSantosh Puranik     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
708491d8ee7SSantosh Puranik     {
709491d8ee7SSantosh Puranik         return "None";
710491d8ee7SSantosh Puranik     }
711491d8ee7SSantosh Puranik     else if (dbusSource ==
712491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.Disk")
713491d8ee7SSantosh Puranik     {
714491d8ee7SSantosh Puranik         return "Hdd";
715491d8ee7SSantosh Puranik     }
716491d8ee7SSantosh Puranik     else if (dbusSource ==
717a71dc0b7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia")
718491d8ee7SSantosh Puranik     {
719491d8ee7SSantosh Puranik         return "Cd";
720491d8ee7SSantosh Puranik     }
721491d8ee7SSantosh Puranik     else if (dbusSource ==
722491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.Network")
723491d8ee7SSantosh Puranik     {
724491d8ee7SSantosh Puranik         return "Pxe";
725491d8ee7SSantosh Puranik     }
7269f16b2c1SJennifer Lee     else if (dbusSource ==
7279f16b2c1SJennifer Lee              "xyz.openbmc_project.Control.Boot.Source.Sources.Removable")
7289f16b2c1SJennifer Lee     {
7299f16b2c1SJennifer Lee         return "Usb";
7309f16b2c1SJennifer Lee     }
731491d8ee7SSantosh Puranik     else
732491d8ee7SSantosh Puranik     {
733491d8ee7SSantosh Puranik         return "";
734491d8ee7SSantosh Puranik     }
735491d8ee7SSantosh Puranik }
736491d8ee7SSantosh Puranik 
737491d8ee7SSantosh Puranik /**
738491d8ee7SSantosh Puranik  * @brief Traslates boot mode DBUS property value to redfish.
739491d8ee7SSantosh Puranik  *
740491d8ee7SSantosh Puranik  * @param[in] dbusMode    The boot mode in DBUS speak.
741491d8ee7SSantosh Puranik  *
742491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode in Redfish terms. If translation
743491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
744491d8ee7SSantosh Puranik  */
745491d8ee7SSantosh Puranik static std::string dbusToRfBootMode(const std::string &dbusMode)
746491d8ee7SSantosh Puranik {
747491d8ee7SSantosh Puranik     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
748491d8ee7SSantosh Puranik     {
749491d8ee7SSantosh Puranik         return "None";
750491d8ee7SSantosh Puranik     }
751491d8ee7SSantosh Puranik     else if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe")
752491d8ee7SSantosh Puranik     {
753491d8ee7SSantosh Puranik         return "Diags";
754491d8ee7SSantosh Puranik     }
755491d8ee7SSantosh Puranik     else if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup")
756491d8ee7SSantosh Puranik     {
757491d8ee7SSantosh Puranik         return "BiosSetup";
758491d8ee7SSantosh Puranik     }
759491d8ee7SSantosh Puranik     else
760491d8ee7SSantosh Puranik     {
761491d8ee7SSantosh Puranik         return "";
762491d8ee7SSantosh Puranik     }
763491d8ee7SSantosh Puranik }
764491d8ee7SSantosh Puranik 
765491d8ee7SSantosh Puranik /**
766491d8ee7SSantosh Puranik  * @brief Traslates boot source from Redfish to DBUS property value.
767491d8ee7SSantosh Puranik  *
768491d8ee7SSantosh Puranik  * @param[in] rfSource    The boot source in Redfish.
769491d8ee7SSantosh Puranik  *
770491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source as expected by DBUS.
771491d8ee7SSantosh Puranik  * If translation cannot be done, returns an empty string.
772491d8ee7SSantosh Puranik  */
773491d8ee7SSantosh Puranik static std::string rfToDbusBootSource(const std::string &rfSource)
774491d8ee7SSantosh Puranik {
775491d8ee7SSantosh Puranik     if (rfSource == "None")
776491d8ee7SSantosh Puranik     {
777491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
778491d8ee7SSantosh Puranik     }
779491d8ee7SSantosh Puranik     else if (rfSource == "Hdd")
780491d8ee7SSantosh Puranik     {
781491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Source.Sources.Disk";
782491d8ee7SSantosh Puranik     }
783491d8ee7SSantosh Puranik     else if (rfSource == "Cd")
784491d8ee7SSantosh Puranik     {
785a71dc0b7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia";
786491d8ee7SSantosh Puranik     }
787491d8ee7SSantosh Puranik     else if (rfSource == "Pxe")
788491d8ee7SSantosh Puranik     {
789491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Source.Sources.Network";
790491d8ee7SSantosh Puranik     }
7919f16b2c1SJennifer Lee     else if (rfSource == "Usb")
7929f16b2c1SJennifer Lee     {
7939f16b2c1SJennifer Lee         return "xyz.openbmc_project.Control.Boot.Source.Sources.Removable";
7949f16b2c1SJennifer Lee     }
795491d8ee7SSantosh Puranik     else
796491d8ee7SSantosh Puranik     {
797491d8ee7SSantosh Puranik         return "";
798491d8ee7SSantosh Puranik     }
799491d8ee7SSantosh Puranik }
800491d8ee7SSantosh Puranik 
801491d8ee7SSantosh Puranik /**
802491d8ee7SSantosh Puranik  * @brief Traslates boot mode from Redfish to DBUS property value.
803491d8ee7SSantosh Puranik  *
804491d8ee7SSantosh Puranik  * @param[in] rfMode    The boot mode in Redfish.
805491d8ee7SSantosh Puranik  *
806491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode as expected by DBUS.
807491d8ee7SSantosh Puranik  * If translation cannot be done, returns an empty string.
808491d8ee7SSantosh Puranik  */
809491d8ee7SSantosh Puranik static std::string rfToDbusBootMode(const std::string &rfMode)
810491d8ee7SSantosh Puranik {
811491d8ee7SSantosh Puranik     if (rfMode == "None")
812491d8ee7SSantosh Puranik     {
813491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
814491d8ee7SSantosh Puranik     }
815491d8ee7SSantosh Puranik     else if (rfMode == "Diags")
816491d8ee7SSantosh Puranik     {
817491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe";
818491d8ee7SSantosh Puranik     }
819491d8ee7SSantosh Puranik     else if (rfMode == "BiosSetup")
820491d8ee7SSantosh Puranik     {
821491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup";
822491d8ee7SSantosh Puranik     }
823491d8ee7SSantosh Puranik     else
824491d8ee7SSantosh Puranik     {
825491d8ee7SSantosh Puranik         return "";
826491d8ee7SSantosh Puranik     }
827491d8ee7SSantosh Puranik }
828491d8ee7SSantosh Puranik 
829491d8ee7SSantosh Puranik /**
830491d8ee7SSantosh Puranik  * @brief Retrieves boot mode over DBUS and fills out the response
831491d8ee7SSantosh Puranik  *
832491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
833491d8ee7SSantosh Puranik  * @param[in] bootDbusObj   The dbus object to query for boot properties.
834491d8ee7SSantosh Puranik  *
835491d8ee7SSantosh Puranik  * @return None.
836491d8ee7SSantosh Puranik  */
837491d8ee7SSantosh Puranik static void getBootMode(std::shared_ptr<AsyncResp> aResp,
838491d8ee7SSantosh Puranik                         std::string bootDbusObj)
839491d8ee7SSantosh Puranik {
840491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
841491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec,
842491d8ee7SSantosh Puranik                 const std::variant<std::string> &bootMode) {
843491d8ee7SSantosh Puranik             if (ec)
844491d8ee7SSantosh Puranik             {
845491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
846491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
847491d8ee7SSantosh Puranik                 return;
848491d8ee7SSantosh Puranik             }
849491d8ee7SSantosh Puranik 
850491d8ee7SSantosh Puranik             const std::string *bootModeStr =
851491d8ee7SSantosh Puranik                 std::get_if<std::string>(&bootMode);
852491d8ee7SSantosh Puranik 
853491d8ee7SSantosh Puranik             if (!bootModeStr)
854491d8ee7SSantosh Puranik             {
855491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
856491d8ee7SSantosh Puranik                 return;
857491d8ee7SSantosh Puranik             }
858491d8ee7SSantosh Puranik 
859491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot mode: " << *bootModeStr;
860491d8ee7SSantosh Puranik 
861491d8ee7SSantosh Puranik             // TODO (Santosh): Do we need to support override mode?
862491d8ee7SSantosh Puranik             aResp->res.jsonValue["Boot"]["BootSourceOverrideMode"] = "Legacy";
863491d8ee7SSantosh Puranik             aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget@Redfish."
864491d8ee7SSantosh Puranik                                          "AllowableValues"] = {
865491d8ee7SSantosh Puranik                 "None", "Pxe", "Hdd", "Cd", "Diags", "BiosSetup"};
866491d8ee7SSantosh Puranik 
867491d8ee7SSantosh Puranik             if (*bootModeStr !=
868491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
869491d8ee7SSantosh Puranik             {
870491d8ee7SSantosh Puranik                 auto rfMode = dbusToRfBootMode(*bootModeStr);
871491d8ee7SSantosh Puranik                 if (!rfMode.empty())
872491d8ee7SSantosh Puranik                 {
873491d8ee7SSantosh Puranik                     aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
874491d8ee7SSantosh Puranik                         rfMode;
875491d8ee7SSantosh Puranik                 }
876491d8ee7SSantosh Puranik             }
877491d8ee7SSantosh Puranik 
878491d8ee7SSantosh Puranik             // If the BootSourceOverrideTarget is still "None" at the end,
879491d8ee7SSantosh Puranik             // reset the BootSourceOverrideEnabled to indicate that
880491d8ee7SSantosh Puranik             // overrides are disabled
881491d8ee7SSantosh Puranik             if (aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] ==
882491d8ee7SSantosh Puranik                 "None")
883491d8ee7SSantosh Puranik             {
884491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
885491d8ee7SSantosh Puranik                     "Disabled";
886491d8ee7SSantosh Puranik             }
887491d8ee7SSantosh Puranik         },
888491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootDbusObj,
889491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
890491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode");
891491d8ee7SSantosh Puranik }
892491d8ee7SSantosh Puranik 
893491d8ee7SSantosh Puranik /**
894491d8ee7SSantosh Puranik  * @brief Retrieves boot source over DBUS
895491d8ee7SSantosh Puranik  *
896491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
897491d8ee7SSantosh Puranik  * @param[in] oneTimeEnable Boolean to indicate boot properties are one-time.
898491d8ee7SSantosh Puranik  *
899491d8ee7SSantosh Puranik  * @return None.
900491d8ee7SSantosh Puranik  */
901491d8ee7SSantosh Puranik static void getBootSource(std::shared_ptr<AsyncResp> aResp, bool oneTimeEnabled)
902491d8ee7SSantosh Puranik {
903491d8ee7SSantosh Puranik     std::string bootDbusObj =
904491d8ee7SSantosh Puranik         oneTimeEnabled ? "/xyz/openbmc_project/control/host0/boot/one_time"
905491d8ee7SSantosh Puranik                        : "/xyz/openbmc_project/control/host0/boot";
906491d8ee7SSantosh Puranik 
907491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Is one time: " << oneTimeEnabled;
908491d8ee7SSantosh Puranik     aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
909491d8ee7SSantosh Puranik         (oneTimeEnabled) ? "Once" : "Continuous";
910491d8ee7SSantosh Puranik 
911491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
912491d8ee7SSantosh Puranik         [aResp, bootDbusObj](const boost::system::error_code ec,
913491d8ee7SSantosh Puranik                              const std::variant<std::string> &bootSource) {
914491d8ee7SSantosh Puranik             if (ec)
915491d8ee7SSantosh Puranik             {
916491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
917491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
918491d8ee7SSantosh Puranik                 return;
919491d8ee7SSantosh Puranik             }
920491d8ee7SSantosh Puranik 
921491d8ee7SSantosh Puranik             const std::string *bootSourceStr =
922491d8ee7SSantosh Puranik                 std::get_if<std::string>(&bootSource);
923491d8ee7SSantosh Puranik 
924491d8ee7SSantosh Puranik             if (!bootSourceStr)
925491d8ee7SSantosh Puranik             {
926491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
927491d8ee7SSantosh Puranik                 return;
928491d8ee7SSantosh Puranik             }
929491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot source: " << *bootSourceStr;
930491d8ee7SSantosh Puranik 
931491d8ee7SSantosh Puranik             auto rfSource = dbusToRfBootSource(*bootSourceStr);
932491d8ee7SSantosh Puranik             if (!rfSource.empty())
933491d8ee7SSantosh Puranik             {
934491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
935491d8ee7SSantosh Puranik                     rfSource;
936491d8ee7SSantosh Puranik             }
937491d8ee7SSantosh Puranik         },
938491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootDbusObj,
939491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
940491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource");
941491d8ee7SSantosh Puranik     getBootMode(std::move(aResp), std::move(bootDbusObj));
942491d8ee7SSantosh Puranik }
943491d8ee7SSantosh Puranik 
944491d8ee7SSantosh Puranik /**
945491d8ee7SSantosh Puranik  * @brief Retrieves "One time" enabled setting over DBUS and calls function to
946491d8ee7SSantosh Puranik  * get boot source and boot mode.
947491d8ee7SSantosh Puranik  *
948491d8ee7SSantosh Puranik  * @param[in] aResp     Shared pointer for generating response message.
949491d8ee7SSantosh Puranik  *
950491d8ee7SSantosh Puranik  * @return None.
951491d8ee7SSantosh Puranik  */
952491d8ee7SSantosh Puranik static void getBootProperties(std::shared_ptr<AsyncResp> aResp)
953491d8ee7SSantosh Puranik {
954491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Get boot information.";
955491d8ee7SSantosh Puranik 
956491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
957c5d03ff4SJennifer Lee         [aResp](const boost::system::error_code ec,
958491d8ee7SSantosh Puranik                 const sdbusplus::message::variant<bool> &oneTime) {
959491d8ee7SSantosh Puranik             if (ec)
960491d8ee7SSantosh Puranik             {
961491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
9622a833c77SJames Feist                 // not an error, don't have to have the interface
963491d8ee7SSantosh Puranik                 return;
964491d8ee7SSantosh Puranik             }
965491d8ee7SSantosh Puranik 
966491d8ee7SSantosh Puranik             const bool *oneTimePtr = std::get_if<bool>(&oneTime);
967491d8ee7SSantosh Puranik 
968491d8ee7SSantosh Puranik             if (!oneTimePtr)
969491d8ee7SSantosh Puranik             {
970491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
971491d8ee7SSantosh Puranik                 return;
972491d8ee7SSantosh Puranik             }
973491d8ee7SSantosh Puranik             getBootSource(aResp, *oneTimePtr);
974491d8ee7SSantosh Puranik         },
975491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
976491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
977491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
978491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled");
979491d8ee7SSantosh Puranik }
980491d8ee7SSantosh Puranik 
981491d8ee7SSantosh Puranik /**
982491d8ee7SSantosh Puranik  * @brief Sets boot properties into DBUS object(s).
983491d8ee7SSantosh Puranik  *
984491d8ee7SSantosh Puranik  * @param[in] aResp           Shared pointer for generating response message.
985491d8ee7SSantosh Puranik  * @param[in] oneTimeEnabled  Is "one-time" setting already enabled.
986491d8ee7SSantosh Puranik  * @param[in] bootSource      The boot source to set.
987491d8ee7SSantosh Puranik  * @param[in] bootEnable      The source override "enable" to set.
988491d8ee7SSantosh Puranik  *
989491d8ee7SSantosh Puranik  * @return None.
990491d8ee7SSantosh Puranik  */
991491d8ee7SSantosh Puranik static void setBootModeOrSource(std::shared_ptr<AsyncResp> aResp,
992491d8ee7SSantosh Puranik                                 bool oneTimeEnabled,
993491d8ee7SSantosh Puranik                                 std::optional<std::string> bootSource,
994491d8ee7SSantosh Puranik                                 std::optional<std::string> bootEnable)
995491d8ee7SSantosh Puranik {
996491d8ee7SSantosh Puranik     if (bootEnable && (bootEnable != "Once") && (bootEnable != "Continuous") &&
997491d8ee7SSantosh Puranik         (bootEnable != "Disabled"))
998491d8ee7SSantosh Puranik     {
999491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Unsupported value for BootSourceOverrideEnabled: "
1000491d8ee7SSantosh Puranik                          << *bootEnable;
1001491d8ee7SSantosh Puranik         messages::propertyValueNotInList(aResp->res, *bootEnable,
1002491d8ee7SSantosh Puranik                                          "BootSourceOverrideEnabled");
1003491d8ee7SSantosh Puranik         return;
1004491d8ee7SSantosh Puranik     }
1005491d8ee7SSantosh Puranik 
1006491d8ee7SSantosh Puranik     bool oneTimeSetting = oneTimeEnabled;
1007491d8ee7SSantosh Puranik     // Validate incoming parameters
1008491d8ee7SSantosh Puranik     if (bootEnable)
1009491d8ee7SSantosh Puranik     {
1010491d8ee7SSantosh Puranik         if (*bootEnable == "Once")
1011491d8ee7SSantosh Puranik         {
1012491d8ee7SSantosh Puranik             oneTimeSetting = true;
1013491d8ee7SSantosh Puranik         }
1014491d8ee7SSantosh Puranik         else if (*bootEnable == "Continuous")
1015491d8ee7SSantosh Puranik         {
1016491d8ee7SSantosh Puranik             oneTimeSetting = false;
1017491d8ee7SSantosh Puranik         }
1018491d8ee7SSantosh Puranik         else if (*bootEnable == "Disabled")
1019491d8ee7SSantosh Puranik         {
1020491d8ee7SSantosh Puranik             oneTimeSetting = false;
1021491d8ee7SSantosh Puranik         }
1022491d8ee7SSantosh Puranik         else
1023491d8ee7SSantosh Puranik         {
1024491d8ee7SSantosh Puranik 
1025491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Unsupported value for "
1026491d8ee7SSantosh Puranik                                 "BootSourceOverrideEnabled: "
1027491d8ee7SSantosh Puranik                              << *bootEnable;
1028491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootEnable,
1029491d8ee7SSantosh Puranik                                              "BootSourceOverrideEnabled");
1030491d8ee7SSantosh Puranik             return;
1031491d8ee7SSantosh Puranik         }
1032491d8ee7SSantosh Puranik     }
1033491d8ee7SSantosh Puranik     std::string bootSourceStr;
1034491d8ee7SSantosh Puranik     std::string bootModeStr;
1035491d8ee7SSantosh Puranik     if (bootSource)
1036491d8ee7SSantosh Puranik     {
1037491d8ee7SSantosh Puranik         bootSourceStr = rfToDbusBootSource(*bootSource);
1038491d8ee7SSantosh Puranik         bootModeStr = rfToDbusBootMode(*bootSource);
1039491d8ee7SSantosh Puranik 
1040491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
1041491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
1042491d8ee7SSantosh Puranik 
1043491d8ee7SSantosh Puranik         if (bootSourceStr.empty() && bootModeStr.empty())
1044491d8ee7SSantosh Puranik         {
1045491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Invalid property value for "
1046491d8ee7SSantosh Puranik                                 "BootSourceOverrideTarget: "
1047491d8ee7SSantosh Puranik                              << *bootSource;
1048491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootSource,
1049491d8ee7SSantosh Puranik                                              "BootSourceTargetOverride");
1050491d8ee7SSantosh Puranik             return;
1051491d8ee7SSantosh Puranik         }
1052491d8ee7SSantosh Puranik     }
1053491d8ee7SSantosh Puranik     const char *bootObj =
1054491d8ee7SSantosh Puranik         oneTimeSetting ? "/xyz/openbmc_project/control/host0/boot/one_time"
1055491d8ee7SSantosh Puranik                        : "/xyz/openbmc_project/control/host0/boot";
1056491d8ee7SSantosh Puranik     // Figure out what properties to set
1057491d8ee7SSantosh Puranik     if (bootEnable && (*bootEnable == "Disabled"))
1058491d8ee7SSantosh Puranik     {
1059491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot source override will be disabled";
1060491d8ee7SSantosh Puranik         // Request to only turn OFF/ON enabled, if turning enabled OFF, need
1061491d8ee7SSantosh Puranik         // to reset the source and mode too. If turning it ON, we only need
1062491d8ee7SSantosh Puranik         // to set the enabled property
1063491d8ee7SSantosh Puranik         bootSourceStr =
1064491d8ee7SSantosh Puranik             "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
1065491d8ee7SSantosh Puranik         bootModeStr = "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
1066491d8ee7SSantosh Puranik     }
1067491d8ee7SSantosh Puranik     else if (bootSource)
1068491d8ee7SSantosh Puranik     {
1069491d8ee7SSantosh Puranik         // Source target specified
1070491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot source: " << *bootSource;
1071491d8ee7SSantosh Puranik         // Figure out which DBUS interface and property to use
1072491d8ee7SSantosh Puranik         bootSourceStr = rfToDbusBootSource(*bootSource);
1073491d8ee7SSantosh Puranik         bootModeStr = rfToDbusBootMode(*bootSource);
1074491d8ee7SSantosh Puranik 
1075491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
1076491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
1077491d8ee7SSantosh Puranik 
1078491d8ee7SSantosh Puranik         if (bootSourceStr.empty() && bootModeStr.empty())
1079491d8ee7SSantosh Puranik         {
1080491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Invalid property value for "
1081491d8ee7SSantosh Puranik                                 "BootSourceOverrideTarget: "
1082491d8ee7SSantosh Puranik                              << *bootSource;
1083491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootSource,
1084491d8ee7SSantosh Puranik                                              "BootSourceTargetOverride");
1085491d8ee7SSantosh Puranik             return;
1086491d8ee7SSantosh Puranik         }
1087491d8ee7SSantosh Puranik 
1088491d8ee7SSantosh Puranik         if (!bootSourceStr.empty())
1089491d8ee7SSantosh Puranik         {
1090491d8ee7SSantosh Puranik             // If setting to anything other than default, also reset boot
1091491d8ee7SSantosh Puranik             // mode property
1092491d8ee7SSantosh Puranik             if (bootSourceStr !=
1093491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
1094491d8ee7SSantosh Puranik             {
1095491d8ee7SSantosh Puranik                 bootModeStr =
1096491d8ee7SSantosh Puranik                     "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
1097491d8ee7SSantosh Puranik             }
1098491d8ee7SSantosh Puranik         }
1099491d8ee7SSantosh Puranik         else // if (!bootModeStr.empty())
1100491d8ee7SSantosh Puranik         {
1101491d8ee7SSantosh Puranik             // If setting to anything other than default, also reset boot
1102491d8ee7SSantosh Puranik             // source property
1103491d8ee7SSantosh Puranik             if (bootModeStr !=
1104491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
1105491d8ee7SSantosh Puranik             {
1106491d8ee7SSantosh Puranik                 bootSourceStr =
1107491d8ee7SSantosh Puranik                     "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
1108491d8ee7SSantosh Puranik             }
1109491d8ee7SSantosh Puranik         }
1110491d8ee7SSantosh Puranik     }
1111491d8ee7SSantosh Puranik     if (!bootSourceStr.empty())
1112491d8ee7SSantosh Puranik     {
1113491d8ee7SSantosh Puranik         crow::connections::systemBus->async_method_call(
1114491d8ee7SSantosh Puranik             [aResp](const boost::system::error_code ec) {
1115491d8ee7SSantosh Puranik                 if (ec)
1116491d8ee7SSantosh Puranik                 {
1117491d8ee7SSantosh Puranik                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1118491d8ee7SSantosh Puranik                     messages::internalError(aResp->res);
1119491d8ee7SSantosh Puranik                     return;
1120491d8ee7SSantosh Puranik                 }
1121491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "Boot source update done.";
1122491d8ee7SSantosh Puranik             },
1123491d8ee7SSantosh Puranik             "xyz.openbmc_project.Settings", bootObj,
1124491d8ee7SSantosh Puranik             "org.freedesktop.DBus.Properties", "Set",
1125491d8ee7SSantosh Puranik             "xyz.openbmc_project.Control.Boot.Source", "BootSource",
1126491d8ee7SSantosh Puranik             std::variant<std::string>(bootSourceStr));
1127491d8ee7SSantosh Puranik     }
1128491d8ee7SSantosh Puranik     if (!bootModeStr.empty())
1129491d8ee7SSantosh Puranik     {
1130491d8ee7SSantosh Puranik         crow::connections::systemBus->async_method_call(
1131491d8ee7SSantosh Puranik             [aResp](const boost::system::error_code ec) {
1132491d8ee7SSantosh Puranik                 if (ec)
1133491d8ee7SSantosh Puranik                 {
1134491d8ee7SSantosh Puranik                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1135491d8ee7SSantosh Puranik                     messages::internalError(aResp->res);
1136491d8ee7SSantosh Puranik                     return;
1137491d8ee7SSantosh Puranik                 }
1138491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "Boot mode update done.";
1139491d8ee7SSantosh Puranik             },
1140491d8ee7SSantosh Puranik             "xyz.openbmc_project.Settings", bootObj,
1141491d8ee7SSantosh Puranik             "org.freedesktop.DBus.Properties", "Set",
1142491d8ee7SSantosh Puranik             "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
1143491d8ee7SSantosh Puranik             std::variant<std::string>(bootModeStr));
1144491d8ee7SSantosh Puranik     }
1145491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1146491d8ee7SSantosh Puranik         [aResp{std::move(aResp)}](const boost::system::error_code ec) {
1147491d8ee7SSantosh Puranik             if (ec)
1148491d8ee7SSantosh Puranik             {
1149491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1150491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1151491d8ee7SSantosh Puranik                 return;
1152491d8ee7SSantosh Puranik             }
1153491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot enable update done.";
1154491d8ee7SSantosh Puranik         },
1155491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
1156491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
1157491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1158491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled",
1159491d8ee7SSantosh Puranik         std::variant<bool>(oneTimeSetting));
1160491d8ee7SSantosh Puranik }
1161491d8ee7SSantosh Puranik 
1162491d8ee7SSantosh Puranik /**
1163491d8ee7SSantosh Puranik  * @brief Retrieves "One time" enabled setting over DBUS and calls function to
1164491d8ee7SSantosh Puranik  * set boot source/boot mode properties.
1165491d8ee7SSantosh Puranik  *
1166491d8ee7SSantosh Puranik  * @param[in] aResp      Shared pointer for generating response message.
1167491d8ee7SSantosh Puranik  * @param[in] bootSource The boot source from incoming RF request.
1168491d8ee7SSantosh Puranik  * @param[in] bootEnable The boot override enable from incoming RF request.
1169491d8ee7SSantosh Puranik  *
1170491d8ee7SSantosh Puranik  * @return None.
1171491d8ee7SSantosh Puranik  */
1172491d8ee7SSantosh Puranik static void setBootProperties(std::shared_ptr<AsyncResp> aResp,
1173491d8ee7SSantosh Puranik                               std::optional<std::string> bootSource,
1174491d8ee7SSantosh Puranik                               std::optional<std::string> bootEnable)
1175491d8ee7SSantosh Puranik {
1176491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set boot information.";
1177491d8ee7SSantosh Puranik 
1178491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1179491d8ee7SSantosh Puranik         [aResp{std::move(aResp)}, bootSource{std::move(bootSource)},
1180491d8ee7SSantosh Puranik          bootEnable{std::move(bootEnable)}](
1181491d8ee7SSantosh Puranik             const boost::system::error_code ec,
1182491d8ee7SSantosh Puranik             const sdbusplus::message::variant<bool> &oneTime) {
1183491d8ee7SSantosh Puranik             if (ec)
1184491d8ee7SSantosh Puranik             {
1185491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1186491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1187491d8ee7SSantosh Puranik                 return;
1188491d8ee7SSantosh Puranik             }
1189491d8ee7SSantosh Puranik 
1190491d8ee7SSantosh Puranik             const bool *oneTimePtr = std::get_if<bool>(&oneTime);
1191491d8ee7SSantosh Puranik 
1192491d8ee7SSantosh Puranik             if (!oneTimePtr)
1193491d8ee7SSantosh Puranik             {
1194491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1195491d8ee7SSantosh Puranik                 return;
1196491d8ee7SSantosh Puranik             }
1197491d8ee7SSantosh Puranik 
1198491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Got one time: " << *oneTimePtr;
1199491d8ee7SSantosh Puranik 
1200491d8ee7SSantosh Puranik             setBootModeOrSource(aResp, *oneTimePtr, std::move(bootSource),
1201491d8ee7SSantosh Puranik                                 std::move(bootEnable));
1202491d8ee7SSantosh Puranik         },
1203491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
1204491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
1205491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
1206491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled");
1207491d8ee7SSantosh Puranik }
1208491d8ee7SSantosh Puranik 
1209491d8ee7SSantosh Puranik /**
1210c5b2abe0SLewanczyk, Dawid  * SystemsCollection derived class for delivering ComputerSystems Collection
1211c5b2abe0SLewanczyk, Dawid  * Schema
1212c5b2abe0SLewanczyk, Dawid  */
12131abe55efSEd Tanous class SystemsCollection : public Node
12141abe55efSEd Tanous {
1215c5b2abe0SLewanczyk, Dawid   public:
12161abe55efSEd Tanous     SystemsCollection(CrowApp &app) : Node(app, "/redfish/v1/Systems/")
12171abe55efSEd Tanous     {
1218c5b2abe0SLewanczyk, Dawid         entityPrivileges = {
1219c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::get, {{"Login"}}},
1220c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::head, {{"Login"}}},
1221c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1222c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1223c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1224c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1225c5b2abe0SLewanczyk, Dawid     }
1226c5b2abe0SLewanczyk, Dawid 
1227c5b2abe0SLewanczyk, Dawid   private:
122855c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
12291abe55efSEd Tanous                const std::vector<std::string> &params) override
12301abe55efSEd Tanous     {
12310f74e643SEd Tanous         res.jsonValue["@odata.type"] =
12320f74e643SEd Tanous             "#ComputerSystemCollection.ComputerSystemCollection";
12330f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Systems";
12340f74e643SEd Tanous         res.jsonValue["@odata.context"] =
12350f74e643SEd Tanous             "/redfish/v1/"
12360f74e643SEd Tanous             "$metadata#ComputerSystemCollection.ComputerSystemCollection";
12370f74e643SEd Tanous         res.jsonValue["Name"] = "Computer System Collection";
1238029573d4SEd Tanous         res.jsonValue["Members"] = {
1239029573d4SEd Tanous             {{"@odata.id", "/redfish/v1/Systems/system"}}};
1240029573d4SEd Tanous         res.jsonValue["Members@odata.count"] = 1;
1241029573d4SEd Tanous         res.end();
1242c5b2abe0SLewanczyk, Dawid     }
1243c5b2abe0SLewanczyk, Dawid };
1244c5b2abe0SLewanczyk, Dawid 
1245c5b2abe0SLewanczyk, Dawid /**
1246cc340dd9SEd Tanous  * SystemActionsReset class supports handle POST method for Reset action.
1247cc340dd9SEd Tanous  * The class retrieves and sends data directly to D-Bus.
1248cc340dd9SEd Tanous  */
1249cc340dd9SEd Tanous class SystemActionsReset : public Node
1250cc340dd9SEd Tanous {
1251cc340dd9SEd Tanous   public:
1252cc340dd9SEd Tanous     SystemActionsReset(CrowApp &app) :
1253029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset/")
1254cc340dd9SEd Tanous     {
1255cc340dd9SEd Tanous         entityPrivileges = {
1256cc340dd9SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1257cc340dd9SEd Tanous     }
1258cc340dd9SEd Tanous 
1259cc340dd9SEd Tanous   private:
1260cc340dd9SEd Tanous     /**
1261cc340dd9SEd Tanous      * Function handles POST method request.
1262cc340dd9SEd Tanous      * Analyzes POST body message before sends Reset request data to D-Bus.
1263cc340dd9SEd Tanous      */
1264cc340dd9SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
1265cc340dd9SEd Tanous                 const std::vector<std::string> &params) override
1266cc340dd9SEd Tanous     {
1267cc340dd9SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1268cc340dd9SEd Tanous 
12699712f8acSEd Tanous         std::string resetType;
12709712f8acSEd Tanous         if (!json_util::readJson(req, res, "ResetType", resetType))
1271cc340dd9SEd Tanous         {
1272cc340dd9SEd Tanous             return;
1273cc340dd9SEd Tanous         }
1274cc340dd9SEd Tanous 
1275d22c8396SJason M. Bills         // Get the command and host vs. chassis
1276cc340dd9SEd Tanous         std::string command;
1277d22c8396SJason M. Bills         bool hostCommand;
12789712f8acSEd Tanous         if (resetType == "On")
1279cc340dd9SEd Tanous         {
1280cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.On";
1281d22c8396SJason M. Bills             hostCommand = true;
1282d22c8396SJason M. Bills         }
1283d22c8396SJason M. Bills         else if (resetType == "ForceOff")
1284d22c8396SJason M. Bills         {
1285d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Chassis.Transition.Off";
1286d22c8396SJason M. Bills             hostCommand = false;
1287d22c8396SJason M. Bills         }
1288d22c8396SJason M. Bills         else if (resetType == "ForceOn")
1289d22c8396SJason M. Bills         {
1290d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Host.Transition.On";
1291d22c8396SJason M. Bills             hostCommand = true;
1292d22c8396SJason M. Bills         }
1293d22c8396SJason M. Bills         else if (resetType == "ForceRestart")
1294d22c8396SJason M. Bills         {
1295d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Chassis.Transition.Reset";
1296d22c8396SJason M. Bills             hostCommand = false;
1297cc340dd9SEd Tanous         }
12989712f8acSEd Tanous         else if (resetType == "GracefulShutdown")
1299cc340dd9SEd Tanous         {
1300cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.Off";
1301d22c8396SJason M. Bills             hostCommand = true;
1302cc340dd9SEd Tanous         }
13039712f8acSEd Tanous         else if (resetType == "GracefulRestart")
1304cc340dd9SEd Tanous         {
13059712f8acSEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.Reboot";
1306d22c8396SJason M. Bills             hostCommand = true;
1307d22c8396SJason M. Bills         }
1308d22c8396SJason M. Bills         else if (resetType == "PowerCycle")
1309d22c8396SJason M. Bills         {
1310d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Chassis.Transition.PowerCycle";
1311d22c8396SJason M. Bills             hostCommand = false;
1312cc340dd9SEd Tanous         }
1313bfd5b826SLakshminarayana R. Kammath         else if (resetType == "Nmi")
1314bfd5b826SLakshminarayana R. Kammath         {
1315bfd5b826SLakshminarayana R. Kammath             doNMI(asyncResp);
1316bfd5b826SLakshminarayana R. Kammath             return;
1317bfd5b826SLakshminarayana R. Kammath         }
1318cc340dd9SEd Tanous         else
1319cc340dd9SEd Tanous         {
1320f12894f8SJason M. Bills             messages::actionParameterUnknown(res, "Reset", resetType);
1321cc340dd9SEd Tanous             return;
1322cc340dd9SEd Tanous         }
1323cc340dd9SEd Tanous 
1324d22c8396SJason M. Bills         if (hostCommand)
1325d22c8396SJason M. Bills         {
1326cc340dd9SEd Tanous             crow::connections::systemBus->async_method_call(
1327d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
1328cc340dd9SEd Tanous                     if (ec)
1329cc340dd9SEd Tanous                     {
1330cc340dd9SEd Tanous                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1331d22c8396SJason M. Bills                         if (ec.value() == boost::asio::error::invalid_argument)
1332d22c8396SJason M. Bills                         {
1333d22c8396SJason M. Bills                             messages::actionParameterNotSupported(
1334d22c8396SJason M. Bills                                 asyncResp->res, resetType, "Reset");
1335d22c8396SJason M. Bills                         }
1336d22c8396SJason M. Bills                         else
1337d22c8396SJason M. Bills                         {
1338f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
1339d22c8396SJason M. Bills                         }
1340cc340dd9SEd Tanous                         return;
1341cc340dd9SEd Tanous                     }
1342f12894f8SJason M. Bills                     messages::success(asyncResp->res);
1343cc340dd9SEd Tanous                 },
1344cc340dd9SEd Tanous                 "xyz.openbmc_project.State.Host",
1345cc340dd9SEd Tanous                 "/xyz/openbmc_project/state/host0",
1346cc340dd9SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
13479712f8acSEd Tanous                 "xyz.openbmc_project.State.Host", "RequestedHostTransition",
1348abf2add6SEd Tanous                 std::variant<std::string>{command});
1349cc340dd9SEd Tanous         }
1350d22c8396SJason M. Bills         else
1351d22c8396SJason M. Bills         {
1352d22c8396SJason M. Bills             crow::connections::systemBus->async_method_call(
1353d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
1354d22c8396SJason M. Bills                     if (ec)
1355d22c8396SJason M. Bills                     {
1356d22c8396SJason M. Bills                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1357d22c8396SJason M. Bills                         if (ec.value() == boost::asio::error::invalid_argument)
1358d22c8396SJason M. Bills                         {
1359d22c8396SJason M. Bills                             messages::actionParameterNotSupported(
1360d22c8396SJason M. Bills                                 asyncResp->res, resetType, "Reset");
1361d22c8396SJason M. Bills                         }
1362d22c8396SJason M. Bills                         else
1363d22c8396SJason M. Bills                         {
1364d22c8396SJason M. Bills                             messages::internalError(asyncResp->res);
1365d22c8396SJason M. Bills                         }
1366d22c8396SJason M. Bills                         return;
1367d22c8396SJason M. Bills                     }
1368d22c8396SJason M. Bills                     messages::success(asyncResp->res);
1369d22c8396SJason M. Bills                 },
1370d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis",
1371d22c8396SJason M. Bills                 "/xyz/openbmc_project/state/chassis0",
1372d22c8396SJason M. Bills                 "org.freedesktop.DBus.Properties", "Set",
1373d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition",
1374d22c8396SJason M. Bills                 std::variant<std::string>{command});
1375d22c8396SJason M. Bills         }
1376d22c8396SJason M. Bills     }
1377bfd5b826SLakshminarayana R. Kammath     /**
1378bfd5b826SLakshminarayana R. Kammath      * Function transceives data with dbus directly.
1379bfd5b826SLakshminarayana R. Kammath      */
1380bfd5b826SLakshminarayana R. Kammath     void doNMI(const std::shared_ptr<AsyncResp> &asyncResp)
1381bfd5b826SLakshminarayana R. Kammath     {
1382bfd5b826SLakshminarayana R. Kammath         constexpr char const *serviceName =
1383bfd5b826SLakshminarayana R. Kammath             "xyz.openbmc_project.Control.Host.NMI";
1384bfd5b826SLakshminarayana R. Kammath         constexpr char const *objectPath =
1385bfd5b826SLakshminarayana R. Kammath             "/xyz/openbmc_project/control/host0/nmi";
1386bfd5b826SLakshminarayana R. Kammath         constexpr char const *interfaceName =
1387bfd5b826SLakshminarayana R. Kammath             "xyz.openbmc_project.Control.Host.NMI";
1388bfd5b826SLakshminarayana R. Kammath         constexpr char const *method = "NMI";
1389bfd5b826SLakshminarayana R. Kammath 
1390bfd5b826SLakshminarayana R. Kammath         crow::connections::systemBus->async_method_call(
1391bfd5b826SLakshminarayana R. Kammath             [asyncResp](const boost::system::error_code ec) {
1392bfd5b826SLakshminarayana R. Kammath                 if (ec)
1393bfd5b826SLakshminarayana R. Kammath                 {
1394bfd5b826SLakshminarayana R. Kammath                     BMCWEB_LOG_ERROR << " Bad D-Bus request error: " << ec;
1395bfd5b826SLakshminarayana R. Kammath                     messages::internalError(asyncResp->res);
1396bfd5b826SLakshminarayana R. Kammath                     return;
1397bfd5b826SLakshminarayana R. Kammath                 }
1398bfd5b826SLakshminarayana R. Kammath                 messages::success(asyncResp->res);
1399bfd5b826SLakshminarayana R. Kammath             },
1400bfd5b826SLakshminarayana R. Kammath             serviceName, objectPath, interfaceName, method);
1401bfd5b826SLakshminarayana R. Kammath     }
1402cc340dd9SEd Tanous };
1403cc340dd9SEd Tanous 
1404cc340dd9SEd Tanous /**
14056617338dSEd Tanous  * Systems derived class for delivering Computer Systems Schema.
1406c5b2abe0SLewanczyk, Dawid  */
14071abe55efSEd Tanous class Systems : public Node
14081abe55efSEd Tanous {
1409c5b2abe0SLewanczyk, Dawid   public:
1410c5b2abe0SLewanczyk, Dawid     /*
1411c5b2abe0SLewanczyk, Dawid      * Default Constructor
1412c5b2abe0SLewanczyk, Dawid      */
1413029573d4SEd Tanous     Systems(CrowApp &app) : Node(app, "/redfish/v1/Systems/system/")
14141abe55efSEd Tanous     {
1415c5b2abe0SLewanczyk, Dawid         entityPrivileges = {
1416c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::get, {{"Login"}}},
1417c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::head, {{"Login"}}},
1418c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1419c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1420c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1421c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1422c5b2abe0SLewanczyk, Dawid     }
1423c5b2abe0SLewanczyk, Dawid 
1424c5b2abe0SLewanczyk, Dawid   private:
1425c5b2abe0SLewanczyk, Dawid     /**
1426c5b2abe0SLewanczyk, Dawid      * Functions triggers appropriate requests on DBus
1427c5b2abe0SLewanczyk, Dawid      */
142855c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
14291abe55efSEd Tanous                const std::vector<std::string> &params) override
14301abe55efSEd Tanous     {
1431491d8ee7SSantosh Puranik         res.jsonValue["@odata.type"] = "#ComputerSystem.v1_6_0.ComputerSystem";
14320f74e643SEd Tanous         res.jsonValue["@odata.context"] =
14330f74e643SEd Tanous             "/redfish/v1/$metadata#ComputerSystem.ComputerSystem";
1434029573d4SEd Tanous         res.jsonValue["Name"] = "Computer System";
1435029573d4SEd Tanous         res.jsonValue["Id"] = "system";
14360f74e643SEd Tanous         res.jsonValue["SystemType"] = "Physical";
14370f74e643SEd Tanous         res.jsonValue["Description"] = "Computer System";
14380f74e643SEd Tanous         res.jsonValue["ProcessorSummary"]["Count"] = 0;
14390f74e643SEd Tanous         res.jsonValue["ProcessorSummary"]["Status"]["State"] = "Disabled";
14400f74e643SEd Tanous         res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] = int(0);
14410f74e643SEd Tanous         res.jsonValue["MemorySummary"]["Status"]["State"] = "Disabled";
1442029573d4SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Systems/system";
144304a258f4SEd Tanous 
1444443c2934SRapkiewicz, Pawel         res.jsonValue["Processors"] = {
1445029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/Processors"}};
1446443c2934SRapkiewicz, Pawel         res.jsonValue["Memory"] = {
1447029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/Memory"}};
1448029573d4SEd Tanous 
1449cc340dd9SEd Tanous         // TODO Need to support ForceRestart.
1450cc340dd9SEd Tanous         res.jsonValue["Actions"]["#ComputerSystem.Reset"] = {
1451cc340dd9SEd Tanous             {"target",
1452029573d4SEd Tanous              "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset"},
1453cc340dd9SEd Tanous             {"ResetType@Redfish.AllowableValues",
1454d22c8396SJason M. Bills              {"On", "ForceOff", "ForceOn", "ForceRestart", "GracefulRestart",
1455bfd5b826SLakshminarayana R. Kammath               "GracefulShutdown", "PowerCycle", "Nmi"}}};
1456c5b2abe0SLewanczyk, Dawid 
1457c4bf6374SJason M. Bills         res.jsonValue["LogServices"] = {
1458029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/LogServices"}};
1459c4bf6374SJason M. Bills 
1460c5d03ff4SJennifer Lee         res.jsonValue["Links"]["ManagedBy"] = {
1461c5d03ff4SJennifer Lee             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
1462c5d03ff4SJennifer Lee 
1463c5d03ff4SJennifer Lee         res.jsonValue["Status"] = {
1464c5d03ff4SJennifer Lee             {"Health", "OK"},
1465c5d03ff4SJennifer Lee             {"State", "Enabled"},
1466c5d03ff4SJennifer Lee         };
1467a0803efaSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1468c5b2abe0SLewanczyk, Dawid 
1469b49ac873SJames Feist         constexpr const std::array<const char *, 2> inventoryForSystems = {
1470b49ac873SJames Feist             "xyz.openbmc_project.Inventory.Item.Dimm",
1471b49ac873SJames Feist             "xyz.openbmc_project.Inventory.Item.Cpu"};
1472b49ac873SJames Feist 
1473b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
1474b49ac873SJames Feist         crow::connections::systemBus->async_method_call(
1475b49ac873SJames Feist             [health](const boost::system::error_code ec,
1476b49ac873SJames Feist                      std::vector<std::string> &resp) {
1477b49ac873SJames Feist                 if (ec)
1478b49ac873SJames Feist                 {
1479b49ac873SJames Feist                     // no inventory
1480b49ac873SJames Feist                     return;
1481b49ac873SJames Feist                 }
1482b49ac873SJames Feist 
1483b49ac873SJames Feist                 health->inventory = std::move(resp);
1484b49ac873SJames Feist             },
1485b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper",
1486b49ac873SJames Feist             "/xyz/openbmc_project/object_mapper",
1487b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1488b49ac873SJames Feist             int32_t(0), inventoryForSystems);
1489b49ac873SJames Feist 
1490b49ac873SJames Feist         health->populate();
1491b49ac873SJames Feist 
1492c5d03ff4SJennifer Lee         getMainChassisId(asyncResp, [](const std::string &chassisId,
1493c5d03ff4SJennifer Lee                                        std::shared_ptr<AsyncResp> aRsp) {
1494c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["Chassis"] = {
1495c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
1496c5d03ff4SJennifer Lee         });
14976c34de48SEd Tanous         getLedGroupIdentify(
1498a0803efaSEd Tanous             asyncResp,
1499c5d03ff4SJennifer Lee             [](const bool &asserted, const std::shared_ptr<AsyncResp> aRsp) {
15001abe55efSEd Tanous                 if (asserted)
15011abe55efSEd Tanous                 {
1502c5b2abe0SLewanczyk, Dawid                     // If led group is asserted, then another call is needed to
1503c5b2abe0SLewanczyk, Dawid                     // get led status
15046c34de48SEd Tanous                     getLedIdentify(
1505c5d03ff4SJennifer Lee                         aRsp, [](const std::string &ledStatus,
1506c5d03ff4SJennifer Lee                                  const std::shared_ptr<AsyncResp> aRsp) {
15071abe55efSEd Tanous                             if (!ledStatus.empty())
15081abe55efSEd Tanous                             {
1509c5d03ff4SJennifer Lee                                 aRsp->res.jsonValue["IndicatorLED"] = ledStatus;
1510c5b2abe0SLewanczyk, Dawid                             }
1511c5b2abe0SLewanczyk, Dawid                         });
15121abe55efSEd Tanous                 }
15131abe55efSEd Tanous                 else
15141abe55efSEd Tanous                 {
1515c5d03ff4SJennifer Lee                     aRsp->res.jsonValue["IndicatorLED"] = "Off";
1516c5b2abe0SLewanczyk, Dawid                 }
1517c5b2abe0SLewanczyk, Dawid             });
1518029573d4SEd Tanous         getComputerSystem(asyncResp);
15196c34de48SEd Tanous         getHostState(asyncResp);
1520491d8ee7SSantosh Puranik         getBootProperties(asyncResp);
1521f5c9f8bdSJason M. Bills         getPCIeDeviceList(asyncResp);
1522c5b2abe0SLewanczyk, Dawid     }
1523c5b2abe0SLewanczyk, Dawid 
152455c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
15251abe55efSEd Tanous                  const std::vector<std::string> &params) override
15261abe55efSEd Tanous     {
1527cde19e5fSSantosh Puranik         std::optional<std::string> indicatorLed;
1528491d8ee7SSantosh Puranik         std::optional<nlohmann::json> bootProps;
152941352c24SSantosh Puranik         auto asyncResp = std::make_shared<AsyncResp>(res);
153041352c24SSantosh Puranik 
153141352c24SSantosh Puranik         if (!json_util::readJson(req, asyncResp->res, "IndicatorLED",
153241352c24SSantosh Puranik                                  indicatorLed, "Boot", bootProps))
15336617338dSEd Tanous         {
15346617338dSEd Tanous             return;
15356617338dSEd Tanous         }
1536491d8ee7SSantosh Puranik 
1537d573bb2aSJennifer Lee         asyncResp->res.result(boost::beast::http::status::no_content);
1538491d8ee7SSantosh Puranik 
1539491d8ee7SSantosh Puranik         if (bootProps)
1540491d8ee7SSantosh Puranik         {
1541491d8ee7SSantosh Puranik             std::optional<std::string> bootSource;
1542491d8ee7SSantosh Puranik             std::optional<std::string> bootEnable;
1543491d8ee7SSantosh Puranik 
1544491d8ee7SSantosh Puranik             if (!json_util::readJson(*bootProps, asyncResp->res,
1545491d8ee7SSantosh Puranik                                      "BootSourceOverrideTarget", bootSource,
1546491d8ee7SSantosh Puranik                                      "BootSourceOverrideEnabled", bootEnable))
1547491d8ee7SSantosh Puranik             {
1548491d8ee7SSantosh Puranik                 return;
1549491d8ee7SSantosh Puranik             }
1550491d8ee7SSantosh Puranik             setBootProperties(asyncResp, std::move(bootSource),
1551491d8ee7SSantosh Puranik                               std::move(bootEnable));
1552491d8ee7SSantosh Puranik         }
15539712f8acSEd Tanous         if (indicatorLed)
15546617338dSEd Tanous         {
15559712f8acSEd Tanous             std::string dbusLedState;
1556d573bb2aSJennifer Lee             if (*indicatorLed == "Lit")
15579712f8acSEd Tanous             {
1558d573bb2aSJennifer Lee                 dbusLedState = "xyz.openbmc_project.Led.Physical.Action.On";
15596617338dSEd Tanous             }
15605c6221acSGunnar Mills             else if (*indicatorLed == "Blinking")
15616617338dSEd Tanous             {
15625c6221acSGunnar Mills                 dbusLedState = "xyz.openbmc_project.Led.Physical.Action.Blink";
15636617338dSEd Tanous             }
15649712f8acSEd Tanous             else if (*indicatorLed == "Off")
15656617338dSEd Tanous             {
15669712f8acSEd Tanous                 dbusLedState = "xyz.openbmc_project.Led.Physical.Action.Off";
15676617338dSEd Tanous             }
15686617338dSEd Tanous             else
15696617338dSEd Tanous             {
1570a08b46ccSJason M. Bills                 messages::propertyValueNotInList(res, *indicatorLed,
1571a08b46ccSJason M. Bills                                                  "IndicatorLED");
15726617338dSEd Tanous                 return;
15736617338dSEd Tanous             }
15746617338dSEd Tanous 
1575c5b2abe0SLewanczyk, Dawid             // Update led group
157655c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Update led group.";
157755c7b7a2SEd Tanous             crow::connections::systemBus->async_method_call(
1578cde19e5fSSantosh Puranik                 [asyncResp](const boost::system::error_code ec) {
15791abe55efSEd Tanous                     if (ec)
15801abe55efSEd Tanous                     {
158155c7b7a2SEd Tanous                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1582f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
1583c5b2abe0SLewanczyk, Dawid                         return;
1584c5b2abe0SLewanczyk, Dawid                     }
158555c7b7a2SEd Tanous                     BMCWEB_LOG_DEBUG << "Led group update done.";
1586c5b2abe0SLewanczyk, Dawid                 },
1587c5b2abe0SLewanczyk, Dawid                 "xyz.openbmc_project.LED.GroupManager",
1588c5b2abe0SLewanczyk, Dawid                 "/xyz/openbmc_project/led/groups/enclosure_identify",
1589c5b2abe0SLewanczyk, Dawid                 "org.freedesktop.DBus.Properties", "Set",
1590c5b2abe0SLewanczyk, Dawid                 "xyz.openbmc_project.Led.Group", "Asserted",
1591abf2add6SEd Tanous                 std::variant<bool>(
15926617338dSEd Tanous                     (dbusLedState ==
15936617338dSEd Tanous                              "xyz.openbmc_project.Led.Physical.Action.Off"
15946617338dSEd Tanous                          ? false
15956617338dSEd Tanous                          : true)));
1596c5b2abe0SLewanczyk, Dawid             // Update identify led status
159755c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Update led SoftwareInventoryCollection.";
159855c7b7a2SEd Tanous             crow::connections::systemBus->async_method_call(
15996617338dSEd Tanous                 [asyncResp{std::move(asyncResp)},
16009712f8acSEd Tanous                  indicatorLed{std::move(*indicatorLed)}](
1601c5b2abe0SLewanczyk, Dawid                     const boost::system::error_code ec) {
16021abe55efSEd Tanous                     if (ec)
16031abe55efSEd Tanous                     {
160455c7b7a2SEd Tanous                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1605f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
1606c5b2abe0SLewanczyk, Dawid                         return;
1607c5b2abe0SLewanczyk, Dawid                     }
160855c7b7a2SEd Tanous                     BMCWEB_LOG_DEBUG << "Led state update done.";
1609c5b2abe0SLewanczyk, Dawid                 },
1610c5b2abe0SLewanczyk, Dawid                 "xyz.openbmc_project.LED.Controller.identify",
1611c5b2abe0SLewanczyk, Dawid                 "/xyz/openbmc_project/led/physical/identify",
1612c5b2abe0SLewanczyk, Dawid                 "org.freedesktop.DBus.Properties", "Set",
1613c5b2abe0SLewanczyk, Dawid                 "xyz.openbmc_project.Led.Physical", "State",
1614abf2add6SEd Tanous                 std::variant<std::string>(dbusLedState));
16156617338dSEd Tanous         }
1616c5b2abe0SLewanczyk, Dawid     }
1617c5b2abe0SLewanczyk, Dawid };
1618c5b2abe0SLewanczyk, Dawid } // namespace redfish
1619