blob: 88d7f8aaeb8210a350e765edf71b1b8f1c282795 [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
Riya Dixit49cfb132023-03-02 04:26:53 -06007#include <phosphor-logging/lg2.hpp>
8
9PHOSPHOR_LOG2_USING;
10
George Liu456c9a22020-01-13 11:36:22 +080011namespace pldm
12{
George Liu456c9a22020-01-13 11:36:22 +080013namespace responder
14{
George Liu456c9a22020-01-13 11:36:22 +080015namespace pdr_numeric_effecter
16{
George Liu456c9a22020-01-13 11:36:22 +080017using Json = nlohmann::json;
18
19static 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 Liu36e81352020-07-01 14:40:30 +080028template <class DBusInterface, class Handler>
29void generateNumericEffecterPDR(const DBusInterface& dBusIntf, const Json& json,
30 Handler& handler,
George Liu456c9a22020-01-13 11:36:22 +080031 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 Edabcf91ac2021-03-14 13:50:48 +053042 if (!pdr)
43 {
Riya Dixit49cfb132023-03-02 04:26:53 -060044 error("Failed to get numeric effecter PDR.");
Manojkiran Edabcf91ac2021-03-14 13:50:48 +053045 continue;
46 }
George Liu456c9a22020-01-13 11:36:22 +080047 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 Williams6da4f912023-05-10 07:50:53 -050051 pdr->hdr.length = sizeof(pldm_numeric_effecter_value_pdr) -
52 sizeof(pldm_pdr_hdr);
George Liu456c9a22020-01-13 11:36:22 +080053
54 pdr->terminus_handle = e.value("terminus_handle", 0);
55 pdr->effecter_id = handler.getNextEffecterId();
George Liuc4ea6a92020-07-14 15:48:44 +080056
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);
Pavithra Barithaya5f213472022-08-29 02:20:19 -050076
77 // do not create the PDR when the FRU or the entity path is not
78 // present
79 if (!pdr->entity_type)
80 {
Riya Dixit797f3382023-08-22 22:27:51 -050081 error("The entity path for the FRU is not present.");
Pavithra Barithaya5f213472022-08-29 02:20:19 -050082 continue;
83 }
George Liuc4ea6a92020-07-14 15:48:44 +080084 }
85 }
86 catch (const std::exception& ex)
87 {
88 pdr->entity_type = e.value("type", 0);
89 pdr->entity_instance = e.value("instance", 0);
90 pdr->container_id = e.value("container", 0);
91 }
92
George Liu456c9a22020-01-13 11:36:22 +080093 pdr->effecter_semantic_id = e.value("effecter_semantic_id", 0);
94 pdr->effecter_init = e.value("effecter_init", PLDM_NO_INIT);
95 pdr->effecter_auxiliary_names = e.value("effecter_init", false);
96 pdr->base_unit = e.value("base_unit", 0);
97 pdr->unit_modifier = e.value("unit_modifier", 0);
98 pdr->rate_unit = e.value("rate_unit", 0);
99 pdr->base_oem_unit_handle = e.value("base_oem_unit_handle", 0);
100 pdr->aux_unit = e.value("aux_unit", 0);
101 pdr->aux_unit_modifier = e.value("aux_unit_modifier", 0);
102 pdr->aux_oem_unit_handle = e.value("aux_oem_unit_handle", 0);
103 pdr->aux_rate_unit = e.value("aux_rate_unit", 0);
104 pdr->is_linear = e.value("is_linear", true);
Patrick Williams6da4f912023-05-10 07:50:53 -0500105 pdr->effecter_data_size = e.value("effecter_data_size",
106 PLDM_EFFECTER_DATA_SIZE_UINT8);
George Liu456c9a22020-01-13 11:36:22 +0800107 pdr->resolution = e.value("effecter_resolution_init", 1.00);
108 pdr->offset = e.value("offset", 0.00);
109 pdr->accuracy = e.value("accuracy", 0);
110 pdr->plus_tolerance = e.value("plus_tolerance", 0);
111 pdr->minus_tolerance = e.value("minus_tolerance", 0);
Patrick Williams6da4f912023-05-10 07:50:53 -0500112 pdr->state_transition_interval = e.value("state_transition_interval",
113 0.00);
George Liu456c9a22020-01-13 11:36:22 +0800114 pdr->transition_interval = e.value("transition_interval", 0.00);
115 switch (pdr->effecter_data_size)
116 {
117 case PLDM_EFFECTER_DATA_SIZE_UINT8:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530118 pdr->max_settable.value_u8 = e.value("max_settable", 0);
119 pdr->min_settable.value_u8 = e.value("min_settable", 0);
George Liu456c9a22020-01-13 11:36:22 +0800120 break;
121 case PLDM_EFFECTER_DATA_SIZE_SINT8:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530122 pdr->max_settable.value_s8 = e.value("max_settable", 0);
123 pdr->min_settable.value_s8 = e.value("min_settable", 0);
George Liu456c9a22020-01-13 11:36:22 +0800124 break;
125 case PLDM_EFFECTER_DATA_SIZE_UINT16:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530126 pdr->max_settable.value_u16 = e.value("max_settable", 0);
127 pdr->min_settable.value_u16 = e.value("min_settable", 0);
George Liu456c9a22020-01-13 11:36:22 +0800128 break;
129 case PLDM_EFFECTER_DATA_SIZE_SINT16:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530130 pdr->max_settable.value_s16 = e.value("max_settable", 0);
131 pdr->min_settable.value_s16 = e.value("min_settable", 0);
George Liu456c9a22020-01-13 11:36:22 +0800132 break;
133 case PLDM_EFFECTER_DATA_SIZE_UINT32:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530134 pdr->max_settable.value_u32 = e.value("max_settable", 0);
135 pdr->min_settable.value_u32 = e.value("min_settable", 0);
George Liu456c9a22020-01-13 11:36:22 +0800136 break;
137 case PLDM_EFFECTER_DATA_SIZE_SINT32:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530138 pdr->max_settable.value_s32 = e.value("max_settable", 0);
139 pdr->min_settable.value_s32 = e.value("min_settable", 0);
George Liu456c9a22020-01-13 11:36:22 +0800140 break;
141 default:
142 break;
143 }
144
Patrick Williams6da4f912023-05-10 07:50:53 -0500145 pdr->range_field_format = e.value("range_field_format",
146 PLDM_RANGE_FIELD_FORMAT_UINT8);
George Liu456c9a22020-01-13 11:36:22 +0800147 pdr->range_field_support.byte = e.value("range_field_support", 0);
148 switch (pdr->range_field_format)
149 {
150 case PLDM_RANGE_FIELD_FORMAT_UINT8:
151 pdr->nominal_value.value_u8 = e.value("nominal_value", 0);
152 pdr->normal_max.value_u8 = e.value("normal_max", 0);
153 pdr->normal_min.value_u8 = e.value("normal_min", 0);
154 pdr->rated_max.value_u8 = e.value("rated_max", 0);
155 pdr->rated_min.value_u8 = e.value("rated_min", 0);
156 break;
157 case PLDM_RANGE_FIELD_FORMAT_SINT8:
158 pdr->nominal_value.value_s8 = e.value("nominal_value", 0);
159 pdr->normal_max.value_s8 = e.value("normal_max", 0);
160 pdr->normal_min.value_s8 = e.value("normal_min", 0);
161 pdr->rated_max.value_s8 = e.value("rated_max", 0);
162 pdr->rated_min.value_s8 = e.value("rated_min", 0);
163 break;
164 case PLDM_RANGE_FIELD_FORMAT_UINT16:
165 pdr->nominal_value.value_u16 = e.value("nominal_value", 0);
166 pdr->normal_max.value_u16 = e.value("normal_max", 0);
167 pdr->normal_min.value_u16 = e.value("normal_min", 0);
168 pdr->rated_max.value_u16 = e.value("rated_max", 0);
169 pdr->rated_min.value_u16 = e.value("rated_min", 0);
170 break;
171 case PLDM_RANGE_FIELD_FORMAT_SINT16:
172 pdr->nominal_value.value_s16 = e.value("nominal_value", 0);
173 pdr->normal_max.value_s16 = e.value("normal_max", 0);
174 pdr->normal_min.value_s16 = e.value("normal_min", 0);
175 pdr->rated_max.value_s16 = e.value("rated_max", 0);
176 pdr->rated_min.value_s16 = e.value("rated_min", 0);
177 break;
178 case PLDM_RANGE_FIELD_FORMAT_UINT32:
179 pdr->nominal_value.value_u32 = e.value("nominal_value", 0);
180 pdr->normal_max.value_u32 = e.value("normal_max", 0);
181 pdr->normal_min.value_u32 = e.value("normal_min", 0);
182 pdr->rated_max.value_u32 = e.value("rated_max", 0);
183 pdr->rated_min.value_u32 = e.value("rated_min", 0);
184 break;
185 case PLDM_RANGE_FIELD_FORMAT_SINT32:
186 pdr->nominal_value.value_s32 = e.value("nominal_value", 0);
187 pdr->normal_max.value_s32 = e.value("normal_max", 0);
188 pdr->normal_min.value_s32 = e.value("normal_min", 0);
189 pdr->rated_max.value_s32 = e.value("rated_max", 0);
190 pdr->rated_min.value_s32 = e.value("rated_min", 0);
191 break;
192 case PLDM_RANGE_FIELD_FORMAT_REAL32:
193 pdr->nominal_value.value_f32 = e.value("nominal_value", 0);
194 pdr->normal_max.value_f32 = e.value("normal_max", 0);
195 pdr->normal_min.value_f32 = e.value("normal_min", 0);
196 pdr->rated_max.value_f32 = e.value("rated_max", 0);
197 pdr->rated_min.value_f32 = e.value("rated_min", 0);
198 break;
199 default:
200 break;
201 }
202
203 auto dbusEntry = e.value("dbus", empty);
204 auto objectPath = dbusEntry.value("path", "");
205 auto interface = dbusEntry.value("interface", "");
206 auto propertyName = dbusEntry.value("property_name", "");
207 auto propertyType = dbusEntry.value("property_type", "");
George Liu36e81352020-07-01 14:40:30 +0800208
Brad Bishop5079ac42021-08-19 18:35:06 -0400209 pldm::responder::pdr_utils::DbusMappings dbusMappings{};
210 pldm::responder::pdr_utils::DbusValMaps dbusValMaps{};
George Liu821ebc42021-01-26 14:36:11 +0800211 pldm::utils::DBusMapping dbusMapping{};
George Liu36e81352020-07-01 14:40:30 +0800212 try
213 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500214 auto service = dBusIntf.getService(objectPath.c_str(),
215 interface.c_str());
George Liu821ebc42021-01-26 14:36:11 +0800216
217 dbusMapping = pldm::utils::DBusMapping{objectPath, interface,
218 propertyName, propertyType};
George Liu36e81352020-07-01 14:40:30 +0800219 }
220 catch (const std::exception& e)
221 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600222 error(
223 "D-Bus object path does not exist, effecter ID: {EFFECTER_ID}",
224 "EFFECTER_ID", static_cast<uint16_t>(pdr->effecter_id));
George Liu36e81352020-07-01 14:40:30 +0800225 }
George Liu456c9a22020-01-13 11:36:22 +0800226 dbusMappings.emplace_back(std::move(dbusMapping));
George Liu821ebc42021-01-26 14:36:11 +0800227
George Liu456c9a22020-01-13 11:36:22 +0800228 handler.addDbusObjMaps(
229 pdr->effecter_id,
230 std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)));
231
232 pdr_utils::PdrEntry pdrEntry{};
233 pdrEntry.data = entry.data();
234 pdrEntry.size = sizeof(pldm_numeric_effecter_value_pdr);
235 repo.addRecord(pdrEntry);
236 }
237}
238
239} // namespace pdr_numeric_effecter
240} // namespace responder
241} // namespace pldm