xref: /openbmc/bmcweb/features/redfish/lib/systems.hpp (revision 57e8c9be2255aa877b2d927aaa49354412b7b07f)
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*57e8c9beSAlpana Kumari /*
32*57e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Count" based on Cpu PresenceState
33*57e8c9beSAlpana Kumari  *
34*57e8c9beSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
35*57e8c9beSAlpana Kumari  * @param[in] cpuPresenceState CPU present or not
36*57e8c9beSAlpana Kumari  *
37*57e8c9beSAlpana Kumari  * @return None.
38*57e8c9beSAlpana Kumari  */
39*57e8c9beSAlpana Kumari void modifyCpuPresenceState(std::shared_ptr<AsyncResp> aResp,
40*57e8c9beSAlpana Kumari                             const std::variant<bool> &cpuPresenceState)
41*57e8c9beSAlpana Kumari {
42*57e8c9beSAlpana Kumari     const bool *isCpuPresent = std::get_if<bool>(&cpuPresenceState);
43*57e8c9beSAlpana Kumari 
44*57e8c9beSAlpana Kumari     if (isCpuPresent == nullptr)
45*57e8c9beSAlpana Kumari     {
46*57e8c9beSAlpana Kumari         messages::internalError(aResp->res);
47*57e8c9beSAlpana Kumari         return;
48*57e8c9beSAlpana Kumari     }
49*57e8c9beSAlpana Kumari     BMCWEB_LOG_DEBUG << "Cpu Present:" << *isCpuPresent;
50*57e8c9beSAlpana Kumari 
51*57e8c9beSAlpana Kumari     nlohmann::json &procCount =
52*57e8c9beSAlpana Kumari         aResp->res.jsonValue["ProcessorSummary"]["Count"];
53*57e8c9beSAlpana Kumari     if (*isCpuPresent == true)
54*57e8c9beSAlpana Kumari     {
55*57e8c9beSAlpana Kumari         procCount = procCount.get<int>() + 1;
56*57e8c9beSAlpana Kumari     }
57*57e8c9beSAlpana Kumari     aResp->res.jsonValue["ProcessorSummary"]["Count"] = procCount;
58*57e8c9beSAlpana Kumari }
59*57e8c9beSAlpana Kumari 
60*57e8c9beSAlpana Kumari /*
61*57e8c9beSAlpana Kumari  * @brief Update "ProcessorSummary" "Status" "State" based on
62*57e8c9beSAlpana Kumari  *        CPU Functional State
63*57e8c9beSAlpana Kumari  *
64*57e8c9beSAlpana Kumari  * @param[in] aResp Shared pointer for completing asynchronous calls
65*57e8c9beSAlpana Kumari  * @param[in] cpuFunctionalState is CPU functional true/false
66*57e8c9beSAlpana Kumari  *
67*57e8c9beSAlpana Kumari  * @return None.
68*57e8c9beSAlpana Kumari  */
69*57e8c9beSAlpana Kumari void modifyCpuFunctionalState(std::shared_ptr<AsyncResp> aResp,
70*57e8c9beSAlpana Kumari                               const std::variant<bool> &cpuFunctionalState)
71*57e8c9beSAlpana Kumari {
72*57e8c9beSAlpana Kumari     const bool *isCpuFunctional = std::get_if<bool>(&cpuFunctionalState);
73*57e8c9beSAlpana Kumari 
74*57e8c9beSAlpana Kumari     if (isCpuFunctional == nullptr)
75*57e8c9beSAlpana Kumari     {
76*57e8c9beSAlpana Kumari         messages::internalError(aResp->res);
77*57e8c9beSAlpana Kumari         return;
78*57e8c9beSAlpana Kumari     }
79*57e8c9beSAlpana Kumari     BMCWEB_LOG_DEBUG << "Cpu Functional:" << *isCpuFunctional;
80*57e8c9beSAlpana Kumari 
81*57e8c9beSAlpana Kumari     nlohmann::json &prevProcState =
82*57e8c9beSAlpana Kumari         aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"];
83*57e8c9beSAlpana Kumari 
84*57e8c9beSAlpana Kumari     // Set it as Enabled if atleast one CPU is functional
85*57e8c9beSAlpana Kumari     // Update STATE only if previous State was Non_Functional and current CPU is
86*57e8c9beSAlpana Kumari     // Functional.
87*57e8c9beSAlpana Kumari     if (prevProcState == "Disabled")
88*57e8c9beSAlpana Kumari     {
89*57e8c9beSAlpana Kumari         if (*isCpuFunctional == true)
90*57e8c9beSAlpana Kumari         {
91*57e8c9beSAlpana Kumari             aResp->res.jsonValue["ProcessorSummary"]["Status"]["State"] =
92*57e8c9beSAlpana Kumari                 "Enabled";
93*57e8c9beSAlpana Kumari         }
94*57e8c9beSAlpana Kumari     }
95*57e8c9beSAlpana Kumari }
96*57e8c9beSAlpana Kumari 
97*57e8c9beSAlpana Kumari /*
98c5b2abe0SLewanczyk, Dawid  * @brief Retrieves computer system properties over dbus
99c5b2abe0SLewanczyk, Dawid  *
100c5b2abe0SLewanczyk, Dawid  * @param[in] aResp Shared pointer for completing asynchronous calls
101c5b2abe0SLewanczyk, Dawid  * @param[in] name  Computer system name from request
102c5b2abe0SLewanczyk, Dawid  *
103c5b2abe0SLewanczyk, Dawid  * @return None.
104c5b2abe0SLewanczyk, Dawid  */
105029573d4SEd Tanous void getComputerSystem(std::shared_ptr<AsyncResp> aResp)
1061abe55efSEd Tanous {
10755c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get available system components.";
10855c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
109c5d03ff4SJennifer Lee         [aResp](
110c5b2abe0SLewanczyk, Dawid             const boost::system::error_code ec,
111c5b2abe0SLewanczyk, Dawid             const std::vector<std::pair<
1126c34de48SEd Tanous                 std::string,
1136c34de48SEd Tanous                 std::vector<std::pair<std::string, std::vector<std::string>>>>>
114c5b2abe0SLewanczyk, Dawid                 &subtree) {
1151abe55efSEd Tanous             if (ec)
1161abe55efSEd Tanous             {
11755c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error";
118f12894f8SJason M. Bills                 messages::internalError(aResp->res);
119c5b2abe0SLewanczyk, Dawid                 return;
120c5b2abe0SLewanczyk, Dawid             }
121c5b2abe0SLewanczyk, Dawid             // Iterate over all retrieved ObjectPaths.
1226c34de48SEd Tanous             for (const std::pair<std::string,
1236c34de48SEd Tanous                                  std::vector<std::pair<
1246c34de48SEd Tanous                                      std::string, std::vector<std::string>>>>
1251abe55efSEd Tanous                      &object : subtree)
1261abe55efSEd Tanous             {
127c5b2abe0SLewanczyk, Dawid                 const std::string &path = object.first;
12855c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "Got path: " << path;
1291abe55efSEd Tanous                 const std::vector<
1301abe55efSEd Tanous                     std::pair<std::string, std::vector<std::string>>>
131c5b2abe0SLewanczyk, Dawid                     &connectionNames = object.second;
1321abe55efSEd Tanous                 if (connectionNames.size() < 1)
1331abe55efSEd Tanous                 {
134c5b2abe0SLewanczyk, Dawid                     continue;
135c5b2abe0SLewanczyk, Dawid                 }
136029573d4SEd Tanous 
1376c34de48SEd Tanous                 // This is not system, so check if it's cpu, dimm, UUID or
1386c34de48SEd Tanous                 // BiosVer
13904a258f4SEd Tanous                 for (const auto &connection : connectionNames)
1401abe55efSEd Tanous                 {
14104a258f4SEd Tanous                     for (const auto &interfaceName : connection.second)
1421abe55efSEd Tanous                     {
14304a258f4SEd Tanous                         if (interfaceName ==
14404a258f4SEd Tanous                             "xyz.openbmc_project.Inventory.Item.Dimm")
1451abe55efSEd Tanous                         {
1461abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
14704a258f4SEd Tanous                                 << "Found Dimm, now get its properties.";
14855c7b7a2SEd Tanous                             crow::connections::systemBus->async_method_call(
149029573d4SEd Tanous                                 [aResp](const boost::system::error_code ec,
1506c34de48SEd Tanous                                         const std::vector<
1516c34de48SEd Tanous                                             std::pair<std::string, VariantType>>
1521abe55efSEd Tanous                                             &properties) {
1531abe55efSEd Tanous                                     if (ec)
1541abe55efSEd Tanous                                     {
1551abe55efSEd Tanous                                         BMCWEB_LOG_ERROR
1566c34de48SEd Tanous                                             << "DBUS response error " << ec;
157f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
158c5b2abe0SLewanczyk, Dawid                                         return;
159c5b2abe0SLewanczyk, Dawid                                     }
1606c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
1616c34de48SEd Tanous                                                      << properties.size()
162c5b2abe0SLewanczyk, Dawid                                                      << "Dimm properties.";
16304a258f4SEd Tanous                                     for (const std::pair<std::string,
16404a258f4SEd Tanous                                                          VariantType>
16504a258f4SEd Tanous                                              &property : properties)
1661abe55efSEd Tanous                                     {
167029573d4SEd Tanous                                         if (property.first == "MemorySizeInKb")
1681abe55efSEd Tanous                                         {
16904a258f4SEd Tanous                                             const uint64_t *value =
170029573d4SEd Tanous                                                 sdbusplus::message::variant_ns::
171029573d4SEd Tanous                                                     get_if<uint64_t>(
1721b6b96c5SEd Tanous                                                         &property.second);
17304a258f4SEd Tanous                                             if (value != nullptr)
1741abe55efSEd Tanous                                             {
1751abe55efSEd Tanous                                                 aResp->res.jsonValue
1766c34de48SEd Tanous                                                     ["TotalSystemMemoryGi"
1776c34de48SEd Tanous                                                      "B"] +=
17804a258f4SEd Tanous                                                     *value / (1024 * 1024);
179029573d4SEd Tanous                                                 aResp->res
180029573d4SEd Tanous                                                     .jsonValue["MemorySummary"]
181029573d4SEd Tanous                                                               ["Status"]
182029573d4SEd Tanous                                                               ["State"] =
1831abe55efSEd Tanous                                                     "Enabled";
184c5b2abe0SLewanczyk, Dawid                                             }
185c5b2abe0SLewanczyk, Dawid                                         }
186c5b2abe0SLewanczyk, Dawid                                     }
187c5b2abe0SLewanczyk, Dawid                                 },
18804a258f4SEd Tanous                                 connection.first, path,
1896c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
1906c34de48SEd Tanous                                 "xyz.openbmc_project.Inventory.Item.Dimm");
1911abe55efSEd Tanous                         }
19204a258f4SEd Tanous                         else if (interfaceName ==
19304a258f4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.Cpu")
1941abe55efSEd Tanous                         {
1951abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
19604a258f4SEd Tanous                                 << "Found Cpu, now get its properties.";
197*57e8c9beSAlpana Kumari 
198a0803efaSEd Tanous                             crow::connections::systemBus->async_method_call(
199*57e8c9beSAlpana Kumari                                 [aResp, service{connection.first},
200*57e8c9beSAlpana Kumari                                  path(std::move(path))](
201*57e8c9beSAlpana Kumari                                     const boost::system::error_code ec,
2026c34de48SEd Tanous                                     const std::vector<
2036c34de48SEd Tanous                                         std::pair<std::string, VariantType>>
2041abe55efSEd Tanous                                         &properties) {
2051abe55efSEd Tanous                                     if (ec)
2061abe55efSEd Tanous                                     {
2071abe55efSEd Tanous                                         BMCWEB_LOG_ERROR
2086c34de48SEd Tanous                                             << "DBUS response error " << ec;
209f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
210c5b2abe0SLewanczyk, Dawid                                         return;
211c5b2abe0SLewanczyk, Dawid                                     }
2126c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
2136c34de48SEd Tanous                                                      << properties.size()
214c5b2abe0SLewanczyk, Dawid                                                      << "Cpu properties.";
215*57e8c9beSAlpana Kumari 
216*57e8c9beSAlpana Kumari                                     if (properties.size() > 0)
217*57e8c9beSAlpana Kumari                                     {
21804a258f4SEd Tanous                                         for (const auto &property : properties)
2191abe55efSEd Tanous                                         {
220*57e8c9beSAlpana Kumari                                             if (property.first ==
221*57e8c9beSAlpana Kumari                                                 "ProcessorFamily")
2221abe55efSEd Tanous                                             {
223a0803efaSEd Tanous                                                 const std::string *value =
224*57e8c9beSAlpana Kumari                                                     sdbusplus::message::
225*57e8c9beSAlpana Kumari                                                         variant_ns::get_if<
226*57e8c9beSAlpana Kumari                                                             std::string>(
2271b6b96c5SEd Tanous                                                             &property.second);
2281abe55efSEd Tanous                                                 if (value != nullptr)
2291abe55efSEd Tanous                                                 {
230*57e8c9beSAlpana Kumari                                                     nlohmann::json
231*57e8c9beSAlpana Kumari                                                         &procSummary =
2321abe55efSEd Tanous                                                             aResp->res.jsonValue
2336c34de48SEd Tanous                                                                 ["ProcessorSumm"
23404a258f4SEd Tanous                                                                  "ary"];
23504a258f4SEd Tanous                                                     nlohmann::json &procCount =
23604a258f4SEd Tanous                                                         procSummary["Count"];
23704a258f4SEd Tanous                                                     procCount =
238*57e8c9beSAlpana Kumari                                                         procCount.get<int>() +
239*57e8c9beSAlpana Kumari                                                         1;
240*57e8c9beSAlpana Kumari                                                     procSummary["Status"]
241*57e8c9beSAlpana Kumari                                                                ["State"] =
242c5b2abe0SLewanczyk, Dawid                                                                    "Enabled";
243*57e8c9beSAlpana Kumari                                                     procSummary["Model"] =
244*57e8c9beSAlpana Kumari                                                         *value;
245c5b2abe0SLewanczyk, Dawid                                                 }
246c5b2abe0SLewanczyk, Dawid                                             }
247c5b2abe0SLewanczyk, Dawid                                         }
248*57e8c9beSAlpana Kumari                                     }
249*57e8c9beSAlpana Kumari                                     else
250*57e8c9beSAlpana Kumari                                     {
251*57e8c9beSAlpana Kumari                                         auto getCpuPresenceState =
252*57e8c9beSAlpana Kumari                                             [aResp](
253*57e8c9beSAlpana Kumari                                                 const boost::system::error_code
254*57e8c9beSAlpana Kumari                                                     ec,
255*57e8c9beSAlpana Kumari                                                 const std::variant<bool>
256*57e8c9beSAlpana Kumari                                                     &cpuPresenceCheck) {
257*57e8c9beSAlpana Kumari                                                 if (ec)
258*57e8c9beSAlpana Kumari                                                 {
259*57e8c9beSAlpana Kumari                                                     BMCWEB_LOG_ERROR
260*57e8c9beSAlpana Kumari                                                         << "DBUS response "
261*57e8c9beSAlpana Kumari                                                            "error "
262*57e8c9beSAlpana Kumari                                                         << ec;
263*57e8c9beSAlpana Kumari                                                     return;
264*57e8c9beSAlpana Kumari                                                 }
265*57e8c9beSAlpana Kumari                                                 modifyCpuPresenceState(
266*57e8c9beSAlpana Kumari                                                     aResp, cpuPresenceCheck);
267*57e8c9beSAlpana Kumari                                             };
268*57e8c9beSAlpana Kumari 
269*57e8c9beSAlpana Kumari                                         auto getCpuFunctionalState =
270*57e8c9beSAlpana Kumari                                             [aResp](
271*57e8c9beSAlpana Kumari                                                 const boost::system::error_code
272*57e8c9beSAlpana Kumari                                                     ec,
273*57e8c9beSAlpana Kumari                                                 const std::variant<bool>
274*57e8c9beSAlpana Kumari                                                     &cpuFunctionalCheck) {
275*57e8c9beSAlpana Kumari                                                 if (ec)
276*57e8c9beSAlpana Kumari                                                 {
277*57e8c9beSAlpana Kumari                                                     BMCWEB_LOG_ERROR
278*57e8c9beSAlpana Kumari                                                         << "DBUS response "
279*57e8c9beSAlpana Kumari                                                            "error "
280*57e8c9beSAlpana Kumari                                                         << ec;
281*57e8c9beSAlpana Kumari                                                     return;
282*57e8c9beSAlpana Kumari                                                 }
283*57e8c9beSAlpana Kumari                                                 modifyCpuFunctionalState(
284*57e8c9beSAlpana Kumari                                                     aResp, cpuFunctionalCheck);
285*57e8c9beSAlpana Kumari                                             };
286*57e8c9beSAlpana Kumari                                         // Get the Presence of CPU
287*57e8c9beSAlpana Kumari                                         crow::connections::systemBus
288*57e8c9beSAlpana Kumari                                             ->async_method_call(
289*57e8c9beSAlpana Kumari                                                 std::move(getCpuPresenceState),
290*57e8c9beSAlpana Kumari                                                 service, path,
291*57e8c9beSAlpana Kumari                                                 "org.freedesktop.DBus."
292*57e8c9beSAlpana Kumari                                                 "Properties",
293*57e8c9beSAlpana Kumari                                                 "Get",
294*57e8c9beSAlpana Kumari                                                 "xyz.openbmc_project.Inventory."
295*57e8c9beSAlpana Kumari                                                 "Item",
296*57e8c9beSAlpana Kumari                                                 "Present");
297*57e8c9beSAlpana Kumari 
298*57e8c9beSAlpana Kumari                                         // Get the Functional State
299*57e8c9beSAlpana Kumari                                         crow::connections::systemBus
300*57e8c9beSAlpana Kumari                                             ->async_method_call(
301*57e8c9beSAlpana Kumari                                                 std::move(
302*57e8c9beSAlpana Kumari                                                     getCpuFunctionalState),
303*57e8c9beSAlpana Kumari                                                 service, path,
304*57e8c9beSAlpana Kumari                                                 "org.freedesktop.DBus."
305*57e8c9beSAlpana Kumari                                                 "Properties",
306*57e8c9beSAlpana Kumari                                                 "Get",
307*57e8c9beSAlpana Kumari                                                 "xyz.openbmc_project.State."
308*57e8c9beSAlpana Kumari                                                 "Decorator."
309*57e8c9beSAlpana Kumari                                                 "OperationalStatus",
310*57e8c9beSAlpana Kumari                                                 "Functional");
311*57e8c9beSAlpana Kumari 
312*57e8c9beSAlpana Kumari                                         // Get the MODEL from
313*57e8c9beSAlpana Kumari                                         // xyz.openbmc_project.Inventory.Decorator.Asset
314*57e8c9beSAlpana Kumari                                         // support it later as Model  is Empty
315*57e8c9beSAlpana Kumari                                         // currently.
316*57e8c9beSAlpana Kumari                                     }
317c5b2abe0SLewanczyk, Dawid                                 },
31804a258f4SEd Tanous                                 connection.first, path,
3196c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
3206c34de48SEd Tanous                                 "xyz.openbmc_project.Inventory.Item.Cpu");
3211abe55efSEd Tanous                         }
32204a258f4SEd Tanous                         else if (interfaceName ==
32304a258f4SEd Tanous                                  "xyz.openbmc_project.Common.UUID")
3241abe55efSEd Tanous                         {
3251abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
32604a258f4SEd Tanous                                 << "Found UUID, now get its properties.";
32755c7b7a2SEd Tanous                             crow::connections::systemBus->async_method_call(
328029573d4SEd Tanous                                 [aResp](const boost::system::error_code ec,
3296c34de48SEd Tanous                                         const std::vector<
3306c34de48SEd Tanous                                             std::pair<std::string, VariantType>>
3311abe55efSEd Tanous                                             &properties) {
3321abe55efSEd Tanous                                     if (ec)
3331abe55efSEd Tanous                                     {
3341abe55efSEd Tanous                                         BMCWEB_LOG_DEBUG
3356c34de48SEd Tanous                                             << "DBUS response error " << ec;
336f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
337c5b2abe0SLewanczyk, Dawid                                         return;
338c5b2abe0SLewanczyk, Dawid                                     }
3396c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
3406c34de48SEd Tanous                                                      << properties.size()
341c5b2abe0SLewanczyk, Dawid                                                      << "UUID properties.";
3421abe55efSEd Tanous                                     for (const std::pair<std::string,
34304a258f4SEd Tanous                                                          VariantType>
34404a258f4SEd Tanous                                              &property : properties)
3451abe55efSEd Tanous                                     {
34604a258f4SEd Tanous                                         if (property.first == "UUID")
3471abe55efSEd Tanous                                         {
348c5b2abe0SLewanczyk, Dawid                                             const std::string *value =
349029573d4SEd Tanous                                                 sdbusplus::message::variant_ns::
350029573d4SEd Tanous                                                     get_if<std::string>(
3511b6b96c5SEd Tanous                                                         &property.second);
35204a258f4SEd Tanous 
3531abe55efSEd Tanous                                             if (value != nullptr)
3541abe55efSEd Tanous                                             {
355029573d4SEd Tanous                                                 std::string valueStr = *value;
35604a258f4SEd Tanous                                                 if (valueStr.size() == 32)
3571abe55efSEd Tanous                                                 {
358029573d4SEd Tanous                                                     valueStr.insert(8, 1, '-');
359029573d4SEd Tanous                                                     valueStr.insert(13, 1, '-');
360029573d4SEd Tanous                                                     valueStr.insert(18, 1, '-');
361029573d4SEd Tanous                                                     valueStr.insert(23, 1, '-');
36204a258f4SEd Tanous                                                 }
363029573d4SEd Tanous                                                 BMCWEB_LOG_DEBUG << "UUID = "
36404a258f4SEd Tanous                                                                  << valueStr;
365029573d4SEd Tanous                                                 aResp->res.jsonValue["UUID"] =
36604a258f4SEd Tanous                                                     valueStr;
367c5b2abe0SLewanczyk, Dawid                                             }
368c5b2abe0SLewanczyk, Dawid                                         }
369c5b2abe0SLewanczyk, Dawid                                     }
370c5b2abe0SLewanczyk, Dawid                                 },
37104a258f4SEd Tanous                                 connection.first, path,
3726c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
3731abe55efSEd Tanous                                 "xyz.openbmc_project.Common.UUID");
374c5b2abe0SLewanczyk, Dawid                         }
375029573d4SEd Tanous                         else if (interfaceName ==
376029573d4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.System")
3771abe55efSEd Tanous                         {
378029573d4SEd Tanous                             crow::connections::systemBus->async_method_call(
379029573d4SEd Tanous                                 [aResp](const boost::system::error_code ec,
380029573d4SEd Tanous                                         const std::vector<
381029573d4SEd Tanous                                             std::pair<std::string, VariantType>>
382029573d4SEd Tanous                                             &propertiesList) {
383029573d4SEd Tanous                                     if (ec)
384029573d4SEd Tanous                                     {
385e4a4b9a9SJames Feist                                         // doesn't have to include this
386e4a4b9a9SJames Feist                                         // interface
387029573d4SEd Tanous                                         return;
388029573d4SEd Tanous                                     }
389029573d4SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
390029573d4SEd Tanous                                                      << propertiesList.size()
391029573d4SEd Tanous                                                      << "properties for system";
392029573d4SEd Tanous                                     for (const std::pair<std::string,
393029573d4SEd Tanous                                                          VariantType>
394029573d4SEd Tanous                                              &property : propertiesList)
395029573d4SEd Tanous                                     {
396fc5afcf9Sbeccabroek                                         const std::string &propertyName =
397fc5afcf9Sbeccabroek                                             property.first;
398fc5afcf9Sbeccabroek                                         if ((propertyName == "PartNumber") ||
399fc5afcf9Sbeccabroek                                             (propertyName == "SerialNumber") ||
400fc5afcf9Sbeccabroek                                             (propertyName == "Manufacturer") ||
401fc5afcf9Sbeccabroek                                             (propertyName == "Model"))
402fc5afcf9Sbeccabroek                                         {
403029573d4SEd Tanous                                             const std::string *value =
404fc5afcf9Sbeccabroek                                                 std::get_if<std::string>(
405029573d4SEd Tanous                                                     &property.second);
406029573d4SEd Tanous                                             if (value != nullptr)
407029573d4SEd Tanous                                             {
408029573d4SEd Tanous                                                 aResp->res
409fc5afcf9Sbeccabroek                                                     .jsonValue[propertyName] =
410029573d4SEd Tanous                                                     *value;
411029573d4SEd Tanous                                             }
412029573d4SEd Tanous                                         }
413fc5afcf9Sbeccabroek                                     }
414029573d4SEd Tanous                                     aResp->res.jsonValue["Name"] = "system";
415029573d4SEd Tanous                                     aResp->res.jsonValue["Id"] =
416029573d4SEd Tanous                                         aResp->res.jsonValue["SerialNumber"];
417cb7e1e7bSAndrew Geissler                                     // Grab the bios version
418cb7e1e7bSAndrew Geissler                                     fw_util::getActiveFwVersion(
419cb7e1e7bSAndrew Geissler                                         aResp, fw_util::biosPurpose,
420cb7e1e7bSAndrew Geissler                                         "BiosVersion");
421029573d4SEd Tanous                                 },
422029573d4SEd Tanous                                 connection.first, path,
423029573d4SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
424029573d4SEd Tanous                                 "xyz.openbmc_project.Inventory.Decorator."
425029573d4SEd Tanous                                 "Asset");
426e4a4b9a9SJames Feist 
427e4a4b9a9SJames Feist                             crow::connections::systemBus->async_method_call(
428e4a4b9a9SJames Feist                                 [aResp](
429e4a4b9a9SJames Feist                                     const boost::system::error_code ec,
430e4a4b9a9SJames Feist                                     const std::variant<std::string> &property) {
431e4a4b9a9SJames Feist                                     if (ec)
432e4a4b9a9SJames Feist                                     {
433e4a4b9a9SJames Feist                                         // doesn't have to include this
434e4a4b9a9SJames Feist                                         // interface
435e4a4b9a9SJames Feist                                         return;
436e4a4b9a9SJames Feist                                     }
437e4a4b9a9SJames Feist 
438e4a4b9a9SJames Feist                                     const std::string *value =
439e4a4b9a9SJames Feist                                         std::get_if<std::string>(&property);
440e4a4b9a9SJames Feist                                     if (value != nullptr)
441e4a4b9a9SJames Feist                                     {
442e4a4b9a9SJames Feist                                         aResp->res.jsonValue["AssetTag"] =
443e4a4b9a9SJames Feist                                             *value;
444e4a4b9a9SJames Feist                                     }
445e4a4b9a9SJames Feist                                 },
446e4a4b9a9SJames Feist                                 connection.first, path,
447e4a4b9a9SJames Feist                                 "org.freedesktop.DBus.Properties", "Get",
448e4a4b9a9SJames Feist                                 "xyz.openbmc_project.Inventory.Decorator."
449e4a4b9a9SJames Feist                                 "AssetTag",
450e4a4b9a9SJames Feist                                 "AssetTag");
451029573d4SEd Tanous                         }
452029573d4SEd Tanous                     }
453029573d4SEd Tanous                 }
454c5b2abe0SLewanczyk, Dawid             }
455c5b2abe0SLewanczyk, Dawid         },
456c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper",
457c5b2abe0SLewanczyk, Dawid         "/xyz/openbmc_project/object_mapper",
458c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
4596617338dSEd Tanous         "/xyz/openbmc_project/inventory", int32_t(0),
4606617338dSEd Tanous         std::array<const char *, 5>{
4616617338dSEd Tanous             "xyz.openbmc_project.Inventory.Decorator.Asset",
4626617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Cpu",
4636617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Dimm",
4646617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.System",
4656617338dSEd Tanous             "xyz.openbmc_project.Common.UUID",
4666617338dSEd Tanous         });
467c5b2abe0SLewanczyk, Dawid }
468c5b2abe0SLewanczyk, Dawid 
469c5b2abe0SLewanczyk, Dawid /**
470c5b2abe0SLewanczyk, Dawid  * @brief Retrieves identify led group properties over dbus
471c5b2abe0SLewanczyk, Dawid  *
472491d8ee7SSantosh Puranik  * @param[in] aResp     Shared pointer for generating response message.
473c5b2abe0SLewanczyk, Dawid  * @param[in] callback  Callback for process retrieved data.
474c5b2abe0SLewanczyk, Dawid  *
475c5b2abe0SLewanczyk, Dawid  * @return None.
476c5b2abe0SLewanczyk, Dawid  */
477c5b2abe0SLewanczyk, Dawid template <typename CallbackFunc>
478a0803efaSEd Tanous void getLedGroupIdentify(std::shared_ptr<AsyncResp> aResp,
4791abe55efSEd Tanous                          CallbackFunc &&callback)
4801abe55efSEd Tanous {
48155c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get led groups";
48255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
483c5d03ff4SJennifer Lee         [aResp,
4846617338dSEd Tanous          callback{std::move(callback)}](const boost::system::error_code &ec,
4851abe55efSEd Tanous                                         const ManagedObjectsType &resp) {
4861abe55efSEd Tanous             if (ec)
4871abe55efSEd Tanous             {
48855c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
489f12894f8SJason M. Bills                 messages::internalError(aResp->res);
490c5b2abe0SLewanczyk, Dawid                 return;
491c5b2abe0SLewanczyk, Dawid             }
4926c34de48SEd Tanous             BMCWEB_LOG_DEBUG << "Got " << resp.size() << "led group objects.";
4931abe55efSEd Tanous             for (const auto &objPath : resp)
4941abe55efSEd Tanous             {
495c5b2abe0SLewanczyk, Dawid                 const std::string &path = objPath.first;
4961abe55efSEd Tanous                 if (path.rfind("enclosure_identify") != std::string::npos)
4971abe55efSEd Tanous                 {
4981abe55efSEd Tanous                     for (const auto &interface : objPath.second)
4991abe55efSEd Tanous                     {
5006c34de48SEd Tanous                         if (interface.first == "xyz.openbmc_project.Led.Group")
5011abe55efSEd Tanous                         {
5021abe55efSEd Tanous                             for (const auto &property : interface.second)
5031abe55efSEd Tanous                             {
5041abe55efSEd Tanous                                 if (property.first == "Asserted")
5051abe55efSEd Tanous                                 {
506c5b2abe0SLewanczyk, Dawid                                     const bool *asserted =
507abf2add6SEd Tanous                                         std::get_if<bool>(&property.second);
5081abe55efSEd Tanous                                     if (nullptr != asserted)
5091abe55efSEd Tanous                                     {
510c5b2abe0SLewanczyk, Dawid                                         callback(*asserted, aResp);
5111abe55efSEd Tanous                                     }
5121abe55efSEd Tanous                                     else
5131abe55efSEd Tanous                                     {
514c5b2abe0SLewanczyk, Dawid                                         callback(false, aResp);
515c5b2abe0SLewanczyk, Dawid                                     }
516c5b2abe0SLewanczyk, Dawid                                 }
517c5b2abe0SLewanczyk, Dawid                             }
518c5b2abe0SLewanczyk, Dawid                         }
519c5b2abe0SLewanczyk, Dawid                     }
520c5b2abe0SLewanczyk, Dawid                 }
521c5b2abe0SLewanczyk, Dawid             }
522c5b2abe0SLewanczyk, Dawid         },
523c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.LED.GroupManager",
5246c34de48SEd Tanous         "/xyz/openbmc_project/led/groups", "org.freedesktop.DBus.ObjectManager",
5256c34de48SEd Tanous         "GetManagedObjects");
526c5b2abe0SLewanczyk, Dawid }
527c5b2abe0SLewanczyk, Dawid 
528c5b2abe0SLewanczyk, Dawid template <typename CallbackFunc>
5296c34de48SEd Tanous void getLedIdentify(std::shared_ptr<AsyncResp> aResp, CallbackFunc &&callback)
5301abe55efSEd Tanous {
53155c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get identify led properties";
53255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
5336617338dSEd Tanous         [aResp,
5346617338dSEd Tanous          callback{std::move(callback)}](const boost::system::error_code ec,
535c5b2abe0SLewanczyk, Dawid                                         const PropertiesType &properties) {
5361abe55efSEd Tanous             if (ec)
5371abe55efSEd Tanous             {
53855c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
539f12894f8SJason M. Bills                 messages::internalError(aResp->res);
540c5b2abe0SLewanczyk, Dawid                 return;
541c5b2abe0SLewanczyk, Dawid             }
5421abe55efSEd Tanous             BMCWEB_LOG_DEBUG << "Got " << properties.size()
5431abe55efSEd Tanous                              << "led properties.";
544c5b2abe0SLewanczyk, Dawid             std::string output;
5451abe55efSEd Tanous             for (const auto &property : properties)
5461abe55efSEd Tanous             {
5471abe55efSEd Tanous                 if (property.first == "State")
5481abe55efSEd Tanous                 {
549c5b2abe0SLewanczyk, Dawid                     const std::string *s =
550abf2add6SEd Tanous                         std::get_if<std::string>(&property.second);
5511abe55efSEd Tanous                     if (nullptr != s)
5521abe55efSEd Tanous                     {
55355c7b7a2SEd Tanous                         BMCWEB_LOG_DEBUG << "Identify Led State: " << *s;
554c5b2abe0SLewanczyk, Dawid                         const auto pos = s->rfind('.');
5551abe55efSEd Tanous                         if (pos != std::string::npos)
5561abe55efSEd Tanous                         {
557c5b2abe0SLewanczyk, Dawid                             auto led = s->substr(pos + 1);
5581abe55efSEd Tanous                             for (const std::pair<const char *, const char *>
5591abe55efSEd Tanous                                      &p :
5601abe55efSEd Tanous                                  std::array<
5616c34de48SEd Tanous                                      std::pair<const char *, const char *>, 3>{
5626c34de48SEd Tanous                                      {{"On", "Lit"},
563c5b2abe0SLewanczyk, Dawid                                       {"Blink", "Blinking"},
5641abe55efSEd Tanous                                       {"Off", "Off"}}})
5651abe55efSEd Tanous                             {
5661abe55efSEd Tanous                                 if (led == p.first)
5671abe55efSEd Tanous                                 {
568c5b2abe0SLewanczyk, Dawid                                     output = p.second;
569c5b2abe0SLewanczyk, Dawid                                 }
570c5b2abe0SLewanczyk, Dawid                             }
571c5b2abe0SLewanczyk, Dawid                         }
572c5b2abe0SLewanczyk, Dawid                     }
573c5b2abe0SLewanczyk, Dawid                 }
574c5b2abe0SLewanczyk, Dawid             }
575c5b2abe0SLewanczyk, Dawid             callback(output, aResp);
576c5b2abe0SLewanczyk, Dawid         },
577c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.LED.Controller.identify",
578c5b2abe0SLewanczyk, Dawid         "/xyz/openbmc_project/led/physical/identify",
579c5b2abe0SLewanczyk, Dawid         "org.freedesktop.DBus.Properties", "GetAll",
580c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.Led.Physical");
581c5b2abe0SLewanczyk, Dawid }
582c5b2abe0SLewanczyk, Dawid /**
583c5b2abe0SLewanczyk, Dawid  * @brief Retrieves host state properties over dbus
584c5b2abe0SLewanczyk, Dawid  *
585c5b2abe0SLewanczyk, Dawid  * @param[in] aResp     Shared pointer for completing asynchronous calls.
586c5b2abe0SLewanczyk, Dawid  *
587c5b2abe0SLewanczyk, Dawid  * @return None.
588c5b2abe0SLewanczyk, Dawid  */
589a0803efaSEd Tanous void getHostState(std::shared_ptr<AsyncResp> aResp)
5901abe55efSEd Tanous {
59155c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get host information.";
59255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
593c5d03ff4SJennifer Lee         [aResp](const boost::system::error_code ec,
594abf2add6SEd Tanous                 const std::variant<std::string> &hostState) {
5951abe55efSEd Tanous             if (ec)
5961abe55efSEd Tanous             {
59755c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
598f12894f8SJason M. Bills                 messages::internalError(aResp->res);
599c5b2abe0SLewanczyk, Dawid                 return;
600c5b2abe0SLewanczyk, Dawid             }
6016617338dSEd Tanous 
602abf2add6SEd Tanous             const std::string *s = std::get_if<std::string>(&hostState);
60355c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Host state: " << *s;
6046617338dSEd Tanous             if (s != nullptr)
6051abe55efSEd Tanous             {
606c5b2abe0SLewanczyk, Dawid                 // Verify Host State
60794732661SAndrew Geissler                 if (*s == "xyz.openbmc_project.State.Host.HostState.Running")
6081abe55efSEd Tanous                 {
60955c7b7a2SEd Tanous                     aResp->res.jsonValue["PowerState"] = "On";
6106617338dSEd Tanous                     aResp->res.jsonValue["Status"]["State"] = "Enabled";
6111abe55efSEd Tanous                 }
6121abe55efSEd Tanous                 else
6131abe55efSEd Tanous                 {
61455c7b7a2SEd Tanous                     aResp->res.jsonValue["PowerState"] = "Off";
6156617338dSEd Tanous                     aResp->res.jsonValue["Status"]["State"] = "Disabled";
616c5b2abe0SLewanczyk, Dawid                 }
617c5b2abe0SLewanczyk, Dawid             }
618c5b2abe0SLewanczyk, Dawid         },
6196c34de48SEd Tanous         "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
6206617338dSEd Tanous         "org.freedesktop.DBus.Properties", "Get",
6216617338dSEd Tanous         "xyz.openbmc_project.State.Host", "CurrentHostState");
622c5b2abe0SLewanczyk, Dawid }
623c5b2abe0SLewanczyk, Dawid 
624c5b2abe0SLewanczyk, Dawid /**
625491d8ee7SSantosh Puranik  * @brief Traslates boot source DBUS property value to redfish.
626491d8ee7SSantosh Puranik  *
627491d8ee7SSantosh Puranik  * @param[in] dbusSource    The boot source in DBUS speak.
628491d8ee7SSantosh Puranik  *
629491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source in Redfish terms. If translation
630491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
631491d8ee7SSantosh Puranik  */
632491d8ee7SSantosh Puranik static std::string dbusToRfBootSource(const std::string &dbusSource)
633491d8ee7SSantosh Puranik {
634491d8ee7SSantosh Puranik     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
635491d8ee7SSantosh Puranik     {
636491d8ee7SSantosh Puranik         return "None";
637491d8ee7SSantosh Puranik     }
638491d8ee7SSantosh Puranik     else if (dbusSource ==
639491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.Disk")
640491d8ee7SSantosh Puranik     {
641491d8ee7SSantosh Puranik         return "Hdd";
642491d8ee7SSantosh Puranik     }
643491d8ee7SSantosh Puranik     else if (dbusSource ==
644a71dc0b7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia")
645491d8ee7SSantosh Puranik     {
646491d8ee7SSantosh Puranik         return "Cd";
647491d8ee7SSantosh Puranik     }
648491d8ee7SSantosh Puranik     else if (dbusSource ==
649491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.Network")
650491d8ee7SSantosh Puranik     {
651491d8ee7SSantosh Puranik         return "Pxe";
652491d8ee7SSantosh Puranik     }
6539f16b2c1SJennifer Lee     else if (dbusSource ==
6549f16b2c1SJennifer Lee              "xyz.openbmc_project.Control.Boot.Source.Sources.Removable")
6559f16b2c1SJennifer Lee     {
6569f16b2c1SJennifer Lee         return "Usb";
6579f16b2c1SJennifer Lee     }
658491d8ee7SSantosh Puranik     else
659491d8ee7SSantosh Puranik     {
660491d8ee7SSantosh Puranik         return "";
661491d8ee7SSantosh Puranik     }
662491d8ee7SSantosh Puranik }
663491d8ee7SSantosh Puranik 
664491d8ee7SSantosh Puranik /**
665491d8ee7SSantosh Puranik  * @brief Traslates boot mode DBUS property value to redfish.
666491d8ee7SSantosh Puranik  *
667491d8ee7SSantosh Puranik  * @param[in] dbusMode    The boot mode in DBUS speak.
668491d8ee7SSantosh Puranik  *
669491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode in Redfish terms. If translation
670491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
671491d8ee7SSantosh Puranik  */
672491d8ee7SSantosh Puranik static std::string dbusToRfBootMode(const std::string &dbusMode)
673491d8ee7SSantosh Puranik {
674491d8ee7SSantosh Puranik     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
675491d8ee7SSantosh Puranik     {
676491d8ee7SSantosh Puranik         return "None";
677491d8ee7SSantosh Puranik     }
678491d8ee7SSantosh Puranik     else if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe")
679491d8ee7SSantosh Puranik     {
680491d8ee7SSantosh Puranik         return "Diags";
681491d8ee7SSantosh Puranik     }
682491d8ee7SSantosh Puranik     else if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup")
683491d8ee7SSantosh Puranik     {
684491d8ee7SSantosh Puranik         return "BiosSetup";
685491d8ee7SSantosh Puranik     }
686491d8ee7SSantosh Puranik     else
687491d8ee7SSantosh Puranik     {
688491d8ee7SSantosh Puranik         return "";
689491d8ee7SSantosh Puranik     }
690491d8ee7SSantosh Puranik }
691491d8ee7SSantosh Puranik 
692491d8ee7SSantosh Puranik /**
693491d8ee7SSantosh Puranik  * @brief Traslates boot source from Redfish to DBUS property value.
694491d8ee7SSantosh Puranik  *
695491d8ee7SSantosh Puranik  * @param[in] rfSource    The boot source in Redfish.
696491d8ee7SSantosh Puranik  *
697491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source as expected by DBUS.
698491d8ee7SSantosh Puranik  * If translation cannot be done, returns an empty string.
699491d8ee7SSantosh Puranik  */
700491d8ee7SSantosh Puranik static std::string rfToDbusBootSource(const std::string &rfSource)
701491d8ee7SSantosh Puranik {
702491d8ee7SSantosh Puranik     if (rfSource == "None")
703491d8ee7SSantosh Puranik     {
704491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
705491d8ee7SSantosh Puranik     }
706491d8ee7SSantosh Puranik     else if (rfSource == "Hdd")
707491d8ee7SSantosh Puranik     {
708491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Source.Sources.Disk";
709491d8ee7SSantosh Puranik     }
710491d8ee7SSantosh Puranik     else if (rfSource == "Cd")
711491d8ee7SSantosh Puranik     {
712a71dc0b7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia";
713491d8ee7SSantosh Puranik     }
714491d8ee7SSantosh Puranik     else if (rfSource == "Pxe")
715491d8ee7SSantosh Puranik     {
716491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Source.Sources.Network";
717491d8ee7SSantosh Puranik     }
7189f16b2c1SJennifer Lee     else if (rfSource == "Usb")
7199f16b2c1SJennifer Lee     {
7209f16b2c1SJennifer Lee         return "xyz.openbmc_project.Control.Boot.Source.Sources.Removable";
7219f16b2c1SJennifer Lee     }
722491d8ee7SSantosh Puranik     else
723491d8ee7SSantosh Puranik     {
724491d8ee7SSantosh Puranik         return "";
725491d8ee7SSantosh Puranik     }
726491d8ee7SSantosh Puranik }
727491d8ee7SSantosh Puranik 
728491d8ee7SSantosh Puranik /**
729491d8ee7SSantosh Puranik  * @brief Traslates boot mode from Redfish to DBUS property value.
730491d8ee7SSantosh Puranik  *
731491d8ee7SSantosh Puranik  * @param[in] rfMode    The boot mode in Redfish.
732491d8ee7SSantosh Puranik  *
733491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode as expected by DBUS.
734491d8ee7SSantosh Puranik  * If translation cannot be done, returns an empty string.
735491d8ee7SSantosh Puranik  */
736491d8ee7SSantosh Puranik static std::string rfToDbusBootMode(const std::string &rfMode)
737491d8ee7SSantosh Puranik {
738491d8ee7SSantosh Puranik     if (rfMode == "None")
739491d8ee7SSantosh Puranik     {
740491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
741491d8ee7SSantosh Puranik     }
742491d8ee7SSantosh Puranik     else if (rfMode == "Diags")
743491d8ee7SSantosh Puranik     {
744491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe";
745491d8ee7SSantosh Puranik     }
746491d8ee7SSantosh Puranik     else if (rfMode == "BiosSetup")
747491d8ee7SSantosh Puranik     {
748491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup";
749491d8ee7SSantosh Puranik     }
750491d8ee7SSantosh Puranik     else
751491d8ee7SSantosh Puranik     {
752491d8ee7SSantosh Puranik         return "";
753491d8ee7SSantosh Puranik     }
754491d8ee7SSantosh Puranik }
755491d8ee7SSantosh Puranik 
756491d8ee7SSantosh Puranik /**
757491d8ee7SSantosh Puranik  * @brief Retrieves boot mode over DBUS and fills out the response
758491d8ee7SSantosh Puranik  *
759491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
760491d8ee7SSantosh Puranik  * @param[in] bootDbusObj   The dbus object to query for boot properties.
761491d8ee7SSantosh Puranik  *
762491d8ee7SSantosh Puranik  * @return None.
763491d8ee7SSantosh Puranik  */
764491d8ee7SSantosh Puranik static void getBootMode(std::shared_ptr<AsyncResp> aResp,
765491d8ee7SSantosh Puranik                         std::string bootDbusObj)
766491d8ee7SSantosh Puranik {
767491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
768491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec,
769491d8ee7SSantosh Puranik                 const std::variant<std::string> &bootMode) {
770491d8ee7SSantosh Puranik             if (ec)
771491d8ee7SSantosh Puranik             {
772491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
773491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
774491d8ee7SSantosh Puranik                 return;
775491d8ee7SSantosh Puranik             }
776491d8ee7SSantosh Puranik 
777491d8ee7SSantosh Puranik             const std::string *bootModeStr =
778491d8ee7SSantosh Puranik                 std::get_if<std::string>(&bootMode);
779491d8ee7SSantosh Puranik 
780491d8ee7SSantosh Puranik             if (!bootModeStr)
781491d8ee7SSantosh Puranik             {
782491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
783491d8ee7SSantosh Puranik                 return;
784491d8ee7SSantosh Puranik             }
785491d8ee7SSantosh Puranik 
786491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot mode: " << *bootModeStr;
787491d8ee7SSantosh Puranik 
788491d8ee7SSantosh Puranik             // TODO (Santosh): Do we need to support override mode?
789491d8ee7SSantosh Puranik             aResp->res.jsonValue["Boot"]["BootSourceOverrideMode"] = "Legacy";
790491d8ee7SSantosh Puranik             aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget@Redfish."
791491d8ee7SSantosh Puranik                                          "AllowableValues"] = {
792491d8ee7SSantosh Puranik                 "None", "Pxe", "Hdd", "Cd", "Diags", "BiosSetup"};
793491d8ee7SSantosh Puranik 
794491d8ee7SSantosh Puranik             if (*bootModeStr !=
795491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
796491d8ee7SSantosh Puranik             {
797491d8ee7SSantosh Puranik                 auto rfMode = dbusToRfBootMode(*bootModeStr);
798491d8ee7SSantosh Puranik                 if (!rfMode.empty())
799491d8ee7SSantosh Puranik                 {
800491d8ee7SSantosh Puranik                     aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
801491d8ee7SSantosh Puranik                         rfMode;
802491d8ee7SSantosh Puranik                 }
803491d8ee7SSantosh Puranik             }
804491d8ee7SSantosh Puranik 
805491d8ee7SSantosh Puranik             // If the BootSourceOverrideTarget is still "None" at the end,
806491d8ee7SSantosh Puranik             // reset the BootSourceOverrideEnabled to indicate that
807491d8ee7SSantosh Puranik             // overrides are disabled
808491d8ee7SSantosh Puranik             if (aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] ==
809491d8ee7SSantosh Puranik                 "None")
810491d8ee7SSantosh Puranik             {
811491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
812491d8ee7SSantosh Puranik                     "Disabled";
813491d8ee7SSantosh Puranik             }
814491d8ee7SSantosh Puranik         },
815491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootDbusObj,
816491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
817491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode");
818491d8ee7SSantosh Puranik }
819491d8ee7SSantosh Puranik 
820491d8ee7SSantosh Puranik /**
821491d8ee7SSantosh Puranik  * @brief Retrieves boot source over DBUS
822491d8ee7SSantosh Puranik  *
823491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
824491d8ee7SSantosh Puranik  * @param[in] oneTimeEnable Boolean to indicate boot properties are one-time.
825491d8ee7SSantosh Puranik  *
826491d8ee7SSantosh Puranik  * @return None.
827491d8ee7SSantosh Puranik  */
828491d8ee7SSantosh Puranik static void getBootSource(std::shared_ptr<AsyncResp> aResp, bool oneTimeEnabled)
829491d8ee7SSantosh Puranik {
830491d8ee7SSantosh Puranik     std::string bootDbusObj =
831491d8ee7SSantosh Puranik         oneTimeEnabled ? "/xyz/openbmc_project/control/host0/boot/one_time"
832491d8ee7SSantosh Puranik                        : "/xyz/openbmc_project/control/host0/boot";
833491d8ee7SSantosh Puranik 
834491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Is one time: " << oneTimeEnabled;
835491d8ee7SSantosh Puranik     aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
836491d8ee7SSantosh Puranik         (oneTimeEnabled) ? "Once" : "Continuous";
837491d8ee7SSantosh Puranik 
838491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
839491d8ee7SSantosh Puranik         [aResp, bootDbusObj](const boost::system::error_code ec,
840491d8ee7SSantosh Puranik                              const std::variant<std::string> &bootSource) {
841491d8ee7SSantosh Puranik             if (ec)
842491d8ee7SSantosh Puranik             {
843491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
844491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
845491d8ee7SSantosh Puranik                 return;
846491d8ee7SSantosh Puranik             }
847491d8ee7SSantosh Puranik 
848491d8ee7SSantosh Puranik             const std::string *bootSourceStr =
849491d8ee7SSantosh Puranik                 std::get_if<std::string>(&bootSource);
850491d8ee7SSantosh Puranik 
851491d8ee7SSantosh Puranik             if (!bootSourceStr)
852491d8ee7SSantosh Puranik             {
853491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
854491d8ee7SSantosh Puranik                 return;
855491d8ee7SSantosh Puranik             }
856491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot source: " << *bootSourceStr;
857491d8ee7SSantosh Puranik 
858491d8ee7SSantosh Puranik             auto rfSource = dbusToRfBootSource(*bootSourceStr);
859491d8ee7SSantosh Puranik             if (!rfSource.empty())
860491d8ee7SSantosh Puranik             {
861491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
862491d8ee7SSantosh Puranik                     rfSource;
863491d8ee7SSantosh Puranik             }
864491d8ee7SSantosh Puranik         },
865491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootDbusObj,
866491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
867491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource");
868491d8ee7SSantosh Puranik     getBootMode(std::move(aResp), std::move(bootDbusObj));
869491d8ee7SSantosh Puranik }
870491d8ee7SSantosh Puranik 
871491d8ee7SSantosh Puranik /**
872491d8ee7SSantosh Puranik  * @brief Retrieves "One time" enabled setting over DBUS and calls function to
873491d8ee7SSantosh Puranik  * get boot source and boot mode.
874491d8ee7SSantosh Puranik  *
875491d8ee7SSantosh Puranik  * @param[in] aResp     Shared pointer for generating response message.
876491d8ee7SSantosh Puranik  *
877491d8ee7SSantosh Puranik  * @return None.
878491d8ee7SSantosh Puranik  */
879491d8ee7SSantosh Puranik static void getBootProperties(std::shared_ptr<AsyncResp> aResp)
880491d8ee7SSantosh Puranik {
881491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Get boot information.";
882491d8ee7SSantosh Puranik 
883491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
884c5d03ff4SJennifer Lee         [aResp](const boost::system::error_code ec,
885491d8ee7SSantosh Puranik                 const sdbusplus::message::variant<bool> &oneTime) {
886491d8ee7SSantosh Puranik             if (ec)
887491d8ee7SSantosh Puranik             {
888491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
889491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
890491d8ee7SSantosh Puranik                 return;
891491d8ee7SSantosh Puranik             }
892491d8ee7SSantosh Puranik 
893491d8ee7SSantosh Puranik             const bool *oneTimePtr = std::get_if<bool>(&oneTime);
894491d8ee7SSantosh Puranik 
895491d8ee7SSantosh Puranik             if (!oneTimePtr)
896491d8ee7SSantosh Puranik             {
897491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
898491d8ee7SSantosh Puranik                 return;
899491d8ee7SSantosh Puranik             }
900491d8ee7SSantosh Puranik             getBootSource(aResp, *oneTimePtr);
901491d8ee7SSantosh Puranik         },
902491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
903491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
904491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
905491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled");
906491d8ee7SSantosh Puranik }
907491d8ee7SSantosh Puranik 
908491d8ee7SSantosh Puranik /**
909491d8ee7SSantosh Puranik  * @brief Sets boot properties into DBUS object(s).
910491d8ee7SSantosh Puranik  *
911491d8ee7SSantosh Puranik  * @param[in] aResp           Shared pointer for generating response message.
912491d8ee7SSantosh Puranik  * @param[in] oneTimeEnabled  Is "one-time" setting already enabled.
913491d8ee7SSantosh Puranik  * @param[in] bootSource      The boot source to set.
914491d8ee7SSantosh Puranik  * @param[in] bootEnable      The source override "enable" to set.
915491d8ee7SSantosh Puranik  *
916491d8ee7SSantosh Puranik  * @return None.
917491d8ee7SSantosh Puranik  */
918491d8ee7SSantosh Puranik static void setBootModeOrSource(std::shared_ptr<AsyncResp> aResp,
919491d8ee7SSantosh Puranik                                 bool oneTimeEnabled,
920491d8ee7SSantosh Puranik                                 std::optional<std::string> bootSource,
921491d8ee7SSantosh Puranik                                 std::optional<std::string> bootEnable)
922491d8ee7SSantosh Puranik {
923491d8ee7SSantosh Puranik     if (bootEnable && (bootEnable != "Once") && (bootEnable != "Continuous") &&
924491d8ee7SSantosh Puranik         (bootEnable != "Disabled"))
925491d8ee7SSantosh Puranik     {
926491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Unsupported value for BootSourceOverrideEnabled: "
927491d8ee7SSantosh Puranik                          << *bootEnable;
928491d8ee7SSantosh Puranik         messages::propertyValueNotInList(aResp->res, *bootEnable,
929491d8ee7SSantosh Puranik                                          "BootSourceOverrideEnabled");
930491d8ee7SSantosh Puranik         return;
931491d8ee7SSantosh Puranik     }
932491d8ee7SSantosh Puranik 
933491d8ee7SSantosh Puranik     bool oneTimeSetting = oneTimeEnabled;
934491d8ee7SSantosh Puranik     // Validate incoming parameters
935491d8ee7SSantosh Puranik     if (bootEnable)
936491d8ee7SSantosh Puranik     {
937491d8ee7SSantosh Puranik         if (*bootEnable == "Once")
938491d8ee7SSantosh Puranik         {
939491d8ee7SSantosh Puranik             oneTimeSetting = true;
940491d8ee7SSantosh Puranik         }
941491d8ee7SSantosh Puranik         else if (*bootEnable == "Continuous")
942491d8ee7SSantosh Puranik         {
943491d8ee7SSantosh Puranik             oneTimeSetting = false;
944491d8ee7SSantosh Puranik         }
945491d8ee7SSantosh Puranik         else if (*bootEnable == "Disabled")
946491d8ee7SSantosh Puranik         {
947491d8ee7SSantosh Puranik             oneTimeSetting = false;
948491d8ee7SSantosh Puranik         }
949491d8ee7SSantosh Puranik         else
950491d8ee7SSantosh Puranik         {
951491d8ee7SSantosh Puranik 
952491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Unsupported value for "
953491d8ee7SSantosh Puranik                                 "BootSourceOverrideEnabled: "
954491d8ee7SSantosh Puranik                              << *bootEnable;
955491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootEnable,
956491d8ee7SSantosh Puranik                                              "BootSourceOverrideEnabled");
957491d8ee7SSantosh Puranik             return;
958491d8ee7SSantosh Puranik         }
959491d8ee7SSantosh Puranik     }
960491d8ee7SSantosh Puranik     std::string bootSourceStr;
961491d8ee7SSantosh Puranik     std::string bootModeStr;
962491d8ee7SSantosh Puranik     if (bootSource)
963491d8ee7SSantosh Puranik     {
964491d8ee7SSantosh Puranik         bootSourceStr = rfToDbusBootSource(*bootSource);
965491d8ee7SSantosh Puranik         bootModeStr = rfToDbusBootMode(*bootSource);
966491d8ee7SSantosh Puranik 
967491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
968491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
969491d8ee7SSantosh Puranik 
970491d8ee7SSantosh Puranik         if (bootSourceStr.empty() && bootModeStr.empty())
971491d8ee7SSantosh Puranik         {
972491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Invalid property value for "
973491d8ee7SSantosh Puranik                                 "BootSourceOverrideTarget: "
974491d8ee7SSantosh Puranik                              << *bootSource;
975491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootSource,
976491d8ee7SSantosh Puranik                                              "BootSourceTargetOverride");
977491d8ee7SSantosh Puranik             return;
978491d8ee7SSantosh Puranik         }
979491d8ee7SSantosh Puranik     }
980491d8ee7SSantosh Puranik     const char *bootObj =
981491d8ee7SSantosh Puranik         oneTimeSetting ? "/xyz/openbmc_project/control/host0/boot/one_time"
982491d8ee7SSantosh Puranik                        : "/xyz/openbmc_project/control/host0/boot";
983491d8ee7SSantosh Puranik     // Figure out what properties to set
984491d8ee7SSantosh Puranik     if (bootEnable && (*bootEnable == "Disabled"))
985491d8ee7SSantosh Puranik     {
986491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot source override will be disabled";
987491d8ee7SSantosh Puranik         // Request to only turn OFF/ON enabled, if turning enabled OFF, need
988491d8ee7SSantosh Puranik         // to reset the source and mode too. If turning it ON, we only need
989491d8ee7SSantosh Puranik         // to set the enabled property
990491d8ee7SSantosh Puranik         bootSourceStr =
991491d8ee7SSantosh Puranik             "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
992491d8ee7SSantosh Puranik         bootModeStr = "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
993491d8ee7SSantosh Puranik     }
994491d8ee7SSantosh Puranik     else if (bootSource)
995491d8ee7SSantosh Puranik     {
996491d8ee7SSantosh Puranik         // Source target specified
997491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot source: " << *bootSource;
998491d8ee7SSantosh Puranik         // Figure out which DBUS interface and property to use
999491d8ee7SSantosh Puranik         bootSourceStr = rfToDbusBootSource(*bootSource);
1000491d8ee7SSantosh Puranik         bootModeStr = rfToDbusBootMode(*bootSource);
1001491d8ee7SSantosh Puranik 
1002491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
1003491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
1004491d8ee7SSantosh Puranik 
1005491d8ee7SSantosh Puranik         if (bootSourceStr.empty() && bootModeStr.empty())
1006491d8ee7SSantosh Puranik         {
1007491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Invalid property value for "
1008491d8ee7SSantosh Puranik                                 "BootSourceOverrideTarget: "
1009491d8ee7SSantosh Puranik                              << *bootSource;
1010491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootSource,
1011491d8ee7SSantosh Puranik                                              "BootSourceTargetOverride");
1012491d8ee7SSantosh Puranik             return;
1013491d8ee7SSantosh Puranik         }
1014491d8ee7SSantosh Puranik 
1015491d8ee7SSantosh Puranik         if (!bootSourceStr.empty())
1016491d8ee7SSantosh Puranik         {
1017491d8ee7SSantosh Puranik             // If setting to anything other than default, also reset boot
1018491d8ee7SSantosh Puranik             // mode property
1019491d8ee7SSantosh Puranik             if (bootSourceStr !=
1020491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
1021491d8ee7SSantosh Puranik             {
1022491d8ee7SSantosh Puranik                 bootModeStr =
1023491d8ee7SSantosh Puranik                     "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
1024491d8ee7SSantosh Puranik             }
1025491d8ee7SSantosh Puranik         }
1026491d8ee7SSantosh Puranik         else // if (!bootModeStr.empty())
1027491d8ee7SSantosh Puranik         {
1028491d8ee7SSantosh Puranik             // If setting to anything other than default, also reset boot
1029491d8ee7SSantosh Puranik             // source property
1030491d8ee7SSantosh Puranik             if (bootModeStr !=
1031491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
1032491d8ee7SSantosh Puranik             {
1033491d8ee7SSantosh Puranik                 bootSourceStr =
1034491d8ee7SSantosh Puranik                     "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
1035491d8ee7SSantosh Puranik             }
1036491d8ee7SSantosh Puranik         }
1037491d8ee7SSantosh Puranik     }
1038491d8ee7SSantosh Puranik     if (!bootSourceStr.empty())
1039491d8ee7SSantosh Puranik     {
1040491d8ee7SSantosh Puranik         crow::connections::systemBus->async_method_call(
1041491d8ee7SSantosh Puranik             [aResp](const boost::system::error_code ec) {
1042491d8ee7SSantosh Puranik                 if (ec)
1043491d8ee7SSantosh Puranik                 {
1044491d8ee7SSantosh Puranik                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1045491d8ee7SSantosh Puranik                     messages::internalError(aResp->res);
1046491d8ee7SSantosh Puranik                     return;
1047491d8ee7SSantosh Puranik                 }
1048491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "Boot source update done.";
1049491d8ee7SSantosh Puranik             },
1050491d8ee7SSantosh Puranik             "xyz.openbmc_project.Settings", bootObj,
1051491d8ee7SSantosh Puranik             "org.freedesktop.DBus.Properties", "Set",
1052491d8ee7SSantosh Puranik             "xyz.openbmc_project.Control.Boot.Source", "BootSource",
1053491d8ee7SSantosh Puranik             std::variant<std::string>(bootSourceStr));
1054491d8ee7SSantosh Puranik     }
1055491d8ee7SSantosh Puranik     if (!bootModeStr.empty())
1056491d8ee7SSantosh Puranik     {
1057491d8ee7SSantosh Puranik         crow::connections::systemBus->async_method_call(
1058491d8ee7SSantosh Puranik             [aResp](const boost::system::error_code ec) {
1059491d8ee7SSantosh Puranik                 if (ec)
1060491d8ee7SSantosh Puranik                 {
1061491d8ee7SSantosh Puranik                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1062491d8ee7SSantosh Puranik                     messages::internalError(aResp->res);
1063491d8ee7SSantosh Puranik                     return;
1064491d8ee7SSantosh Puranik                 }
1065491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "Boot mode update done.";
1066491d8ee7SSantosh Puranik             },
1067491d8ee7SSantosh Puranik             "xyz.openbmc_project.Settings", bootObj,
1068491d8ee7SSantosh Puranik             "org.freedesktop.DBus.Properties", "Set",
1069491d8ee7SSantosh Puranik             "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
1070491d8ee7SSantosh Puranik             std::variant<std::string>(bootModeStr));
1071491d8ee7SSantosh Puranik     }
1072491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1073491d8ee7SSantosh Puranik         [aResp{std::move(aResp)}](const boost::system::error_code ec) {
1074491d8ee7SSantosh Puranik             if (ec)
1075491d8ee7SSantosh Puranik             {
1076491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1077491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1078491d8ee7SSantosh Puranik                 return;
1079491d8ee7SSantosh Puranik             }
1080491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot enable update done.";
1081491d8ee7SSantosh Puranik         },
1082491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
1083491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
1084491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1085491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled",
1086491d8ee7SSantosh Puranik         std::variant<bool>(oneTimeSetting));
1087491d8ee7SSantosh Puranik }
1088491d8ee7SSantosh Puranik 
1089491d8ee7SSantosh Puranik /**
1090491d8ee7SSantosh Puranik  * @brief Retrieves "One time" enabled setting over DBUS and calls function to
1091491d8ee7SSantosh Puranik  * set boot source/boot mode properties.
1092491d8ee7SSantosh Puranik  *
1093491d8ee7SSantosh Puranik  * @param[in] aResp      Shared pointer for generating response message.
1094491d8ee7SSantosh Puranik  * @param[in] bootSource The boot source from incoming RF request.
1095491d8ee7SSantosh Puranik  * @param[in] bootEnable The boot override enable from incoming RF request.
1096491d8ee7SSantosh Puranik  *
1097491d8ee7SSantosh Puranik  * @return None.
1098491d8ee7SSantosh Puranik  */
1099491d8ee7SSantosh Puranik static void setBootProperties(std::shared_ptr<AsyncResp> aResp,
1100491d8ee7SSantosh Puranik                               std::optional<std::string> bootSource,
1101491d8ee7SSantosh Puranik                               std::optional<std::string> bootEnable)
1102491d8ee7SSantosh Puranik {
1103491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set boot information.";
1104491d8ee7SSantosh Puranik 
1105491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
1106491d8ee7SSantosh Puranik         [aResp{std::move(aResp)}, bootSource{std::move(bootSource)},
1107491d8ee7SSantosh Puranik          bootEnable{std::move(bootEnable)}](
1108491d8ee7SSantosh Puranik             const boost::system::error_code ec,
1109491d8ee7SSantosh Puranik             const sdbusplus::message::variant<bool> &oneTime) {
1110491d8ee7SSantosh Puranik             if (ec)
1111491d8ee7SSantosh Puranik             {
1112491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1113491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1114491d8ee7SSantosh Puranik                 return;
1115491d8ee7SSantosh Puranik             }
1116491d8ee7SSantosh Puranik 
1117491d8ee7SSantosh Puranik             const bool *oneTimePtr = std::get_if<bool>(&oneTime);
1118491d8ee7SSantosh Puranik 
1119491d8ee7SSantosh Puranik             if (!oneTimePtr)
1120491d8ee7SSantosh Puranik             {
1121491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
1122491d8ee7SSantosh Puranik                 return;
1123491d8ee7SSantosh Puranik             }
1124491d8ee7SSantosh Puranik 
1125491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Got one time: " << *oneTimePtr;
1126491d8ee7SSantosh Puranik 
1127491d8ee7SSantosh Puranik             setBootModeOrSource(aResp, *oneTimePtr, std::move(bootSource),
1128491d8ee7SSantosh Puranik                                 std::move(bootEnable));
1129491d8ee7SSantosh Puranik         },
1130491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
1131491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
1132491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
1133491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled");
1134491d8ee7SSantosh Puranik }
1135491d8ee7SSantosh Puranik 
1136491d8ee7SSantosh Puranik /**
1137c5b2abe0SLewanczyk, Dawid  * SystemsCollection derived class for delivering ComputerSystems Collection
1138c5b2abe0SLewanczyk, Dawid  * Schema
1139c5b2abe0SLewanczyk, Dawid  */
11401abe55efSEd Tanous class SystemsCollection : public Node
11411abe55efSEd Tanous {
1142c5b2abe0SLewanczyk, Dawid   public:
11431abe55efSEd Tanous     SystemsCollection(CrowApp &app) : Node(app, "/redfish/v1/Systems/")
11441abe55efSEd Tanous     {
1145c5b2abe0SLewanczyk, Dawid         entityPrivileges = {
1146c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::get, {{"Login"}}},
1147c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::head, {{"Login"}}},
1148c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1149c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1150c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1151c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1152c5b2abe0SLewanczyk, Dawid     }
1153c5b2abe0SLewanczyk, Dawid 
1154c5b2abe0SLewanczyk, Dawid   private:
115555c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
11561abe55efSEd Tanous                const std::vector<std::string> &params) override
11571abe55efSEd Tanous     {
11580f74e643SEd Tanous         res.jsonValue["@odata.type"] =
11590f74e643SEd Tanous             "#ComputerSystemCollection.ComputerSystemCollection";
11600f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Systems";
11610f74e643SEd Tanous         res.jsonValue["@odata.context"] =
11620f74e643SEd Tanous             "/redfish/v1/"
11630f74e643SEd Tanous             "$metadata#ComputerSystemCollection.ComputerSystemCollection";
11640f74e643SEd Tanous         res.jsonValue["Name"] = "Computer System Collection";
1165029573d4SEd Tanous         res.jsonValue["Members"] = {
1166029573d4SEd Tanous             {{"@odata.id", "/redfish/v1/Systems/system"}}};
1167029573d4SEd Tanous         res.jsonValue["Members@odata.count"] = 1;
1168029573d4SEd Tanous         res.end();
1169c5b2abe0SLewanczyk, Dawid     }
1170c5b2abe0SLewanczyk, Dawid };
1171c5b2abe0SLewanczyk, Dawid 
1172c5b2abe0SLewanczyk, Dawid /**
1173cc340dd9SEd Tanous  * SystemActionsReset class supports handle POST method for Reset action.
1174cc340dd9SEd Tanous  * The class retrieves and sends data directly to D-Bus.
1175cc340dd9SEd Tanous  */
1176cc340dd9SEd Tanous class SystemActionsReset : public Node
1177cc340dd9SEd Tanous {
1178cc340dd9SEd Tanous   public:
1179cc340dd9SEd Tanous     SystemActionsReset(CrowApp &app) :
1180029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset/")
1181cc340dd9SEd Tanous     {
1182cc340dd9SEd Tanous         entityPrivileges = {
1183cc340dd9SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1184cc340dd9SEd Tanous     }
1185cc340dd9SEd Tanous 
1186cc340dd9SEd Tanous   private:
1187cc340dd9SEd Tanous     /**
1188cc340dd9SEd Tanous      * Function handles POST method request.
1189cc340dd9SEd Tanous      * Analyzes POST body message before sends Reset request data to D-Bus.
1190cc340dd9SEd Tanous      */
1191cc340dd9SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
1192cc340dd9SEd Tanous                 const std::vector<std::string> &params) override
1193cc340dd9SEd Tanous     {
1194cc340dd9SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1195cc340dd9SEd Tanous 
11969712f8acSEd Tanous         std::string resetType;
11979712f8acSEd Tanous         if (!json_util::readJson(req, res, "ResetType", resetType))
1198cc340dd9SEd Tanous         {
1199cc340dd9SEd Tanous             return;
1200cc340dd9SEd Tanous         }
1201cc340dd9SEd Tanous 
1202d22c8396SJason M. Bills         // Get the command and host vs. chassis
1203cc340dd9SEd Tanous         std::string command;
1204d22c8396SJason M. Bills         bool hostCommand;
12059712f8acSEd Tanous         if (resetType == "On")
1206cc340dd9SEd Tanous         {
1207cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.On";
1208d22c8396SJason M. Bills             hostCommand = true;
1209d22c8396SJason M. Bills         }
1210d22c8396SJason M. Bills         else if (resetType == "ForceOff")
1211d22c8396SJason M. Bills         {
1212d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Chassis.Transition.Off";
1213d22c8396SJason M. Bills             hostCommand = false;
1214d22c8396SJason M. Bills         }
1215d22c8396SJason M. Bills         else if (resetType == "ForceOn")
1216d22c8396SJason M. Bills         {
1217d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Host.Transition.On";
1218d22c8396SJason M. Bills             hostCommand = true;
1219d22c8396SJason M. Bills         }
1220d22c8396SJason M. Bills         else if (resetType == "ForceRestart")
1221d22c8396SJason M. Bills         {
1222d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Chassis.Transition.Reset";
1223d22c8396SJason M. Bills             hostCommand = false;
1224cc340dd9SEd Tanous         }
12259712f8acSEd Tanous         else if (resetType == "GracefulShutdown")
1226cc340dd9SEd Tanous         {
1227cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.Off";
1228d22c8396SJason M. Bills             hostCommand = true;
1229cc340dd9SEd Tanous         }
12309712f8acSEd Tanous         else if (resetType == "GracefulRestart")
1231cc340dd9SEd Tanous         {
12329712f8acSEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.Reboot";
1233d22c8396SJason M. Bills             hostCommand = true;
1234d22c8396SJason M. Bills         }
1235d22c8396SJason M. Bills         else if (resetType == "PowerCycle")
1236d22c8396SJason M. Bills         {
1237d22c8396SJason M. Bills             command = "xyz.openbmc_project.State.Chassis.Transition.PowerCycle";
1238d22c8396SJason M. Bills             hostCommand = false;
1239cc340dd9SEd Tanous         }
1240bfd5b826SLakshminarayana R. Kammath         else if (resetType == "Nmi")
1241bfd5b826SLakshminarayana R. Kammath         {
1242bfd5b826SLakshminarayana R. Kammath             doNMI(asyncResp);
1243bfd5b826SLakshminarayana R. Kammath             return;
1244bfd5b826SLakshminarayana R. Kammath         }
1245cc340dd9SEd Tanous         else
1246cc340dd9SEd Tanous         {
1247f12894f8SJason M. Bills             messages::actionParameterUnknown(res, "Reset", resetType);
1248cc340dd9SEd Tanous             return;
1249cc340dd9SEd Tanous         }
1250cc340dd9SEd Tanous 
1251d22c8396SJason M. Bills         if (hostCommand)
1252d22c8396SJason M. Bills         {
1253cc340dd9SEd Tanous             crow::connections::systemBus->async_method_call(
1254d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
1255cc340dd9SEd Tanous                     if (ec)
1256cc340dd9SEd Tanous                     {
1257cc340dd9SEd Tanous                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1258d22c8396SJason M. Bills                         if (ec.value() == boost::asio::error::invalid_argument)
1259d22c8396SJason M. Bills                         {
1260d22c8396SJason M. Bills                             messages::actionParameterNotSupported(
1261d22c8396SJason M. Bills                                 asyncResp->res, resetType, "Reset");
1262d22c8396SJason M. Bills                         }
1263d22c8396SJason M. Bills                         else
1264d22c8396SJason M. Bills                         {
1265f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
1266d22c8396SJason M. Bills                         }
1267cc340dd9SEd Tanous                         return;
1268cc340dd9SEd Tanous                     }
1269f12894f8SJason M. Bills                     messages::success(asyncResp->res);
1270cc340dd9SEd Tanous                 },
1271cc340dd9SEd Tanous                 "xyz.openbmc_project.State.Host",
1272cc340dd9SEd Tanous                 "/xyz/openbmc_project/state/host0",
1273cc340dd9SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
12749712f8acSEd Tanous                 "xyz.openbmc_project.State.Host", "RequestedHostTransition",
1275abf2add6SEd Tanous                 std::variant<std::string>{command});
1276cc340dd9SEd Tanous         }
1277d22c8396SJason M. Bills         else
1278d22c8396SJason M. Bills         {
1279d22c8396SJason M. Bills             crow::connections::systemBus->async_method_call(
1280d22c8396SJason M. Bills                 [asyncResp, resetType](const boost::system::error_code ec) {
1281d22c8396SJason M. Bills                     if (ec)
1282d22c8396SJason M. Bills                     {
1283d22c8396SJason M. Bills                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1284d22c8396SJason M. Bills                         if (ec.value() == boost::asio::error::invalid_argument)
1285d22c8396SJason M. Bills                         {
1286d22c8396SJason M. Bills                             messages::actionParameterNotSupported(
1287d22c8396SJason M. Bills                                 asyncResp->res, resetType, "Reset");
1288d22c8396SJason M. Bills                         }
1289d22c8396SJason M. Bills                         else
1290d22c8396SJason M. Bills                         {
1291d22c8396SJason M. Bills                             messages::internalError(asyncResp->res);
1292d22c8396SJason M. Bills                         }
1293d22c8396SJason M. Bills                         return;
1294d22c8396SJason M. Bills                     }
1295d22c8396SJason M. Bills                     messages::success(asyncResp->res);
1296d22c8396SJason M. Bills                 },
1297d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis",
1298d22c8396SJason M. Bills                 "/xyz/openbmc_project/state/chassis0",
1299d22c8396SJason M. Bills                 "org.freedesktop.DBus.Properties", "Set",
1300d22c8396SJason M. Bills                 "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition",
1301d22c8396SJason M. Bills                 std::variant<std::string>{command});
1302d22c8396SJason M. Bills         }
1303d22c8396SJason M. Bills     }
1304bfd5b826SLakshminarayana R. Kammath     /**
1305bfd5b826SLakshminarayana R. Kammath      * Function transceives data with dbus directly.
1306bfd5b826SLakshminarayana R. Kammath      */
1307bfd5b826SLakshminarayana R. Kammath     void doNMI(const std::shared_ptr<AsyncResp> &asyncResp)
1308bfd5b826SLakshminarayana R. Kammath     {
1309bfd5b826SLakshminarayana R. Kammath         constexpr char const *serviceName =
1310bfd5b826SLakshminarayana R. Kammath             "xyz.openbmc_project.Control.Host.NMI";
1311bfd5b826SLakshminarayana R. Kammath         constexpr char const *objectPath =
1312bfd5b826SLakshminarayana R. Kammath             "/xyz/openbmc_project/control/host0/nmi";
1313bfd5b826SLakshminarayana R. Kammath         constexpr char const *interfaceName =
1314bfd5b826SLakshminarayana R. Kammath             "xyz.openbmc_project.Control.Host.NMI";
1315bfd5b826SLakshminarayana R. Kammath         constexpr char const *method = "NMI";
1316bfd5b826SLakshminarayana R. Kammath 
1317bfd5b826SLakshminarayana R. Kammath         crow::connections::systemBus->async_method_call(
1318bfd5b826SLakshminarayana R. Kammath             [asyncResp](const boost::system::error_code ec) {
1319bfd5b826SLakshminarayana R. Kammath                 if (ec)
1320bfd5b826SLakshminarayana R. Kammath                 {
1321bfd5b826SLakshminarayana R. Kammath                     BMCWEB_LOG_ERROR << " Bad D-Bus request error: " << ec;
1322bfd5b826SLakshminarayana R. Kammath                     messages::internalError(asyncResp->res);
1323bfd5b826SLakshminarayana R. Kammath                     return;
1324bfd5b826SLakshminarayana R. Kammath                 }
1325bfd5b826SLakshminarayana R. Kammath                 messages::success(asyncResp->res);
1326bfd5b826SLakshminarayana R. Kammath             },
1327bfd5b826SLakshminarayana R. Kammath             serviceName, objectPath, interfaceName, method);
1328bfd5b826SLakshminarayana R. Kammath     }
1329cc340dd9SEd Tanous };
1330cc340dd9SEd Tanous 
1331cc340dd9SEd Tanous /**
13326617338dSEd Tanous  * Systems derived class for delivering Computer Systems Schema.
1333c5b2abe0SLewanczyk, Dawid  */
13341abe55efSEd Tanous class Systems : public Node
13351abe55efSEd Tanous {
1336c5b2abe0SLewanczyk, Dawid   public:
1337c5b2abe0SLewanczyk, Dawid     /*
1338c5b2abe0SLewanczyk, Dawid      * Default Constructor
1339c5b2abe0SLewanczyk, Dawid      */
1340029573d4SEd Tanous     Systems(CrowApp &app) : Node(app, "/redfish/v1/Systems/system/")
13411abe55efSEd Tanous     {
1342c5b2abe0SLewanczyk, Dawid         entityPrivileges = {
1343c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::get, {{"Login"}}},
1344c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::head, {{"Login"}}},
1345c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1346c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1347c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1348c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1349c5b2abe0SLewanczyk, Dawid     }
1350c5b2abe0SLewanczyk, Dawid 
1351c5b2abe0SLewanczyk, Dawid   private:
1352c5b2abe0SLewanczyk, Dawid     /**
1353c5b2abe0SLewanczyk, Dawid      * Functions triggers appropriate requests on DBus
1354c5b2abe0SLewanczyk, Dawid      */
135555c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
13561abe55efSEd Tanous                const std::vector<std::string> &params) override
13571abe55efSEd Tanous     {
1358491d8ee7SSantosh Puranik         res.jsonValue["@odata.type"] = "#ComputerSystem.v1_6_0.ComputerSystem";
13590f74e643SEd Tanous         res.jsonValue["@odata.context"] =
13600f74e643SEd Tanous             "/redfish/v1/$metadata#ComputerSystem.ComputerSystem";
1361029573d4SEd Tanous         res.jsonValue["Name"] = "Computer System";
1362029573d4SEd Tanous         res.jsonValue["Id"] = "system";
13630f74e643SEd Tanous         res.jsonValue["SystemType"] = "Physical";
13640f74e643SEd Tanous         res.jsonValue["Description"] = "Computer System";
13650f74e643SEd Tanous         res.jsonValue["ProcessorSummary"]["Count"] = 0;
13660f74e643SEd Tanous         res.jsonValue["ProcessorSummary"]["Status"]["State"] = "Disabled";
13670f74e643SEd Tanous         res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] = int(0);
13680f74e643SEd Tanous         res.jsonValue["MemorySummary"]["Status"]["State"] = "Disabled";
1369029573d4SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Systems/system";
137004a258f4SEd Tanous 
1371443c2934SRapkiewicz, Pawel         res.jsonValue["Processors"] = {
1372029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/Processors"}};
1373443c2934SRapkiewicz, Pawel         res.jsonValue["Memory"] = {
1374029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/Memory"}};
1375029573d4SEd Tanous 
1376cc340dd9SEd Tanous         // TODO Need to support ForceRestart.
1377cc340dd9SEd Tanous         res.jsonValue["Actions"]["#ComputerSystem.Reset"] = {
1378cc340dd9SEd Tanous             {"target",
1379029573d4SEd Tanous              "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset"},
1380cc340dd9SEd Tanous             {"ResetType@Redfish.AllowableValues",
1381d22c8396SJason M. Bills              {"On", "ForceOff", "ForceOn", "ForceRestart", "GracefulRestart",
1382bfd5b826SLakshminarayana R. Kammath               "GracefulShutdown", "PowerCycle", "Nmi"}}};
1383c5b2abe0SLewanczyk, Dawid 
1384c4bf6374SJason M. Bills         res.jsonValue["LogServices"] = {
1385029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/LogServices"}};
1386c4bf6374SJason M. Bills 
1387c5d03ff4SJennifer Lee         res.jsonValue["Links"]["ManagedBy"] = {
1388c5d03ff4SJennifer Lee             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
1389c5d03ff4SJennifer Lee 
1390c5d03ff4SJennifer Lee         res.jsonValue["Status"] = {
1391c5d03ff4SJennifer Lee             {"Health", "OK"},
1392c5d03ff4SJennifer Lee             {"State", "Enabled"},
1393c5d03ff4SJennifer Lee         };
1394a0803efaSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1395c5b2abe0SLewanczyk, Dawid 
1396b49ac873SJames Feist         constexpr const std::array<const char *, 2> inventoryForSystems = {
1397b49ac873SJames Feist             "xyz.openbmc_project.Inventory.Item.Dimm",
1398b49ac873SJames Feist             "xyz.openbmc_project.Inventory.Item.Cpu"};
1399b49ac873SJames Feist 
1400b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
1401b49ac873SJames Feist         crow::connections::systemBus->async_method_call(
1402b49ac873SJames Feist             [health](const boost::system::error_code ec,
1403b49ac873SJames Feist                      std::vector<std::string> &resp) {
1404b49ac873SJames Feist                 if (ec)
1405b49ac873SJames Feist                 {
1406b49ac873SJames Feist                     // no inventory
1407b49ac873SJames Feist                     return;
1408b49ac873SJames Feist                 }
1409b49ac873SJames Feist 
1410b49ac873SJames Feist                 health->inventory = std::move(resp);
1411b49ac873SJames Feist             },
1412b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper",
1413b49ac873SJames Feist             "/xyz/openbmc_project/object_mapper",
1414b49ac873SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1415b49ac873SJames Feist             int32_t(0), inventoryForSystems);
1416b49ac873SJames Feist 
1417b49ac873SJames Feist         health->populate();
1418b49ac873SJames Feist 
1419c5d03ff4SJennifer Lee         getMainChassisId(asyncResp, [](const std::string &chassisId,
1420c5d03ff4SJennifer Lee                                        std::shared_ptr<AsyncResp> aRsp) {
1421c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["Chassis"] = {
1422c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
1423c5d03ff4SJennifer Lee         });
14246c34de48SEd Tanous         getLedGroupIdentify(
1425a0803efaSEd Tanous             asyncResp,
1426c5d03ff4SJennifer Lee             [](const bool &asserted, const std::shared_ptr<AsyncResp> aRsp) {
14271abe55efSEd Tanous                 if (asserted)
14281abe55efSEd Tanous                 {
1429c5b2abe0SLewanczyk, Dawid                     // If led group is asserted, then another call is needed to
1430c5b2abe0SLewanczyk, Dawid                     // get led status
14316c34de48SEd Tanous                     getLedIdentify(
1432c5d03ff4SJennifer Lee                         aRsp, [](const std::string &ledStatus,
1433c5d03ff4SJennifer Lee                                  const std::shared_ptr<AsyncResp> aRsp) {
14341abe55efSEd Tanous                             if (!ledStatus.empty())
14351abe55efSEd Tanous                             {
1436c5d03ff4SJennifer Lee                                 aRsp->res.jsonValue["IndicatorLED"] = ledStatus;
1437c5b2abe0SLewanczyk, Dawid                             }
1438c5b2abe0SLewanczyk, Dawid                         });
14391abe55efSEd Tanous                 }
14401abe55efSEd Tanous                 else
14411abe55efSEd Tanous                 {
1442c5d03ff4SJennifer Lee                     aRsp->res.jsonValue["IndicatorLED"] = "Off";
1443c5b2abe0SLewanczyk, Dawid                 }
1444c5b2abe0SLewanczyk, Dawid             });
1445029573d4SEd Tanous         getComputerSystem(asyncResp);
14466c34de48SEd Tanous         getHostState(asyncResp);
1447491d8ee7SSantosh Puranik         getBootProperties(asyncResp);
1448f5c9f8bdSJason M. Bills         getPCIeDeviceList(asyncResp);
1449c5b2abe0SLewanczyk, Dawid     }
1450c5b2abe0SLewanczyk, Dawid 
145155c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
14521abe55efSEd Tanous                  const std::vector<std::string> &params) override
14531abe55efSEd Tanous     {
1454cde19e5fSSantosh Puranik         std::optional<std::string> indicatorLed;
1455491d8ee7SSantosh Puranik         std::optional<nlohmann::json> bootProps;
1456491d8ee7SSantosh Puranik         if (!json_util::readJson(req, res, "IndicatorLED", indicatorLed, "Boot",
1457491d8ee7SSantosh Puranik                                  bootProps))
14586617338dSEd Tanous         {
14596617338dSEd Tanous             return;
14606617338dSEd Tanous         }
1461491d8ee7SSantosh Puranik 
1462029573d4SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1463d573bb2aSJennifer Lee         asyncResp->res.result(boost::beast::http::status::no_content);
1464491d8ee7SSantosh Puranik 
1465491d8ee7SSantosh Puranik         if (bootProps)
1466491d8ee7SSantosh Puranik         {
1467491d8ee7SSantosh Puranik             std::optional<std::string> bootSource;
1468491d8ee7SSantosh Puranik             std::optional<std::string> bootEnable;
1469491d8ee7SSantosh Puranik 
1470491d8ee7SSantosh Puranik             if (!json_util::readJson(*bootProps, asyncResp->res,
1471491d8ee7SSantosh Puranik                                      "BootSourceOverrideTarget", bootSource,
1472491d8ee7SSantosh Puranik                                      "BootSourceOverrideEnabled", bootEnable))
1473491d8ee7SSantosh Puranik             {
1474491d8ee7SSantosh Puranik                 return;
1475491d8ee7SSantosh Puranik             }
1476491d8ee7SSantosh Puranik             setBootProperties(asyncResp, std::move(bootSource),
1477491d8ee7SSantosh Puranik                               std::move(bootEnable));
1478491d8ee7SSantosh Puranik         }
14799712f8acSEd Tanous         if (indicatorLed)
14806617338dSEd Tanous         {
14819712f8acSEd Tanous             std::string dbusLedState;
1482d573bb2aSJennifer Lee             if (*indicatorLed == "Lit")
14839712f8acSEd Tanous             {
1484d573bb2aSJennifer Lee                 dbusLedState = "xyz.openbmc_project.Led.Physical.Action.On";
14856617338dSEd Tanous             }
14865c6221acSGunnar Mills             else if (*indicatorLed == "Blinking")
14876617338dSEd Tanous             {
14885c6221acSGunnar Mills                 dbusLedState = "xyz.openbmc_project.Led.Physical.Action.Blink";
14896617338dSEd Tanous             }
14909712f8acSEd Tanous             else if (*indicatorLed == "Off")
14916617338dSEd Tanous             {
14929712f8acSEd Tanous                 dbusLedState = "xyz.openbmc_project.Led.Physical.Action.Off";
14936617338dSEd Tanous             }
14946617338dSEd Tanous             else
14956617338dSEd Tanous             {
1496a08b46ccSJason M. Bills                 messages::propertyValueNotInList(res, *indicatorLed,
1497a08b46ccSJason M. Bills                                                  "IndicatorLED");
14986617338dSEd Tanous                 return;
14996617338dSEd Tanous             }
15006617338dSEd Tanous 
1501c5b2abe0SLewanczyk, Dawid             // Update led group
150255c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Update led group.";
150355c7b7a2SEd Tanous             crow::connections::systemBus->async_method_call(
1504cde19e5fSSantosh Puranik                 [asyncResp](const boost::system::error_code ec) {
15051abe55efSEd Tanous                     if (ec)
15061abe55efSEd Tanous                     {
150755c7b7a2SEd Tanous                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1508f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
1509c5b2abe0SLewanczyk, Dawid                         return;
1510c5b2abe0SLewanczyk, Dawid                     }
151155c7b7a2SEd Tanous                     BMCWEB_LOG_DEBUG << "Led group update done.";
1512c5b2abe0SLewanczyk, Dawid                 },
1513c5b2abe0SLewanczyk, Dawid                 "xyz.openbmc_project.LED.GroupManager",
1514c5b2abe0SLewanczyk, Dawid                 "/xyz/openbmc_project/led/groups/enclosure_identify",
1515c5b2abe0SLewanczyk, Dawid                 "org.freedesktop.DBus.Properties", "Set",
1516c5b2abe0SLewanczyk, Dawid                 "xyz.openbmc_project.Led.Group", "Asserted",
1517abf2add6SEd Tanous                 std::variant<bool>(
15186617338dSEd Tanous                     (dbusLedState ==
15196617338dSEd Tanous                              "xyz.openbmc_project.Led.Physical.Action.Off"
15206617338dSEd Tanous                          ? false
15216617338dSEd Tanous                          : true)));
1522c5b2abe0SLewanczyk, Dawid             // Update identify led status
152355c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Update led SoftwareInventoryCollection.";
152455c7b7a2SEd Tanous             crow::connections::systemBus->async_method_call(
15256617338dSEd Tanous                 [asyncResp{std::move(asyncResp)},
15269712f8acSEd Tanous                  indicatorLed{std::move(*indicatorLed)}](
1527c5b2abe0SLewanczyk, Dawid                     const boost::system::error_code ec) {
15281abe55efSEd Tanous                     if (ec)
15291abe55efSEd Tanous                     {
153055c7b7a2SEd Tanous                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1531f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
1532c5b2abe0SLewanczyk, Dawid                         return;
1533c5b2abe0SLewanczyk, Dawid                     }
153455c7b7a2SEd Tanous                     BMCWEB_LOG_DEBUG << "Led state update done.";
1535c5b2abe0SLewanczyk, Dawid                 },
1536c5b2abe0SLewanczyk, Dawid                 "xyz.openbmc_project.LED.Controller.identify",
1537c5b2abe0SLewanczyk, Dawid                 "/xyz/openbmc_project/led/physical/identify",
1538c5b2abe0SLewanczyk, Dawid                 "org.freedesktop.DBus.Properties", "Set",
1539c5b2abe0SLewanczyk, Dawid                 "xyz.openbmc_project.Led.Physical", "State",
1540abf2add6SEd Tanous                 std::variant<std::string>(dbusLedState));
15416617338dSEd Tanous         }
1542c5b2abe0SLewanczyk, Dawid     }
1543c5b2abe0SLewanczyk, Dawid };
1544c5b2abe0SLewanczyk, Dawid } // namespace redfish
1545