Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 3 | #include <app.h> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 4 | |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 5 | #include <boost/uuid/uuid.hpp> |
| 6 | #include <boost/uuid/uuid_generators.hpp> |
| 7 | #include <boost/uuid/uuid_io.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 8 | #include <dbus_singleton.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 | |
| 19 | std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher; |
| 20 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 21 | inline void uploadImageHandler(const crow::Request& req, crow::Response& res, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 22 | const std::string& filename) |
| 23 | { |
| 24 | // Only allow one FW update at a time |
| 25 | if (fwUpdateMatcher != nullptr) |
| 26 | { |
| 27 | res.addHeader("Retry-After", "30"); |
| 28 | res.result(boost::beast::http::status::service_unavailable); |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 29 | res.end(); |
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 | |
Lei YU | 9f898f8 | 2019-03-08 16:52:10 +0800 | [diff] [blame] | 38 | auto timeoutHandler = [&res](const boost::system::error_code& ec) { |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 39 | fwUpdateMatcher = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | if (ec == asio::error::operation_aborted) |
| 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 | |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 53 | res.result(boost::beast::http::status::bad_request); |
| 54 | res.jsonValue = { |
| 55 | {"data", |
| 56 | {{"description", |
| 57 | "Version already exists or failed to be extracted"}}}, |
| 58 | {"message", "400 Bad Request"}, |
| 59 | {"status", "error"}}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 60 | res.end(); |
Lei YU | 9f898f8 | 2019-03-08 16:52:10 +0800 | [diff] [blame] | 61 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 62 | |
| 63 | std::function<void(sdbusplus::message::message&)> callback = |
| 64 | [&res](sdbusplus::message::message& m) { |
| 65 | BMCWEB_LOG_DEBUG << "Match fired"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 67 | sdbusplus::message::object_path path; |
| 68 | std::vector<std::pair< |
| 69 | std::string, |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 70 | std::vector<std::pair<std::string, std::variant<std::string>>>>> |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 71 | interfaces; |
| 72 | m.read(path, interfaces); |
| 73 | |
| 74 | if (std::find_if(interfaces.begin(), interfaces.end(), |
| 75 | [](const auto& i) { |
| 76 | return i.first == |
| 77 | "xyz.openbmc_project.Software.Version"; |
| 78 | }) != interfaces.end()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 79 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 80 | timeout.cancel(); |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 81 | |
| 82 | std::size_t index = path.str.rfind('/'); |
| 83 | if (index != std::string::npos) |
| 84 | { |
| 85 | path.str.erase(0, index + 1); |
| 86 | } |
| 87 | res.jsonValue = {{"data", std::move(path.str)}, |
| 88 | {"message", "200 OK"}, |
| 89 | {"status", "ok"}}; |
| 90 | BMCWEB_LOG_DEBUG << "ending response"; |
| 91 | res.end(); |
| 92 | fwUpdateMatcher = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 93 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 94 | }; |
| 95 | fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>( |
| 96 | *crow::connections::systemBus, |
| 97 | "interface='org.freedesktop.DBus.ObjectManager',type='signal'," |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 98 | "member='InterfacesAdded',path='/xyz/openbmc_project/software'", |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 99 | callback); |
| 100 | |
| 101 | std::string filepath( |
| 102 | "/tmp/images/" + |
| 103 | boost::uuids::to_string(boost::uuids::random_generator()())); |
| 104 | BMCWEB_LOG_DEBUG << "Writing file to " << filepath; |
| 105 | std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | |
| 106 | std::ofstream::trunc); |
| 107 | out << req.body; |
| 108 | out.close(); |
Lei YU | 9f898f8 | 2019-03-08 16:52:10 +0800 | [diff] [blame] | 109 | timeout.async_wait(timeoutHandler); |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 112 | template <typename... Middlewares> |
| 113 | void requestRoutes(Crow<Middlewares...>& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 114 | { |
| 115 | BMCWEB_ROUTE(app, "/upload/image/<str>") |
Ed Tanous | 8251ffe | 2019-10-10 14:33:54 -0700 | [diff] [blame] | 116 | .requires({"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)( |
| 118 | [](const crow::Request& req, crow::Response& res, |
| 119 | const std::string& filename) { |
| 120 | uploadImageHandler(req, res, filename); |
| 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 | BMCWEB_ROUTE(app, "/upload/image") |
Ed Tanous | 8251ffe | 2019-10-10 14:33:54 -0700 | [diff] [blame] | 124 | .requires({"ConfigureComponents", "ConfigureManager"}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 125 | .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 126 | [](const crow::Request& req, crow::Response& res) { |
| 127 | uploadImageHandler(req, res, ""); |
| 128 | }); |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 129 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 130 | } // namespace image_upload |
| 131 | } // namespace crow |