1a2870726SGeorge Liu #pragma once 2a2870726SGeorge Liu 312afe110SSampa Misra #include "pdr.hpp" 412afe110SSampa Misra #include "pdr_utils.hpp" 56492f524SGeorge Liu 6cc5f1586SManojkiran Eda #include <config.h> 7c453e164SGeorge Liu #include <libpldm/platform.h> 8cc5f1586SManojkiran Eda 949cfb138SRiya Dixit #include <phosphor-logging/lg2.hpp> 1049cfb138SRiya Dixit 1149cfb138SRiya Dixit PHOSPHOR_LOG2_USING; 1249cfb138SRiya Dixit 13a2870726SGeorge Liu namespace pldm 14a2870726SGeorge Liu { 15a2870726SGeorge Liu namespace responder 16a2870726SGeorge Liu { 17a2870726SGeorge Liu namespace pdr_state_effecter 18a2870726SGeorge Liu { 19a2870726SGeorge Liu using Json = nlohmann::json; 20a2870726SGeorge Liu 21a2870726SGeorge Liu static const Json empty{}; 22a2870726SGeorge Liu 23a2870726SGeorge Liu /** @brief Parse PDR JSON file and generate state effecter PDR structure 24a2870726SGeorge Liu * 25a2870726SGeorge Liu * @param[in] json - the JSON Object with the state effecter PDR 26a2870726SGeorge Liu * @param[out] handler - the Parser of PLDM command handler 27a2870726SGeorge Liu * @param[out] repo - pdr::RepoInterface 28a2870726SGeorge Liu * 29a2870726SGeorge Liu */ 3036e81352SGeorge Liu template <class DBusInterface, class Handler> 3136e81352SGeorge Liu void generateStateEffecterPDR(const DBusInterface& dBusIntf, const Json& json, 3236e81352SGeorge Liu Handler& handler, pdr_utils::RepoInterface& repo) 33a2870726SGeorge Liu { 34a2870726SGeorge Liu static const std::vector<Json> emptyList{}; 35a2870726SGeorge Liu auto entries = json.value("entries", emptyList); 36a2870726SGeorge Liu for (const auto& e : entries) 37a2870726SGeorge Liu { 38a2870726SGeorge Liu size_t pdrSize = 0; 39a2870726SGeorge Liu auto effecters = e.value("effecters", emptyList); 40a2870726SGeorge Liu for (const auto& effecter : effecters) 41a2870726SGeorge Liu { 42a2870726SGeorge Liu auto set = effecter.value("set", empty); 43a2870726SGeorge Liu auto statesSize = set.value("size", 0); 44a2870726SGeorge Liu if (!statesSize) 45a2870726SGeorge Liu { 4649cfb138SRiya Dixit error( 4749cfb138SRiya Dixit "Malformed PDR JSON return pdrEntry;- no state set info, TYPE={STATE_EFFECTER_PDR}", 4849cfb138SRiya Dixit "STATE_EFFECTER_PDR", 4949cfb138SRiya Dixit static_cast<unsigned>(PLDM_STATE_EFFECTER_PDR)); 50a2870726SGeorge Liu throw InternalFailure(); 51a2870726SGeorge Liu } 52a2870726SGeorge Liu pdrSize += sizeof(state_effecter_possible_states) - 53a2870726SGeorge Liu sizeof(bitfield8_t) + (sizeof(bitfield8_t) * statesSize); 54a2870726SGeorge Liu } 55a2870726SGeorge Liu pdrSize += sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t); 56a2870726SGeorge Liu 57a2870726SGeorge Liu std::vector<uint8_t> entry{}; 58a2870726SGeorge Liu entry.resize(pdrSize); 59a2870726SGeorge Liu 60a2870726SGeorge Liu pldm_state_effecter_pdr* pdr = 61a2870726SGeorge Liu reinterpret_cast<pldm_state_effecter_pdr*>(entry.data()); 62bcf91accSManojkiran Eda if (!pdr) 63bcf91accSManojkiran Eda { 6449cfb138SRiya Dixit error("Failed to get state effecter PDR."); 65bcf91accSManojkiran Eda continue; 66bcf91accSManojkiran Eda } 67a2870726SGeorge Liu pdr->hdr.record_handle = 0; 68a2870726SGeorge Liu pdr->hdr.version = 1; 69a2870726SGeorge Liu pdr->hdr.type = PLDM_STATE_EFFECTER_PDR; 70a2870726SGeorge Liu pdr->hdr.record_change_num = 0; 71a2870726SGeorge Liu pdr->hdr.length = pdrSize - sizeof(pldm_pdr_hdr); 72a2870726SGeorge Liu 73cc5f1586SManojkiran Eda pdr->terminus_handle = TERMINUS_HANDLE; 74a2870726SGeorge Liu pdr->effecter_id = handler.getNextEffecterId(); 75c4ea6a90SGeorge Liu 76c4ea6a90SGeorge Liu try 77c4ea6a90SGeorge Liu { 78c4ea6a90SGeorge Liu std::string entity_path = e.value("entity_path", ""); 79c4ea6a90SGeorge Liu auto& associatedEntityMap = handler.getAssociateEntityMap(); 80c4ea6a90SGeorge Liu if (entity_path != "" && associatedEntityMap.find(entity_path) != 81c4ea6a90SGeorge Liu associatedEntityMap.end()) 82c4ea6a90SGeorge Liu { 83c4ea6a90SGeorge Liu pdr->entity_type = 84c4ea6a90SGeorge Liu associatedEntityMap.at(entity_path).entity_type; 85c4ea6a90SGeorge Liu pdr->entity_instance = 86c4ea6a90SGeorge Liu associatedEntityMap.at(entity_path).entity_instance_num; 87c4ea6a90SGeorge Liu pdr->container_id = 88c4ea6a90SGeorge Liu associatedEntityMap.at(entity_path).entity_container_id; 89c4ea6a90SGeorge Liu } 90c4ea6a90SGeorge Liu else 91c4ea6a90SGeorge Liu { 92a2870726SGeorge Liu pdr->entity_type = e.value("type", 0); 93a2870726SGeorge Liu pdr->entity_instance = e.value("instance", 0); 94a2870726SGeorge Liu pdr->container_id = e.value("container", 0); 955f213471SPavithra Barithaya 965f213471SPavithra Barithaya // do not create the PDR when the FRU or the entity path is not 975f213471SPavithra Barithaya // present 985f213471SPavithra Barithaya if (!pdr->entity_type) 995f213471SPavithra Barithaya { 1005f213471SPavithra Barithaya std::cerr << "The entity path for the FRU is not present." 1015f213471SPavithra Barithaya << std::endl; 1025f213471SPavithra Barithaya continue; 1035f213471SPavithra Barithaya } 104c4ea6a90SGeorge Liu } 105c4ea6a90SGeorge Liu } 106c4ea6a90SGeorge Liu catch (const std::exception& ex) 107c4ea6a90SGeorge Liu { 108c4ea6a90SGeorge Liu pdr->entity_type = e.value("type", 0); 109c4ea6a90SGeorge Liu pdr->entity_instance = e.value("instance", 0); 110c4ea6a90SGeorge Liu pdr->container_id = e.value("container", 0); 111c4ea6a90SGeorge Liu } 112c4ea6a90SGeorge Liu 113a2870726SGeorge Liu pdr->effecter_semantic_id = 0; 114a2870726SGeorge Liu pdr->effecter_init = PLDM_NO_INIT; 115a2870726SGeorge Liu pdr->has_description_pdr = false; 116a2870726SGeorge Liu pdr->composite_effecter_count = effecters.size(); 117a2870726SGeorge Liu 1185079ac4aSBrad Bishop pldm::responder::pdr_utils::DbusMappings dbusMappings{}; 1195079ac4aSBrad Bishop pldm::responder::pdr_utils::DbusValMaps dbusValMaps{}; 120*6da4f91bSPatrick Williams uint8_t* start = entry.data() + sizeof(pldm_state_effecter_pdr) - 121*6da4f91bSPatrick Williams sizeof(uint8_t); 122a2870726SGeorge Liu for (const auto& effecter : effecters) 123a2870726SGeorge Liu { 124a2870726SGeorge Liu auto set = effecter.value("set", empty); 125a2870726SGeorge Liu state_effecter_possible_states* possibleStates = 126a2870726SGeorge Liu reinterpret_cast<state_effecter_possible_states*>(start); 127a2870726SGeorge Liu possibleStates->state_set_id = set.value("id", 0); 128a2870726SGeorge Liu possibleStates->possible_states_size = set.value("size", 0); 129a2870726SGeorge Liu 130a2870726SGeorge Liu start += sizeof(possibleStates->state_set_id) + 131a2870726SGeorge Liu sizeof(possibleStates->possible_states_size); 132a2870726SGeorge Liu static const std::vector<uint8_t> emptyStates{}; 1335079ac4aSBrad Bishop pldm::responder::pdr_utils::PossibleValues stateValues; 134a2870726SGeorge Liu auto states = set.value("states", emptyStates); 135a2870726SGeorge Liu for (const auto& state : states) 136a2870726SGeorge Liu { 137a2870726SGeorge Liu auto index = state / 8; 138a2870726SGeorge Liu auto bit = state - (index * 8); 139a2870726SGeorge Liu bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index); 140a2870726SGeorge Liu bf->byte |= 1 << bit; 141adbe1726SGeorge Liu stateValues.emplace_back(state); 142a2870726SGeorge Liu } 143a2870726SGeorge Liu start += possibleStates->possible_states_size; 144a2870726SGeorge Liu 145a2870726SGeorge Liu auto dbusEntry = effecter.value("dbus", empty); 146a2870726SGeorge Liu auto objectPath = dbusEntry.value("path", ""); 147a2870726SGeorge Liu auto interface = dbusEntry.value("interface", ""); 148a2870726SGeorge Liu auto propertyName = dbusEntry.value("property_name", ""); 149a2870726SGeorge Liu auto propertyType = dbusEntry.value("property_type", ""); 15036e81352SGeorge Liu 1515079ac4aSBrad Bishop pldm::responder::pdr_utils::StatestoDbusVal dbusIdToValMap{}; 152821ebc47SGeorge Liu pldm::utils::DBusMapping dbusMapping{}; 15336e81352SGeorge Liu try 15436e81352SGeorge Liu { 155*6da4f91bSPatrick Williams auto service = dBusIntf.getService(objectPath.c_str(), 156*6da4f91bSPatrick Williams interface.c_str()); 157821ebc47SGeorge Liu 158821ebc47SGeorge Liu dbusMapping = pldm::utils::DBusMapping{ 159821ebc47SGeorge Liu objectPath, interface, propertyName, propertyType}; 1605079ac4aSBrad Bishop dbusIdToValMap = pldm::responder::pdr_utils::populateMapping( 161821ebc47SGeorge Liu propertyType, dbusEntry["property_values"], stateValues); 16236e81352SGeorge Liu } 16336e81352SGeorge Liu catch (const std::exception& e) 16436e81352SGeorge Liu { 16549cfb138SRiya Dixit error( 16649cfb138SRiya Dixit "D-Bus object path does not exist, effecter ID: {EFFECTER_ID}", 16749cfb138SRiya Dixit "EFFECTER_ID", static_cast<uint16_t>(pdr->effecter_id)); 16836e81352SGeorge Liu } 16936e81352SGeorge Liu 170a2870726SGeorge Liu dbusMappings.emplace_back(std::move(dbusMapping)); 171a2870726SGeorge Liu dbusValMaps.emplace_back(std::move(dbusIdToValMap)); 172a2870726SGeorge Liu } 173a2870726SGeorge Liu handler.addDbusObjMaps( 174a2870726SGeorge Liu pdr->effecter_id, 175a2870726SGeorge Liu std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps))); 1765079ac4aSBrad Bishop pldm::responder::pdr_utils::PdrEntry pdrEntry{}; 177a2870726SGeorge Liu pdrEntry.data = entry.data(); 178a2870726SGeorge Liu pdrEntry.size = pdrSize; 179a2870726SGeorge Liu repo.addRecord(pdrEntry); 180a2870726SGeorge Liu } 181a2870726SGeorge Liu } 182a2870726SGeorge Liu 183a2870726SGeorge Liu } // namespace pdr_state_effecter 184a2870726SGeorge Liu } // namespace responder 185a2870726SGeorge Liu } // namespace pldm 186