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