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         pdr->entity_type = e.value("entity_type", 0);
51         pdr->entity_instance = e.value("entity_instance", 0);
52         pdr->container_id = e.value("container_id", 0);
53         pdr->effecter_semantic_id = e.value("effecter_semantic_id", 0);
54         pdr->effecter_init = e.value("effecter_init", PLDM_NO_INIT);
55         pdr->effecter_auxiliary_names = e.value("effecter_init", false);
56         pdr->base_unit = e.value("base_unit", 0);
57         pdr->unit_modifier = e.value("unit_modifier", 0);
58         pdr->rate_unit = e.value("rate_unit", 0);
59         pdr->base_oem_unit_handle = e.value("base_oem_unit_handle", 0);
60         pdr->aux_unit = e.value("aux_unit", 0);
61         pdr->aux_unit_modifier = e.value("aux_unit_modifier", 0);
62         pdr->aux_oem_unit_handle = e.value("aux_oem_unit_handle", 0);
63         pdr->aux_rate_unit = e.value("aux_rate_unit", 0);
64         pdr->is_linear = e.value("is_linear", true);
65         pdr->effecter_data_size =
66             e.value("effecter_data_size", PLDM_EFFECTER_DATA_SIZE_UINT8);
67         pdr->resolution = e.value("effecter_resolution_init", 1.00);
68         pdr->offset = e.value("offset", 0.00);
69         pdr->accuracy = e.value("accuracy", 0);
70         pdr->plus_tolerance = e.value("plus_tolerance", 0);
71         pdr->minus_tolerance = e.value("minus_tolerance", 0);
72         pdr->state_transition_interval =
73             e.value("state_transition_interval", 0.00);
74         pdr->transition_interval = e.value("transition_interval", 0.00);
75         switch (pdr->effecter_data_size)
76         {
77             case PLDM_EFFECTER_DATA_SIZE_UINT8:
78                 pdr->max_set_table.value_u8 = e.value("max_set_table", 0);
79                 pdr->min_set_table.value_u8 = e.value("min_set_table", 0);
80                 break;
81             case PLDM_EFFECTER_DATA_SIZE_SINT8:
82                 pdr->max_set_table.value_s8 = e.value("max_set_table", 0);
83                 pdr->min_set_table.value_s8 = e.value("min_set_table", 0);
84                 break;
85             case PLDM_EFFECTER_DATA_SIZE_UINT16:
86                 pdr->max_set_table.value_u16 = e.value("max_set_table", 0);
87                 pdr->min_set_table.value_u16 = e.value("min_set_table", 0);
88                 break;
89             case PLDM_EFFECTER_DATA_SIZE_SINT16:
90                 pdr->max_set_table.value_s16 = e.value("max_set_table", 0);
91                 pdr->min_set_table.value_s16 = e.value("min_set_table", 0);
92                 break;
93             case PLDM_EFFECTER_DATA_SIZE_UINT32:
94                 pdr->max_set_table.value_u32 = e.value("max_set_table", 0);
95                 pdr->min_set_table.value_u32 = e.value("min_set_table", 0);
96                 break;
97             case PLDM_EFFECTER_DATA_SIZE_SINT32:
98                 pdr->max_set_table.value_s32 = e.value("max_set_table", 0);
99                 pdr->min_set_table.value_s32 = e.value("min_set_table", 0);
100                 break;
101             default:
102                 break;
103         }
104 
105         pdr->range_field_format =
106             e.value("range_field_format", PLDM_RANGE_FIELD_FORMAT_UINT8);
107         pdr->range_field_support.byte = e.value("range_field_support", 0);
108         switch (pdr->range_field_format)
109         {
110             case PLDM_RANGE_FIELD_FORMAT_UINT8:
111                 pdr->nominal_value.value_u8 = e.value("nominal_value", 0);
112                 pdr->normal_max.value_u8 = e.value("normal_max", 0);
113                 pdr->normal_min.value_u8 = e.value("normal_min", 0);
114                 pdr->rated_max.value_u8 = e.value("rated_max", 0);
115                 pdr->rated_min.value_u8 = e.value("rated_min", 0);
116                 break;
117             case PLDM_RANGE_FIELD_FORMAT_SINT8:
118                 pdr->nominal_value.value_s8 = e.value("nominal_value", 0);
119                 pdr->normal_max.value_s8 = e.value("normal_max", 0);
120                 pdr->normal_min.value_s8 = e.value("normal_min", 0);
121                 pdr->rated_max.value_s8 = e.value("rated_max", 0);
122                 pdr->rated_min.value_s8 = e.value("rated_min", 0);
123                 break;
124             case PLDM_RANGE_FIELD_FORMAT_UINT16:
125                 pdr->nominal_value.value_u16 = e.value("nominal_value", 0);
126                 pdr->normal_max.value_u16 = e.value("normal_max", 0);
127                 pdr->normal_min.value_u16 = e.value("normal_min", 0);
128                 pdr->rated_max.value_u16 = e.value("rated_max", 0);
129                 pdr->rated_min.value_u16 = e.value("rated_min", 0);
130                 break;
131             case PLDM_RANGE_FIELD_FORMAT_SINT16:
132                 pdr->nominal_value.value_s16 = e.value("nominal_value", 0);
133                 pdr->normal_max.value_s16 = e.value("normal_max", 0);
134                 pdr->normal_min.value_s16 = e.value("normal_min", 0);
135                 pdr->rated_max.value_s16 = e.value("rated_max", 0);
136                 pdr->rated_min.value_s16 = e.value("rated_min", 0);
137                 break;
138             case PLDM_RANGE_FIELD_FORMAT_UINT32:
139                 pdr->nominal_value.value_u32 = e.value("nominal_value", 0);
140                 pdr->normal_max.value_u32 = e.value("normal_max", 0);
141                 pdr->normal_min.value_u32 = e.value("normal_min", 0);
142                 pdr->rated_max.value_u32 = e.value("rated_max", 0);
143                 pdr->rated_min.value_u32 = e.value("rated_min", 0);
144                 break;
145             case PLDM_RANGE_FIELD_FORMAT_SINT32:
146                 pdr->nominal_value.value_s32 = e.value("nominal_value", 0);
147                 pdr->normal_max.value_s32 = e.value("normal_max", 0);
148                 pdr->normal_min.value_s32 = e.value("normal_min", 0);
149                 pdr->rated_max.value_s32 = e.value("rated_max", 0);
150                 pdr->rated_min.value_s32 = e.value("rated_min", 0);
151                 break;
152             case PLDM_RANGE_FIELD_FORMAT_REAL32:
153                 pdr->nominal_value.value_f32 = e.value("nominal_value", 0);
154                 pdr->normal_max.value_f32 = e.value("normal_max", 0);
155                 pdr->normal_min.value_f32 = e.value("normal_min", 0);
156                 pdr->rated_max.value_f32 = e.value("rated_max", 0);
157                 pdr->rated_min.value_f32 = e.value("rated_min", 0);
158                 break;
159             default:
160                 break;
161         }
162 
163         auto dbusEntry = e.value("dbus", empty);
164         auto objectPath = dbusEntry.value("path", "");
165         auto interface = dbusEntry.value("interface", "");
166         auto propertyName = dbusEntry.value("property_name", "");
167         auto propertyType = dbusEntry.value("property_type", "");
168 
169         try
170         {
171             auto service =
172                 dBusIntf.getService(objectPath.c_str(), interface.c_str());
173         }
174         catch (const std::exception& e)
175         {
176             continue;
177         }
178 
179         pldm::utils::DBusMapping dbusMapping{objectPath, interface,
180                                              propertyName, propertyType};
181         DbusMappings dbusMappings{};
182         DbusValMaps dbusValMaps{};
183         dbusMappings.emplace_back(std::move(dbusMapping));
184         handler.addDbusObjMaps(
185             pdr->effecter_id,
186             std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)));
187 
188         pdr_utils::PdrEntry pdrEntry{};
189         pdrEntry.data = entry.data();
190         pdrEntry.size = sizeof(pldm_numeric_effecter_value_pdr);
191         repo.addRecord(pdrEntry);
192     }
193 }
194 
195 } // namespace pdr_numeric_effecter
196 } // namespace responder
197 } // namespace pldm
198