Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <iostream> |
| 4 | #include <numeric> |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 5 | #include "message.hpp" |
| 6 | #include "message_parsers.hpp" |
| 7 | #include "session.hpp" |
Tom Joseph | f846fb3 | 2017-03-31 10:36:23 +0530 | [diff] [blame] | 8 | #include "sol/console_buffer.hpp" |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 9 | |
| 10 | namespace message |
| 11 | { |
| 12 | |
| 13 | class Handler |
| 14 | { |
| 15 | public: |
Tom Joseph | f846fb3 | 2017-03-31 10:36:23 +0530 | [diff] [blame] | 16 | explicit Handler(std::shared_ptr<udpsocket::Channel> channel, |
| 17 | uint32_t sessionID = |
| 18 | message::Message::MESSAGE_INVALID_SESSION_ID): |
| 19 | sessionID(sessionID), |
| 20 | channel(channel) {} |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 21 | |
| 22 | Handler() = delete; |
| 23 | ~Handler() = default; |
| 24 | Handler(const Handler&) = default; |
| 25 | Handler& operator=(const Handler&) = default; |
| 26 | Handler(Handler&&) = default; |
| 27 | Handler& operator=(Handler&&) = default; |
| 28 | |
| 29 | /* |
| 30 | * @brief Receive the IPMI packet |
| 31 | * |
| 32 | * Read the data on the socket, get the parser based on the Session |
| 33 | * header type and flatten the payload and generate the IPMI message |
| 34 | * |
| 35 | * @return IPMI Message on success and nullptr on failure |
| 36 | * |
| 37 | */ |
| 38 | std::unique_ptr<Message> receive(); |
| 39 | |
| 40 | /* |
| 41 | * @brief Process the incoming IPMI message |
| 42 | * |
| 43 | * The incoming message payload is handled and the command handler for |
| 44 | * the Network function and Command is executed and the response message |
| 45 | * is returned |
| 46 | * |
| 47 | * @param[in] inMessage - Incoming Message |
| 48 | * |
| 49 | * @return Outgoing message on success and nullptr on failure |
| 50 | */ |
| 51 | std::unique_ptr<Message> executeCommand(Message& inMessage); |
| 52 | |
| 53 | /* |
| 54 | * @brief Send the outgoing message |
| 55 | * |
| 56 | * The payload in the outgoing message is flattened and sent out on the |
| 57 | * socket |
| 58 | * |
| 59 | * @param[in] outMessage - Outgoing Message |
| 60 | * |
| 61 | * @return Zero on success and <0 on failure |
| 62 | */ |
| 63 | int send(Message& outMessage); |
| 64 | |
Tom Joseph | ff84849 | 2017-03-31 10:43:48 +0530 | [diff] [blame] | 65 | /** @brief Set socket channel in session object */ |
| 66 | void setChannelInSession() const; |
| 67 | |
Tom Joseph | 22596f2 | 2017-03-31 10:52:27 +0530 | [diff] [blame] | 68 | /** @brief Send the SOL payload |
| 69 | * |
| 70 | * The SOL payload is flattened and sent out on the socket |
| 71 | * |
| 72 | * @param[in] input - SOL Payload |
| 73 | */ |
| 74 | void sendSOLPayload(const sol::Buffer& input); |
| 75 | |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame^] | 76 | /** @brief Send the unsolicited IPMI payload to the remote console. |
| 77 | * |
| 78 | * This is used by commands like SOL activating, in which case the BMC |
| 79 | * has to notify the remote console that a SOL payload is activating |
| 80 | * on another channel. |
| 81 | * |
| 82 | * @param[in] netfn - Net function. |
| 83 | * @param[in] cmd - Command. |
| 84 | * @param[in] input - Command request data. |
| 85 | */ |
| 86 | void sendUnsolicitedIPMIPayload(uint8_t netfn, |
| 87 | uint8_t cmd, |
| 88 | const std::vector<uint8_t>& input); |
| 89 | |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 90 | // BMC Session ID for the Channel |
| 91 | session::SessionID sessionID; |
Tom Joseph | f846fb3 | 2017-03-31 10:36:23 +0530 | [diff] [blame] | 92 | |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 93 | private: |
Tom Joseph | ff84849 | 2017-03-31 10:43:48 +0530 | [diff] [blame] | 94 | /** @brief Socket channel for communicating with the remote client.*/ |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 95 | std::shared_ptr<udpsocket::Channel> channel; |
| 96 | |
Tom Joseph | 31133bc | 2017-03-31 10:46:12 +0530 | [diff] [blame] | 97 | parser::SessionHeader sessionHeader = parser::SessionHeader::IPMI20; |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * @brief Create the response IPMI message |
| 101 | * |
| 102 | * The IPMI outgoing message is constructed out of payload and the |
| 103 | * corresponding fields are populated.For the payload type IPMI, the |
| 104 | * LAN message header and trailer are added. |
| 105 | * |
| 106 | * @tparam[in] T - Outgoing message payload type |
| 107 | * @param[in] output - Payload for outgoing message |
| 108 | * @param[in] inMessage - Incoming IPMI message |
| 109 | * |
| 110 | * @return Outgoing message on success and nullptr on failure |
| 111 | */ |
| 112 | template<PayloadType T> |
| 113 | std::unique_ptr<Message> createResponse(std::vector<uint8_t>& output, |
| 114 | Message& inMessage) |
| 115 | { |
| 116 | auto outMessage = std::make_unique<Message>(); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 117 | outMessage->payloadType = T; |
| 118 | outMessage->payload = output; |
| 119 | return outMessage; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * @brief Extract the command from the IPMI payload |
| 124 | * |
| 125 | * @param[in] message - Incoming message |
| 126 | * |
| 127 | * @return Command ID in the incoming message |
| 128 | */ |
| 129 | uint32_t getCommand(Message& message); |
| 130 | |
| 131 | /* |
| 132 | * @brief Calculate 8 bit 2's complement checksum |
| 133 | * |
| 134 | * Initialize checksum to 0. For each byte, checksum = (checksum + byte) |
| 135 | * modulo 256. Then checksum = - checksum. When the checksum and the |
| 136 | * bytes are added together, modulo 256, the result should be 0. |
| 137 | */ |
| 138 | uint8_t crc8bit(const uint8_t* ptr, const size_t len) |
| 139 | { |
| 140 | return (0x100 - std::accumulate(ptr,ptr+len,0)); |
| 141 | } |
| 142 | |
| 143 | }; |
| 144 | |
| 145 | } //namespace message |