blob: b742519d72abee996c373b70eace41fbda795f1a [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;
Delphine CC Chiud2e48992023-12-05 16:29:51 +080020using HandlerFunc = std::function<Response(
21 pldm_tid_t tid, const pldm_msg* request, size_t reqMsgLen)>;
Deepak Kodihallibc669f12019-11-28 08:52:07 -060022
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 *
Delphine CC Chiud2e48992023-12-05 16:29:51 +080030 * @param[in] tid - PLDM request TID
Deepak Kodihallibc669f12019-11-28 08:52:07 -060031 * @param[in] pldmCommand - PLDM command code
32 * @param[in] request - PLDM request message
33 * @param[in] reqMsgLen - PLDM request message size
34 * @return PLDM response message
35 */
Delphine CC Chiud2e48992023-12-05 16:29:51 +080036 Response handle(pldm_tid_t tid, Command pldmCommand,
37 const pldm_msg* request, size_t reqMsgLen)
Deepak Kodihallibc669f12019-11-28 08:52:07 -060038 {
Delphine CC Chiud2e48992023-12-05 16:29:51 +080039 return handlers.at(pldmCommand)(tid, request, reqMsgLen);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060040 }
41
John Wang7f02d702019-12-03 13:38:14 +080042 /** @brief Create a response message containing only cc
43 *
44 * @param[in] request - PLDM request message
45 * @param[in] cc - Completion Code
46 * @return PLDM response message
47 */
48 static Response ccOnlyResponse(const pldm_msg* request, uint8_t cc)
49 {
50 Response response(sizeof(pldm_msg), 0);
51 auto ptr = reinterpret_cast<pldm_msg*>(response.data());
Patrick Williams6da4f912023-05-10 07:50:53 -050052 auto rc = encode_cc_only_resp(request->hdr.instance_id,
53 request->hdr.type, request->hdr.command,
54 cc, ptr);
John Wang7f02d702019-12-03 13:38:14 +080055 assert(rc == PLDM_SUCCESS);
56 return response;
57 }
58
Deepak Kodihallibc669f12019-11-28 08:52:07 -060059 protected:
60 /** @brief map of PLDM command code to handler - to be populated by derived
61 * classes.
62 */
63 std::map<Command, HandlerFunc> handlers;
64};
65
66} // namespace responder
67} // namespace pldm