blob: cfa95ebba24bc08099709c8255c07fe0d2509ff7 [file] [log] [blame]
Deepak Kodihallibc669f12019-11-28 08:52:07 -06001#pragma once
2
George Liuc453e162022-12-21 17:16:23 +08003#include <libpldm/base.h>
George Liu6492f522020-06-16 10:34:05 +08004
John Wang7f02d702019-12-03 13:38:14 +08005#include <cassert>
Deepak Kodihallibc669f12019-11-28 08:52:07 -06006#include <functional>
7#include <map>
John Wang7f02d702019-12-03 13:38:14 +08008#include <vector>
Deepak Kodihallibc669f12019-11-28 08:52:07 -06009
Deepak Kodihallibc669f12019-11-28 08:52:07 -060010namespace pldm
11{
12
13using Command = uint8_t;
14
15namespace responder
16{
17
18using Response = std::vector<uint8_t>;
19class CmdHandler;
20using HandlerFunc =
21 std::function<Response(const pldm_msg* request, size_t reqMsgLen)>;
22
23class CmdHandler
24{
25 public:
Joe Komlodi5e8aee72022-07-20 22:13:08 +000026 virtual ~CmdHandler() = default;
27
Deepak Kodihallibc669f12019-11-28 08:52:07 -060028 /** @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
John Wang7f02d702019-12-03 13:38:14 +080041 /** @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());
Patrick Williams6da4f912023-05-10 07:50:53 -050051 auto rc = encode_cc_only_resp(request->hdr.instance_id,
52 request->hdr.type, request->hdr.command,
53 cc, ptr);
John Wang7f02d702019-12-03 13:38:14 +080054 assert(rc == PLDM_SUCCESS);
55 return response;
56 }
57
Deepak Kodihallibc669f12019-11-28 08:52:07 -060058 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