George Liu | 456c9a2 | 2020-01-13 11:36:22 +0800 | [diff] [blame^] | 1 | #pragma once |
| 2 | |
| 3 | #include "libpldmresponder/pdr_utils.hpp" |
| 4 | |
| 5 | #include "libpldm/platform.h" |
| 6 | |
| 7 | namespace pldm |
| 8 | { |
| 9 | |
| 10 | namespace responder |
| 11 | { |
| 12 | |
| 13 | namespace pdr_numeric_effecter |
| 14 | { |
| 15 | |
| 16 | using Json = nlohmann::json; |
| 17 | |
| 18 | static const Json empty{}; |
| 19 | |
| 20 | /** @brief Parse PDR JSON file and generate numeric effecter PDR structure |
| 21 | * |
| 22 | * @param[in] json - the JSON Object with the numeric effecter PDR |
| 23 | * @param[out] handler - the Parser of PLDM command handler |
| 24 | * @param[out] repo - pdr::RepoInterface |
| 25 | * |
| 26 | */ |
| 27 | template <class Handler> |
| 28 | void generateNumericEffecterPDR(const Json& json, Handler& handler, |
| 29 | pdr_utils::RepoInterface& repo) |
| 30 | { |
| 31 | static const std::vector<Json> emptyList{}; |
| 32 | auto entries = json.value("entries", emptyList); |
| 33 | for (const auto& e : entries) |
| 34 | { |
| 35 | std::vector<uint8_t> entry{}; |
| 36 | entry.resize(sizeof(pldm_numeric_effecter_value_pdr)); |
| 37 | |
| 38 | pldm_numeric_effecter_value_pdr* pdr = |
| 39 | reinterpret_cast<pldm_numeric_effecter_value_pdr*>(entry.data()); |
| 40 | pdr->hdr.record_handle = 0; |
| 41 | pdr->hdr.version = 1; |
| 42 | pdr->hdr.type = PLDM_NUMERIC_EFFECTER_PDR; |
| 43 | pdr->hdr.record_change_num = 0; |
| 44 | pdr->hdr.length = |
| 45 | sizeof(pldm_numeric_effecter_value_pdr) - sizeof(pldm_pdr_hdr); |
| 46 | |
| 47 | pdr->terminus_handle = e.value("terminus_handle", 0); |
| 48 | pdr->effecter_id = handler.getNextEffecterId(); |
| 49 | pdr->entity_type = e.value("entity_type", 0); |
| 50 | pdr->entity_instance = e.value("entity_instance", 0); |
| 51 | pdr->container_id = e.value("container_id", 0); |
| 52 | pdr->effecter_semantic_id = e.value("effecter_semantic_id", 0); |
| 53 | pdr->effecter_init = e.value("effecter_init", PLDM_NO_INIT); |
| 54 | pdr->effecter_auxiliary_names = e.value("effecter_init", false); |
| 55 | pdr->base_unit = e.value("base_unit", 0); |
| 56 | pdr->unit_modifier = e.value("unit_modifier", 0); |
| 57 | pdr->rate_unit = e.value("rate_unit", 0); |
| 58 | pdr->base_oem_unit_handle = e.value("base_oem_unit_handle", 0); |
| 59 | pdr->aux_unit = e.value("aux_unit", 0); |
| 60 | pdr->aux_unit_modifier = e.value("aux_unit_modifier", 0); |
| 61 | pdr->aux_oem_unit_handle = e.value("aux_oem_unit_handle", 0); |
| 62 | pdr->aux_rate_unit = e.value("aux_rate_unit", 0); |
| 63 | pdr->is_linear = e.value("is_linear", true); |
| 64 | pdr->effecter_data_size = |
| 65 | e.value("effecter_data_size", PLDM_EFFECTER_DATA_SIZE_UINT8); |
| 66 | pdr->resolution = e.value("effecter_resolution_init", 1.00); |
| 67 | pdr->offset = e.value("offset", 0.00); |
| 68 | pdr->accuracy = e.value("accuracy", 0); |
| 69 | pdr->plus_tolerance = e.value("plus_tolerance", 0); |
| 70 | pdr->minus_tolerance = e.value("minus_tolerance", 0); |
| 71 | pdr->state_transition_interval = |
| 72 | e.value("state_transition_interval", 0.00); |
| 73 | pdr->transition_interval = e.value("transition_interval", 0.00); |
| 74 | switch (pdr->effecter_data_size) |
| 75 | { |
| 76 | case PLDM_EFFECTER_DATA_SIZE_UINT8: |
| 77 | pdr->max_set_table.value_u8 = e.value("max_set_table", 0); |
| 78 | pdr->min_set_table.value_u8 = e.value("min_set_table", 0); |
| 79 | break; |
| 80 | case PLDM_EFFECTER_DATA_SIZE_SINT8: |
| 81 | pdr->max_set_table.value_s8 = e.value("max_set_table", 0); |
| 82 | pdr->min_set_table.value_s8 = e.value("min_set_table", 0); |
| 83 | break; |
| 84 | case PLDM_EFFECTER_DATA_SIZE_UINT16: |
| 85 | pdr->max_set_table.value_u16 = e.value("max_set_table", 0); |
| 86 | pdr->min_set_table.value_u16 = e.value("min_set_table", 0); |
| 87 | break; |
| 88 | case PLDM_EFFECTER_DATA_SIZE_SINT16: |
| 89 | pdr->max_set_table.value_s16 = e.value("max_set_table", 0); |
| 90 | pdr->min_set_table.value_s16 = e.value("min_set_table", 0); |
| 91 | break; |
| 92 | case PLDM_EFFECTER_DATA_SIZE_UINT32: |
| 93 | pdr->max_set_table.value_u32 = e.value("max_set_table", 0); |
| 94 | pdr->min_set_table.value_u32 = e.value("min_set_table", 0); |
| 95 | break; |
| 96 | case PLDM_EFFECTER_DATA_SIZE_SINT32: |
| 97 | pdr->max_set_table.value_s32 = e.value("max_set_table", 0); |
| 98 | pdr->min_set_table.value_s32 = e.value("min_set_table", 0); |
| 99 | break; |
| 100 | default: |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | pdr->range_field_format = |
| 105 | e.value("range_field_format", PLDM_RANGE_FIELD_FORMAT_UINT8); |
| 106 | pdr->range_field_support.byte = e.value("range_field_support", 0); |
| 107 | switch (pdr->range_field_format) |
| 108 | { |
| 109 | case PLDM_RANGE_FIELD_FORMAT_UINT8: |
| 110 | pdr->nominal_value.value_u8 = e.value("nominal_value", 0); |
| 111 | pdr->normal_max.value_u8 = e.value("normal_max", 0); |
| 112 | pdr->normal_min.value_u8 = e.value("normal_min", 0); |
| 113 | pdr->rated_max.value_u8 = e.value("rated_max", 0); |
| 114 | pdr->rated_min.value_u8 = e.value("rated_min", 0); |
| 115 | break; |
| 116 | case PLDM_RANGE_FIELD_FORMAT_SINT8: |
| 117 | pdr->nominal_value.value_s8 = e.value("nominal_value", 0); |
| 118 | pdr->normal_max.value_s8 = e.value("normal_max", 0); |
| 119 | pdr->normal_min.value_s8 = e.value("normal_min", 0); |
| 120 | pdr->rated_max.value_s8 = e.value("rated_max", 0); |
| 121 | pdr->rated_min.value_s8 = e.value("rated_min", 0); |
| 122 | break; |
| 123 | case PLDM_RANGE_FIELD_FORMAT_UINT16: |
| 124 | pdr->nominal_value.value_u16 = e.value("nominal_value", 0); |
| 125 | pdr->normal_max.value_u16 = e.value("normal_max", 0); |
| 126 | pdr->normal_min.value_u16 = e.value("normal_min", 0); |
| 127 | pdr->rated_max.value_u16 = e.value("rated_max", 0); |
| 128 | pdr->rated_min.value_u16 = e.value("rated_min", 0); |
| 129 | break; |
| 130 | case PLDM_RANGE_FIELD_FORMAT_SINT16: |
| 131 | pdr->nominal_value.value_s16 = e.value("nominal_value", 0); |
| 132 | pdr->normal_max.value_s16 = e.value("normal_max", 0); |
| 133 | pdr->normal_min.value_s16 = e.value("normal_min", 0); |
| 134 | pdr->rated_max.value_s16 = e.value("rated_max", 0); |
| 135 | pdr->rated_min.value_s16 = e.value("rated_min", 0); |
| 136 | break; |
| 137 | case PLDM_RANGE_FIELD_FORMAT_UINT32: |
| 138 | pdr->nominal_value.value_u32 = e.value("nominal_value", 0); |
| 139 | pdr->normal_max.value_u32 = e.value("normal_max", 0); |
| 140 | pdr->normal_min.value_u32 = e.value("normal_min", 0); |
| 141 | pdr->rated_max.value_u32 = e.value("rated_max", 0); |
| 142 | pdr->rated_min.value_u32 = e.value("rated_min", 0); |
| 143 | break; |
| 144 | case PLDM_RANGE_FIELD_FORMAT_SINT32: |
| 145 | pdr->nominal_value.value_s32 = e.value("nominal_value", 0); |
| 146 | pdr->normal_max.value_s32 = e.value("normal_max", 0); |
| 147 | pdr->normal_min.value_s32 = e.value("normal_min", 0); |
| 148 | pdr->rated_max.value_s32 = e.value("rated_max", 0); |
| 149 | pdr->rated_min.value_s32 = e.value("rated_min", 0); |
| 150 | break; |
| 151 | case PLDM_RANGE_FIELD_FORMAT_REAL32: |
| 152 | pdr->nominal_value.value_f32 = e.value("nominal_value", 0); |
| 153 | pdr->normal_max.value_f32 = e.value("normal_max", 0); |
| 154 | pdr->normal_min.value_f32 = e.value("normal_min", 0); |
| 155 | pdr->rated_max.value_f32 = e.value("rated_max", 0); |
| 156 | pdr->rated_min.value_f32 = e.value("rated_min", 0); |
| 157 | break; |
| 158 | default: |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | auto dbusEntry = e.value("dbus", empty); |
| 163 | auto objectPath = dbusEntry.value("path", ""); |
| 164 | auto interface = dbusEntry.value("interface", ""); |
| 165 | auto propertyName = dbusEntry.value("property_name", ""); |
| 166 | auto propertyType = dbusEntry.value("property_type", ""); |
| 167 | pldm::utils::DBusMapping dbusMapping{objectPath, interface, |
| 168 | propertyName, propertyType}; |
| 169 | DbusMappings dbusMappings{}; |
| 170 | DbusValMaps dbusValMaps{}; |
| 171 | dbusMappings.emplace_back(std::move(dbusMapping)); |
| 172 | handler.addDbusObjMaps( |
| 173 | pdr->effecter_id, |
| 174 | std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps))); |
| 175 | |
| 176 | pdr_utils::PdrEntry pdrEntry{}; |
| 177 | pdrEntry.data = entry.data(); |
| 178 | pdrEntry.size = sizeof(pldm_numeric_effecter_value_pdr); |
| 179 | repo.addRecord(pdrEntry); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | } // namespace pdr_numeric_effecter |
| 184 | } // namespace responder |
| 185 | } // namespace pldm |