blob: 3af461bca4620fca8c25cf715b980eb0d87317b7 [file] [log] [blame]
Ed Tanous3dac7492017-08-02 13:46:20 -07001#include <boost/asio.hpp>
2#include <boost/container/flat_map.hpp>
3#include <boost/container/stable_vector.hpp>
4
Ed Tanousf9273472017-02-28 16:05:13 -08005#include "crow/app.h"
Ed Tanousc4771fb2017-03-13 13:39:49 -07006#include "crow/ci_map.h"
Ed Tanousf9273472017-02-28 16:05:13 -08007#include "crow/common.h"
8#include "crow/dumb_timer_queue.h"
9#include "crow/http_connection.h"
Ed Tanousc4771fb2017-03-13 13:39:49 -070010#include "crow/http_parser_merged.h"
Ed Tanousf9273472017-02-28 16:05:13 -080011#include "crow/http_request.h"
12#include "crow/http_response.h"
13#include "crow/http_server.h"
Ed Tanousf9273472017-02-28 16:05:13 -080014#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 Tanousc4771fb2017-03-13 13:39:49 -070019#include "crow/query_string.h"
Ed Tanousf9273472017-02-28 16:05:13 -080020#include "crow/routing.h"
Ed Tanous0fdddb12017-02-28 11:06:34 -080021#include "crow/settings.h"
22#include "crow/socket_adaptors.h"
Ed Tanous0fdddb12017-02-28 11:06:34 -080023#include "crow/utility.h"
Ed Tanous0fdddb12017-02-28 11:06:34 -080024#include "crow/websocket.h"
Ed Tanous0fdddb12017-02-28 11:06:34 -080025
Ed Tanous3dac7492017-08-02 13:46:20 -070026#include "redfish_v1.hpp"
Ed Tanous7d3dba42017-04-05 13:04:39 -070027#include "security_headers_middleware.hpp"
Ed Tanous9140a672017-04-24 17:01:32 -070028#include "ssl_key_handler.hpp"
Ed Tanous7d3dba42017-04-05 13:04:39 -070029#include "token_authorization_middleware.hpp"
Ed Tanous9140a672017-04-24 17:01:32 -070030#include "web_kvm.hpp"
31#include "webassets.hpp"
Ed Tanousb4d29f42017-03-24 16:39:25 -070032
Ed Tanous3dac7492017-08-02 13:46:20 -070033#include "nlohmann/json.hpp"
Ed Tanousb4d29f42017-03-24 16:39:25 -070034
Ed Tanousc9b55212017-06-12 13:25:51 -070035#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 Tanouscc5a37f2017-05-11 10:27:23 -070040
Ed Tanous3dac7492017-08-02 13:46:20 -070041#include <chrono>
Ed Tanous0fdddb12017-02-28 11:06:34 -080042#include <iostream>
Ed Tanousc4771fb2017-03-13 13:39:49 -070043#include <memory>
Ed Tanous0fdddb12017-02-28 11:06:34 -080044#include <string>
Ed Tanousc4771fb2017-03-13 13:39:49 -070045#include <unordered_set>
Ed Tanous9b65f1f2017-03-07 15:17:13 -080046
Ed Tanous4d92cbf2017-06-22 15:41:02 -070047static std::shared_ptr<dbus::connection> system_bus;
Ed Tanous3dac7492017-08-02 13:46:20 -070048static std::vector<dbus::match> dbus_matches;
Ed Tanous4d92cbf2017-06-22 15:41:02 -070049static std::shared_ptr<dbus::filter> sensor_filter;
Ed Tanouscc5a37f2017-05-11 10:27:23 -070050
Ed Tanous3dac7492017-08-02 13:46:20 -070051struct DbusWebsocketSession {
52 std::vector<dbus::match> matches;
53 std::vector<dbus::filter> filters;
54};
Ed Tanouscc5a37f2017-05-11 10:27:23 -070055
Ed Tanous3dac7492017-08-02 13:46:20 -070056static boost::container::flat_map<crow::websocket::connection*,
57 DbusWebsocketSession>
58 sessions;
59
60void on_property_update(dbus::filter& filter, boost::system::error_code ec,
61 dbus::message s) {
Ed Tanous4d92cbf2017-06-22 15:41:02 -070062 std::string object_name;
63 std::vector<std::pair<std::string, dbus::dbus_variant>> values;
64 s.unpack(object_name).unpack(values);
Ed Tanous3dac7492017-08-02 13:46:20 -070065 nlohmann::json j;
Ed Tanous4d92cbf2017-06-22 15:41:02 -070066 for (auto& value : values) {
Ed Tanous4d92cbf2017-06-22 15:41:02 -070067 boost::apply_visitor([&](auto val) { j[s.get_path()] = val; },
68 value.second);
Ed Tanouscc5a37f2017-05-11 10:27:23 -070069 }
Ed Tanous3dac7492017-08-02 13:46:20 -070070 auto data_to_send = j.dump();
71
72 for (auto& session : sessions) {
73 session.first->send_text(data_to_send);
Ed Tanouscc5a37f2017-05-11 10:27:23 -070074 }
Ed Tanous3dac7492017-08-02 13:46:20 -070075 filter.async_dispatch([&](boost::system::error_code ec, dbus::message s) {
76 on_property_update(filter, ec, s);;
77 });
Ed Tanous4d92cbf2017-06-22 15:41:02 -070078};
Ed Tanouscc5a37f2017-05-11 10:27:23 -070079
Ed Tanous99923322017-03-03 14:21:24 -080080int main(int argc, char** argv) {
Ed Tanous3dac7492017-08-02 13:46:20 -070081 // Build an io_service (there should only be 1)
82 auto io = std::make_shared<boost::asio::io_service>();
83
Ed Tanous4758d5b2017-06-06 15:28:13 -070084 bool enable_ssl = true;
Ed Tanous99923322017-03-03 14:21:24 -080085 std::string ssl_pem_file("server.pem");
Ed Tanous0fdddb12017-02-28 11:06:34 -080086
Ed Tanous4758d5b2017-06-06 15:28:13 -070087 if (enable_ssl) {
88 ensuressl::ensure_openssl_key_present_and_valid(ssl_pem_file);
89 }
90
Ed Tanous3dac7492017-08-02 13:46:20 -070091 crow::App<
Ed Tanousef915c22017-08-09 11:13:00 -070092 crow::TokenAuthorizationMiddleware, crow::SecurityHeadersMiddleware>
Ed Tanous3dac7492017-08-02 13:46:20 -070093 app(io);
Ed Tanousb4d29f42017-03-24 16:39:25 -070094
Ed Tanous99923322017-03-03 14:21:24 -080095 crow::webassets::request_routes(app);
Ed Tanousc81ca422017-03-21 16:18:49 -070096 crow::kvm::request_routes(app);
Ed Tanous3dac7492017-08-02 13:46:20 -070097 crow::redfish::request_routes(app);
Ed Tanous0fdddb12017-02-28 11:06:34 -080098
Ed Tanous9b65f1f2017-03-07 15:17:13 -080099 crow::logger::setLogLevel(crow::LogLevel::INFO);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700100
Ed Tanous3dac7492017-08-02 13:46:20 -0700101 CROW_ROUTE(app, "/dbus_monitor")
Ed Tanousc4771fb2017-03-13 13:39:49 -0700102 .websocket()
103 .onopen([&](crow::websocket::connection& conn) {
Ed Tanous3dac7492017-08-02 13:46:20 -0700104 sessions[&conn] = DbusWebsocketSession();
Ed Tanousf3d847c2017-06-12 16:01:42 -0700105
Ed Tanous3dac7492017-08-02 13:46:20 -0700106 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 Tanousc4771fb2017-03-13 13:39:49 -0700120 })
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700121 .onclose(
122 [&](crow::websocket::connection& conn, const std::string& reason) {
Ed Tanous3dac7492017-08-02 13:46:20 -0700123 sessions.erase(&conn);
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700124 })
125 .onmessage([&](crow::websocket::connection& conn, const std::string& data,
126 bool is_binary) {
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700127 CROW_LOG_ERROR << "Got unexpected message from client on sensorws";
Ed Tanousc4771fb2017-03-13 13:39:49 -0700128 });
Ed Tanouscc5a37f2017-05-11 10:27:23 -0700129
Ed Tanous4758d5b2017-06-06 15:28:13 -0700130 CROW_ROUTE(app, "/intel/firmwareupload")
131 .methods("POST"_method)([](const crow::request& req) {
Ed Tanous3dac7492017-08-02 13:46:20 -0700132 auto filepath = "/tmp/fw_update_image";
133 std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
134 std::ofstream::trunc);
Ed Tanous4758d5b2017-06-06 15:28:13 -0700135 out << req.body;
136 out.close();
137
Ed Tanous3dac7492017-08-02 13:46:20 -0700138 nlohmann::json j;
Ed Tanous4758d5b2017-06-06 15:28:13 -0700139 j["status"] = "Upload Successfull";
140
Ed Tanous3dac7492017-08-02 13:46:20 -0700141 dbus::endpoint fw_update_endpoint(
142 "xyz.openbmc_project.fwupdate1.server",
143 "/xyz/openbmc_project/fwupdate1", "xyz.openbmc_project.fwupdate1");
144
145 auto m = dbus::message::new_call(fw_update_endpoint, "start");
146
147 m.pack(std::string("file://") + filepath);
148 system_bus->send(m);
149
Ed Tanous4758d5b2017-06-06 15:28:13 -0700150 return j;
151 });
152
Ed Tanous3dac7492017-08-02 13:46:20 -0700153 crow::logger::setLogLevel(crow::LogLevel::DEBUG);
154 auto test = app.get_routes();
155 app.debug_print();
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700156 std::cout << "Building SSL context\n";
Ed Tanous4758d5b2017-06-06 15:28:13 -0700157
Ed Tanous4c3cbc62017-05-16 09:17:42 -0700158 int port = 18080;
Ed Tanousf0226cd2017-05-16 12:35:38 -0700159
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700160 std::cout << "Starting webserver on port " << port << "\n";
Ed Tanous4758d5b2017-06-06 15:28:13 -0700161 app.port(port);
162 if (enable_ssl) {
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700163 std::cout << "SSL Enabled\n";
Ed Tanous4758d5b2017-06-06 15:28:13 -0700164 auto ssl_context = ensuressl::get_ssl_context(ssl_pem_file);
165 app.ssl(std::move(ssl_context));
166 }
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700167 // app.concurrency(4);
Ed Tanous3dac7492017-08-02 13:46:20 -0700168
169 // Start dbus connection
170 system_bus = std::make_shared<dbus::connection>(*io, dbus::bus::system);
171
Ed Tanous4758d5b2017-06-06 15:28:13 -0700172 app.run();
Ed Tanous0fdddb12017-02-28 11:06:34 -0800173}