Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 1 | #include "libpldm/base.h" |
| 2 | |
| 3 | #include "base.hpp" |
| 4 | |
| 5 | #include <array> |
| 6 | #include <map> |
| 7 | #include <stdexcept> |
| 8 | #include <vector> |
| 9 | |
| 10 | namespace pldm |
| 11 | { |
| 12 | namespace responder |
| 13 | { |
| 14 | |
| 15 | using Cmd = std::vector<uint8_t>; |
| 16 | |
| 17 | static const std::map<Type, Cmd> capabilities{ |
| 18 | {PLDM_BASE, {PLDM_GET_PLDM_TYPES, PLDM_GET_PLDM_COMMANDS}}}; |
| 19 | |
| 20 | void getPLDMTypes(const pldm_msg_payload* request, pldm_msg* response) |
| 21 | { |
| 22 | // DSP0240 has this as a bitfield8[N], where N = 0 to 7 |
| 23 | std::array<uint8_t, 8> types{}; |
| 24 | for (const auto& type : capabilities) |
| 25 | { |
| 26 | auto index = type.first / 8; |
| 27 | // <Type Number> = <Array Index> * 8 + <bit position> |
| 28 | auto bit = type.first - (index * 8); |
| 29 | types[index] |= 1 << bit; |
| 30 | } |
| 31 | |
| 32 | encode_get_types_resp(0, PLDM_SUCCESS, types.data(), response); |
| 33 | } |
| 34 | |
| 35 | void getPLDMCommands(const pldm_msg_payload* request, pldm_msg* response) |
| 36 | { |
| 37 | pldm_version version{}; |
| 38 | Type type; |
| 39 | |
| 40 | if (request->payload_length != (sizeof(version) + sizeof(type))) |
| 41 | { |
| 42 | encode_get_commands_resp(0, PLDM_ERROR_INVALID_LENGTH, nullptr, |
| 43 | response); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | decode_get_commands_req(request, &type, &version); |
| 48 | |
| 49 | // DSP0240 has this as a bitfield8[N], where N = 0 to 31 |
| 50 | std::array<uint8_t, 32> cmds{}; |
| 51 | if (capabilities.find(type) == capabilities.end()) |
| 52 | { |
| 53 | encode_get_commands_resp(0, PLDM_ERROR_INVALID_PLDM_TYPE, nullptr, |
| 54 | response); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | for (const auto& cmd : capabilities.at(type)) |
| 59 | { |
| 60 | auto index = cmd / 8; |
| 61 | // <Type Number> = <Array Index> * 8 + <bit position> |
| 62 | auto bit = cmd - (index * 8); |
| 63 | cmds[index] |= 1 << bit; |
| 64 | } |
| 65 | |
| 66 | encode_get_commands_resp(0, PLDM_SUCCESS, cmds.data(), response); |
| 67 | } |
| 68 | |
| 69 | } // namespace responder |
| 70 | } // namespace pldm |