1 #pragma once 2 3 #include <app.h> 4 5 #include <boost/uuid/uuid.hpp> 6 #include <boost/uuid/uuid_generators.hpp> 7 #include <boost/uuid/uuid_io.hpp> 8 #include <dbus_singleton.hpp> 9 10 #include <cstdio> 11 #include <fstream> 12 #include <memory> 13 14 namespace crow 15 { 16 namespace image_upload 17 { 18 19 static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher; 20 21 inline void uploadImageHandler(const crow::Request& req, crow::Response& res) 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); 28 res.end(); 29 return; 30 } 31 // Make this const static so it survives outside this method 32 static boost::asio::steady_timer timeout(*req.ioService, 33 std::chrono::seconds(5)); 34 35 timeout.expires_after(std::chrono::seconds(15)); 36 37 auto timeoutHandler = [&res](const boost::system::error_code& ec) { 38 fwUpdateMatcher = nullptr; 39 if (ec == boost::asio::error::operation_aborted) 40 { 41 // expected, we were canceled before the timer completed. 42 return; 43 } 44 BMCWEB_LOG_ERROR << "Timed out waiting for Version interface"; 45 46 if (ec) 47 { 48 BMCWEB_LOG_ERROR << "Async_wait failed " << ec; 49 return; 50 } 51 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"}}; 59 res.end(); 60 }; 61 62 std::function<void(sdbusplus::message::message&)> callback = 63 [&res](sdbusplus::message::message& m) { 64 BMCWEB_LOG_DEBUG << "Match fired"; 65 66 sdbusplus::message::object_path path; 67 std::vector<std::pair< 68 std::string, 69 std::vector<std::pair<std::string, std::variant<std::string>>>>> 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()) 78 { 79 timeout.cancel(); 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; 92 } 93 }; 94 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>( 95 *crow::connections::systemBus, 96 "interface='org.freedesktop.DBus.ObjectManager',type='signal'," 97 "member='InterfacesAdded',path='/xyz/openbmc_project/software'", 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(); 108 timeout.async_wait(timeoutHandler); 109 } 110 111 inline void requestRoutes(App& app) 112 { 113 BMCWEB_ROUTE(app, "/upload/image/<str>") 114 .privileges({"ConfigureComponents", "ConfigureManager"}) 115 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)( 116 [](const crow::Request& req, crow::Response& res, 117 const std::string&) { uploadImageHandler(req, res); }); 118 119 BMCWEB_ROUTE(app, "/upload/image") 120 .privileges({"ConfigureComponents", "ConfigureManager"}) 121 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)( 122 [](const crow::Request& req, crow::Response& res) { 123 uploadImageHandler(req, res); 124 }); 125 } 126 } // namespace image_upload 127 } // namespace crow 128