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