blob: 1b0a09a4bac98963354ce3681fb019be50b7e32a [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>
Ed Tanous168e20c2021-12-13 14:39:53 -08008#include <dbus_utility.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
zhanghch058d1b46d2021-04-01 11:18:24 +080021inline void
22 uploadImageHandler(const crow::Request& req,
23 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -070024{
25 // Only allow one FW update at a time
26 if (fwUpdateMatcher != nullptr)
27 {
zhanghch058d1b46d2021-04-01 11:18:24 +080028 asyncResp->res.addHeader("Retry-After", "30");
29 asyncResp->res.result(boost::beast::http::status::service_unavailable);
Ed Tanous1abe55e2018-09-05 08:30:59 -070030 return;
31 }
32 // Make this const static so it survives outside this method
Ed Tanous271584a2019-07-09 16:24:22 -070033 static boost::asio::steady_timer timeout(*req.ioService,
34 std::chrono::seconds(5));
Ed Tanous1abe55e2018-09-05 08:30:59 -070035
Ed Tanous271584a2019-07-09 16:24:22 -070036 timeout.expires_after(std::chrono::seconds(15));
Ed Tanous1abe55e2018-09-05 08:30:59 -070037
zhanghch058d1b46d2021-04-01 11:18:24 +080038 auto timeoutHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanousc3ee5222018-05-01 12:58:27 -070039 fwUpdateMatcher = nullptr;
Ed Tanous23e64202020-09-15 19:21:30 -070040 if (ec == boost::asio::error::operation_aborted)
Ed Tanous1abe55e2018-09-05 08:30:59 -070041 {
42 // expected, we were canceled before the timer completed.
43 return;
44 }
Matt Spinlerc9008502019-01-21 12:21:25 -060045 BMCWEB_LOG_ERROR << "Timed out waiting for Version interface";
Ed Tanousc3ee5222018-05-01 12:58:27 -070046
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 if (ec)
48 {
49 BMCWEB_LOG_ERROR << "Async_wait failed " << ec;
50 return;
51 }
52
zhanghch058d1b46d2021-04-01 11:18:24 +080053 asyncResp->res.result(boost::beast::http::status::bad_request);
54 asyncResp->res.jsonValue = {
Matt Spinlerc9008502019-01-21 12:21:25 -060055 {"data",
56 {{"description",
57 "Version already exists or failed to be extracted"}}},
58 {"message", "400 Bad Request"},
59 {"status", "error"}};
Lei YU9f898f82019-03-08 16:52:10 +080060 };
Ed Tanous1abe55e2018-09-05 08:30:59 -070061
62 std::function<void(sdbusplus::message::message&)> callback =
zhanghch058d1b46d2021-04-01 11:18:24 +080063 [asyncResp](sdbusplus::message::message& m) {
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 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<
Ed Tanous168e20c2021-12-13 14:39:53 -080068 std::string, std::vector<std::pair<
69 std::string, dbus::utility::DbusVariantType>>>>
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();
Ed Tanous2dfd18e2020-12-18 00:41:31 +000080 std::string leaf = path.filename();
81 if (leaf.empty())
Matt Spinlerc9008502019-01-21 12:21:25 -060082 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +000083 leaf = path.str;
Matt Spinlerc9008502019-01-21 12:21:25 -060084 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +000085
zhanghch058d1b46d2021-04-01 11:18:24 +080086 asyncResp->res.jsonValue = {
Ed Tanous2dfd18e2020-12-18 00:41:31 +000087 {"data", leaf}, {"message", "200 OK"}, {"status", "ok"}};
Matt Spinlerc9008502019-01-21 12:21:25 -060088 BMCWEB_LOG_DEBUG << "ending response";
Matt Spinlerc9008502019-01-21 12:21:25 -060089 fwUpdateMatcher = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -070090 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070091 };
92 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
93 *crow::connections::systemBus,
94 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
Matt Spinlerc9008502019-01-21 12:21:25 -060095 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
Ed Tanous1abe55e2018-09-05 08:30:59 -070096 callback);
97
98 std::string filepath(
99 "/tmp/images/" +
100 boost::uuids::to_string(boost::uuids::random_generator()()));
101 BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
102 std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
103 std::ofstream::trunc);
104 out << req.body;
105 out.close();
Lei YU9f898f82019-03-08 16:52:10 +0800106 timeout.async_wait(timeoutHandler);
Ed Tanousc3ee5222018-05-01 12:58:27 -0700107}
108
Ed Tanous23a21a12020-07-25 04:45:05 +0000109inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110{
111 BMCWEB_ROUTE(app, "/upload/image/<str>")
Ed Tanous432a8902021-06-14 15:28:56 -0700112 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700113 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800114 [](const crow::Request& req,
115 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
116 const std::string&) { uploadImageHandler(req, asyncResp); });
Ed Tanousc3ee5222018-05-01 12:58:27 -0700117
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118 BMCWEB_ROUTE(app, "/upload/image")
Ed Tanous432a8902021-06-14 15:28:56 -0700119 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700120 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800121 [](const crow::Request& req,
122 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
123 uploadImageHandler(req, asyncResp);
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