Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 3 | #include "config.h" |
| 4 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 5 | #include "handler.hpp" |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 6 | #include "libpldmresponder/pdr.hpp" |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 7 | #include "libpldmresponder/pdr_utils.hpp" |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 8 | #include "utils.hpp" |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 9 | |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 10 | #include <stdint.h> |
| 11 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 12 | #include <map> |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 13 | |
| 14 | #include "libpldm/platform.h" |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 15 | #include "libpldm/states.h" |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 16 | |
| 17 | namespace pldm |
| 18 | { |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 19 | namespace responder |
| 20 | { |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 21 | namespace platform |
| 22 | { |
| 23 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 24 | using DbusPath = std::string; |
| 25 | using EffecterObjs = std::vector<DbusPath>; |
| 26 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 27 | class Handler : public CmdHandler |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 28 | { |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 29 | public: |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 30 | Handler(const std::string& dir, pldm_pdr* repo) : pdrRepo(repo) |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 31 | { |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 32 | generate(dir, pdrRepo); |
| 33 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 34 | handlers.emplace(PLDM_GET_PDR, |
| 35 | [this](const pldm_msg* request, size_t payloadLength) { |
| 36 | return this->getPDR(request, payloadLength); |
| 37 | }); |
| 38 | handlers.emplace(PLDM_SET_STATE_EFFECTER_STATES, |
| 39 | [this](const pldm_msg* request, size_t payloadLength) { |
| 40 | return this->setStateEffecterStates(request, |
| 41 | payloadLength); |
| 42 | }); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 45 | const EffecterObjs& getEffecterObjs(uint16_t effecterId) const |
| 46 | { |
| 47 | return effecterObjs.at(effecterId); |
| 48 | } |
| 49 | |
| 50 | void addEffecterObjs(uint16_t effecterId, EffecterObjs&& paths) |
| 51 | { |
| 52 | effecterObjs.emplace(effecterId, std::move(paths)); |
| 53 | } |
| 54 | |
| 55 | uint16_t getNextEffecterId() |
| 56 | { |
| 57 | return ++nextEffecterId; |
| 58 | } |
| 59 | |
| 60 | /** @brief Parse PDR JSONs and build PDR repository |
| 61 | * |
| 62 | * @param[in] dir - directory housing platform specific PDR JSON files |
| 63 | * @param[in] repo - instance of concrete implementation of Repo |
| 64 | */ |
| 65 | void generate(const std::string& dir, Repo& repo); |
| 66 | |
| 67 | /** @brief Parse PDR JSONs and build state effecter PDR repository |
| 68 | * |
| 69 | * @param[in] json - platform specific PDR JSON files |
| 70 | * @param[in] repo - instance of state effecter implementation of Repo |
| 71 | */ |
| 72 | void generateStateEffecterRepo(const Json& json, Repo& repo); |
| 73 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 74 | /** @brief Handler for GetPDR |
| 75 | * |
| 76 | * @param[in] request - Request message payload |
| 77 | * @param[in] payloadLength - Request payload length |
| 78 | * @param[out] Response - Response message written here |
| 79 | */ |
| 80 | Response getPDR(const pldm_msg* request, size_t payloadLength); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 81 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 82 | /** @brief Handler for setStateEffecterStates |
| 83 | * |
| 84 | * @param[in] request - Request message |
| 85 | * @param[in] payloadLength - Request payload length |
| 86 | * @return Response - PLDM Response message |
| 87 | */ |
| 88 | Response setStateEffecterStates(const pldm_msg* request, |
| 89 | size_t payloadLength); |
| 90 | |
| 91 | /** @brief Function to set the effecter requested by pldm requester |
| 92 | * @param[in] dBusIntf - The interface object |
| 93 | * @param[in] effecterId - Effecter ID sent by the requester to act on |
| 94 | * @param[in] stateField - The state field data for each of the states, |
| 95 | * equal to composite effecter count in number |
| 96 | * @return - Success or failure in setting the states. Returns failure in |
| 97 | * terms of PLDM completion codes if atleast one state fails to be set |
| 98 | */ |
| 99 | template <class DBusInterface> |
| 100 | int setStateEffecterStatesHandler( |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 101 | const DBusInterface& dBusIntf, uint16_t effecterId, |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 102 | const std::vector<set_effecter_state_field>& stateField) |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 103 | { |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 104 | using namespace pldm::responder::pdr; |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 105 | using namespace pldm::utils; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 106 | using namespace std::string_literals; |
| 107 | using DBusProperty = std::variant<std::string, bool>; |
| 108 | using StateSetId = uint16_t; |
| 109 | using StateSetNum = uint8_t; |
| 110 | using PropertyMap = |
| 111 | std::map<StateSetId, std::map<StateSetNum, DBusProperty>>; |
| 112 | static const PropertyMap stateNumToDbusProp = { |
| 113 | {PLDM_BOOT_PROGRESS_STATE, |
| 114 | {{PLDM_BOOT_NOT_ACTIVE, |
| 115 | "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus." |
| 116 | "Standby"s}, |
| 117 | {PLDM_BOOT_COMPLETED, |
| 118 | "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus." |
| 119 | "BootComplete"s}}}, |
| 120 | {PLDM_SYSTEM_POWER_STATE, |
| 121 | {{PLDM_OFF_SOFT_GRACEFUL, |
| 122 | "xyz.openbmc_project.State.Chassis.Transition.Off"s}}}}; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 123 | using namespace pldm::responder::pdr; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 124 | |
| 125 | state_effecter_possible_states* states = nullptr; |
| 126 | pldm_state_effecter_pdr* pdr = nullptr; |
| 127 | uint8_t compEffecterCnt = stateField.size(); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 128 | PdrEntry pdrEntry{}; |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 129 | auto pdrRecord = pdrRepo.getFirstRecord(pdrEntry); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 130 | while (pdrRecord) |
| 131 | { |
| 132 | pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data); |
| 133 | if (pdr->effecter_id != effecterId) |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 134 | { |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 135 | pdr = nullptr; |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 136 | pdrRecord = pdrRepo.getNextRecord(pdrRecord, pdrEntry); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 137 | continue; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 138 | } |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 139 | |
| 140 | states = reinterpret_cast<state_effecter_possible_states*>( |
| 141 | pdr->possible_states); |
| 142 | if (compEffecterCnt > pdr->composite_effecter_count) |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 143 | { |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 144 | std::cerr << "The requester sent wrong composite effecter" |
| 145 | << " count for the effecter, EFFECTER_ID=" |
| 146 | << effecterId << "COMP_EFF_CNT=" << compEffecterCnt |
| 147 | << "\n"; |
| 148 | return PLDM_ERROR_INVALID_DATA; |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 149 | } |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 150 | break; |
| 151 | } |
| 152 | |
| 153 | if (!pdr) |
| 154 | { |
| 155 | return PLDM_PLATFORM_INVALID_EFFECTER_ID; |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 156 | } |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 157 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 158 | std::map<StateSetId, std::function<int(const std::string& objPath, |
| 159 | const uint8_t currState)>> |
| 160 | effecterToDbusEntries = { |
| 161 | {PLDM_BOOT_PROGRESS_STATE, |
| 162 | [&](const std::string& objPath, const uint8_t currState) { |
| 163 | auto stateSet = |
| 164 | stateNumToDbusProp.find(PLDM_BOOT_PROGRESS_STATE); |
| 165 | if (stateSet == stateNumToDbusProp.end()) |
| 166 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 167 | std::cerr << "Couldn't find D-Bus mapping for " |
| 168 | << "PLDM_BOOT_PROGRESS_STATE, EFFECTER_ID=" |
| 169 | << effecterId << "\n"; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 170 | return PLDM_ERROR; |
| 171 | } |
| 172 | auto iter = stateSet->second.find( |
| 173 | stateField[currState].effecter_state); |
| 174 | if (iter == stateSet->second.end()) |
| 175 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 176 | std::cerr << "Invalid state field passed or field not " |
| 177 | << "found for PLDM_BOOT_PROGRESS_STATE, " |
| 178 | "EFFECTER_ID=" |
| 179 | << effecterId << " FIELD=" |
| 180 | << stateField[currState].effecter_state |
| 181 | << " OBJECT_PATH=" << objPath.c_str() |
| 182 | << "\n"; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 183 | return PLDM_ERROR_INVALID_DATA; |
| 184 | } |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 185 | PropertyValue value{std::get<std::string>(iter->second)}; |
| 186 | DBusMapping dbusMapping{ |
| 187 | objPath, |
| 188 | "xyz.openbmc_project.State.OperatingSystem.Status", |
| 189 | "OperatingSystemState", "string"}; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 190 | try |
| 191 | { |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 192 | dBusIntf.setDbusProperty(dbusMapping, value); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 193 | } |
| 194 | catch (const std::exception& e) |
| 195 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 196 | std::cerr |
| 197 | << "Error setting property, ERROR=" << e.what() |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 198 | << " PROPERTY=" << dbusMapping.propertyName |
| 199 | << " INTERFACE=" |
| 200 | << dbusMapping.interface << " PATH=" |
| 201 | << dbusMapping.objectPath << "\n"; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 202 | return PLDM_ERROR; |
| 203 | } |
| 204 | return PLDM_SUCCESS; |
| 205 | }}, |
| 206 | {PLDM_SYSTEM_POWER_STATE, |
| 207 | [&](const std::string& objPath, const uint8_t currState) { |
| 208 | auto stateSet = |
| 209 | stateNumToDbusProp.find(PLDM_SYSTEM_POWER_STATE); |
| 210 | if (stateSet == stateNumToDbusProp.end()) |
| 211 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 212 | std::cerr << "Couldn't find D-Bus mapping for " |
| 213 | << "PLDM_SYSTEM_POWER_STATE, EFFECTER_ID=" |
| 214 | << effecterId << "\n"; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 215 | return PLDM_ERROR; |
| 216 | } |
| 217 | auto iter = stateSet->second.find( |
| 218 | stateField[currState].effecter_state); |
| 219 | if (iter == stateSet->second.end()) |
| 220 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 221 | std::cerr << "Invalid state field passed or field not " |
| 222 | << "found for PLDM_SYSTEM_POWER_STATE, " |
| 223 | "EFFECTER_ID=" |
| 224 | << effecterId << " FIELD=" |
| 225 | << stateField[currState].effecter_state |
| 226 | << " OBJECT_PATH=" << objPath.c_str() |
| 227 | << "\n"; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 228 | return PLDM_ERROR_INVALID_DATA; |
| 229 | } |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 230 | PropertyValue value{std::get<std::string>(iter->second)}; |
| 231 | DBusMapping dbusMapping{ |
| 232 | objPath, "xyz.openbmc_project.State.Chassis", |
| 233 | "RequestedPowerTransition", "string"}; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 234 | try |
| 235 | { |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 236 | dBusIntf.setDbusProperty(dbusMapping, value); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 237 | } |
| 238 | catch (const std::exception& e) |
| 239 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 240 | std::cerr |
| 241 | << "Error setting property, ERROR=" << e.what() |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 242 | << " PROPERTY=" << dbusMapping.propertyName |
| 243 | << " INTERFACE=" |
| 244 | << dbusMapping.interface << " PATH=" |
| 245 | << dbusMapping.objectPath << "\n"; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 246 | return PLDM_ERROR; |
| 247 | } |
| 248 | return PLDM_SUCCESS; |
| 249 | }}}; |
| 250 | |
| 251 | int rc = PLDM_SUCCESS; |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 252 | const auto& paths = getEffecterObjs(effecterId); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 253 | for (uint8_t currState = 0; currState < compEffecterCnt; ++currState) |
| 254 | { |
| 255 | std::vector<StateSetNum> allowed{}; |
| 256 | // computation is based on table 79 from DSP0248 v1.1.1 |
| 257 | uint8_t bitfieldIndex = stateField[currState].effecter_state / 8; |
| 258 | uint8_t bit = |
| 259 | stateField[currState].effecter_state - (8 * bitfieldIndex); |
| 260 | if (states->possible_states_size < bitfieldIndex || |
| 261 | !(states->states[bitfieldIndex].byte & (1 << bit))) |
| 262 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 263 | std::cerr << "Invalid state set value, EFFECTER_ID=" |
| 264 | << effecterId |
| 265 | << " VALUE=" << stateField[currState].effecter_state |
| 266 | << " COMPOSITE_EFFECTER_ID=" << currState |
| 267 | << " DBUS_PATH=" << paths[currState].c_str() << "\n"; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 268 | rc = PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE; |
| 269 | break; |
| 270 | } |
| 271 | auto iter = effecterToDbusEntries.find(states->state_set_id); |
| 272 | if (iter == effecterToDbusEntries.end()) |
| 273 | { |
| 274 | uint16_t setId = states->state_set_id; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 275 | std::cerr << "Did not find the state set for the" |
| 276 | << " state effecter pdr, STATE=" << setId |
| 277 | << " EFFECTER_ID=" << effecterId << "\n"; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 278 | rc = PLDM_PLATFORM_INVALID_STATE_VALUE; |
| 279 | break; |
| 280 | } |
| 281 | if (stateField[currState].set_request == PLDM_REQUEST_SET) |
| 282 | { |
| 283 | rc = iter->second(paths[currState], currState); |
| 284 | if (rc != PLDM_SUCCESS) |
| 285 | { |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | uint8_t* nextState = |
| 290 | reinterpret_cast<uint8_t*>(states) + |
| 291 | sizeof(state_effecter_possible_states) - |
| 292 | sizeof(states->states) + |
| 293 | (states->possible_states_size * sizeof(states->states)); |
| 294 | states = |
| 295 | reinterpret_cast<state_effecter_possible_states*>(nextState); |
| 296 | } |
| 297 | return rc; |
| 298 | } |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 299 | |
| 300 | private: |
| 301 | pdr_utils::Repo pdrRepo; |
| 302 | uint16_t nextEffecterId{}; |
| 303 | std::map<uint16_t, EffecterObjs> effecterObjs{}; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | } // namespace platform |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 307 | } // namespace responder |
| 308 | } // namespace pldm |