blob: df6f56c4a1f9249da63f303446a54d7880bf8e16 [file] [log] [blame]
Ed Tanousc3ee5222018-05-01 12:58:27 -07001#pragma once
2
Ed Tanous04e438c2020-10-03 08:06:26 -07003#include <app.hpp>
Ed Tanousc3ee5222018-05-01 12:58:27 -07004#include <boost/uuid/uuid.hpp>
5#include <boost/uuid/uuid_generators.hpp>
6#include <boost/uuid/uuid_io.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07007#include <dbus_singleton.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05008
9#include <cstdio>
Ed Tanous1abe55e2018-09-05 08:30:59 -070010#include <fstream>
11#include <memory>
Ed Tanousc3ee5222018-05-01 12:58:27 -070012
Ed Tanous1abe55e2018-09-05 08:30:59 -070013namespace crow
14{
15namespace image_upload
16{
Ed Tanousc3ee5222018-05-01 12:58:27 -070017
Ed Tanous23a21a12020-07-25 04:45:05 +000018static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;
Ed Tanousc3ee5222018-05-01 12:58:27 -070019
Ed Tanouscb13a392020-07-25 19:02:03 +000020inline void uploadImageHandler(const crow::Request& req, crow::Response& res)
Ed Tanous1abe55e2018-09-05 08:30:59 -070021{
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 Tanousc3ee5222018-05-01 12:58:27 -070027 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -070028 return;
29 }
30 // Make this const static so it survives outside this method
Ed Tanous271584a2019-07-09 16:24:22 -070031 static boost::asio::steady_timer timeout(*req.ioService,
32 std::chrono::seconds(5));
Ed Tanous1abe55e2018-09-05 08:30:59 -070033
Ed Tanous271584a2019-07-09 16:24:22 -070034 timeout.expires_after(std::chrono::seconds(15));
Ed Tanous1abe55e2018-09-05 08:30:59 -070035
Lei YU9f898f82019-03-08 16:52:10 +080036 auto timeoutHandler = [&res](const boost::system::error_code& ec) {
Ed Tanousc3ee5222018-05-01 12:58:27 -070037 fwUpdateMatcher = nullptr;
Ed Tanous23e64202020-09-15 19:21:30 -070038 if (ec == boost::asio::error::operation_aborted)
Ed Tanous1abe55e2018-09-05 08:30:59 -070039 {
40 // expected, we were canceled before the timer completed.
41 return;
42 }
Matt Spinlerc9008502019-01-21 12:21:25 -060043 BMCWEB_LOG_ERROR << "Timed out waiting for Version interface";
Ed Tanousc3ee5222018-05-01 12:58:27 -070044
Ed Tanous1abe55e2018-09-05 08:30:59 -070045 if (ec)
46 {
47 BMCWEB_LOG_ERROR << "Async_wait failed " << ec;
48 return;
49 }
50
Matt Spinlerc9008502019-01-21 12:21:25 -060051 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 Tanous1abe55e2018-09-05 08:30:59 -070058 res.end();
Lei YU9f898f82019-03-08 16:52:10 +080059 };
Ed Tanous1abe55e2018-09-05 08:30:59 -070060
61 std::function<void(sdbusplus::message::message&)> callback =
62 [&res](sdbusplus::message::message& m) {
63 BMCWEB_LOG_DEBUG << "Match fired";
Ed Tanous1abe55e2018-09-05 08:30:59 -070064
Matt Spinlerc9008502019-01-21 12:21:25 -060065 sdbusplus::message::object_path path;
66 std::vector<std::pair<
67 std::string,
Ed Tanousabf2add2019-01-22 16:40:12 -080068 std::vector<std::pair<std::string, std::variant<std::string>>>>>
Matt Spinlerc9008502019-01-21 12:21:25 -060069 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 Tanous1abe55e2018-09-05 08:30:59 -070077 {
Ed Tanous271584a2019-07-09 16:24:22 -070078 timeout.cancel();
Matt Spinlerc9008502019-01-21 12:21:25 -060079
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 Tanous1abe55e2018-09-05 08:30:59 -070091 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070092 };
93 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
94 *crow::connections::systemBus,
95 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
Matt Spinlerc9008502019-01-21 12:21:25 -060096 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
Ed Tanous1abe55e2018-09-05 08:30:59 -070097 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 YU9f898f82019-03-08 16:52:10 +0800107 timeout.async_wait(timeoutHandler);
Ed Tanousc3ee5222018-05-01 12:58:27 -0700108}
109
Ed Tanous23a21a12020-07-25 04:45:05 +0000110inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111{
112 BMCWEB_ROUTE(app, "/upload/image/<str>")
Ed Tanous23a21a12020-07-25 04:45:05 +0000113 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700114 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
115 [](const crow::Request& req, crow::Response& res,
Ed Tanouscb13a392020-07-25 19:02:03 +0000116 const std::string&) { uploadImageHandler(req, res); });
Ed Tanousc3ee5222018-05-01 12:58:27 -0700117
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118 BMCWEB_ROUTE(app, "/upload/image")
Ed Tanous23a21a12020-07-25 04:45:05 +0000119 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700120 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700121 [](const crow::Request& req, crow::Response& res) {
Ed Tanouscb13a392020-07-25 19:02:03 +0000122 uploadImageHandler(req, res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700123 });
Ed Tanousc3ee5222018-05-01 12:58:27 -0700124}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700125} // namespace image_upload
126} // namespace crow