blob: db76086dfcd5077cdbac29c2f3a149b4893873b4 [file] [log] [blame]
George Liua2870722020-02-11 11:09:30 +08001#pragma once
2
Sampa Misra12afe112020-05-25 11:40:44 -05003#include "pdr.hpp"
4#include "pdr_utils.hpp"
George Liu6492f522020-06-16 10:34:05 +08005
Manojkiran Edacc5f1582021-09-29 17:03:06 +05306#include <config.h>
George Liuc453e162022-12-21 17:16:23 +08007#include <libpldm/platform.h>
Manojkiran Edacc5f1582021-09-29 17:03:06 +05308
Riya Dixit49cfb132023-03-02 04:26:53 -06009#include <phosphor-logging/lg2.hpp>
10
11PHOSPHOR_LOG2_USING;
12
George Liua2870722020-02-11 11:09:30 +080013namespace pldm
14{
George Liua2870722020-02-11 11:09:30 +080015namespace responder
16{
George Liua2870722020-02-11 11:09:30 +080017namespace pdr_state_effecter
18{
George Liua2870722020-02-11 11:09:30 +080019using Json = nlohmann::json;
20
21static const Json empty{};
22
23/** @brief Parse PDR JSON file and generate state effecter PDR structure
24 *
25 * @param[in] json - the JSON Object with the state effecter PDR
26 * @param[out] handler - the Parser of PLDM command handler
27 * @param[out] repo - pdr::RepoInterface
28 *
29 */
George Liu36e81352020-07-01 14:40:30 +080030template <class DBusInterface, class Handler>
31void generateStateEffecterPDR(const DBusInterface& dBusIntf, const Json& json,
32 Handler& handler, pdr_utils::RepoInterface& repo)
George Liua2870722020-02-11 11:09:30 +080033{
34 static const std::vector<Json> emptyList{};
35 auto entries = json.value("entries", emptyList);
36 for (const auto& e : entries)
37 {
38 size_t pdrSize = 0;
39 auto effecters = e.value("effecters", emptyList);
40 for (const auto& effecter : effecters)
41 {
42 auto set = effecter.value("set", empty);
43 auto statesSize = set.value("size", 0);
44 if (!statesSize)
45 {
Riya Dixit49cfb132023-03-02 04:26:53 -060046 error(
47 "Malformed PDR JSON return pdrEntry;- no state set info, TYPE={STATE_EFFECTER_PDR}",
48 "STATE_EFFECTER_PDR",
49 static_cast<unsigned>(PLDM_STATE_EFFECTER_PDR));
George Liua2870722020-02-11 11:09:30 +080050 throw InternalFailure();
51 }
52 pdrSize += sizeof(state_effecter_possible_states) -
53 sizeof(bitfield8_t) + (sizeof(bitfield8_t) * statesSize);
54 }
55 pdrSize += sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t);
56
57 std::vector<uint8_t> entry{};
58 entry.resize(pdrSize);
59
60 pldm_state_effecter_pdr* pdr =
61 reinterpret_cast<pldm_state_effecter_pdr*>(entry.data());
Manojkiran Edabcf91ac2021-03-14 13:50:48 +053062 if (!pdr)
63 {
Riya Dixit49cfb132023-03-02 04:26:53 -060064 error("Failed to get state effecter PDR.");
Manojkiran Edabcf91ac2021-03-14 13:50:48 +053065 continue;
66 }
George Liua2870722020-02-11 11:09:30 +080067 pdr->hdr.record_handle = 0;
68 pdr->hdr.version = 1;
69 pdr->hdr.type = PLDM_STATE_EFFECTER_PDR;
70 pdr->hdr.record_change_num = 0;
71 pdr->hdr.length = pdrSize - sizeof(pldm_pdr_hdr);
72
Manojkiran Edacc5f1582021-09-29 17:03:06 +053073 pdr->terminus_handle = TERMINUS_HANDLE;
George Liua2870722020-02-11 11:09:30 +080074 pdr->effecter_id = handler.getNextEffecterId();
George Liuc4ea6a92020-07-14 15:48:44 +080075
76 try
77 {
78 std::string entity_path = e.value("entity_path", "");
79 auto& associatedEntityMap = handler.getAssociateEntityMap();
80 if (entity_path != "" && associatedEntityMap.find(entity_path) !=
81 associatedEntityMap.end())
82 {
83 pdr->entity_type =
84 associatedEntityMap.at(entity_path).entity_type;
85 pdr->entity_instance =
86 associatedEntityMap.at(entity_path).entity_instance_num;
87 pdr->container_id =
88 associatedEntityMap.at(entity_path).entity_container_id;
89 }
90 else
91 {
92 pdr->entity_type = e.value("type", 0);
93 pdr->entity_instance = e.value("instance", 0);
94 pdr->container_id = e.value("container", 0);
Pavithra Barithaya5f213472022-08-29 02:20:19 -050095
96 // do not create the PDR when the FRU or the entity path is not
97 // present
98 if (!pdr->entity_type)
99 {
100 std::cerr << "The entity path for the FRU is not present."
101 << std::endl;
102 continue;
103 }
George Liuc4ea6a92020-07-14 15:48:44 +0800104 }
105 }
106 catch (const std::exception& ex)
107 {
108 pdr->entity_type = e.value("type", 0);
109 pdr->entity_instance = e.value("instance", 0);
110 pdr->container_id = e.value("container", 0);
111 }
112
George Liua2870722020-02-11 11:09:30 +0800113 pdr->effecter_semantic_id = 0;
114 pdr->effecter_init = PLDM_NO_INIT;
115 pdr->has_description_pdr = false;
116 pdr->composite_effecter_count = effecters.size();
117
Brad Bishop5079ac42021-08-19 18:35:06 -0400118 pldm::responder::pdr_utils::DbusMappings dbusMappings{};
119 pldm::responder::pdr_utils::DbusValMaps dbusValMaps{};
George Liua2870722020-02-11 11:09:30 +0800120 uint8_t* start =
121 entry.data() + sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t);
122 for (const auto& effecter : effecters)
123 {
124 auto set = effecter.value("set", empty);
125 state_effecter_possible_states* possibleStates =
126 reinterpret_cast<state_effecter_possible_states*>(start);
127 possibleStates->state_set_id = set.value("id", 0);
128 possibleStates->possible_states_size = set.value("size", 0);
129
130 start += sizeof(possibleStates->state_set_id) +
131 sizeof(possibleStates->possible_states_size);
132 static const std::vector<uint8_t> emptyStates{};
Brad Bishop5079ac42021-08-19 18:35:06 -0400133 pldm::responder::pdr_utils::PossibleValues stateValues;
George Liua2870722020-02-11 11:09:30 +0800134 auto states = set.value("states", emptyStates);
135 for (const auto& state : states)
136 {
137 auto index = state / 8;
138 auto bit = state - (index * 8);
139 bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index);
140 bf->byte |= 1 << bit;
George Liuadbe1722020-05-09 19:20:19 +0800141 stateValues.emplace_back(state);
George Liua2870722020-02-11 11:09:30 +0800142 }
143 start += possibleStates->possible_states_size;
144
145 auto dbusEntry = effecter.value("dbus", empty);
146 auto objectPath = dbusEntry.value("path", "");
147 auto interface = dbusEntry.value("interface", "");
148 auto propertyName = dbusEntry.value("property_name", "");
149 auto propertyType = dbusEntry.value("property_type", "");
George Liu36e81352020-07-01 14:40:30 +0800150
Brad Bishop5079ac42021-08-19 18:35:06 -0400151 pldm::responder::pdr_utils::StatestoDbusVal dbusIdToValMap{};
George Liu821ebc42021-01-26 14:36:11 +0800152 pldm::utils::DBusMapping dbusMapping{};
George Liu36e81352020-07-01 14:40:30 +0800153 try
154 {
155 auto service =
156 dBusIntf.getService(objectPath.c_str(), interface.c_str());
George Liu821ebc42021-01-26 14:36:11 +0800157
158 dbusMapping = pldm::utils::DBusMapping{
159 objectPath, interface, propertyName, propertyType};
Brad Bishop5079ac42021-08-19 18:35:06 -0400160 dbusIdToValMap = pldm::responder::pdr_utils::populateMapping(
George Liu821ebc42021-01-26 14:36:11 +0800161 propertyType, dbusEntry["property_values"], stateValues);
George Liu36e81352020-07-01 14:40:30 +0800162 }
163 catch (const std::exception& e)
164 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600165 error(
166 "D-Bus object path does not exist, effecter ID: {EFFECTER_ID}",
167 "EFFECTER_ID", static_cast<uint16_t>(pdr->effecter_id));
George Liu36e81352020-07-01 14:40:30 +0800168 }
169
George Liua2870722020-02-11 11:09:30 +0800170 dbusMappings.emplace_back(std::move(dbusMapping));
George Liu821ebc42021-01-26 14:36:11 +0800171 dbusValMaps.emplace_back(std::move(dbusIdToValMap));
George Liua2870722020-02-11 11:09:30 +0800172 }
173 handler.addDbusObjMaps(
174 pdr->effecter_id,
175 std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)));
Brad Bishop5079ac42021-08-19 18:35:06 -0400176 pldm::responder::pdr_utils::PdrEntry pdrEntry{};
George Liua2870722020-02-11 11:09:30 +0800177 pdrEntry.data = entry.data();
178 pdrEntry.size = pdrSize;
179 repo.addRecord(pdrEntry);
180 }
181}
182
183} // namespace pdr_state_effecter
184} // namespace responder
185} // namespace pldm