1 #pragma once 2 3 #include "pdr.hpp" 4 #include "pdr_utils.hpp" 5 6 #include <libpldm/platform.h> 7 8 #include <phosphor-logging/lg2.hpp> 9 10 PHOSPHOR_LOG2_USING; 11 12 namespace pldm 13 { 14 namespace responder 15 { 16 namespace pdr_state_effecter 17 { 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 */ 29 template <class DBusInterface, class Handler> 30 void generateStateEffecterPDR(const DBusInterface& dBusIntf, const Json& json, 31 Handler& handler, pdr_utils::RepoInterface& repo) 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 { 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)); 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()); 61 if (!pdr) 62 { 63 error("Failed to get state effecter PDR."); 64 continue; 65 } 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 72 pdr->terminus_handle = TERMINUS_HANDLE; 73 pdr->effecter_id = handler.getNextEffecterId(); 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); 94 95 // do not create the PDR when the FRU or the entity path is not 96 // present 97 if (!pdr->entity_type) 98 { 99 std::cerr << "The entity path for the FRU is not present." 100 << std::endl; 101 continue; 102 } 103 } 104 } 105 catch (const std::exception& ex) 106 { 107 pdr->entity_type = e.value("type", 0); 108 pdr->entity_instance = e.value("instance", 0); 109 pdr->container_id = e.value("container", 0); 110 } 111 112 pdr->effecter_semantic_id = 0; 113 pdr->effecter_init = PLDM_NO_INIT; 114 pdr->has_description_pdr = false; 115 pdr->composite_effecter_count = effecters.size(); 116 117 pldm::responder::pdr_utils::DbusMappings dbusMappings{}; 118 pldm::responder::pdr_utils::DbusValMaps dbusValMaps{}; 119 uint8_t* start = entry.data() + sizeof(pldm_state_effecter_pdr) - 120 sizeof(uint8_t); 121 for (const auto& effecter : effecters) 122 { 123 auto set = effecter.value("set", empty); 124 state_effecter_possible_states* possibleStates = 125 reinterpret_cast<state_effecter_possible_states*>(start); 126 possibleStates->state_set_id = set.value("id", 0); 127 possibleStates->possible_states_size = set.value("size", 0); 128 129 start += sizeof(possibleStates->state_set_id) + 130 sizeof(possibleStates->possible_states_size); 131 static const std::vector<uint8_t> emptyStates{}; 132 pldm::responder::pdr_utils::PossibleValues stateValues; 133 auto states = set.value("states", emptyStates); 134 for (const auto& state : states) 135 { 136 auto index = state / 8; 137 auto bit = state - (index * 8); 138 bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index); 139 bf->byte |= 1 << bit; 140 stateValues.emplace_back(state); 141 } 142 start += possibleStates->possible_states_size; 143 144 auto dbusEntry = effecter.value("dbus", empty); 145 auto objectPath = dbusEntry.value("path", ""); 146 auto interface = dbusEntry.value("interface", ""); 147 auto propertyName = dbusEntry.value("property_name", ""); 148 auto propertyType = dbusEntry.value("property_type", ""); 149 150 pldm::responder::pdr_utils::StatestoDbusVal dbusIdToValMap{}; 151 pldm::utils::DBusMapping dbusMapping{}; 152 try 153 { 154 auto service = dBusIntf.getService(objectPath.c_str(), 155 interface.c_str()); 156 157 dbusMapping = pldm::utils::DBusMapping{ 158 objectPath, interface, propertyName, propertyType}; 159 dbusIdToValMap = pldm::responder::pdr_utils::populateMapping( 160 propertyType, dbusEntry["property_values"], stateValues); 161 } 162 catch (const std::exception& e) 163 { 164 error( 165 "D-Bus object path does not exist, effecter ID: {EFFECTER_ID}", 166 "EFFECTER_ID", static_cast<uint16_t>(pdr->effecter_id)); 167 } 168 169 dbusMappings.emplace_back(std::move(dbusMapping)); 170 dbusValMaps.emplace_back(std::move(dbusIdToValMap)); 171 } 172 handler.addDbusObjMaps( 173 pdr->effecter_id, 174 std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps))); 175 pldm::responder::pdr_utils::PdrEntry pdrEntry{}; 176 pdrEntry.data = entry.data(); 177 pdrEntry.size = pdrSize; 178 repo.addRecord(pdrEntry); 179 } 180 } 181 182 } // namespace pdr_state_effecter 183 } // namespace responder 184 } // namespace pldm 185