George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 3 | #include "common/utils.hpp" |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 4 | #include "libpldmresponder/pdr.hpp" |
| 5 | #include "pdr_utils.hpp" |
Deepak Kodihalli | 1521f6d | 2020-06-16 08:51:02 -0500 | [diff] [blame] | 6 | #include "pldmd/handler.hpp" |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 7 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 8 | #include <libpldm/platform.h> |
| 9 | #include <libpldm/states.h> |
| 10 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 11 | #include <phosphor-logging/lg2.hpp> |
| 12 | |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 13 | #include <cstdint> |
| 14 | #include <map> |
| 15 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 16 | PHOSPHOR_LOG2_USING; |
| 17 | |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 18 | namespace pldm |
| 19 | { |
| 20 | namespace responder |
| 21 | { |
| 22 | namespace platform_state_effecter |
| 23 | { |
| 24 | /** @brief Function to set the effecter requested by pldm requester |
| 25 | * |
| 26 | * @tparam[in] DBusInterface - DBus interface type |
| 27 | * @tparam[in] Handler - pldm::responder::platform::Handler |
| 28 | * @param[in] dBusIntf - The interface object of DBusInterface |
| 29 | * @param[in] handler - The interface object of |
| 30 | * pldm::responder::platform::Handler |
| 31 | * @param[in] effecterId - Effecter ID sent by the requester to act on |
| 32 | * @param[in] stateField - The state field data for each of the states, |
| 33 | * equal to composite effecter count in number |
| 34 | * @return - Success or failure in setting the states. Returns failure in |
Manojkiran Eda | 2576aec | 2024-06-17 12:05:17 +0530 | [diff] [blame] | 35 | * terms of PLDM completion codes if at least one state fails to be set |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 36 | */ |
| 37 | template <class DBusInterface, class Handler> |
| 38 | int setStateEffecterStatesHandler( |
| 39 | const DBusInterface& dBusIntf, Handler& handler, uint16_t effecterId, |
| 40 | const std::vector<set_effecter_state_field>& stateField) |
| 41 | { |
| 42 | using namespace pldm::responder::pdr; |
| 43 | using namespace pldm::utils; |
| 44 | using StateSetNum = uint8_t; |
| 45 | |
| 46 | state_effecter_possible_states* states = nullptr; |
| 47 | pldm_state_effecter_pdr* pdr = nullptr; |
| 48 | uint8_t compEffecterCnt = stateField.size(); |
| 49 | |
| 50 | std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateEffecterPdrRepo( |
| 51 | pldm_pdr_init(), pldm_pdr_destroy); |
Andrew Jeffery | acb2029 | 2023-06-30 11:47:44 +0930 | [diff] [blame] | 52 | if (!stateEffecterPdrRepo) |
| 53 | { |
| 54 | throw std::runtime_error( |
| 55 | "Failed to instantiate state effecter PDR repository"); |
| 56 | } |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 57 | pldm::responder::pdr_utils::Repo stateEffecterPDRs( |
| 58 | stateEffecterPdrRepo.get()); |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 59 | getRepoByType(handler.getRepo(), stateEffecterPDRs, |
| 60 | PLDM_STATE_EFFECTER_PDR); |
| 61 | if (stateEffecterPDRs.empty()) |
| 62 | { |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 63 | error("Failed to get StateEffecterPDR record"); |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 64 | return PLDM_PLATFORM_INVALID_EFFECTER_ID; |
| 65 | } |
| 66 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 67 | pldm::responder::pdr_utils::PdrEntry pdrEntry{}; |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 68 | auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry); |
| 69 | while (pdrRecord) |
| 70 | { |
| 71 | pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data); |
| 72 | if (pdr->effecter_id != effecterId) |
| 73 | { |
| 74 | pdr = nullptr; |
| 75 | pdrRecord = stateEffecterPDRs.getNextRecord(pdrRecord, pdrEntry); |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | states = reinterpret_cast<state_effecter_possible_states*>( |
| 80 | pdr->possible_states); |
| 81 | if (compEffecterCnt > pdr->composite_effecter_count) |
| 82 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 83 | error( |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 84 | "The requester sent wrong composite effecter count '{COMPOSITE_EFFECTER_COUNT}' for the effecter ID '{EFFECTERID}'", |
| 85 | "EFFECTERID", effecterId, "COMPOSITE_EFFECTER_COUNT", |
| 86 | compEffecterCnt); |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 87 | return PLDM_ERROR_INVALID_DATA; |
| 88 | } |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | if (!pdr) |
| 93 | { |
| 94 | return PLDM_PLATFORM_INVALID_EFFECTER_ID; |
| 95 | } |
| 96 | |
| 97 | int rc = PLDM_SUCCESS; |
| 98 | try |
| 99 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 100 | const auto& [dbusMappings, |
| 101 | dbusValMaps] = handler.getDbusObjMaps(effecterId); |
Manojkiran Eda | 3daf7a1 | 2024-04-17 20:33:44 +0530 | [diff] [blame] | 102 | if (dbusMappings.empty() || dbusValMaps.empty()) |
| 103 | { |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 104 | error("DbusMappings for effecter ID '{EFFECTER_ID}' is missing", |
Manojkiran Eda | 3daf7a1 | 2024-04-17 20:33:44 +0530 | [diff] [blame] | 105 | "EFFECTER_ID", effecterId); |
| 106 | return PLDM_ERROR; |
| 107 | } |
| 108 | |
| 109 | for (uint8_t currState = 0; |
| 110 | currState < compEffecterCnt && currState < dbusMappings.size() && |
| 111 | currState < dbusValMaps.size(); |
| 112 | ++currState) |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 113 | { |
| 114 | std::vector<StateSetNum> allowed{}; |
| 115 | // computation is based on table 79 from DSP0248 v1.1.1 |
| 116 | uint8_t bitfieldIndex = stateField[currState].effecter_state / 8; |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 117 | uint8_t bit = stateField[currState].effecter_state - |
| 118 | (8 * bitfieldIndex); |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 119 | if (states->possible_states_size < bitfieldIndex || |
| 120 | !(states->states[bitfieldIndex].byte & (1 << bit))) |
| 121 | { |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 122 | rc = PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE; |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 123 | error( |
| 124 | "Invalid state set value for effecter ID '{EFFECTER_ID}', effecter state '{EFFECTER_STATE}', composite effecter ID '{COMPOSITE_EFFECTER_ID}' and path '{PATH}', response code '{RC}'", |
| 125 | "EFFECTER_ID", effecterId, "EFFECTER_STATE", |
| 126 | stateField[currState].effecter_state, |
| 127 | "COMPOSITE_EFFECTER_ID", currState, "PATH", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 128 | dbusMappings[currState].objectPath, "RC", rc); |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 129 | break; |
| 130 | } |
| 131 | const DBusMapping& dbusMapping = dbusMappings[currState]; |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 132 | const pldm::responder::pdr_utils::StatestoDbusVal& dbusValToMap = |
| 133 | dbusValMaps[currState]; |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 134 | |
| 135 | if (stateField[currState].set_request == PLDM_REQUEST_SET) |
| 136 | { |
| 137 | try |
| 138 | { |
| 139 | dBusIntf.setDbusProperty( |
| 140 | dbusMapping, |
| 141 | dbusValToMap.at(stateField[currState].effecter_state)); |
| 142 | } |
| 143 | catch (const std::exception& e) |
| 144 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 145 | error( |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 146 | "Failed to set property '{PROPERTY}', interface '{INTERFACE}' and path '{PATH}', error - '{ERROR}'", |
| 147 | "PROPERTY", dbusMapping.propertyName, "INTERFACE", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 148 | dbusMapping.interface, "PATH", dbusMapping.objectPath, |
| 149 | "ERROR", e); |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 150 | return PLDM_ERROR; |
| 151 | } |
| 152 | } |
| 153 | uint8_t* nextState = |
| 154 | reinterpret_cast<uint8_t*>(states) + |
| 155 | sizeof(state_effecter_possible_states) - |
| 156 | sizeof(states->states) + |
| 157 | (states->possible_states_size * sizeof(states->states)); |
| 158 | states = |
| 159 | reinterpret_cast<state_effecter_possible_states*>(nextState); |
| 160 | } |
| 161 | } |
| 162 | catch (const std::out_of_range& e) |
| 163 | { |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 164 | error("Unknown effecter ID '{EFFECTERID}', error - {ERROR}", |
| 165 | "EFFECTERID", effecterId, "ERROR", e); |
George Liu | bd5e2ea | 2021-04-22 20:33:06 +0800 | [diff] [blame] | 166 | return PLDM_ERROR; |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | return rc; |
| 170 | } |
| 171 | |
| 172 | } // namespace platform_state_effecter |
| 173 | } // namespace responder |
| 174 | } // namespace pldm |