1a2870726SGeorge Liu #pragma once 2a2870726SGeorge Liu 312afe110SSampa Misra #include "pdr.hpp" 412afe110SSampa Misra #include "pdr_utils.hpp" 56492f524SGeorge Liu 6cc5f1586SManojkiran Eda #include <config.h> 7*c453e164SGeorge Liu #include <libpldm/platform.h> 8cc5f1586SManojkiran Eda 9a2870726SGeorge Liu namespace pldm 10a2870726SGeorge Liu { 11a2870726SGeorge Liu 12a2870726SGeorge Liu namespace responder 13a2870726SGeorge Liu { 14a2870726SGeorge Liu 15a2870726SGeorge Liu namespace pdr_state_effecter 16a2870726SGeorge Liu { 17a2870726SGeorge Liu 18a2870726SGeorge Liu using Json = nlohmann::json; 19a2870726SGeorge Liu 20a2870726SGeorge Liu static const Json empty{}; 21a2870726SGeorge Liu 22a2870726SGeorge Liu /** @brief Parse PDR JSON file and generate state effecter PDR structure 23a2870726SGeorge Liu * 24a2870726SGeorge Liu * @param[in] json - the JSON Object with the state effecter PDR 25a2870726SGeorge Liu * @param[out] handler - the Parser of PLDM command handler 26a2870726SGeorge Liu * @param[out] repo - pdr::RepoInterface 27a2870726SGeorge Liu * 28a2870726SGeorge Liu */ 2936e81352SGeorge Liu template <class DBusInterface, class Handler> 3036e81352SGeorge Liu void generateStateEffecterPDR(const DBusInterface& dBusIntf, const Json& json, 3136e81352SGeorge Liu Handler& handler, pdr_utils::RepoInterface& repo) 32a2870726SGeorge Liu { 33a2870726SGeorge Liu static const std::vector<Json> emptyList{}; 34a2870726SGeorge Liu auto entries = json.value("entries", emptyList); 35a2870726SGeorge Liu for (const auto& e : entries) 36a2870726SGeorge Liu { 37a2870726SGeorge Liu size_t pdrSize = 0; 38a2870726SGeorge Liu auto effecters = e.value("effecters", emptyList); 39a2870726SGeorge Liu for (const auto& effecter : effecters) 40a2870726SGeorge Liu { 41a2870726SGeorge Liu auto set = effecter.value("set", empty); 42a2870726SGeorge Liu auto statesSize = set.value("size", 0); 43a2870726SGeorge Liu if (!statesSize) 44a2870726SGeorge Liu { 45a2870726SGeorge Liu std::cerr << "Malformed PDR JSON return " 46a2870726SGeorge Liu "pdrEntry;- no state set " 47a2870726SGeorge Liu "info, TYPE=" 48a2870726SGeorge Liu << PLDM_STATE_EFFECTER_PDR << "\n"; 49a2870726SGeorge Liu throw InternalFailure(); 50a2870726SGeorge Liu } 51a2870726SGeorge Liu pdrSize += sizeof(state_effecter_possible_states) - 52a2870726SGeorge Liu sizeof(bitfield8_t) + (sizeof(bitfield8_t) * statesSize); 53a2870726SGeorge Liu } 54a2870726SGeorge Liu pdrSize += sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t); 55a2870726SGeorge Liu 56a2870726SGeorge Liu std::vector<uint8_t> entry{}; 57a2870726SGeorge Liu entry.resize(pdrSize); 58a2870726SGeorge Liu 59a2870726SGeorge Liu pldm_state_effecter_pdr* pdr = 60a2870726SGeorge Liu reinterpret_cast<pldm_state_effecter_pdr*>(entry.data()); 61bcf91accSManojkiran Eda if (!pdr) 62bcf91accSManojkiran Eda { 63bcf91accSManojkiran Eda std::cerr << "Failed to get state effecter PDR.\n"; 64bcf91accSManojkiran Eda continue; 65bcf91accSManojkiran Eda } 66a2870726SGeorge Liu pdr->hdr.record_handle = 0; 67a2870726SGeorge Liu pdr->hdr.version = 1; 68a2870726SGeorge Liu pdr->hdr.type = PLDM_STATE_EFFECTER_PDR; 69a2870726SGeorge Liu pdr->hdr.record_change_num = 0; 70a2870726SGeorge Liu pdr->hdr.length = pdrSize - sizeof(pldm_pdr_hdr); 71a2870726SGeorge Liu 72cc5f1586SManojkiran Eda pdr->terminus_handle = TERMINUS_HANDLE; 73a2870726SGeorge Liu pdr->effecter_id = handler.getNextEffecterId(); 74c4ea6a90SGeorge Liu 75c4ea6a90SGeorge Liu try 76c4ea6a90SGeorge Liu { 77c4ea6a90SGeorge Liu std::string entity_path = e.value("entity_path", ""); 78c4ea6a90SGeorge Liu auto& associatedEntityMap = handler.getAssociateEntityMap(); 79c4ea6a90SGeorge Liu if (entity_path != "" && associatedEntityMap.find(entity_path) != 80c4ea6a90SGeorge Liu associatedEntityMap.end()) 81c4ea6a90SGeorge Liu { 82c4ea6a90SGeorge Liu pdr->entity_type = 83c4ea6a90SGeorge Liu associatedEntityMap.at(entity_path).entity_type; 84c4ea6a90SGeorge Liu pdr->entity_instance = 85c4ea6a90SGeorge Liu associatedEntityMap.at(entity_path).entity_instance_num; 86c4ea6a90SGeorge Liu pdr->container_id = 87c4ea6a90SGeorge Liu associatedEntityMap.at(entity_path).entity_container_id; 88c4ea6a90SGeorge Liu } 89c4ea6a90SGeorge Liu else 90c4ea6a90SGeorge Liu { 91a2870726SGeorge Liu pdr->entity_type = e.value("type", 0); 92a2870726SGeorge Liu pdr->entity_instance = e.value("instance", 0); 93a2870726SGeorge Liu pdr->container_id = e.value("container", 0); 94c4ea6a90SGeorge Liu } 95c4ea6a90SGeorge Liu } 96c4ea6a90SGeorge Liu catch (const std::exception& ex) 97c4ea6a90SGeorge Liu { 98c4ea6a90SGeorge Liu pdr->entity_type = e.value("type", 0); 99c4ea6a90SGeorge Liu pdr->entity_instance = e.value("instance", 0); 100c4ea6a90SGeorge Liu pdr->container_id = e.value("container", 0); 101c4ea6a90SGeorge Liu } 102c4ea6a90SGeorge Liu 103a2870726SGeorge Liu pdr->effecter_semantic_id = 0; 104a2870726SGeorge Liu pdr->effecter_init = PLDM_NO_INIT; 105a2870726SGeorge Liu pdr->has_description_pdr = false; 106a2870726SGeorge Liu pdr->composite_effecter_count = effecters.size(); 107a2870726SGeorge Liu 1085079ac4aSBrad Bishop pldm::responder::pdr_utils::DbusMappings dbusMappings{}; 1095079ac4aSBrad Bishop pldm::responder::pdr_utils::DbusValMaps dbusValMaps{}; 110a2870726SGeorge Liu uint8_t* start = 111a2870726SGeorge Liu entry.data() + sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t); 112a2870726SGeorge Liu for (const auto& effecter : effecters) 113a2870726SGeorge Liu { 114a2870726SGeorge Liu auto set = effecter.value("set", empty); 115a2870726SGeorge Liu state_effecter_possible_states* possibleStates = 116a2870726SGeorge Liu reinterpret_cast<state_effecter_possible_states*>(start); 117a2870726SGeorge Liu possibleStates->state_set_id = set.value("id", 0); 118a2870726SGeorge Liu possibleStates->possible_states_size = set.value("size", 0); 119a2870726SGeorge Liu 120a2870726SGeorge Liu start += sizeof(possibleStates->state_set_id) + 121a2870726SGeorge Liu sizeof(possibleStates->possible_states_size); 122a2870726SGeorge Liu static const std::vector<uint8_t> emptyStates{}; 1235079ac4aSBrad Bishop pldm::responder::pdr_utils::PossibleValues stateValues; 124a2870726SGeorge Liu auto states = set.value("states", emptyStates); 125a2870726SGeorge Liu for (const auto& state : states) 126a2870726SGeorge Liu { 127a2870726SGeorge Liu auto index = state / 8; 128a2870726SGeorge Liu auto bit = state - (index * 8); 129a2870726SGeorge Liu bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index); 130a2870726SGeorge Liu bf->byte |= 1 << bit; 131adbe1726SGeorge Liu stateValues.emplace_back(state); 132a2870726SGeorge Liu } 133a2870726SGeorge Liu start += possibleStates->possible_states_size; 134a2870726SGeorge Liu 135a2870726SGeorge Liu auto dbusEntry = effecter.value("dbus", empty); 136a2870726SGeorge Liu auto objectPath = dbusEntry.value("path", ""); 137a2870726SGeorge Liu auto interface = dbusEntry.value("interface", ""); 138a2870726SGeorge Liu auto propertyName = dbusEntry.value("property_name", ""); 139a2870726SGeorge Liu auto propertyType = dbusEntry.value("property_type", ""); 14036e81352SGeorge Liu 1415079ac4aSBrad Bishop pldm::responder::pdr_utils::StatestoDbusVal dbusIdToValMap{}; 142821ebc47SGeorge Liu pldm::utils::DBusMapping dbusMapping{}; 14336e81352SGeorge Liu try 14436e81352SGeorge Liu { 14536e81352SGeorge Liu auto service = 14636e81352SGeorge Liu dBusIntf.getService(objectPath.c_str(), interface.c_str()); 147821ebc47SGeorge Liu 148821ebc47SGeorge Liu dbusMapping = pldm::utils::DBusMapping{ 149821ebc47SGeorge Liu objectPath, interface, propertyName, propertyType}; 1505079ac4aSBrad Bishop dbusIdToValMap = pldm::responder::pdr_utils::populateMapping( 151821ebc47SGeorge Liu propertyType, dbusEntry["property_values"], stateValues); 15236e81352SGeorge Liu } 15336e81352SGeorge Liu catch (const std::exception& e) 15436e81352SGeorge Liu { 155821ebc47SGeorge Liu std::cerr << "D-Bus object path does not exist, effecter ID: " 156821ebc47SGeorge Liu << pdr->effecter_id << "\n"; 15736e81352SGeorge Liu } 15836e81352SGeorge Liu 159a2870726SGeorge Liu dbusMappings.emplace_back(std::move(dbusMapping)); 160a2870726SGeorge Liu dbusValMaps.emplace_back(std::move(dbusIdToValMap)); 161a2870726SGeorge Liu } 162a2870726SGeorge Liu handler.addDbusObjMaps( 163a2870726SGeorge Liu pdr->effecter_id, 164a2870726SGeorge Liu std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps))); 1655079ac4aSBrad Bishop pldm::responder::pdr_utils::PdrEntry pdrEntry{}; 166a2870726SGeorge Liu pdrEntry.data = entry.data(); 167a2870726SGeorge Liu pdrEntry.size = pdrSize; 168a2870726SGeorge Liu repo.addRecord(pdrEntry); 169a2870726SGeorge Liu } 170a2870726SGeorge Liu } 171a2870726SGeorge Liu 172a2870726SGeorge Liu } // namespace pdr_state_effecter 173a2870726SGeorge Liu } // namespace responder 174a2870726SGeorge Liu } // namespace pldm 175