xref: /openbmc/bmcweb/features/redfish/lib/systems.hpp (revision 5c6221ac85ce72a612f7001246fb5a7ab521c7a5)
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 
189712f8acSEd Tanous #include <boost/container/flat_map.hpp>
199712f8acSEd Tanous #include <node.hpp>
20c5b2abe0SLewanczyk, Dawid #include <utils/json_utils.hpp>
21abf2add6SEd Tanous #include <variant>
22c5b2abe0SLewanczyk, Dawid 
231abe55efSEd Tanous namespace redfish
241abe55efSEd Tanous {
25c5b2abe0SLewanczyk, Dawid 
26c5b2abe0SLewanczyk, Dawid /**
27c5b2abe0SLewanczyk, Dawid  * @brief Retrieves computer system properties over dbus
28c5b2abe0SLewanczyk, Dawid  *
29c5b2abe0SLewanczyk, Dawid  * @param[in] aResp Shared pointer for completing asynchronous calls
30c5b2abe0SLewanczyk, Dawid  * @param[in] name  Computer system name from request
31c5b2abe0SLewanczyk, Dawid  *
32c5b2abe0SLewanczyk, Dawid  * @return None.
33c5b2abe0SLewanczyk, Dawid  */
34029573d4SEd Tanous void getComputerSystem(std::shared_ptr<AsyncResp> aResp)
351abe55efSEd Tanous {
3655c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get available system components.";
3755c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
38029573d4SEd Tanous         [aResp{std::move(aResp)}](
39c5b2abe0SLewanczyk, Dawid             const boost::system::error_code ec,
40c5b2abe0SLewanczyk, Dawid             const std::vector<std::pair<
416c34de48SEd Tanous                 std::string,
426c34de48SEd Tanous                 std::vector<std::pair<std::string, std::vector<std::string>>>>>
43c5b2abe0SLewanczyk, Dawid                 &subtree) {
441abe55efSEd Tanous             if (ec)
451abe55efSEd Tanous             {
4655c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error";
47f12894f8SJason M. Bills                 messages::internalError(aResp->res);
48c5b2abe0SLewanczyk, Dawid                 return;
49c5b2abe0SLewanczyk, Dawid             }
50c5b2abe0SLewanczyk, Dawid             // Iterate over all retrieved ObjectPaths.
516c34de48SEd Tanous             for (const std::pair<std::string,
526c34de48SEd Tanous                                  std::vector<std::pair<
536c34de48SEd Tanous                                      std::string, std::vector<std::string>>>>
541abe55efSEd Tanous                      &object : subtree)
551abe55efSEd Tanous             {
56c5b2abe0SLewanczyk, Dawid                 const std::string &path = object.first;
5755c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "Got path: " << path;
581abe55efSEd Tanous                 const std::vector<
591abe55efSEd Tanous                     std::pair<std::string, std::vector<std::string>>>
60c5b2abe0SLewanczyk, Dawid                     &connectionNames = object.second;
611abe55efSEd Tanous                 if (connectionNames.size() < 1)
621abe55efSEd Tanous                 {
63c5b2abe0SLewanczyk, Dawid                     continue;
64c5b2abe0SLewanczyk, Dawid                 }
65029573d4SEd Tanous 
666c34de48SEd Tanous                 // This is not system, so check if it's cpu, dimm, UUID or
676c34de48SEd Tanous                 // BiosVer
6804a258f4SEd Tanous                 for (const auto &connection : connectionNames)
691abe55efSEd Tanous                 {
7004a258f4SEd Tanous                     for (const auto &interfaceName : connection.second)
711abe55efSEd Tanous                     {
7204a258f4SEd Tanous                         if (interfaceName ==
7304a258f4SEd Tanous                             "xyz.openbmc_project.Inventory.Item.Dimm")
741abe55efSEd Tanous                         {
751abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
7604a258f4SEd Tanous                                 << "Found Dimm, now get its properties.";
7755c7b7a2SEd Tanous                             crow::connections::systemBus->async_method_call(
78029573d4SEd Tanous                                 [aResp](const boost::system::error_code ec,
796c34de48SEd Tanous                                         const std::vector<
806c34de48SEd Tanous                                             std::pair<std::string, VariantType>>
811abe55efSEd Tanous                                             &properties) {
821abe55efSEd Tanous                                     if (ec)
831abe55efSEd Tanous                                     {
841abe55efSEd Tanous                                         BMCWEB_LOG_ERROR
856c34de48SEd Tanous                                             << "DBUS response error " << ec;
86f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
87c5b2abe0SLewanczyk, Dawid                                         return;
88c5b2abe0SLewanczyk, Dawid                                     }
896c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
906c34de48SEd Tanous                                                      << properties.size()
91c5b2abe0SLewanczyk, Dawid                                                      << "Dimm properties.";
9204a258f4SEd Tanous                                     for (const std::pair<std::string,
9304a258f4SEd Tanous                                                          VariantType>
9404a258f4SEd Tanous                                              &property : properties)
951abe55efSEd Tanous                                     {
96029573d4SEd Tanous                                         if (property.first == "MemorySizeInKb")
971abe55efSEd Tanous                                         {
9804a258f4SEd Tanous                                             const uint64_t *value =
99029573d4SEd Tanous                                                 sdbusplus::message::variant_ns::
100029573d4SEd Tanous                                                     get_if<uint64_t>(
1011b6b96c5SEd Tanous                                                         &property.second);
10204a258f4SEd Tanous                                             if (value != nullptr)
1031abe55efSEd Tanous                                             {
1041abe55efSEd Tanous                                                 aResp->res.jsonValue
1056c34de48SEd Tanous                                                     ["TotalSystemMemoryGi"
1066c34de48SEd Tanous                                                      "B"] +=
10704a258f4SEd Tanous                                                     *value / (1024 * 1024);
108029573d4SEd Tanous                                                 aResp->res
109029573d4SEd Tanous                                                     .jsonValue["MemorySummary"]
110029573d4SEd Tanous                                                               ["Status"]
111029573d4SEd Tanous                                                               ["State"] =
1121abe55efSEd Tanous                                                     "Enabled";
113c5b2abe0SLewanczyk, Dawid                                             }
114c5b2abe0SLewanczyk, Dawid                                         }
115c5b2abe0SLewanczyk, Dawid                                     }
116c5b2abe0SLewanczyk, Dawid                                 },
11704a258f4SEd Tanous                                 connection.first, path,
1186c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
1196c34de48SEd Tanous                                 "xyz.openbmc_project.Inventory.Item.Dimm");
1201abe55efSEd Tanous                         }
12104a258f4SEd Tanous                         else if (interfaceName ==
12204a258f4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.Cpu")
1231abe55efSEd Tanous                         {
1241abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
12504a258f4SEd Tanous                                 << "Found Cpu, now get its properties.";
126a0803efaSEd Tanous                             crow::connections::systemBus->async_method_call(
127029573d4SEd Tanous                                 [aResp](const boost::system::error_code ec,
1286c34de48SEd Tanous                                         const std::vector<
1296c34de48SEd Tanous                                             std::pair<std::string, VariantType>>
1301abe55efSEd Tanous                                             &properties) {
1311abe55efSEd Tanous                                     if (ec)
1321abe55efSEd Tanous                                     {
1331abe55efSEd Tanous                                         BMCWEB_LOG_ERROR
1346c34de48SEd Tanous                                             << "DBUS response error " << ec;
135f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
136c5b2abe0SLewanczyk, Dawid                                         return;
137c5b2abe0SLewanczyk, Dawid                                     }
1386c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
1396c34de48SEd Tanous                                                      << properties.size()
140c5b2abe0SLewanczyk, Dawid                                                      << "Cpu properties.";
14104a258f4SEd Tanous                                     for (const auto &property : properties)
1421abe55efSEd Tanous                                     {
143029573d4SEd Tanous                                         if (property.first == "ProcessorFamily")
1441abe55efSEd Tanous                                         {
145a0803efaSEd Tanous                                             const std::string *value =
146029573d4SEd Tanous                                                 sdbusplus::message::variant_ns::
147029573d4SEd Tanous                                                     get_if<std::string>(
1481b6b96c5SEd Tanous                                                         &property.second);
1491abe55efSEd Tanous                                             if (value != nullptr)
1501abe55efSEd Tanous                                             {
151029573d4SEd Tanous                                                 nlohmann::json &procSummary =
1521abe55efSEd Tanous                                                     aResp->res.jsonValue
1536c34de48SEd Tanous                                                         ["ProcessorSumm"
15404a258f4SEd Tanous                                                          "ary"];
15504a258f4SEd Tanous                                                 nlohmann::json &procCount =
15604a258f4SEd Tanous                                                     procSummary["Count"];
15704a258f4SEd Tanous 
15804a258f4SEd Tanous                                                 procCount =
159029573d4SEd Tanous                                                     procCount.get<int>() + 1;
160029573d4SEd Tanous                                                 procSummary["Status"]["State"] =
161c5b2abe0SLewanczyk, Dawid                                                     "Enabled";
162029573d4SEd Tanous                                                 procSummary["Model"] = *value;
163c5b2abe0SLewanczyk, Dawid                                             }
164c5b2abe0SLewanczyk, Dawid                                         }
165c5b2abe0SLewanczyk, Dawid                                     }
166c5b2abe0SLewanczyk, Dawid                                 },
16704a258f4SEd Tanous                                 connection.first, path,
1686c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
1696c34de48SEd Tanous                                 "xyz.openbmc_project.Inventory.Item.Cpu");
1701abe55efSEd Tanous                         }
17104a258f4SEd Tanous                         else if (interfaceName ==
17204a258f4SEd Tanous                                  "xyz.openbmc_project.Common.UUID")
1731abe55efSEd Tanous                         {
1741abe55efSEd Tanous                             BMCWEB_LOG_DEBUG
17504a258f4SEd Tanous                                 << "Found UUID, now get its properties.";
17655c7b7a2SEd Tanous                             crow::connections::systemBus->async_method_call(
177029573d4SEd Tanous                                 [aResp](const boost::system::error_code ec,
1786c34de48SEd Tanous                                         const std::vector<
1796c34de48SEd Tanous                                             std::pair<std::string, VariantType>>
1801abe55efSEd Tanous                                             &properties) {
1811abe55efSEd Tanous                                     if (ec)
1821abe55efSEd Tanous                                     {
1831abe55efSEd Tanous                                         BMCWEB_LOG_DEBUG
1846c34de48SEd Tanous                                             << "DBUS response error " << ec;
185f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
186c5b2abe0SLewanczyk, Dawid                                         return;
187c5b2abe0SLewanczyk, Dawid                                     }
1886c34de48SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
1896c34de48SEd Tanous                                                      << properties.size()
190c5b2abe0SLewanczyk, Dawid                                                      << "UUID properties.";
1911abe55efSEd Tanous                                     for (const std::pair<std::string,
19204a258f4SEd Tanous                                                          VariantType>
19304a258f4SEd Tanous                                              &property : properties)
1941abe55efSEd Tanous                                     {
19504a258f4SEd Tanous                                         if (property.first == "BIOSVer")
1961abe55efSEd Tanous                                         {
197c5b2abe0SLewanczyk, Dawid                                             const std::string *value =
198029573d4SEd Tanous                                                 sdbusplus::message::variant_ns::
199029573d4SEd Tanous                                                     get_if<std::string>(
2001b6b96c5SEd Tanous                                                         &property.second);
2011abe55efSEd Tanous                                             if (value != nullptr)
2021abe55efSEd Tanous                                             {
203029573d4SEd Tanous                                                 aResp->res
204029573d4SEd Tanous                                                     .jsonValue["BiosVersion"] =
2051abe55efSEd Tanous                                                     *value;
206c5b2abe0SLewanczyk, Dawid                                             }
207c5b2abe0SLewanczyk, Dawid                                         }
20804a258f4SEd Tanous                                         if (property.first == "UUID")
2091abe55efSEd Tanous                                         {
210c5b2abe0SLewanczyk, Dawid                                             const std::string *value =
211029573d4SEd Tanous                                                 sdbusplus::message::variant_ns::
212029573d4SEd Tanous                                                     get_if<std::string>(
2131b6b96c5SEd Tanous                                                         &property.second);
21404a258f4SEd Tanous 
2151abe55efSEd Tanous                                             if (value != nullptr)
2161abe55efSEd Tanous                                             {
217029573d4SEd Tanous                                                 std::string valueStr = *value;
21804a258f4SEd Tanous                                                 if (valueStr.size() == 32)
2191abe55efSEd Tanous                                                 {
220029573d4SEd Tanous                                                     valueStr.insert(8, 1, '-');
221029573d4SEd Tanous                                                     valueStr.insert(13, 1, '-');
222029573d4SEd Tanous                                                     valueStr.insert(18, 1, '-');
223029573d4SEd Tanous                                                     valueStr.insert(23, 1, '-');
22404a258f4SEd Tanous                                                 }
225029573d4SEd Tanous                                                 BMCWEB_LOG_DEBUG << "UUID = "
22604a258f4SEd Tanous                                                                  << valueStr;
227029573d4SEd Tanous                                                 aResp->res.jsonValue["UUID"] =
22804a258f4SEd Tanous                                                     valueStr;
229c5b2abe0SLewanczyk, Dawid                                             }
230c5b2abe0SLewanczyk, Dawid                                         }
231c5b2abe0SLewanczyk, Dawid                                     }
232c5b2abe0SLewanczyk, Dawid                                 },
23304a258f4SEd Tanous                                 connection.first, path,
2346c34de48SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
2351abe55efSEd Tanous                                 "xyz.openbmc_project.Common.UUID");
236c5b2abe0SLewanczyk, Dawid                         }
237029573d4SEd Tanous                         else if (interfaceName ==
238029573d4SEd Tanous                                  "xyz.openbmc_project.Inventory.Item.System")
2391abe55efSEd Tanous                         {
240029573d4SEd Tanous                             crow::connections::systemBus->async_method_call(
241029573d4SEd Tanous                                 [aResp](const boost::system::error_code ec,
242029573d4SEd Tanous                                         const std::vector<
243029573d4SEd Tanous                                             std::pair<std::string, VariantType>>
244029573d4SEd Tanous                                             &propertiesList) {
245029573d4SEd Tanous                                     if (ec)
246029573d4SEd Tanous                                     {
247029573d4SEd Tanous                                         BMCWEB_LOG_ERROR
248029573d4SEd Tanous                                             << "DBUS response error: " << ec;
249f12894f8SJason M. Bills                                         messages::internalError(aResp->res);
250029573d4SEd Tanous                                         return;
251029573d4SEd Tanous                                     }
252029573d4SEd Tanous                                     BMCWEB_LOG_DEBUG << "Got "
253029573d4SEd Tanous                                                      << propertiesList.size()
254029573d4SEd Tanous                                                      << "properties for system";
255029573d4SEd Tanous                                     for (const std::pair<std::string,
256029573d4SEd Tanous                                                          VariantType>
257029573d4SEd Tanous                                              &property : propertiesList)
258029573d4SEd Tanous                                     {
259fc5afcf9Sbeccabroek                                         const std::string &propertyName =
260fc5afcf9Sbeccabroek                                             property.first;
261fc5afcf9Sbeccabroek                                         if ((propertyName == "PartNumber") ||
262fc5afcf9Sbeccabroek                                             (propertyName == "SerialNumber") ||
263fc5afcf9Sbeccabroek                                             (propertyName == "Manufacturer") ||
264fc5afcf9Sbeccabroek                                             (propertyName == "Model"))
265fc5afcf9Sbeccabroek                                         {
266029573d4SEd Tanous                                             const std::string *value =
267fc5afcf9Sbeccabroek                                                 std::get_if<std::string>(
268029573d4SEd Tanous                                                     &property.second);
269029573d4SEd Tanous                                             if (value != nullptr)
270029573d4SEd Tanous                                             {
271029573d4SEd Tanous                                                 aResp->res
272fc5afcf9Sbeccabroek                                                     .jsonValue[propertyName] =
273029573d4SEd Tanous                                                     *value;
274029573d4SEd Tanous                                             }
275029573d4SEd Tanous                                         }
276fc5afcf9Sbeccabroek                                     }
277029573d4SEd Tanous                                     aResp->res.jsonValue["Name"] = "system";
278029573d4SEd Tanous                                     aResp->res.jsonValue["Id"] =
279029573d4SEd Tanous                                         aResp->res.jsonValue["SerialNumber"];
280029573d4SEd Tanous                                 },
281029573d4SEd Tanous                                 connection.first, path,
282029573d4SEd Tanous                                 "org.freedesktop.DBus.Properties", "GetAll",
283029573d4SEd Tanous                                 "xyz.openbmc_project.Inventory.Decorator."
284029573d4SEd Tanous                                 "Asset");
285029573d4SEd Tanous                         }
286029573d4SEd Tanous                     }
287029573d4SEd Tanous                 }
288c5b2abe0SLewanczyk, Dawid             }
289c5b2abe0SLewanczyk, Dawid         },
290c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper",
291c5b2abe0SLewanczyk, Dawid         "/xyz/openbmc_project/object_mapper",
292c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2936617338dSEd Tanous         "/xyz/openbmc_project/inventory", int32_t(0),
2946617338dSEd Tanous         std::array<const char *, 5>{
2956617338dSEd Tanous             "xyz.openbmc_project.Inventory.Decorator.Asset",
2966617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Cpu",
2976617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.Dimm",
2986617338dSEd Tanous             "xyz.openbmc_project.Inventory.Item.System",
2996617338dSEd Tanous             "xyz.openbmc_project.Common.UUID",
3006617338dSEd Tanous         });
301c5b2abe0SLewanczyk, Dawid }
302c5b2abe0SLewanczyk, Dawid 
303c5b2abe0SLewanczyk, Dawid /**
304c5b2abe0SLewanczyk, Dawid  * @brief Retrieves identify led group properties over dbus
305c5b2abe0SLewanczyk, Dawid  *
306491d8ee7SSantosh Puranik  * @param[in] aResp     Shared pointer for generating response message.
307c5b2abe0SLewanczyk, Dawid  * @param[in] callback  Callback for process retrieved data.
308c5b2abe0SLewanczyk, Dawid  *
309c5b2abe0SLewanczyk, Dawid  * @return None.
310c5b2abe0SLewanczyk, Dawid  */
311c5b2abe0SLewanczyk, Dawid template <typename CallbackFunc>
312a0803efaSEd Tanous void getLedGroupIdentify(std::shared_ptr<AsyncResp> aResp,
3131abe55efSEd Tanous                          CallbackFunc &&callback)
3141abe55efSEd Tanous {
31555c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get led groups";
31655c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
3171abe55efSEd Tanous         [aResp{std::move(aResp)},
3186617338dSEd Tanous          callback{std::move(callback)}](const boost::system::error_code &ec,
3191abe55efSEd Tanous                                         const ManagedObjectsType &resp) {
3201abe55efSEd Tanous             if (ec)
3211abe55efSEd Tanous             {
32255c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
323f12894f8SJason M. Bills                 messages::internalError(aResp->res);
324c5b2abe0SLewanczyk, Dawid                 return;
325c5b2abe0SLewanczyk, Dawid             }
3266c34de48SEd Tanous             BMCWEB_LOG_DEBUG << "Got " << resp.size() << "led group objects.";
3271abe55efSEd Tanous             for (const auto &objPath : resp)
3281abe55efSEd Tanous             {
329c5b2abe0SLewanczyk, Dawid                 const std::string &path = objPath.first;
3301abe55efSEd Tanous                 if (path.rfind("enclosure_identify") != std::string::npos)
3311abe55efSEd Tanous                 {
3321abe55efSEd Tanous                     for (const auto &interface : objPath.second)
3331abe55efSEd Tanous                     {
3346c34de48SEd Tanous                         if (interface.first == "xyz.openbmc_project.Led.Group")
3351abe55efSEd Tanous                         {
3361abe55efSEd Tanous                             for (const auto &property : interface.second)
3371abe55efSEd Tanous                             {
3381abe55efSEd Tanous                                 if (property.first == "Asserted")
3391abe55efSEd Tanous                                 {
340c5b2abe0SLewanczyk, Dawid                                     const bool *asserted =
341abf2add6SEd Tanous                                         std::get_if<bool>(&property.second);
3421abe55efSEd Tanous                                     if (nullptr != asserted)
3431abe55efSEd Tanous                                     {
344c5b2abe0SLewanczyk, Dawid                                         callback(*asserted, aResp);
3451abe55efSEd Tanous                                     }
3461abe55efSEd Tanous                                     else
3471abe55efSEd Tanous                                     {
348c5b2abe0SLewanczyk, Dawid                                         callback(false, aResp);
349c5b2abe0SLewanczyk, Dawid                                     }
350c5b2abe0SLewanczyk, Dawid                                 }
351c5b2abe0SLewanczyk, Dawid                             }
352c5b2abe0SLewanczyk, Dawid                         }
353c5b2abe0SLewanczyk, Dawid                     }
354c5b2abe0SLewanczyk, Dawid                 }
355c5b2abe0SLewanczyk, Dawid             }
356c5b2abe0SLewanczyk, Dawid         },
357c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.LED.GroupManager",
3586c34de48SEd Tanous         "/xyz/openbmc_project/led/groups", "org.freedesktop.DBus.ObjectManager",
3596c34de48SEd Tanous         "GetManagedObjects");
360c5b2abe0SLewanczyk, Dawid }
361c5b2abe0SLewanczyk, Dawid 
362c5b2abe0SLewanczyk, Dawid template <typename CallbackFunc>
3636c34de48SEd Tanous void getLedIdentify(std::shared_ptr<AsyncResp> aResp, CallbackFunc &&callback)
3641abe55efSEd Tanous {
36555c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get identify led properties";
36655c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
3676617338dSEd Tanous         [aResp,
3686617338dSEd Tanous          callback{std::move(callback)}](const boost::system::error_code ec,
369c5b2abe0SLewanczyk, Dawid                                         const PropertiesType &properties) {
3701abe55efSEd Tanous             if (ec)
3711abe55efSEd Tanous             {
37255c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
373f12894f8SJason M. Bills                 messages::internalError(aResp->res);
374c5b2abe0SLewanczyk, Dawid                 return;
375c5b2abe0SLewanczyk, Dawid             }
3761abe55efSEd Tanous             BMCWEB_LOG_DEBUG << "Got " << properties.size()
3771abe55efSEd Tanous                              << "led properties.";
378c5b2abe0SLewanczyk, Dawid             std::string output;
3791abe55efSEd Tanous             for (const auto &property : properties)
3801abe55efSEd Tanous             {
3811abe55efSEd Tanous                 if (property.first == "State")
3821abe55efSEd Tanous                 {
383c5b2abe0SLewanczyk, Dawid                     const std::string *s =
384abf2add6SEd Tanous                         std::get_if<std::string>(&property.second);
3851abe55efSEd Tanous                     if (nullptr != s)
3861abe55efSEd Tanous                     {
38755c7b7a2SEd Tanous                         BMCWEB_LOG_DEBUG << "Identify Led State: " << *s;
388c5b2abe0SLewanczyk, Dawid                         const auto pos = s->rfind('.');
3891abe55efSEd Tanous                         if (pos != std::string::npos)
3901abe55efSEd Tanous                         {
391c5b2abe0SLewanczyk, Dawid                             auto led = s->substr(pos + 1);
3921abe55efSEd Tanous                             for (const std::pair<const char *, const char *>
3931abe55efSEd Tanous                                      &p :
3941abe55efSEd Tanous                                  std::array<
3956c34de48SEd Tanous                                      std::pair<const char *, const char *>, 3>{
3966c34de48SEd Tanous                                      {{"On", "Lit"},
397c5b2abe0SLewanczyk, Dawid                                       {"Blink", "Blinking"},
3981abe55efSEd Tanous                                       {"Off", "Off"}}})
3991abe55efSEd Tanous                             {
4001abe55efSEd Tanous                                 if (led == p.first)
4011abe55efSEd Tanous                                 {
402c5b2abe0SLewanczyk, Dawid                                     output = p.second;
403c5b2abe0SLewanczyk, Dawid                                 }
404c5b2abe0SLewanczyk, Dawid                             }
405c5b2abe0SLewanczyk, Dawid                         }
406c5b2abe0SLewanczyk, Dawid                     }
407c5b2abe0SLewanczyk, Dawid                 }
408c5b2abe0SLewanczyk, Dawid             }
409c5b2abe0SLewanczyk, Dawid             callback(output, aResp);
410c5b2abe0SLewanczyk, Dawid         },
411c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.LED.Controller.identify",
412c5b2abe0SLewanczyk, Dawid         "/xyz/openbmc_project/led/physical/identify",
413c5b2abe0SLewanczyk, Dawid         "org.freedesktop.DBus.Properties", "GetAll",
414c5b2abe0SLewanczyk, Dawid         "xyz.openbmc_project.Led.Physical");
415c5b2abe0SLewanczyk, Dawid }
416c5b2abe0SLewanczyk, Dawid 
417c5b2abe0SLewanczyk, Dawid /**
418c5b2abe0SLewanczyk, Dawid  * @brief Retrieves host state properties over dbus
419c5b2abe0SLewanczyk, Dawid  *
420c5b2abe0SLewanczyk, Dawid  * @param[in] aResp     Shared pointer for completing asynchronous calls.
421c5b2abe0SLewanczyk, Dawid  *
422c5b2abe0SLewanczyk, Dawid  * @return None.
423c5b2abe0SLewanczyk, Dawid  */
424a0803efaSEd Tanous void getHostState(std::shared_ptr<AsyncResp> aResp)
4251abe55efSEd Tanous {
42655c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Get host information.";
42755c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
428abf2add6SEd Tanous         [aResp{std::move(aResp)}](const boost::system::error_code ec,
429abf2add6SEd Tanous                                   const std::variant<std::string> &hostState) {
4301abe55efSEd Tanous             if (ec)
4311abe55efSEd Tanous             {
43255c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
433f12894f8SJason M. Bills                 messages::internalError(aResp->res);
434c5b2abe0SLewanczyk, Dawid                 return;
435c5b2abe0SLewanczyk, Dawid             }
4366617338dSEd Tanous 
437abf2add6SEd Tanous             const std::string *s = std::get_if<std::string>(&hostState);
43855c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Host state: " << *s;
4396617338dSEd Tanous             if (s != nullptr)
4401abe55efSEd Tanous             {
441c5b2abe0SLewanczyk, Dawid                 // Verify Host State
44294732661SAndrew Geissler                 if (*s == "xyz.openbmc_project.State.Host.HostState.Running")
4431abe55efSEd Tanous                 {
44455c7b7a2SEd Tanous                     aResp->res.jsonValue["PowerState"] = "On";
4456617338dSEd Tanous                     aResp->res.jsonValue["Status"]["State"] = "Enabled";
4461abe55efSEd Tanous                 }
4471abe55efSEd Tanous                 else
4481abe55efSEd Tanous                 {
44955c7b7a2SEd Tanous                     aResp->res.jsonValue["PowerState"] = "Off";
4506617338dSEd Tanous                     aResp->res.jsonValue["Status"]["State"] = "Disabled";
451c5b2abe0SLewanczyk, Dawid                 }
452c5b2abe0SLewanczyk, Dawid             }
453c5b2abe0SLewanczyk, Dawid         },
4546c34de48SEd Tanous         "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
4556617338dSEd Tanous         "org.freedesktop.DBus.Properties", "Get",
4566617338dSEd Tanous         "xyz.openbmc_project.State.Host", "CurrentHostState");
457c5b2abe0SLewanczyk, Dawid }
458c5b2abe0SLewanczyk, Dawid 
459c5b2abe0SLewanczyk, Dawid /**
460491d8ee7SSantosh Puranik  * @brief Traslates boot source DBUS property value to redfish.
461491d8ee7SSantosh Puranik  *
462491d8ee7SSantosh Puranik  * @param[in] dbusSource    The boot source in DBUS speak.
463491d8ee7SSantosh Puranik  *
464491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source in Redfish terms. If translation
465491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
466491d8ee7SSantosh Puranik  */
467491d8ee7SSantosh Puranik static std::string dbusToRfBootSource(const std::string &dbusSource)
468491d8ee7SSantosh Puranik {
469491d8ee7SSantosh Puranik     if (dbusSource == "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
470491d8ee7SSantosh Puranik     {
471491d8ee7SSantosh Puranik         return "None";
472491d8ee7SSantosh Puranik     }
473491d8ee7SSantosh Puranik     else if (dbusSource ==
474491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.Disk")
475491d8ee7SSantosh Puranik     {
476491d8ee7SSantosh Puranik         return "Hdd";
477491d8ee7SSantosh Puranik     }
478491d8ee7SSantosh Puranik     else if (dbusSource ==
479491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia")
480491d8ee7SSantosh Puranik     {
481491d8ee7SSantosh Puranik         return "Cd";
482491d8ee7SSantosh Puranik     }
483491d8ee7SSantosh Puranik     else if (dbusSource ==
484491d8ee7SSantosh Puranik              "xyz.openbmc_project.Control.Boot.Source.Sources.Network")
485491d8ee7SSantosh Puranik     {
486491d8ee7SSantosh Puranik         return "Pxe";
487491d8ee7SSantosh Puranik     }
488491d8ee7SSantosh Puranik     else
489491d8ee7SSantosh Puranik     {
490491d8ee7SSantosh Puranik         return "";
491491d8ee7SSantosh Puranik     }
492491d8ee7SSantosh Puranik }
493491d8ee7SSantosh Puranik 
494491d8ee7SSantosh Puranik /**
495491d8ee7SSantosh Puranik  * @brief Traslates boot mode DBUS property value to redfish.
496491d8ee7SSantosh Puranik  *
497491d8ee7SSantosh Puranik  * @param[in] dbusMode    The boot mode in DBUS speak.
498491d8ee7SSantosh Puranik  *
499491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode in Redfish terms. If translation
500491d8ee7SSantosh Puranik  * cannot be done, returns an empty string.
501491d8ee7SSantosh Puranik  */
502491d8ee7SSantosh Puranik static std::string dbusToRfBootMode(const std::string &dbusMode)
503491d8ee7SSantosh Puranik {
504491d8ee7SSantosh Puranik     if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
505491d8ee7SSantosh Puranik     {
506491d8ee7SSantosh Puranik         return "None";
507491d8ee7SSantosh Puranik     }
508491d8ee7SSantosh Puranik     else if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe")
509491d8ee7SSantosh Puranik     {
510491d8ee7SSantosh Puranik         return "Diags";
511491d8ee7SSantosh Puranik     }
512491d8ee7SSantosh Puranik     else if (dbusMode == "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup")
513491d8ee7SSantosh Puranik     {
514491d8ee7SSantosh Puranik         return "BiosSetup";
515491d8ee7SSantosh Puranik     }
516491d8ee7SSantosh Puranik     else
517491d8ee7SSantosh Puranik     {
518491d8ee7SSantosh Puranik         return "";
519491d8ee7SSantosh Puranik     }
520491d8ee7SSantosh Puranik }
521491d8ee7SSantosh Puranik 
522491d8ee7SSantosh Puranik /**
523491d8ee7SSantosh Puranik  * @brief Traslates boot source from Redfish to DBUS property value.
524491d8ee7SSantosh Puranik  *
525491d8ee7SSantosh Puranik  * @param[in] rfSource    The boot source in Redfish.
526491d8ee7SSantosh Puranik  *
527491d8ee7SSantosh Puranik  * @return Returns as a string, the boot source as expected by DBUS.
528491d8ee7SSantosh Puranik  * If translation cannot be done, returns an empty string.
529491d8ee7SSantosh Puranik  */
530491d8ee7SSantosh Puranik static std::string rfToDbusBootSource(const std::string &rfSource)
531491d8ee7SSantosh Puranik {
532491d8ee7SSantosh Puranik     if (rfSource == "None")
533491d8ee7SSantosh Puranik     {
534491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
535491d8ee7SSantosh Puranik     }
536491d8ee7SSantosh Puranik     else if (rfSource == "Hdd")
537491d8ee7SSantosh Puranik     {
538491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Source.Sources.Disk";
539491d8ee7SSantosh Puranik     }
540491d8ee7SSantosh Puranik     else if (rfSource == "Cd")
541491d8ee7SSantosh Puranik     {
542491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Source.Sources.ExternalMedia";
543491d8ee7SSantosh Puranik     }
544491d8ee7SSantosh Puranik     else if (rfSource == "Pxe")
545491d8ee7SSantosh Puranik     {
546491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Source.Sources.Network";
547491d8ee7SSantosh Puranik     }
548491d8ee7SSantosh Puranik     else
549491d8ee7SSantosh Puranik     {
550491d8ee7SSantosh Puranik         return "";
551491d8ee7SSantosh Puranik     }
552491d8ee7SSantosh Puranik }
553491d8ee7SSantosh Puranik 
554491d8ee7SSantosh Puranik /**
555491d8ee7SSantosh Puranik  * @brief Traslates boot mode from Redfish to DBUS property value.
556491d8ee7SSantosh Puranik  *
557491d8ee7SSantosh Puranik  * @param[in] rfMode    The boot mode in Redfish.
558491d8ee7SSantosh Puranik  *
559491d8ee7SSantosh Puranik  * @return Returns as a string, the boot mode as expected by DBUS.
560491d8ee7SSantosh Puranik  * If translation cannot be done, returns an empty string.
561491d8ee7SSantosh Puranik  */
562491d8ee7SSantosh Puranik static std::string rfToDbusBootMode(const std::string &rfMode)
563491d8ee7SSantosh Puranik {
564491d8ee7SSantosh Puranik     if (rfMode == "None")
565491d8ee7SSantosh Puranik     {
566491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
567491d8ee7SSantosh Puranik     }
568491d8ee7SSantosh Puranik     else if (rfMode == "Diags")
569491d8ee7SSantosh Puranik     {
570491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Mode.Modes.Safe";
571491d8ee7SSantosh Puranik     }
572491d8ee7SSantosh Puranik     else if (rfMode == "BiosSetup")
573491d8ee7SSantosh Puranik     {
574491d8ee7SSantosh Puranik         return "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup";
575491d8ee7SSantosh Puranik     }
576491d8ee7SSantosh Puranik     else
577491d8ee7SSantosh Puranik     {
578491d8ee7SSantosh Puranik         return "";
579491d8ee7SSantosh Puranik     }
580491d8ee7SSantosh Puranik }
581491d8ee7SSantosh Puranik 
582491d8ee7SSantosh Puranik /**
583491d8ee7SSantosh Puranik  * @brief Retrieves boot mode over DBUS and fills out the response
584491d8ee7SSantosh Puranik  *
585491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
586491d8ee7SSantosh Puranik  * @param[in] bootDbusObj   The dbus object to query for boot properties.
587491d8ee7SSantosh Puranik  *
588491d8ee7SSantosh Puranik  * @return None.
589491d8ee7SSantosh Puranik  */
590491d8ee7SSantosh Puranik static void getBootMode(std::shared_ptr<AsyncResp> aResp,
591491d8ee7SSantosh Puranik                         std::string bootDbusObj)
592491d8ee7SSantosh Puranik {
593491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
594491d8ee7SSantosh Puranik         [aResp](const boost::system::error_code ec,
595491d8ee7SSantosh Puranik                 const std::variant<std::string> &bootMode) {
596491d8ee7SSantosh Puranik             if (ec)
597491d8ee7SSantosh Puranik             {
598491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
599491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
600491d8ee7SSantosh Puranik                 return;
601491d8ee7SSantosh Puranik             }
602491d8ee7SSantosh Puranik 
603491d8ee7SSantosh Puranik             const std::string *bootModeStr =
604491d8ee7SSantosh Puranik                 std::get_if<std::string>(&bootMode);
605491d8ee7SSantosh Puranik 
606491d8ee7SSantosh Puranik             if (!bootModeStr)
607491d8ee7SSantosh Puranik             {
608491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
609491d8ee7SSantosh Puranik                 return;
610491d8ee7SSantosh Puranik             }
611491d8ee7SSantosh Puranik 
612491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot mode: " << *bootModeStr;
613491d8ee7SSantosh Puranik 
614491d8ee7SSantosh Puranik             // TODO (Santosh): Do we need to support override mode?
615491d8ee7SSantosh Puranik             aResp->res.jsonValue["Boot"]["BootSourceOverrideMode"] = "Legacy";
616491d8ee7SSantosh Puranik             aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget@Redfish."
617491d8ee7SSantosh Puranik                                          "AllowableValues"] = {
618491d8ee7SSantosh Puranik                 "None", "Pxe", "Hdd", "Cd", "Diags", "BiosSetup"};
619491d8ee7SSantosh Puranik 
620491d8ee7SSantosh Puranik             if (*bootModeStr !=
621491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
622491d8ee7SSantosh Puranik             {
623491d8ee7SSantosh Puranik                 auto rfMode = dbusToRfBootMode(*bootModeStr);
624491d8ee7SSantosh Puranik                 if (!rfMode.empty())
625491d8ee7SSantosh Puranik                 {
626491d8ee7SSantosh Puranik                     aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
627491d8ee7SSantosh Puranik                         rfMode;
628491d8ee7SSantosh Puranik                 }
629491d8ee7SSantosh Puranik             }
630491d8ee7SSantosh Puranik 
631491d8ee7SSantosh Puranik             // If the BootSourceOverrideTarget is still "None" at the end,
632491d8ee7SSantosh Puranik             // reset the BootSourceOverrideEnabled to indicate that
633491d8ee7SSantosh Puranik             // overrides are disabled
634491d8ee7SSantosh Puranik             if (aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] ==
635491d8ee7SSantosh Puranik                 "None")
636491d8ee7SSantosh Puranik             {
637491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
638491d8ee7SSantosh Puranik                     "Disabled";
639491d8ee7SSantosh Puranik             }
640491d8ee7SSantosh Puranik         },
641491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootDbusObj,
642491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
643491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Mode", "BootMode");
644491d8ee7SSantosh Puranik }
645491d8ee7SSantosh Puranik 
646491d8ee7SSantosh Puranik /**
647491d8ee7SSantosh Puranik  * @brief Retrieves boot source over DBUS
648491d8ee7SSantosh Puranik  *
649491d8ee7SSantosh Puranik  * @param[in] aResp         Shared pointer for generating response message.
650491d8ee7SSantosh Puranik  * @param[in] oneTimeEnable Boolean to indicate boot properties are one-time.
651491d8ee7SSantosh Puranik  *
652491d8ee7SSantosh Puranik  * @return None.
653491d8ee7SSantosh Puranik  */
654491d8ee7SSantosh Puranik static void getBootSource(std::shared_ptr<AsyncResp> aResp, bool oneTimeEnabled)
655491d8ee7SSantosh Puranik {
656491d8ee7SSantosh Puranik     std::string bootDbusObj =
657491d8ee7SSantosh Puranik         oneTimeEnabled ? "/xyz/openbmc_project/control/host0/boot/one_time"
658491d8ee7SSantosh Puranik                        : "/xyz/openbmc_project/control/host0/boot";
659491d8ee7SSantosh Puranik 
660491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Is one time: " << oneTimeEnabled;
661491d8ee7SSantosh Puranik     aResp->res.jsonValue["Boot"]["BootSourceOverrideEnabled"] =
662491d8ee7SSantosh Puranik         (oneTimeEnabled) ? "Once" : "Continuous";
663491d8ee7SSantosh Puranik 
664491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
665491d8ee7SSantosh Puranik         [aResp, bootDbusObj](const boost::system::error_code ec,
666491d8ee7SSantosh Puranik                              const std::variant<std::string> &bootSource) {
667491d8ee7SSantosh Puranik             if (ec)
668491d8ee7SSantosh Puranik             {
669491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
670491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
671491d8ee7SSantosh Puranik                 return;
672491d8ee7SSantosh Puranik             }
673491d8ee7SSantosh Puranik 
674491d8ee7SSantosh Puranik             const std::string *bootSourceStr =
675491d8ee7SSantosh Puranik                 std::get_if<std::string>(&bootSource);
676491d8ee7SSantosh Puranik 
677491d8ee7SSantosh Puranik             if (!bootSourceStr)
678491d8ee7SSantosh Puranik             {
679491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
680491d8ee7SSantosh Puranik                 return;
681491d8ee7SSantosh Puranik             }
682491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot source: " << *bootSourceStr;
683491d8ee7SSantosh Puranik 
684491d8ee7SSantosh Puranik             auto rfSource = dbusToRfBootSource(*bootSourceStr);
685491d8ee7SSantosh Puranik             if (!rfSource.empty())
686491d8ee7SSantosh Puranik             {
687491d8ee7SSantosh Puranik                 aResp->res.jsonValue["Boot"]["BootSourceOverrideTarget"] =
688491d8ee7SSantosh Puranik                     rfSource;
689491d8ee7SSantosh Puranik             }
690491d8ee7SSantosh Puranik         },
691491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings", bootDbusObj,
692491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
693491d8ee7SSantosh Puranik         "xyz.openbmc_project.Control.Boot.Source", "BootSource");
694491d8ee7SSantosh Puranik     getBootMode(std::move(aResp), std::move(bootDbusObj));
695491d8ee7SSantosh Puranik }
696491d8ee7SSantosh Puranik 
697491d8ee7SSantosh Puranik /**
698491d8ee7SSantosh Puranik  * @brief Retrieves "One time" enabled setting over DBUS and calls function to
699491d8ee7SSantosh Puranik  * get boot source and boot mode.
700491d8ee7SSantosh Puranik  *
701491d8ee7SSantosh Puranik  * @param[in] aResp     Shared pointer for generating response message.
702491d8ee7SSantosh Puranik  *
703491d8ee7SSantosh Puranik  * @return None.
704491d8ee7SSantosh Puranik  */
705491d8ee7SSantosh Puranik static void getBootProperties(std::shared_ptr<AsyncResp> aResp)
706491d8ee7SSantosh Puranik {
707491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Get boot information.";
708491d8ee7SSantosh Puranik 
709491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
710491d8ee7SSantosh Puranik         [aResp{std::move(aResp)}](
711491d8ee7SSantosh Puranik             const boost::system::error_code ec,
712491d8ee7SSantosh Puranik             const sdbusplus::message::variant<bool> &oneTime) {
713491d8ee7SSantosh Puranik             if (ec)
714491d8ee7SSantosh Puranik             {
715491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
716491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
717491d8ee7SSantosh Puranik                 return;
718491d8ee7SSantosh Puranik             }
719491d8ee7SSantosh Puranik 
720491d8ee7SSantosh Puranik             const bool *oneTimePtr = std::get_if<bool>(&oneTime);
721491d8ee7SSantosh Puranik 
722491d8ee7SSantosh Puranik             if (!oneTimePtr)
723491d8ee7SSantosh Puranik             {
724491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
725491d8ee7SSantosh Puranik                 return;
726491d8ee7SSantosh Puranik             }
727491d8ee7SSantosh Puranik             getBootSource(aResp, *oneTimePtr);
728491d8ee7SSantosh Puranik         },
729491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
730491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
731491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
732491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled");
733491d8ee7SSantosh Puranik }
734491d8ee7SSantosh Puranik 
735491d8ee7SSantosh Puranik /**
736491d8ee7SSantosh Puranik  * @brief Sets boot properties into DBUS object(s).
737491d8ee7SSantosh Puranik  *
738491d8ee7SSantosh Puranik  * @param[in] aResp           Shared pointer for generating response message.
739491d8ee7SSantosh Puranik  * @param[in] oneTimeEnabled  Is "one-time" setting already enabled.
740491d8ee7SSantosh Puranik  * @param[in] bootSource      The boot source to set.
741491d8ee7SSantosh Puranik  * @param[in] bootEnable      The source override "enable" to set.
742491d8ee7SSantosh Puranik  *
743491d8ee7SSantosh Puranik  * @return None.
744491d8ee7SSantosh Puranik  */
745491d8ee7SSantosh Puranik static void setBootModeOrSource(std::shared_ptr<AsyncResp> aResp,
746491d8ee7SSantosh Puranik                                 bool oneTimeEnabled,
747491d8ee7SSantosh Puranik                                 std::optional<std::string> bootSource,
748491d8ee7SSantosh Puranik                                 std::optional<std::string> bootEnable)
749491d8ee7SSantosh Puranik {
750491d8ee7SSantosh Puranik     if (bootEnable && (bootEnable != "Once") && (bootEnable != "Continuous") &&
751491d8ee7SSantosh Puranik         (bootEnable != "Disabled"))
752491d8ee7SSantosh Puranik     {
753491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Unsupported value for BootSourceOverrideEnabled: "
754491d8ee7SSantosh Puranik                          << *bootEnable;
755491d8ee7SSantosh Puranik         messages::propertyValueNotInList(aResp->res, *bootEnable,
756491d8ee7SSantosh Puranik                                          "BootSourceOverrideEnabled");
757491d8ee7SSantosh Puranik         return;
758491d8ee7SSantosh Puranik     }
759491d8ee7SSantosh Puranik 
760491d8ee7SSantosh Puranik     bool oneTimeSetting = oneTimeEnabled;
761491d8ee7SSantosh Puranik     // Validate incoming parameters
762491d8ee7SSantosh Puranik     if (bootEnable)
763491d8ee7SSantosh Puranik     {
764491d8ee7SSantosh Puranik         if (*bootEnable == "Once")
765491d8ee7SSantosh Puranik         {
766491d8ee7SSantosh Puranik             oneTimeSetting = true;
767491d8ee7SSantosh Puranik         }
768491d8ee7SSantosh Puranik         else if (*bootEnable == "Continuous")
769491d8ee7SSantosh Puranik         {
770491d8ee7SSantosh Puranik             oneTimeSetting = false;
771491d8ee7SSantosh Puranik         }
772491d8ee7SSantosh Puranik         else if (*bootEnable == "Disabled")
773491d8ee7SSantosh Puranik         {
774491d8ee7SSantosh Puranik             oneTimeSetting = false;
775491d8ee7SSantosh Puranik         }
776491d8ee7SSantosh Puranik         else
777491d8ee7SSantosh Puranik         {
778491d8ee7SSantosh Puranik 
779491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Unsupported value for "
780491d8ee7SSantosh Puranik                                 "BootSourceOverrideEnabled: "
781491d8ee7SSantosh Puranik                              << *bootEnable;
782491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootEnable,
783491d8ee7SSantosh Puranik                                              "BootSourceOverrideEnabled");
784491d8ee7SSantosh Puranik             return;
785491d8ee7SSantosh Puranik         }
786491d8ee7SSantosh Puranik     }
787491d8ee7SSantosh Puranik     std::string bootSourceStr;
788491d8ee7SSantosh Puranik     std::string bootModeStr;
789491d8ee7SSantosh Puranik     if (bootSource)
790491d8ee7SSantosh Puranik     {
791491d8ee7SSantosh Puranik         bootSourceStr = rfToDbusBootSource(*bootSource);
792491d8ee7SSantosh Puranik         bootModeStr = rfToDbusBootMode(*bootSource);
793491d8ee7SSantosh Puranik 
794491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
795491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
796491d8ee7SSantosh Puranik 
797491d8ee7SSantosh Puranik         if (bootSourceStr.empty() && bootModeStr.empty())
798491d8ee7SSantosh Puranik         {
799491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Invalid property value for "
800491d8ee7SSantosh Puranik                                 "BootSourceOverrideTarget: "
801491d8ee7SSantosh Puranik                              << *bootSource;
802491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootSource,
803491d8ee7SSantosh Puranik                                              "BootSourceTargetOverride");
804491d8ee7SSantosh Puranik             return;
805491d8ee7SSantosh Puranik         }
806491d8ee7SSantosh Puranik     }
807491d8ee7SSantosh Puranik     const char *bootObj =
808491d8ee7SSantosh Puranik         oneTimeSetting ? "/xyz/openbmc_project/control/host0/boot/one_time"
809491d8ee7SSantosh Puranik                        : "/xyz/openbmc_project/control/host0/boot";
810491d8ee7SSantosh Puranik     // Figure out what properties to set
811491d8ee7SSantosh Puranik     if (bootEnable && (*bootEnable == "Disabled"))
812491d8ee7SSantosh Puranik     {
813491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot source override will be disabled";
814491d8ee7SSantosh Puranik         // Request to only turn OFF/ON enabled, if turning enabled OFF, need
815491d8ee7SSantosh Puranik         // to reset the source and mode too. If turning it ON, we only need
816491d8ee7SSantosh Puranik         // to set the enabled property
817491d8ee7SSantosh Puranik         bootSourceStr =
818491d8ee7SSantosh Puranik             "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
819491d8ee7SSantosh Puranik         bootModeStr = "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
820491d8ee7SSantosh Puranik     }
821491d8ee7SSantosh Puranik     else if (bootSource)
822491d8ee7SSantosh Puranik     {
823491d8ee7SSantosh Puranik         // Source target specified
824491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "Boot source: " << *bootSource;
825491d8ee7SSantosh Puranik         // Figure out which DBUS interface and property to use
826491d8ee7SSantosh Puranik         bootSourceStr = rfToDbusBootSource(*bootSource);
827491d8ee7SSantosh Puranik         bootModeStr = rfToDbusBootMode(*bootSource);
828491d8ee7SSantosh Puranik 
829491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "DBUS boot source: " << bootSourceStr;
830491d8ee7SSantosh Puranik         BMCWEB_LOG_DEBUG << "DBUS boot mode: " << bootModeStr;
831491d8ee7SSantosh Puranik 
832491d8ee7SSantosh Puranik         if (bootSourceStr.empty() && bootModeStr.empty())
833491d8ee7SSantosh Puranik         {
834491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Invalid property value for "
835491d8ee7SSantosh Puranik                                 "BootSourceOverrideTarget: "
836491d8ee7SSantosh Puranik                              << *bootSource;
837491d8ee7SSantosh Puranik             messages::propertyValueNotInList(aResp->res, *bootSource,
838491d8ee7SSantosh Puranik                                              "BootSourceTargetOverride");
839491d8ee7SSantosh Puranik             return;
840491d8ee7SSantosh Puranik         }
841491d8ee7SSantosh Puranik 
842491d8ee7SSantosh Puranik         if (!bootSourceStr.empty())
843491d8ee7SSantosh Puranik         {
844491d8ee7SSantosh Puranik             // If setting to anything other than default, also reset boot
845491d8ee7SSantosh Puranik             // mode property
846491d8ee7SSantosh Puranik             if (bootSourceStr !=
847491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Source.Sources.Default")
848491d8ee7SSantosh Puranik             {
849491d8ee7SSantosh Puranik                 bootModeStr =
850491d8ee7SSantosh Puranik                     "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
851491d8ee7SSantosh Puranik             }
852491d8ee7SSantosh Puranik         }
853491d8ee7SSantosh Puranik         else // if (!bootModeStr.empty())
854491d8ee7SSantosh Puranik         {
855491d8ee7SSantosh Puranik             // If setting to anything other than default, also reset boot
856491d8ee7SSantosh Puranik             // source property
857491d8ee7SSantosh Puranik             if (bootModeStr !=
858491d8ee7SSantosh Puranik                 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular")
859491d8ee7SSantosh Puranik             {
860491d8ee7SSantosh Puranik                 bootSourceStr =
861491d8ee7SSantosh Puranik                     "xyz.openbmc_project.Control.Boot.Source.Sources.Default";
862491d8ee7SSantosh Puranik             }
863491d8ee7SSantosh Puranik         }
864491d8ee7SSantosh Puranik     }
865491d8ee7SSantosh Puranik     if (!bootSourceStr.empty())
866491d8ee7SSantosh Puranik     {
867491d8ee7SSantosh Puranik         crow::connections::systemBus->async_method_call(
868491d8ee7SSantosh Puranik             [aResp](const boost::system::error_code ec) {
869491d8ee7SSantosh Puranik                 if (ec)
870491d8ee7SSantosh Puranik                 {
871491d8ee7SSantosh Puranik                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
872491d8ee7SSantosh Puranik                     messages::internalError(aResp->res);
873491d8ee7SSantosh Puranik                     return;
874491d8ee7SSantosh Puranik                 }
875491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "Boot source update done.";
876491d8ee7SSantosh Puranik             },
877491d8ee7SSantosh Puranik             "xyz.openbmc_project.Settings", bootObj,
878491d8ee7SSantosh Puranik             "org.freedesktop.DBus.Properties", "Set",
879491d8ee7SSantosh Puranik             "xyz.openbmc_project.Control.Boot.Source", "BootSource",
880491d8ee7SSantosh Puranik             std::variant<std::string>(bootSourceStr));
881491d8ee7SSantosh Puranik     }
882491d8ee7SSantosh Puranik     if (!bootModeStr.empty())
883491d8ee7SSantosh Puranik     {
884491d8ee7SSantosh Puranik         crow::connections::systemBus->async_method_call(
885491d8ee7SSantosh Puranik             [aResp](const boost::system::error_code ec) {
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                 BMCWEB_LOG_DEBUG << "Boot mode update done.";
893491d8ee7SSantosh Puranik             },
894491d8ee7SSantosh Puranik             "xyz.openbmc_project.Settings", bootObj,
895491d8ee7SSantosh Puranik             "org.freedesktop.DBus.Properties", "Set",
896491d8ee7SSantosh Puranik             "xyz.openbmc_project.Control.Boot.Mode", "BootMode",
897491d8ee7SSantosh Puranik             std::variant<std::string>(bootModeStr));
898491d8ee7SSantosh Puranik     }
899491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
900491d8ee7SSantosh Puranik         [aResp{std::move(aResp)}](const boost::system::error_code ec) {
901491d8ee7SSantosh Puranik             if (ec)
902491d8ee7SSantosh Puranik             {
903491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
904491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
905491d8ee7SSantosh Puranik                 return;
906491d8ee7SSantosh Puranik             }
907491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Boot enable update done.";
908491d8ee7SSantosh Puranik         },
909491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
910491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
911491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
912491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled",
913491d8ee7SSantosh Puranik         std::variant<bool>(oneTimeSetting));
914491d8ee7SSantosh Puranik }
915491d8ee7SSantosh Puranik 
916491d8ee7SSantosh Puranik /**
917491d8ee7SSantosh Puranik  * @brief Retrieves "One time" enabled setting over DBUS and calls function to
918491d8ee7SSantosh Puranik  * set boot source/boot mode properties.
919491d8ee7SSantosh Puranik  *
920491d8ee7SSantosh Puranik  * @param[in] aResp      Shared pointer for generating response message.
921491d8ee7SSantosh Puranik  * @param[in] bootSource The boot source from incoming RF request.
922491d8ee7SSantosh Puranik  * @param[in] bootEnable The boot override enable from incoming RF request.
923491d8ee7SSantosh Puranik  *
924491d8ee7SSantosh Puranik  * @return None.
925491d8ee7SSantosh Puranik  */
926491d8ee7SSantosh Puranik static void setBootProperties(std::shared_ptr<AsyncResp> aResp,
927491d8ee7SSantosh Puranik                               std::optional<std::string> bootSource,
928491d8ee7SSantosh Puranik                               std::optional<std::string> bootEnable)
929491d8ee7SSantosh Puranik {
930491d8ee7SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set boot information.";
931491d8ee7SSantosh Puranik 
932491d8ee7SSantosh Puranik     crow::connections::systemBus->async_method_call(
933491d8ee7SSantosh Puranik         [aResp{std::move(aResp)}, bootSource{std::move(bootSource)},
934491d8ee7SSantosh Puranik          bootEnable{std::move(bootEnable)}](
935491d8ee7SSantosh Puranik             const boost::system::error_code ec,
936491d8ee7SSantosh Puranik             const sdbusplus::message::variant<bool> &oneTime) {
937491d8ee7SSantosh Puranik             if (ec)
938491d8ee7SSantosh Puranik             {
939491d8ee7SSantosh Puranik                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
940491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
941491d8ee7SSantosh Puranik                 return;
942491d8ee7SSantosh Puranik             }
943491d8ee7SSantosh Puranik 
944491d8ee7SSantosh Puranik             const bool *oneTimePtr = std::get_if<bool>(&oneTime);
945491d8ee7SSantosh Puranik 
946491d8ee7SSantosh Puranik             if (!oneTimePtr)
947491d8ee7SSantosh Puranik             {
948491d8ee7SSantosh Puranik                 messages::internalError(aResp->res);
949491d8ee7SSantosh Puranik                 return;
950491d8ee7SSantosh Puranik             }
951491d8ee7SSantosh Puranik 
952491d8ee7SSantosh Puranik             BMCWEB_LOG_DEBUG << "Got one time: " << *oneTimePtr;
953491d8ee7SSantosh Puranik 
954491d8ee7SSantosh Puranik             setBootModeOrSource(aResp, *oneTimePtr, std::move(bootSource),
955491d8ee7SSantosh Puranik                                 std::move(bootEnable));
956491d8ee7SSantosh Puranik         },
957491d8ee7SSantosh Puranik         "xyz.openbmc_project.Settings",
958491d8ee7SSantosh Puranik         "/xyz/openbmc_project/control/host0/boot/one_time",
959491d8ee7SSantosh Puranik         "org.freedesktop.DBus.Properties", "Get",
960491d8ee7SSantosh Puranik         "xyz.openbmc_project.Object.Enable", "Enabled");
961491d8ee7SSantosh Puranik }
962491d8ee7SSantosh Puranik 
963491d8ee7SSantosh Puranik /**
964c5b2abe0SLewanczyk, Dawid  * SystemsCollection derived class for delivering ComputerSystems Collection
965c5b2abe0SLewanczyk, Dawid  * Schema
966c5b2abe0SLewanczyk, Dawid  */
9671abe55efSEd Tanous class SystemsCollection : public Node
9681abe55efSEd Tanous {
969c5b2abe0SLewanczyk, Dawid   public:
9701abe55efSEd Tanous     SystemsCollection(CrowApp &app) : Node(app, "/redfish/v1/Systems/")
9711abe55efSEd Tanous     {
972c5b2abe0SLewanczyk, Dawid         entityPrivileges = {
973c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::get, {{"Login"}}},
974c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::head, {{"Login"}}},
975c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
976c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
977c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
978c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
979c5b2abe0SLewanczyk, Dawid     }
980c5b2abe0SLewanczyk, Dawid 
981c5b2abe0SLewanczyk, Dawid   private:
98255c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
9831abe55efSEd Tanous                const std::vector<std::string> &params) override
9841abe55efSEd Tanous     {
9850f74e643SEd Tanous         res.jsonValue["@odata.type"] =
9860f74e643SEd Tanous             "#ComputerSystemCollection.ComputerSystemCollection";
9870f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Systems";
9880f74e643SEd Tanous         res.jsonValue["@odata.context"] =
9890f74e643SEd Tanous             "/redfish/v1/"
9900f74e643SEd Tanous             "$metadata#ComputerSystemCollection.ComputerSystemCollection";
9910f74e643SEd Tanous         res.jsonValue["Name"] = "Computer System Collection";
992029573d4SEd Tanous         res.jsonValue["Members"] = {
993029573d4SEd Tanous             {{"@odata.id", "/redfish/v1/Systems/system"}}};
994029573d4SEd Tanous         res.jsonValue["Members@odata.count"] = 1;
995029573d4SEd Tanous         res.end();
996c5b2abe0SLewanczyk, Dawid     }
997c5b2abe0SLewanczyk, Dawid };
998c5b2abe0SLewanczyk, Dawid 
999c5b2abe0SLewanczyk, Dawid /**
1000cc340dd9SEd Tanous  * SystemActionsReset class supports handle POST method for Reset action.
1001cc340dd9SEd Tanous  * The class retrieves and sends data directly to D-Bus.
1002cc340dd9SEd Tanous  */
1003cc340dd9SEd Tanous class SystemActionsReset : public Node
1004cc340dd9SEd Tanous {
1005cc340dd9SEd Tanous   public:
1006cc340dd9SEd Tanous     SystemActionsReset(CrowApp &app) :
1007029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset/")
1008cc340dd9SEd Tanous     {
1009cc340dd9SEd Tanous         entityPrivileges = {
1010cc340dd9SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1011cc340dd9SEd Tanous     }
1012cc340dd9SEd Tanous 
1013cc340dd9SEd Tanous   private:
1014cc340dd9SEd Tanous     /**
1015cc340dd9SEd Tanous      * Function handles POST method request.
1016cc340dd9SEd Tanous      * Analyzes POST body message before sends Reset request data to D-Bus.
1017cc340dd9SEd Tanous      */
1018cc340dd9SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
1019cc340dd9SEd Tanous                 const std::vector<std::string> &params) override
1020cc340dd9SEd Tanous     {
1021cc340dd9SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1022cc340dd9SEd Tanous 
10239712f8acSEd Tanous         std::string resetType;
10249712f8acSEd Tanous         if (!json_util::readJson(req, res, "ResetType", resetType))
1025cc340dd9SEd Tanous         {
1026cc340dd9SEd Tanous             return;
1027cc340dd9SEd Tanous         }
1028cc340dd9SEd Tanous 
10299712f8acSEd Tanous         if (resetType == "ForceOff")
1030cc340dd9SEd Tanous         {
1031cc340dd9SEd Tanous             // Force off acts on the chassis
1032cc340dd9SEd Tanous             crow::connections::systemBus->async_method_call(
1033cc340dd9SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
1034cc340dd9SEd Tanous                     if (ec)
1035cc340dd9SEd Tanous                     {
10369712f8acSEd Tanous                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1037f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
1038cc340dd9SEd Tanous                         return;
1039cc340dd9SEd Tanous                     }
1040cc340dd9SEd Tanous                     // TODO Consider support polling mechanism to verify
1041cc340dd9SEd Tanous                     // status of host and chassis after execute the
1042cc340dd9SEd Tanous                     // requested action.
1043f12894f8SJason M. Bills                     messages::success(asyncResp->res);
1044cc340dd9SEd Tanous                 },
1045cc340dd9SEd Tanous                 "xyz.openbmc_project.State.Chassis",
1046cc340dd9SEd Tanous                 "/xyz/openbmc_project/state/chassis0",
1047cc340dd9SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
10489712f8acSEd Tanous                 "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition",
1049abf2add6SEd Tanous                 std::variant<std::string>{
10509712f8acSEd Tanous                     "xyz.openbmc_project.State.Chassis.Transition.Off"});
1051cc340dd9SEd Tanous             return;
1052cc340dd9SEd Tanous         }
1053cc340dd9SEd Tanous         // all other actions operate on the host
1054cc340dd9SEd Tanous         std::string command;
1055cc340dd9SEd Tanous         // Execute Reset Action regarding to each reset type.
10569712f8acSEd Tanous         if (resetType == "On")
1057cc340dd9SEd Tanous         {
1058cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.On";
1059cc340dd9SEd Tanous         }
10609712f8acSEd Tanous         else if (resetType == "GracefulShutdown")
1061cc340dd9SEd Tanous         {
1062cc340dd9SEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.Off";
1063cc340dd9SEd Tanous         }
10649712f8acSEd Tanous         else if (resetType == "GracefulRestart")
1065cc340dd9SEd Tanous         {
10669712f8acSEd Tanous             command = "xyz.openbmc_project.State.Host.Transition.Reboot";
1067cc340dd9SEd Tanous         }
1068cc340dd9SEd Tanous         else
1069cc340dd9SEd Tanous         {
1070f12894f8SJason M. Bills             messages::actionParameterUnknown(res, "Reset", resetType);
1071cc340dd9SEd Tanous             return;
1072cc340dd9SEd Tanous         }
1073cc340dd9SEd Tanous 
1074cc340dd9SEd Tanous         crow::connections::systemBus->async_method_call(
1075cc340dd9SEd Tanous             [asyncResp](const boost::system::error_code ec) {
1076cc340dd9SEd Tanous                 if (ec)
1077cc340dd9SEd Tanous                 {
1078cc340dd9SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1079f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
1080cc340dd9SEd Tanous                     return;
1081cc340dd9SEd Tanous                 }
1082cc340dd9SEd Tanous                 // TODO Consider support polling mechanism to verify
1083cc340dd9SEd Tanous                 // status of host and chassis after execute the
1084cc340dd9SEd Tanous                 // requested action.
1085f12894f8SJason M. Bills                 messages::success(asyncResp->res);
1086cc340dd9SEd Tanous             },
1087cc340dd9SEd Tanous             "xyz.openbmc_project.State.Host",
1088cc340dd9SEd Tanous             "/xyz/openbmc_project/state/host0",
1089cc340dd9SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
10909712f8acSEd Tanous             "xyz.openbmc_project.State.Host", "RequestedHostTransition",
1091abf2add6SEd Tanous             std::variant<std::string>{command});
1092cc340dd9SEd Tanous     }
1093cc340dd9SEd Tanous };
1094cc340dd9SEd Tanous 
1095cc340dd9SEd Tanous /**
10966617338dSEd Tanous  * Systems derived class for delivering Computer Systems Schema.
1097c5b2abe0SLewanczyk, Dawid  */
10981abe55efSEd Tanous class Systems : public Node
10991abe55efSEd Tanous {
1100c5b2abe0SLewanczyk, Dawid   public:
1101c5b2abe0SLewanczyk, Dawid     /*
1102c5b2abe0SLewanczyk, Dawid      * Default Constructor
1103c5b2abe0SLewanczyk, Dawid      */
1104029573d4SEd Tanous     Systems(CrowApp &app) : Node(app, "/redfish/v1/Systems/system/")
11051abe55efSEd Tanous     {
1106c5b2abe0SLewanczyk, Dawid         entityPrivileges = {
1107c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::get, {{"Login"}}},
1108c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::head, {{"Login"}}},
1109c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1110c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1111c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1112c5b2abe0SLewanczyk, Dawid             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1113c5b2abe0SLewanczyk, Dawid     }
1114c5b2abe0SLewanczyk, Dawid 
1115c5b2abe0SLewanczyk, Dawid   private:
1116c5b2abe0SLewanczyk, Dawid     /**
1117c5b2abe0SLewanczyk, Dawid      * Functions triggers appropriate requests on DBus
1118c5b2abe0SLewanczyk, Dawid      */
111955c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
11201abe55efSEd Tanous                const std::vector<std::string> &params) override
11211abe55efSEd Tanous     {
1122491d8ee7SSantosh Puranik         res.jsonValue["@odata.type"] = "#ComputerSystem.v1_6_0.ComputerSystem";
11230f74e643SEd Tanous         res.jsonValue["@odata.context"] =
11240f74e643SEd Tanous             "/redfish/v1/$metadata#ComputerSystem.ComputerSystem";
1125029573d4SEd Tanous         res.jsonValue["Name"] = "Computer System";
1126029573d4SEd Tanous         res.jsonValue["Id"] = "system";
11270f74e643SEd Tanous         res.jsonValue["SystemType"] = "Physical";
11280f74e643SEd Tanous         res.jsonValue["Description"] = "Computer System";
11290f74e643SEd Tanous         res.jsonValue["ProcessorSummary"]["Count"] = 0;
11300f74e643SEd Tanous         res.jsonValue["ProcessorSummary"]["Status"]["State"] = "Disabled";
11310f74e643SEd Tanous         res.jsonValue["MemorySummary"]["TotalSystemMemoryGiB"] = int(0);
11320f74e643SEd Tanous         res.jsonValue["MemorySummary"]["Status"]["State"] = "Disabled";
1133029573d4SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Systems/system";
113404a258f4SEd Tanous 
1135443c2934SRapkiewicz, Pawel         res.jsonValue["Processors"] = {
1136029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/Processors"}};
1137443c2934SRapkiewicz, Pawel         res.jsonValue["Memory"] = {
1138029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/Memory"}};
1139029573d4SEd Tanous 
1140cc340dd9SEd Tanous         // TODO Need to support ForceRestart.
1141cc340dd9SEd Tanous         res.jsonValue["Actions"]["#ComputerSystem.Reset"] = {
1142cc340dd9SEd Tanous             {"target",
1143029573d4SEd Tanous              "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset"},
1144cc340dd9SEd Tanous             {"ResetType@Redfish.AllowableValues",
1145cc340dd9SEd Tanous              {"On", "ForceOff", "GracefulRestart", "GracefulShutdown"}}};
1146c5b2abe0SLewanczyk, Dawid 
1147c4bf6374SJason M. Bills         res.jsonValue["LogServices"] = {
1148029573d4SEd Tanous             {"@odata.id", "/redfish/v1/Systems/system/LogServices"}};
1149c4bf6374SJason M. Bills 
1150a0803efaSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1151c5b2abe0SLewanczyk, Dawid 
11526c34de48SEd Tanous         getLedGroupIdentify(
1153a0803efaSEd Tanous             asyncResp,
1154a0803efaSEd Tanous             [&](const bool &asserted, const std::shared_ptr<AsyncResp> &aResp) {
11551abe55efSEd Tanous                 if (asserted)
11561abe55efSEd Tanous                 {
1157c5b2abe0SLewanczyk, Dawid                     // If led group is asserted, then another call is needed to
1158c5b2abe0SLewanczyk, Dawid                     // get led status
11596c34de48SEd Tanous                     getLedIdentify(
1160a0803efaSEd Tanous                         aResp, [](const std::string &ledStatus,
1161a0803efaSEd Tanous                                   const std::shared_ptr<AsyncResp> &aResp) {
11621abe55efSEd Tanous                             if (!ledStatus.empty())
11631abe55efSEd Tanous                             {
11641abe55efSEd Tanous                                 aResp->res.jsonValue["IndicatorLED"] =
11651abe55efSEd Tanous                                     ledStatus;
1166c5b2abe0SLewanczyk, Dawid                             }
1167c5b2abe0SLewanczyk, Dawid                         });
11681abe55efSEd Tanous                 }
11691abe55efSEd Tanous                 else
11701abe55efSEd Tanous                 {
117155c7b7a2SEd Tanous                     aResp->res.jsonValue["IndicatorLED"] = "Off";
1172c5b2abe0SLewanczyk, Dawid                 }
1173c5b2abe0SLewanczyk, Dawid             });
1174029573d4SEd Tanous         getComputerSystem(asyncResp);
11756c34de48SEd Tanous         getHostState(asyncResp);
1176491d8ee7SSantosh Puranik         getBootProperties(asyncResp);
1177c5b2abe0SLewanczyk, Dawid     }
1178c5b2abe0SLewanczyk, Dawid 
117955c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
11801abe55efSEd Tanous                  const std::vector<std::string> &params) override
11811abe55efSEd Tanous     {
1182cde19e5fSSantosh Puranik         std::optional<std::string> indicatorLed;
1183491d8ee7SSantosh Puranik         std::optional<nlohmann::json> bootProps;
1184491d8ee7SSantosh Puranik         if (!json_util::readJson(req, res, "IndicatorLED", indicatorLed, "Boot",
1185491d8ee7SSantosh Puranik                                  bootProps))
11866617338dSEd Tanous         {
11876617338dSEd Tanous             return;
11886617338dSEd Tanous         }
1189491d8ee7SSantosh Puranik 
1190029573d4SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1191029573d4SEd Tanous         messages::success(asyncResp->res);
1192491d8ee7SSantosh Puranik 
1193491d8ee7SSantosh Puranik         if (bootProps)
1194491d8ee7SSantosh Puranik         {
1195491d8ee7SSantosh Puranik             std::optional<std::string> bootSource;
1196491d8ee7SSantosh Puranik             std::optional<std::string> bootEnable;
1197491d8ee7SSantosh Puranik 
1198491d8ee7SSantosh Puranik             if (!json_util::readJson(*bootProps, asyncResp->res,
1199491d8ee7SSantosh Puranik                                      "BootSourceOverrideTarget", bootSource,
1200491d8ee7SSantosh Puranik                                      "BootSourceOverrideEnabled", bootEnable))
1201491d8ee7SSantosh Puranik             {
1202491d8ee7SSantosh Puranik                 return;
1203491d8ee7SSantosh Puranik             }
1204491d8ee7SSantosh Puranik             setBootProperties(asyncResp, std::move(bootSource),
1205491d8ee7SSantosh Puranik                               std::move(bootEnable));
1206491d8ee7SSantosh Puranik         }
12079712f8acSEd Tanous         if (indicatorLed)
12086617338dSEd Tanous         {
12099712f8acSEd Tanous             std::string dbusLedState;
12109712f8acSEd Tanous             if (*indicatorLed == "On")
12119712f8acSEd Tanous             {
12129712f8acSEd Tanous                 dbusLedState = "xyz.openbmc_project.Led.Physical.Action.Lit";
12136617338dSEd Tanous             }
1214*5c6221acSGunnar Mills             else if (*indicatorLed == "Blinking")
12156617338dSEd Tanous             {
1216*5c6221acSGunnar Mills                 dbusLedState = "xyz.openbmc_project.Led.Physical.Action.Blink";
12176617338dSEd Tanous             }
12189712f8acSEd Tanous             else if (*indicatorLed == "Off")
12196617338dSEd Tanous             {
12209712f8acSEd Tanous                 dbusLedState = "xyz.openbmc_project.Led.Physical.Action.Off";
12216617338dSEd Tanous             }
12226617338dSEd Tanous             else
12236617338dSEd Tanous             {
1224a08b46ccSJason M. Bills                 messages::propertyValueNotInList(res, *indicatorLed,
1225a08b46ccSJason M. Bills                                                  "IndicatorLED");
12266617338dSEd Tanous                 return;
12276617338dSEd Tanous             }
12286617338dSEd Tanous 
12296c34de48SEd Tanous             getHostState(asyncResp);
1230029573d4SEd Tanous             getComputerSystem(asyncResp);
1231c5b2abe0SLewanczyk, Dawid 
1232c5b2abe0SLewanczyk, Dawid             // Update led group
123355c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Update led group.";
123455c7b7a2SEd Tanous             crow::connections::systemBus->async_method_call(
1235cde19e5fSSantosh Puranik                 [asyncResp](const boost::system::error_code ec) {
12361abe55efSEd Tanous                     if (ec)
12371abe55efSEd Tanous                     {
123855c7b7a2SEd Tanous                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1239f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
1240c5b2abe0SLewanczyk, Dawid                         return;
1241c5b2abe0SLewanczyk, Dawid                     }
124255c7b7a2SEd Tanous                     BMCWEB_LOG_DEBUG << "Led group update done.";
1243c5b2abe0SLewanczyk, Dawid                 },
1244c5b2abe0SLewanczyk, Dawid                 "xyz.openbmc_project.LED.GroupManager",
1245c5b2abe0SLewanczyk, Dawid                 "/xyz/openbmc_project/led/groups/enclosure_identify",
1246c5b2abe0SLewanczyk, Dawid                 "org.freedesktop.DBus.Properties", "Set",
1247c5b2abe0SLewanczyk, Dawid                 "xyz.openbmc_project.Led.Group", "Asserted",
1248abf2add6SEd Tanous                 std::variant<bool>(
12496617338dSEd Tanous                     (dbusLedState ==
12506617338dSEd Tanous                              "xyz.openbmc_project.Led.Physical.Action.Off"
12516617338dSEd Tanous                          ? false
12526617338dSEd Tanous                          : true)));
1253c5b2abe0SLewanczyk, Dawid             // Update identify led status
125455c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Update led SoftwareInventoryCollection.";
125555c7b7a2SEd Tanous             crow::connections::systemBus->async_method_call(
12566617338dSEd Tanous                 [asyncResp{std::move(asyncResp)},
12579712f8acSEd Tanous                  indicatorLed{std::move(*indicatorLed)}](
1258c5b2abe0SLewanczyk, Dawid                     const boost::system::error_code ec) {
12591abe55efSEd Tanous                     if (ec)
12601abe55efSEd Tanous                     {
126155c7b7a2SEd Tanous                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1262f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
1263c5b2abe0SLewanczyk, Dawid                         return;
1264c5b2abe0SLewanczyk, Dawid                     }
126555c7b7a2SEd Tanous                     BMCWEB_LOG_DEBUG << "Led state update done.";
1266c5b2abe0SLewanczyk, Dawid                 },
1267c5b2abe0SLewanczyk, Dawid                 "xyz.openbmc_project.LED.Controller.identify",
1268c5b2abe0SLewanczyk, Dawid                 "/xyz/openbmc_project/led/physical/identify",
1269c5b2abe0SLewanczyk, Dawid                 "org.freedesktop.DBus.Properties", "Set",
1270c5b2abe0SLewanczyk, Dawid                 "xyz.openbmc_project.Led.Physical", "State",
1271abf2add6SEd Tanous                 std::variant<std::string>(dbusLedState));
12726617338dSEd Tanous         }
1273c5b2abe0SLewanczyk, Dawid     }
1274c5b2abe0SLewanczyk, Dawid };
1275c5b2abe0SLewanczyk, Dawid } // namespace redfish
1276