1 #pragma once
2 
3 #include "libpldm/platform.h"
4 
5 #include "libpldmresponder/pdr_utils.hpp"
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         pdr->hdr.record_handle = 0;
42         pdr->hdr.version = 1;
43         pdr->hdr.type = PLDM_NUMERIC_EFFECTER_PDR;
44         pdr->hdr.record_change_num = 0;
45         pdr->hdr.length =
46             sizeof(pldm_numeric_effecter_value_pdr) - sizeof(pldm_pdr_hdr);
47 
48         pdr->terminus_handle = e.value("terminus_handle", 0);
49         pdr->effecter_id = handler.getNextEffecterId();
50 
51         try
52         {
53             std::string entity_path = e.value("entity_path", "");
54             auto& associatedEntityMap = handler.getAssociateEntityMap();
55             if (entity_path != "" && associatedEntityMap.find(entity_path) !=
56                                          associatedEntityMap.end())
57             {
58                 pdr->entity_type =
59                     associatedEntityMap.at(entity_path).entity_type;
60                 pdr->entity_instance =
61                     associatedEntityMap.at(entity_path).entity_instance_num;
62                 pdr->container_id =
63                     associatedEntityMap.at(entity_path).entity_container_id;
64             }
65             else
66             {
67                 pdr->entity_type = e.value("type", 0);
68                 pdr->entity_instance = e.value("instance", 0);
69                 pdr->container_id = e.value("container", 0);
70             }
71         }
72         catch (const std::exception& ex)
73         {
74             pdr->entity_type = e.value("type", 0);
75             pdr->entity_instance = e.value("instance", 0);
76             pdr->container_id = e.value("container", 0);
77         }
78 
79         pdr->effecter_semantic_id = e.value("effecter_semantic_id", 0);
80         pdr->effecter_init = e.value("effecter_init", PLDM_NO_INIT);
81         pdr->effecter_auxiliary_names = e.value("effecter_init", false);
82         pdr->base_unit = e.value("base_unit", 0);
83         pdr->unit_modifier = e.value("unit_modifier", 0);
84         pdr->rate_unit = e.value("rate_unit", 0);
85         pdr->base_oem_unit_handle = e.value("base_oem_unit_handle", 0);
86         pdr->aux_unit = e.value("aux_unit", 0);
87         pdr->aux_unit_modifier = e.value("aux_unit_modifier", 0);
88         pdr->aux_oem_unit_handle = e.value("aux_oem_unit_handle", 0);
89         pdr->aux_rate_unit = e.value("aux_rate_unit", 0);
90         pdr->is_linear = e.value("is_linear", true);
91         pdr->effecter_data_size =
92             e.value("effecter_data_size", PLDM_EFFECTER_DATA_SIZE_UINT8);
93         pdr->resolution = e.value("effecter_resolution_init", 1.00);
94         pdr->offset = e.value("offset", 0.00);
95         pdr->accuracy = e.value("accuracy", 0);
96         pdr->plus_tolerance = e.value("plus_tolerance", 0);
97         pdr->minus_tolerance = e.value("minus_tolerance", 0);
98         pdr->state_transition_interval =
99             e.value("state_transition_interval", 0.00);
100         pdr->transition_interval = e.value("transition_interval", 0.00);
101         switch (pdr->effecter_data_size)
102         {
103             case PLDM_EFFECTER_DATA_SIZE_UINT8:
104                 pdr->max_set_table.value_u8 = e.value("max_set_table", 0);
105                 pdr->min_set_table.value_u8 = e.value("min_set_table", 0);
106                 break;
107             case PLDM_EFFECTER_DATA_SIZE_SINT8:
108                 pdr->max_set_table.value_s8 = e.value("max_set_table", 0);
109                 pdr->min_set_table.value_s8 = e.value("min_set_table", 0);
110                 break;
111             case PLDM_EFFECTER_DATA_SIZE_UINT16:
112                 pdr->max_set_table.value_u16 = e.value("max_set_table", 0);
113                 pdr->min_set_table.value_u16 = e.value("min_set_table", 0);
114                 break;
115             case PLDM_EFFECTER_DATA_SIZE_SINT16:
116                 pdr->max_set_table.value_s16 = e.value("max_set_table", 0);
117                 pdr->min_set_table.value_s16 = e.value("min_set_table", 0);
118                 break;
119             case PLDM_EFFECTER_DATA_SIZE_UINT32:
120                 pdr->max_set_table.value_u32 = e.value("max_set_table", 0);
121                 pdr->min_set_table.value_u32 = e.value("min_set_table", 0);
122                 break;
123             case PLDM_EFFECTER_DATA_SIZE_SINT32:
124                 pdr->max_set_table.value_s32 = e.value("max_set_table", 0);
125                 pdr->min_set_table.value_s32 = e.value("min_set_table", 0);
126                 break;
127             default:
128                 break;
129         }
130 
131         pdr->range_field_format =
132             e.value("range_field_format", PLDM_RANGE_FIELD_FORMAT_UINT8);
133         pdr->range_field_support.byte = e.value("range_field_support", 0);
134         switch (pdr->range_field_format)
135         {
136             case PLDM_RANGE_FIELD_FORMAT_UINT8:
137                 pdr->nominal_value.value_u8 = e.value("nominal_value", 0);
138                 pdr->normal_max.value_u8 = e.value("normal_max", 0);
139                 pdr->normal_min.value_u8 = e.value("normal_min", 0);
140                 pdr->rated_max.value_u8 = e.value("rated_max", 0);
141                 pdr->rated_min.value_u8 = e.value("rated_min", 0);
142                 break;
143             case PLDM_RANGE_FIELD_FORMAT_SINT8:
144                 pdr->nominal_value.value_s8 = e.value("nominal_value", 0);
145                 pdr->normal_max.value_s8 = e.value("normal_max", 0);
146                 pdr->normal_min.value_s8 = e.value("normal_min", 0);
147                 pdr->rated_max.value_s8 = e.value("rated_max", 0);
148                 pdr->rated_min.value_s8 = e.value("rated_min", 0);
149                 break;
150             case PLDM_RANGE_FIELD_FORMAT_UINT16:
151                 pdr->nominal_value.value_u16 = e.value("nominal_value", 0);
152                 pdr->normal_max.value_u16 = e.value("normal_max", 0);
153                 pdr->normal_min.value_u16 = e.value("normal_min", 0);
154                 pdr->rated_max.value_u16 = e.value("rated_max", 0);
155                 pdr->rated_min.value_u16 = e.value("rated_min", 0);
156                 break;
157             case PLDM_RANGE_FIELD_FORMAT_SINT16:
158                 pdr->nominal_value.value_s16 = e.value("nominal_value", 0);
159                 pdr->normal_max.value_s16 = e.value("normal_max", 0);
160                 pdr->normal_min.value_s16 = e.value("normal_min", 0);
161                 pdr->rated_max.value_s16 = e.value("rated_max", 0);
162                 pdr->rated_min.value_s16 = e.value("rated_min", 0);
163                 break;
164             case PLDM_RANGE_FIELD_FORMAT_UINT32:
165                 pdr->nominal_value.value_u32 = e.value("nominal_value", 0);
166                 pdr->normal_max.value_u32 = e.value("normal_max", 0);
167                 pdr->normal_min.value_u32 = e.value("normal_min", 0);
168                 pdr->rated_max.value_u32 = e.value("rated_max", 0);
169                 pdr->rated_min.value_u32 = e.value("rated_min", 0);
170                 break;
171             case PLDM_RANGE_FIELD_FORMAT_SINT32:
172                 pdr->nominal_value.value_s32 = e.value("nominal_value", 0);
173                 pdr->normal_max.value_s32 = e.value("normal_max", 0);
174                 pdr->normal_min.value_s32 = e.value("normal_min", 0);
175                 pdr->rated_max.value_s32 = e.value("rated_max", 0);
176                 pdr->rated_min.value_s32 = e.value("rated_min", 0);
177                 break;
178             case PLDM_RANGE_FIELD_FORMAT_REAL32:
179                 pdr->nominal_value.value_f32 = e.value("nominal_value", 0);
180                 pdr->normal_max.value_f32 = e.value("normal_max", 0);
181                 pdr->normal_min.value_f32 = e.value("normal_min", 0);
182                 pdr->rated_max.value_f32 = e.value("rated_max", 0);
183                 pdr->rated_min.value_f32 = e.value("rated_min", 0);
184                 break;
185             default:
186                 break;
187         }
188 
189         auto dbusEntry = e.value("dbus", empty);
190         auto objectPath = dbusEntry.value("path", "");
191         auto interface = dbusEntry.value("interface", "");
192         auto propertyName = dbusEntry.value("property_name", "");
193         auto propertyType = dbusEntry.value("property_type", "");
194 
195         try
196         {
197             auto service =
198                 dBusIntf.getService(objectPath.c_str(), interface.c_str());
199         }
200         catch (const std::exception& e)
201         {
202             continue;
203         }
204 
205         pldm::utils::DBusMapping dbusMapping{objectPath, interface,
206                                              propertyName, propertyType};
207         DbusMappings dbusMappings{};
208         DbusValMaps dbusValMaps{};
209         dbusMappings.emplace_back(std::move(dbusMapping));
210         handler.addDbusObjMaps(
211             pdr->effecter_id,
212             std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)));
213 
214         pdr_utils::PdrEntry pdrEntry{};
215         pdrEntry.data = entry.data();
216         pdrEntry.size = sizeof(pldm_numeric_effecter_value_pdr);
217         repo.addRecord(pdrEntry);
218     }
219 }
220 
221 } // namespace pdr_numeric_effecter
222 } // namespace responder
223 } // namespace pldm
224