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