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