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