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                     std::cerr << "The entity path for the FRU is not present."
82                               << std::endl;
83                     continue;
84                 }
85             }
86         }
87         catch (const std::exception& ex)
88         {
89             pdr->entity_type = e.value("type", 0);
90             pdr->entity_instance = e.value("instance", 0);
91             pdr->container_id = e.value("container", 0);
92         }
93 
94         pdr->effecter_semantic_id = e.value("effecter_semantic_id", 0);
95         pdr->effecter_init = e.value("effecter_init", PLDM_NO_INIT);
96         pdr->effecter_auxiliary_names = e.value("effecter_init", false);
97         pdr->base_unit = e.value("base_unit", 0);
98         pdr->unit_modifier = e.value("unit_modifier", 0);
99         pdr->rate_unit = e.value("rate_unit", 0);
100         pdr->base_oem_unit_handle = e.value("base_oem_unit_handle", 0);
101         pdr->aux_unit = e.value("aux_unit", 0);
102         pdr->aux_unit_modifier = e.value("aux_unit_modifier", 0);
103         pdr->aux_oem_unit_handle = e.value("aux_oem_unit_handle", 0);
104         pdr->aux_rate_unit = e.value("aux_rate_unit", 0);
105         pdr->is_linear = e.value("is_linear", true);
106         pdr->effecter_data_size = e.value("effecter_data_size",
107                                           PLDM_EFFECTER_DATA_SIZE_UINT8);
108         pdr->resolution = e.value("effecter_resolution_init", 1.00);
109         pdr->offset = e.value("offset", 0.00);
110         pdr->accuracy = e.value("accuracy", 0);
111         pdr->plus_tolerance = e.value("plus_tolerance", 0);
112         pdr->minus_tolerance = e.value("minus_tolerance", 0);
113         pdr->state_transition_interval = e.value("state_transition_interval",
114                                                  0.00);
115         pdr->transition_interval = e.value("transition_interval", 0.00);
116         switch (pdr->effecter_data_size)
117         {
118             case PLDM_EFFECTER_DATA_SIZE_UINT8:
119                 pdr->max_settable.value_u8 = e.value("max_settable", 0);
120                 pdr->min_settable.value_u8 = e.value("min_settable", 0);
121                 break;
122             case PLDM_EFFECTER_DATA_SIZE_SINT8:
123                 pdr->max_settable.value_s8 = e.value("max_settable", 0);
124                 pdr->min_settable.value_s8 = e.value("min_settable", 0);
125                 break;
126             case PLDM_EFFECTER_DATA_SIZE_UINT16:
127                 pdr->max_settable.value_u16 = e.value("max_settable", 0);
128                 pdr->min_settable.value_u16 = e.value("min_settable", 0);
129                 break;
130             case PLDM_EFFECTER_DATA_SIZE_SINT16:
131                 pdr->max_settable.value_s16 = e.value("max_settable", 0);
132                 pdr->min_settable.value_s16 = e.value("min_settable", 0);
133                 break;
134             case PLDM_EFFECTER_DATA_SIZE_UINT32:
135                 pdr->max_settable.value_u32 = e.value("max_settable", 0);
136                 pdr->min_settable.value_u32 = e.value("min_settable", 0);
137                 break;
138             case PLDM_EFFECTER_DATA_SIZE_SINT32:
139                 pdr->max_settable.value_s32 = e.value("max_settable", 0);
140                 pdr->min_settable.value_s32 = e.value("min_settable", 0);
141                 break;
142             default:
143                 break;
144         }
145 
146         pdr->range_field_format = e.value("range_field_format",
147                                           PLDM_RANGE_FIELD_FORMAT_UINT8);
148         pdr->range_field_support.byte = e.value("range_field_support", 0);
149         switch (pdr->range_field_format)
150         {
151             case PLDM_RANGE_FIELD_FORMAT_UINT8:
152                 pdr->nominal_value.value_u8 = e.value("nominal_value", 0);
153                 pdr->normal_max.value_u8 = e.value("normal_max", 0);
154                 pdr->normal_min.value_u8 = e.value("normal_min", 0);
155                 pdr->rated_max.value_u8 = e.value("rated_max", 0);
156                 pdr->rated_min.value_u8 = e.value("rated_min", 0);
157                 break;
158             case PLDM_RANGE_FIELD_FORMAT_SINT8:
159                 pdr->nominal_value.value_s8 = e.value("nominal_value", 0);
160                 pdr->normal_max.value_s8 = e.value("normal_max", 0);
161                 pdr->normal_min.value_s8 = e.value("normal_min", 0);
162                 pdr->rated_max.value_s8 = e.value("rated_max", 0);
163                 pdr->rated_min.value_s8 = e.value("rated_min", 0);
164                 break;
165             case PLDM_RANGE_FIELD_FORMAT_UINT16:
166                 pdr->nominal_value.value_u16 = e.value("nominal_value", 0);
167                 pdr->normal_max.value_u16 = e.value("normal_max", 0);
168                 pdr->normal_min.value_u16 = e.value("normal_min", 0);
169                 pdr->rated_max.value_u16 = e.value("rated_max", 0);
170                 pdr->rated_min.value_u16 = e.value("rated_min", 0);
171                 break;
172             case PLDM_RANGE_FIELD_FORMAT_SINT16:
173                 pdr->nominal_value.value_s16 = e.value("nominal_value", 0);
174                 pdr->normal_max.value_s16 = e.value("normal_max", 0);
175                 pdr->normal_min.value_s16 = e.value("normal_min", 0);
176                 pdr->rated_max.value_s16 = e.value("rated_max", 0);
177                 pdr->rated_min.value_s16 = e.value("rated_min", 0);
178                 break;
179             case PLDM_RANGE_FIELD_FORMAT_UINT32:
180                 pdr->nominal_value.value_u32 = e.value("nominal_value", 0);
181                 pdr->normal_max.value_u32 = e.value("normal_max", 0);
182                 pdr->normal_min.value_u32 = e.value("normal_min", 0);
183                 pdr->rated_max.value_u32 = e.value("rated_max", 0);
184                 pdr->rated_min.value_u32 = e.value("rated_min", 0);
185                 break;
186             case PLDM_RANGE_FIELD_FORMAT_SINT32:
187                 pdr->nominal_value.value_s32 = e.value("nominal_value", 0);
188                 pdr->normal_max.value_s32 = e.value("normal_max", 0);
189                 pdr->normal_min.value_s32 = e.value("normal_min", 0);
190                 pdr->rated_max.value_s32 = e.value("rated_max", 0);
191                 pdr->rated_min.value_s32 = e.value("rated_min", 0);
192                 break;
193             case PLDM_RANGE_FIELD_FORMAT_REAL32:
194                 pdr->nominal_value.value_f32 = e.value("nominal_value", 0);
195                 pdr->normal_max.value_f32 = e.value("normal_max", 0);
196                 pdr->normal_min.value_f32 = e.value("normal_min", 0);
197                 pdr->rated_max.value_f32 = e.value("rated_max", 0);
198                 pdr->rated_min.value_f32 = e.value("rated_min", 0);
199                 break;
200             default:
201                 break;
202         }
203 
204         auto dbusEntry = e.value("dbus", empty);
205         auto objectPath = dbusEntry.value("path", "");
206         auto interface = dbusEntry.value("interface", "");
207         auto propertyName = dbusEntry.value("property_name", "");
208         auto propertyType = dbusEntry.value("property_type", "");
209 
210         pldm::responder::pdr_utils::DbusMappings dbusMappings{};
211         pldm::responder::pdr_utils::DbusValMaps dbusValMaps{};
212         pldm::utils::DBusMapping dbusMapping{};
213         try
214         {
215             auto service = dBusIntf.getService(objectPath.c_str(),
216                                                interface.c_str());
217 
218             dbusMapping = pldm::utils::DBusMapping{objectPath, interface,
219                                                    propertyName, propertyType};
220         }
221         catch (const std::exception& e)
222         {
223             error(
224                 "D-Bus object path does not exist, effecter ID: {EFFECTER_ID}",
225                 "EFFECTER_ID", static_cast<uint16_t>(pdr->effecter_id));
226         }
227         dbusMappings.emplace_back(std::move(dbusMapping));
228 
229         handler.addDbusObjMaps(
230             pdr->effecter_id,
231             std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)));
232 
233         pdr_utils::PdrEntry pdrEntry{};
234         pdrEntry.data = entry.data();
235         pdrEntry.size = sizeof(pldm_numeric_effecter_value_pdr);
236         repo.addRecord(pdrEntry);
237     }
238 }
239 
240 } // namespace pdr_numeric_effecter
241 } // namespace responder
242 } // namespace pldm
243