xref: /openbmc/pldm/pldmd/handler.hpp (revision 27a022ca)
1 #pragma once
2 
3 #include "libpldm/base.h"
4 
5 #include <cassert>
6 #include <functional>
7 #include <map>
8 #include <vector>
9 
10 namespace pldm
11 {
12 
13 using Command = uint8_t;
14 
15 namespace responder
16 {
17 
18 using Response = std::vector<uint8_t>;
19 class CmdHandler;
20 using HandlerFunc =
21     std::function<Response(const pldm_msg* request, size_t reqMsgLen)>;
22 
23 class CmdHandler
24 {
25   public:
26     virtual ~CmdHandler() = default;
27 
28     /** @brief Invoke a PLDM command handler
29      *
30      *  @param[in] pldmCommand - PLDM command code
31      *  @param[in] request - PLDM request message
32      *  @param[in] reqMsgLen - PLDM request message size
33      *  @return PLDM response message
34      */
35     Response handle(Command pldmCommand, const pldm_msg* request,
36                     size_t reqMsgLen)
37     {
38         return handlers.at(pldmCommand)(request, reqMsgLen);
39     }
40 
41     /** @brief Create a response message containing only cc
42      *
43      *  @param[in] request - PLDM request message
44      *  @param[in] cc - Completion Code
45      *  @return PLDM response message
46      */
47     static Response ccOnlyResponse(const pldm_msg* request, uint8_t cc)
48     {
49         Response response(sizeof(pldm_msg), 0);
50         auto ptr = reinterpret_cast<pldm_msg*>(response.data());
51         auto rc =
52             encode_cc_only_resp(request->hdr.instance_id, request->hdr.type,
53                                 request->hdr.command, cc, ptr);
54         assert(rc == PLDM_SUCCESS);
55         return response;
56     }
57 
58   protected:
59     /** @brief map of PLDM command code to handler - to be populated by derived
60      *         classes.
61      */
62     std::map<Command, HandlerFunc> handlers;
63 };
64 
65 } // namespace responder
66 } // namespace pldm
67