Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 3 | #include <app.hpp> |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 4 | #include <boost/uuid/uuid.hpp> |
| 5 | #include <boost/uuid/uuid_generators.hpp> |
| 6 | #include <boost/uuid/uuid_io.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 7 | #include <dbus_singleton.hpp> |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 8 | #include <dbus_utility.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 9 | |
| 10 | #include <cstdio> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 11 | #include <fstream> |
| 12 | #include <memory> |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 13 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 14 | namespace crow |
| 15 | { |
| 16 | namespace image_upload |
| 17 | { |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 18 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 19 | static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher; |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 20 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 21 | inline void |
| 22 | uploadImageHandler(const crow::Request& req, |
| 23 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 24 | { |
| 25 | // Only allow one FW update at a time |
| 26 | if (fwUpdateMatcher != nullptr) |
| 27 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 28 | asyncResp->res.addHeader("Retry-After", "30"); |
| 29 | asyncResp->res.result(boost::beast::http::status::service_unavailable); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | return; |
| 31 | } |
| 32 | // Make this const static so it survives outside this method |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 33 | static boost::asio::steady_timer timeout(*req.ioService, |
| 34 | std::chrono::seconds(5)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 35 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 36 | timeout.expires_after(std::chrono::seconds(15)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 37 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 38 | auto timeoutHandler = [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 39 | fwUpdateMatcher = nullptr; |
Ed Tanous | 23e6420 | 2020-09-15 19:21:30 -0700 | [diff] [blame] | 40 | if (ec == boost::asio::error::operation_aborted) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 41 | { |
| 42 | // expected, we were canceled before the timer completed. |
| 43 | return; |
| 44 | } |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 45 | BMCWEB_LOG_ERROR << "Timed out waiting for Version interface"; |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 46 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 47 | if (ec) |
| 48 | { |
| 49 | BMCWEB_LOG_ERROR << "Async_wait failed " << ec; |
| 50 | return; |
| 51 | } |
| 52 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 53 | asyncResp->res.result(boost::beast::http::status::bad_request); |
| 54 | asyncResp->res.jsonValue = { |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 55 | {"data", |
| 56 | {{"description", |
| 57 | "Version already exists or failed to be extracted"}}}, |
| 58 | {"message", "400 Bad Request"}, |
| 59 | {"status", "error"}}; |
Lei YU | 9f898f8 | 2019-03-08 16:52:10 +0800 | [diff] [blame] | 60 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 61 | |
| 62 | std::function<void(sdbusplus::message::message&)> callback = |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 63 | [asyncResp](sdbusplus::message::message& m) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | BMCWEB_LOG_DEBUG << "Match fired"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 65 | |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 66 | sdbusplus::message::object_path path; |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 67 | dbus::utility::DBusInteracesMap interfaces; |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 68 | m.read(path, interfaces); |
| 69 | |
| 70 | if (std::find_if(interfaces.begin(), interfaces.end(), |
| 71 | [](const auto& i) { |
| 72 | return i.first == |
| 73 | "xyz.openbmc_project.Software.Version"; |
| 74 | }) != interfaces.end()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 75 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 76 | timeout.cancel(); |
Ed Tanous | 2dfd18e | 2020-12-18 00:41:31 +0000 | [diff] [blame] | 77 | std::string leaf = path.filename(); |
| 78 | if (leaf.empty()) |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 79 | { |
Ed Tanous | 2dfd18e | 2020-12-18 00:41:31 +0000 | [diff] [blame] | 80 | leaf = path.str; |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 81 | } |
Ed Tanous | 2dfd18e | 2020-12-18 00:41:31 +0000 | [diff] [blame] | 82 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 83 | asyncResp->res.jsonValue = { |
Ed Tanous | 2dfd18e | 2020-12-18 00:41:31 +0000 | [diff] [blame] | 84 | {"data", leaf}, {"message", "200 OK"}, {"status", "ok"}}; |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 85 | BMCWEB_LOG_DEBUG << "ending response"; |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 86 | fwUpdateMatcher = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 87 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 88 | }; |
| 89 | fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>( |
| 90 | *crow::connections::systemBus, |
| 91 | "interface='org.freedesktop.DBus.ObjectManager',type='signal'," |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 92 | "member='InterfacesAdded',path='/xyz/openbmc_project/software'", |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 93 | callback); |
| 94 | |
| 95 | std::string filepath( |
| 96 | "/tmp/images/" + |
| 97 | boost::uuids::to_string(boost::uuids::random_generator()())); |
| 98 | BMCWEB_LOG_DEBUG << "Writing file to " << filepath; |
| 99 | std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | |
| 100 | std::ofstream::trunc); |
| 101 | out << req.body; |
| 102 | out.close(); |
Lei YU | 9f898f8 | 2019-03-08 16:52:10 +0800 | [diff] [blame] | 103 | timeout.async_wait(timeoutHandler); |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 106 | inline void requestRoutes(App& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 107 | { |
| 108 | BMCWEB_ROUTE(app, "/upload/image/<str>") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 109 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 110 | .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 111 | [](const crow::Request& req, |
| 112 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 113 | const std::string&) { uploadImageHandler(req, asyncResp); }); |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 114 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 115 | BMCWEB_ROUTE(app, "/upload/image") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 116 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 117 | .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 118 | [](const crow::Request& req, |
| 119 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 120 | uploadImageHandler(req, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 121 | }); |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 122 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 123 | } // namespace image_upload |
| 124 | } // namespace crow |