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