blob: c1a703289944627866e5ad635cb235fdecd7e60b [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"
Lei YU0a4dde42022-07-12 19:42:15 +08006#include "sessions_manager.hpp"
Tom Josephf846fb32017-03-31 10:36:23 +05307#include "sol/console_buffer.hpp"
Tom Josephe6361a22016-08-10 06:56:25 -05008
Vernon Maueryd999ffc2018-10-25 09:16:05 -07009#include <memory>
Vernon Mauery9e801a22018-10-12 13:20:49 -070010
Tom Josephe6361a22016-08-10 06:56:25 -050011namespace message
12{
13
Vernon Mauery8d6f2002018-11-07 09:55:53 -080014class Handler : public std::enable_shared_from_this<Handler>
Tom Josephe6361a22016-08-10 06:56:25 -050015{
Vernon Mauery9e801a22018-10-12 13:20:49 -070016 public:
Vernon Mauery8d6f2002018-11-07 09:55:53 -080017 /**
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 Williams84256242024-08-16 15:20:21 -040024 sessionID(sessionID), channel(channel), io(io)
Lei YU0a4dde42022-07-12 19:42:15 +080025 {
26 if (sessionID != message::Message::MESSAGE_INVALID_SESSION_ID)
27 {
28 session = session::Manager::get().getSession(sessionID);
29 }
30 }
Tom Josephe6361a22016-08-10 06:56:25 -050031
Vernon Mauery8d6f2002018-11-07 09:55:53 -080032 /**
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 Williams84256242024-08-16 15:20:21 -040037 sessionID(sessionID), channel(channel), io(nullptr)
Lei YU0a4dde42022-07-12 19:42:15 +080038 {
39 if (sessionID != message::Message::MESSAGE_INVALID_SESSION_ID)
40 {
41 session = session::Manager::get().getSession(sessionID);
42 }
43 }
Vernon Mauery8d6f2002018-11-07 09:55:53 -080044
45 ~Handler();
Vernon Mauery9e801a22018-10-12 13:20:49 -070046 Handler() = delete;
Vernon Mauery7f268e42018-10-26 10:26:01 -070047 Handler(const Handler&) = delete;
48 Handler& operator=(const Handler&) = delete;
49 Handler(Handler&&) = delete;
50 Handler& operator=(Handler&&) = delete;
Tom Josephe6361a22016-08-10 06:56:25 -050051
Vernon Mauery9e801a22018-10-12 13:20:49 -070052 /**
Vernon Mauery9e801a22018-10-12 13:20:49 -070053 * @brief Process the incoming IPMI message
54 *
Vernon Mauery8d6f2002018-11-07 09:55:53 -080055 * 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 Mauery9e801a22018-10-12 13:20:49 -070060 */
Vernon Mauery8d6f2002018-11-07 09:55:53 -080061 void processIncoming();
Tom Josephe6361a22016-08-10 06:56:25 -050062
Vernon Mauery9e801a22018-10-12 13:20:49 -070063 /** @brief Set socket channel in session object */
64 void setChannelInSession() const;
Tom Josephff848492017-03-31 10:43:48 +053065
Vernon Mauery9e801a22018-10-12 13:20:49 -070066 /** @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 Joseph22596f22017-03-31 10:52:27 +053073
Vernon Mauery9e801a22018-10-12 13:20:49 -070074 /** @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 Joseph63d3e492017-03-31 11:01:08 +053086
Vernon Mauery9e801a22018-10-12 13:20:49 -070087 // BMC Session ID for the Channel
88 session::SessionID sessionID;
Tom Josephf846fb32017-03-31 10:36:23 +053089
Vernon Mauery8d6f2002018-11-07 09:55:53 -080090 /** @brief response to send back */
91 std::optional<std::vector<uint8_t>> outPayload;
92
Vernon Mauery9e801a22018-10-12 13:20:49 -070093 private:
Vernon Mauery8d6f2002018-11-07 09:55:53 -080094 /**
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 Pakhomovde7dd5c2021-02-27 18:45:22 +0300103 * @brief Get Session data from the IPMI packet
104 *
105 */
106 void updSessionData(std::shared_ptr<Message>& inMessage);
107
108 /**
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800109 * @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 Pakhomovde7dd5c2021-02-27 18:45:22 +0300126#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 Mauery9e801a22018-10-12 13:20:49 -0700141 /** @brief Socket channel for communicating with the remote client.*/
142 std::shared_ptr<udpsocket::Channel> channel;
Tom Josephe6361a22016-08-10 06:56:25 -0500143
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800144 /** @brief asio io context to run asynchrounously */
145 std::shared_ptr<boost::asio::io_context> io;
146
Vernon Mauery9e801a22018-10-12 13:20:49 -0700147 parser::SessionHeader sessionHeader = parser::SessionHeader::IPMI20;
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800148
Vernon Maueryec5ddaf2022-08-03 11:00:50 -0700149 std::shared_ptr<message::Message> inMessage{};
Lei YU0a4dde42022-07-12 19:42:15 +0800150
151 /** @brief The IPMI session of the handler */
Vernon Maueryec5ddaf2022-08-03 11:00:50 -0700152 std::shared_ptr<session::Session> session{};
Tom Josephe6361a22016-08-10 06:56:25 -0500153};
154
Vernon Mauery9e801a22018-10-12 13:20:49 -0700155} // namespace message