1 #pragma once 2 3 #include "libpldmresponder/pdr_utils.hpp" 4 5 #include <libpldm/platform.h> 6 7 #include <phosphor-logging/lg2.hpp> 8 9 PHOSPHOR_LOG2_USING; 10 11 namespace pldm 12 { 13 namespace responder 14 { 15 namespace pdr_numeric_effecter 16 { 17 using Json = nlohmann::json; 18 19 static const Json empty{}; 20 21 /** @brief Parse PDR JSON file and generate numeric effecter PDR structure 22 * 23 * @param[in] json - the JSON Object with the numeric effecter PDR 24 * @param[out] handler - the Parser of PLDM command handler 25 * @param[out] repo - pdr::RepoInterface 26 * 27 */ 28 template <class DBusInterface, class Handler> 29 void generateNumericEffecterPDR(const DBusInterface& dBusIntf, const Json& json, 30 Handler& handler, 31 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 std::vector<uint8_t> entry{}; 38 entry.resize(sizeof(pldm_numeric_effecter_value_pdr)); 39 40 pldm_numeric_effecter_value_pdr* pdr = 41 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(entry.data()); 42 if (!pdr) 43 { 44 error("Failed to get numeric effecter PDR."); 45 continue; 46 } 47 pdr->hdr.record_handle = 0; 48 pdr->hdr.version = 1; 49 pdr->hdr.type = PLDM_NUMERIC_EFFECTER_PDR; 50 pdr->hdr.record_change_num = 0; 51 pdr->hdr.length = sizeof(pldm_numeric_effecter_value_pdr) - 52 sizeof(pldm_pdr_hdr); 53 54 pdr->terminus_handle = e.value("terminus_handle", 0); 55 pdr->effecter_id = handler.getNextEffecterId(); 56 57 try 58 { 59 std::string entity_path = e.value("entity_path", ""); 60 auto& associatedEntityMap = handler.getAssociateEntityMap(); 61 if (entity_path != "" && associatedEntityMap.find(entity_path) != 62 associatedEntityMap.end()) 63 { 64 pdr->entity_type = 65 associatedEntityMap.at(entity_path).entity_type; 66 pdr->entity_instance = 67 associatedEntityMap.at(entity_path).entity_instance_num; 68 pdr->container_id = 69 associatedEntityMap.at(entity_path).entity_container_id; 70 } 71 else 72 { 73 pdr->entity_type = e.value("type", 0); 74 pdr->entity_instance = e.value("instance", 0); 75 pdr->container_id = e.value("container", 0); 76 77 // do not create the PDR when the FRU or the entity path is not 78 // present 79 if (!pdr->entity_type) 80 { 81 continue; 82 } 83 } 84 } 85 catch (const std::exception&) 86 { 87 pdr->entity_type = e.value("type", 0); 88 pdr->entity_instance = e.value("instance", 0); 89 pdr->container_id = e.value("container", 0); 90 } 91 92 pdr->effecter_semantic_id = e.value("effecter_semantic_id", 0); 93 pdr->effecter_init = e.value("effecter_init", PLDM_NO_INIT); 94 pdr->effecter_auxiliary_names = e.value("effecter_init", false); 95 pdr->base_unit = e.value("base_unit", 0); 96 pdr->unit_modifier = e.value("unit_modifier", 0); 97 pdr->rate_unit = e.value("rate_unit", 0); 98 pdr->base_oem_unit_handle = e.value("base_oem_unit_handle", 0); 99 pdr->aux_unit = e.value("aux_unit", 0); 100 pdr->aux_unit_modifier = e.value("aux_unit_modifier", 0); 101 pdr->aux_oem_unit_handle = e.value("aux_oem_unit_handle", 0); 102 pdr->aux_rate_unit = e.value("aux_rate_unit", 0); 103 pdr->is_linear = e.value("is_linear", true); 104 pdr->effecter_data_size = e.value("effecter_data_size", 105 PLDM_EFFECTER_DATA_SIZE_UINT8); 106 pdr->resolution = e.value("effecter_resolution_init", 1.00); 107 pdr->offset = e.value("offset", 0.00); 108 pdr->accuracy = e.value("accuracy", 0); 109 pdr->plus_tolerance = e.value("plus_tolerance", 0); 110 pdr->minus_tolerance = e.value("minus_tolerance", 0); 111 pdr->state_transition_interval = e.value("state_transition_interval", 112 0.00); 113 pdr->transition_interval = e.value("transition_interval", 0.00); 114 switch (pdr->effecter_data_size) 115 { 116 case PLDM_EFFECTER_DATA_SIZE_UINT8: 117 pdr->max_settable.value_u8 = e.value("max_settable", 0); 118 pdr->min_settable.value_u8 = e.value("min_settable", 0); 119 break; 120 case PLDM_EFFECTER_DATA_SIZE_SINT8: 121 pdr->max_settable.value_s8 = e.value("max_settable", 0); 122 pdr->min_settable.value_s8 = e.value("min_settable", 0); 123 break; 124 case PLDM_EFFECTER_DATA_SIZE_UINT16: 125 pdr->max_settable.value_u16 = e.value("max_settable", 0); 126 pdr->min_settable.value_u16 = e.value("min_settable", 0); 127 break; 128 case PLDM_EFFECTER_DATA_SIZE_SINT16: 129 pdr->max_settable.value_s16 = e.value("max_settable", 0); 130 pdr->min_settable.value_s16 = e.value("min_settable", 0); 131 break; 132 case PLDM_EFFECTER_DATA_SIZE_UINT32: 133 pdr->max_settable.value_u32 = e.value("max_settable", 0); 134 pdr->min_settable.value_u32 = e.value("min_settable", 0); 135 break; 136 case PLDM_EFFECTER_DATA_SIZE_SINT32: 137 pdr->max_settable.value_s32 = e.value("max_settable", 0); 138 pdr->min_settable.value_s32 = e.value("min_settable", 0); 139 break; 140 default: 141 break; 142 } 143 144 pdr->range_field_format = e.value("range_field_format", 145 PLDM_RANGE_FIELD_FORMAT_UINT8); 146 pdr->range_field_support.byte = e.value("range_field_support", 0); 147 switch (pdr->range_field_format) 148 { 149 case PLDM_RANGE_FIELD_FORMAT_UINT8: 150 pdr->nominal_value.value_u8 = e.value("nominal_value", 0); 151 pdr->normal_max.value_u8 = e.value("normal_max", 0); 152 pdr->normal_min.value_u8 = e.value("normal_min", 0); 153 pdr->rated_max.value_u8 = e.value("rated_max", 0); 154 pdr->rated_min.value_u8 = e.value("rated_min", 0); 155 break; 156 case PLDM_RANGE_FIELD_FORMAT_SINT8: 157 pdr->nominal_value.value_s8 = e.value("nominal_value", 0); 158 pdr->normal_max.value_s8 = e.value("normal_max", 0); 159 pdr->normal_min.value_s8 = e.value("normal_min", 0); 160 pdr->rated_max.value_s8 = e.value("rated_max", 0); 161 pdr->rated_min.value_s8 = e.value("rated_min", 0); 162 break; 163 case PLDM_RANGE_FIELD_FORMAT_UINT16: 164 pdr->nominal_value.value_u16 = e.value("nominal_value", 0); 165 pdr->normal_max.value_u16 = e.value("normal_max", 0); 166 pdr->normal_min.value_u16 = e.value("normal_min", 0); 167 pdr->rated_max.value_u16 = e.value("rated_max", 0); 168 pdr->rated_min.value_u16 = e.value("rated_min", 0); 169 break; 170 case PLDM_RANGE_FIELD_FORMAT_SINT16: 171 pdr->nominal_value.value_s16 = e.value("nominal_value", 0); 172 pdr->normal_max.value_s16 = e.value("normal_max", 0); 173 pdr->normal_min.value_s16 = e.value("normal_min", 0); 174 pdr->rated_max.value_s16 = e.value("rated_max", 0); 175 pdr->rated_min.value_s16 = e.value("rated_min", 0); 176 break; 177 case PLDM_RANGE_FIELD_FORMAT_UINT32: 178 pdr->nominal_value.value_u32 = e.value("nominal_value", 0); 179 pdr->normal_max.value_u32 = e.value("normal_max", 0); 180 pdr->normal_min.value_u32 = e.value("normal_min", 0); 181 pdr->rated_max.value_u32 = e.value("rated_max", 0); 182 pdr->rated_min.value_u32 = e.value("rated_min", 0); 183 break; 184 case PLDM_RANGE_FIELD_FORMAT_SINT32: 185 pdr->nominal_value.value_s32 = e.value("nominal_value", 0); 186 pdr->normal_max.value_s32 = e.value("normal_max", 0); 187 pdr->normal_min.value_s32 = e.value("normal_min", 0); 188 pdr->rated_max.value_s32 = e.value("rated_max", 0); 189 pdr->rated_min.value_s32 = e.value("rated_min", 0); 190 break; 191 case PLDM_RANGE_FIELD_FORMAT_REAL32: 192 pdr->nominal_value.value_f32 = e.value("nominal_value", 0); 193 pdr->normal_max.value_f32 = e.value("normal_max", 0); 194 pdr->normal_min.value_f32 = e.value("normal_min", 0); 195 pdr->rated_max.value_f32 = e.value("rated_max", 0); 196 pdr->rated_min.value_f32 = e.value("rated_min", 0); 197 break; 198 default: 199 break; 200 } 201 202 auto dbusEntry = e.value("dbus", empty); 203 auto objectPath = dbusEntry.value("path", ""); 204 auto interface = dbusEntry.value("interface", ""); 205 auto propertyName = dbusEntry.value("property_name", ""); 206 auto propertyType = dbusEntry.value("property_type", ""); 207 208 pldm::responder::pdr_utils::DbusMappings dbusMappings{}; 209 pldm::responder::pdr_utils::DbusValMaps dbusValMaps{}; 210 pldm::utils::DBusMapping dbusMapping{}; 211 try 212 { 213 auto service = dBusIntf.getService(objectPath.c_str(), 214 interface.c_str()); 215 216 dbusMapping = pldm::utils::DBusMapping{objectPath, interface, 217 propertyName, propertyType}; 218 } 219 catch (const std::exception& e) 220 { 221 error( 222 "D-Bus object path does not exist, effecter ID: {EFFECTER_ID}", 223 "EFFECTER_ID", static_cast<uint16_t>(pdr->effecter_id)); 224 } 225 dbusMappings.emplace_back(std::move(dbusMapping)); 226 227 handler.addDbusObjMaps( 228 pdr->effecter_id, 229 std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps))); 230 231 pdr_utils::PdrEntry pdrEntry{}; 232 pdrEntry.data = entry.data(); 233 pdrEntry.size = sizeof(pldm_numeric_effecter_value_pdr); 234 repo.addRecord(pdrEntry); 235 } 236 } 237 238 } // namespace pdr_numeric_effecter 239 } // namespace responder 240 } // namespace pldm 241