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