George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Sampa Misra | 12afe11 | 2020-05-25 11:40:44 -0500 | [diff] [blame] | 3 | #include "pdr.hpp" |
| 4 | #include "pdr_utils.hpp" |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 5 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 6 | #include <libpldm/platform.h> |
Manojkiran Eda | cc5f158 | 2021-09-29 17:03:06 +0530 | [diff] [blame] | 7 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 8 | #include <phosphor-logging/lg2.hpp> |
| 9 | |
| 10 | PHOSPHOR_LOG2_USING; |
| 11 | |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 12 | namespace pldm |
| 13 | { |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 14 | namespace responder |
| 15 | { |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 16 | namespace pdr_state_effecter |
| 17 | { |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 18 | using Json = nlohmann::json; |
| 19 | |
| 20 | static const Json empty{}; |
| 21 | |
| 22 | /** @brief Parse PDR JSON file and generate state effecter PDR structure |
| 23 | * |
| 24 | * @param[in] json - the JSON Object with the state effecter PDR |
| 25 | * @param[out] handler - the Parser of PLDM command handler |
| 26 | * @param[out] repo - pdr::RepoInterface |
| 27 | * |
| 28 | */ |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 29 | template <class DBusInterface, class Handler> |
| 30 | void generateStateEffecterPDR(const DBusInterface& dBusIntf, const Json& json, |
| 31 | Handler& handler, pdr_utils::RepoInterface& repo) |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 32 | { |
| 33 | static const std::vector<Json> emptyList{}; |
| 34 | auto entries = json.value("entries", emptyList); |
| 35 | for (const auto& e : entries) |
| 36 | { |
| 37 | size_t pdrSize = 0; |
| 38 | auto effecters = e.value("effecters", emptyList); |
| 39 | for (const auto& effecter : effecters) |
| 40 | { |
| 41 | auto set = effecter.value("set", empty); |
| 42 | auto statesSize = set.value("size", 0); |
| 43 | if (!statesSize) |
| 44 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 45 | error( |
| 46 | "Malformed PDR JSON return pdrEntry;- no state set info, TYPE={STATE_EFFECTER_PDR}", |
| 47 | "STATE_EFFECTER_PDR", |
| 48 | static_cast<unsigned>(PLDM_STATE_EFFECTER_PDR)); |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 49 | throw InternalFailure(); |
| 50 | } |
| 51 | pdrSize += sizeof(state_effecter_possible_states) - |
| 52 | sizeof(bitfield8_t) + (sizeof(bitfield8_t) * statesSize); |
| 53 | } |
| 54 | pdrSize += sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t); |
| 55 | |
| 56 | std::vector<uint8_t> entry{}; |
| 57 | entry.resize(pdrSize); |
| 58 | |
| 59 | pldm_state_effecter_pdr* pdr = |
| 60 | reinterpret_cast<pldm_state_effecter_pdr*>(entry.data()); |
Manojkiran Eda | bcf91ac | 2021-03-14 13:50:48 +0530 | [diff] [blame] | 61 | if (!pdr) |
| 62 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 63 | error("Failed to get state effecter PDR."); |
Manojkiran Eda | bcf91ac | 2021-03-14 13:50:48 +0530 | [diff] [blame] | 64 | continue; |
| 65 | } |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 66 | pdr->hdr.record_handle = 0; |
| 67 | pdr->hdr.version = 1; |
| 68 | pdr->hdr.type = PLDM_STATE_EFFECTER_PDR; |
| 69 | pdr->hdr.record_change_num = 0; |
| 70 | pdr->hdr.length = pdrSize - sizeof(pldm_pdr_hdr); |
| 71 | |
Manojkiran Eda | cc5f158 | 2021-09-29 17:03:06 +0530 | [diff] [blame] | 72 | pdr->terminus_handle = TERMINUS_HANDLE; |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 73 | pdr->effecter_id = handler.getNextEffecterId(); |
George Liu | c4ea6a9 | 2020-07-14 15:48:44 +0800 | [diff] [blame] | 74 | |
| 75 | try |
| 76 | { |
| 77 | std::string entity_path = e.value("entity_path", ""); |
| 78 | auto& associatedEntityMap = handler.getAssociateEntityMap(); |
| 79 | if (entity_path != "" && associatedEntityMap.find(entity_path) != |
| 80 | associatedEntityMap.end()) |
| 81 | { |
| 82 | pdr->entity_type = |
| 83 | associatedEntityMap.at(entity_path).entity_type; |
| 84 | pdr->entity_instance = |
| 85 | associatedEntityMap.at(entity_path).entity_instance_num; |
| 86 | pdr->container_id = |
| 87 | associatedEntityMap.at(entity_path).entity_container_id; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | pdr->entity_type = e.value("type", 0); |
| 92 | pdr->entity_instance = e.value("instance", 0); |
| 93 | pdr->container_id = e.value("container", 0); |
Pavithra Barithaya | 5f21347 | 2022-08-29 02:20:19 -0500 | [diff] [blame] | 94 | |
| 95 | // do not create the PDR when the FRU or the entity path is not |
| 96 | // present |
| 97 | if (!pdr->entity_type) |
| 98 | { |
Pavithra Barithaya | 5f21347 | 2022-08-29 02:20:19 -0500 | [diff] [blame] | 99 | continue; |
| 100 | } |
George Liu | c4ea6a9 | 2020-07-14 15:48:44 +0800 | [diff] [blame] | 101 | } |
| 102 | } |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 103 | catch (const std::exception&) |
George Liu | c4ea6a9 | 2020-07-14 15:48:44 +0800 | [diff] [blame] | 104 | { |
| 105 | pdr->entity_type = e.value("type", 0); |
| 106 | pdr->entity_instance = e.value("instance", 0); |
| 107 | pdr->container_id = e.value("container", 0); |
| 108 | } |
| 109 | |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 110 | pdr->effecter_semantic_id = 0; |
| 111 | pdr->effecter_init = PLDM_NO_INIT; |
| 112 | pdr->has_description_pdr = false; |
| 113 | pdr->composite_effecter_count = effecters.size(); |
| 114 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 115 | pldm::responder::pdr_utils::DbusMappings dbusMappings{}; |
| 116 | pldm::responder::pdr_utils::DbusValMaps dbusValMaps{}; |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 117 | uint8_t* start = entry.data() + sizeof(pldm_state_effecter_pdr) - |
| 118 | sizeof(uint8_t); |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 119 | for (const auto& effecter : effecters) |
| 120 | { |
| 121 | auto set = effecter.value("set", empty); |
| 122 | state_effecter_possible_states* possibleStates = |
| 123 | reinterpret_cast<state_effecter_possible_states*>(start); |
| 124 | possibleStates->state_set_id = set.value("id", 0); |
| 125 | possibleStates->possible_states_size = set.value("size", 0); |
| 126 | |
| 127 | start += sizeof(possibleStates->state_set_id) + |
| 128 | sizeof(possibleStates->possible_states_size); |
| 129 | static const std::vector<uint8_t> emptyStates{}; |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 130 | pldm::responder::pdr_utils::PossibleValues stateValues; |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 131 | auto states = set.value("states", emptyStates); |
| 132 | for (const auto& state : states) |
| 133 | { |
| 134 | auto index = state / 8; |
| 135 | auto bit = state - (index * 8); |
| 136 | bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index); |
| 137 | bf->byte |= 1 << bit; |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 138 | stateValues.emplace_back(state); |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 139 | } |
| 140 | start += possibleStates->possible_states_size; |
| 141 | |
| 142 | auto dbusEntry = effecter.value("dbus", empty); |
| 143 | auto objectPath = dbusEntry.value("path", ""); |
| 144 | auto interface = dbusEntry.value("interface", ""); |
| 145 | auto propertyName = dbusEntry.value("property_name", ""); |
| 146 | auto propertyType = dbusEntry.value("property_type", ""); |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 147 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 148 | pldm::responder::pdr_utils::StatestoDbusVal dbusIdToValMap{}; |
George Liu | 821ebc4 | 2021-01-26 14:36:11 +0800 | [diff] [blame] | 149 | pldm::utils::DBusMapping dbusMapping{}; |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 150 | try |
| 151 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 152 | auto service = dBusIntf.getService(objectPath.c_str(), |
| 153 | interface.c_str()); |
George Liu | 821ebc4 | 2021-01-26 14:36:11 +0800 | [diff] [blame] | 154 | |
| 155 | dbusMapping = pldm::utils::DBusMapping{ |
| 156 | objectPath, interface, propertyName, propertyType}; |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 157 | dbusIdToValMap = pldm::responder::pdr_utils::populateMapping( |
George Liu | 821ebc4 | 2021-01-26 14:36:11 +0800 | [diff] [blame] | 158 | propertyType, dbusEntry["property_values"], stateValues); |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 159 | } |
| 160 | catch (const std::exception& e) |
| 161 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 162 | error( |
Manojkiran Eda | c81d112 | 2024-02-21 20:17:34 +0530 | [diff] [blame] | 163 | "Failed to create effecter PDR, D-Bus object '{PATH}' returned {ERROR}", |
| 164 | "PATH", objectPath, "ERROR", e); |
| 165 | continue; |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 166 | } |
| 167 | |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 168 | dbusMappings.emplace_back(std::move(dbusMapping)); |
George Liu | 821ebc4 | 2021-01-26 14:36:11 +0800 | [diff] [blame] | 169 | dbusValMaps.emplace_back(std::move(dbusIdToValMap)); |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 170 | } |
| 171 | handler.addDbusObjMaps( |
| 172 | pdr->effecter_id, |
| 173 | std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps))); |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 174 | pldm::responder::pdr_utils::PdrEntry pdrEntry{}; |
George Liu | a287072 | 2020-02-11 11:09:30 +0800 | [diff] [blame] | 175 | pdrEntry.data = entry.data(); |
| 176 | pdrEntry.size = pdrSize; |
| 177 | repo.addRecord(pdrEntry); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | } // namespace pdr_state_effecter |
| 182 | } // namespace responder |
| 183 | } // namespace pldm |