xref: /openbmc/pldm/libpldmresponder/pdr_state_effecter.hpp (revision cc5f1586972a6d0a3465d90b1a139b63fd2f4f10)
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 
8*cc5f1586SManojkiran Eda #include <config.h>
9*cc5f1586SManojkiran Eda 
10a2870726SGeorge Liu namespace pldm
11a2870726SGeorge Liu {
12a2870726SGeorge Liu 
13a2870726SGeorge Liu namespace responder
14a2870726SGeorge Liu {
15a2870726SGeorge Liu 
16a2870726SGeorge Liu namespace pdr_state_effecter
17a2870726SGeorge Liu {
18a2870726SGeorge Liu 
19a2870726SGeorge Liu using Json = nlohmann::json;
20a2870726SGeorge Liu 
21a2870726SGeorge Liu static const Json empty{};
22a2870726SGeorge Liu 
23a2870726SGeorge Liu /** @brief Parse PDR JSON file and generate state effecter PDR structure
24a2870726SGeorge Liu  *
25a2870726SGeorge Liu  *  @param[in] json - the JSON Object with the state effecter PDR
26a2870726SGeorge Liu  *  @param[out] handler - the Parser of PLDM command handler
27a2870726SGeorge Liu  *  @param[out] repo - pdr::RepoInterface
28a2870726SGeorge Liu  *
29a2870726SGeorge Liu  */
3036e81352SGeorge Liu template <class DBusInterface, class Handler>
3136e81352SGeorge Liu void generateStateEffecterPDR(const DBusInterface& dBusIntf, const Json& json,
3236e81352SGeorge Liu                               Handler& handler, pdr_utils::RepoInterface& repo)
33a2870726SGeorge Liu {
34a2870726SGeorge Liu     static const std::vector<Json> emptyList{};
35a2870726SGeorge Liu     auto entries = json.value("entries", emptyList);
36a2870726SGeorge Liu     for (const auto& e : entries)
37a2870726SGeorge Liu     {
38a2870726SGeorge Liu         size_t pdrSize = 0;
39a2870726SGeorge Liu         auto effecters = e.value("effecters", emptyList);
40a2870726SGeorge Liu         for (const auto& effecter : effecters)
41a2870726SGeorge Liu         {
42a2870726SGeorge Liu             auto set = effecter.value("set", empty);
43a2870726SGeorge Liu             auto statesSize = set.value("size", 0);
44a2870726SGeorge Liu             if (!statesSize)
45a2870726SGeorge Liu             {
46a2870726SGeorge Liu                 std::cerr << "Malformed PDR JSON return "
47a2870726SGeorge Liu                              "pdrEntry;- no state set "
48a2870726SGeorge Liu                              "info, TYPE="
49a2870726SGeorge Liu                           << PLDM_STATE_EFFECTER_PDR << "\n";
50a2870726SGeorge Liu                 throw InternalFailure();
51a2870726SGeorge Liu             }
52a2870726SGeorge Liu             pdrSize += sizeof(state_effecter_possible_states) -
53a2870726SGeorge Liu                        sizeof(bitfield8_t) + (sizeof(bitfield8_t) * statesSize);
54a2870726SGeorge Liu         }
55a2870726SGeorge Liu         pdrSize += sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t);
56a2870726SGeorge Liu 
57a2870726SGeorge Liu         std::vector<uint8_t> entry{};
58a2870726SGeorge Liu         entry.resize(pdrSize);
59a2870726SGeorge Liu 
60a2870726SGeorge Liu         pldm_state_effecter_pdr* pdr =
61a2870726SGeorge Liu             reinterpret_cast<pldm_state_effecter_pdr*>(entry.data());
62bcf91accSManojkiran Eda         if (!pdr)
63bcf91accSManojkiran Eda         {
64bcf91accSManojkiran Eda             std::cerr << "Failed to get state effecter PDR.\n";
65bcf91accSManojkiran Eda             continue;
66bcf91accSManojkiran Eda         }
67a2870726SGeorge Liu         pdr->hdr.record_handle = 0;
68a2870726SGeorge Liu         pdr->hdr.version = 1;
69a2870726SGeorge Liu         pdr->hdr.type = PLDM_STATE_EFFECTER_PDR;
70a2870726SGeorge Liu         pdr->hdr.record_change_num = 0;
71a2870726SGeorge Liu         pdr->hdr.length = pdrSize - sizeof(pldm_pdr_hdr);
72a2870726SGeorge Liu 
73*cc5f1586SManojkiran Eda         pdr->terminus_handle = TERMINUS_HANDLE;
74a2870726SGeorge Liu         pdr->effecter_id = handler.getNextEffecterId();
75c4ea6a90SGeorge Liu 
76c4ea6a90SGeorge Liu         try
77c4ea6a90SGeorge Liu         {
78c4ea6a90SGeorge Liu             std::string entity_path = e.value("entity_path", "");
79c4ea6a90SGeorge Liu             auto& associatedEntityMap = handler.getAssociateEntityMap();
80c4ea6a90SGeorge Liu             if (entity_path != "" && associatedEntityMap.find(entity_path) !=
81c4ea6a90SGeorge Liu                                          associatedEntityMap.end())
82c4ea6a90SGeorge Liu             {
83c4ea6a90SGeorge Liu                 pdr->entity_type =
84c4ea6a90SGeorge Liu                     associatedEntityMap.at(entity_path).entity_type;
85c4ea6a90SGeorge Liu                 pdr->entity_instance =
86c4ea6a90SGeorge Liu                     associatedEntityMap.at(entity_path).entity_instance_num;
87c4ea6a90SGeorge Liu                 pdr->container_id =
88c4ea6a90SGeorge Liu                     associatedEntityMap.at(entity_path).entity_container_id;
89c4ea6a90SGeorge Liu             }
90c4ea6a90SGeorge Liu             else
91c4ea6a90SGeorge Liu             {
92a2870726SGeorge Liu                 pdr->entity_type = e.value("type", 0);
93a2870726SGeorge Liu                 pdr->entity_instance = e.value("instance", 0);
94a2870726SGeorge Liu                 pdr->container_id = e.value("container", 0);
95c4ea6a90SGeorge Liu             }
96c4ea6a90SGeorge Liu         }
97c4ea6a90SGeorge Liu         catch (const std::exception& ex)
98c4ea6a90SGeorge Liu         {
99c4ea6a90SGeorge Liu             pdr->entity_type = e.value("type", 0);
100c4ea6a90SGeorge Liu             pdr->entity_instance = e.value("instance", 0);
101c4ea6a90SGeorge Liu             pdr->container_id = e.value("container", 0);
102c4ea6a90SGeorge Liu         }
103c4ea6a90SGeorge Liu 
104a2870726SGeorge Liu         pdr->effecter_semantic_id = 0;
105a2870726SGeorge Liu         pdr->effecter_init = PLDM_NO_INIT;
106a2870726SGeorge Liu         pdr->has_description_pdr = false;
107a2870726SGeorge Liu         pdr->composite_effecter_count = effecters.size();
108a2870726SGeorge Liu 
1095079ac4aSBrad Bishop         pldm::responder::pdr_utils::DbusMappings dbusMappings{};
1105079ac4aSBrad Bishop         pldm::responder::pdr_utils::DbusValMaps dbusValMaps{};
111a2870726SGeorge Liu         uint8_t* start =
112a2870726SGeorge Liu             entry.data() + sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t);
113a2870726SGeorge Liu         for (const auto& effecter : effecters)
114a2870726SGeorge Liu         {
115a2870726SGeorge Liu             auto set = effecter.value("set", empty);
116a2870726SGeorge Liu             state_effecter_possible_states* possibleStates =
117a2870726SGeorge Liu                 reinterpret_cast<state_effecter_possible_states*>(start);
118a2870726SGeorge Liu             possibleStates->state_set_id = set.value("id", 0);
119a2870726SGeorge Liu             possibleStates->possible_states_size = set.value("size", 0);
120a2870726SGeorge Liu 
121a2870726SGeorge Liu             start += sizeof(possibleStates->state_set_id) +
122a2870726SGeorge Liu                      sizeof(possibleStates->possible_states_size);
123a2870726SGeorge Liu             static const std::vector<uint8_t> emptyStates{};
1245079ac4aSBrad Bishop             pldm::responder::pdr_utils::PossibleValues stateValues;
125a2870726SGeorge Liu             auto states = set.value("states", emptyStates);
126a2870726SGeorge Liu             for (const auto& state : states)
127a2870726SGeorge Liu             {
128a2870726SGeorge Liu                 auto index = state / 8;
129a2870726SGeorge Liu                 auto bit = state - (index * 8);
130a2870726SGeorge Liu                 bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index);
131a2870726SGeorge Liu                 bf->byte |= 1 << bit;
132adbe1726SGeorge Liu                 stateValues.emplace_back(state);
133a2870726SGeorge Liu             }
134a2870726SGeorge Liu             start += possibleStates->possible_states_size;
135a2870726SGeorge Liu 
136a2870726SGeorge Liu             auto dbusEntry = effecter.value("dbus", empty);
137a2870726SGeorge Liu             auto objectPath = dbusEntry.value("path", "");
138a2870726SGeorge Liu             auto interface = dbusEntry.value("interface", "");
139a2870726SGeorge Liu             auto propertyName = dbusEntry.value("property_name", "");
140a2870726SGeorge Liu             auto propertyType = dbusEntry.value("property_type", "");
14136e81352SGeorge Liu 
1425079ac4aSBrad Bishop             pldm::responder::pdr_utils::StatestoDbusVal dbusIdToValMap{};
143821ebc47SGeorge Liu             pldm::utils::DBusMapping dbusMapping{};
14436e81352SGeorge Liu             try
14536e81352SGeorge Liu             {
14636e81352SGeorge Liu                 auto service =
14736e81352SGeorge Liu                     dBusIntf.getService(objectPath.c_str(), interface.c_str());
148821ebc47SGeorge Liu 
149821ebc47SGeorge Liu                 dbusMapping = pldm::utils::DBusMapping{
150821ebc47SGeorge Liu                     objectPath, interface, propertyName, propertyType};
1515079ac4aSBrad Bishop                 dbusIdToValMap = pldm::responder::pdr_utils::populateMapping(
152821ebc47SGeorge Liu                     propertyType, dbusEntry["property_values"], stateValues);
15336e81352SGeorge Liu             }
15436e81352SGeorge Liu             catch (const std::exception& e)
15536e81352SGeorge Liu             {
156821ebc47SGeorge Liu                 std::cerr << "D-Bus object path does not exist, effecter ID: "
157821ebc47SGeorge Liu                           << pdr->effecter_id << "\n";
15836e81352SGeorge Liu             }
15936e81352SGeorge Liu 
160a2870726SGeorge Liu             dbusMappings.emplace_back(std::move(dbusMapping));
161a2870726SGeorge Liu             dbusValMaps.emplace_back(std::move(dbusIdToValMap));
162a2870726SGeorge Liu         }
163a2870726SGeorge Liu         handler.addDbusObjMaps(
164a2870726SGeorge Liu             pdr->effecter_id,
165a2870726SGeorge Liu             std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)));
1665079ac4aSBrad Bishop         pldm::responder::pdr_utils::PdrEntry pdrEntry{};
167a2870726SGeorge Liu         pdrEntry.data = entry.data();
168a2870726SGeorge Liu         pdrEntry.size = pdrSize;
169a2870726SGeorge Liu         repo.addRecord(pdrEntry);
170a2870726SGeorge Liu     }
171a2870726SGeorge Liu }
172a2870726SGeorge Liu 
173a2870726SGeorge Liu } // namespace pdr_state_effecter
174a2870726SGeorge Liu } // namespace responder
175a2870726SGeorge Liu } // namespace pldm
176