xref: /openbmc/bmcweb/features/redfish/lib/update_service.hpp (revision f4b65ab1316a7b4ce9387540735c8280147075c0)
1729dae72SJennifer Lee /*
2729dae72SJennifer Lee // Copyright (c) 2018 Intel Corporation
3729dae72SJennifer Lee //
4729dae72SJennifer Lee // Licensed under the Apache License, Version 2.0 (the "License");
5729dae72SJennifer Lee // you may not use this file except in compliance with the License.
6729dae72SJennifer Lee // You may obtain a copy of the License at
7729dae72SJennifer Lee //
8729dae72SJennifer Lee //      http://www.apache.org/licenses/LICENSE-2.0
9729dae72SJennifer Lee //
10729dae72SJennifer Lee // Unless required by applicable law or agreed to in writing, software
11729dae72SJennifer Lee // distributed under the License is distributed on an "AS IS" BASIS,
12729dae72SJennifer Lee // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13729dae72SJennifer Lee // See the License for the specific language governing permissions and
14729dae72SJennifer Lee // limitations under the License.
15729dae72SJennifer Lee */
16729dae72SJennifer Lee #pragma once
17729dae72SJennifer Lee 
18729dae72SJennifer Lee #include "node.hpp"
191abe55efSEd Tanous 
20729dae72SJennifer Lee #include <boost/container/flat_map.hpp>
21729dae72SJennifer Lee 
221abe55efSEd Tanous namespace redfish
231abe55efSEd Tanous {
24acb7cfb4SJennifer Lee static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;
25729dae72SJennifer Lee 
261abe55efSEd Tanous class UpdateService : public Node
271abe55efSEd Tanous {
28729dae72SJennifer Lee   public:
291abe55efSEd Tanous     UpdateService(CrowApp &app) : Node(app, "/redfish/v1/UpdateService/")
301abe55efSEd Tanous     {
31729dae72SJennifer Lee         Node::json["@odata.type"] = "#UpdateService.v1_2_0.UpdateService";
32729dae72SJennifer Lee         Node::json["@odata.id"] = "/redfish/v1/UpdateService";
33729dae72SJennifer Lee         Node::json["@odata.context"] =
34729dae72SJennifer Lee             "/redfish/v1/$metadata#UpdateService.UpdateService";
35729dae72SJennifer Lee         Node::json["Id"] = "UpdateService";
36729dae72SJennifer Lee         Node::json["Description"] = "Service for Software Update";
37729dae72SJennifer Lee         Node::json["Name"] = "Update Service";
38acb7cfb4SJennifer Lee         Node::json["HttpPushUri"] = "/redfish/v1/UpdateService";
39c711bf86SEd Tanous         // UpdateService cannot be disabled
40c711bf86SEd Tanous         Node::json["ServiceEnabled"] = true;
416c4eb9deSJennifer Lee         Node::json["FirmwareInventory"] = {
426c4eb9deSJennifer Lee             {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
43729dae72SJennifer Lee 
44729dae72SJennifer Lee         entityPrivileges = {
45729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
46729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
47729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
48729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
49729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
50729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
51729dae72SJennifer Lee     }
52729dae72SJennifer Lee 
53729dae72SJennifer Lee   private:
5455c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
551abe55efSEd Tanous                const std::vector<std::string> &params) override
561abe55efSEd Tanous     {
5755c7b7a2SEd Tanous         res.jsonValue = Node::json;
58729dae72SJennifer Lee         res.end();
59729dae72SJennifer Lee     }
601abe55efSEd Tanous     static void activateImage(const std::string &objPath)
611abe55efSEd Tanous     {
62acb7cfb4SJennifer Lee         crow::connections::systemBus->async_method_call(
63c711bf86SEd Tanous             [objPath](const boost::system::error_code error_code) {
641abe55efSEd Tanous                 if (error_code)
651abe55efSEd Tanous                 {
66acb7cfb4SJennifer Lee                     BMCWEB_LOG_DEBUG << "error_code = " << error_code;
67acb7cfb4SJennifer Lee                     BMCWEB_LOG_DEBUG << "error msg = " << error_code.message();
68acb7cfb4SJennifer Lee                 }
69acb7cfb4SJennifer Lee             },
70c711bf86SEd Tanous             "xyz.openbmc_project.Software.BMC.Updater", objPath,
71acb7cfb4SJennifer Lee             "org.freedesktop.DBus.Properties", "Set",
72acb7cfb4SJennifer Lee             "xyz.openbmc_project.Software.Activation", "RequestedActivation",
73acb7cfb4SJennifer Lee             sdbusplus::message::variant<std::string>(
74acb7cfb4SJennifer Lee                 "xyz.openbmc_project.Software.Activation.RequestedActivations."
75acb7cfb4SJennifer Lee                 "Active"));
76acb7cfb4SJennifer Lee     }
77acb7cfb4SJennifer Lee     void doPost(crow::Response &res, const crow::Request &req,
781abe55efSEd Tanous                 const std::vector<std::string> &params) override
791abe55efSEd Tanous     {
80acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "doPost...";
81acb7cfb4SJennifer Lee 
82acb7cfb4SJennifer Lee         // Only allow one FW update at a time
831abe55efSEd Tanous         if (fwUpdateMatcher != nullptr)
841abe55efSEd Tanous         {
85acb7cfb4SJennifer Lee             res.addHeader("Retry-After", "30");
86acb7cfb4SJennifer Lee             res.result(boost::beast::http::status::service_unavailable);
87acb7cfb4SJennifer Lee             res.jsonValue = messages::serviceTemporarilyUnavailable("3");
88acb7cfb4SJennifer Lee             res.end();
89acb7cfb4SJennifer Lee             return;
90acb7cfb4SJennifer Lee         }
91acb7cfb4SJennifer Lee         // Make this const static so it survives outside this method
921abe55efSEd Tanous         static boost::asio::deadline_timer timeout(
931abe55efSEd Tanous             *req.ioService, boost::posix_time::seconds(5));
94acb7cfb4SJennifer Lee 
95acb7cfb4SJennifer Lee         timeout.expires_from_now(boost::posix_time::seconds(5));
96acb7cfb4SJennifer Lee 
97acb7cfb4SJennifer Lee         timeout.async_wait([&res](const boost::system::error_code &ec) {
98acb7cfb4SJennifer Lee             fwUpdateMatcher = nullptr;
991abe55efSEd Tanous             if (ec == boost::asio::error::operation_aborted)
1001abe55efSEd Tanous             {
101acb7cfb4SJennifer Lee                 // expected, we were canceled before the timer completed.
102acb7cfb4SJennifer Lee                 return;
103acb7cfb4SJennifer Lee             }
1041abe55efSEd Tanous             BMCWEB_LOG_ERROR
1051abe55efSEd Tanous                 << "Timed out waiting for firmware object being created";
1061abe55efSEd Tanous             BMCWEB_LOG_ERROR
1071abe55efSEd Tanous                 << "FW image may has already been uploaded to server";
1081abe55efSEd Tanous             if (ec)
1091abe55efSEd Tanous             {
110acb7cfb4SJennifer Lee                 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
111acb7cfb4SJennifer Lee                 return;
112acb7cfb4SJennifer Lee             }
113acb7cfb4SJennifer Lee 
114acb7cfb4SJennifer Lee             res.result(boost::beast::http::status::internal_server_error);
115acb7cfb4SJennifer Lee             res.jsonValue = redfish::messages::internalError();
116acb7cfb4SJennifer Lee             res.end();
117acb7cfb4SJennifer Lee         });
118acb7cfb4SJennifer Lee 
119acb7cfb4SJennifer Lee         auto callback = [&res](sdbusplus::message::message &m) {
120acb7cfb4SJennifer Lee             BMCWEB_LOG_DEBUG << "Match fired";
121acb7cfb4SJennifer Lee             bool flag = false;
122acb7cfb4SJennifer Lee 
1231abe55efSEd Tanous             if (m.is_method_error())
1241abe55efSEd Tanous             {
125acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "Dbus method error!!!";
126acb7cfb4SJennifer Lee                 res.end();
127acb7cfb4SJennifer Lee                 return;
128acb7cfb4SJennifer Lee             }
129acb7cfb4SJennifer Lee             std::vector<std::pair<
130acb7cfb4SJennifer Lee                 std::string,
1311abe55efSEd Tanous                 std::vector<std::pair<
1321abe55efSEd Tanous                     std::string, sdbusplus::message::variant<std::string>>>>>
1333ae837c9SEd Tanous                 interfacesProperties;
134acb7cfb4SJennifer Lee 
135c711bf86SEd Tanous             sdbusplus::message::object_path objPath;
136acb7cfb4SJennifer Lee 
1373ae837c9SEd Tanous             m.read(objPath, interfacesProperties); // Read in the object path
138acb7cfb4SJennifer Lee                                                    // that was just created
139c711bf86SEd Tanous             // std::string str_objpath = objPath.str;  // keep a copy for
140acb7cfb4SJennifer Lee             // constructing response message
141c711bf86SEd Tanous             BMCWEB_LOG_DEBUG << "obj path = " << objPath.str; // str_objpath;
1423ae837c9SEd Tanous             for (auto &interface : interfacesProperties)
1431abe55efSEd Tanous             {
144acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "interface = " << interface.first;
145acb7cfb4SJennifer Lee 
1461abe55efSEd Tanous                 if (interface.first ==
1471abe55efSEd Tanous                     "xyz.openbmc_project.Software.Activation")
1481abe55efSEd Tanous                 {
1491abe55efSEd Tanous                     // cancel timer only when
1501abe55efSEd Tanous                     // xyz.openbmc_project.Software.Activation interface is
1511abe55efSEd Tanous                     // added
152acb7cfb4SJennifer Lee                     boost::system::error_code ec;
153acb7cfb4SJennifer Lee                     timeout.cancel(ec);
1541abe55efSEd Tanous                     if (ec)
1551abe55efSEd Tanous                     {
156acb7cfb4SJennifer Lee                         BMCWEB_LOG_ERROR << "error canceling timer " << ec;
157acb7cfb4SJennifer Lee                     }
158c711bf86SEd Tanous                     UpdateService::activateImage(objPath.str); // str_objpath);
159acb7cfb4SJennifer Lee                     res.jsonValue = redfish::messages::success();
160acb7cfb4SJennifer Lee                     BMCWEB_LOG_DEBUG << "ending response";
161acb7cfb4SJennifer Lee                     res.end();
162acb7cfb4SJennifer Lee                     fwUpdateMatcher = nullptr;
163acb7cfb4SJennifer Lee                 }
164acb7cfb4SJennifer Lee             }
165acb7cfb4SJennifer Lee         };
166acb7cfb4SJennifer Lee 
167acb7cfb4SJennifer Lee         fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
168acb7cfb4SJennifer Lee             *crow::connections::systemBus,
169acb7cfb4SJennifer Lee             "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
170acb7cfb4SJennifer Lee             "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
171acb7cfb4SJennifer Lee             callback);
172acb7cfb4SJennifer Lee 
173acb7cfb4SJennifer Lee         std::string filepath(
174acb7cfb4SJennifer Lee             "/tmp/images/" +
175acb7cfb4SJennifer Lee             boost::uuids::to_string(boost::uuids::random_generator()()));
176acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
177acb7cfb4SJennifer Lee         std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
178acb7cfb4SJennifer Lee                                         std::ofstream::trunc);
179acb7cfb4SJennifer Lee         out << req.body;
180acb7cfb4SJennifer Lee         out.close();
181acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "file upload complete!!";
182acb7cfb4SJennifer Lee     }
183729dae72SJennifer Lee };
184729dae72SJennifer Lee 
1851abe55efSEd Tanous class SoftwareInventoryCollection : public Node
1861abe55efSEd Tanous {
187729dae72SJennifer Lee   public:
188729dae72SJennifer Lee     template <typename CrowApp>
1891abe55efSEd Tanous     SoftwareInventoryCollection(CrowApp &app) :
1901abe55efSEd Tanous         Node(app, "/redfish/v1/UpdateService/FirmwareInventory/")
1911abe55efSEd Tanous     {
192729dae72SJennifer Lee         Node::json["@odata.type"] =
193729dae72SJennifer Lee             "#SoftwareInventoryCollection.SoftwareInventoryCollection";
1946c4eb9deSJennifer Lee         Node::json["@odata.id"] = "/redfish/v1/UpdateService/FirmwareInventory";
195729dae72SJennifer Lee         Node::json["@odata.context"] =
196729dae72SJennifer Lee             "/redfish/v1/"
197729dae72SJennifer Lee             "$metadata#SoftwareInventoryCollection.SoftwareInventoryCollection";
198729dae72SJennifer Lee         Node::json["Name"] = "Software Inventory Collection";
199729dae72SJennifer Lee 
200729dae72SJennifer Lee         entityPrivileges = {
201729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
202729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
203729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
204729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
205729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
206729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
207729dae72SJennifer Lee     }
208729dae72SJennifer Lee 
209729dae72SJennifer Lee   private:
21055c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
2111abe55efSEd Tanous                const std::vector<std::string> &params) override
2121abe55efSEd Tanous     {
213c711bf86SEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
21455c7b7a2SEd Tanous         res.jsonValue = Node::json;
215c711bf86SEd Tanous 
216c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
217c711bf86SEd Tanous             [asyncResp](
218c711bf86SEd Tanous                 const boost::system::error_code ec,
2196c4eb9deSJennifer Lee                 const std::vector<std::pair<
2201abe55efSEd Tanous                     std::string, std::vector<std::pair<
2211abe55efSEd Tanous                                      std::string, std::vector<std::string>>>>>
2226c4eb9deSJennifer Lee                     &subtree) {
2231abe55efSEd Tanous                 if (ec)
2241abe55efSEd Tanous                 {
225c711bf86SEd Tanous                     asyncResp->res.result(
226c711bf86SEd Tanous                         boost::beast::http::status::internal_server_error);
2276c4eb9deSJennifer Lee                     return;
228729dae72SJennifer Lee                 }
229c711bf86SEd Tanous                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
230c711bf86SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
2316c4eb9deSJennifer Lee 
2321abe55efSEd Tanous                 for (auto &obj : subtree)
2331abe55efSEd Tanous                 {
2341abe55efSEd Tanous                     const std::vector<
2351abe55efSEd Tanous                         std::pair<std::string, std::vector<std::string>>>
2366c4eb9deSJennifer Lee                         &connections = obj.second;
2376c4eb9deSJennifer Lee 
238*f4b65ab1SJennifer Lee                     // if can't parse fw id then return
239*f4b65ab1SJennifer Lee                     std::size_t idPos = obj.first.rfind("/");
240*f4b65ab1SJennifer Lee                     if (idPos == std::string::npos ||
241*f4b65ab1SJennifer Lee                         idPos + 1 == obj.first.size())
242*f4b65ab1SJennifer Lee                     {
243*f4b65ab1SJennifer Lee                         asyncResp->res.result(
244*f4b65ab1SJennifer Lee                             boost::beast::http::status::internal_server_error);
245*f4b65ab1SJennifer Lee                         asyncResp->res.jsonValue = messages::internalError();
246*f4b65ab1SJennifer Lee                         BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
247*f4b65ab1SJennifer Lee                         return;
248*f4b65ab1SJennifer Lee                     }
249*f4b65ab1SJennifer Lee 
250*f4b65ab1SJennifer Lee                     std::string swId = obj.first.substr(idPos + 1);
251*f4b65ab1SJennifer Lee 
2521abe55efSEd Tanous                     for (auto &conn : connections)
2531abe55efSEd Tanous                     {
254c711bf86SEd Tanous                         const std::string &connectionName = conn.first;
2551abe55efSEd Tanous                         BMCWEB_LOG_DEBUG << "connectionName = "
2561abe55efSEd Tanous                                          << connectionName;
25755c7b7a2SEd Tanous                         BMCWEB_LOG_DEBUG << "obj.first = " << obj.first;
2586c4eb9deSJennifer Lee 
25955c7b7a2SEd Tanous                         crow::connections::systemBus->async_method_call(
260*f4b65ab1SJennifer Lee                             [asyncResp,
261*f4b65ab1SJennifer Lee                              swId](const boost::system::error_code error_code,
262c711bf86SEd Tanous                                    const VariantType &activation) {
2631abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
2641abe55efSEd Tanous                                     << "safe returned in lambda function";
2651abe55efSEd Tanous                                 if (error_code)
2661abe55efSEd Tanous                                 {
267acb7cfb4SJennifer Lee                                     asyncResp->res.result(
2681abe55efSEd Tanous                                         boost::beast::http::status::
2691abe55efSEd Tanous                                             internal_server_error);
2706c4eb9deSJennifer Lee                                     return;
2716c4eb9deSJennifer Lee                                 }
272c711bf86SEd Tanous 
273*f4b65ab1SJennifer Lee                                 const std::string *swActivationStatus =
2741abe55efSEd Tanous                                     mapbox::getPtr<const std::string>(
2751abe55efSEd Tanous                                         activation);
276*f4b65ab1SJennifer Lee                                 if (swActivationStatus == nullptr)
2771abe55efSEd Tanous                                 {
278c711bf86SEd Tanous                                     asyncResp->res.result(
2791abe55efSEd Tanous                                         boost::beast::http::status::
2801abe55efSEd Tanous                                             internal_server_error);
281acb7cfb4SJennifer Lee                                     return;
282acb7cfb4SJennifer Lee                                 }
283*f4b65ab1SJennifer Lee                                 if (swActivationStatus != nullptr &&
284*f4b65ab1SJennifer Lee                                     *swActivationStatus !=
285*f4b65ab1SJennifer Lee                                         "xyz.openbmc_project.Software."
286*f4b65ab1SJennifer Lee                                         "Activation."
287*f4b65ab1SJennifer Lee                                         "Activations.Active")
2881abe55efSEd Tanous                                 {
289*f4b65ab1SJennifer Lee                                     // The activation status of this software is
290*f4b65ab1SJennifer Lee                                     // not currently active, so does not need to
291*f4b65ab1SJennifer Lee                                     // be listed in the response
292c711bf86SEd Tanous                                     return;
293c711bf86SEd Tanous                                 }
294c711bf86SEd Tanous                                 nlohmann::json &members =
295c711bf86SEd Tanous                                     asyncResp->res.jsonValue["Members"];
296c711bf86SEd Tanous                                 members.push_back(
297*f4b65ab1SJennifer Lee                                     {{"@odata.id", "/redfish/v1/UpdateService/"
2981abe55efSEd Tanous                                                    "FirmwareInventory/" +
299*f4b65ab1SJennifer Lee                                                        swId}});
3001abe55efSEd Tanous                                 asyncResp->res
3011abe55efSEd Tanous                                     .jsonValue["Members@odata.count"] =
302c711bf86SEd Tanous                                     members.size();
3036c4eb9deSJennifer Lee                             },
3041abe55efSEd Tanous                             connectionName, obj.first,
3051abe55efSEd Tanous                             "org.freedesktop.DBus.Properties", "Get",
3061abe55efSEd Tanous                             "xyz.openbmc_project.Software.Activation",
307acb7cfb4SJennifer Lee                             "Activation");
3086c4eb9deSJennifer Lee                     }
3096c4eb9deSJennifer Lee                 }
310c711bf86SEd Tanous             },
311c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
312c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
313c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
314c711bf86SEd Tanous             "/xyz/openbmc_project/software", int32_t(1),
3151abe55efSEd Tanous             std::array<const char *, 1>{
3161abe55efSEd Tanous                 "xyz.openbmc_project.Software.Version"});
317729dae72SJennifer Lee     }
318729dae72SJennifer Lee };
319c711bf86SEd Tanous 
3201abe55efSEd Tanous class SoftwareInventory : public Node
3211abe55efSEd Tanous {
322729dae72SJennifer Lee   public:
323729dae72SJennifer Lee     template <typename CrowApp>
3241abe55efSEd Tanous     SoftwareInventory(CrowApp &app) :
3251abe55efSEd Tanous         Node(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/",
3261abe55efSEd Tanous              std::string())
3271abe55efSEd Tanous     {
3281abe55efSEd Tanous         Node::json["@odata.type"] =
3291abe55efSEd Tanous             "#SoftwareInventory.v1_1_0.SoftwareInventory";
330729dae72SJennifer Lee         Node::json["@odata.context"] =
331729dae72SJennifer Lee             "/redfish/v1/$metadata#SoftwareInventory.SoftwareInventory";
332729dae72SJennifer Lee         Node::json["Name"] = "Software Inventory";
3336c4eb9deSJennifer Lee         Node::json["Updateable"] = false;
334acb7cfb4SJennifer Lee         Node::json["Status"]["Health"] = "OK";
335acb7cfb4SJennifer Lee         Node::json["Status"]["HealthRollup"] = "OK";
336acb7cfb4SJennifer Lee         Node::json["Status"]["State"] = "Enabled";
337729dae72SJennifer Lee         entityPrivileges = {
338729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
339729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
340729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
341729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
342729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
343729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
344729dae72SJennifer Lee     }
345729dae72SJennifer Lee 
346729dae72SJennifer Lee   private:
34755c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
3481abe55efSEd Tanous                const std::vector<std::string> &params) override
3491abe55efSEd Tanous     {
350c711bf86SEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
35155c7b7a2SEd Tanous         res.jsonValue = Node::json;
3526c4eb9deSJennifer Lee 
3531abe55efSEd Tanous         if (params.size() != 1)
3541abe55efSEd Tanous         {
355729dae72SJennifer Lee             res.result(boost::beast::http::status::internal_server_error);
356acb7cfb4SJennifer Lee             res.jsonValue = messages::internalError();
357729dae72SJennifer Lee             res.end();
358729dae72SJennifer Lee             return;
359729dae72SJennifer Lee         }
360729dae72SJennifer Lee 
3613ae837c9SEd Tanous         std::shared_ptr<std::string> swId =
362c711bf86SEd Tanous             std::make_shared<std::string>(params[0]);
363c711bf86SEd Tanous 
36455c7b7a2SEd Tanous         res.jsonValue["@odata.id"] =
3653ae837c9SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
366c711bf86SEd Tanous 
367c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
3683ae837c9SEd Tanous             [asyncResp, swId](
369c711bf86SEd Tanous                 const boost::system::error_code ec,
3706c4eb9deSJennifer Lee                 const std::vector<std::pair<
3711abe55efSEd Tanous                     std::string, std::vector<std::pair<
3721abe55efSEd Tanous                                      std::string, std::vector<std::string>>>>>
3736c4eb9deSJennifer Lee                     &subtree) {
37455c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "doGet callback...";
3751abe55efSEd Tanous                 if (ec)
3761abe55efSEd Tanous                 {
377c711bf86SEd Tanous                     asyncResp->res.result(
378c711bf86SEd Tanous                         boost::beast::http::status::internal_server_error);
3796c4eb9deSJennifer Lee                     return;
3806c4eb9deSJennifer Lee                 }
3816c4eb9deSJennifer Lee 
3821abe55efSEd Tanous                 for (const std::pair<
3831abe55efSEd Tanous                          std::string,
3841abe55efSEd Tanous                          std::vector<
3851abe55efSEd Tanous                              std::pair<std::string, std::vector<std::string>>>>
3861abe55efSEd Tanous                          &obj : subtree)
3871abe55efSEd Tanous                 {
3883ae837c9SEd Tanous                     if (boost::ends_with(obj.first, *swId) != true)
3891abe55efSEd Tanous                     {
390acb7cfb4SJennifer Lee                         continue;
391acb7cfb4SJennifer Lee                     }
392acb7cfb4SJennifer Lee 
393*f4b65ab1SJennifer Lee                     if (obj.second.size() < 1)
3941abe55efSEd Tanous                     {
395acb7cfb4SJennifer Lee                         continue;
396acb7cfb4SJennifer Lee                     }
3976c4eb9deSJennifer Lee 
39855c7b7a2SEd Tanous                     crow::connections::systemBus->async_method_call(
3991abe55efSEd Tanous                         [asyncResp,
4003ae837c9SEd Tanous                          swId](const boost::system::error_code error_code,
4011abe55efSEd Tanous                                const boost::container::flat_map<
4021abe55efSEd Tanous                                    std::string, VariantType> &propertiesList) {
4031abe55efSEd Tanous                             if (error_code)
4041abe55efSEd Tanous                             {
405c711bf86SEd Tanous                                 asyncResp->res.result(
4061abe55efSEd Tanous                                     boost::beast::http::status::
4071abe55efSEd Tanous                                         internal_server_error);
4086c4eb9deSJennifer Lee                                 return;
4096c4eb9deSJennifer Lee                             }
4101abe55efSEd Tanous                             boost::container::flat_map<
4111abe55efSEd Tanous                                 std::string, VariantType>::const_iterator it =
4126c4eb9deSJennifer Lee                                 propertiesList.find("Purpose");
4131abe55efSEd Tanous                             if (it == propertiesList.end())
4141abe55efSEd Tanous                             {
4151abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
4161abe55efSEd Tanous                                     << "Can't find property \"Purpose\"!";
417c711bf86SEd Tanous                                 asyncResp->res.result(
4181abe55efSEd Tanous                                     boost::beast::http::status::
4191abe55efSEd Tanous                                         internal_server_error);
4206c4eb9deSJennifer Lee                                 return;
4216c4eb9deSJennifer Lee                             }
4223ae837c9SEd Tanous                             const std::string *swInvPurpose =
423acb7cfb4SJennifer Lee                                 mapbox::getPtr<const std::string>(it->second);
4243ae837c9SEd Tanous                             if (swInvPurpose == nullptr)
4251abe55efSEd Tanous                             {
4261abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
4271abe55efSEd Tanous                                     << "wrong types for property\"Purpose\"!";
428c711bf86SEd Tanous                                 asyncResp->res.result(
4291abe55efSEd Tanous                                     boost::beast::http::status::
4301abe55efSEd Tanous                                         internal_server_error);
431acb7cfb4SJennifer Lee                                 return;
432acb7cfb4SJennifer Lee                             }
433c711bf86SEd Tanous 
4343ae837c9SEd Tanous                             BMCWEB_LOG_DEBUG << "swInvPurpose = "
4353ae837c9SEd Tanous                                              << *swInvPurpose;
436c711bf86SEd Tanous                             it = propertiesList.find("Version");
4371abe55efSEd Tanous                             if (it == propertiesList.end())
4381abe55efSEd Tanous                             {
4391abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
4401abe55efSEd Tanous                                     << "Can't find property \"Version\"!";
441c711bf86SEd Tanous                                 asyncResp->res.result(
4421abe55efSEd Tanous                                     boost::beast::http::status::
4431abe55efSEd Tanous                                         internal_server_error);
444c711bf86SEd Tanous                                 return;
445acb7cfb4SJennifer Lee                             }
446acb7cfb4SJennifer Lee 
447*f4b65ab1SJennifer Lee                             BMCWEB_LOG_DEBUG << "Version found!";
448c711bf86SEd Tanous 
449*f4b65ab1SJennifer Lee                             const std::string *version =
450*f4b65ab1SJennifer Lee                                 mapbox::getPtr<const std::string>(it->second);
451*f4b65ab1SJennifer Lee 
452*f4b65ab1SJennifer Lee                             if (version == nullptr)
4531abe55efSEd Tanous                             {
4541abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
4551abe55efSEd Tanous                                     << "Can't find property \"Version\"!";
456c711bf86SEd Tanous                                 asyncResp->res.result(
4571abe55efSEd Tanous                                     boost::beast::http::status::
4581abe55efSEd Tanous                                         internal_server_error);
4596c4eb9deSJennifer Lee                                 return;
4606c4eb9deSJennifer Lee                             }
461c711bf86SEd Tanous                             asyncResp->res.jsonValue["Version"] = *version;
4623ae837c9SEd Tanous                             asyncResp->res.jsonValue["Id"] = *swId;
4636c4eb9deSJennifer Lee                         },
464c711bf86SEd Tanous                         obj.second[0].first, obj.first,
465c711bf86SEd Tanous                         "org.freedesktop.DBus.Properties", "GetAll",
466c711bf86SEd Tanous                         "xyz.openbmc_project.Software.Version");
4676c4eb9deSJennifer Lee                 }
468c711bf86SEd Tanous             },
469c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
470c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
471c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
472c711bf86SEd Tanous             "/xyz/openbmc_project/software", int32_t(1),
4731abe55efSEd Tanous             std::array<const char *, 1>{
4741abe55efSEd Tanous                 "xyz.openbmc_project.Software.Version"});
4756c4eb9deSJennifer Lee     }
476729dae72SJennifer Lee };
477729dae72SJennifer Lee 
478729dae72SJennifer Lee } // namespace redfish
479