1 
2 #include "platform.hpp"
3 
4 #include "utils.hpp"
5 
6 namespace pldm
7 {
8 namespace responder
9 {
10 namespace platform
11 {
12 
13 using namespace pldm::responder::effecter::dbus_mapping;
14 
15 Response Handler::getPDR(const pldm_msg* request, size_t payloadLength)
16 {
17     Response response(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES, 0);
18     auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
19 
20     if (payloadLength != PLDM_GET_PDR_REQ_BYTES)
21     {
22         return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
23     }
24 
25     uint32_t recordHandle{};
26     uint32_t dataTransferHandle{};
27     uint8_t transferOpFlag{};
28     uint16_t reqSizeBytes{};
29     uint16_t recordChangeNum{};
30 
31     auto rc = decode_get_pdr_req(request, payloadLength, &recordHandle,
32                                  &dataTransferHandle, &transferOpFlag,
33                                  &reqSizeBytes, &recordChangeNum);
34     if (rc != PLDM_SUCCESS)
35     {
36         return CmdHandler::ccOnlyResponse(request, rc);
37     }
38 
39     uint32_t nextRecordHandle{};
40     uint16_t respSizeBytes{};
41     uint8_t* recordData = nullptr;
42     try
43     {
44         pdr::Repo& pdrRepo = pdr::get(PDR_JSONS_DIR);
45         nextRecordHandle = pdrRepo.getNextRecordHandle(recordHandle);
46         pdr::Entry e;
47         if (reqSizeBytes)
48         {
49             e = pdrRepo.at(recordHandle);
50             respSizeBytes = e.size();
51             if (respSizeBytes > reqSizeBytes)
52             {
53                 respSizeBytes = reqSizeBytes;
54             }
55             recordData = e.data();
56         }
57         response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES +
58                             respSizeBytes,
59                         0);
60         responsePtr = reinterpret_cast<pldm_msg*>(response.data());
61         rc = encode_get_pdr_resp(request->hdr.instance_id, PLDM_SUCCESS,
62                                  nextRecordHandle, 0, PLDM_START, respSizeBytes,
63                                  recordData, 0, responsePtr);
64         if (rc != PLDM_SUCCESS)
65         {
66             return ccOnlyResponse(request, rc);
67         }
68     }
69     catch (const std::out_of_range& e)
70     {
71         return CmdHandler::ccOnlyResponse(request,
72                                           PLDM_PLATFORM_INVALID_RECORD_HANDLE);
73     }
74     catch (const std::exception& e)
75     {
76         std::cerr << "Error accessing PDR, HANDLE=" << recordHandle
77                   << " ERROR=" << e.what() << "\n";
78         return CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
79     }
80     return response;
81 }
82 
83 Response Handler::setStateEffecterStates(const pldm_msg* request,
84                                          size_t payloadLength)
85 {
86     Response response(
87         sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES, 0);
88     auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
89     uint16_t effecterId;
90     uint8_t compEffecterCnt;
91     constexpr auto maxCompositeEffecterCnt = 8;
92     std::vector<set_effecter_state_field> stateField(maxCompositeEffecterCnt,
93                                                      {0, 0});
94 
95     if ((payloadLength > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) ||
96         (payloadLength < sizeof(effecterId) + sizeof(compEffecterCnt) +
97                              sizeof(set_effecter_state_field)))
98     {
99         return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
100     }
101 
102     int rc = decode_set_state_effecter_states_req(request, payloadLength,
103                                                   &effecterId, &compEffecterCnt,
104                                                   stateField.data());
105 
106     if (rc != PLDM_SUCCESS)
107     {
108         return CmdHandler::ccOnlyResponse(request, rc);
109     }
110 
111     stateField.resize(compEffecterCnt);
112     const pldm::utils::DBusHandler dBusIntf;
113     rc = setStateEffecterStatesHandler<pldm::utils::DBusHandler>(
114         dBusIntf, effecterId, stateField);
115     if (rc != PLDM_SUCCESS)
116     {
117         return CmdHandler::ccOnlyResponse(request, rc);
118     }
119 
120     rc = encode_set_state_effecter_states_resp(request->hdr.instance_id, rc,
121                                                responsePtr);
122     if (rc != PLDM_SUCCESS)
123     {
124         return ccOnlyResponse(request, rc);
125     }
126 
127     return response;
128 }
129 
130 } // namespace platform
131 } // namespace responder
132 } // namespace pldm
133