blob: 1a0ace04bb5ddfa4dbaaf9eea00ba37d6493d085 [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
zhanghch058d1b46d2021-04-01 11:18:24 +080020inline void
21 uploadImageHandler(const crow::Request& req,
22 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -070023{
24 // Only allow one FW update at a time
25 if (fwUpdateMatcher != nullptr)
26 {
zhanghch058d1b46d2021-04-01 11:18:24 +080027 asyncResp->res.addHeader("Retry-After", "30");
28 asyncResp->res.result(boost::beast::http::status::service_unavailable);
Ed Tanous1abe55e2018-09-05 08:30:59 -070029 return;
30 }
31 // Make this const static so it survives outside this method
Ed Tanous271584a2019-07-09 16:24:22 -070032 static boost::asio::steady_timer timeout(*req.ioService,
33 std::chrono::seconds(5));
Ed Tanous1abe55e2018-09-05 08:30:59 -070034
Ed Tanous271584a2019-07-09 16:24:22 -070035 timeout.expires_after(std::chrono::seconds(15));
Ed Tanous1abe55e2018-09-05 08:30:59 -070036
zhanghch058d1b46d2021-04-01 11:18:24 +080037 auto timeoutHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanousc3ee5222018-05-01 12:58:27 -070038 fwUpdateMatcher = nullptr;
Ed Tanous23e64202020-09-15 19:21:30 -070039 if (ec == boost::asio::error::operation_aborted)
Ed Tanous1abe55e2018-09-05 08:30:59 -070040 {
41 // expected, we were canceled before the timer completed.
42 return;
43 }
Matt Spinlerc9008502019-01-21 12:21:25 -060044 BMCWEB_LOG_ERROR << "Timed out waiting for Version interface";
Ed Tanousc3ee5222018-05-01 12:58:27 -070045
Ed Tanous1abe55e2018-09-05 08:30:59 -070046 if (ec)
47 {
48 BMCWEB_LOG_ERROR << "Async_wait failed " << ec;
49 return;
50 }
51
zhanghch058d1b46d2021-04-01 11:18:24 +080052 asyncResp->res.result(boost::beast::http::status::bad_request);
53 asyncResp->res.jsonValue = {
Matt Spinlerc9008502019-01-21 12:21:25 -060054 {"data",
55 {{"description",
56 "Version already exists or failed to be extracted"}}},
57 {"message", "400 Bad Request"},
58 {"status", "error"}};
Lei YU9f898f82019-03-08 16:52:10 +080059 };
Ed Tanous1abe55e2018-09-05 08:30:59 -070060
61 std::function<void(sdbusplus::message::message&)> callback =
zhanghch058d1b46d2021-04-01 11:18:24 +080062 [asyncResp](sdbusplus::message::message& m) {
Ed Tanous1abe55e2018-09-05 08:30:59 -070063 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();
Ed Tanous2dfd18e2020-12-18 00:41:31 +000079 std::string leaf = path.filename();
80 if (leaf.empty())
Matt Spinlerc9008502019-01-21 12:21:25 -060081 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +000082 leaf = path.str;
Matt Spinlerc9008502019-01-21 12:21:25 -060083 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +000084
zhanghch058d1b46d2021-04-01 11:18:24 +080085 asyncResp->res.jsonValue = {
Ed Tanous2dfd18e2020-12-18 00:41:31 +000086 {"data", leaf}, {"message", "200 OK"}, {"status", "ok"}};
Matt Spinlerc9008502019-01-21 12:21:25 -060087 BMCWEB_LOG_DEBUG << "ending response";
Matt Spinlerc9008502019-01-21 12:21:25 -060088 fwUpdateMatcher = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -070089 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070090 };
91 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
92 *crow::connections::systemBus,
93 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
Matt Spinlerc9008502019-01-21 12:21:25 -060094 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
Ed Tanous1abe55e2018-09-05 08:30:59 -070095 callback);
96
97 std::string filepath(
98 "/tmp/images/" +
99 boost::uuids::to_string(boost::uuids::random_generator()()));
100 BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
101 std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
102 std::ofstream::trunc);
103 out << req.body;
104 out.close();
Lei YU9f898f82019-03-08 16:52:10 +0800105 timeout.async_wait(timeoutHandler);
Ed Tanousc3ee5222018-05-01 12:58:27 -0700106}
107
Ed Tanous23a21a12020-07-25 04:45:05 +0000108inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109{
110 BMCWEB_ROUTE(app, "/upload/image/<str>")
Ed Tanous432a8902021-06-14 15:28:56 -0700111 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700112 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800113 [](const crow::Request& req,
114 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
115 const std::string&) { uploadImageHandler(req, asyncResp); });
Ed Tanousc3ee5222018-05-01 12:58:27 -0700116
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117 BMCWEB_ROUTE(app, "/upload/image")
Ed Tanous432a8902021-06-14 15:28:56 -0700118 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700119 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800120 [](const crow::Request& req,
121 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
122 uploadImageHandler(req, asyncResp);
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