| Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 | 
 | 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors | 
| Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 3 | #pragma once | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 4 | #include "app.hpp" | 
 | 5 | #include "async_resp.hpp" | 
| Ed Tanous | faf100f | 2023-05-25 10:03:14 -0700 | [diff] [blame] | 6 | #include "websocket.hpp" | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 7 |  | 
| Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 8 | #include <sys/socket.h> | 
 | 9 |  | 
| Ed Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 10 | #include <boost/asio/local/stream_protocol.hpp> | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 11 | #include <boost/container/flat_map.hpp> | 
| Gunnar Mills | 8b901d7 | 2024-05-03 16:58:32 -0500 | [diff] [blame] | 12 | #include <boost/system/error_code.hpp> | 
 | 13 |  | 
 | 14 | #include <array> | 
 | 15 | #include <memory> | 
 | 16 | #include <string> | 
 | 17 | #include <string_view> | 
| Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 18 |  | 
 | 19 | namespace crow | 
 | 20 | { | 
 | 21 | namespace obmc_console | 
 | 22 | { | 
 | 23 |  | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 24 | // Update this value each time we add new console route. | 
 | 25 | static constexpr const uint maxSessions = 32; | 
| Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 26 |  | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 27 | class ConsoleHandler : public std::enable_shared_from_this<ConsoleHandler> | 
| Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 28 | { | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 29 |   public: | 
 | 30 |     ConsoleHandler(boost::asio::io_context& ioc, | 
 | 31 |                    crow::websocket::Connection& connIn) : | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 32 |         hostSocket(ioc), conn(connIn) | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 33 |     {} | 
 | 34 |  | 
 | 35 |     ~ConsoleHandler() = default; | 
 | 36 |  | 
 | 37 |     ConsoleHandler(const ConsoleHandler&) = delete; | 
 | 38 |     ConsoleHandler(ConsoleHandler&&) = delete; | 
 | 39 |     ConsoleHandler& operator=(const ConsoleHandler&) = delete; | 
 | 40 |     ConsoleHandler& operator=(ConsoleHandler&&) = delete; | 
 | 41 |  | 
 | 42 |     void doWrite() | 
| Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 43 |     { | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 44 |         if (doingWrite) | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 45 |         { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 46 |             BMCWEB_LOG_DEBUG("Already writing.  Bailing out"); | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 47 |             return; | 
 | 48 |         } | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 49 |  | 
 | 50 |         if (inputBuffer.empty()) | 
 | 51 |         { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 52 |             BMCWEB_LOG_DEBUG("Outbuffer empty.  Bailing out"); | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 53 |             return; | 
 | 54 |         } | 
 | 55 |  | 
 | 56 |         doingWrite = true; | 
 | 57 |         hostSocket.async_write_some( | 
 | 58 |             boost::asio::buffer(inputBuffer.data(), inputBuffer.size()), | 
 | 59 |             [weak(weak_from_this())](const boost::beast::error_code& ec, | 
 | 60 |                                      std::size_t bytesWritten) { | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 61 |                 std::shared_ptr<ConsoleHandler> self = weak.lock(); | 
 | 62 |                 if (self == nullptr) | 
 | 63 |                 { | 
 | 64 |                     return; | 
 | 65 |                 } | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 66 |  | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 67 |                 self->doingWrite = false; | 
 | 68 |                 self->inputBuffer.erase(0, bytesWritten); | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 69 |  | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 70 |                 if (ec == boost::asio::error::eof) | 
 | 71 |                 { | 
 | 72 |                     self->conn.close("Error in reading to host port"); | 
 | 73 |                     return; | 
 | 74 |                 } | 
 | 75 |                 if (ec) | 
 | 76 |                 { | 
 | 77 |                     BMCWEB_LOG_ERROR("Error in host serial write {}", | 
 | 78 |                                      ec.message()); | 
 | 79 |                     return; | 
 | 80 |                 } | 
 | 81 |                 self->doWrite(); | 
 | 82 |             }); | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 83 |     } | 
 | 84 |  | 
| Ed Tanous | 099793f | 2023-05-31 08:54:26 -0700 | [diff] [blame] | 85 |     static void afterSendEx(const std::weak_ptr<ConsoleHandler>& weak) | 
 | 86 |     { | 
 | 87 |         std::shared_ptr<ConsoleHandler> self = weak.lock(); | 
 | 88 |         if (self == nullptr) | 
 | 89 |         { | 
 | 90 |             return; | 
 | 91 |         } | 
 | 92 |         self->doRead(); | 
 | 93 |     } | 
 | 94 |  | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 95 |     void doRead() | 
 | 96 |     { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 97 |         BMCWEB_LOG_DEBUG("Reading from socket"); | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 98 |         hostSocket.async_read_some( | 
| Ed Tanous | 099793f | 2023-05-31 08:54:26 -0700 | [diff] [blame] | 99 |             boost::asio::buffer(outputBuffer), | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 100 |             [this, weakSelf(weak_from_this())]( | 
 | 101 |                 const boost::system::error_code& ec, std::size_t bytesRead) { | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 102 |                 BMCWEB_LOG_DEBUG("read done.  Read {} bytes", bytesRead); | 
 | 103 |                 std::shared_ptr<ConsoleHandler> self = weakSelf.lock(); | 
 | 104 |                 if (self == nullptr) | 
 | 105 |                 { | 
 | 106 |                     return; | 
 | 107 |                 } | 
 | 108 |                 if (ec) | 
 | 109 |                 { | 
 | 110 |                     BMCWEB_LOG_ERROR("Couldn't read from host serial port: {}", | 
 | 111 |                                      ec.message()); | 
 | 112 |                     conn.close("Error connecting to host port"); | 
 | 113 |                     return; | 
 | 114 |                 } | 
 | 115 |                 std::string_view payload(outputBuffer.data(), bytesRead); | 
 | 116 |                 self->conn.sendEx( | 
 | 117 |                     crow::websocket::MessageType::Binary, payload, | 
 | 118 |                     std::bind_front(afterSendEx, weak_from_this())); | 
 | 119 |             }); | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 120 |     } | 
 | 121 |  | 
 | 122 |     bool connect(int fd) | 
 | 123 |     { | 
 | 124 |         boost::system::error_code ec; | 
 | 125 |         boost::asio::local::stream_protocol proto; | 
 | 126 |  | 
 | 127 |         hostSocket.assign(proto, fd, ec); | 
 | 128 |  | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 129 |         if (ec) | 
 | 130 |         { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 131 |             BMCWEB_LOG_ERROR( | 
 | 132 |                 "Failed to assign the DBUS socket Socket assign error: {}", | 
 | 133 |                 ec.message()); | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 134 |             return false; | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 135 |         } | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 136 |  | 
 | 137 |         conn.resumeRead(); | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 138 |         doWrite(); | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 139 |         doRead(); | 
 | 140 |         return true; | 
 | 141 |     } | 
 | 142 |  | 
 | 143 |     boost::asio::local::stream_protocol::socket hostSocket; | 
 | 144 |  | 
| Ed Tanous | 099793f | 2023-05-31 08:54:26 -0700 | [diff] [blame] | 145 |     std::array<char, 4096> outputBuffer{}; | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 146 |  | 
 | 147 |     std::string inputBuffer; | 
 | 148 |     bool doingWrite = false; | 
 | 149 |     crow::websocket::Connection& conn; | 
 | 150 | }; | 
 | 151 |  | 
 | 152 | using ObmcConsoleMap = boost::container::flat_map< | 
 | 153 |     crow::websocket::Connection*, std::shared_ptr<ConsoleHandler>, std::less<>, | 
 | 154 |     std::vector<std::pair<crow::websocket::Connection*, | 
 | 155 |                           std::shared_ptr<ConsoleHandler>>>>; | 
 | 156 |  | 
 | 157 | inline ObmcConsoleMap& getConsoleHandlerMap() | 
 | 158 | { | 
 | 159 |     static ObmcConsoleMap map; | 
 | 160 |     return map; | 
| Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 161 | } | 
 | 162 |  | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 163 | // Remove connection from the connection map and if connection map is empty | 
 | 164 | // then remove the handler from handlers map. | 
 | 165 | inline void onClose(crow::websocket::Connection& conn, const std::string& err) | 
| Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 166 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 167 |     BMCWEB_LOG_INFO("Closing websocket. Reason: {}", err); | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 168 |  | 
 | 169 |     auto iter = getConsoleHandlerMap().find(&conn); | 
 | 170 |     if (iter == getConsoleHandlerMap().end()) | 
| AppaRao Puli | 5238bd3 | 2020-11-17 11:03:11 +0530 | [diff] [blame] | 171 |     { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 172 |         BMCWEB_LOG_CRITICAL("Unable to find connection {}", logPtr(&conn)); | 
| AppaRao Puli | 5238bd3 | 2020-11-17 11:03:11 +0530 | [diff] [blame] | 173 |         return; | 
 | 174 |     } | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 175 |     BMCWEB_LOG_DEBUG("Remove connection {} from obmc console", logPtr(&conn)); | 
| AppaRao Puli | 5238bd3 | 2020-11-17 11:03:11 +0530 | [diff] [blame] | 176 |  | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 177 |     // Removed last connection so remove the path | 
 | 178 |     getConsoleHandlerMap().erase(iter); | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 179 | } | 
 | 180 |  | 
 | 181 | inline void connectConsoleSocket(crow::websocket::Connection& conn, | 
 | 182 |                                  const boost::system::error_code& ec, | 
 | 183 |                                  const sdbusplus::message::unix_fd& unixfd) | 
 | 184 | { | 
| Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 185 |     if (ec) | 
 | 186 |     { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 187 |         BMCWEB_LOG_ERROR( | 
 | 188 |             "Failed to call console Connect() method DBUS error: {}", | 
 | 189 |             ec.message()); | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 190 |         conn.close("Failed to connect"); | 
| Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 191 |         return; | 
 | 192 |     } | 
 | 193 |  | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 194 |     // Look up the handler | 
 | 195 |     auto iter = getConsoleHandlerMap().find(&conn); | 
 | 196 |     if (iter == getConsoleHandlerMap().end()) | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 197 |     { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 198 |         BMCWEB_LOG_ERROR("Connection was already closed"); | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 199 |         return; | 
 | 200 |     } | 
 | 201 |  | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 202 |     int fd = dup(unixfd); | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 203 |     if (fd == -1) | 
 | 204 |     { | 
| Ed Tanous | 7da633f | 2024-12-02 08:25:38 -0800 | [diff] [blame] | 205 |         BMCWEB_LOG_ERROR("Failed to dup the DBUS unixfd error"); | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 206 |         conn.close("Internal error"); | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 207 |         return; | 
 | 208 |     } | 
 | 209 |  | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 210 |     BMCWEB_LOG_DEBUG("Console duped FD: {}", fd); | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 211 |  | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 212 |     if (!iter->second->connect(fd)) | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 213 |     { | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 214 |         close(fd); | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 215 |         conn.close("Internal Error"); | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 216 |     } | 
 | 217 | } | 
 | 218 |  | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 219 | inline void processConsoleObject( | 
 | 220 |     crow::websocket::Connection& conn, const std::string& consoleObjPath, | 
 | 221 |     const boost::system::error_code& ec, | 
 | 222 |     const ::dbus::utility::MapperGetObject& objInfo) | 
| Ninad Palsule | 052bcbf | 2023-05-30 11:10:58 -0500 | [diff] [blame] | 223 | { | 
 | 224 |     // Look up the handler | 
 | 225 |     auto iter = getConsoleHandlerMap().find(&conn); | 
 | 226 |     if (iter == getConsoleHandlerMap().end()) | 
 | 227 |     { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 228 |         BMCWEB_LOG_ERROR("Connection was already closed"); | 
| Ninad Palsule | 052bcbf | 2023-05-30 11:10:58 -0500 | [diff] [blame] | 229 |         return; | 
 | 230 |     } | 
 | 231 |  | 
 | 232 |     if (ec) | 
 | 233 |     { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 234 |         BMCWEB_LOG_WARNING("getDbusObject() for consoles failed. DBUS error:{}", | 
 | 235 |                            ec.message()); | 
| Ninad Palsule | 052bcbf | 2023-05-30 11:10:58 -0500 | [diff] [blame] | 236 |         conn.close("getDbusObject() for consoles failed."); | 
 | 237 |         return; | 
 | 238 |     } | 
 | 239 |  | 
 | 240 |     const auto valueIface = objInfo.begin(); | 
 | 241 |     if (valueIface == objInfo.end()) | 
 | 242 |     { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 243 |         BMCWEB_LOG_WARNING("getDbusObject() returned unexpected size: {}", | 
 | 244 |                            objInfo.size()); | 
| Ninad Palsule | 052bcbf | 2023-05-30 11:10:58 -0500 | [diff] [blame] | 245 |         conn.close("getDbusObject() returned unexpected size"); | 
 | 246 |         return; | 
 | 247 |     } | 
 | 248 |  | 
 | 249 |     const std::string& consoleService = valueIface->first; | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 250 |     BMCWEB_LOG_DEBUG("Looking up unixFD for Service {} Path {}", consoleService, | 
 | 251 |                      consoleObjPath); | 
| Ninad Palsule | 052bcbf | 2023-05-30 11:10:58 -0500 | [diff] [blame] | 252 |     // Call Connect() method to get the unix FD | 
 | 253 |     crow::connections::systemBus->async_method_call( | 
 | 254 |         [&conn](const boost::system::error_code& ec1, | 
 | 255 |                 const sdbusplus::message::unix_fd& unixfd) { | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 256 |             connectConsoleSocket(conn, ec1, unixfd); | 
 | 257 |         }, | 
| Ninad Palsule | 052bcbf | 2023-05-30 11:10:58 -0500 | [diff] [blame] | 258 |         consoleService, consoleObjPath, "xyz.openbmc_project.Console.Access", | 
 | 259 |         "Connect"); | 
 | 260 | } | 
 | 261 |  | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 262 | // Query consoles from DBUS and find the matching to the | 
 | 263 | // rules string. | 
 | 264 | inline void onOpen(crow::websocket::Connection& conn) | 
 | 265 | { | 
| Ninad Palsule | 052bcbf | 2023-05-30 11:10:58 -0500 | [diff] [blame] | 266 |     std::string consoleLeaf; | 
 | 267 |  | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 268 |     BMCWEB_LOG_DEBUG("Connection {} opened", logPtr(&conn)); | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 269 |  | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 270 |     if (getConsoleHandlerMap().size() >= maxSessions) | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 271 |     { | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 272 |         conn.close("Max sessions are already connected"); | 
 | 273 |         return; | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 274 |     } | 
 | 275 |  | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 276 |     std::shared_ptr<ConsoleHandler> handler = | 
 | 277 |         std::make_shared<ConsoleHandler>(conn.getIoContext(), conn); | 
 | 278 |     getConsoleHandlerMap().emplace(&conn, handler); | 
 | 279 |  | 
 | 280 |     conn.deferRead(); | 
 | 281 |  | 
| Ninad Palsule | 052bcbf | 2023-05-30 11:10:58 -0500 | [diff] [blame] | 282 |     // Keep old path for backward compatibility | 
 | 283 |     if (conn.url().path() == "/console0") | 
 | 284 |     { | 
 | 285 |         consoleLeaf = "default"; | 
 | 286 |     } | 
 | 287 |     else | 
 | 288 |     { | 
 | 289 |         // Get the console id from console router path and prepare the console | 
 | 290 |         // object path and console service. | 
 | 291 |         consoleLeaf = conn.url().segments().back(); | 
 | 292 |     } | 
 | 293 |     std::string consolePath = | 
 | 294 |         sdbusplus::message::object_path("/xyz/openbmc_project/console") / | 
 | 295 |         consoleLeaf; | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 296 |  | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 297 |     BMCWEB_LOG_DEBUG("Console Object path = {} Request target = {}", | 
 | 298 |                      consolePath, conn.url().path()); | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 299 |  | 
| Ninad Palsule | 052bcbf | 2023-05-30 11:10:58 -0500 | [diff] [blame] | 300 |     // mapper call lambda | 
 | 301 |     constexpr std::array<std::string_view, 1> interfaces = { | 
 | 302 |         "xyz.openbmc_project.Console.Access"}; | 
 | 303 |  | 
 | 304 |     dbus::utility::getDbusObject( | 
 | 305 |         consolePath, interfaces, | 
 | 306 |         [&conn, consolePath](const boost::system::error_code& ec, | 
 | 307 |                              const ::dbus::utility::MapperGetObject& objInfo) { | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 308 |             processConsoleObject(conn, consolePath, ec, objInfo); | 
 | 309 |         }); | 
| Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 310 | } | 
 | 311 |  | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 312 | inline void onMessage(crow::websocket::Connection& conn, | 
 | 313 |                       const std::string& data, bool /*isBinary*/) | 
 | 314 | { | 
 | 315 |     auto handler = getConsoleHandlerMap().find(&conn); | 
 | 316 |     if (handler == getConsoleHandlerMap().end()) | 
 | 317 |     { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 318 |         BMCWEB_LOG_CRITICAL("Unable to find connection {}", logPtr(&conn)); | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 319 |         return; | 
 | 320 |     } | 
 | 321 |     handler->second->inputBuffer += data; | 
 | 322 |     handler->second->doWrite(); | 
 | 323 | } | 
 | 324 |  | 
| Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 325 | inline void requestRoutes(App& app) | 
| Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 326 | { | 
 | 327 |     BMCWEB_ROUTE(app, "/console0") | 
| Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 328 |         .privileges({{"OpenBMCHostConsole"}}) | 
| Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 329 |         .websocket() | 
| Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 330 |         .onopen(onOpen) | 
| Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 331 |         .onclose(onClose) | 
 | 332 |         .onmessage(onMessage); | 
| Ninad Palsule | 052bcbf | 2023-05-30 11:10:58 -0500 | [diff] [blame] | 333 |  | 
 | 334 |     BMCWEB_ROUTE(app, "/console/<str>") | 
 | 335 |         .privileges({{"OpenBMCHostConsole"}}) | 
 | 336 |         .websocket() | 
 | 337 |         .onopen(onOpen) | 
 | 338 |         .onclose(onClose) | 
 | 339 |         .onmessage(onMessage); | 
| Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 340 | } | 
 | 341 | } // namespace obmc_console | 
 | 342 | } // namespace crow |