Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 1 | #include "registration.hpp" |
| 2 | |
| 3 | #include <map> |
| 4 | #include <phosphor-logging/log.hpp> |
| 5 | |
| 6 | using namespace phosphor::logging; |
| 7 | |
| 8 | namespace pldm |
| 9 | { |
| 10 | |
| 11 | namespace responder |
| 12 | { |
| 13 | |
| 14 | namespace internal |
| 15 | { |
| 16 | using Command = uint8_t; |
| 17 | using CommandHandler = std::map<Command, Handler>; |
| 18 | using Type = uint8_t; |
| 19 | std::map<Type, CommandHandler> typeHandlers; |
| 20 | } // namespace internal |
| 21 | |
| 22 | void registerHandler(uint8_t pldmType, uint8_t pldmCommand, Handler&& handler) |
| 23 | { |
| 24 | using namespace internal; |
| 25 | CommandHandler& ch = typeHandlers[pldmType]; |
| 26 | ch.emplace(pldmCommand, std::move(handler)); |
| 27 | } |
| 28 | |
| 29 | Response invokeHandler(uint8_t pldmType, uint8_t pldmCommand, |
| 30 | const pldm_msg* request, size_t payloadLength) |
| 31 | { |
| 32 | using namespace internal; |
| 33 | Response response; |
| 34 | if (!(typeHandlers.end() == typeHandlers.find(pldmType))) |
| 35 | { |
| 36 | if (!((typeHandlers.at(pldmType)).end() == |
| 37 | (typeHandlers.at(pldmType)).find(pldmCommand))) |
| 38 | { |
| 39 | response = typeHandlers.at(pldmType).at(pldmCommand)(request, |
| 40 | payloadLength); |
| 41 | if (response.empty()) |
| 42 | { |
| 43 | log<level::ERR>("Encountered invalid response"); |
| 44 | } |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | log<level::ERR>("Unsupported PLDM command", |
| 49 | entry("TYPE=0x%02x", pldmType), |
| 50 | entry("COMMAND=0x%02x", pldmCommand)); |
| 51 | } |
| 52 | } |
| 53 | else |
| 54 | { |
| 55 | log<level::ERR>("Unsupported PLDM TYPE", |
| 56 | entry("TYPE=0x%02x", pldmType)); |
| 57 | } |
| 58 | |
| 59 | return response; |
| 60 | } |
| 61 | |
| 62 | } // namespace responder |
| 63 | } // namespace pldm |