blob: 88197ca8c3f9bac77ea59584cf8719f217579630 [file] [log] [blame]
Tom Josephe6361a22016-08-10 06:56:25 -05001#pragma once
2
Tom Josephe6361a22016-08-10 06:56:25 -05003#include "message.hpp"
4#include "message_parsers.hpp"
5#include "session.hpp"
Tom Josephf846fb32017-03-31 10:36:23 +05306#include "sol/console_buffer.hpp"
Tom Josephe6361a22016-08-10 06:56:25 -05007
Vernon Maueryd999ffc2018-10-25 09:16:05 -07008#include <memory>
Vernon Mauery9e801a22018-10-12 13:20:49 -07009
Tom Josephe6361a22016-08-10 06:56:25 -050010namespace message
11{
12
Vernon Mauery8d6f2002018-11-07 09:55:53 -080013class Handler : public std::enable_shared_from_this<Handler>
Tom Josephe6361a22016-08-10 06:56:25 -050014{
Vernon Mauery9e801a22018-10-12 13:20:49 -070015 public:
Vernon Mauery8d6f2002018-11-07 09:55:53 -080016 /**
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 Mauery9e801a22018-10-12 13:20:49 -070023 sessionID(sessionID),
Vernon Mauery8d6f2002018-11-07 09:55:53 -080024 channel(channel), io(io)
George Liubc8958f2022-07-04 09:29:49 +080025 {}
Tom Josephe6361a22016-08-10 06:56:25 -050026
Vernon Mauery8d6f2002018-11-07 09:55:53 -080027 /**
28 * @brief Create a Handler intended for a send only (SOL)
29 */
30 Handler(std::shared_ptr<udpsocket::Channel> channel,
31 uint32_t sessionID = message::Message::MESSAGE_INVALID_SESSION_ID) :
32 sessionID(sessionID),
33 channel(channel), io(nullptr)
George Liubc8958f2022-07-04 09:29:49 +080034 {}
Vernon Mauery8d6f2002018-11-07 09:55:53 -080035
36 ~Handler();
Vernon Mauery9e801a22018-10-12 13:20:49 -070037 Handler() = delete;
Vernon Mauery7f268e42018-10-26 10:26:01 -070038 Handler(const Handler&) = delete;
39 Handler& operator=(const Handler&) = delete;
40 Handler(Handler&&) = delete;
41 Handler& operator=(Handler&&) = delete;
Tom Josephe6361a22016-08-10 06:56:25 -050042
Vernon Mauery9e801a22018-10-12 13:20:49 -070043 /**
Vernon Mauery9e801a22018-10-12 13:20:49 -070044 * @brief Process the incoming IPMI message
45 *
Vernon Mauery8d6f2002018-11-07 09:55:53 -080046 * The incoming payload is read from the channel. If a message is read, it
47 * is passed onto executeCommand, which may or may not execute the command
48 * asynchrounously. If the command is executed asynchrounously, a shared_ptr
49 * of self via shared_from_this will keep this object alive until the
50 * response is ready. Then on the destructor, the response will be sent.
Vernon Mauery9e801a22018-10-12 13:20:49 -070051 */
Vernon Mauery8d6f2002018-11-07 09:55:53 -080052 void processIncoming();
Tom Josephe6361a22016-08-10 06:56:25 -050053
Vernon Mauery9e801a22018-10-12 13:20:49 -070054 /** @brief Set socket channel in session object */
55 void setChannelInSession() const;
Tom Josephff848492017-03-31 10:43:48 +053056
Vernon Mauery9e801a22018-10-12 13:20:49 -070057 /** @brief Send the SOL payload
58 *
59 * The SOL payload is flattened and sent out on the socket
60 *
61 * @param[in] input - SOL Payload
62 */
63 void sendSOLPayload(const std::vector<uint8_t>& input);
Tom Joseph22596f22017-03-31 10:52:27 +053064
Vernon Mauery9e801a22018-10-12 13:20:49 -070065 /** @brief Send the unsolicited IPMI payload to the remote console.
66 *
67 * This is used by commands like SOL activating, in which case the BMC
68 * has to notify the remote console that a SOL payload is activating
69 * on another channel.
70 *
71 * @param[in] netfn - Net function.
72 * @param[in] cmd - Command.
73 * @param[in] input - Command request data.
74 */
75 void sendUnsolicitedIPMIPayload(uint8_t netfn, uint8_t cmd,
76 const std::vector<uint8_t>& input);
Tom Joseph63d3e492017-03-31 11:01:08 +053077
Vernon Mauery9e801a22018-10-12 13:20:49 -070078 // BMC Session ID for the Channel
79 session::SessionID sessionID;
Tom Josephf846fb32017-03-31 10:36:23 +053080
Vernon Mauery8d6f2002018-11-07 09:55:53 -080081 /** @brief response to send back */
82 std::optional<std::vector<uint8_t>> outPayload;
83
Vernon Mauery9e801a22018-10-12 13:20:49 -070084 private:
Vernon Mauery8d6f2002018-11-07 09:55:53 -080085 /**
86 * @brief Receive the IPMI packet
87 *
88 * Read the data on the socket, get the parser based on the Session
89 * header type and flatten the payload and generate the IPMI message
90 */
91 bool receive();
92
93 /**
Kirill Pakhomovde7dd5c2021-02-27 18:45:22 +030094 * @brief Get Session data from the IPMI packet
95 *
96 */
97 void updSessionData(std::shared_ptr<Message>& inMessage);
98
99 /**
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800100 * @brief Process the incoming IPMI message
101 *
102 * The incoming message payload is handled and the command handler for
103 * the Network function and Command is executed and the response message
104 * is returned
105 */
106 void executeCommand();
107
108 /** @brief Send the outgoing message
109 *
110 * The payload in the outgoing message is flattened and sent out on the
111 * socket
112 *
113 * @param[in] outMessage - Outgoing Message
114 */
115 void send(std::shared_ptr<Message> outMessage);
116
Kirill Pakhomovde7dd5c2021-02-27 18:45:22 +0300117#ifdef RMCP_PING
118 /** @brief Send the outgoing ASF message
119 *
120 * The outgoing ASF message contains only ASF message header
121 * which is flattened and sent out on the socket
122 */
123 void sendASF();
124#endif // RMCP_PING
125
126 /** @brief Write the packet to the socket
127 *
128 * @param[in] packet - Outgoing packet
129 */
130 void writeData(const std::vector<uint8_t>& packet);
131
Vernon Mauery9e801a22018-10-12 13:20:49 -0700132 /** @brief Socket channel for communicating with the remote client.*/
133 std::shared_ptr<udpsocket::Channel> channel;
Tom Josephe6361a22016-08-10 06:56:25 -0500134
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800135 /** @brief asio io context to run asynchrounously */
136 std::shared_ptr<boost::asio::io_context> io;
137
Vernon Mauery9e801a22018-10-12 13:20:49 -0700138 parser::SessionHeader sessionHeader = parser::SessionHeader::IPMI20;
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800139
140 std::shared_ptr<message::Message> inMessage;
Tom Josephe6361a22016-08-10 06:56:25 -0500141};
142
Vernon Mauery9e801a22018-10-12 13:20:49 -0700143} // namespace message