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