xref: /openbmc/pldm/libpldmresponder/pdr_state_effecter.hpp (revision 821ebc47712a81a933f00a67a1bf43bd6093b8df)
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();
68c4ea6a90SGeorge Liu 
69c4ea6a90SGeorge Liu         try
70c4ea6a90SGeorge Liu         {
71c4ea6a90SGeorge Liu             std::string entity_path = e.value("entity_path", "");
72c4ea6a90SGeorge Liu             auto& associatedEntityMap = handler.getAssociateEntityMap();
73c4ea6a90SGeorge Liu             if (entity_path != "" && associatedEntityMap.find(entity_path) !=
74c4ea6a90SGeorge Liu                                          associatedEntityMap.end())
75c4ea6a90SGeorge Liu             {
76c4ea6a90SGeorge Liu                 pdr->entity_type =
77c4ea6a90SGeorge Liu                     associatedEntityMap.at(entity_path).entity_type;
78c4ea6a90SGeorge Liu                 pdr->entity_instance =
79c4ea6a90SGeorge Liu                     associatedEntityMap.at(entity_path).entity_instance_num;
80c4ea6a90SGeorge Liu                 pdr->container_id =
81c4ea6a90SGeorge Liu                     associatedEntityMap.at(entity_path).entity_container_id;
82c4ea6a90SGeorge Liu             }
83c4ea6a90SGeorge Liu             else
84c4ea6a90SGeorge 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);
88c4ea6a90SGeorge Liu             }
89c4ea6a90SGeorge Liu         }
90c4ea6a90SGeorge Liu         catch (const std::exception& ex)
91c4ea6a90SGeorge Liu         {
92c4ea6a90SGeorge Liu             pdr->entity_type = e.value("type", 0);
93c4ea6a90SGeorge Liu             pdr->entity_instance = e.value("instance", 0);
94c4ea6a90SGeorge Liu             pdr->container_id = e.value("container", 0);
95c4ea6a90SGeorge Liu         }
96c4ea6a90SGeorge 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 
135*821ebc47SGeorge Liu             StatestoDbusVal dbusIdToValMap{};
136*821ebc47SGeorge Liu             pldm::utils::DBusMapping dbusMapping{};
13736e81352SGeorge Liu             try
13836e81352SGeorge Liu             {
13936e81352SGeorge Liu                 auto service =
14036e81352SGeorge Liu                     dBusIntf.getService(objectPath.c_str(), interface.c_str());
141*821ebc47SGeorge Liu 
142*821ebc47SGeorge Liu                 dbusMapping = pldm::utils::DBusMapping{
143*821ebc47SGeorge Liu                     objectPath, interface, propertyName, propertyType};
144*821ebc47SGeorge Liu                 dbusIdToValMap = populateMapping(
145*821ebc47SGeorge Liu                     propertyType, dbusEntry["property_values"], stateValues);
14636e81352SGeorge Liu             }
14736e81352SGeorge Liu             catch (const std::exception& e)
14836e81352SGeorge Liu             {
149*821ebc47SGeorge Liu                 std::cerr << "D-Bus object path does not exist, effecter ID: "
150*821ebc47SGeorge Liu                           << pdr->effecter_id << "\n";
15136e81352SGeorge Liu             }
15236e81352SGeorge Liu 
153a2870726SGeorge Liu             dbusMappings.emplace_back(std::move(dbusMapping));
154a2870726SGeorge Liu             dbusValMaps.emplace_back(std::move(dbusIdToValMap));
155a2870726SGeorge Liu         }
156a2870726SGeorge Liu         handler.addDbusObjMaps(
157a2870726SGeorge Liu             pdr->effecter_id,
158a2870726SGeorge Liu             std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)));
159a2870726SGeorge Liu         PdrEntry pdrEntry{};
160a2870726SGeorge Liu         pdrEntry.data = entry.data();
161a2870726SGeorge Liu         pdrEntry.size = pdrSize;
162a2870726SGeorge Liu         repo.addRecord(pdrEntry);
163a2870726SGeorge Liu     }
164a2870726SGeorge Liu }
165a2870726SGeorge Liu 
166a2870726SGeorge Liu } // namespace pdr_state_effecter
167a2870726SGeorge Liu } // namespace responder
168a2870726SGeorge Liu } // namespace pldm
169