Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
William A. Kennington III | 194375f | 2018-12-14 02:14:33 -0800 | [diff] [blame] | 3 | #include <ipmid/api.h> |
Patrick Venture | dd0459e | 2018-09-08 09:27:16 -0700 | [diff] [blame] | 4 | |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 5 | #include <ipmid/iana.hpp> |
| 6 | |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 7 | #include <array> |
Patrick Venture | 082a39c | 2020-05-07 17:26:47 -0700 | [diff] [blame] | 8 | #include <cstddef> |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 9 | #include <cstdint> |
| 10 | #include <functional> |
| 11 | #include <vector> |
| 12 | |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 13 | namespace oem |
| 14 | { |
Patrick Venture | 082a39c | 2020-05-07 17:26:47 -0700 | [diff] [blame] | 15 | constexpr std::size_t groupMagicSize = 3; |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 16 | |
Patrick Venture | 082a39c | 2020-05-07 17:26:47 -0700 | [diff] [blame] | 17 | using Group = std::array<std::uint8_t, groupMagicSize>; |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 18 | |
| 19 | // Handler signature includes ipmi cmd to support wildcard cmd match. |
| 20 | // Buffers and lengths exclude the OemGroup bytes in the IPMI message. |
| 21 | // dataLen supplies length of reqBuf upon call, and should be set to the |
| 22 | // length of replyBuf upon return - conventional in this code base. |
Patrick Venture | 082a39c | 2020-05-07 17:26:47 -0700 | [diff] [blame] | 23 | using Handler = std::function<ipmi_ret_t(ipmi_cmd_t, // cmd byte |
| 24 | const std::uint8_t*, // reqBuf |
| 25 | std::uint8_t*, // replyBuf |
| 26 | std::size_t*)>; // dataLen |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 27 | |
| 28 | /// Router Interface class. |
| 29 | /// @brief Abstract Router Interface |
| 30 | class Router |
| 31 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 32 | public: |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 33 | virtual ~Router() {} |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 34 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 35 | /// Enable message routing to begin. |
| 36 | virtual void activate() = 0; |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 37 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 38 | /// Register a handler for given OEMNumber & cmd. |
| 39 | /// Use IPMI_CMD_WILDCARD to catch any unregistered cmd |
| 40 | /// for the given OEMNumber. |
| 41 | /// |
| 42 | /// @param[in] oen - the OEM Number. |
| 43 | /// @param[in] cmd - the Command. |
| 44 | /// @param[in] handler - the handler to call given that OEN and |
| 45 | /// command. |
| 46 | virtual void registerHandler(Number oen, ipmi_cmd_t cmd, |
| 47 | Handler handler) = 0; |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | /// Expose mutable Router for configuration & activation. |
| 51 | /// |
| 52 | /// @returns pointer to OEM Router to use. |
| 53 | Router* mutableRouter(); |
| 54 | |
| 55 | /// Convert a group to an OEN. |
| 56 | /// |
| 57 | /// @param[in] oeg - request buffer for IPMI command. |
| 58 | /// @return the OEN. |
Patrick Venture | 082a39c | 2020-05-07 17:26:47 -0700 | [diff] [blame] | 59 | constexpr Number toOemNumber(const std::uint8_t oeg[groupMagicSize]) |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 60 | { |
| 61 | return (oeg[2] << 16) | (oeg[1] << 8) | oeg[0]; |
| 62 | } |
| 63 | |
| 64 | /// Given a Group convert to an OEN. |
| 65 | /// |
| 66 | /// @param[in] oeg - OEM Group reference. |
| 67 | /// @return the OEN. |
| 68 | constexpr Number toOemNumber(const Group& oeg) |
| 69 | { |
| 70 | return (oeg[2] << 16) | (oeg[1] << 8) | oeg[0]; |
| 71 | } |
| 72 | |
| 73 | /// Given an OEN, conver to the OEM Group. |
| 74 | /// |
| 75 | /// @param[in] oen - the OEM Number. |
| 76 | /// @return the OEM Group. |
| 77 | constexpr Group toOemGroup(Number oen) |
| 78 | { |
Patrick Venture | 082a39c | 2020-05-07 17:26:47 -0700 | [diff] [blame] | 79 | return Group{static_cast<std::uint8_t>(oen), |
| 80 | static_cast<std::uint8_t>(oen >> 8), |
| 81 | static_cast<std::uint8_t>(oen >> 16)}; |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 84 | } // namespace oem |