blob: 533ed6a42538d20541467ee196f3ed52575fbac7 [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 /**
96 * @brief Process the incoming IPMI message
97 *
98 * The incoming message payload is handled and the command handler for
99 * the Network function and Command is executed and the response message
100 * is returned
101 */
102 void executeCommand();
103
104 /** @brief Send the outgoing message
105 *
106 * The payload in the outgoing message is flattened and sent out on the
107 * socket
108 *
109 * @param[in] outMessage - Outgoing Message
110 */
111 void send(std::shared_ptr<Message> outMessage);
112
Vernon Mauery9e801a22018-10-12 13:20:49 -0700113 /** @brief Socket channel for communicating with the remote client.*/
114 std::shared_ptr<udpsocket::Channel> channel;
Tom Josephe6361a22016-08-10 06:56:25 -0500115
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800116 /** @brief asio io context to run asynchrounously */
117 std::shared_ptr<boost::asio::io_context> io;
118
Vernon Mauery9e801a22018-10-12 13:20:49 -0700119 parser::SessionHeader sessionHeader = parser::SessionHeader::IPMI20;
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800120
121 std::shared_ptr<message::Message> inMessage;
Tom Josephe6361a22016-08-10 06:56:25 -0500122};
123
Vernon Mauery9e801a22018-10-12 13:20:49 -0700124} // namespace message