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