blob: 9d0a0ca92179ea756661318e10662f17ec0f5063 [file] [log] [blame]
Ed Tanousc3ee5222018-05-01 12:58:27 -07001#pragma once
2
Ed Tanousc94ad492019-10-10 15:39:33 -07003#include <app.h>
Ed Tanous1abe55e2018-09-05 08:30:59 -07004
Ed Tanousc3ee5222018-05-01 12:58:27 -07005#include <boost/uuid/uuid.hpp>
6#include <boost/uuid/uuid_generators.hpp>
7#include <boost/uuid/uuid_io.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07008#include <dbus_singleton.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05009
10#include <cstdio>
Ed Tanous1abe55e2018-09-05 08:30:59 -070011#include <fstream>
12#include <memory>
Ed Tanousc3ee5222018-05-01 12:58:27 -070013
Ed Tanous1abe55e2018-09-05 08:30:59 -070014namespace crow
15{
16namespace image_upload
17{
Ed Tanousc3ee5222018-05-01 12:58:27 -070018
Ed Tanous23a21a12020-07-25 04:45:05 +000019static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;
Ed Tanousc3ee5222018-05-01 12:58:27 -070020
Ed Tanouscb13a392020-07-25 19:02:03 +000021inline void uploadImageHandler(const crow::Request& req, crow::Response& res)
Ed Tanous1abe55e2018-09-05 08:30:59 -070022{
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 Tanousc3ee5222018-05-01 12:58:27 -070028 res.end();
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
Lei YU9f898f82019-03-08 16:52:10 +080037 auto timeoutHandler = [&res](const boost::system::error_code& ec) {
Ed Tanousc3ee5222018-05-01 12:58:27 -070038 fwUpdateMatcher = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -070039 if (ec == asio::error::operation_aborted)
40 {
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
Matt Spinlerc9008502019-01-21 12:21:25 -060052 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 Tanous1abe55e2018-09-05 08:30:59 -070059 res.end();
Lei YU9f898f82019-03-08 16:52:10 +080060 };
Ed Tanous1abe55e2018-09-05 08:30:59 -070061
62 std::function<void(sdbusplus::message::message&)> callback =
63 [&res](sdbusplus::message::message& m) {
64 BMCWEB_LOG_DEBUG << "Match fired";
Ed Tanous1abe55e2018-09-05 08:30:59 -070065
Matt Spinlerc9008502019-01-21 12:21:25 -060066 sdbusplus::message::object_path path;
67 std::vector<std::pair<
68 std::string,
Ed Tanousabf2add2019-01-22 16:40:12 -080069 std::vector<std::pair<std::string, std::variant<std::string>>>>>
Matt Spinlerc9008502019-01-21 12:21:25 -060070 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 Tanous1abe55e2018-09-05 08:30:59 -070078 {
Ed Tanous271584a2019-07-09 16:24:22 -070079 timeout.cancel();
Matt Spinlerc9008502019-01-21 12:21:25 -060080
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 Tanous1abe55e2018-09-05 08:30:59 -070092 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070093 };
94 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
95 *crow::connections::systemBus,
96 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
Matt Spinlerc9008502019-01-21 12:21:25 -060097 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
Ed Tanous1abe55e2018-09-05 08:30:59 -070098 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 YU9f898f82019-03-08 16:52:10 +0800108 timeout.async_wait(timeoutHandler);
Ed Tanousc3ee5222018-05-01 12:58:27 -0700109}
110
Ed Tanous23a21a12020-07-25 04:45:05 +0000111inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112{
113 BMCWEB_ROUTE(app, "/upload/image/<str>")
Ed Tanous23a21a12020-07-25 04:45:05 +0000114 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700115 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
116 [](const crow::Request& req, crow::Response& res,
Ed Tanouscb13a392020-07-25 19:02:03 +0000117 const std::string&) { uploadImageHandler(req, res); });
Ed Tanousc3ee5222018-05-01 12:58:27 -0700118
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119 BMCWEB_ROUTE(app, "/upload/image")
Ed Tanous23a21a12020-07-25 04:45:05 +0000120 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700121 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700122 [](const crow::Request& req, crow::Response& res) {
Ed Tanouscb13a392020-07-25 19:02:03 +0000123 uploadImageHandler(req, res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700124 });
Ed Tanousc3ee5222018-05-01 12:58:27 -0700125}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700126} // namespace image_upload
127} // namespace crow