Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 3 | #include <libpldm/base.h> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 4 | |
John Wang | 7f02d70 | 2019-12-03 13:38:14 +0800 | [diff] [blame] | 5 | #include <cassert> |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 6 | #include <functional> |
| 7 | #include <map> |
John Wang | 7f02d70 | 2019-12-03 13:38:14 +0800 | [diff] [blame] | 8 | #include <vector> |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 9 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 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; |
Delphine CC Chiu | d2e4899 | 2023-12-05 16:29:51 +0800 | [diff] [blame] | 20 | using HandlerFunc = std::function<Response( |
| 21 | pldm_tid_t tid, const pldm_msg* request, size_t reqMsgLen)>; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 22 | |
| 23 | class CmdHandler |
| 24 | { |
| 25 | public: |
Joe Komlodi | 5e8aee7 | 2022-07-20 22:13:08 +0000 | [diff] [blame] | 26 | virtual ~CmdHandler() = default; |
| 27 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 28 | /** @brief Invoke a PLDM command handler |
| 29 | * |
Delphine CC Chiu | d2e4899 | 2023-12-05 16:29:51 +0800 | [diff] [blame] | 30 | * @param[in] tid - PLDM request TID |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 31 | * @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 Chiu | d2e4899 | 2023-12-05 16:29:51 +0800 | [diff] [blame] | 36 | Response handle(pldm_tid_t tid, Command pldmCommand, |
| 37 | const pldm_msg* request, size_t reqMsgLen) |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 38 | { |
Delphine CC Chiu | d2e4899 | 2023-12-05 16:29:51 +0800 | [diff] [blame] | 39 | return handlers.at(pldmCommand)(tid, request, reqMsgLen); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 40 | } |
| 41 | |
John Wang | 7f02d70 | 2019-12-03 13:38:14 +0800 | [diff] [blame] | 42 | /** @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 Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 52 | auto rc = encode_cc_only_resp(request->hdr.instance_id, |
| 53 | request->hdr.type, request->hdr.command, |
| 54 | cc, ptr); |
John Wang | 7f02d70 | 2019-12-03 13:38:14 +0800 | [diff] [blame] | 55 | assert(rc == PLDM_SUCCESS); |
| 56 | return response; |
| 57 | } |
| 58 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 59 | 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 |