xref: /openbmc/bmcweb/features/redfish/lib/chassis.hpp (revision 9f8bfa7c6deb3808e3679d332479311dc4202819)
1e37f8451SRapkiewicz, Pawel /*
2e37f8451SRapkiewicz, Pawel // Copyright (c) 2018 Intel Corporation
3e37f8451SRapkiewicz, Pawel //
4e37f8451SRapkiewicz, Pawel // Licensed under the Apache License, Version 2.0 (the "License");
5e37f8451SRapkiewicz, Pawel // you may not use this file except in compliance with the License.
6e37f8451SRapkiewicz, Pawel // You may obtain a copy of the License at
7e37f8451SRapkiewicz, Pawel //
8e37f8451SRapkiewicz, Pawel //      http://www.apache.org/licenses/LICENSE-2.0
9e37f8451SRapkiewicz, Pawel //
10e37f8451SRapkiewicz, Pawel // Unless required by applicable law or agreed to in writing, software
11e37f8451SRapkiewicz, Pawel // distributed under the License is distributed on an "AS IS" BASIS,
12e37f8451SRapkiewicz, Pawel // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e37f8451SRapkiewicz, Pawel // See the License for the specific language governing permissions and
14e37f8451SRapkiewicz, Pawel // limitations under the License.
15e37f8451SRapkiewicz, Pawel */
16e37f8451SRapkiewicz, Pawel #pragma once
17e37f8451SRapkiewicz, Pawel 
18b49ac873SJames Feist #include "health.hpp"
191c8fba97SJames Feist #include "led.hpp"
20e37f8451SRapkiewicz, Pawel #include "node.hpp"
211abe55efSEd Tanous 
22e37f8451SRapkiewicz, Pawel #include <boost/container/flat_map.hpp>
2302f6ff19SGunnar Mills #include <utils/collection.hpp>
241214b7e7SGunnar Mills 
25abf2add6SEd Tanous #include <variant>
26e37f8451SRapkiewicz, Pawel 
271abe55efSEd Tanous namespace redfish
281abe55efSEd Tanous {
29e37f8451SRapkiewicz, Pawel 
30e37f8451SRapkiewicz, Pawel /**
31beeca0aeSGunnar Mills  * @brief Retrieves chassis state properties over dbus
32beeca0aeSGunnar Mills  *
33beeca0aeSGunnar Mills  * @param[in] aResp - Shared pointer for completing asynchronous calls.
34beeca0aeSGunnar Mills  *
35beeca0aeSGunnar Mills  * @return None.
36beeca0aeSGunnar Mills  */
3723a21a1cSEd Tanous inline void getChassisState(std::shared_ptr<AsyncResp> aResp)
38beeca0aeSGunnar Mills {
39beeca0aeSGunnar Mills     crow::connections::systemBus->async_method_call(
40beeca0aeSGunnar Mills         [aResp{std::move(aResp)}](
41beeca0aeSGunnar Mills             const boost::system::error_code ec,
42beeca0aeSGunnar Mills             const std::variant<std::string>& chassisState) {
43beeca0aeSGunnar Mills             if (ec)
44beeca0aeSGunnar Mills             {
45beeca0aeSGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
46beeca0aeSGunnar Mills                 messages::internalError(aResp->res);
47beeca0aeSGunnar Mills                 return;
48beeca0aeSGunnar Mills             }
49beeca0aeSGunnar Mills 
50beeca0aeSGunnar Mills             const std::string* s = std::get_if<std::string>(&chassisState);
51beeca0aeSGunnar Mills             BMCWEB_LOG_DEBUG << "Chassis state: " << *s;
52beeca0aeSGunnar Mills             if (s != nullptr)
53beeca0aeSGunnar Mills             {
54beeca0aeSGunnar Mills                 // Verify Chassis State
55beeca0aeSGunnar Mills                 if (*s == "xyz.openbmc_project.State.Chassis.PowerState.On")
56beeca0aeSGunnar Mills                 {
57beeca0aeSGunnar Mills                     aResp->res.jsonValue["PowerState"] = "On";
58beeca0aeSGunnar Mills                     aResp->res.jsonValue["Status"]["State"] = "Enabled";
59beeca0aeSGunnar Mills                 }
60beeca0aeSGunnar Mills                 else if (*s ==
61beeca0aeSGunnar Mills                          "xyz.openbmc_project.State.Chassis.PowerState.Off")
62beeca0aeSGunnar Mills                 {
63beeca0aeSGunnar Mills                     aResp->res.jsonValue["PowerState"] = "Off";
64beeca0aeSGunnar Mills                     aResp->res.jsonValue["Status"]["State"] = "StandbyOffline";
65beeca0aeSGunnar Mills                 }
66beeca0aeSGunnar Mills             }
67beeca0aeSGunnar Mills         },
68beeca0aeSGunnar Mills         "xyz.openbmc_project.State.Chassis",
69beeca0aeSGunnar Mills         "/xyz/openbmc_project/state/chassis0",
70beeca0aeSGunnar Mills         "org.freedesktop.DBus.Properties", "Get",
71beeca0aeSGunnar Mills         "xyz.openbmc_project.State.Chassis", "CurrentPowerState");
72beeca0aeSGunnar Mills }
73beeca0aeSGunnar Mills 
74beeca0aeSGunnar Mills /**
75e37f8451SRapkiewicz, Pawel  * DBus types primitives for several generic DBus interfaces
76e37f8451SRapkiewicz, Pawel  * TODO(Pawel) consider move this to separate file into boost::dbus
77e37f8451SRapkiewicz, Pawel  */
7855c7b7a2SEd Tanous // Note, this is not a very useful Variant, but because it isn't used to get
79aa2e59c1SEd Tanous // values, it should be as simple as possible
80aa2e59c1SEd Tanous // TODO(ed) invent a nullvariant type
815fd7ba65SCheng C Yang using VariantType = std::variant<bool, std::string, uint64_t, uint32_t>;
82aa2e59c1SEd Tanous using ManagedObjectsType = std::vector<std::pair<
83aa2e59c1SEd Tanous     sdbusplus::message::object_path,
84aa2e59c1SEd Tanous     std::vector<std::pair<std::string,
85aa2e59c1SEd Tanous                           std::vector<std::pair<std::string, VariantType>>>>>>;
86e37f8451SRapkiewicz, Pawel 
87aa2e59c1SEd Tanous using PropertiesType = boost::container::flat_map<std::string, VariantType>;
88e37f8451SRapkiewicz, Pawel 
8923a21a1cSEd Tanous inline void getIntrusionByService(std::shared_ptr<AsyncResp> aResp,
90c181942fSQiang XU                                   const std::string& service,
91c181942fSQiang XU                                   const std::string& objPath)
92c181942fSQiang XU {
93c181942fSQiang XU     BMCWEB_LOG_DEBUG << "Get intrusion status by service \n";
94c181942fSQiang XU 
95c181942fSQiang XU     crow::connections::systemBus->async_method_call(
96c181942fSQiang XU         [aResp{std::move(aResp)}](const boost::system::error_code ec,
97c181942fSQiang XU                                   const std::variant<std::string>& value) {
98c181942fSQiang XU             if (ec)
99c181942fSQiang XU             {
1004e0453b1SGunnar Mills                 // do not add err msg in redfish response, because this is not
101c181942fSQiang XU                 //     mandatory property
102c181942fSQiang XU                 BMCWEB_LOG_ERROR << "DBUS response error " << ec << "\n";
103c181942fSQiang XU                 return;
104c181942fSQiang XU             }
105c181942fSQiang XU 
106c181942fSQiang XU             const std::string* status = std::get_if<std::string>(&value);
107c181942fSQiang XU 
108c181942fSQiang XU             if (status == nullptr)
109c181942fSQiang XU             {
110c181942fSQiang XU                 BMCWEB_LOG_ERROR << "intrusion status read error \n";
111c181942fSQiang XU                 return;
112c181942fSQiang XU             }
113c181942fSQiang XU 
114c181942fSQiang XU             aResp->res.jsonValue["PhysicalSecurity"] = {
115c181942fSQiang XU                 {"IntrusionSensorNumber", 1}, {"IntrusionSensor", *status}};
116c181942fSQiang XU         },
117c181942fSQiang XU         service, objPath, "org.freedesktop.DBus.Properties", "Get",
118c181942fSQiang XU         "xyz.openbmc_project.Chassis.Intrusion", "Status");
119c181942fSQiang XU }
120c181942fSQiang XU 
121c181942fSQiang XU /**
122c181942fSQiang XU  * Retrieves physical security properties over dbus
123c181942fSQiang XU  */
12423a21a1cSEd Tanous inline void getPhysicalSecurityData(std::shared_ptr<AsyncResp> aResp)
125c181942fSQiang XU {
126c181942fSQiang XU     crow::connections::systemBus->async_method_call(
127c181942fSQiang XU         [aResp{std::move(aResp)}](
128c181942fSQiang XU             const boost::system::error_code ec,
129c181942fSQiang XU             const std::vector<std::pair<
130c181942fSQiang XU                 std::string,
1311214b7e7SGunnar Mills                 std::vector<std::pair<std::string, std::vector<std::string>>>>>&
1321214b7e7SGunnar Mills                 subtree) {
133c181942fSQiang XU             if (ec)
134c181942fSQiang XU             {
1354e0453b1SGunnar Mills                 // do not add err msg in redfish response, because this is not
136c181942fSQiang XU                 //     mandatory property
137c181942fSQiang XU                 BMCWEB_LOG_ERROR << "DBUS error: no matched iface " << ec
138c181942fSQiang XU                                  << "\n";
139c181942fSQiang XU                 return;
140c181942fSQiang XU             }
141c181942fSQiang XU             // Iterate over all retrieved ObjectPaths.
142c181942fSQiang XU             for (const auto& object : subtree)
143c181942fSQiang XU             {
144c181942fSQiang XU                 for (const auto& service : object.second)
145c181942fSQiang XU                 {
146c181942fSQiang XU                     getIntrusionByService(aResp, service.first, object.first);
147c181942fSQiang XU                     return;
148c181942fSQiang XU                 }
149c181942fSQiang XU             }
150c181942fSQiang XU         },
151c181942fSQiang XU         "xyz.openbmc_project.ObjectMapper",
152c181942fSQiang XU         "/xyz/openbmc_project/object_mapper",
153c181942fSQiang XU         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
154271584abSEd Tanous         "/xyz/openbmc_project/Intrusion", 1,
155c181942fSQiang XU         std::array<const char*, 1>{"xyz.openbmc_project.Chassis.Intrusion"});
156c181942fSQiang XU }
157c181942fSQiang XU 
158e37f8451SRapkiewicz, Pawel /**
159e37f8451SRapkiewicz, Pawel  * ChassisCollection derived class for delivering Chassis Collection Schema
160e37f8451SRapkiewicz, Pawel  */
1611abe55efSEd Tanous class ChassisCollection : public Node
1621abe55efSEd Tanous {
163e37f8451SRapkiewicz, Pawel   public:
16452cc112dSEd Tanous     ChassisCollection(App& app) : Node(app, "/redfish/v1/Chassis/")
1651abe55efSEd Tanous     {
166e0d918bcSEd Tanous         entityPrivileges = {
167e0d918bcSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
168e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
169e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
170e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
171e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
172e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
173e37f8451SRapkiewicz, Pawel     }
174e37f8451SRapkiewicz, Pawel 
175e37f8451SRapkiewicz, Pawel   private:
176e37f8451SRapkiewicz, Pawel     /**
177e37f8451SRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
178e37f8451SRapkiewicz, Pawel      */
179cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
180cb13a392SEd Tanous                const std::vector<std::string>&) override
1811abe55efSEd Tanous     {
1820f74e643SEd Tanous         res.jsonValue["@odata.type"] = "#ChassisCollection.ChassisCollection";
1830f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
1840f74e643SEd Tanous         res.jsonValue["Name"] = "Chassis Collection";
1850f74e643SEd Tanous 
18662d5e2e4SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
187e37f8451SRapkiewicz, Pawel 
18802f6ff19SGunnar Mills         collection_util::getCollectionMembers(
18902f6ff19SGunnar Mills             asyncResp, "/redfish/v1/Chassis",
19002f6ff19SGunnar Mills             {"xyz.openbmc_project.Inventory.Item.Board",
19102f6ff19SGunnar Mills              "xyz.openbmc_project.Inventory.Item.Chassis"});
19262d5e2e4SEd Tanous     }
193e37f8451SRapkiewicz, Pawel };
194e37f8451SRapkiewicz, Pawel 
195e37f8451SRapkiewicz, Pawel /**
196e37f8451SRapkiewicz, Pawel  * Chassis override class for delivering Chassis Schema
197e37f8451SRapkiewicz, Pawel  */
1981abe55efSEd Tanous class Chassis : public Node
1991abe55efSEd Tanous {
200e37f8451SRapkiewicz, Pawel   public:
20152cc112dSEd Tanous     Chassis(App& app) : Node(app, "/redfish/v1/Chassis/<str>/", std::string())
2021abe55efSEd Tanous     {
203e0d918bcSEd Tanous         entityPrivileges = {
204e0d918bcSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
205e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
206e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
207e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
208e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
209e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
210e37f8451SRapkiewicz, Pawel     }
211e37f8451SRapkiewicz, Pawel 
212e37f8451SRapkiewicz, Pawel   private:
213e37f8451SRapkiewicz, Pawel     /**
214e37f8451SRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
215e37f8451SRapkiewicz, Pawel      */
216cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
2171abe55efSEd Tanous                const std::vector<std::string>& params) override
2181abe55efSEd Tanous     {
219adc4f0dbSShawn McCarney         const std::array<const char*, 2> interfaces = {
220734bfe90SGunnar Mills             "xyz.openbmc_project.Inventory.Item.Board",
221adc4f0dbSShawn McCarney             "xyz.openbmc_project.Inventory.Item.Chassis"};
222734bfe90SGunnar Mills 
223e37f8451SRapkiewicz, Pawel         // Check if there is required param, truly entering this shall be
224e37f8451SRapkiewicz, Pawel         // impossible.
2251abe55efSEd Tanous         if (params.size() != 1)
2261abe55efSEd Tanous         {
227f12894f8SJason M. Bills             messages::internalError(res);
228e37f8451SRapkiewicz, Pawel             res.end();
229e37f8451SRapkiewicz, Pawel             return;
230e37f8451SRapkiewicz, Pawel         }
23199cffd7fSShawn McCarney         const std::string& chassisId = params[0];
232e37f8451SRapkiewicz, Pawel 
23362d5e2e4SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
23455c7b7a2SEd Tanous         crow::connections::systemBus->async_method_call(
23562d5e2e4SEd Tanous             [asyncResp, chassisId(std::string(chassisId))](
23662d5e2e4SEd Tanous                 const boost::system::error_code ec,
2371c8fba97SJames Feist                 const crow::openbmc_mapper::GetSubTreeType& subtree) {
23862d5e2e4SEd Tanous                 if (ec)
2391abe55efSEd Tanous                 {
240f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
241daf36e2eSEd Tanous                     return;
242daf36e2eSEd Tanous                 }
243daf36e2eSEd Tanous                 // Iterate over all retrieved ObjectPaths.
2441abe55efSEd Tanous                 for (const std::pair<
2451abe55efSEd Tanous                          std::string,
2461abe55efSEd Tanous                          std::vector<
2471214b7e7SGunnar Mills                              std::pair<std::string, std::vector<std::string>>>>&
2481214b7e7SGunnar Mills                          object : subtree)
2491abe55efSEd Tanous                 {
250daf36e2eSEd Tanous                     const std::string& path = object.first;
2511abe55efSEd Tanous                     const std::vector<
2521214b7e7SGunnar Mills                         std::pair<std::string, std::vector<std::string>>>&
2531214b7e7SGunnar Mills                         connectionNames = object.second;
254e0d918bcSEd Tanous 
2551abe55efSEd Tanous                     if (!boost::ends_with(path, chassisId))
2561abe55efSEd Tanous                     {
257daf36e2eSEd Tanous                         continue;
258daf36e2eSEd Tanous                     }
25926f03899SShawn McCarney 
260b49ac873SJames Feist                     auto health = std::make_shared<HealthPopulate>(asyncResp);
261b49ac873SJames Feist 
262b49ac873SJames Feist                     crow::connections::systemBus->async_method_call(
26323a21a1cSEd Tanous                         [health](const boost::system::error_code ec2,
264b49ac873SJames Feist                                  std::variant<std::vector<std::string>>& resp) {
26523a21a1cSEd Tanous                             if (ec2)
266b49ac873SJames Feist                             {
267b49ac873SJames Feist                                 return; // no sensors = no failures
268b49ac873SJames Feist                             }
269b49ac873SJames Feist                             std::vector<std::string>* data =
270b49ac873SJames Feist                                 std::get_if<std::vector<std::string>>(&resp);
271b49ac873SJames Feist                             if (data == nullptr)
272b49ac873SJames Feist                             {
273b49ac873SJames Feist                                 return;
274b49ac873SJames Feist                             }
275b49ac873SJames Feist                             health->inventory = std::move(*data);
276b49ac873SJames Feist                         },
277b49ac873SJames Feist                         "xyz.openbmc_project.ObjectMapper",
278b49ac873SJames Feist                         path + "/all_sensors",
279b49ac873SJames Feist                         "org.freedesktop.DBus.Properties", "Get",
280b49ac873SJames Feist                         "xyz.openbmc_project.Association", "endpoints");
281b49ac873SJames Feist 
282b49ac873SJames Feist                     health->populate();
283b49ac873SJames Feist 
2841abe55efSEd Tanous                     if (connectionNames.size() < 1)
2851abe55efSEd Tanous                     {
2861c8fba97SJames Feist                         BMCWEB_LOG_ERROR << "Got 0 Connection names";
287e0d918bcSEd Tanous                         continue;
288daf36e2eSEd Tanous                     }
289e0d918bcSEd Tanous 
29049c53ac9SJohnathan Mantey                     asyncResp->res.jsonValue["@odata.type"] =
291*9f8bfa7cSGunnar Mills                         "#Chassis.v1_14_0.Chassis";
29249c53ac9SJohnathan Mantey                     asyncResp->res.jsonValue["@odata.id"] =
29349c53ac9SJohnathan Mantey                         "/redfish/v1/Chassis/" + chassisId;
29449c53ac9SJohnathan Mantey                     asyncResp->res.jsonValue["Name"] = "Chassis Collection";
29549c53ac9SJohnathan Mantey                     asyncResp->res.jsonValue["ChassisType"] = "RackMount";
296dd99e04bSP.K. Lee                     asyncResp->res.jsonValue["Actions"]["#Chassis.Reset"] = {
297dd99e04bSP.K. Lee                         {"target", "/redfish/v1/Chassis/" + chassisId +
298dd99e04bSP.K. Lee                                        "/Actions/Chassis.Reset"},
2991cb1a9e6SAppaRao Puli                         {"@Redfish.ActionInfo", "/redfish/v1/Chassis/" +
3001cb1a9e6SAppaRao Puli                                                     chassisId +
3011cb1a9e6SAppaRao Puli                                                     "/ResetActionInfo"}};
302adbe192aSJason M. Bills                     asyncResp->res.jsonValue["PCIeDevices"] = {
303adbe192aSJason M. Bills                         {"@odata.id",
304adbe192aSJason M. Bills                          "/redfish/v1/Systems/system/PCIeDevices"}};
30549c53ac9SJohnathan Mantey 
30649c53ac9SJohnathan Mantey                     const std::string& connectionName =
30749c53ac9SJohnathan Mantey                         connectionNames[0].first;
3081c8fba97SJames Feist 
30923a21a1cSEd Tanous                     const std::vector<std::string>& interfaces2 =
3101c8fba97SJames Feist                         connectionNames[0].second;
3111c8fba97SJames Feist                     const std::array<const char*, 2> hasIndicatorLed = {
3121c8fba97SJames Feist                         "xyz.openbmc_project.Inventory.Item.Panel",
3131c8fba97SJames Feist                         "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
3141c8fba97SJames Feist 
3151c8fba97SJames Feist                     for (const char* interface : hasIndicatorLed)
3161c8fba97SJames Feist                     {
31723a21a1cSEd Tanous                         if (std::find(interfaces2.begin(), interfaces2.end(),
31823a21a1cSEd Tanous                                       interface) != interfaces2.end())
3191c8fba97SJames Feist                         {
3201c8fba97SJames Feist                             getIndicatorLedState(asyncResp);
321*9f8bfa7cSGunnar Mills                             getLocationIndicatorActive(asyncResp);
3221c8fba97SJames Feist                             break;
3231c8fba97SJames Feist                         }
3241c8fba97SJames Feist                     }
3251c8fba97SJames Feist 
32655c7b7a2SEd Tanous                     crow::connections::systemBus->async_method_call(
32762d5e2e4SEd Tanous                         [asyncResp, chassisId(std::string(chassisId))](
32890728b54SEd Tanous                             const boost::system::error_code /*ec2*/,
3291abe55efSEd Tanous                             const std::vector<std::pair<
3301abe55efSEd Tanous                                 std::string, VariantType>>& propertiesList) {
3311214b7e7SGunnar Mills                             for (const std::pair<std::string, VariantType>&
3321214b7e7SGunnar Mills                                      property : propertiesList)
3331abe55efSEd Tanous                             {
33499cffd7fSShawn McCarney                                 // Store DBus properties that are also Redfish
33599cffd7fSShawn McCarney                                 // properties with same name and a string value
33699cffd7fSShawn McCarney                                 const std::string& propertyName =
33799cffd7fSShawn McCarney                                     property.first;
33899cffd7fSShawn McCarney                                 if ((propertyName == "PartNumber") ||
33999cffd7fSShawn McCarney                                     (propertyName == "SerialNumber") ||
34099cffd7fSShawn McCarney                                     (propertyName == "Manufacturer") ||
34199cffd7fSShawn McCarney                                     (propertyName == "Model"))
34299cffd7fSShawn McCarney                                 {
343daf36e2eSEd Tanous                                     const std::string* value =
34499cffd7fSShawn McCarney                                         std::get_if<std::string>(
34599cffd7fSShawn McCarney                                             &property.second);
3461abe55efSEd Tanous                                     if (value != nullptr)
3471abe55efSEd Tanous                                     {
34899cffd7fSShawn McCarney                                         asyncResp->res.jsonValue[propertyName] =
34962d5e2e4SEd Tanous                                             *value;
350daf36e2eSEd Tanous                                     }
351daf36e2eSEd Tanous                                 }
35299cffd7fSShawn McCarney                             }
35362d5e2e4SEd Tanous                             asyncResp->res.jsonValue["Name"] = chassisId;
35462d5e2e4SEd Tanous                             asyncResp->res.jsonValue["Id"] = chassisId;
35562d5e2e4SEd Tanous                             asyncResp->res.jsonValue["Thermal"] = {
3561abe55efSEd Tanous                                 {"@odata.id", "/redfish/v1/Chassis/" +
3571abe55efSEd Tanous                                                   chassisId + "/Thermal"}};
3582474adfaSEd Tanous                             // Power object
3592474adfaSEd Tanous                             asyncResp->res.jsonValue["Power"] = {
3602474adfaSEd Tanous                                 {"@odata.id", "/redfish/v1/Chassis/" +
3612474adfaSEd Tanous                                                   chassisId + "/Power"}};
36295a3ecadSAnthony Wilson                             // SensorCollection
36395a3ecadSAnthony Wilson                             asyncResp->res.jsonValue["Sensors"] = {
36495a3ecadSAnthony Wilson                                 {"@odata.id", "/redfish/v1/Chassis/" +
36595a3ecadSAnthony Wilson                                                   chassisId + "/Sensors"}};
366029573d4SEd Tanous                             asyncResp->res.jsonValue["Status"] = {
367029573d4SEd Tanous                                 {"State", "Enabled"},
368029573d4SEd Tanous                             };
3692474adfaSEd Tanous 
370029573d4SEd Tanous                             asyncResp->res
371029573d4SEd Tanous                                 .jsonValue["Links"]["ComputerSystems"] = {
372029573d4SEd Tanous                                 {{"@odata.id", "/redfish/v1/Systems/system"}}};
373029573d4SEd Tanous                             asyncResp->res.jsonValue["Links"]["ManagedBy"] = {
374029573d4SEd Tanous                                 {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
375beeca0aeSGunnar Mills                             getChassisState(asyncResp);
376daf36e2eSEd Tanous                         },
377daf36e2eSEd Tanous                         connectionName, path, "org.freedesktop.DBus.Properties",
3781abe55efSEd Tanous                         "GetAll",
3791abe55efSEd Tanous                         "xyz.openbmc_project.Inventory.Decorator.Asset");
380daf36e2eSEd Tanous                     return;
381daf36e2eSEd Tanous                 }
382e0d918bcSEd Tanous 
383daf36e2eSEd Tanous                 // Couldn't find an object with that name.  return an error
384f12894f8SJason M. Bills                 messages::resourceNotFound(
385*9f8bfa7cSGunnar Mills                     asyncResp->res, "#Chassis.v1_14_0.Chassis", chassisId);
386daf36e2eSEd Tanous             },
387daf36e2eSEd Tanous             "xyz.openbmc_project.ObjectMapper",
388daf36e2eSEd Tanous             "/xyz/openbmc_project/object_mapper",
389daf36e2eSEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
390271584abSEd Tanous             "/xyz/openbmc_project/inventory", 0, interfaces);
391c181942fSQiang XU 
392c181942fSQiang XU         getPhysicalSecurityData(asyncResp);
393e37f8451SRapkiewicz, Pawel     }
3941c8fba97SJames Feist 
3951c8fba97SJames Feist     void doPatch(crow::Response& res, const crow::Request& req,
3961c8fba97SJames Feist                  const std::vector<std::string>& params) override
3971c8fba97SJames Feist     {
398*9f8bfa7cSGunnar Mills         std::optional<bool> locationIndicatorActive;
3991c8fba97SJames Feist         std::optional<std::string> indicatorLed;
4001c8fba97SJames Feist         auto asyncResp = std::make_shared<AsyncResp>(res);
4011c8fba97SJames Feist 
4021c8fba97SJames Feist         if (params.size() != 1)
4031c8fba97SJames Feist         {
4041c8fba97SJames Feist             return;
4051c8fba97SJames Feist         }
4061c8fba97SJames Feist 
407*9f8bfa7cSGunnar Mills         if (!json_util::readJson(req, res, "LocationIndicatorActive",
408*9f8bfa7cSGunnar Mills                                  locationIndicatorActive, "IndicatorLED",
409*9f8bfa7cSGunnar Mills                                  indicatorLed))
4101c8fba97SJames Feist         {
4111c8fba97SJames Feist             return;
4121c8fba97SJames Feist         }
4131c8fba97SJames Feist 
414*9f8bfa7cSGunnar Mills         // TODO (Gunnar): Remove IndicatorLED after enough time has passed
415*9f8bfa7cSGunnar Mills         if (!locationIndicatorActive && !indicatorLed)
4161c8fba97SJames Feist         {
4171c8fba97SJames Feist             return; // delete this when we support more patch properties
4181c8fba97SJames Feist         }
4191c8fba97SJames Feist 
4201c8fba97SJames Feist         const std::array<const char*, 2> interfaces = {
4211c8fba97SJames Feist             "xyz.openbmc_project.Inventory.Item.Board",
4221c8fba97SJames Feist             "xyz.openbmc_project.Inventory.Item.Chassis"};
4231c8fba97SJames Feist 
4241c8fba97SJames Feist         const std::string& chassisId = params[0];
4251c8fba97SJames Feist 
4261c8fba97SJames Feist         crow::connections::systemBus->async_method_call(
427*9f8bfa7cSGunnar Mills             [asyncResp, chassisId, locationIndicatorActive, indicatorLed](
4281c8fba97SJames Feist                 const boost::system::error_code ec,
4291c8fba97SJames Feist                 const crow::openbmc_mapper::GetSubTreeType& subtree) {
4301c8fba97SJames Feist                 if (ec)
4311c8fba97SJames Feist                 {
4321c8fba97SJames Feist                     messages::internalError(asyncResp->res);
4331c8fba97SJames Feist                     return;
4341c8fba97SJames Feist                 }
4351c8fba97SJames Feist 
4361c8fba97SJames Feist                 // Iterate over all retrieved ObjectPaths.
4371c8fba97SJames Feist                 for (const std::pair<
4381c8fba97SJames Feist                          std::string,
4391c8fba97SJames Feist                          std::vector<
4401214b7e7SGunnar Mills                              std::pair<std::string, std::vector<std::string>>>>&
4411214b7e7SGunnar Mills                          object : subtree)
4421c8fba97SJames Feist                 {
4431c8fba97SJames Feist                     const std::string& path = object.first;
4441c8fba97SJames Feist                     const std::vector<
4451214b7e7SGunnar Mills                         std::pair<std::string, std::vector<std::string>>>&
4461214b7e7SGunnar Mills                         connectionNames = object.second;
4471c8fba97SJames Feist 
4481c8fba97SJames Feist                     if (!boost::ends_with(path, chassisId))
4491c8fba97SJames Feist                     {
4501c8fba97SJames Feist                         continue;
4511c8fba97SJames Feist                     }
4521c8fba97SJames Feist 
4531c8fba97SJames Feist                     if (connectionNames.size() < 1)
4541c8fba97SJames Feist                     {
4551c8fba97SJames Feist                         BMCWEB_LOG_ERROR << "Got 0 Connection names";
4561c8fba97SJames Feist                         continue;
4571c8fba97SJames Feist                     }
4581c8fba97SJames Feist 
45923a21a1cSEd Tanous                     const std::vector<std::string>& interfaces3 =
4601c8fba97SJames Feist                         connectionNames[0].second;
4611c8fba97SJames Feist 
4621c8fba97SJames Feist                     const std::array<const char*, 2> hasIndicatorLed = {
4631c8fba97SJames Feist                         "xyz.openbmc_project.Inventory.Item.Panel",
4641c8fba97SJames Feist                         "xyz.openbmc_project.Inventory.Item.Board."
4651c8fba97SJames Feist                         "Motherboard"};
4661c8fba97SJames Feist                     bool indicatorChassis = false;
4671c8fba97SJames Feist                     for (const char* interface : hasIndicatorLed)
4681c8fba97SJames Feist                     {
469*9f8bfa7cSGunnar Mills                         if (std::find(interfaces3.begin(), interfaces3.end(),
47023a21a1cSEd Tanous                                       interface) != interfaces3.end())
4711c8fba97SJames Feist                         {
4721c8fba97SJames Feist                             indicatorChassis = true;
4731c8fba97SJames Feist                             break;
4741c8fba97SJames Feist                         }
4751c8fba97SJames Feist                     }
476*9f8bfa7cSGunnar Mills                     if (locationIndicatorActive)
477*9f8bfa7cSGunnar Mills                     {
478*9f8bfa7cSGunnar Mills                         if (indicatorChassis)
479*9f8bfa7cSGunnar Mills                         {
480*9f8bfa7cSGunnar Mills                             setLocationIndicatorActive(
481*9f8bfa7cSGunnar Mills                                 asyncResp, *locationIndicatorActive);
482*9f8bfa7cSGunnar Mills                         }
483*9f8bfa7cSGunnar Mills                         else
484*9f8bfa7cSGunnar Mills                         {
485*9f8bfa7cSGunnar Mills                             messages::propertyUnknown(
486*9f8bfa7cSGunnar Mills                                 asyncResp->res, "LocationIndicatorActive");
487*9f8bfa7cSGunnar Mills                         }
488*9f8bfa7cSGunnar Mills                     }
489*9f8bfa7cSGunnar Mills                     if (indicatorLed)
490*9f8bfa7cSGunnar Mills                     {
4911c8fba97SJames Feist                         if (indicatorChassis)
4921c8fba97SJames Feist                         {
493f23b7296SEd Tanous                             setIndicatorLedState(asyncResp, *indicatorLed);
4941c8fba97SJames Feist                         }
4951c8fba97SJames Feist                         else
4961c8fba97SJames Feist                         {
4971c8fba97SJames Feist                             messages::propertyUnknown(asyncResp->res,
4981c8fba97SJames Feist                                                       "IndicatorLED");
4991c8fba97SJames Feist                         }
5001c8fba97SJames Feist                     }
5011c8fba97SJames Feist                     return;
5021c8fba97SJames Feist                 }
5031c8fba97SJames Feist 
5041c8fba97SJames Feist                 messages::resourceNotFound(
505*9f8bfa7cSGunnar Mills                     asyncResp->res, "#Chassis.v1_14_0.Chassis", chassisId);
5061c8fba97SJames Feist             },
5071c8fba97SJames Feist             "xyz.openbmc_project.ObjectMapper",
5081c8fba97SJames Feist             "/xyz/openbmc_project/object_mapper",
5091c8fba97SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
5101c8fba97SJames Feist             "/xyz/openbmc_project/inventory", 0, interfaces);
5111c8fba97SJames Feist     }
51262d5e2e4SEd Tanous };
513dd99e04bSP.K. Lee 
514b5a76932SEd Tanous inline void doChassisPowerCycle(const std::shared_ptr<AsyncResp>& asyncResp)
515dd99e04bSP.K. Lee {
516c3b3c92aSVijay Khemka     const char* busName = "xyz.openbmc_project.ObjectMapper";
517c3b3c92aSVijay Khemka     const char* path = "/xyz/openbmc_project/object_mapper";
518c3b3c92aSVijay Khemka     const char* interface = "xyz.openbmc_project.ObjectMapper";
519c3b3c92aSVijay Khemka     const char* method = "GetSubTreePaths";
520c3b3c92aSVijay Khemka 
521c3b3c92aSVijay Khemka     const std::array<const char*, 1> interfaces = {
522c3b3c92aSVijay Khemka         "xyz.openbmc_project.State.Chassis"};
523c3b3c92aSVijay Khemka 
524c3b3c92aSVijay Khemka     // Use mapper to get subtree paths.
525c3b3c92aSVijay Khemka     crow::connections::systemBus->async_method_call(
526c3b3c92aSVijay Khemka         [asyncResp](const boost::system::error_code ec,
527c3b3c92aSVijay Khemka                     const std::vector<std::string>& chassisList) {
528c3b3c92aSVijay Khemka             if (ec)
529c3b3c92aSVijay Khemka             {
530c3b3c92aSVijay Khemka                 BMCWEB_LOG_DEBUG << "[mapper] Bad D-Bus request error: " << ec;
531c3b3c92aSVijay Khemka                 messages::internalError(asyncResp->res);
532c3b3c92aSVijay Khemka                 return;
533c3b3c92aSVijay Khemka             }
534c3b3c92aSVijay Khemka 
535dd99e04bSP.K. Lee             const char* processName = "xyz.openbmc_project.State.Chassis";
536dd99e04bSP.K. Lee             const char* interfaceName = "xyz.openbmc_project.State.Chassis";
537dd99e04bSP.K. Lee             const char* destProperty = "RequestedPowerTransition";
538dd99e04bSP.K. Lee             const std::string propertyValue =
539dd99e04bSP.K. Lee                 "xyz.openbmc_project.State.Chassis.Transition.PowerCycle";
540c3b3c92aSVijay Khemka             std::string objectPath =
541c3b3c92aSVijay Khemka                 "/xyz/openbmc_project/state/chassis_system0";
542c3b3c92aSVijay Khemka 
543c3b3c92aSVijay Khemka             /* Look for system reset chassis path */
544c3b3c92aSVijay Khemka             if ((std::find(chassisList.begin(), chassisList.end(),
545c3b3c92aSVijay Khemka                            objectPath)) == chassisList.end())
546c3b3c92aSVijay Khemka             {
547c3b3c92aSVijay Khemka                 /* We prefer to reset the full chassis_system, but if it doesn't
548c3b3c92aSVijay Khemka                  * exist on some platforms, fall back to a host-only power reset
549c3b3c92aSVijay Khemka                  */
550c3b3c92aSVijay Khemka                 objectPath = "/xyz/openbmc_project/state/chassis0";
551c3b3c92aSVijay Khemka             }
552dd99e04bSP.K. Lee 
553dd99e04bSP.K. Lee             crow::connections::systemBus->async_method_call(
554dd99e04bSP.K. Lee                 [asyncResp](const boost::system::error_code ec) {
555dd99e04bSP.K. Lee                     // Use "Set" method to set the property value.
556dd99e04bSP.K. Lee                     if (ec)
557dd99e04bSP.K. Lee                     {
558c3b3c92aSVijay Khemka                         BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: "
559c3b3c92aSVijay Khemka                                          << ec;
560dd99e04bSP.K. Lee                         messages::internalError(asyncResp->res);
561dd99e04bSP.K. Lee                         return;
562dd99e04bSP.K. Lee                     }
563dd99e04bSP.K. Lee 
564dd99e04bSP.K. Lee                     messages::success(asyncResp->res);
565dd99e04bSP.K. Lee                 },
566c3b3c92aSVijay Khemka                 processName, objectPath, "org.freedesktop.DBus.Properties",
567c3b3c92aSVijay Khemka                 "Set", interfaceName, destProperty,
568c3b3c92aSVijay Khemka                 std::variant<std::string>{propertyValue});
569c3b3c92aSVijay Khemka         },
570c3b3c92aSVijay Khemka         busName, path, interface, method, "/", 0, interfaces);
571dd99e04bSP.K. Lee }
572dd99e04bSP.K. Lee 
573dd99e04bSP.K. Lee /**
574dd99e04bSP.K. Lee  * ChassisResetAction class supports the POST method for the Reset
575dd99e04bSP.K. Lee  * action.
576dd99e04bSP.K. Lee  */
577dd99e04bSP.K. Lee class ChassisResetAction : public Node
578dd99e04bSP.K. Lee {
579dd99e04bSP.K. Lee   public:
58052cc112dSEd Tanous     ChassisResetAction(App& app) :
581dd99e04bSP.K. Lee         Node(app, "/redfish/v1/Chassis/<str>/Actions/Chassis.Reset/",
582dd99e04bSP.K. Lee              std::string())
583dd99e04bSP.K. Lee     {
584dd99e04bSP.K. Lee         entityPrivileges = {
585dd99e04bSP.K. Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
586dd99e04bSP.K. Lee     }
587dd99e04bSP.K. Lee 
588dd99e04bSP.K. Lee   private:
589dd99e04bSP.K. Lee     /**
590dd99e04bSP.K. Lee      * Function handles POST method request.
591dd99e04bSP.K. Lee      * Analyzes POST body before sending Reset request data to D-Bus.
592dd99e04bSP.K. Lee      */
593dd99e04bSP.K. Lee     void doPost(crow::Response& res, const crow::Request& req,
594cb13a392SEd Tanous                 const std::vector<std::string>&) override
595dd99e04bSP.K. Lee     {
596dd99e04bSP.K. Lee         BMCWEB_LOG_DEBUG << "Post Chassis Reset.";
597dd99e04bSP.K. Lee 
598dd99e04bSP.K. Lee         std::string resetType;
599dd99e04bSP.K. Lee         auto asyncResp = std::make_shared<AsyncResp>(res);
600dd99e04bSP.K. Lee 
601dd99e04bSP.K. Lee         if (!json_util::readJson(req, asyncResp->res, "ResetType", resetType))
602dd99e04bSP.K. Lee         {
603dd99e04bSP.K. Lee             return;
604dd99e04bSP.K. Lee         }
605dd99e04bSP.K. Lee 
606dd99e04bSP.K. Lee         if (resetType != "PowerCycle")
607dd99e04bSP.K. Lee         {
608dd99e04bSP.K. Lee             BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
609dd99e04bSP.K. Lee                              << resetType;
610dd99e04bSP.K. Lee             messages::actionParameterNotSupported(asyncResp->res, resetType,
611dd99e04bSP.K. Lee                                                   "ResetType");
612dd99e04bSP.K. Lee 
613dd99e04bSP.K. Lee             return;
614dd99e04bSP.K. Lee         }
615dd99e04bSP.K. Lee         doChassisPowerCycle(asyncResp);
616dd99e04bSP.K. Lee     }
617dd99e04bSP.K. Lee };
6181cb1a9e6SAppaRao Puli 
6191cb1a9e6SAppaRao Puli /**
6201cb1a9e6SAppaRao Puli  * ChassisResetActionInfo derived class for delivering Chassis
6211cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
6221cb1a9e6SAppaRao Puli  */
6231cb1a9e6SAppaRao Puli class ChassisResetActionInfo : public Node
6241cb1a9e6SAppaRao Puli {
6251cb1a9e6SAppaRao Puli   public:
6261cb1a9e6SAppaRao Puli     /*
6271cb1a9e6SAppaRao Puli      * Default Constructor
6281cb1a9e6SAppaRao Puli      */
62952cc112dSEd Tanous     ChassisResetActionInfo(App& app) :
6301cb1a9e6SAppaRao Puli         Node(app, "/redfish/v1/Chassis/<str>/ResetActionInfo/", std::string())
6311cb1a9e6SAppaRao Puli     {
6321cb1a9e6SAppaRao Puli         entityPrivileges = {
6331cb1a9e6SAppaRao Puli             {boost::beast::http::verb::get, {{"Login"}}},
6341cb1a9e6SAppaRao Puli             {boost::beast::http::verb::head, {{"Login"}}},
6351cb1a9e6SAppaRao Puli             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
6361cb1a9e6SAppaRao Puli             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
6371cb1a9e6SAppaRao Puli             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
6381cb1a9e6SAppaRao Puli             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
6391cb1a9e6SAppaRao Puli     }
6401cb1a9e6SAppaRao Puli 
6411cb1a9e6SAppaRao Puli   private:
6421cb1a9e6SAppaRao Puli     /**
6431cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
6441cb1a9e6SAppaRao Puli      */
645cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
6461cb1a9e6SAppaRao Puli                const std::vector<std::string>& params) override
6471cb1a9e6SAppaRao Puli     {
6481cb1a9e6SAppaRao Puli         if (params.size() != 1)
6491cb1a9e6SAppaRao Puli         {
6501cb1a9e6SAppaRao Puli             messages::internalError(res);
6511cb1a9e6SAppaRao Puli             res.end();
6521cb1a9e6SAppaRao Puli             return;
6531cb1a9e6SAppaRao Puli         }
6541cb1a9e6SAppaRao Puli         const std::string& chassisId = params[0];
6551cb1a9e6SAppaRao Puli 
6561cb1a9e6SAppaRao Puli         res.jsonValue = {{"@odata.type", "#ActionInfo.v1_1_2.ActionInfo"},
6571cb1a9e6SAppaRao Puli                          {"@odata.id", "/redfish/v1/Chassis/" + chassisId +
6581cb1a9e6SAppaRao Puli                                            "/ResetActionInfo"},
6591cb1a9e6SAppaRao Puli                          {"Name", "Reset Action Info"},
6601cb1a9e6SAppaRao Puli                          {"Id", "ResetActionInfo"},
6611cb1a9e6SAppaRao Puli                          {"Parameters",
6621cb1a9e6SAppaRao Puli                           {{{"Name", "ResetType"},
6631cb1a9e6SAppaRao Puli                             {"Required", true},
6641cb1a9e6SAppaRao Puli                             {"DataType", "String"},
6651cb1a9e6SAppaRao Puli                             {"AllowableValues", {"PowerCycle"}}}}}};
6661cb1a9e6SAppaRao Puli         res.end();
6671cb1a9e6SAppaRao Puli     }
6681cb1a9e6SAppaRao Puli };
6691cb1a9e6SAppaRao Puli 
670e37f8451SRapkiewicz, Pawel } // namespace redfish
671