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