blob: 2eb47370ebb9374ed984ff0192e2222bef271747 [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)
Vernon Mauery9e801a22018-10-12 13:20:49 -070025 {
26 }
Tom Josephe6361a22016-08-10 06:56:25 -050027
Vernon Mauery8d6f2002018-11-07 09:55:53 -080028 /**
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 Mauery9e801a22018-10-12 13:20:49 -070039 Handler() = delete;
Vernon Mauery7f268e42018-10-26 10:26:01 -070040 Handler(const Handler&) = delete;
41 Handler& operator=(const Handler&) = delete;
42 Handler(Handler&&) = delete;
43 Handler& operator=(Handler&&) = delete;
Tom Josephe6361a22016-08-10 06:56:25 -050044
Vernon Mauery9e801a22018-10-12 13:20:49 -070045 /**
Vernon Mauery9e801a22018-10-12 13:20:49 -070046 * @brief Process the incoming IPMI message
47 *
Vernon Mauery8d6f2002018-11-07 09:55:53 -080048 * 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 Mauery9e801a22018-10-12 13:20:49 -070053 */
Vernon Mauery8d6f2002018-11-07 09:55:53 -080054 void processIncoming();
Tom Josephe6361a22016-08-10 06:56:25 -050055
Vernon Mauery9e801a22018-10-12 13:20:49 -070056 /** @brief Set socket channel in session object */
57 void setChannelInSession() const;
Tom Josephff848492017-03-31 10:43:48 +053058
Vernon Mauery9e801a22018-10-12 13:20:49 -070059 /** @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 Joseph22596f22017-03-31 10:52:27 +053066
Vernon Mauery9e801a22018-10-12 13:20:49 -070067 /** @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 Joseph63d3e492017-03-31 11:01:08 +053079
Vernon Mauery9e801a22018-10-12 13:20:49 -070080 // BMC Session ID for the Channel
81 session::SessionID sessionID;
Tom Josephf846fb32017-03-31 10:36:23 +053082
Vernon Mauery8d6f2002018-11-07 09:55:53 -080083 /** @brief response to send back */
84 std::optional<std::vector<uint8_t>> outPayload;
85
Vernon Mauery9e801a22018-10-12 13:20:49 -070086 private:
Vernon Mauery8d6f2002018-11-07 09:55:53 -080087 /**
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 Pakhomovde7dd5c2021-02-27 18:45:22 +030096 * @brief Get Session data from the IPMI packet
97 *
98 */
99 void updSessionData(std::shared_ptr<Message>& inMessage);
100
101 /**
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800102 * @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 Pakhomovde7dd5c2021-02-27 18:45:22 +0300119#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 Mauery9e801a22018-10-12 13:20:49 -0700134 /** @brief Socket channel for communicating with the remote client.*/
135 std::shared_ptr<udpsocket::Channel> channel;
Tom Josephe6361a22016-08-10 06:56:25 -0500136
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800137 /** @brief asio io context to run asynchrounously */
138 std::shared_ptr<boost::asio::io_context> io;
139
Vernon Mauery9e801a22018-10-12 13:20:49 -0700140 parser::SessionHeader sessionHeader = parser::SessionHeader::IPMI20;
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800141
142 std::shared_ptr<message::Message> inMessage;
Tom Josephe6361a22016-08-10 06:56:25 -0500143};
144
Vernon Mauery9e801a22018-10-12 13:20:49 -0700145} // namespace message