Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 1 | #include "dbus_to_host_effecters.hpp" |
| 2 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 3 | #include <libpldm/pdr.h> |
| 4 | #include <libpldm/platform.h> |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 5 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 6 | #include <phosphor-logging/lg2.hpp> |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 7 | #include <xyz/openbmc_project/Common/error.hpp> |
Pavithra Barithaya | 7b4d59a | 2024-02-05 09:09:30 -0600 | [diff] [blame] | 8 | #include <xyz/openbmc_project/State/Boot/Progress/client.hpp> |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 9 | #include <xyz/openbmc_project/State/OperatingSystem/Status/server.hpp> |
| 10 | |
| 11 | #include <fstream> |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 12 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 13 | PHOSPHOR_LOG2_USING; |
| 14 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 15 | using namespace pldm::utils; |
| 16 | |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 17 | namespace pldm |
| 18 | { |
| 19 | namespace host_effecters |
| 20 | { |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 21 | using InternalFailure = |
| 22 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 23 | |
| 24 | constexpr auto hostEffecterJson = "dbus_to_host_effecter.json"; |
| 25 | |
| 26 | void HostEffecterParser::populatePropVals( |
| 27 | const Json& dBusValues, std::vector<PropertyValue>& propertyValues, |
| 28 | const std::string& propertyType) |
| 29 | |
| 30 | { |
| 31 | for (const auto& elem : dBusValues) |
| 32 | { |
| 33 | auto value = jsonEntryToDbusVal(propertyType, elem); |
| 34 | propertyValues.emplace_back(value); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | void HostEffecterParser::parseEffecterJson(const std::string& jsonPath) |
| 39 | { |
| 40 | fs::path jsonDir(jsonPath); |
| 41 | if (!fs::exists(jsonDir) || fs::is_empty(jsonDir)) |
| 42 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 43 | error("Host Effecter json path does not exist, DIR = {JSON_PATH}", |
| 44 | "JSON_PATH", jsonPath.c_str()); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 45 | return; |
| 46 | } |
| 47 | |
| 48 | fs::path jsonFilePath = jsonDir / hostEffecterJson; |
| 49 | if (!fs::exists(jsonFilePath)) |
| 50 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 51 | error("json does not exist, PATH = {JSON_PATH}", "JSON_PATH", |
| 52 | jsonFilePath.c_str()); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 53 | throw InternalFailure(); |
| 54 | } |
| 55 | |
| 56 | std::ifstream jsonFile(jsonFilePath); |
| 57 | auto data = Json::parse(jsonFile, nullptr, false); |
| 58 | if (data.is_discarded()) |
| 59 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 60 | error("Parsing json file failed, FILE = {JSON_PATH}", "JSON_PATH", |
| 61 | jsonFilePath.c_str()); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 62 | throw InternalFailure(); |
| 63 | } |
| 64 | const Json empty{}; |
| 65 | const std::vector<Json> emptyList{}; |
| 66 | |
| 67 | auto entries = data.value("entries", emptyList); |
| 68 | for (const auto& entry : entries) |
| 69 | { |
| 70 | EffecterInfo effecterInfo; |
| 71 | effecterInfo.mctpEid = entry.value("mctp_eid", 0xFF); |
| 72 | auto jsonEffecterInfo = entry.value("effecter_info", empty); |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 73 | auto effecterId = jsonEffecterInfo.value("effecterID", |
| 74 | PLDM_INVALID_EFFECTER_ID); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 75 | effecterInfo.containerId = jsonEffecterInfo.value("containerID", 0); |
| 76 | effecterInfo.entityType = jsonEffecterInfo.value("entityType", 0); |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 77 | effecterInfo.entityInstance = jsonEffecterInfo.value("entityInstance", |
| 78 | 0); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 79 | effecterInfo.compEffecterCnt = |
| 80 | jsonEffecterInfo.value("compositeEffecterCount", 0); |
| 81 | auto effecters = entry.value("effecters", emptyList); |
| 82 | for (const auto& effecter : effecters) |
| 83 | { |
| 84 | DBusEffecterMapping dbusInfo{}; |
| 85 | auto jsonDbusInfo = effecter.value("dbus_info", empty); |
| 86 | dbusInfo.dbusMap.objectPath = jsonDbusInfo.value("object_path", ""); |
| 87 | dbusInfo.dbusMap.interface = jsonDbusInfo.value("interface", ""); |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 88 | dbusInfo.dbusMap.propertyName = jsonDbusInfo.value("property_name", |
| 89 | ""); |
| 90 | dbusInfo.dbusMap.propertyType = jsonDbusInfo.value("property_type", |
| 91 | ""); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 92 | Json propertyValues = jsonDbusInfo["property_values"]; |
| 93 | |
| 94 | populatePropVals(propertyValues, dbusInfo.propertyValues, |
| 95 | dbusInfo.dbusMap.propertyType); |
| 96 | |
| 97 | const std::vector<uint8_t> emptyStates{}; |
| 98 | auto state = effecter.value("state", empty); |
| 99 | dbusInfo.state.stateSetId = state.value("id", 0); |
| 100 | auto states = state.value("state_values", emptyStates); |
| 101 | if (dbusInfo.propertyValues.size() != states.size()) |
| 102 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 103 | error( |
| 104 | "Number of states do not match with number of D-Bus property values in the json. Object path {OBJ_PATH} and property {PROP_NAME} will not be monitored", |
| 105 | "OBJ_PATH", dbusInfo.dbusMap.objectPath.c_str(), |
| 106 | "PROP_NAME", dbusInfo.dbusMap.propertyName); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 107 | continue; |
| 108 | } |
| 109 | for (const auto& s : states) |
| 110 | { |
| 111 | dbusInfo.state.states.emplace_back(s); |
| 112 | } |
| 113 | |
| 114 | auto effecterInfoIndex = hostEffecterInfo.size(); |
| 115 | auto dbusInfoIndex = effecterInfo.dbusInfo.size(); |
| 116 | createHostEffecterMatch( |
| 117 | dbusInfo.dbusMap.objectPath, dbusInfo.dbusMap.interface, |
| 118 | effecterInfoIndex, dbusInfoIndex, effecterId); |
| 119 | effecterInfo.dbusInfo.emplace_back(std::move(dbusInfo)); |
| 120 | } |
| 121 | hostEffecterInfo.emplace_back(std::move(effecterInfo)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void HostEffecterParser::processHostEffecterChangeNotification( |
| 126 | const DbusChgHostEffecterProps& chProperties, size_t effecterInfoIndex, |
| 127 | size_t dbusInfoIndex, uint16_t effecterId) |
| 128 | { |
Pavithra Barithaya | 7b4d59a | 2024-02-05 09:09:30 -0600 | [diff] [blame] | 129 | using BootProgress = |
| 130 | sdbusplus::client::xyz::openbmc_project::state::boot::Progress<>; |
| 131 | |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 132 | const auto& propertyName = hostEffecterInfo[effecterInfoIndex] |
| 133 | .dbusInfo[dbusInfoIndex] |
| 134 | .dbusMap.propertyName; |
| 135 | |
| 136 | const auto& it = chProperties.find(propertyName); |
| 137 | |
| 138 | if (it == chProperties.end()) |
| 139 | { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | if (effecterId == PLDM_INVALID_EFFECTER_ID) |
| 144 | { |
Sampa Misra | a4a9616 | 2020-07-14 05:33:46 -0500 | [diff] [blame] | 145 | constexpr auto localOrRemote = false; |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 146 | effecterId = findStateEffecterId( |
| 147 | pdrRepo, hostEffecterInfo[effecterInfoIndex].entityType, |
| 148 | hostEffecterInfo[effecterInfoIndex].entityInstance, |
| 149 | hostEffecterInfo[effecterInfoIndex].containerId, |
| 150 | hostEffecterInfo[effecterInfoIndex] |
| 151 | .dbusInfo[dbusInfoIndex] |
Sampa Misra | a4a9616 | 2020-07-14 05:33:46 -0500 | [diff] [blame] | 152 | .state.stateSetId, |
| 153 | localOrRemote); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 154 | if (effecterId == PLDM_INVALID_EFFECTER_ID) |
| 155 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 156 | error("Effecter id not found in pdr repo"); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | } |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 160 | constexpr auto hostStatePath = "/xyz/openbmc_project/state/host0"; |
| 161 | |
| 162 | try |
| 163 | { |
| 164 | auto propVal = dbusHandler->getDbusPropertyVariant( |
Pavithra Barithaya | 7b4d59a | 2024-02-05 09:09:30 -0600 | [diff] [blame] | 165 | hostStatePath, "BootProgress", BootProgress::interface); |
Patrick Williams | e4e2e82 | 2024-02-07 11:00:52 -0600 | [diff] [blame] | 166 | |
| 167 | using Stages = BootProgress::ProgressStages; |
| 168 | auto currHostState = sdbusplus::message::convert_from_string<Stages>( |
| 169 | std::get<std::string>(propVal)) |
| 170 | .value(); |
| 171 | |
| 172 | if (currHostState != Stages::SystemInitComplete && |
| 173 | currHostState != Stages::OSRunning && |
| 174 | currHostState != Stages::SystemSetup) |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 175 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 176 | info("Host is not up. Current host state: {CUR_HOST_STATE}", |
Patrick Williams | e4e2e82 | 2024-02-07 11:00:52 -0600 | [diff] [blame] | 177 | "CUR_HOST_STATE", currHostState); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | } |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 181 | catch (const sdbusplus::exception_t& e) |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 182 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 183 | error( |
| 184 | "Error in getting current host state. Will still continue to set the host effecter - {ERR_EXCEP}", |
| 185 | "ERR_EXCEP", e.what()); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 186 | } |
| 187 | uint8_t newState{}; |
| 188 | try |
| 189 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 190 | newState = findNewStateValue(effecterInfoIndex, dbusInfoIndex, |
| 191 | it->second); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 192 | } |
| 193 | catch (const std::out_of_range& e) |
| 194 | { |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 195 | error("New state not found in json: {ERROR}", "ERROR", e); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | |
| 199 | std::vector<set_effecter_state_field> stateField; |
| 200 | for (uint8_t i = 0; i < hostEffecterInfo[effecterInfoIndex].compEffecterCnt; |
| 201 | i++) |
| 202 | { |
| 203 | if (i == dbusInfoIndex) |
| 204 | { |
| 205 | stateField.push_back({PLDM_REQUEST_SET, newState}); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | stateField.push_back({PLDM_NO_CHANGE, 0}); |
| 210 | } |
| 211 | } |
| 212 | int rc{}; |
| 213 | try |
| 214 | { |
| 215 | rc = setHostStateEffecter(effecterInfoIndex, stateField, effecterId); |
| 216 | } |
| 217 | catch (const std::runtime_error& e) |
| 218 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 219 | error("Could not set host state effecter"); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | if (rc != PLDM_SUCCESS) |
| 223 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 224 | error("Could not set the host state effecter, rc= {RC}", "RC", rc); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | uint8_t |
| 229 | HostEffecterParser::findNewStateValue(size_t effecterInfoIndex, |
| 230 | size_t dbusInfoIndex, |
| 231 | const PropertyValue& propertyValue) |
| 232 | { |
| 233 | const auto& propValues = hostEffecterInfo[effecterInfoIndex] |
| 234 | .dbusInfo[dbusInfoIndex] |
| 235 | .propertyValues; |
| 236 | auto it = std::find(propValues.begin(), propValues.end(), propertyValue); |
| 237 | uint8_t newState{}; |
| 238 | if (it != propValues.end()) |
| 239 | { |
| 240 | auto index = std::distance(propValues.begin(), it); |
| 241 | newState = hostEffecterInfo[effecterInfoIndex] |
| 242 | .dbusInfo[dbusInfoIndex] |
| 243 | .state.states[index]; |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | throw std::out_of_range("new state not found in json"); |
| 248 | } |
| 249 | return newState; |
| 250 | } |
| 251 | |
| 252 | int HostEffecterParser::setHostStateEffecter( |
| 253 | size_t effecterInfoIndex, std::vector<set_effecter_state_field>& stateField, |
| 254 | uint16_t effecterId) |
| 255 | { |
| 256 | uint8_t& mctpEid = hostEffecterInfo[effecterInfoIndex].mctpEid; |
| 257 | uint8_t& compEffCnt = hostEffecterInfo[effecterInfoIndex].compEffecterCnt; |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 258 | auto instanceId = instanceIdDb->next(mctpEid); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 259 | |
| 260 | std::vector<uint8_t> requestMsg( |
| 261 | sizeof(pldm_msg_hdr) + sizeof(effecterId) + sizeof(compEffCnt) + |
| 262 | sizeof(set_effecter_state_field) * compEffCnt, |
| 263 | 0); |
| 264 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 265 | auto rc = encode_set_state_effecter_states_req( |
| 266 | instanceId, effecterId, compEffCnt, stateField.data(), request); |
| 267 | |
| 268 | if (rc != PLDM_SUCCESS) |
| 269 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 270 | error("Message encode failure. PLDM error code = {RC}", "RC", lg2::hex, |
| 271 | rc); |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 272 | instanceIdDb->free(mctpEid, instanceId); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 273 | return rc; |
| 274 | } |
| 275 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 276 | auto setStateEffecterStatesRespHandler = |
| 277 | [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 278 | if (response == nullptr || !respMsgLen) |
| 279 | { |
| 280 | error( |
| 281 | "Failed to receive response for setStateEffecterStates command"); |
| 282 | return; |
| 283 | } |
| 284 | uint8_t completionCode{}; |
| 285 | auto rc = decode_set_state_effecter_states_resp(response, respMsgLen, |
| 286 | &completionCode); |
| 287 | if (rc) |
| 288 | { |
| 289 | error("Failed to decode setStateEffecterStates response, rc {RC}", |
| 290 | "RC", rc); |
| 291 | pldm::utils::reportError( |
Manojkiran Eda | 92fb0b5 | 2024-04-17 10:48:17 +0530 | [diff] [blame^] | 292 | "xyz.openbmc_project.bmc.pldm.SetHostEffecterFailed"); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 293 | } |
| 294 | if (completionCode) |
| 295 | { |
| 296 | error("Failed to set a Host effecter, cc = {CC}", "CC", |
| 297 | static_cast<unsigned>(completionCode)); |
| 298 | pldm::utils::reportError( |
Manojkiran Eda | 92fb0b5 | 2024-04-17 10:48:17 +0530 | [diff] [blame^] | 299 | "xyz.openbmc_project.bmc.pldm.SetHostEffecterFailed"); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 300 | } |
| 301 | }; |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 302 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 303 | rc = handler->registerRequest( |
| 304 | mctpEid, instanceId, PLDM_PLATFORM, PLDM_SET_STATE_EFFECTER_STATES, |
| 305 | std::move(requestMsg), std::move(setStateEffecterStatesRespHandler)); |
| 306 | if (rc) |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 307 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 308 | error("Failed to send request to set an effecter on Host"); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 309 | } |
| 310 | return rc; |
| 311 | } |
| 312 | |
| 313 | void HostEffecterParser::createHostEffecterMatch(const std::string& objectPath, |
| 314 | const std::string& interface, |
| 315 | size_t effecterInfoIndex, |
| 316 | size_t dbusInfoIndex, |
| 317 | uint16_t effecterId) |
| 318 | { |
| 319 | using namespace sdbusplus::bus::match::rules; |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 320 | effecterInfoMatch.emplace_back(std::make_unique<sdbusplus::bus::match_t>( |
| 321 | pldm::utils::DBusHandler::getBus(), |
| 322 | propertiesChanged(objectPath, interface), |
| 323 | [this, effecterInfoIndex, dbusInfoIndex, |
| 324 | effecterId](sdbusplus::message_t& msg) { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 325 | DbusChgHostEffecterProps props; |
| 326 | std::string iface; |
| 327 | msg.read(iface, props); |
| 328 | processHostEffecterChangeNotification(props, effecterInfoIndex, |
| 329 | dbusInfoIndex, effecterId); |
Patrick Williams | a675662 | 2023-10-20 11:19:15 -0500 | [diff] [blame] | 330 | })); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | } // namespace host_effecters |
| 334 | } // namespace pldm |