Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 3 | #include "message.hpp" |
| 4 | #include "message_parsers.hpp" |
| 5 | #include "session.hpp" |
Lei YU | 0a4dde4 | 2022-07-12 19:42:15 +0800 | [diff] [blame] | 6 | #include "sessions_manager.hpp" |
Tom Joseph | f846fb3 | 2017-03-31 10:36:23 +0530 | [diff] [blame] | 7 | #include "sol/console_buffer.hpp" |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 8 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 9 | #include <memory> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 10 | |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 11 | namespace message |
| 12 | { |
| 13 | |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 14 | class Handler : public std::enable_shared_from_this<Handler> |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 15 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 16 | public: |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 17 | /** |
| 18 | * @brief Create a Handler intended for a full transaction |
| 19 | * that may or may not use asynchronous responses |
| 20 | */ |
| 21 | Handler(std::shared_ptr<udpsocket::Channel> channel, |
| 22 | std::shared_ptr<boost::asio::io_context> io, |
| 23 | uint32_t sessionID = message::Message::MESSAGE_INVALID_SESSION_ID) : |
Patrick Williams | 8425624 | 2024-08-16 15:20:21 -0400 | [diff] [blame] | 24 | sessionID(sessionID), channel(channel), io(io) |
Lei YU | 0a4dde4 | 2022-07-12 19:42:15 +0800 | [diff] [blame] | 25 | { |
| 26 | if (sessionID != message::Message::MESSAGE_INVALID_SESSION_ID) |
| 27 | { |
| 28 | session = session::Manager::get().getSession(sessionID); |
| 29 | } |
| 30 | } |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 31 | |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 32 | /** |
| 33 | * @brief Create a Handler intended for a send only (SOL) |
| 34 | */ |
| 35 | Handler(std::shared_ptr<udpsocket::Channel> channel, |
| 36 | uint32_t sessionID = message::Message::MESSAGE_INVALID_SESSION_ID) : |
Patrick Williams | 8425624 | 2024-08-16 15:20:21 -0400 | [diff] [blame] | 37 | sessionID(sessionID), channel(channel), io(nullptr) |
Lei YU | 0a4dde4 | 2022-07-12 19:42:15 +0800 | [diff] [blame] | 38 | { |
| 39 | if (sessionID != message::Message::MESSAGE_INVALID_SESSION_ID) |
| 40 | { |
| 41 | session = session::Manager::get().getSession(sessionID); |
| 42 | } |
| 43 | } |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 44 | |
| 45 | ~Handler(); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 46 | Handler() = delete; |
Vernon Mauery | 7f268e4 | 2018-10-26 10:26:01 -0700 | [diff] [blame] | 47 | Handler(const Handler&) = delete; |
| 48 | Handler& operator=(const Handler&) = delete; |
| 49 | Handler(Handler&&) = delete; |
| 50 | Handler& operator=(Handler&&) = delete; |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 51 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 52 | /** |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 53 | * @brief Process the incoming IPMI message |
| 54 | * |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 55 | * The incoming payload is read from the channel. If a message is read, it |
| 56 | * is passed onto executeCommand, which may or may not execute the command |
| 57 | * asynchrounously. If the command is executed asynchrounously, a shared_ptr |
| 58 | * of self via shared_from_this will keep this object alive until the |
| 59 | * response is ready. Then on the destructor, the response will be sent. |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 60 | */ |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 61 | void processIncoming(); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 62 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 63 | /** @brief Set socket channel in session object */ |
| 64 | void setChannelInSession() const; |
Tom Joseph | ff84849 | 2017-03-31 10:43:48 +0530 | [diff] [blame] | 65 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 66 | /** @brief Send the SOL payload |
| 67 | * |
| 68 | * The SOL payload is flattened and sent out on the socket |
| 69 | * |
| 70 | * @param[in] input - SOL Payload |
| 71 | */ |
| 72 | void sendSOLPayload(const std::vector<uint8_t>& input); |
Tom Joseph | 22596f2 | 2017-03-31 10:52:27 +0530 | [diff] [blame] | 73 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 74 | /** @brief Send the unsolicited IPMI payload to the remote console. |
| 75 | * |
| 76 | * This is used by commands like SOL activating, in which case the BMC |
| 77 | * has to notify the remote console that a SOL payload is activating |
| 78 | * on another channel. |
| 79 | * |
| 80 | * @param[in] netfn - Net function. |
| 81 | * @param[in] cmd - Command. |
| 82 | * @param[in] input - Command request data. |
| 83 | */ |
| 84 | void sendUnsolicitedIPMIPayload(uint8_t netfn, uint8_t cmd, |
| 85 | const std::vector<uint8_t>& input); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 86 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 87 | // BMC Session ID for the Channel |
| 88 | session::SessionID sessionID; |
Tom Joseph | f846fb3 | 2017-03-31 10:36:23 +0530 | [diff] [blame] | 89 | |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 90 | /** @brief response to send back */ |
| 91 | std::optional<std::vector<uint8_t>> outPayload; |
| 92 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 93 | private: |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 94 | /** |
| 95 | * @brief Receive the IPMI packet |
| 96 | * |
| 97 | * Read the data on the socket, get the parser based on the Session |
| 98 | * header type and flatten the payload and generate the IPMI message |
| 99 | */ |
| 100 | bool receive(); |
| 101 | |
| 102 | /** |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 103 | * @brief Get Session data from the IPMI packet |
| 104 | * |
| 105 | */ |
| 106 | void updSessionData(std::shared_ptr<Message>& inMessage); |
| 107 | |
| 108 | /** |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 109 | * @brief Process the incoming IPMI message |
| 110 | * |
| 111 | * The incoming message payload is handled and the command handler for |
| 112 | * the Network function and Command is executed and the response message |
| 113 | * is returned |
| 114 | */ |
| 115 | void executeCommand(); |
| 116 | |
| 117 | /** @brief Send the outgoing message |
| 118 | * |
| 119 | * The payload in the outgoing message is flattened and sent out on the |
| 120 | * socket |
| 121 | * |
| 122 | * @param[in] outMessage - Outgoing Message |
| 123 | */ |
| 124 | void send(std::shared_ptr<Message> outMessage); |
| 125 | |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 126 | #ifdef RMCP_PING |
| 127 | /** @brief Send the outgoing ASF message |
| 128 | * |
| 129 | * The outgoing ASF message contains only ASF message header |
| 130 | * which is flattened and sent out on the socket |
| 131 | */ |
| 132 | void sendASF(); |
| 133 | #endif // RMCP_PING |
| 134 | |
| 135 | /** @brief Write the packet to the socket |
| 136 | * |
| 137 | * @param[in] packet - Outgoing packet |
| 138 | */ |
| 139 | void writeData(const std::vector<uint8_t>& packet); |
| 140 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 141 | /** @brief Socket channel for communicating with the remote client.*/ |
| 142 | std::shared_ptr<udpsocket::Channel> channel; |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 143 | |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 144 | /** @brief asio io context to run asynchrounously */ |
| 145 | std::shared_ptr<boost::asio::io_context> io; |
| 146 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 147 | parser::SessionHeader sessionHeader = parser::SessionHeader::IPMI20; |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 148 | |
Vernon Mauery | ec5ddaf | 2022-08-03 11:00:50 -0700 | [diff] [blame] | 149 | std::shared_ptr<message::Message> inMessage{}; |
Lei YU | 0a4dde4 | 2022-07-12 19:42:15 +0800 | [diff] [blame] | 150 | |
| 151 | /** @brief The IPMI session of the handler */ |
Vernon Mauery | ec5ddaf | 2022-08-03 11:00:50 -0700 | [diff] [blame] | 152 | std::shared_ptr<session::Session> session{}; |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 153 | }; |
| 154 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 155 | } // namespace message |