xref: /openbmc/bmcweb/features/redfish/lib/update_service.hpp (revision 274dfe625f862e8ded2d4eb88dd856cf66bf54bf)
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>
2187d84729SAndrew Geissler #include <utils/fw_utils.hpp>
22abf2add6SEd Tanous #include <variant>
23729dae72SJennifer Lee 
241abe55efSEd Tanous namespace redfish
251abe55efSEd Tanous {
2627826b5fSEd Tanous 
270e7de46fSAndrew Geissler // Match signals added on software path
28acb7cfb4SJennifer Lee static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;
290e7de46fSAndrew Geissler // Only allow one update at a time
300e7de46fSAndrew Geissler static bool fwUpdateInProgress = false;
3186adcd6dSAndrew Geissler // Timer for software available
32271584abSEd Tanous static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer;
3386adcd6dSAndrew Geissler 
3486adcd6dSAndrew Geissler static void cleanUp()
3586adcd6dSAndrew Geissler {
3686adcd6dSAndrew Geissler     fwUpdateInProgress = false;
3786adcd6dSAndrew Geissler     fwUpdateMatcher = nullptr;
3886adcd6dSAndrew Geissler }
3986adcd6dSAndrew Geissler static void activateImage(const std::string &objPath,
4086adcd6dSAndrew Geissler                           const std::string &service)
4186adcd6dSAndrew Geissler {
4286adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service;
4386adcd6dSAndrew Geissler     crow::connections::systemBus->async_method_call(
4486adcd6dSAndrew Geissler         [](const boost::system::error_code error_code) {
4586adcd6dSAndrew Geissler             if (error_code)
4686adcd6dSAndrew Geissler             {
4786adcd6dSAndrew Geissler                 BMCWEB_LOG_DEBUG << "error_code = " << error_code;
4886adcd6dSAndrew Geissler                 BMCWEB_LOG_DEBUG << "error msg = " << error_code.message();
4986adcd6dSAndrew Geissler             }
5086adcd6dSAndrew Geissler         },
5186adcd6dSAndrew Geissler         service, objPath, "org.freedesktop.DBus.Properties", "Set",
5286adcd6dSAndrew Geissler         "xyz.openbmc_project.Software.Activation", "RequestedActivation",
5386adcd6dSAndrew Geissler         std::variant<std::string>(
5486adcd6dSAndrew Geissler             "xyz.openbmc_project.Software.Activation.RequestedActivations."
5586adcd6dSAndrew Geissler             "Active"));
5686adcd6dSAndrew Geissler }
570554c984SAndrew Geissler 
580554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
590554c984SAndrew Geissler // then no asyncResp updates will occur
6086adcd6dSAndrew Geissler static void softwareInterfaceAdded(std::shared_ptr<AsyncResp> asyncResp,
6186adcd6dSAndrew Geissler                                    sdbusplus::message::message &m)
6286adcd6dSAndrew Geissler {
6386adcd6dSAndrew Geissler     std::vector<std::pair<
6486adcd6dSAndrew Geissler         std::string,
6586adcd6dSAndrew Geissler         std::vector<std::pair<std::string, std::variant<std::string>>>>>
6686adcd6dSAndrew Geissler         interfacesProperties;
6786adcd6dSAndrew Geissler 
6886adcd6dSAndrew Geissler     sdbusplus::message::object_path objPath;
6986adcd6dSAndrew Geissler 
7086adcd6dSAndrew Geissler     m.read(objPath, interfacesProperties);
7186adcd6dSAndrew Geissler 
7286adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
7386adcd6dSAndrew Geissler     for (auto &interface : interfacesProperties)
7486adcd6dSAndrew Geissler     {
7586adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "interface = " << interface.first;
7686adcd6dSAndrew Geissler 
7786adcd6dSAndrew Geissler         if (interface.first == "xyz.openbmc_project.Software.Activation")
7886adcd6dSAndrew Geissler         {
7986adcd6dSAndrew Geissler             // Found our interface, disable callbacks
8086adcd6dSAndrew Geissler             fwUpdateMatcher = nullptr;
8186adcd6dSAndrew Geissler 
8286adcd6dSAndrew Geissler             // Retrieve service and activate
8386adcd6dSAndrew Geissler             crow::connections::systemBus->async_method_call(
8486adcd6dSAndrew Geissler                 [objPath, asyncResp](
8586adcd6dSAndrew Geissler                     const boost::system::error_code error_code,
8686adcd6dSAndrew Geissler                     const std::vector<std::pair<
8786adcd6dSAndrew Geissler                         std::string, std::vector<std::string>>> &objInfo) {
8886adcd6dSAndrew Geissler                     if (error_code)
8986adcd6dSAndrew Geissler                     {
9086adcd6dSAndrew Geissler                         BMCWEB_LOG_DEBUG << "error_code = " << error_code;
9186adcd6dSAndrew Geissler                         BMCWEB_LOG_DEBUG << "error msg = "
9286adcd6dSAndrew Geissler                                          << error_code.message();
930554c984SAndrew Geissler                         if (asyncResp)
940554c984SAndrew Geissler                         {
9586adcd6dSAndrew Geissler                             messages::internalError(asyncResp->res);
960554c984SAndrew Geissler                         }
9786adcd6dSAndrew Geissler                         cleanUp();
9886adcd6dSAndrew Geissler                         return;
9986adcd6dSAndrew Geissler                     }
10086adcd6dSAndrew Geissler                     // Ensure we only got one service back
10186adcd6dSAndrew Geissler                     if (objInfo.size() != 1)
10286adcd6dSAndrew Geissler                     {
10386adcd6dSAndrew Geissler                         BMCWEB_LOG_ERROR << "Invalid Object Size "
10486adcd6dSAndrew Geissler                                          << objInfo.size();
1050554c984SAndrew Geissler                         if (asyncResp)
1060554c984SAndrew Geissler                         {
10786adcd6dSAndrew Geissler                             messages::internalError(asyncResp->res);
1080554c984SAndrew Geissler                         }
10986adcd6dSAndrew Geissler                         cleanUp();
11086adcd6dSAndrew Geissler                         return;
11186adcd6dSAndrew Geissler                     }
11286adcd6dSAndrew Geissler                     // cancel timer only when
11386adcd6dSAndrew Geissler                     // xyz.openbmc_project.Software.Activation interface
11486adcd6dSAndrew Geissler                     // is added
11586adcd6dSAndrew Geissler                     fwAvailableTimer = nullptr;
11686adcd6dSAndrew Geissler 
11786adcd6dSAndrew Geissler                     activateImage(objPath.str, objInfo[0].first);
1180554c984SAndrew Geissler                     if (asyncResp)
1190554c984SAndrew Geissler                     {
12086adcd6dSAndrew Geissler                         redfish::messages::success(asyncResp->res);
1210554c984SAndrew Geissler                     }
12286adcd6dSAndrew Geissler                     fwUpdateInProgress = false;
12386adcd6dSAndrew Geissler                 },
12486adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper",
12586adcd6dSAndrew Geissler                 "/xyz/openbmc_project/object_mapper",
12686adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str,
12786adcd6dSAndrew Geissler                 std::array<const char *, 1>{
12886adcd6dSAndrew Geissler                     "xyz.openbmc_project.Software.Activation"});
12986adcd6dSAndrew Geissler         }
13086adcd6dSAndrew Geissler     }
13186adcd6dSAndrew Geissler }
13286adcd6dSAndrew Geissler 
1330554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
1340554c984SAndrew Geissler // then no asyncResp updates will occur
13586adcd6dSAndrew Geissler static void monitorForSoftwareAvailable(std::shared_ptr<AsyncResp> asyncResp,
1360554c984SAndrew Geissler                                         const crow::Request &req,
1370554c984SAndrew Geissler                                         int timeoutTimeSeconds = 5)
13886adcd6dSAndrew Geissler {
13986adcd6dSAndrew Geissler     // Only allow one FW update at a time
14086adcd6dSAndrew Geissler     if (fwUpdateInProgress != false)
14186adcd6dSAndrew Geissler     {
1420554c984SAndrew Geissler         if (asyncResp)
1430554c984SAndrew Geissler         {
14486adcd6dSAndrew Geissler             messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
1450554c984SAndrew Geissler         }
14686adcd6dSAndrew Geissler         return;
14786adcd6dSAndrew Geissler     }
14886adcd6dSAndrew Geissler 
1490554c984SAndrew Geissler     fwAvailableTimer =
150271584abSEd Tanous         std::make_unique<boost::asio::steady_timer>(*req.ioService);
15186adcd6dSAndrew Geissler 
152271584abSEd Tanous     fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
15386adcd6dSAndrew Geissler 
15486adcd6dSAndrew Geissler     fwAvailableTimer->async_wait(
15586adcd6dSAndrew Geissler         [asyncResp](const boost::system::error_code &ec) {
15686adcd6dSAndrew Geissler             cleanUp();
15786adcd6dSAndrew Geissler             if (ec == boost::asio::error::operation_aborted)
15886adcd6dSAndrew Geissler             {
15986adcd6dSAndrew Geissler                 // expected, we were canceled before the timer completed.
16086adcd6dSAndrew Geissler                 return;
16186adcd6dSAndrew Geissler             }
16286adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR
16386adcd6dSAndrew Geissler                 << "Timed out waiting for firmware object being created";
16486adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR
16586adcd6dSAndrew Geissler                 << "FW image may has already been uploaded to server";
16686adcd6dSAndrew Geissler             if (ec)
16786adcd6dSAndrew Geissler             {
16886adcd6dSAndrew Geissler                 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
16986adcd6dSAndrew Geissler                 return;
17086adcd6dSAndrew Geissler             }
1710554c984SAndrew Geissler             if (asyncResp)
1720554c984SAndrew Geissler             {
17386adcd6dSAndrew Geissler                 redfish::messages::internalError(asyncResp->res);
1740554c984SAndrew Geissler             }
17586adcd6dSAndrew Geissler         });
17686adcd6dSAndrew Geissler 
17786adcd6dSAndrew Geissler     auto callback = [asyncResp](sdbusplus::message::message &m) {
17886adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "Match fired";
17986adcd6dSAndrew Geissler         softwareInterfaceAdded(asyncResp, m);
18086adcd6dSAndrew Geissler     };
18186adcd6dSAndrew Geissler 
18286adcd6dSAndrew Geissler     fwUpdateInProgress = true;
18386adcd6dSAndrew Geissler 
18486adcd6dSAndrew Geissler     fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
18586adcd6dSAndrew Geissler         *crow::connections::systemBus,
18686adcd6dSAndrew Geissler         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
18786adcd6dSAndrew Geissler         "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
18886adcd6dSAndrew Geissler         callback);
18986adcd6dSAndrew Geissler }
190729dae72SJennifer Lee 
1910554c984SAndrew Geissler /**
1920554c984SAndrew Geissler  * UpdateServiceActionsSimpleUpdate class supports handle POST method for
1930554c984SAndrew Geissler  * SimpleUpdate action.
1940554c984SAndrew Geissler  */
1950554c984SAndrew Geissler class UpdateServiceActionsSimpleUpdate : public Node
1960554c984SAndrew Geissler {
1970554c984SAndrew Geissler   public:
1980554c984SAndrew Geissler     UpdateServiceActionsSimpleUpdate(CrowApp &app) :
1990554c984SAndrew Geissler         Node(app,
2000554c984SAndrew Geissler              "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
2010554c984SAndrew Geissler     {
2020554c984SAndrew Geissler         entityPrivileges = {
2030554c984SAndrew Geissler             {boost::beast::http::verb::get, {{"Login"}}},
2040554c984SAndrew Geissler             {boost::beast::http::verb::head, {{"Login"}}},
2050554c984SAndrew Geissler             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2060554c984SAndrew Geissler             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2070554c984SAndrew Geissler             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2080554c984SAndrew Geissler             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2090554c984SAndrew Geissler     }
2100554c984SAndrew Geissler 
2110554c984SAndrew Geissler   private:
2120554c984SAndrew Geissler     void doPost(crow::Response &res, const crow::Request &req,
2130554c984SAndrew Geissler                 const std::vector<std::string> &params) override
2140554c984SAndrew Geissler     {
2150554c984SAndrew Geissler         std::optional<std::string> transferProtocol;
2160554c984SAndrew Geissler         std::string imageURI;
2170554c984SAndrew Geissler         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2180554c984SAndrew Geissler 
2190554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
2200554c984SAndrew Geissler 
2210554c984SAndrew Geissler         // User can pass in both TransferProtocol and ImageURI parameters or
2220554c984SAndrew Geissler         // they can pass in just the ImageURI with the transfer protocl embedded
2230554c984SAndrew Geissler         // within it.
2240554c984SAndrew Geissler         // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
2250554c984SAndrew Geissler         // 2) ImageURI:tftp://1.1.1.1/myfile.bin
2260554c984SAndrew Geissler 
2270554c984SAndrew Geissler         if (!json_util::readJson(req, asyncResp->res, "TransferProtocol",
2280554c984SAndrew Geissler                                  transferProtocol, "ImageURI", imageURI))
2290554c984SAndrew Geissler         {
2300554c984SAndrew Geissler             BMCWEB_LOG_DEBUG
2310554c984SAndrew Geissler                 << "Missing TransferProtocol or ImageURI parameter";
2320554c984SAndrew Geissler             return;
2330554c984SAndrew Geissler         }
2340554c984SAndrew Geissler         if (!transferProtocol)
2350554c984SAndrew Geissler         {
2360554c984SAndrew Geissler             // Must be option 2
2370554c984SAndrew Geissler             // Verify ImageURI has transfer protocol in it
2380554c984SAndrew Geissler             size_t separator = imageURI.find(":");
2390554c984SAndrew Geissler             if ((separator == std::string::npos) ||
2400554c984SAndrew Geissler                 ((separator + 1) > imageURI.size()))
2410554c984SAndrew Geissler             {
2420554c984SAndrew Geissler                 messages::actionParameterValueTypeError(
2430554c984SAndrew Geissler                     asyncResp->res, imageURI, "ImageURI",
2440554c984SAndrew Geissler                     "UpdateService.SimpleUpdate");
2450554c984SAndrew Geissler                 BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
2460554c984SAndrew Geissler                                  << imageURI;
2470554c984SAndrew Geissler                 return;
2480554c984SAndrew Geissler             }
2490554c984SAndrew Geissler             transferProtocol = imageURI.substr(0, separator);
2500554c984SAndrew Geissler             // Ensure protocol is upper case for a common comparison path below
2510554c984SAndrew Geissler             boost::to_upper(*transferProtocol);
2520554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
2530554c984SAndrew Geissler                              << *transferProtocol;
2540554c984SAndrew Geissler 
2550554c984SAndrew Geissler             // Adjust imageURI to not have the protocol on it for parsing
2560554c984SAndrew Geissler             // below
2570554c984SAndrew Geissler             // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
2580554c984SAndrew Geissler             imageURI = imageURI.substr(separator + 3);
2590554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
2600554c984SAndrew Geissler         }
2610554c984SAndrew Geissler 
2620554c984SAndrew Geissler         // OpenBMC currently only supports TFTP
2630554c984SAndrew Geissler         if (*transferProtocol != "TFTP")
2640554c984SAndrew Geissler         {
2650554c984SAndrew Geissler             messages::actionParameterNotSupported(asyncResp->res,
2660554c984SAndrew Geissler                                                   "TransferProtocol",
2670554c984SAndrew Geissler                                                   "UpdateService.SimpleUpdate");
2680554c984SAndrew Geissler             BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
2690554c984SAndrew Geissler                              << *transferProtocol;
2700554c984SAndrew Geissler             return;
2710554c984SAndrew Geissler         }
2720554c984SAndrew Geissler 
2730554c984SAndrew Geissler         // Format should be <IP or Hostname>/<file> for imageURI
2740554c984SAndrew Geissler         size_t separator = imageURI.find("/");
2750554c984SAndrew Geissler         if ((separator == std::string::npos) ||
2760554c984SAndrew Geissler             ((separator + 1) > imageURI.size()))
2770554c984SAndrew Geissler         {
2780554c984SAndrew Geissler             messages::actionParameterValueTypeError(
2790554c984SAndrew Geissler                 asyncResp->res, imageURI, "ImageURI",
2800554c984SAndrew Geissler                 "UpdateService.SimpleUpdate");
2810554c984SAndrew Geissler             BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
2820554c984SAndrew Geissler             return;
2830554c984SAndrew Geissler         }
2840554c984SAndrew Geissler 
2850554c984SAndrew Geissler         std::string tftpServer = imageURI.substr(0, separator);
2860554c984SAndrew Geissler         std::string fwFile = imageURI.substr(separator + 1);
2870554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
2880554c984SAndrew Geissler 
2890554c984SAndrew Geissler         // Setup callback for when new software detected
2900554c984SAndrew Geissler         // Give TFTP 2 minutes to complete
2910554c984SAndrew Geissler         monitorForSoftwareAvailable(nullptr, req, 120);
2920554c984SAndrew Geissler 
2930554c984SAndrew Geissler         // TFTP can take up to 2 minutes depending on image size and
2940554c984SAndrew Geissler         // connection speed. Return to caller as soon as the TFTP operation
2950554c984SAndrew Geissler         // has been started. The callback above will ensure the activate
2960554c984SAndrew Geissler         // is started once the download has completed
2970554c984SAndrew Geissler         redfish::messages::success(asyncResp->res);
2980554c984SAndrew Geissler 
2990554c984SAndrew Geissler         // Call TFTP service
3000554c984SAndrew Geissler         crow::connections::systemBus->async_method_call(
3010554c984SAndrew Geissler             [](const boost::system::error_code ec) {
3020554c984SAndrew Geissler                 if (ec)
3030554c984SAndrew Geissler                 {
3040554c984SAndrew Geissler                     // messages::internalError(asyncResp->res);
3050554c984SAndrew Geissler                     cleanUp();
3060554c984SAndrew Geissler                     BMCWEB_LOG_DEBUG << "error_code = " << ec;
3070554c984SAndrew Geissler                     BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
3080554c984SAndrew Geissler                 }
3090554c984SAndrew Geissler                 else
3100554c984SAndrew Geissler                 {
3110554c984SAndrew Geissler                     BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
3120554c984SAndrew Geissler                 }
3130554c984SAndrew Geissler             },
3140554c984SAndrew Geissler             "xyz.openbmc_project.Software.Download",
3150554c984SAndrew Geissler             "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP",
3160554c984SAndrew Geissler             "DownloadViaTFTP", fwFile, tftpServer);
3170554c984SAndrew Geissler 
3180554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
3190554c984SAndrew Geissler     }
3200554c984SAndrew Geissler };
3210554c984SAndrew Geissler 
3221abe55efSEd Tanous class UpdateService : public Node
3231abe55efSEd Tanous {
324729dae72SJennifer Lee   public:
3251abe55efSEd Tanous     UpdateService(CrowApp &app) : Node(app, "/redfish/v1/UpdateService/")
3261abe55efSEd Tanous     {
327729dae72SJennifer Lee         entityPrivileges = {
328729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
329729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
330729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
331729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
332729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
333729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
334729dae72SJennifer Lee     }
335729dae72SJennifer Lee 
336729dae72SJennifer Lee   private:
33755c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
3381abe55efSEd Tanous                const std::vector<std::string> &params) override
3391abe55efSEd Tanous     {
340*274dfe62SJayashankar Padath         std::shared_ptr<AsyncResp> aResp = std::make_shared<AsyncResp>(res);
341*274dfe62SJayashankar Padath         res.jsonValue["@odata.type"] = "#UpdateService.v1_4_0.UpdateService";
3420f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
3430f74e643SEd Tanous         res.jsonValue["@odata.context"] =
3440f74e643SEd Tanous             "/redfish/v1/$metadata#UpdateService.UpdateService";
3450f74e643SEd Tanous         res.jsonValue["Id"] = "UpdateService";
3460f74e643SEd Tanous         res.jsonValue["Description"] = "Service for Software Update";
3470f74e643SEd Tanous         res.jsonValue["Name"] = "Update Service";
3480f74e643SEd Tanous         res.jsonValue["HttpPushUri"] = "/redfish/v1/UpdateService";
3490f74e643SEd Tanous         // UpdateService cannot be disabled
3500f74e643SEd Tanous         res.jsonValue["ServiceEnabled"] = true;
3510f74e643SEd Tanous         res.jsonValue["FirmwareInventory"] = {
3520f74e643SEd Tanous             {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
3530554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
3540554c984SAndrew Geissler         // Update Actions object.
3550554c984SAndrew Geissler         nlohmann::json &updateSvcSimpleUpdate =
3560554c984SAndrew Geissler             res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
3570554c984SAndrew Geissler         updateSvcSimpleUpdate["target"] =
3580554c984SAndrew Geissler             "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
3590554c984SAndrew Geissler         updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = {
3600554c984SAndrew Geissler             "TFTP"};
3610554c984SAndrew Geissler #endif
362*274dfe62SJayashankar Padath         // Get the current ApplyTime value
363*274dfe62SJayashankar Padath         crow::connections::systemBus->async_method_call(
364*274dfe62SJayashankar Padath             [aResp](const boost::system::error_code ec,
365*274dfe62SJayashankar Padath                     const std::variant<std::string> &applyTime) {
366*274dfe62SJayashankar Padath                 if (ec)
367*274dfe62SJayashankar Padath                 {
368*274dfe62SJayashankar Padath                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
369*274dfe62SJayashankar Padath                     messages::internalError(aResp->res);
370*274dfe62SJayashankar Padath                     return;
371*274dfe62SJayashankar Padath                 }
372*274dfe62SJayashankar Padath 
373*274dfe62SJayashankar Padath                 const std::string *s = std::get_if<std::string>(&applyTime);
374*274dfe62SJayashankar Padath                 if (s == nullptr)
375*274dfe62SJayashankar Padath                 {
376*274dfe62SJayashankar Padath                     return;
377*274dfe62SJayashankar Padath                 }
378*274dfe62SJayashankar Padath                 // Store the ApplyTime Value
379*274dfe62SJayashankar Padath                 if (*s == "xyz.openbmc_project.Software.ApplyTime."
380*274dfe62SJayashankar Padath                           "RequestedApplyTimes.Immediate")
381*274dfe62SJayashankar Padath                 {
382*274dfe62SJayashankar Padath                     aResp->res.jsonValue["HttpPushUriOptions"]
383*274dfe62SJayashankar Padath                                         ["HttpPushUriApplyTime"]["ApplyTime"] =
384*274dfe62SJayashankar Padath                         "Immediate";
385*274dfe62SJayashankar Padath                 }
386*274dfe62SJayashankar Padath                 else if (*s == "xyz.openbmc_project.Software.ApplyTime."
387*274dfe62SJayashankar Padath                                "RequestedApplyTimes.OnReset")
388*274dfe62SJayashankar Padath                 {
389*274dfe62SJayashankar Padath                     aResp->res.jsonValue["HttpPushUriOptions"]
390*274dfe62SJayashankar Padath                                         ["HttpPushUriApplyTime"]["ApplyTime"] =
391*274dfe62SJayashankar Padath                         "OnReset";
392*274dfe62SJayashankar Padath                 }
393*274dfe62SJayashankar Padath             },
394*274dfe62SJayashankar Padath             "xyz.openbmc_project.Settings",
395*274dfe62SJayashankar Padath             "/xyz/openbmc_project/software/apply_time",
396*274dfe62SJayashankar Padath             "org.freedesktop.DBus.Properties", "Get",
397*274dfe62SJayashankar Padath             "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime");
398729dae72SJennifer Lee     }
3990e7de46fSAndrew Geissler 
400fa1a5a38SJayashankar Padath     void doPatch(crow::Response &res, const crow::Request &req,
401fa1a5a38SJayashankar Padath                  const std::vector<std::string> &params) override
402fa1a5a38SJayashankar Padath     {
403fa1a5a38SJayashankar Padath         BMCWEB_LOG_DEBUG << "doPatch...";
404fa1a5a38SJayashankar Padath 
405fa1a5a38SJayashankar Padath         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
406fa1a5a38SJayashankar Padath 
407*274dfe62SJayashankar Padath         std::optional<nlohmann::json> pushUriOptions;
408*274dfe62SJayashankar Padath         if (!json_util::readJson(req, res, "HttpPushUriOptions",
409*274dfe62SJayashankar Padath                                  pushUriOptions))
410fa1a5a38SJayashankar Padath         {
411fa1a5a38SJayashankar Padath             return;
412fa1a5a38SJayashankar Padath         }
413fa1a5a38SJayashankar Padath 
414*274dfe62SJayashankar Padath         if (pushUriOptions)
415*274dfe62SJayashankar Padath         {
416*274dfe62SJayashankar Padath             std::optional<nlohmann::json> pushUriApplyTime;
417*274dfe62SJayashankar Padath             if (!json_util::readJson(*pushUriOptions, res,
418*274dfe62SJayashankar Padath                                      "HttpPushUriApplyTime", pushUriApplyTime))
419*274dfe62SJayashankar Padath             {
420*274dfe62SJayashankar Padath                 return;
421*274dfe62SJayashankar Padath             }
422*274dfe62SJayashankar Padath 
423*274dfe62SJayashankar Padath             if (pushUriApplyTime)
424*274dfe62SJayashankar Padath             {
425*274dfe62SJayashankar Padath                 std::optional<std::string> applyTime;
426*274dfe62SJayashankar Padath                 if (!json_util::readJson(*pushUriApplyTime, res, "ApplyTime",
427*274dfe62SJayashankar Padath                                          applyTime))
428*274dfe62SJayashankar Padath                 {
429*274dfe62SJayashankar Padath                     return;
430*274dfe62SJayashankar Padath                 }
431*274dfe62SJayashankar Padath 
432*274dfe62SJayashankar Padath                 if (applyTime)
433fa1a5a38SJayashankar Padath                 {
434fa1a5a38SJayashankar Padath                     std::string applyTimeNewVal;
435fa1a5a38SJayashankar Padath                     if (applyTime == "Immediate")
436fa1a5a38SJayashankar Padath                     {
437*274dfe62SJayashankar Padath                         applyTimeNewVal =
438*274dfe62SJayashankar Padath                             "xyz.openbmc_project.Software.ApplyTime."
439fa1a5a38SJayashankar Padath                             "RequestedApplyTimes.Immediate";
440fa1a5a38SJayashankar Padath                     }
441*274dfe62SJayashankar Padath                     else if (applyTime == "OnReset")
442*274dfe62SJayashankar Padath                     {
443*274dfe62SJayashankar Padath                         applyTimeNewVal =
444*274dfe62SJayashankar Padath                             "xyz.openbmc_project.Software.ApplyTime."
445*274dfe62SJayashankar Padath                             "RequestedApplyTimes.OnReset";
446*274dfe62SJayashankar Padath                     }
447fa1a5a38SJayashankar Padath                     else
448fa1a5a38SJayashankar Padath                     {
449*274dfe62SJayashankar Padath                         BMCWEB_LOG_INFO
450*274dfe62SJayashankar Padath                             << "ApplyTime value is not in the list of "
451*274dfe62SJayashankar Padath                                "acceptable values";
452*274dfe62SJayashankar Padath                         messages::propertyValueNotInList(
453*274dfe62SJayashankar Padath                             asyncResp->res, *applyTime, "ApplyTime");
454*274dfe62SJayashankar Padath                         return;
455fa1a5a38SJayashankar Padath                     }
456fa1a5a38SJayashankar Padath 
457fa1a5a38SJayashankar Padath                     // Set the requested image apply time value
458fa1a5a38SJayashankar Padath                     crow::connections::systemBus->async_method_call(
459fa1a5a38SJayashankar Padath                         [asyncResp](const boost::system::error_code ec) {
460fa1a5a38SJayashankar Padath                             if (ec)
461fa1a5a38SJayashankar Padath                             {
462*274dfe62SJayashankar Padath                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
463*274dfe62SJayashankar Padath                                                  << ec;
464fa1a5a38SJayashankar Padath                                 messages::internalError(asyncResp->res);
465fa1a5a38SJayashankar Padath                                 return;
466fa1a5a38SJayashankar Padath                             }
467fa1a5a38SJayashankar Padath                             messages::success(asyncResp->res);
468fa1a5a38SJayashankar Padath                         },
469fa1a5a38SJayashankar Padath                         "xyz.openbmc_project.Settings",
470fa1a5a38SJayashankar Padath                         "/xyz/openbmc_project/software/apply_time",
471fa1a5a38SJayashankar Padath                         "org.freedesktop.DBus.Properties", "Set",
472*274dfe62SJayashankar Padath                         "xyz.openbmc_project.Software.ApplyTime",
473*274dfe62SJayashankar Padath                         "RequestedApplyTime",
474fa1a5a38SJayashankar Padath                         std::variant<std::string>{applyTimeNewVal});
475fa1a5a38SJayashankar Padath                 }
476*274dfe62SJayashankar Padath             }
477fa1a5a38SJayashankar Padath         }
478fa1a5a38SJayashankar Padath     }
479fa1a5a38SJayashankar Padath 
480acb7cfb4SJennifer Lee     void doPost(crow::Response &res, const crow::Request &req,
4811abe55efSEd Tanous                 const std::vector<std::string> &params) override
4821abe55efSEd Tanous     {
483acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "doPost...";
484acb7cfb4SJennifer Lee 
4850e7de46fSAndrew Geissler         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
486acb7cfb4SJennifer Lee 
48786adcd6dSAndrew Geissler         // Setup callback for when new software detected
48886adcd6dSAndrew Geissler         monitorForSoftwareAvailable(asyncResp, req);
489acb7cfb4SJennifer Lee 
490acb7cfb4SJennifer Lee         std::string filepath(
491acb7cfb4SJennifer Lee             "/tmp/images/" +
492acb7cfb4SJennifer Lee             boost::uuids::to_string(boost::uuids::random_generator()()));
493acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
494acb7cfb4SJennifer Lee         std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
495acb7cfb4SJennifer Lee                                         std::ofstream::trunc);
496acb7cfb4SJennifer Lee         out << req.body;
497acb7cfb4SJennifer Lee         out.close();
498acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "file upload complete!!";
499acb7cfb4SJennifer Lee     }
500729dae72SJennifer Lee };
501729dae72SJennifer Lee 
5021abe55efSEd Tanous class SoftwareInventoryCollection : public Node
5031abe55efSEd Tanous {
504729dae72SJennifer Lee   public:
505729dae72SJennifer Lee     template <typename CrowApp>
5061abe55efSEd Tanous     SoftwareInventoryCollection(CrowApp &app) :
5071abe55efSEd Tanous         Node(app, "/redfish/v1/UpdateService/FirmwareInventory/")
5081abe55efSEd Tanous     {
509729dae72SJennifer Lee         entityPrivileges = {
510729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
511729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
512729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
513729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
514729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
515729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
516729dae72SJennifer Lee     }
517729dae72SJennifer Lee 
518729dae72SJennifer Lee   private:
51955c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
5201abe55efSEd Tanous                const std::vector<std::string> &params) override
5211abe55efSEd Tanous     {
522c711bf86SEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
5230f74e643SEd Tanous         res.jsonValue["@odata.type"] =
5240f74e643SEd Tanous             "#SoftwareInventoryCollection.SoftwareInventoryCollection";
5250f74e643SEd Tanous         res.jsonValue["@odata.id"] =
5260f74e643SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory";
5270f74e643SEd Tanous         res.jsonValue["@odata.context"] =
5280f74e643SEd Tanous             "/redfish/v1/"
5290f74e643SEd Tanous             "$metadata#SoftwareInventoryCollection.SoftwareInventoryCollection";
5300f74e643SEd Tanous         res.jsonValue["Name"] = "Software Inventory Collection";
531c711bf86SEd Tanous 
532c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
533c711bf86SEd Tanous             [asyncResp](
534c711bf86SEd Tanous                 const boost::system::error_code ec,
5356c4eb9deSJennifer Lee                 const std::vector<std::pair<
5361abe55efSEd Tanous                     std::string, std::vector<std::pair<
5371abe55efSEd Tanous                                      std::string, std::vector<std::string>>>>>
5386c4eb9deSJennifer Lee                     &subtree) {
5391abe55efSEd Tanous                 if (ec)
5401abe55efSEd Tanous                 {
541f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
5426c4eb9deSJennifer Lee                     return;
543729dae72SJennifer Lee                 }
544c711bf86SEd Tanous                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
545c711bf86SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
5466c4eb9deSJennifer Lee 
5471abe55efSEd Tanous                 for (auto &obj : subtree)
5481abe55efSEd Tanous                 {
549f4b65ab1SJennifer Lee                     // if can't parse fw id then return
55027826b5fSEd Tanous                     std::size_t idPos;
55127826b5fSEd Tanous                     if ((idPos = obj.first.rfind("/")) == std::string::npos)
552f4b65ab1SJennifer Lee                     {
553f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
554f4b65ab1SJennifer Lee                         BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
555f4b65ab1SJennifer Lee                         return;
556f4b65ab1SJennifer Lee                     }
557f4b65ab1SJennifer Lee                     std::string swId = obj.first.substr(idPos + 1);
558f4b65ab1SJennifer Lee 
559c711bf86SEd Tanous                     nlohmann::json &members =
560c711bf86SEd Tanous                         asyncResp->res.jsonValue["Members"];
561c711bf86SEd Tanous                     members.push_back(
562f4b65ab1SJennifer Lee                         {{"@odata.id", "/redfish/v1/UpdateService/"
5631abe55efSEd Tanous                                        "FirmwareInventory/" +
564f4b65ab1SJennifer Lee                                            swId}});
565e0dd8057SAndrew Geissler                     asyncResp->res.jsonValue["Members@odata.count"] =
566c711bf86SEd Tanous                         members.size();
5676c4eb9deSJennifer Lee                 }
568c711bf86SEd Tanous             },
569c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
570c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
571271584abSEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
572271584abSEd Tanous             static_cast<int32_t>(0),
5731abe55efSEd Tanous             std::array<const char *, 1>{
5741abe55efSEd Tanous                 "xyz.openbmc_project.Software.Version"});
575729dae72SJennifer Lee     }
576729dae72SJennifer Lee };
577c711bf86SEd Tanous 
5781abe55efSEd Tanous class SoftwareInventory : public Node
5791abe55efSEd Tanous {
580729dae72SJennifer Lee   public:
581729dae72SJennifer Lee     template <typename CrowApp>
5821abe55efSEd Tanous     SoftwareInventory(CrowApp &app) :
5831abe55efSEd Tanous         Node(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/",
5841abe55efSEd Tanous              std::string())
5851abe55efSEd Tanous     {
586729dae72SJennifer Lee         entityPrivileges = {
587729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
588729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
589729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
590729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
591729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
592729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
593729dae72SJennifer Lee     }
594729dae72SJennifer Lee 
595729dae72SJennifer Lee   private:
59687d84729SAndrew Geissler     /* Fill related item links (i.e. bmc, bios) in for inventory */
59787d84729SAndrew Geissler     static void getRelatedItems(std::shared_ptr<AsyncResp> aResp,
59887d84729SAndrew Geissler                                 const std::string &purpose)
59987d84729SAndrew Geissler     {
60087d84729SAndrew Geissler         if (purpose == fw_util::bmcPurpose)
60187d84729SAndrew Geissler         {
60287d84729SAndrew Geissler             nlohmann::json &members = aResp->res.jsonValue["RelatedItem"];
60387d84729SAndrew Geissler             members.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}});
60487d84729SAndrew Geissler             aResp->res.jsonValue["Members@odata.count"] = members.size();
60587d84729SAndrew Geissler         }
60687d84729SAndrew Geissler         else if (purpose == fw_util::biosPurpose)
60787d84729SAndrew Geissler         {
60887d84729SAndrew Geissler             // TODO(geissonator) Need BIOS schema support added for this
60987d84729SAndrew Geissler             //                   to be valid
61087d84729SAndrew Geissler             // nlohmann::json &members = aResp->res.jsonValue["RelatedItem"];
61187d84729SAndrew Geissler             // members.push_back(
61287d84729SAndrew Geissler             //    {{"@odata.id", "/redfish/v1/Systems/system/BIOS"}});
61387d84729SAndrew Geissler             // aResp->res.jsonValue["Members@odata.count"] = members.size();
61487d84729SAndrew Geissler         }
61587d84729SAndrew Geissler         else
61687d84729SAndrew Geissler         {
61787d84729SAndrew Geissler             BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
61887d84729SAndrew Geissler         }
61987d84729SAndrew Geissler     }
62087d84729SAndrew Geissler 
62155c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
6221abe55efSEd Tanous                const std::vector<std::string> &params) override
6231abe55efSEd Tanous     {
624c711bf86SEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
6256c4eb9deSJennifer Lee 
6261abe55efSEd Tanous         if (params.size() != 1)
6271abe55efSEd Tanous         {
628f12894f8SJason M. Bills             messages::internalError(res);
629729dae72SJennifer Lee             res.end();
630729dae72SJennifer Lee             return;
631729dae72SJennifer Lee         }
632729dae72SJennifer Lee 
6333ae837c9SEd Tanous         std::shared_ptr<std::string> swId =
634c711bf86SEd Tanous             std::make_shared<std::string>(params[0]);
635c711bf86SEd Tanous 
63655c7b7a2SEd Tanous         res.jsonValue["@odata.id"] =
6373ae837c9SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
638c711bf86SEd Tanous 
639c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
6403ae837c9SEd Tanous             [asyncResp, swId](
641c711bf86SEd Tanous                 const boost::system::error_code ec,
6426c4eb9deSJennifer Lee                 const std::vector<std::pair<
6431abe55efSEd Tanous                     std::string, std::vector<std::pair<
6441abe55efSEd Tanous                                      std::string, std::vector<std::string>>>>>
6456c4eb9deSJennifer Lee                     &subtree) {
64655c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "doGet callback...";
6471abe55efSEd Tanous                 if (ec)
6481abe55efSEd Tanous                 {
649f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
6506c4eb9deSJennifer Lee                     return;
6516c4eb9deSJennifer Lee                 }
6526c4eb9deSJennifer Lee 
6536913228dSAndrew Geissler                 // Ensure we find our input swId, otherwise return an error
6546913228dSAndrew Geissler                 bool found = false;
6551abe55efSEd Tanous                 for (const std::pair<
6561abe55efSEd Tanous                          std::string,
6571abe55efSEd Tanous                          std::vector<
6581abe55efSEd Tanous                              std::pair<std::string, std::vector<std::string>>>>
6591abe55efSEd Tanous                          &obj : subtree)
6601abe55efSEd Tanous                 {
6613ae837c9SEd Tanous                     if (boost::ends_with(obj.first, *swId) != true)
6621abe55efSEd Tanous                     {
663acb7cfb4SJennifer Lee                         continue;
664acb7cfb4SJennifer Lee                     }
665acb7cfb4SJennifer Lee 
666f4b65ab1SJennifer Lee                     if (obj.second.size() < 1)
6671abe55efSEd Tanous                     {
668acb7cfb4SJennifer Lee                         continue;
669acb7cfb4SJennifer Lee                     }
6706c4eb9deSJennifer Lee 
6716913228dSAndrew Geissler                     found = true;
672e0dd8057SAndrew Geissler                     fw_util::getFwStatus(asyncResp, swId, obj.second[0].first);
673e0dd8057SAndrew Geissler 
67455c7b7a2SEd Tanous                     crow::connections::systemBus->async_method_call(
6751abe55efSEd Tanous                         [asyncResp,
6763ae837c9SEd Tanous                          swId](const boost::system::error_code error_code,
6771abe55efSEd Tanous                                const boost::container::flat_map<
6781abe55efSEd Tanous                                    std::string, VariantType> &propertiesList) {
6791abe55efSEd Tanous                             if (error_code)
6801abe55efSEd Tanous                             {
681f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
6826c4eb9deSJennifer Lee                                 return;
6836c4eb9deSJennifer Lee                             }
6841abe55efSEd Tanous                             boost::container::flat_map<
6851abe55efSEd Tanous                                 std::string, VariantType>::const_iterator it =
6866c4eb9deSJennifer Lee                                 propertiesList.find("Purpose");
6871abe55efSEd Tanous                             if (it == propertiesList.end())
6881abe55efSEd Tanous                             {
6891abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
6901abe55efSEd Tanous                                     << "Can't find property \"Purpose\"!";
691f12894f8SJason M. Bills                                 messages::propertyMissing(asyncResp->res,
692f12894f8SJason M. Bills                                                           "Purpose");
6936c4eb9deSJennifer Lee                                 return;
6946c4eb9deSJennifer Lee                             }
6953ae837c9SEd Tanous                             const std::string *swInvPurpose =
696abf2add6SEd Tanous                                 std::get_if<std::string>(&it->second);
6973ae837c9SEd Tanous                             if (swInvPurpose == nullptr)
6981abe55efSEd Tanous                             {
6991abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
7001abe55efSEd Tanous                                     << "wrong types for property\"Purpose\"!";
701f12894f8SJason M. Bills                                 messages::propertyValueTypeError(asyncResp->res,
702f12894f8SJason M. Bills                                                                  "", "Purpose");
703acb7cfb4SJennifer Lee                                 return;
704acb7cfb4SJennifer Lee                             }
705c711bf86SEd Tanous 
7063ae837c9SEd Tanous                             BMCWEB_LOG_DEBUG << "swInvPurpose = "
7073ae837c9SEd Tanous                                              << *swInvPurpose;
708c711bf86SEd Tanous                             it = propertiesList.find("Version");
7091abe55efSEd Tanous                             if (it == propertiesList.end())
7101abe55efSEd Tanous                             {
7111abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
7121abe55efSEd Tanous                                     << "Can't find property \"Version\"!";
713f12894f8SJason M. Bills                                 messages::propertyMissing(asyncResp->res,
714f12894f8SJason M. Bills                                                           "Version");
715c711bf86SEd Tanous                                 return;
716acb7cfb4SJennifer Lee                             }
717acb7cfb4SJennifer Lee 
718f4b65ab1SJennifer Lee                             BMCWEB_LOG_DEBUG << "Version found!";
719c711bf86SEd Tanous 
720f4b65ab1SJennifer Lee                             const std::string *version =
721abf2add6SEd Tanous                                 std::get_if<std::string>(&it->second);
722f4b65ab1SJennifer Lee 
723f4b65ab1SJennifer Lee                             if (version == nullptr)
7241abe55efSEd Tanous                             {
7251abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
7261abe55efSEd Tanous                                     << "Can't find property \"Version\"!";
727f12894f8SJason M. Bills 
728f12894f8SJason M. Bills                                 messages::propertyValueTypeError(asyncResp->res,
729f12894f8SJason M. Bills                                                                  "", "Version");
7306c4eb9deSJennifer Lee                                 return;
7316c4eb9deSJennifer Lee                             }
732c711bf86SEd Tanous                             asyncResp->res.jsonValue["Version"] = *version;
7333ae837c9SEd Tanous                             asyncResp->res.jsonValue["Id"] = *swId;
73454daabe7SAndrew Geissler 
73554daabe7SAndrew Geissler                             // swInvPurpose is of format:
73654daabe7SAndrew Geissler                             // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
737e2e96770SJames Feist                             // Translate this to "ABC image"
73854daabe7SAndrew Geissler                             size_t endDesc = swInvPurpose->rfind(".");
73954daabe7SAndrew Geissler                             if (endDesc == std::string::npos)
74054daabe7SAndrew Geissler                             {
74154daabe7SAndrew Geissler                                 messages::internalError(asyncResp->res);
74254daabe7SAndrew Geissler                                 return;
74354daabe7SAndrew Geissler                             }
74454daabe7SAndrew Geissler                             endDesc++;
74554daabe7SAndrew Geissler                             if (endDesc >= swInvPurpose->size())
74654daabe7SAndrew Geissler                             {
74754daabe7SAndrew Geissler                                 messages::internalError(asyncResp->res);
74854daabe7SAndrew Geissler                                 return;
74954daabe7SAndrew Geissler                             }
75054daabe7SAndrew Geissler 
75154daabe7SAndrew Geissler                             std::string formatDesc =
75254daabe7SAndrew Geissler                                 swInvPurpose->substr(endDesc);
75354daabe7SAndrew Geissler                             asyncResp->res.jsonValue["Description"] =
754e2e96770SJames Feist                                 formatDesc + " image";
75587d84729SAndrew Geissler                             getRelatedItems(asyncResp, *swInvPurpose);
7566c4eb9deSJennifer Lee                         },
757c711bf86SEd Tanous                         obj.second[0].first, obj.first,
758c711bf86SEd Tanous                         "org.freedesktop.DBus.Properties", "GetAll",
759c711bf86SEd Tanous                         "xyz.openbmc_project.Software.Version");
7606c4eb9deSJennifer Lee                 }
7616913228dSAndrew Geissler                 if (!found)
7626913228dSAndrew Geissler                 {
7636913228dSAndrew Geissler                     BMCWEB_LOG_ERROR << "Input swID " + *swId + " not found!";
7646913228dSAndrew Geissler                     messages::resourceMissingAtURI(
7656913228dSAndrew Geissler                         asyncResp->res,
7666913228dSAndrew Geissler                         "/redfish/v1/UpdateService/FirmwareInventory/" + *swId);
7676913228dSAndrew Geissler                     return;
7686913228dSAndrew Geissler                 }
7694e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["@odata.type"] =
7704e68c45bSAyushi Smriti                     "#SoftwareInventory.v1_1_0.SoftwareInventory";
7714e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["@odata.context"] =
7724e68c45bSAyushi Smriti                     "/redfish/v1/$metadata#SoftwareInventory.SoftwareInventory";
7734e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["Name"] = "Software Inventory";
7744e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["Updateable"] = false;
7754e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
7764e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
777c711bf86SEd Tanous             },
778c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
779c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
780271584abSEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
781271584abSEd Tanous             static_cast<int32_t>(0),
7821abe55efSEd Tanous             std::array<const char *, 1>{
7831abe55efSEd Tanous                 "xyz.openbmc_project.Software.Version"});
7846c4eb9deSJennifer Lee     }
785729dae72SJennifer Lee };
786729dae72SJennifer Lee 
787729dae72SJennifer Lee } // namespace redfish
788