Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 1 | |
| 2 | #include "platform.hpp" |
| 3 | |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame^] | 4 | #include "pdr_state_effecter.hpp" |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 5 | #include "utils.hpp" |
| 6 | |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 7 | namespace pldm |
| 8 | { |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 9 | namespace responder |
| 10 | { |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 11 | namespace platform |
| 12 | { |
| 13 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 14 | using InternalFailure = |
| 15 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 16 | |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame] | 17 | static const Json empty{}; |
| 18 | |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame^] | 19 | void Handler::addDbusObjMaps( |
| 20 | uint16_t effecterId, |
| 21 | std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> dbusObj) |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame] | 22 | { |
| 23 | dbusObjMaps.emplace(effecterId, dbusObj); |
| 24 | } |
| 25 | |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame^] | 26 | const std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps>& |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame] | 27 | Handler::getDbusObjMaps(uint16_t effecterId) const |
| 28 | { |
| 29 | return dbusObjMaps.at(effecterId); |
| 30 | } |
| 31 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 32 | void Handler::generate(const std::string& dir, Repo& repo) |
| 33 | { |
| 34 | // A map of PDR type to a lambda that handles creation of that PDR type. |
| 35 | // The lambda essentially would parse the platform specific PDR JSONs to |
| 36 | // generate the PDR structures. This function iterates through the map to |
| 37 | // invoke all lambdas, so that all PDR types can be created. |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame^] | 38 | |
| 39 | const std::map<Type, generatePDR> generateHandlers = { |
| 40 | {PLDM_STATE_EFFECTER_PDR, |
| 41 | [this](const auto& json, RepoInterface& repo) { |
| 42 | pdr_state_effecter::generateStateEffecterPDR<Handler>(json, *this, |
| 43 | repo); |
| 44 | }}}; |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 45 | |
| 46 | Type pdrType{}; |
| 47 | for (const auto& dirEntry : fs::directory_iterator(dir)) |
| 48 | { |
| 49 | try |
| 50 | { |
| 51 | auto json = readJson(dirEntry.path().string()); |
| 52 | if (!json.empty()) |
| 53 | { |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame] | 54 | auto effecterPDRs = json.value("effecterPDRs", empty); |
| 55 | for (const auto& effecter : effecterPDRs) |
| 56 | { |
| 57 | pdrType = effecter.value("pdrType", 0); |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame^] | 58 | generateHandlers.at(pdrType)(effecter, repo); |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame] | 59 | } |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | catch (const InternalFailure& e) |
| 63 | { |
| 64 | std::cerr << "PDR config directory does not exist or empty, TYPE= " |
| 65 | << pdrType << "PATH= " << dirEntry |
| 66 | << " ERROR=" << e.what() << "\n"; |
| 67 | } |
| 68 | catch (const Json::exception& e) |
| 69 | { |
| 70 | std::cerr << "Failed parsing PDR JSON file, TYPE= " << pdrType |
| 71 | << " ERROR=" << e.what() << "\n"; |
| 72 | pldm::utils::reportError( |
| 73 | "xyz.openbmc_project.bmc.pldm.InternalFailure"); |
| 74 | } |
| 75 | catch (const std::exception& e) |
| 76 | { |
| 77 | std::cerr << "Failed parsing PDR JSON file, TYPE= " << pdrType |
| 78 | << " ERROR=" << e.what() << "\n"; |
| 79 | pldm::utils::reportError( |
| 80 | "xyz.openbmc_project.bmc.pldm.InternalFailure"); |
| 81 | } |
| 82 | } |
| 83 | } |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 84 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 85 | Response Handler::getPDR(const pldm_msg* request, size_t payloadLength) |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 86 | { |
| 87 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES, 0); |
| 88 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 89 | |
| 90 | if (payloadLength != PLDM_GET_PDR_REQ_BYTES) |
| 91 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 92 | return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | uint32_t recordHandle{}; |
| 96 | uint32_t dataTransferHandle{}; |
| 97 | uint8_t transferOpFlag{}; |
| 98 | uint16_t reqSizeBytes{}; |
| 99 | uint16_t recordChangeNum{}; |
| 100 | |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 101 | auto rc = decode_get_pdr_req(request, payloadLength, &recordHandle, |
| 102 | &dataTransferHandle, &transferOpFlag, |
| 103 | &reqSizeBytes, &recordChangeNum); |
| 104 | if (rc != PLDM_SUCCESS) |
| 105 | { |
| 106 | return CmdHandler::ccOnlyResponse(request, rc); |
| 107 | } |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 108 | |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 109 | uint16_t respSizeBytes{}; |
| 110 | uint8_t* recordData = nullptr; |
| 111 | try |
| 112 | { |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 113 | pdr_utils::PdrEntry e; |
| 114 | auto record = pdr::getRecordByHandle(pdrRepo, recordHandle, e); |
| 115 | if (record == NULL) |
| 116 | { |
| 117 | return CmdHandler::ccOnlyResponse( |
| 118 | request, PLDM_PLATFORM_INVALID_RECORD_HANDLE); |
| 119 | } |
| 120 | |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 121 | if (reqSizeBytes) |
| 122 | { |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 123 | respSizeBytes = e.size; |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 124 | if (respSizeBytes > reqSizeBytes) |
| 125 | { |
| 126 | respSizeBytes = reqSizeBytes; |
| 127 | } |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 128 | recordData = e.data; |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 129 | } |
| 130 | response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES + |
| 131 | respSizeBytes, |
| 132 | 0); |
| 133 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Deepak Kodihalli | 22b5a7d | 2020-03-17 23:28:41 -0500 | [diff] [blame] | 134 | rc = encode_get_pdr_resp( |
| 135 | request->hdr.instance_id, PLDM_SUCCESS, e.handle.nextRecordHandle, |
| 136 | 0, PLDM_START_AND_END, respSizeBytes, recordData, 0, responsePtr); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 137 | if (rc != PLDM_SUCCESS) |
| 138 | { |
| 139 | return ccOnlyResponse(request, rc); |
| 140 | } |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 141 | } |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 142 | catch (const std::exception& e) |
| 143 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 144 | std::cerr << "Error accessing PDR, HANDLE=" << recordHandle |
| 145 | << " ERROR=" << e.what() << "\n"; |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 146 | return CmdHandler::ccOnlyResponse(request, PLDM_ERROR); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 147 | } |
| 148 | return response; |
| 149 | } |
| 150 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 151 | Response Handler::setStateEffecterStates(const pldm_msg* request, |
| 152 | size_t payloadLength) |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 153 | { |
| 154 | Response response( |
| 155 | sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES, 0); |
| 156 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 157 | uint16_t effecterId; |
| 158 | uint8_t compEffecterCnt; |
| 159 | constexpr auto maxCompositeEffecterCnt = 8; |
| 160 | std::vector<set_effecter_state_field> stateField(maxCompositeEffecterCnt, |
| 161 | {0, 0}); |
| 162 | |
| 163 | if ((payloadLength > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) || |
| 164 | (payloadLength < sizeof(effecterId) + sizeof(compEffecterCnt) + |
| 165 | sizeof(set_effecter_state_field))) |
| 166 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 167 | return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | int rc = decode_set_state_effecter_states_req(request, payloadLength, |
| 171 | &effecterId, &compEffecterCnt, |
| 172 | stateField.data()); |
| 173 | |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 174 | if (rc != PLDM_SUCCESS) |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 175 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 176 | return CmdHandler::ccOnlyResponse(request, rc); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 177 | } |
| 178 | |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 179 | stateField.resize(compEffecterCnt); |
| 180 | const pldm::utils::DBusHandler dBusIntf; |
| 181 | rc = setStateEffecterStatesHandler<pldm::utils::DBusHandler>( |
| 182 | dBusIntf, effecterId, stateField); |
| 183 | if (rc != PLDM_SUCCESS) |
| 184 | { |
| 185 | return CmdHandler::ccOnlyResponse(request, rc); |
| 186 | } |
| 187 | |
| 188 | rc = encode_set_state_effecter_states_resp(request->hdr.instance_id, rc, |
| 189 | responsePtr); |
| 190 | if (rc != PLDM_SUCCESS) |
| 191 | { |
| 192 | return ccOnlyResponse(request, rc); |
| 193 | } |
| 194 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 195 | return response; |
| 196 | } |
| 197 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 198 | } // namespace platform |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 199 | } // namespace responder |
| 200 | } // namespace pldm |