blob: f3bb8f44038820d393102f2c1c77c63bf6e69da3 [file] [log] [blame]
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06001#include "libpldm/base.h"
2
3#include "base.hpp"
4
5#include <array>
6#include <map>
7#include <stdexcept>
8#include <vector>
9
10namespace pldm
11{
12namespace responder
13{
14
15using Cmd = std::vector<uint8_t>;
16
17static const std::map<Type, Cmd> capabilities{
18 {PLDM_BASE, {PLDM_GET_PLDM_TYPES, PLDM_GET_PLDM_COMMANDS}}};
19
20void 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
35void 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