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