1 #pragma once
2 
3 #include "pdr.hpp"
4 #include "pdr_utils.hpp"
5 
6 #include <libpldm/platform.h>
7 
8 #include <phosphor-logging/lg2.hpp>
9 
10 PHOSPHOR_LOG2_USING;
11 
12 namespace pldm
13 {
14 namespace responder
15 {
16 namespace pdr_state_effecter
17 {
18 using Json = nlohmann::json;
19 
20 static const Json empty{};
21 
22 /** @brief Parse PDR JSON file and generate state effecter PDR structure
23  *
24  *  @param[in] json - the JSON Object with the state effecter PDR
25  *  @param[out] handler - the Parser of PLDM command handler
26  *  @param[out] repo - pdr::RepoInterface
27  *
28  */
29 template <class DBusInterface, class Handler>
30 void generateStateEffecterPDR(const DBusInterface& dBusIntf, const Json& json,
31                               Handler& handler, 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         size_t pdrSize = 0;
38         auto effecters = e.value("effecters", emptyList);
39         for (const auto& effecter : effecters)
40         {
41             auto set = effecter.value("set", empty);
42             auto statesSize = set.value("size", 0);
43             if (!statesSize)
44             {
45                 error(
46                     "Malformed PDR JSON return pdrEntry;- no state set info, TYPE={STATE_EFFECTER_PDR}",
47                     "STATE_EFFECTER_PDR",
48                     static_cast<unsigned>(PLDM_STATE_EFFECTER_PDR));
49                 throw InternalFailure();
50             }
51             pdrSize += sizeof(state_effecter_possible_states) -
52                        sizeof(bitfield8_t) + (sizeof(bitfield8_t) * statesSize);
53         }
54         pdrSize += sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t);
55 
56         std::vector<uint8_t> entry{};
57         entry.resize(pdrSize);
58 
59         pldm_state_effecter_pdr* pdr =
60             reinterpret_cast<pldm_state_effecter_pdr*>(entry.data());
61         if (!pdr)
62         {
63             error("Failed to get state effecter PDR.");
64             continue;
65         }
66         pdr->hdr.record_handle = 0;
67         pdr->hdr.version = 1;
68         pdr->hdr.type = PLDM_STATE_EFFECTER_PDR;
69         pdr->hdr.record_change_num = 0;
70         pdr->hdr.length = pdrSize - sizeof(pldm_pdr_hdr);
71 
72         pdr->terminus_handle = TERMINUS_HANDLE;
73 
74         try
75         {
76             std::string entity_path = e.value("entity_path", "");
77             auto& associatedEntityMap = handler.getAssociateEntityMap();
78             if (entity_path != "" && associatedEntityMap.contains(entity_path))
79             {
80                 pdr->entity_type =
81                     associatedEntityMap.at(entity_path).entity_type;
82                 pdr->entity_instance =
83                     associatedEntityMap.at(entity_path).entity_instance_num;
84                 pdr->container_id =
85                     associatedEntityMap.at(entity_path).entity_container_id;
86             }
87             else
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                 // do not create the PDR when the FRU or the entity path is not
94                 // present
95                 if (!pdr->entity_type)
96                 {
97                     continue;
98                 }
99             }
100         }
101         catch (const std::exception&)
102         {
103             pdr->entity_type = e.value("type", 0);
104             pdr->entity_instance = e.value("instance", 0);
105             pdr->container_id = e.value("container", 0);
106         }
107 
108         pdr->effecter_semantic_id = 0;
109         pdr->effecter_init = PLDM_NO_INIT;
110         pdr->has_description_pdr = false;
111         pdr->composite_effecter_count = effecters.size();
112 
113         pldm::responder::pdr_utils::DbusMappings dbusMappings{};
114         pldm::responder::pdr_utils::DbusValMaps dbusValMaps{};
115         uint8_t* start = entry.data() + sizeof(pldm_state_effecter_pdr) -
116                          sizeof(uint8_t);
117         for (const auto& effecter : effecters)
118         {
119             auto set = effecter.value("set", empty);
120             state_effecter_possible_states* possibleStates =
121                 reinterpret_cast<state_effecter_possible_states*>(start);
122             possibleStates->state_set_id = set.value("id", 0);
123             possibleStates->possible_states_size = set.value("size", 0);
124 
125             start += sizeof(possibleStates->state_set_id) +
126                      sizeof(possibleStates->possible_states_size);
127             static const std::vector<uint8_t> emptyStates{};
128             pldm::responder::pdr_utils::PossibleValues stateValues;
129             auto states = set.value("states", emptyStates);
130             for (const auto& state : states)
131             {
132                 auto index = state / 8;
133                 auto bit = state - (index * 8);
134                 bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index);
135                 bf->byte |= 1 << bit;
136                 stateValues.emplace_back(state);
137             }
138             start += possibleStates->possible_states_size;
139 
140             auto dbusEntry = effecter.value("dbus", empty);
141             auto objectPath = dbusEntry.value("path", "");
142             auto interface = dbusEntry.value("interface", "");
143             auto propertyName = dbusEntry.value("property_name", "");
144             auto propertyType = dbusEntry.value("property_type", "");
145 
146             pldm::responder::pdr_utils::StatestoDbusVal dbusIdToValMap{};
147             pldm::utils::DBusMapping dbusMapping{};
148             try
149             {
150                 auto service = dBusIntf.getService(objectPath.c_str(),
151                                                    interface.c_str());
152 
153                 dbusMapping = pldm::utils::DBusMapping{
154                     objectPath, interface, propertyName, propertyType};
155                 dbusIdToValMap = pldm::responder::pdr_utils::populateMapping(
156                     propertyType, dbusEntry["property_values"], stateValues);
157             }
158             catch (const std::exception& e)
159             {
160                 error(
161                     "Failed to create effecter PDR, D-Bus object '{PATH}' returned {ERROR}",
162                     "PATH", objectPath, "ERROR", e);
163                 break;
164             }
165             dbusMappings.emplace_back(std::move(dbusMapping));
166             dbusValMaps.emplace_back(std::move(dbusIdToValMap));
167         }
168         if (!(dbusMappings.empty() && dbusValMaps.empty()))
169         {
170             pdr->effecter_id = handler.getNextEffecterId();
171             handler.addDbusObjMaps(pdr->effecter_id,
172                                    std::make_tuple(std::move(dbusMappings),
173                                                    std::move(dbusValMaps)));
174             pldm::responder::pdr_utils::PdrEntry pdrEntry{};
175             pdrEntry.data = entry.data();
176             pdrEntry.size = pdrSize;
177             repo.addRecord(pdrEntry);
178         }
179     }
180 }
181 
182 } // namespace pdr_state_effecter
183 } // namespace responder
184 } // namespace pldm
185