Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 1 | #include <boost/asio.hpp> |
| 2 | #include <boost/container/flat_map.hpp> |
| 3 | #include <boost/container/stable_vector.hpp> |
| 4 | |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 5 | #include "crow/app.h" |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 6 | #include "crow/ci_map.h" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 7 | #include "crow/common.h" |
| 8 | #include "crow/dumb_timer_queue.h" |
| 9 | #include "crow/http_connection.h" |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 10 | #include "crow/http_parser_merged.h" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 11 | #include "crow/http_request.h" |
| 12 | #include "crow/http_response.h" |
| 13 | #include "crow/http_server.h" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 14 | #include "crow/logging.h" |
| 15 | #include "crow/middleware.h" |
| 16 | #include "crow/middleware_context.h" |
| 17 | #include "crow/mustache.h" |
| 18 | #include "crow/parser.h" |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 19 | #include "crow/query_string.h" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 20 | #include "crow/routing.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 21 | #include "crow/settings.h" |
| 22 | #include "crow/socket_adaptors.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 23 | #include "crow/utility.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 24 | #include "crow/websocket.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 25 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 26 | #include "redfish_v1.hpp" |
Ed Tanous | 7d3dba4 | 2017-04-05 13:04:39 -0700 | [diff] [blame] | 27 | #include "security_headers_middleware.hpp" |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 28 | #include "ssl_key_handler.hpp" |
Ed Tanous | 7d3dba4 | 2017-04-05 13:04:39 -0700 | [diff] [blame] | 29 | #include "token_authorization_middleware.hpp" |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 30 | #include "web_kvm.hpp" |
| 31 | #include "webassets.hpp" |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 32 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 33 | #include "nlohmann/json.hpp" |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 34 | |
Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 35 | #include <dbus/connection.hpp> |
| 36 | #include <dbus/endpoint.hpp> |
| 37 | #include <dbus/filter.hpp> |
| 38 | #include <dbus/match.hpp> |
| 39 | #include <dbus/message.hpp> |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 40 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 41 | #include <chrono> |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 42 | #include <iostream> |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 43 | #include <memory> |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 44 | #include <string> |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 45 | #include <unordered_set> |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 46 | |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 47 | static std::shared_ptr<dbus::connection> system_bus; |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 48 | static std::vector<dbus::match> dbus_matches; |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 49 | static std::shared_ptr<dbus::filter> sensor_filter; |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 50 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 51 | struct DbusWebsocketSession { |
| 52 | std::vector<dbus::match> matches; |
| 53 | std::vector<dbus::filter> filters; |
| 54 | }; |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 55 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 56 | static boost::container::flat_map<crow::websocket::connection*, |
| 57 | DbusWebsocketSession> |
| 58 | sessions; |
| 59 | |
| 60 | void on_property_update(dbus::filter& filter, boost::system::error_code ec, |
| 61 | dbus::message s) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 62 | std::string object_name; |
| 63 | std::vector<std::pair<std::string, dbus::dbus_variant>> values; |
| 64 | s.unpack(object_name).unpack(values); |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 65 | nlohmann::json j; |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 66 | for (auto& value : values) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 67 | boost::apply_visitor([&](auto val) { j[s.get_path()] = val; }, |
| 68 | value.second); |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 69 | } |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 70 | auto data_to_send = j.dump(); |
| 71 | |
| 72 | for (auto& session : sessions) { |
| 73 | session.first->send_text(data_to_send); |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 74 | } |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 75 | filter.async_dispatch([&](boost::system::error_code ec, dbus::message s) { |
| 76 | on_property_update(filter, ec, s);; |
| 77 | }); |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 78 | }; |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 79 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 80 | int main(int argc, char** argv) { |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 81 | // Build an io_service (there should only be 1) |
| 82 | auto io = std::make_shared<boost::asio::io_service>(); |
| 83 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 84 | bool enable_ssl = true; |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 85 | std::string ssl_pem_file("server.pem"); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 86 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 87 | if (enable_ssl) { |
| 88 | ensuressl::ensure_openssl_key_present_and_valid(ssl_pem_file); |
| 89 | } |
| 90 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 91 | crow::App< |
| 92 | /*crow::TokenAuthorizationMiddleware, */ crow::SecurityHeadersMiddleware> |
| 93 | app(io); |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 94 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 95 | crow::webassets::request_routes(app); |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 96 | crow::kvm::request_routes(app); |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 97 | crow::redfish::request_routes(app); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 98 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 99 | crow::logger::setLogLevel(crow::LogLevel::INFO); |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 100 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 101 | CROW_ROUTE(app, "/dbus_monitor") |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 102 | .websocket() |
| 103 | .onopen([&](crow::websocket::connection& conn) { |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 104 | sessions[&conn] = DbusWebsocketSession(); |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 105 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 106 | sessions[&conn].matches.emplace_back( |
| 107 | system_bus, |
| 108 | "type='signal',path_namespace='/xyz/openbmc_project/sensors'"); |
| 109 | |
| 110 | sessions[&conn].filters.emplace_back(system_bus, [](dbus::message m) { |
| 111 | auto member = m.get_member(); |
| 112 | return member == "PropertiesChanged"; |
| 113 | }); |
| 114 | auto& this_filter = sessions[&conn].filters.back(); |
| 115 | this_filter.async_dispatch( |
| 116 | [&](boost::system::error_code ec, dbus::message s) { |
| 117 | on_property_update(this_filter, ec, s);; |
| 118 | }); |
| 119 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 120 | }) |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 121 | .onclose( |
| 122 | [&](crow::websocket::connection& conn, const std::string& reason) { |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 123 | sessions.erase(&conn); |
| 124 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 125 | }) |
| 126 | .onmessage([&](crow::websocket::connection& conn, const std::string& data, |
| 127 | bool is_binary) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 128 | CROW_LOG_ERROR << "Got unexpected message from client on sensorws"; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 129 | }); |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 130 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 131 | CROW_ROUTE(app, "/intel/firmwareupload") |
| 132 | .methods("POST"_method)([](const crow::request& req) { |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 133 | auto filepath = "/tmp/fw_update_image"; |
| 134 | std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | |
| 135 | std::ofstream::trunc); |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 136 | out << req.body; |
| 137 | out.close(); |
| 138 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 139 | nlohmann::json j; |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 140 | j["status"] = "Upload Successfull"; |
| 141 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 142 | dbus::endpoint fw_update_endpoint( |
| 143 | "xyz.openbmc_project.fwupdate1.server", |
| 144 | "/xyz/openbmc_project/fwupdate1", "xyz.openbmc_project.fwupdate1"); |
| 145 | |
| 146 | auto m = dbus::message::new_call(fw_update_endpoint, "start"); |
| 147 | |
| 148 | m.pack(std::string("file://") + filepath); |
| 149 | system_bus->send(m); |
| 150 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 151 | return j; |
| 152 | }); |
| 153 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 154 | crow::logger::setLogLevel(crow::LogLevel::DEBUG); |
| 155 | auto test = app.get_routes(); |
| 156 | app.debug_print(); |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 157 | std::cout << "Building SSL context\n"; |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 158 | |
Ed Tanous | 4c3cbc6 | 2017-05-16 09:17:42 -0700 | [diff] [blame] | 159 | int port = 18080; |
Ed Tanous | f0226cd | 2017-05-16 12:35:38 -0700 | [diff] [blame] | 160 | |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 161 | std::cout << "Starting webserver on port " << port << "\n"; |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 162 | app.port(port); |
| 163 | if (enable_ssl) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 164 | std::cout << "SSL Enabled\n"; |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 165 | auto ssl_context = ensuressl::get_ssl_context(ssl_pem_file); |
| 166 | app.ssl(std::move(ssl_context)); |
| 167 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 168 | // app.concurrency(4); |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame^] | 169 | |
| 170 | // Start dbus connection |
| 171 | system_bus = std::make_shared<dbus::connection>(*io, dbus::bus::system); |
| 172 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 173 | app.run(); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 174 | } |