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 | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 48 | doingWrite = false; |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 49 | inputBuffer.erase(0, bytesWritten); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 50 | |
| 51 | if (ec == boost::asio::error::eof) |
| 52 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 53 | for (crow::websocket::Connection* session : sessions) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 54 | { |
| 55 | session->close("Error in reading to host port"); |
| 56 | } |
| 57 | return; |
| 58 | } |
| 59 | if (ec) |
| 60 | { |
| 61 | BMCWEB_LOG_ERROR << "Error in host serial write " << ec; |
| 62 | return; |
| 63 | } |
| 64 | doWrite(); |
| 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) { |
| 80 | BMCWEB_LOG_DEBUG << "read done. Read " << bytesRead << " bytes"; |
| 81 | if (ec) |
| 82 | { |
| 83 | BMCWEB_LOG_ERROR << "Couldn't read from host serial port: " |
| 84 | << ec; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 85 | for (crow::websocket::Connection* session : sessions) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 86 | { |
| 87 | session->close("Error in connecting to host port"); |
| 88 | } |
| 89 | return; |
| 90 | } |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 91 | std::string_view payload(outputBuffer.data(), bytesRead); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 92 | for (crow::websocket::Connection* session : sessions) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 93 | { |
Cheng C Yang | 81bfd11 | 2019-01-03 14:57:03 +0800 | [diff] [blame] | 94 | session->sendBinary(payload); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 95 | } |
| 96 | doRead(); |
| 97 | }); |
| 98 | } |
| 99 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 100 | inline void connectHandler(const boost::system::error_code& ec) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 101 | { |
| 102 | if (ec) |
| 103 | { |
| 104 | BMCWEB_LOG_ERROR << "Couldn't connect to host serial port: " << ec; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 105 | for (crow::websocket::Connection* session : sessions) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 106 | { |
| 107 | session->close("Error in connecting to host port"); |
| 108 | } |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | doWrite(); |
| 113 | doRead(); |
| 114 | } |
| 115 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 116 | inline void requestRoutes(App& app) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 117 | { |
| 118 | BMCWEB_ROUTE(app, "/console0") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 119 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 120 | .websocket() |
zhanghch05 | 7772638 | 2021-10-21 14:07:57 +0800 | [diff] [blame] | 121 | .onopen([](crow::websocket::Connection& conn) { |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 122 | BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened"; |
| 123 | |
| 124 | sessions.insert(&conn); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 125 | if (hostSocket == nullptr) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 126 | { |
| 127 | const std::string consoleName("\0obmc-console", 13); |
| 128 | boost::asio::local::stream_protocol::endpoint ep(consoleName); |
| 129 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 130 | hostSocket = std::make_unique< |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 131 | boost::asio::local::stream_protocol::socket>( |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 132 | conn.getIoContext()); |
| 133 | hostSocket->async_connect(ep, connectHandler); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 134 | } |
| 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 |