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 9a2870726SGeorge Liu namespace pldm 10a2870726SGeorge Liu { 11a2870726SGeorge Liu namespace responder 12a2870726SGeorge Liu { 13a2870726SGeorge Liu namespace pdr_state_effecter 14a2870726SGeorge Liu { 15a2870726SGeorge Liu using Json = nlohmann::json; 16a2870726SGeorge Liu 17a2870726SGeorge Liu static const Json empty{}; 18a2870726SGeorge Liu 19a2870726SGeorge Liu /** @brief Parse PDR JSON file and generate state effecter PDR structure 20a2870726SGeorge Liu * 21a2870726SGeorge Liu * @param[in] json - the JSON Object with the state effecter PDR 22a2870726SGeorge Liu * @param[out] handler - the Parser of PLDM command handler 23a2870726SGeorge Liu * @param[out] repo - pdr::RepoInterface 24a2870726SGeorge Liu * 25a2870726SGeorge Liu */ 2636e81352SGeorge Liu template <class DBusInterface, class Handler> 2736e81352SGeorge Liu void generateStateEffecterPDR(const DBusInterface& dBusIntf, const Json& json, 2836e81352SGeorge Liu Handler& handler, pdr_utils::RepoInterface& repo) 29a2870726SGeorge Liu { 30a2870726SGeorge Liu static const std::vector<Json> emptyList{}; 31a2870726SGeorge Liu auto entries = json.value("entries", emptyList); 32a2870726SGeorge Liu for (const auto& e : entries) 33a2870726SGeorge Liu { 34a2870726SGeorge Liu size_t pdrSize = 0; 35a2870726SGeorge Liu auto effecters = e.value("effecters", emptyList); 36a2870726SGeorge Liu for (const auto& effecter : effecters) 37a2870726SGeorge Liu { 38a2870726SGeorge Liu auto set = effecter.value("set", empty); 39a2870726SGeorge Liu auto statesSize = set.value("size", 0); 40a2870726SGeorge Liu if (!statesSize) 41a2870726SGeorge Liu { 42a2870726SGeorge Liu std::cerr << "Malformed PDR JSON return " 43a2870726SGeorge Liu "pdrEntry;- no state set " 44a2870726SGeorge Liu "info, TYPE=" 45a2870726SGeorge Liu << PLDM_STATE_EFFECTER_PDR << "\n"; 46a2870726SGeorge Liu throw InternalFailure(); 47a2870726SGeorge Liu } 48a2870726SGeorge Liu pdrSize += sizeof(state_effecter_possible_states) - 49a2870726SGeorge Liu sizeof(bitfield8_t) + (sizeof(bitfield8_t) * statesSize); 50a2870726SGeorge Liu } 51a2870726SGeorge Liu pdrSize += sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t); 52a2870726SGeorge Liu 53a2870726SGeorge Liu std::vector<uint8_t> entry{}; 54a2870726SGeorge Liu entry.resize(pdrSize); 55a2870726SGeorge Liu 56a2870726SGeorge Liu pldm_state_effecter_pdr* pdr = 57a2870726SGeorge Liu reinterpret_cast<pldm_state_effecter_pdr*>(entry.data()); 58bcf91accSManojkiran Eda if (!pdr) 59bcf91accSManojkiran Eda { 60bcf91accSManojkiran Eda std::cerr << "Failed to get state effecter PDR.\n"; 61bcf91accSManojkiran Eda continue; 62bcf91accSManojkiran Eda } 63a2870726SGeorge Liu pdr->hdr.record_handle = 0; 64a2870726SGeorge Liu pdr->hdr.version = 1; 65a2870726SGeorge Liu pdr->hdr.type = PLDM_STATE_EFFECTER_PDR; 66a2870726SGeorge Liu pdr->hdr.record_change_num = 0; 67a2870726SGeorge Liu pdr->hdr.length = pdrSize - sizeof(pldm_pdr_hdr); 68a2870726SGeorge Liu 69cc5f1586SManojkiran Eda pdr->terminus_handle = TERMINUS_HANDLE; 70a2870726SGeorge Liu pdr->effecter_id = handler.getNextEffecterId(); 71c4ea6a90SGeorge Liu 72c4ea6a90SGeorge Liu try 73c4ea6a90SGeorge Liu { 74c4ea6a90SGeorge Liu std::string entity_path = e.value("entity_path", ""); 75c4ea6a90SGeorge Liu auto& associatedEntityMap = handler.getAssociateEntityMap(); 76c4ea6a90SGeorge Liu if (entity_path != "" && associatedEntityMap.find(entity_path) != 77c4ea6a90SGeorge Liu associatedEntityMap.end()) 78c4ea6a90SGeorge Liu { 79c4ea6a90SGeorge Liu pdr->entity_type = 80c4ea6a90SGeorge Liu associatedEntityMap.at(entity_path).entity_type; 81c4ea6a90SGeorge Liu pdr->entity_instance = 82c4ea6a90SGeorge Liu associatedEntityMap.at(entity_path).entity_instance_num; 83c4ea6a90SGeorge Liu pdr->container_id = 84c4ea6a90SGeorge Liu associatedEntityMap.at(entity_path).entity_container_id; 85c4ea6a90SGeorge Liu } 86c4ea6a90SGeorge Liu else 87c4ea6a90SGeorge Liu { 88a2870726SGeorge Liu pdr->entity_type = e.value("type", 0); 89a2870726SGeorge Liu pdr->entity_instance = e.value("instance", 0); 90a2870726SGeorge Liu pdr->container_id = e.value("container", 0); 91*5f213471SPavithra Barithaya 92*5f213471SPavithra Barithaya // do not create the PDR when the FRU or the entity path is not 93*5f213471SPavithra Barithaya // present 94*5f213471SPavithra Barithaya if (!pdr->entity_type) 95*5f213471SPavithra Barithaya { 96*5f213471SPavithra Barithaya std::cerr << "The entity path for the FRU is not present." 97*5f213471SPavithra Barithaya << std::endl; 98*5f213471SPavithra Barithaya continue; 99*5f213471SPavithra Barithaya } 100c4ea6a90SGeorge Liu } 101c4ea6a90SGeorge Liu } 102c4ea6a90SGeorge Liu catch (const std::exception& ex) 103c4ea6a90SGeorge Liu { 104c4ea6a90SGeorge Liu pdr->entity_type = e.value("type", 0); 105c4ea6a90SGeorge Liu pdr->entity_instance = e.value("instance", 0); 106c4ea6a90SGeorge Liu pdr->container_id = e.value("container", 0); 107c4ea6a90SGeorge Liu } 108c4ea6a90SGeorge Liu 109a2870726SGeorge Liu pdr->effecter_semantic_id = 0; 110a2870726SGeorge Liu pdr->effecter_init = PLDM_NO_INIT; 111a2870726SGeorge Liu pdr->has_description_pdr = false; 112a2870726SGeorge Liu pdr->composite_effecter_count = effecters.size(); 113a2870726SGeorge Liu 1145079ac4aSBrad Bishop pldm::responder::pdr_utils::DbusMappings dbusMappings{}; 1155079ac4aSBrad Bishop pldm::responder::pdr_utils::DbusValMaps dbusValMaps{}; 116a2870726SGeorge Liu uint8_t* start = 117a2870726SGeorge Liu entry.data() + sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t); 118a2870726SGeorge Liu for (const auto& effecter : effecters) 119a2870726SGeorge Liu { 120a2870726SGeorge Liu auto set = effecter.value("set", empty); 121a2870726SGeorge Liu state_effecter_possible_states* possibleStates = 122a2870726SGeorge Liu reinterpret_cast<state_effecter_possible_states*>(start); 123a2870726SGeorge Liu possibleStates->state_set_id = set.value("id", 0); 124a2870726SGeorge Liu possibleStates->possible_states_size = set.value("size", 0); 125a2870726SGeorge Liu 126a2870726SGeorge Liu start += sizeof(possibleStates->state_set_id) + 127a2870726SGeorge Liu sizeof(possibleStates->possible_states_size); 128a2870726SGeorge Liu static const std::vector<uint8_t> emptyStates{}; 1295079ac4aSBrad Bishop pldm::responder::pdr_utils::PossibleValues stateValues; 130a2870726SGeorge Liu auto states = set.value("states", emptyStates); 131a2870726SGeorge Liu for (const auto& state : states) 132a2870726SGeorge Liu { 133a2870726SGeorge Liu auto index = state / 8; 134a2870726SGeorge Liu auto bit = state - (index * 8); 135a2870726SGeorge Liu bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index); 136a2870726SGeorge Liu bf->byte |= 1 << bit; 137adbe1726SGeorge Liu stateValues.emplace_back(state); 138a2870726SGeorge Liu } 139a2870726SGeorge Liu start += possibleStates->possible_states_size; 140a2870726SGeorge Liu 141a2870726SGeorge Liu auto dbusEntry = effecter.value("dbus", empty); 142a2870726SGeorge Liu auto objectPath = dbusEntry.value("path", ""); 143a2870726SGeorge Liu auto interface = dbusEntry.value("interface", ""); 144a2870726SGeorge Liu auto propertyName = dbusEntry.value("property_name", ""); 145a2870726SGeorge Liu auto propertyType = dbusEntry.value("property_type", ""); 14636e81352SGeorge Liu 1475079ac4aSBrad Bishop pldm::responder::pdr_utils::StatestoDbusVal dbusIdToValMap{}; 148821ebc47SGeorge Liu pldm::utils::DBusMapping dbusMapping{}; 14936e81352SGeorge Liu try 15036e81352SGeorge Liu { 15136e81352SGeorge Liu auto service = 15236e81352SGeorge Liu dBusIntf.getService(objectPath.c_str(), interface.c_str()); 153821ebc47SGeorge Liu 154821ebc47SGeorge Liu dbusMapping = pldm::utils::DBusMapping{ 155821ebc47SGeorge Liu objectPath, interface, propertyName, propertyType}; 1565079ac4aSBrad Bishop dbusIdToValMap = pldm::responder::pdr_utils::populateMapping( 157821ebc47SGeorge Liu propertyType, dbusEntry["property_values"], stateValues); 15836e81352SGeorge Liu } 15936e81352SGeorge Liu catch (const std::exception& e) 16036e81352SGeorge Liu { 161821ebc47SGeorge Liu std::cerr << "D-Bus object path does not exist, effecter ID: " 162821ebc47SGeorge Liu << pdr->effecter_id << "\n"; 16336e81352SGeorge Liu } 16436e81352SGeorge Liu 165a2870726SGeorge Liu dbusMappings.emplace_back(std::move(dbusMapping)); 166a2870726SGeorge Liu dbusValMaps.emplace_back(std::move(dbusIdToValMap)); 167a2870726SGeorge Liu } 168a2870726SGeorge Liu handler.addDbusObjMaps( 169a2870726SGeorge Liu pdr->effecter_id, 170a2870726SGeorge Liu std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps))); 1715079ac4aSBrad Bishop pldm::responder::pdr_utils::PdrEntry pdrEntry{}; 172a2870726SGeorge Liu pdrEntry.data = entry.data(); 173a2870726SGeorge Liu pdrEntry.size = pdrSize; 174a2870726SGeorge Liu repo.addRecord(pdrEntry); 175a2870726SGeorge Liu } 176a2870726SGeorge Liu } 177a2870726SGeorge Liu 178a2870726SGeorge Liu } // namespace pdr_state_effecter 179a2870726SGeorge Liu } // namespace responder 180a2870726SGeorge Liu } // namespace pldm 181