Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 3 | #include "libpldm/base.h" |
| 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; |
| 20 | using HandlerFunc = |
| 21 | std::function<Response(const pldm_msg* request, size_t reqMsgLen)>; |
| 22 | |
| 23 | class CmdHandler |
| 24 | { |
| 25 | public: |
| 26 | /** @brief Invoke a PLDM command handler |
| 27 | * |
| 28 | * @param[in] pldmCommand - PLDM command code |
| 29 | * @param[in] request - PLDM request message |
| 30 | * @param[in] reqMsgLen - PLDM request message size |
| 31 | * @return PLDM response message |
| 32 | */ |
| 33 | Response handle(Command pldmCommand, const pldm_msg* request, |
| 34 | size_t reqMsgLen) |
| 35 | { |
| 36 | return handlers.at(pldmCommand)(request, reqMsgLen); |
| 37 | } |
| 38 | |
John Wang | 7f02d70 | 2019-12-03 13:38:14 +0800 | [diff] [blame] | 39 | /** @brief Create a response message containing only cc |
| 40 | * |
| 41 | * @param[in] request - PLDM request message |
| 42 | * @param[in] cc - Completion Code |
| 43 | * @return PLDM response message |
| 44 | */ |
| 45 | static Response ccOnlyResponse(const pldm_msg* request, uint8_t cc) |
| 46 | { |
| 47 | Response response(sizeof(pldm_msg), 0); |
| 48 | auto ptr = reinterpret_cast<pldm_msg*>(response.data()); |
| 49 | auto rc = |
| 50 | encode_cc_only_resp(request->hdr.instance_id, request->hdr.type, |
| 51 | request->hdr.command, cc, ptr); |
| 52 | assert(rc == PLDM_SUCCESS); |
| 53 | return response; |
| 54 | } |
| 55 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 56 | protected: |
| 57 | /** @brief map of PLDM command code to handler - to be populated by derived |
| 58 | * classes. |
| 59 | */ |
| 60 | std::map<Command, HandlerFunc> handlers; |
| 61 | }; |
| 62 | |
| 63 | } // namespace responder |
| 64 | } // namespace pldm |