Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 1 | #include "crow/app.h" |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 2 | #include "crow/ci_map.h" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 3 | #include "crow/common.h" |
| 4 | #include "crow/dumb_timer_queue.h" |
| 5 | #include "crow/http_connection.h" |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 6 | #include "crow/http_parser_merged.h" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 7 | #include "crow/http_request.h" |
| 8 | #include "crow/http_response.h" |
| 9 | #include "crow/http_server.h" |
| 10 | #include "crow/json.h" |
| 11 | #include "crow/logging.h" |
| 12 | #include "crow/middleware.h" |
| 13 | #include "crow/middleware_context.h" |
| 14 | #include "crow/mustache.h" |
| 15 | #include "crow/parser.h" |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 16 | #include "crow/query_string.h" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 17 | #include "crow/routing.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 18 | #include "crow/settings.h" |
| 19 | #include "crow/socket_adaptors.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 20 | #include "crow/utility.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 21 | #include "crow/websocket.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 22 | |
Ed Tanous | 7d3dba4 | 2017-04-05 13:04:39 -0700 | [diff] [blame] | 23 | #include "security_headers_middleware.hpp" |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 24 | #include "ssl_key_handler.hpp" |
Ed Tanous | 7d3dba4 | 2017-04-05 13:04:39 -0700 | [diff] [blame] | 25 | #include "token_authorization_middleware.hpp" |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 26 | #include "web_kvm.hpp" |
| 27 | #include "webassets.hpp" |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 28 | |
| 29 | #include <boost/asio.hpp> |
| 30 | #include <boost/endian/arithmetic.hpp> |
| 31 | |
Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 32 | #include <dbus/connection.hpp> |
| 33 | #include <dbus/endpoint.hpp> |
| 34 | #include <dbus/filter.hpp> |
| 35 | #include <dbus/match.hpp> |
| 36 | #include <dbus/message.hpp> |
| 37 | #include <dbus/utility.hpp> |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 38 | |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 39 | #include <iostream> |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 40 | #include <memory> |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 41 | #include <string> |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 42 | #include <unordered_set> |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 43 | |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 44 | static std::shared_ptr<dbus::connection> system_bus; |
| 45 | static std::shared_ptr<dbus::match> sensor_match; |
| 46 | static std::shared_ptr<dbus::filter> sensor_filter; |
| 47 | static std::shared_ptr<dbus::filter> sensor_callback; |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 48 | |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 49 | std::unordered_set<crow::websocket::connection*> users; |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 50 | |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 51 | void on_sensor_update(boost::system::error_code ec, dbus::message s) { |
| 52 | std::string object_name; |
| 53 | std::vector<std::pair<std::string, dbus::dbus_variant>> values; |
| 54 | s.unpack(object_name).unpack(values); |
| 55 | crow::json::wvalue j; |
| 56 | for (auto& value : values) { |
| 57 | //std::cout << "Got sensor value for " << s.get_path() << "\n"; |
| 58 | boost::apply_visitor([&](auto val) { j[s.get_path()] = val; }, |
| 59 | value.second); |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 60 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 61 | for (auto conn : users) { |
| 62 | conn->send_text(crow::json::dump(j)); |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 63 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 64 | sensor_filter->async_dispatch(on_sensor_update); |
| 65 | }; |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 66 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 67 | int main(int argc, char** argv) { |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 68 | bool enable_ssl = true; |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 69 | std::string ssl_pem_file("server.pem"); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 70 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 71 | if (enable_ssl) { |
| 72 | ensuressl::ensure_openssl_key_present_and_valid(ssl_pem_file); |
| 73 | } |
| 74 | |
Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 75 | crow::App<crow::TokenAuthorizationMiddleware, crow::SecurityHeadersMiddleware> |
Ed Tanous | 7d3dba4 | 2017-04-05 13:04:39 -0700 | [diff] [blame] | 76 | app; |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 77 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 78 | crow::webassets::request_routes(app); |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 79 | crow::kvm::request_routes(app); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 80 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 81 | crow::logger::setLogLevel(crow::LogLevel::INFO); |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 82 | CROW_ROUTE(app, "/systeminfo") |
| 83 | ([]() { |
| 84 | |
| 85 | crow::json::wvalue j; |
| 86 | j["device_id"] = 0x7B; |
| 87 | j["device_provides_sdrs"] = true; |
| 88 | j["device_revision"] = true; |
| 89 | j["device_available"] = true; |
| 90 | j["firmware_revision"] = "0.68"; |
| 91 | |
| 92 | j["ipmi_revision"] = "2.0"; |
| 93 | j["supports_chassis_device"] = true; |
| 94 | j["supports_bridge"] = true; |
| 95 | j["supports_ipmb_event_generator"] = true; |
| 96 | j["supports_ipmb_event_receiver"] = true; |
| 97 | j["supports_fru_inventory_device"] = true; |
| 98 | j["supports_sel_device"] = true; |
| 99 | j["supports_sdr_repository_device"] = true; |
| 100 | j["supports_sensor_device"] = true; |
| 101 | |
| 102 | j["firmware_aux_revision"] = "0.60.foobar"; |
| 103 | |
| 104 | return j; |
| 105 | }); |
| 106 | |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 107 | CROW_ROUTE(app, "/sensorws") |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 108 | .websocket() |
| 109 | .onopen([&](crow::websocket::connection& conn) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 110 | system_bus = std::make_shared<dbus::connection>(conn.get_io_service(), |
| 111 | dbus::bus::system); |
| 112 | sensor_match = std::make_shared<dbus::match>( |
| 113 | *system_bus, |
| 114 | "type='signal',path_namespace='/xyz/openbmc_project/sensors'"); |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 115 | |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 116 | sensor_filter = |
| 117 | std::make_shared<dbus::filter>(*system_bus, [](dbus::message& m) { |
| 118 | auto member = m.get_member(); |
| 119 | return member == "PropertiesChanged"; |
| 120 | }); |
| 121 | /* |
| 122 | std::function<void(boost::system::error_code, dbus::message)> |
| 123 | sensor_callback = [&conn, sensor_callback]( |
| 124 | boost::system::error_code ec, dbus::message s) { |
| 125 | std::string object_name; |
| 126 | std::vector<std::pair<std::string, dbus::dbus_variant>> values; |
| 127 | s.unpack(object_name).unpack(values); |
| 128 | crow::json::wvalue j; |
| 129 | for (auto& value : values) { |
| 130 | std::cout << "Got sensor value for " << s.get_path() << "\n"; |
| 131 | boost::apply_visitor([&](auto val) { j[s.get_path()] = val; }, |
| 132 | value.second); |
| 133 | } |
| 134 | for (auto conn : users) { |
| 135 | conn.send_text(crow::json::dump(j)); |
| 136 | } |
| 137 | sensor_filter->async_dispatch(sensor_callback); |
| 138 | }; |
| 139 | */ |
| 140 | sensor_filter->async_dispatch(on_sensor_update); |
| 141 | users.insert(&conn); |
| 142 | ; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 143 | }) |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 144 | .onclose( |
| 145 | [&](crow::websocket::connection& conn, const std::string& reason) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 146 | // TODO(ed) needs lock |
| 147 | users.erase(&conn); |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 148 | }) |
| 149 | .onmessage([&](crow::websocket::connection& conn, const std::string& data, |
| 150 | bool is_binary) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 151 | CROW_LOG_ERROR << "Got unexpected message from client on sensorws"; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 152 | }); |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 153 | |
| 154 | CROW_ROUTE(app, "/sensortest") |
Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 155 | ([](const crow::request& req, crow::response& res) { |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 156 | crow::json::wvalue j; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 157 | |
Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 158 | dbus::connection system_bus(*req.io_service, dbus::bus::system); |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 159 | dbus::endpoint test_daemon("org.openbmc.Sensors", |
| 160 | "/org/openbmc/sensors/tach", |
| 161 | "org.freedesktop.DBus.Introspectable"); |
| 162 | dbus::message m = dbus::message::new_call(test_daemon, "Introspect"); |
| 163 | system_bus.async_send( |
| 164 | m, |
| 165 | [&j, &system_bus](const boost::system::error_code ec, dbus::message r) { |
| 166 | std::string xml; |
| 167 | r.unpack(xml); |
| 168 | std::vector<std::string> dbus_objects; |
| 169 | dbus::read_dbus_xml_names(xml, dbus_objects); |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 170 | |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 171 | for (auto& object : dbus_objects) { |
| 172 | dbus::endpoint test_daemon("org.openbmc.Sensors", |
| 173 | "/org/openbmc/sensors/tach/" + object, |
| 174 | "org.openbmc.SensorValue"); |
| 175 | dbus::message m2 = dbus::message::new_call(test_daemon, "getValue"); |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 176 | |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 177 | system_bus.async_send( |
| 178 | m2, [&](const boost::system::error_code ec, dbus::message r) { |
| 179 | int32_t value; |
| 180 | r.unpack(value); |
| 181 | // TODO(ed) if we ever go multithread, j needs a lock |
| 182 | j[object] = value; |
| 183 | }); |
| 184 | } |
Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 185 | |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 186 | }); |
Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 187 | |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 188 | }); |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 189 | |
| 190 | CROW_ROUTE(app, "/intel/firmwareupload") |
| 191 | .methods("POST"_method)([](const crow::request& req) { |
| 192 | // TODO(ed) handle errors here (file exists already and is locked, ect) |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 193 | std::ofstream out( |
| 194 | "/tmp/fw_update_image", |
| 195 | std::ofstream::out | std::ofstream::binary | std::ofstream::trunc); |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 196 | out << req.body; |
| 197 | out.close(); |
| 198 | |
| 199 | crow::json::wvalue j; |
| 200 | j["status"] = "Upload Successfull"; |
| 201 | |
| 202 | return j; |
| 203 | }); |
| 204 | |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 205 | std::cout << "Building SSL context\n"; |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 206 | |
Ed Tanous | 4c3cbc6 | 2017-05-16 09:17:42 -0700 | [diff] [blame] | 207 | int port = 18080; |
Ed Tanous | f0226cd | 2017-05-16 12:35:38 -0700 | [diff] [blame] | 208 | |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 209 | std::cout << "Starting webserver on port " << port << "\n"; |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 210 | app.port(port); |
| 211 | if (enable_ssl) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 212 | std::cout << "SSL Enabled\n"; |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 213 | auto ssl_context = ensuressl::get_ssl_context(ssl_pem_file); |
| 214 | app.ssl(std::move(ssl_context)); |
| 215 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame^] | 216 | // app.concurrency(4); |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 217 | app.run(); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 218 | } |