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