xref: /openbmc/pldm/libpldmresponder/pdr_state_effecter.hpp (revision c4ea6a905fed95476c4595df2ba6bc84175e57fc)
1a2870726SGeorge Liu #pragma once
2a2870726SGeorge Liu 
3a2870726SGeorge Liu #include "libpldm/platform.h"
4a2870726SGeorge Liu 
512afe110SSampa Misra #include "pdr.hpp"
612afe110SSampa Misra #include "pdr_utils.hpp"
76492f524SGeorge Liu 
8a2870726SGeorge Liu namespace pldm
9a2870726SGeorge Liu {
10a2870726SGeorge Liu 
11a2870726SGeorge Liu namespace responder
12a2870726SGeorge Liu {
13a2870726SGeorge Liu 
14a2870726SGeorge Liu namespace pdr_state_effecter
15a2870726SGeorge Liu {
16a2870726SGeorge Liu 
17a2870726SGeorge Liu using Json = nlohmann::json;
18a2870726SGeorge Liu 
19a2870726SGeorge Liu static const Json empty{};
20a2870726SGeorge Liu 
21a2870726SGeorge Liu /** @brief Parse PDR JSON file and generate state effecter PDR structure
22a2870726SGeorge Liu  *
23a2870726SGeorge Liu  *  @param[in] json - the JSON Object with the state effecter PDR
24a2870726SGeorge Liu  *  @param[out] handler - the Parser of PLDM command handler
25a2870726SGeorge Liu  *  @param[out] repo - pdr::RepoInterface
26a2870726SGeorge Liu  *
27a2870726SGeorge Liu  */
2836e81352SGeorge Liu template <class DBusInterface, class Handler>
2936e81352SGeorge Liu void generateStateEffecterPDR(const DBusInterface& dBusIntf, const Json& json,
3036e81352SGeorge Liu                               Handler& handler, pdr_utils::RepoInterface& repo)
31a2870726SGeorge Liu {
32a2870726SGeorge Liu     static const std::vector<Json> emptyList{};
33a2870726SGeorge Liu     auto entries = json.value("entries", emptyList);
34a2870726SGeorge Liu     for (const auto& e : entries)
35a2870726SGeorge Liu     {
36a2870726SGeorge Liu         size_t pdrSize = 0;
37a2870726SGeorge Liu         auto effecters = e.value("effecters", emptyList);
38a2870726SGeorge Liu         for (const auto& effecter : effecters)
39a2870726SGeorge Liu         {
40a2870726SGeorge Liu             auto set = effecter.value("set", empty);
41a2870726SGeorge Liu             auto statesSize = set.value("size", 0);
42a2870726SGeorge Liu             if (!statesSize)
43a2870726SGeorge Liu             {
44a2870726SGeorge Liu                 std::cerr << "Malformed PDR JSON return "
45a2870726SGeorge Liu                              "pdrEntry;- no state set "
46a2870726SGeorge Liu                              "info, TYPE="
47a2870726SGeorge Liu                           << PLDM_STATE_EFFECTER_PDR << "\n";
48a2870726SGeorge Liu                 throw InternalFailure();
49a2870726SGeorge Liu             }
50a2870726SGeorge Liu             pdrSize += sizeof(state_effecter_possible_states) -
51a2870726SGeorge Liu                        sizeof(bitfield8_t) + (sizeof(bitfield8_t) * statesSize);
52a2870726SGeorge Liu         }
53a2870726SGeorge Liu         pdrSize += sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t);
54a2870726SGeorge Liu 
55a2870726SGeorge Liu         std::vector<uint8_t> entry{};
56a2870726SGeorge Liu         entry.resize(pdrSize);
57a2870726SGeorge Liu 
58a2870726SGeorge Liu         pldm_state_effecter_pdr* pdr =
59a2870726SGeorge Liu             reinterpret_cast<pldm_state_effecter_pdr*>(entry.data());
60a2870726SGeorge Liu         pdr->hdr.record_handle = 0;
61a2870726SGeorge Liu         pdr->hdr.version = 1;
62a2870726SGeorge Liu         pdr->hdr.type = PLDM_STATE_EFFECTER_PDR;
63a2870726SGeorge Liu         pdr->hdr.record_change_num = 0;
64a2870726SGeorge Liu         pdr->hdr.length = pdrSize - sizeof(pldm_pdr_hdr);
65a2870726SGeorge Liu 
6612afe110SSampa Misra         pdr->terminus_handle = pdr::BmcPldmTerminusHandle;
67a2870726SGeorge Liu         pdr->effecter_id = handler.getNextEffecterId();
68*c4ea6a90SGeorge Liu 
69*c4ea6a90SGeorge Liu         try
70*c4ea6a90SGeorge Liu         {
71*c4ea6a90SGeorge Liu             std::string entity_path = e.value("entity_path", "");
72*c4ea6a90SGeorge Liu             auto& associatedEntityMap = handler.getAssociateEntityMap();
73*c4ea6a90SGeorge Liu             if (entity_path != "" && associatedEntityMap.find(entity_path) !=
74*c4ea6a90SGeorge Liu                                          associatedEntityMap.end())
75*c4ea6a90SGeorge Liu             {
76*c4ea6a90SGeorge Liu                 pdr->entity_type =
77*c4ea6a90SGeorge Liu                     associatedEntityMap.at(entity_path).entity_type;
78*c4ea6a90SGeorge Liu                 pdr->entity_instance =
79*c4ea6a90SGeorge Liu                     associatedEntityMap.at(entity_path).entity_instance_num;
80*c4ea6a90SGeorge Liu                 pdr->container_id =
81*c4ea6a90SGeorge Liu                     associatedEntityMap.at(entity_path).entity_container_id;
82*c4ea6a90SGeorge Liu             }
83*c4ea6a90SGeorge Liu             else
84*c4ea6a90SGeorge Liu             {
85a2870726SGeorge Liu                 pdr->entity_type = e.value("type", 0);
86a2870726SGeorge Liu                 pdr->entity_instance = e.value("instance", 0);
87a2870726SGeorge Liu                 pdr->container_id = e.value("container", 0);
88*c4ea6a90SGeorge Liu             }
89*c4ea6a90SGeorge Liu         }
90*c4ea6a90SGeorge Liu         catch (const std::exception& ex)
91*c4ea6a90SGeorge Liu         {
92*c4ea6a90SGeorge Liu             pdr->entity_type = e.value("type", 0);
93*c4ea6a90SGeorge Liu             pdr->entity_instance = e.value("instance", 0);
94*c4ea6a90SGeorge Liu             pdr->container_id = e.value("container", 0);
95*c4ea6a90SGeorge Liu         }
96*c4ea6a90SGeorge Liu 
97a2870726SGeorge Liu         pdr->effecter_semantic_id = 0;
98a2870726SGeorge Liu         pdr->effecter_init = PLDM_NO_INIT;
99a2870726SGeorge Liu         pdr->has_description_pdr = false;
100a2870726SGeorge Liu         pdr->composite_effecter_count = effecters.size();
101a2870726SGeorge Liu 
102a2870726SGeorge Liu         DbusMappings dbusMappings{};
103a2870726SGeorge Liu         DbusValMaps dbusValMaps{};
104a2870726SGeorge Liu         uint8_t* start =
105a2870726SGeorge Liu             entry.data() + sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t);
106a2870726SGeorge Liu         for (const auto& effecter : effecters)
107a2870726SGeorge Liu         {
108a2870726SGeorge Liu             auto set = effecter.value("set", empty);
109a2870726SGeorge Liu             state_effecter_possible_states* possibleStates =
110a2870726SGeorge Liu                 reinterpret_cast<state_effecter_possible_states*>(start);
111a2870726SGeorge Liu             possibleStates->state_set_id = set.value("id", 0);
112a2870726SGeorge Liu             possibleStates->possible_states_size = set.value("size", 0);
113a2870726SGeorge Liu 
114a2870726SGeorge Liu             start += sizeof(possibleStates->state_set_id) +
115a2870726SGeorge Liu                      sizeof(possibleStates->possible_states_size);
116a2870726SGeorge Liu             static const std::vector<uint8_t> emptyStates{};
117a2870726SGeorge Liu             PossibleValues stateValues;
118a2870726SGeorge Liu             auto states = set.value("states", emptyStates);
119a2870726SGeorge Liu             for (const auto& state : states)
120a2870726SGeorge Liu             {
121a2870726SGeorge Liu                 auto index = state / 8;
122a2870726SGeorge Liu                 auto bit = state - (index * 8);
123a2870726SGeorge Liu                 bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index);
124a2870726SGeorge Liu                 bf->byte |= 1 << bit;
125adbe1726SGeorge Liu                 stateValues.emplace_back(state);
126a2870726SGeorge Liu             }
127a2870726SGeorge Liu             start += possibleStates->possible_states_size;
128a2870726SGeorge Liu 
129a2870726SGeorge Liu             auto dbusEntry = effecter.value("dbus", empty);
130a2870726SGeorge Liu             auto objectPath = dbusEntry.value("path", "");
131a2870726SGeorge Liu             auto interface = dbusEntry.value("interface", "");
132a2870726SGeorge Liu             auto propertyName = dbusEntry.value("property_name", "");
133a2870726SGeorge Liu             auto propertyType = dbusEntry.value("property_type", "");
13436e81352SGeorge Liu 
13536e81352SGeorge Liu             try
13636e81352SGeorge Liu             {
13736e81352SGeorge Liu                 auto service =
13836e81352SGeorge Liu                     dBusIntf.getService(objectPath.c_str(), interface.c_str());
13936e81352SGeorge Liu             }
14036e81352SGeorge Liu             catch (const std::exception& e)
14136e81352SGeorge Liu             {
14236e81352SGeorge Liu                 continue;
14336e81352SGeorge Liu             }
14436e81352SGeorge Liu 
145a2870726SGeorge Liu             pldm::utils::DBusMapping dbusMapping{objectPath, interface,
146a2870726SGeorge Liu                                                  propertyName, propertyType};
147a2870726SGeorge Liu             dbusMappings.emplace_back(std::move(dbusMapping));
148a2870726SGeorge Liu 
149a2870726SGeorge Liu             Json propValues = dbusEntry["property_values"];
150a2870726SGeorge Liu             StatestoDbusVal dbusIdToValMap =
151a2870726SGeorge Liu                 populateMapping(propertyType, propValues, stateValues);
152a2870726SGeorge Liu             if (!dbusIdToValMap.empty())
153a2870726SGeorge Liu             {
154a2870726SGeorge Liu                 dbusValMaps.emplace_back(std::move(dbusIdToValMap));
155a2870726SGeorge Liu             }
156a2870726SGeorge Liu         }
157a2870726SGeorge Liu         handler.addDbusObjMaps(
158a2870726SGeorge Liu             pdr->effecter_id,
159a2870726SGeorge Liu             std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)));
160a2870726SGeorge Liu         PdrEntry pdrEntry{};
161a2870726SGeorge Liu         pdrEntry.data = entry.data();
162a2870726SGeorge Liu         pdrEntry.size = pdrSize;
163a2870726SGeorge Liu         repo.addRecord(pdrEntry);
164a2870726SGeorge Liu     }
165a2870726SGeorge Liu }
166a2870726SGeorge Liu 
167a2870726SGeorge Liu } // namespace pdr_state_effecter
168a2870726SGeorge Liu } // namespace responder
169a2870726SGeorge Liu } // namespace pldm
170