blob: df5faf98185fd2fa109dc6362f155885e35386b6 [file] [log] [blame]
George Liu456c9a22020-01-13 11:36:22 +08001#pragma once
2
George Liu6492f522020-06-16 10:34:05 +08003#include "libpldmresponder/pdr_utils.hpp"
4
George Liuc453e162022-12-21 17:16:23 +08005#include <libpldm/platform.h>
6
George Liu456c9a22020-01-13 11:36:22 +08007namespace pldm
8{
George Liu456c9a22020-01-13 11:36:22 +08009namespace responder
10{
George Liu456c9a22020-01-13 11:36:22 +080011namespace pdr_numeric_effecter
12{
George Liu456c9a22020-01-13 11:36:22 +080013using Json = nlohmann::json;
14
15static const Json empty{};
16
17/** @brief Parse PDR JSON file and generate numeric effecter PDR structure
18 *
19 * @param[in] json - the JSON Object with the numeric effecter PDR
20 * @param[out] handler - the Parser of PLDM command handler
21 * @param[out] repo - pdr::RepoInterface
22 *
23 */
George Liu36e81352020-07-01 14:40:30 +080024template <class DBusInterface, class Handler>
25void generateNumericEffecterPDR(const DBusInterface& dBusIntf, const Json& json,
26 Handler& handler,
George Liu456c9a22020-01-13 11:36:22 +080027 pdr_utils::RepoInterface& repo)
28{
29 static const std::vector<Json> emptyList{};
30 auto entries = json.value("entries", emptyList);
31 for (const auto& e : entries)
32 {
33 std::vector<uint8_t> entry{};
34 entry.resize(sizeof(pldm_numeric_effecter_value_pdr));
35
36 pldm_numeric_effecter_value_pdr* pdr =
37 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(entry.data());
Manojkiran Edabcf91ac2021-03-14 13:50:48 +053038 if (!pdr)
39 {
40 std::cerr << "Failed to get numeric effecter PDR.\n";
41 continue;
42 }
George Liu456c9a22020-01-13 11:36:22 +080043 pdr->hdr.record_handle = 0;
44 pdr->hdr.version = 1;
45 pdr->hdr.type = PLDM_NUMERIC_EFFECTER_PDR;
46 pdr->hdr.record_change_num = 0;
47 pdr->hdr.length =
48 sizeof(pldm_numeric_effecter_value_pdr) - sizeof(pldm_pdr_hdr);
49
50 pdr->terminus_handle = e.value("terminus_handle", 0);
51 pdr->effecter_id = handler.getNextEffecterId();
George Liuc4ea6a92020-07-14 15:48:44 +080052
53 try
54 {
55 std::string entity_path = e.value("entity_path", "");
56 auto& associatedEntityMap = handler.getAssociateEntityMap();
57 if (entity_path != "" && associatedEntityMap.find(entity_path) !=
58 associatedEntityMap.end())
59 {
60 pdr->entity_type =
61 associatedEntityMap.at(entity_path).entity_type;
62 pdr->entity_instance =
63 associatedEntityMap.at(entity_path).entity_instance_num;
64 pdr->container_id =
65 associatedEntityMap.at(entity_path).entity_container_id;
66 }
67 else
68 {
69 pdr->entity_type = e.value("type", 0);
70 pdr->entity_instance = e.value("instance", 0);
71 pdr->container_id = e.value("container", 0);
Pavithra Barithaya5f213472022-08-29 02:20:19 -050072
73 // do not create the PDR when the FRU or the entity path is not
74 // present
75 if (!pdr->entity_type)
76 {
77 std::cerr << "The entity path for the FRU is not present."
78 << std::endl;
79 continue;
80 }
George Liuc4ea6a92020-07-14 15:48:44 +080081 }
82 }
83 catch (const std::exception& ex)
84 {
85 pdr->entity_type = e.value("type", 0);
86 pdr->entity_instance = e.value("instance", 0);
87 pdr->container_id = e.value("container", 0);
88 }
89
George Liu456c9a22020-01-13 11:36:22 +080090 pdr->effecter_semantic_id = e.value("effecter_semantic_id", 0);
91 pdr->effecter_init = e.value("effecter_init", PLDM_NO_INIT);
92 pdr->effecter_auxiliary_names = e.value("effecter_init", false);
93 pdr->base_unit = e.value("base_unit", 0);
94 pdr->unit_modifier = e.value("unit_modifier", 0);
95 pdr->rate_unit = e.value("rate_unit", 0);
96 pdr->base_oem_unit_handle = e.value("base_oem_unit_handle", 0);
97 pdr->aux_unit = e.value("aux_unit", 0);
98 pdr->aux_unit_modifier = e.value("aux_unit_modifier", 0);
99 pdr->aux_oem_unit_handle = e.value("aux_oem_unit_handle", 0);
100 pdr->aux_rate_unit = e.value("aux_rate_unit", 0);
101 pdr->is_linear = e.value("is_linear", true);
102 pdr->effecter_data_size =
103 e.value("effecter_data_size", PLDM_EFFECTER_DATA_SIZE_UINT8);
104 pdr->resolution = e.value("effecter_resolution_init", 1.00);
105 pdr->offset = e.value("offset", 0.00);
106 pdr->accuracy = e.value("accuracy", 0);
107 pdr->plus_tolerance = e.value("plus_tolerance", 0);
108 pdr->minus_tolerance = e.value("minus_tolerance", 0);
109 pdr->state_transition_interval =
110 e.value("state_transition_interval", 0.00);
111 pdr->transition_interval = e.value("transition_interval", 0.00);
112 switch (pdr->effecter_data_size)
113 {
114 case PLDM_EFFECTER_DATA_SIZE_UINT8:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530115 pdr->max_settable.value_u8 = e.value("max_settable", 0);
116 pdr->min_settable.value_u8 = e.value("min_settable", 0);
George Liu456c9a22020-01-13 11:36:22 +0800117 break;
118 case PLDM_EFFECTER_DATA_SIZE_SINT8:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530119 pdr->max_settable.value_s8 = e.value("max_settable", 0);
120 pdr->min_settable.value_s8 = e.value("min_settable", 0);
George Liu456c9a22020-01-13 11:36:22 +0800121 break;
122 case PLDM_EFFECTER_DATA_SIZE_UINT16:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530123 pdr->max_settable.value_u16 = e.value("max_settable", 0);
124 pdr->min_settable.value_u16 = e.value("min_settable", 0);
George Liu456c9a22020-01-13 11:36:22 +0800125 break;
126 case PLDM_EFFECTER_DATA_SIZE_SINT16:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530127 pdr->max_settable.value_s16 = e.value("max_settable", 0);
128 pdr->min_settable.value_s16 = e.value("min_settable", 0);
George Liu456c9a22020-01-13 11:36:22 +0800129 break;
130 case PLDM_EFFECTER_DATA_SIZE_UINT32:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530131 pdr->max_settable.value_u32 = e.value("max_settable", 0);
132 pdr->min_settable.value_u32 = e.value("min_settable", 0);
George Liu456c9a22020-01-13 11:36:22 +0800133 break;
134 case PLDM_EFFECTER_DATA_SIZE_SINT32:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530135 pdr->max_settable.value_s32 = e.value("max_settable", 0);
136 pdr->min_settable.value_s32 = e.value("min_settable", 0);
George Liu456c9a22020-01-13 11:36:22 +0800137 break;
138 default:
139 break;
140 }
141
142 pdr->range_field_format =
143 e.value("range_field_format", PLDM_RANGE_FIELD_FORMAT_UINT8);
144 pdr->range_field_support.byte = e.value("range_field_support", 0);
145 switch (pdr->range_field_format)
146 {
147 case PLDM_RANGE_FIELD_FORMAT_UINT8:
148 pdr->nominal_value.value_u8 = e.value("nominal_value", 0);
149 pdr->normal_max.value_u8 = e.value("normal_max", 0);
150 pdr->normal_min.value_u8 = e.value("normal_min", 0);
151 pdr->rated_max.value_u8 = e.value("rated_max", 0);
152 pdr->rated_min.value_u8 = e.value("rated_min", 0);
153 break;
154 case PLDM_RANGE_FIELD_FORMAT_SINT8:
155 pdr->nominal_value.value_s8 = e.value("nominal_value", 0);
156 pdr->normal_max.value_s8 = e.value("normal_max", 0);
157 pdr->normal_min.value_s8 = e.value("normal_min", 0);
158 pdr->rated_max.value_s8 = e.value("rated_max", 0);
159 pdr->rated_min.value_s8 = e.value("rated_min", 0);
160 break;
161 case PLDM_RANGE_FIELD_FORMAT_UINT16:
162 pdr->nominal_value.value_u16 = e.value("nominal_value", 0);
163 pdr->normal_max.value_u16 = e.value("normal_max", 0);
164 pdr->normal_min.value_u16 = e.value("normal_min", 0);
165 pdr->rated_max.value_u16 = e.value("rated_max", 0);
166 pdr->rated_min.value_u16 = e.value("rated_min", 0);
167 break;
168 case PLDM_RANGE_FIELD_FORMAT_SINT16:
169 pdr->nominal_value.value_s16 = e.value("nominal_value", 0);
170 pdr->normal_max.value_s16 = e.value("normal_max", 0);
171 pdr->normal_min.value_s16 = e.value("normal_min", 0);
172 pdr->rated_max.value_s16 = e.value("rated_max", 0);
173 pdr->rated_min.value_s16 = e.value("rated_min", 0);
174 break;
175 case PLDM_RANGE_FIELD_FORMAT_UINT32:
176 pdr->nominal_value.value_u32 = e.value("nominal_value", 0);
177 pdr->normal_max.value_u32 = e.value("normal_max", 0);
178 pdr->normal_min.value_u32 = e.value("normal_min", 0);
179 pdr->rated_max.value_u32 = e.value("rated_max", 0);
180 pdr->rated_min.value_u32 = e.value("rated_min", 0);
181 break;
182 case PLDM_RANGE_FIELD_FORMAT_SINT32:
183 pdr->nominal_value.value_s32 = e.value("nominal_value", 0);
184 pdr->normal_max.value_s32 = e.value("normal_max", 0);
185 pdr->normal_min.value_s32 = e.value("normal_min", 0);
186 pdr->rated_max.value_s32 = e.value("rated_max", 0);
187 pdr->rated_min.value_s32 = e.value("rated_min", 0);
188 break;
189 case PLDM_RANGE_FIELD_FORMAT_REAL32:
190 pdr->nominal_value.value_f32 = e.value("nominal_value", 0);
191 pdr->normal_max.value_f32 = e.value("normal_max", 0);
192 pdr->normal_min.value_f32 = e.value("normal_min", 0);
193 pdr->rated_max.value_f32 = e.value("rated_max", 0);
194 pdr->rated_min.value_f32 = e.value("rated_min", 0);
195 break;
196 default:
197 break;
198 }
199
200 auto dbusEntry = e.value("dbus", empty);
201 auto objectPath = dbusEntry.value("path", "");
202 auto interface = dbusEntry.value("interface", "");
203 auto propertyName = dbusEntry.value("property_name", "");
204 auto propertyType = dbusEntry.value("property_type", "");
George Liu36e81352020-07-01 14:40:30 +0800205
Brad Bishop5079ac42021-08-19 18:35:06 -0400206 pldm::responder::pdr_utils::DbusMappings dbusMappings{};
207 pldm::responder::pdr_utils::DbusValMaps dbusValMaps{};
George Liu821ebc42021-01-26 14:36:11 +0800208 pldm::utils::DBusMapping dbusMapping{};
George Liu36e81352020-07-01 14:40:30 +0800209 try
210 {
211 auto service =
212 dBusIntf.getService(objectPath.c_str(), interface.c_str());
George Liu821ebc42021-01-26 14:36:11 +0800213
214 dbusMapping = pldm::utils::DBusMapping{objectPath, interface,
215 propertyName, propertyType};
George Liu36e81352020-07-01 14:40:30 +0800216 }
217 catch (const std::exception& e)
218 {
George Liu821ebc42021-01-26 14:36:11 +0800219 std::cerr << "D-Bus object path does not exist, effecter ID: "
220 << pdr->effecter_id << "\n";
George Liu36e81352020-07-01 14:40:30 +0800221 }
George Liu456c9a22020-01-13 11:36:22 +0800222 dbusMappings.emplace_back(std::move(dbusMapping));
George Liu821ebc42021-01-26 14:36:11 +0800223
George Liu456c9a22020-01-13 11:36:22 +0800224 handler.addDbusObjMaps(
225 pdr->effecter_id,
226 std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)));
227
228 pdr_utils::PdrEntry pdrEntry{};
229 pdrEntry.data = entry.data();
230 pdrEntry.size = sizeof(pldm_numeric_effecter_value_pdr);
231 repo.addRecord(pdrEntry);
232 }
233}
234
235} // namespace pdr_numeric_effecter
236} // namespace responder
237} // namespace pldm