Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 2 | #include <app.h> |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 3 | #include <sys/socket.h> |
Ed Tanous | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 4 | #include <websocket.h> |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 5 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 6 | #include <async_resp.hpp> |
Ed Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 7 | #include <boost/asio/local/stream_protocol.hpp> |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 8 | #include <boost/container/flat_map.hpp> |
| 9 | #include <boost/container/flat_set.hpp> |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 10 | |
| 11 | namespace crow |
| 12 | { |
| 13 | namespace obmc_console |
| 14 | { |
| 15 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 16 | static std::unique_ptr<boost::asio::local::stream_protocol::socket> hostSocket; |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 17 | |
| 18 | static std::array<char, 4096> outputBuffer; |
| 19 | static std::string inputBuffer; |
| 20 | |
| 21 | static boost::container::flat_set<crow::websocket::Connection*> sessions; |
| 22 | |
| 23 | static bool doingWrite = false; |
| 24 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 25 | inline void doWrite() |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 26 | { |
| 27 | if (doingWrite) |
| 28 | { |
| 29 | BMCWEB_LOG_DEBUG << "Already writing. Bailing out"; |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | if (inputBuffer.empty()) |
| 34 | { |
| 35 | BMCWEB_LOG_DEBUG << "Outbuffer empty. Bailing out"; |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | doingWrite = true; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 40 | hostSocket->async_write_some( |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 41 | boost::asio::buffer(inputBuffer.data(), inputBuffer.size()), |
| 42 | [](boost::beast::error_code ec, std::size_t bytes_written) { |
| 43 | doingWrite = false; |
| 44 | inputBuffer.erase(0, bytes_written); |
| 45 | |
| 46 | if (ec == boost::asio::error::eof) |
| 47 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 48 | for (crow::websocket::Connection* session : sessions) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 49 | { |
| 50 | session->close("Error in reading to host port"); |
| 51 | } |
| 52 | return; |
| 53 | } |
| 54 | if (ec) |
| 55 | { |
| 56 | BMCWEB_LOG_ERROR << "Error in host serial write " << ec; |
| 57 | return; |
| 58 | } |
| 59 | doWrite(); |
| 60 | }); |
| 61 | } |
| 62 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 63 | inline void doRead() |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 64 | { |
| 65 | BMCWEB_LOG_DEBUG << "Reading from socket"; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 66 | hostSocket->async_read_some( |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 67 | boost::asio::buffer(outputBuffer.data(), outputBuffer.size()), |
| 68 | [](const boost::system::error_code& ec, std::size_t bytesRead) { |
| 69 | BMCWEB_LOG_DEBUG << "read done. Read " << bytesRead << " bytes"; |
| 70 | if (ec) |
| 71 | { |
| 72 | BMCWEB_LOG_ERROR << "Couldn't read from host serial port: " |
| 73 | << ec; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 74 | for (crow::websocket::Connection* session : sessions) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 75 | { |
| 76 | session->close("Error in connecting to host port"); |
| 77 | } |
| 78 | return; |
| 79 | } |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 80 | std::string_view payload(outputBuffer.data(), bytesRead); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 81 | for (crow::websocket::Connection* session : sessions) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 82 | { |
Cheng C Yang | 81bfd11 | 2019-01-03 14:57:03 +0800 | [diff] [blame] | 83 | session->sendBinary(payload); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 84 | } |
| 85 | doRead(); |
| 86 | }); |
| 87 | } |
| 88 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 89 | inline void connectHandler(const boost::system::error_code& ec) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 90 | { |
| 91 | if (ec) |
| 92 | { |
| 93 | BMCWEB_LOG_ERROR << "Couldn't connect to host serial port: " << ec; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 94 | for (crow::websocket::Connection* session : sessions) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 95 | { |
| 96 | session->close("Error in connecting to host port"); |
| 97 | } |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | doWrite(); |
| 102 | doRead(); |
| 103 | } |
| 104 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 105 | inline void requestRoutes(App& app) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 106 | { |
| 107 | BMCWEB_ROUTE(app, "/console0") |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 108 | .privileges({"ConfigureComponents", "ConfigureManager"}) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 109 | .websocket() |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 110 | .onopen([](crow::websocket::Connection& conn, |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 111 | const std::shared_ptr<bmcweb::AsyncResp>&) { |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 112 | BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened"; |
| 113 | |
| 114 | sessions.insert(&conn); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 115 | if (hostSocket == nullptr) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 116 | { |
| 117 | const std::string consoleName("\0obmc-console", 13); |
| 118 | boost::asio::local::stream_protocol::endpoint ep(consoleName); |
| 119 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 120 | hostSocket = std::make_unique< |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 121 | boost::asio::local::stream_protocol::socket>( |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 122 | conn.getIoContext()); |
| 123 | hostSocket->async_connect(ep, connectHandler); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 124 | } |
| 125 | }) |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 126 | .onclose([](crow::websocket::Connection& conn, |
| 127 | [[maybe_unused]] const std::string& reason) { |
| 128 | sessions.erase(&conn); |
| 129 | if (sessions.empty()) |
| 130 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 131 | hostSocket = nullptr; |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 132 | inputBuffer.clear(); |
| 133 | inputBuffer.shrink_to_fit(); |
| 134 | } |
| 135 | }) |
| 136 | .onmessage([]([[maybe_unused]] crow::websocket::Connection& conn, |
| 137 | const std::string& data, |
| 138 | [[maybe_unused]] bool is_binary) { |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 139 | inputBuffer += data; |
| 140 | doWrite(); |
| 141 | }); |
| 142 | } |
| 143 | } // namespace obmc_console |
| 144 | } // namespace crow |