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