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 DBusInterface, class Handler>
28 void generateNumericEffecterPDR(const DBusInterface& dBusIntf, const Json& json,
29                                 Handler& handler,
30                                 pdr_utils::RepoInterface& repo)
31 {
32     static const std::vector<Json> emptyList{};
33     auto entries = json.value("entries", emptyList);
34     for (const auto& e : entries)
35     {
36         std::vector<uint8_t> entry{};
37         entry.resize(sizeof(pldm_numeric_effecter_value_pdr));
38 
39         pldm_numeric_effecter_value_pdr* pdr =
40             reinterpret_cast<pldm_numeric_effecter_value_pdr*>(entry.data());
41         if (!pdr)
42         {
43             std::cerr << "Failed to get numeric effecter PDR.\n";
44             continue;
45         }
46         pdr->hdr.record_handle = 0;
47         pdr->hdr.version = 1;
48         pdr->hdr.type = PLDM_NUMERIC_EFFECTER_PDR;
49         pdr->hdr.record_change_num = 0;
50         pdr->hdr.length =
51             sizeof(pldm_numeric_effecter_value_pdr) - sizeof(pldm_pdr_hdr);
52 
53         pdr->terminus_handle = e.value("terminus_handle", 0);
54         pdr->effecter_id = handler.getNextEffecterId();
55 
56         try
57         {
58             std::string entity_path = e.value("entity_path", "");
59             auto& associatedEntityMap = handler.getAssociateEntityMap();
60             if (entity_path != "" && associatedEntityMap.find(entity_path) !=
61                                          associatedEntityMap.end())
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);
75             }
76         }
77         catch (const std::exception& ex)
78         {
79             pdr->entity_type = e.value("type", 0);
80             pdr->entity_instance = e.value("instance", 0);
81             pdr->container_id = e.value("container", 0);
82         }
83 
84         pdr->effecter_semantic_id = e.value("effecter_semantic_id", 0);
85         pdr->effecter_init = e.value("effecter_init", PLDM_NO_INIT);
86         pdr->effecter_auxiliary_names = e.value("effecter_init", false);
87         pdr->base_unit = e.value("base_unit", 0);
88         pdr->unit_modifier = e.value("unit_modifier", 0);
89         pdr->rate_unit = e.value("rate_unit", 0);
90         pdr->base_oem_unit_handle = e.value("base_oem_unit_handle", 0);
91         pdr->aux_unit = e.value("aux_unit", 0);
92         pdr->aux_unit_modifier = e.value("aux_unit_modifier", 0);
93         pdr->aux_oem_unit_handle = e.value("aux_oem_unit_handle", 0);
94         pdr->aux_rate_unit = e.value("aux_rate_unit", 0);
95         pdr->is_linear = e.value("is_linear", true);
96         pdr->effecter_data_size =
97             e.value("effecter_data_size", PLDM_EFFECTER_DATA_SIZE_UINT8);
98         pdr->resolution = e.value("effecter_resolution_init", 1.00);
99         pdr->offset = e.value("offset", 0.00);
100         pdr->accuracy = e.value("accuracy", 0);
101         pdr->plus_tolerance = e.value("plus_tolerance", 0);
102         pdr->minus_tolerance = e.value("minus_tolerance", 0);
103         pdr->state_transition_interval =
104             e.value("state_transition_interval", 0.00);
105         pdr->transition_interval = e.value("transition_interval", 0.00);
106         switch (pdr->effecter_data_size)
107         {
108             case PLDM_EFFECTER_DATA_SIZE_UINT8:
109                 pdr->max_settable.value_u8 = e.value("max_settable", 0);
110                 pdr->min_settable.value_u8 = e.value("min_settable", 0);
111                 break;
112             case PLDM_EFFECTER_DATA_SIZE_SINT8:
113                 pdr->max_settable.value_s8 = e.value("max_settable", 0);
114                 pdr->min_settable.value_s8 = e.value("min_settable", 0);
115                 break;
116             case PLDM_EFFECTER_DATA_SIZE_UINT16:
117                 pdr->max_settable.value_u16 = e.value("max_settable", 0);
118                 pdr->min_settable.value_u16 = e.value("min_settable", 0);
119                 break;
120             case PLDM_EFFECTER_DATA_SIZE_SINT16:
121                 pdr->max_settable.value_s16 = e.value("max_settable", 0);
122                 pdr->min_settable.value_s16 = e.value("min_settable", 0);
123                 break;
124             case PLDM_EFFECTER_DATA_SIZE_UINT32:
125                 pdr->max_settable.value_u32 = e.value("max_settable", 0);
126                 pdr->min_settable.value_u32 = e.value("min_settable", 0);
127                 break;
128             case PLDM_EFFECTER_DATA_SIZE_SINT32:
129                 pdr->max_settable.value_s32 = e.value("max_settable", 0);
130                 pdr->min_settable.value_s32 = e.value("min_settable", 0);
131                 break;
132             default:
133                 break;
134         }
135 
136         pdr->range_field_format =
137             e.value("range_field_format", PLDM_RANGE_FIELD_FORMAT_UINT8);
138         pdr->range_field_support.byte = e.value("range_field_support", 0);
139         switch (pdr->range_field_format)
140         {
141             case PLDM_RANGE_FIELD_FORMAT_UINT8:
142                 pdr->nominal_value.value_u8 = e.value("nominal_value", 0);
143                 pdr->normal_max.value_u8 = e.value("normal_max", 0);
144                 pdr->normal_min.value_u8 = e.value("normal_min", 0);
145                 pdr->rated_max.value_u8 = e.value("rated_max", 0);
146                 pdr->rated_min.value_u8 = e.value("rated_min", 0);
147                 break;
148             case PLDM_RANGE_FIELD_FORMAT_SINT8:
149                 pdr->nominal_value.value_s8 = e.value("nominal_value", 0);
150                 pdr->normal_max.value_s8 = e.value("normal_max", 0);
151                 pdr->normal_min.value_s8 = e.value("normal_min", 0);
152                 pdr->rated_max.value_s8 = e.value("rated_max", 0);
153                 pdr->rated_min.value_s8 = e.value("rated_min", 0);
154                 break;
155             case PLDM_RANGE_FIELD_FORMAT_UINT16:
156                 pdr->nominal_value.value_u16 = e.value("nominal_value", 0);
157                 pdr->normal_max.value_u16 = e.value("normal_max", 0);
158                 pdr->normal_min.value_u16 = e.value("normal_min", 0);
159                 pdr->rated_max.value_u16 = e.value("rated_max", 0);
160                 pdr->rated_min.value_u16 = e.value("rated_min", 0);
161                 break;
162             case PLDM_RANGE_FIELD_FORMAT_SINT16:
163                 pdr->nominal_value.value_s16 = e.value("nominal_value", 0);
164                 pdr->normal_max.value_s16 = e.value("normal_max", 0);
165                 pdr->normal_min.value_s16 = e.value("normal_min", 0);
166                 pdr->rated_max.value_s16 = e.value("rated_max", 0);
167                 pdr->rated_min.value_s16 = e.value("rated_min", 0);
168                 break;
169             case PLDM_RANGE_FIELD_FORMAT_UINT32:
170                 pdr->nominal_value.value_u32 = e.value("nominal_value", 0);
171                 pdr->normal_max.value_u32 = e.value("normal_max", 0);
172                 pdr->normal_min.value_u32 = e.value("normal_min", 0);
173                 pdr->rated_max.value_u32 = e.value("rated_max", 0);
174                 pdr->rated_min.value_u32 = e.value("rated_min", 0);
175                 break;
176             case PLDM_RANGE_FIELD_FORMAT_SINT32:
177                 pdr->nominal_value.value_s32 = e.value("nominal_value", 0);
178                 pdr->normal_max.value_s32 = e.value("normal_max", 0);
179                 pdr->normal_min.value_s32 = e.value("normal_min", 0);
180                 pdr->rated_max.value_s32 = e.value("rated_max", 0);
181                 pdr->rated_min.value_s32 = e.value("rated_min", 0);
182                 break;
183             case PLDM_RANGE_FIELD_FORMAT_REAL32:
184                 pdr->nominal_value.value_f32 = e.value("nominal_value", 0);
185                 pdr->normal_max.value_f32 = e.value("normal_max", 0);
186                 pdr->normal_min.value_f32 = e.value("normal_min", 0);
187                 pdr->rated_max.value_f32 = e.value("rated_max", 0);
188                 pdr->rated_min.value_f32 = e.value("rated_min", 0);
189                 break;
190             default:
191                 break;
192         }
193 
194         auto dbusEntry = e.value("dbus", empty);
195         auto objectPath = dbusEntry.value("path", "");
196         auto interface = dbusEntry.value("interface", "");
197         auto propertyName = dbusEntry.value("property_name", "");
198         auto propertyType = dbusEntry.value("property_type", "");
199 
200         pldm::responder::pdr_utils::DbusMappings dbusMappings{};
201         pldm::responder::pdr_utils::DbusValMaps dbusValMaps{};
202         pldm::utils::DBusMapping dbusMapping{};
203         try
204         {
205             auto service =
206                 dBusIntf.getService(objectPath.c_str(), interface.c_str());
207 
208             dbusMapping = pldm::utils::DBusMapping{objectPath, interface,
209                                                    propertyName, propertyType};
210         }
211         catch (const std::exception& e)
212         {
213             std::cerr << "D-Bus object path does not exist, effecter ID: "
214                       << pdr->effecter_id << "\n";
215         }
216         dbusMappings.emplace_back(std::move(dbusMapping));
217 
218         handler.addDbusObjMaps(
219             pdr->effecter_id,
220             std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)));
221 
222         pdr_utils::PdrEntry pdrEntry{};
223         pdrEntry.data = entry.data();
224         pdrEntry.size = sizeof(pldm_numeric_effecter_value_pdr);
225         repo.addRecord(pdrEntry);
226     }
227 }
228 
229 } // namespace pdr_numeric_effecter
230 } // namespace responder
231 } // namespace pldm
232