Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 3 | #include "message_handler.hpp" |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 4 | |
William A. Kennington III | 4f09eae | 2019-02-12 17:10:35 -0800 | [diff] [blame] | 5 | #include <ipmid/api.h> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 6 | |
Andrew Geissler | 9d9b763 | 2020-05-17 09:18:05 -0500 | [diff] [blame] | 7 | #include <cstddef> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 8 | #include <functional> |
| 9 | #include <map> |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 10 | |
| 11 | namespace command |
| 12 | { |
| 13 | |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 14 | struct CommandID |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 15 | { |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 16 | static constexpr size_t lunBits = 2; |
Patrick Williams | 099fb09 | 2023-05-10 07:50:31 -0500 | [diff] [blame] | 17 | CommandID(uint32_t command) : command(command) {} |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 18 | |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 19 | uint8_t netFnLun() const |
| 20 | { |
| 21 | return static_cast<uint8_t>(command >> CHAR_BIT); |
| 22 | } |
| 23 | uint8_t netFn() const |
| 24 | { |
| 25 | return netFnLun() >> lunBits; |
| 26 | } |
| 27 | uint8_t lun() const |
| 28 | { |
| 29 | return netFnLun() & ((1 << (lunBits + 1)) - 1); |
| 30 | } |
| 31 | uint8_t cmd() const |
| 32 | { |
| 33 | return static_cast<uint8_t>(command); |
| 34 | } |
| 35 | uint32_t command; |
| 36 | }; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 37 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 38 | /** |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 39 | * CommandFunctor is the functor register for commands defined in |
| 40 | * phosphor-net-ipmid. This would take the request part of the command as a |
| 41 | * vector and a reference to the message handler. The response part of the |
| 42 | * command is returned as a vector. |
| 43 | */ |
| 44 | using CommandFunctor = std::function<std::vector<uint8_t>( |
Vernon Mauery | 41ff9b5 | 2021-06-11 11:37:40 -0700 | [diff] [blame] | 45 | const std::vector<uint8_t>&, std::shared_ptr<message::Handler>&)>; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 46 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 47 | /** |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 48 | * @struct CmdDetails |
| 49 | * |
| 50 | * Command details is used to register commands supported in phosphor-net-ipmid. |
| 51 | */ |
| 52 | struct CmdDetails |
| 53 | { |
| 54 | CommandID command; |
| 55 | CommandFunctor functor; |
| 56 | session::Privilege privilege; |
| 57 | bool sessionless; |
| 58 | }; |
| 59 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 60 | /** |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 61 | * @enum NetFns |
| 62 | * |
| 63 | * A field that identifies the functional class of the message. The Network |
| 64 | * Function clusters IPMI commands into different sets. |
| 65 | */ |
| 66 | enum class NetFns |
| 67 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 68 | CHASSIS = (0x00 << 10), |
| 69 | CHASSIS_RESP = (0x01 << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 70 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 71 | BRIDGE = (0x02 << 10), |
| 72 | BRIDGE_RESP = (0x03 << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 73 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 74 | SENSOR = (0x04 << 10), |
| 75 | SENSOR_RESP = (0x05 << 10), |
| 76 | EVENT = (0x04 << 10), |
| 77 | EVENT_RESP = (0x05 << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 78 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 79 | APP = (0x06 << 10), |
| 80 | APP_RESP = (0x07 << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 81 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 82 | FIRMWARE = (0x08 << 10), |
| 83 | FIRMWARE_RESP = (0x09 << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 84 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 85 | STORAGE = (0x0A << 10), |
| 86 | STORAGE_RESP = (0x0B << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 87 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 88 | TRANSPORT = (0x0C << 10), |
| 89 | TRANSPORT_RESP = (0x0D << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 90 | |
| 91 | //>> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 92 | RESERVED_START = (0x0E << 10), |
| 93 | RESERVED_END = (0x2B << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 94 | //<< |
| 95 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 96 | GROUP_EXTN = (0x2C << 10), |
| 97 | GROUP_EXTN_RESP = (0x2D << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 98 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 99 | OEM = (0x2E << 10), |
| 100 | OEM_RESP = (0x2F << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 101 | }; |
| 102 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 103 | /** |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 104 | * @class Entry |
| 105 | * |
| 106 | * This is the base class for registering IPMI commands. There are two ways of |
| 107 | * registering commands to phosphor-net-ipmid, the session related commands and |
| 108 | * provider commands |
| 109 | * |
| 110 | * Every commands has a privilege level which mentions the minimum session |
| 111 | * privilege level needed to execute the command |
| 112 | */ |
| 113 | |
| 114 | class Entry |
| 115 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 116 | public: |
| 117 | Entry(CommandID command, session::Privilege privilege) : |
| 118 | command(command), privilege(privilege) |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 119 | {} |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 120 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 121 | /** |
| 122 | * @brief Execute the command |
| 123 | * |
| 124 | * Execute the command |
| 125 | * |
| 126 | * @param[in] commandData - Request Data for the command |
| 127 | * @param[in] handler - Reference to the Message Handler |
| 128 | * |
| 129 | * @return Response data for the command |
| 130 | */ |
| 131 | virtual std::vector<uint8_t> |
| 132 | executeCommand(std::vector<uint8_t>& commandData, |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 133 | std::shared_ptr<message::Handler> handler) = 0; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 134 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 135 | auto getCommand() const |
| 136 | { |
| 137 | return command; |
| 138 | } |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 139 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 140 | auto getPrivilege() const |
| 141 | { |
| 142 | return privilege; |
| 143 | } |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 144 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 145 | virtual ~Entry() = default; |
| 146 | Entry(const Entry&) = default; |
| 147 | Entry& operator=(const Entry&) = default; |
| 148 | Entry(Entry&&) = default; |
| 149 | Entry& operator=(Entry&&) = default; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 150 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 151 | protected: |
| 152 | CommandID command; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 153 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 154 | // Specifies the minimum privilege level required to execute this command |
| 155 | session::Privilege privilege; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 156 | }; |
| 157 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 158 | /** |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 159 | * @class NetIpmidEntry |
| 160 | * |
| 161 | * NetIpmidEntry is used to register commands that are consumed only in |
| 162 | * phosphor-net-ipmid. The RAKP commands, session commands and user management |
| 163 | * commands are examples of this. |
| 164 | * |
| 165 | * There are certain IPMI commands that can be executed before session can be |
| 166 | * established like Get System GUID, Get Channel Authentication Capabilities |
| 167 | * and RAKP commands. |
| 168 | */ |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 169 | class NetIpmidEntry final : public Entry |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 170 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 171 | public: |
| 172 | NetIpmidEntry(CommandID command, CommandFunctor functor, |
| 173 | session::Privilege privilege, bool sessionless) : |
Patrick Williams | 8425624 | 2024-08-16 15:20:21 -0400 | [diff] [blame] | 174 | Entry(command, privilege), functor(functor), sessionless(sessionless) |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 175 | {} |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 176 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 177 | /** |
| 178 | * @brief Execute the command |
| 179 | * |
| 180 | * Execute the command |
| 181 | * |
| 182 | * @param[in] commandData - Request Data for the command |
| 183 | * @param[in] handler - Reference to the Message Handler |
| 184 | * |
| 185 | * @return Response data for the command |
| 186 | */ |
| 187 | std::vector<uint8_t> |
| 188 | executeCommand(std::vector<uint8_t>& commandData, |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 189 | std::shared_ptr<message::Handler> handler) override; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 190 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 191 | virtual ~NetIpmidEntry() = default; |
| 192 | NetIpmidEntry(const NetIpmidEntry&) = default; |
| 193 | NetIpmidEntry& operator=(const NetIpmidEntry&) = default; |
| 194 | NetIpmidEntry(NetIpmidEntry&&) = default; |
| 195 | NetIpmidEntry& operator=(NetIpmidEntry&&) = default; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 196 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 197 | private: |
| 198 | CommandFunctor functor; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 199 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 200 | bool sessionless; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 201 | }; |
| 202 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 203 | /** |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 204 | * @class Table |
| 205 | * |
| 206 | * Table keeps the IPMI command entries as a sorted associative container with |
| 207 | * Command ID as the unique key. It has interfaces for registering commands |
| 208 | * and executing a command. |
| 209 | */ |
| 210 | class Table |
| 211 | { |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 212 | private: |
| 213 | struct Private |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 214 | {}; |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 215 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 216 | public: |
Patrick Williams | 099fb09 | 2023-05-10 07:50:31 -0500 | [diff] [blame] | 217 | explicit Table(const Private&) {} |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 218 | Table() = delete; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 219 | ~Table() = default; |
| 220 | // Command Table is a singleton so copy, copy-assignment, move and |
| 221 | // move assignment is deleted |
| 222 | Table(const Table&) = delete; |
| 223 | Table& operator=(const Table&) = delete; |
| 224 | Table(Table&&) = default; |
| 225 | Table& operator=(Table&&) = default; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 226 | |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 227 | /** |
| 228 | * @brief Get a reference to the singleton Table |
| 229 | * |
| 230 | * @return Table reference |
| 231 | */ |
| 232 | static Table& get() |
| 233 | { |
| 234 | static std::shared_ptr<Table> ptr = nullptr; |
| 235 | if (!ptr) |
| 236 | { |
| 237 | ptr = std::make_shared<Table>(Private()); |
| 238 | } |
| 239 | return *ptr; |
| 240 | } |
| 241 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 242 | using CommandTable = std::map<uint32_t, std::unique_ptr<Entry>>; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 243 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 244 | /** |
| 245 | * @brief Register a command |
| 246 | * |
| 247 | * Register a command with the command table |
| 248 | * |
| 249 | * @param[in] inCommand - Command ID |
| 250 | * @param[in] entry - Command Entry |
| 251 | * |
| 252 | * @return: None |
| 253 | * |
| 254 | * @note: Duplicate registrations will be rejected. |
| 255 | * |
| 256 | */ |
| 257 | void registerCommand(CommandID inCommand, std::unique_ptr<Entry>&& entry); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 258 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 259 | /** |
| 260 | * @brief Execute the command |
| 261 | * |
| 262 | * Execute the command for the corresponding CommandID |
| 263 | * |
| 264 | * @param[in] inCommand - Command ID to execute. |
| 265 | * @param[in] commandData - Request Data for the command |
| 266 | * @param[in] handler - Reference to the Message Handler |
| 267 | * |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 268 | */ |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 269 | void executeCommand(uint32_t inCommand, std::vector<uint8_t>& commandData, |
| 270 | std::shared_ptr<message::Handler> handler); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 271 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 272 | private: |
| 273 | CommandTable commandTable; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 274 | }; |
| 275 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 276 | } // namespace command |