blob: 7fb7957464665032d80cf8b0a1eca31d09c6b6a0 [file] [log] [blame]
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05001#include "registration.hpp"
2
3#include <map>
4#include <phosphor-logging/log.hpp>
5
6using namespace phosphor::logging;
7
8namespace pldm
9{
10
11namespace responder
12{
13
14namespace internal
15{
16using Command = uint8_t;
17using CommandHandler = std::map<Command, Handler>;
18using Type = uint8_t;
19std::map<Type, CommandHandler> typeHandlers;
20} // namespace internal
21
22void 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
29Response 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