blob: 68cfb00aa15f56bdaa7b8165a99d8b7a3273c751 [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
George Liuc453e162022-12-21 17:16:23 +08006#include <libpldm/platform.h>
Manojkiran Edacc5f1582021-09-29 17:03:06 +05307
Riya Dixit49cfb132023-03-02 04:26:53 -06008#include <phosphor-logging/lg2.hpp>
9
10PHOSPHOR_LOG2_USING;
11
George Liua2870722020-02-11 11:09:30 +080012namespace pldm
13{
George Liua2870722020-02-11 11:09:30 +080014namespace responder
15{
George Liua2870722020-02-11 11:09:30 +080016namespace pdr_state_effecter
17{
George Liua2870722020-02-11 11:09:30 +080018using Json = nlohmann::json;
19
20static 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 */
George Liu36e81352020-07-01 14:40:30 +080029template <class DBusInterface, class Handler>
30void generateStateEffecterPDR(const DBusInterface& dBusIntf, const Json& json,
31 Handler& handler, pdr_utils::RepoInterface& repo)
George Liua2870722020-02-11 11:09:30 +080032{
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 {
Riya Dixit49cfb132023-03-02 04:26:53 -060045 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));
George Liua2870722020-02-11 11:09:30 +080049 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());
Manojkiran Edabcf91ac2021-03-14 13:50:48 +053061 if (!pdr)
62 {
Riya Dixit49cfb132023-03-02 04:26:53 -060063 error("Failed to get state effecter PDR.");
Manojkiran Edabcf91ac2021-03-14 13:50:48 +053064 continue;
65 }
George Liua2870722020-02-11 11:09:30 +080066 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
Manojkiran Edacc5f1582021-09-29 17:03:06 +053072 pdr->terminus_handle = TERMINUS_HANDLE;
George Liua2870722020-02-11 11:09:30 +080073 pdr->effecter_id = handler.getNextEffecterId();
George Liuc4ea6a92020-07-14 15:48:44 +080074
75 try
76 {
77 std::string entity_path = e.value("entity_path", "");
78 auto& associatedEntityMap = handler.getAssociateEntityMap();
Sagar Srinivas06f9b292024-03-31 11:35:28 -050079 if (entity_path != "" && associatedEntityMap.contains(entity_path))
George Liuc4ea6a92020-07-14 15:48:44 +080080 {
81 pdr->entity_type =
82 associatedEntityMap.at(entity_path).entity_type;
83 pdr->entity_instance =
84 associatedEntityMap.at(entity_path).entity_instance_num;
85 pdr->container_id =
86 associatedEntityMap.at(entity_path).entity_container_id;
87 }
88 else
89 {
90 pdr->entity_type = e.value("type", 0);
91 pdr->entity_instance = e.value("instance", 0);
92 pdr->container_id = e.value("container", 0);
Pavithra Barithaya5f213472022-08-29 02:20:19 -050093
94 // do not create the PDR when the FRU or the entity path is not
95 // present
96 if (!pdr->entity_type)
97 {
Pavithra Barithaya5f213472022-08-29 02:20:19 -050098 continue;
99 }
George Liuc4ea6a92020-07-14 15:48:44 +0800100 }
101 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500102 catch (const std::exception&)
George Liuc4ea6a92020-07-14 15:48:44 +0800103 {
104 pdr->entity_type = e.value("type", 0);
105 pdr->entity_instance = e.value("instance", 0);
106 pdr->container_id = e.value("container", 0);
107 }
108
George Liua2870722020-02-11 11:09:30 +0800109 pdr->effecter_semantic_id = 0;
110 pdr->effecter_init = PLDM_NO_INIT;
111 pdr->has_description_pdr = false;
112 pdr->composite_effecter_count = effecters.size();
113
Brad Bishop5079ac42021-08-19 18:35:06 -0400114 pldm::responder::pdr_utils::DbusMappings dbusMappings{};
115 pldm::responder::pdr_utils::DbusValMaps dbusValMaps{};
Patrick Williams6da4f912023-05-10 07:50:53 -0500116 uint8_t* start = entry.data() + sizeof(pldm_state_effecter_pdr) -
117 sizeof(uint8_t);
George Liua2870722020-02-11 11:09:30 +0800118 for (const auto& effecter : effecters)
119 {
120 auto set = effecter.value("set", empty);
121 state_effecter_possible_states* possibleStates =
122 reinterpret_cast<state_effecter_possible_states*>(start);
123 possibleStates->state_set_id = set.value("id", 0);
124 possibleStates->possible_states_size = set.value("size", 0);
125
126 start += sizeof(possibleStates->state_set_id) +
127 sizeof(possibleStates->possible_states_size);
128 static const std::vector<uint8_t> emptyStates{};
Brad Bishop5079ac42021-08-19 18:35:06 -0400129 pldm::responder::pdr_utils::PossibleValues stateValues;
George Liua2870722020-02-11 11:09:30 +0800130 auto states = set.value("states", emptyStates);
131 for (const auto& state : states)
132 {
133 auto index = state / 8;
134 auto bit = state - (index * 8);
135 bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index);
136 bf->byte |= 1 << bit;
George Liuadbe1722020-05-09 19:20:19 +0800137 stateValues.emplace_back(state);
George Liua2870722020-02-11 11:09:30 +0800138 }
139 start += possibleStates->possible_states_size;
140
141 auto dbusEntry = effecter.value("dbus", empty);
142 auto objectPath = dbusEntry.value("path", "");
143 auto interface = dbusEntry.value("interface", "");
144 auto propertyName = dbusEntry.value("property_name", "");
145 auto propertyType = dbusEntry.value("property_type", "");
George Liu36e81352020-07-01 14:40:30 +0800146
Brad Bishop5079ac42021-08-19 18:35:06 -0400147 pldm::responder::pdr_utils::StatestoDbusVal dbusIdToValMap{};
George Liu821ebc42021-01-26 14:36:11 +0800148 pldm::utils::DBusMapping dbusMapping{};
George Liu36e81352020-07-01 14:40:30 +0800149 try
150 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500151 auto service = dBusIntf.getService(objectPath.c_str(),
152 interface.c_str());
George Liu821ebc42021-01-26 14:36:11 +0800153
154 dbusMapping = pldm::utils::DBusMapping{
155 objectPath, interface, propertyName, propertyType};
Brad Bishop5079ac42021-08-19 18:35:06 -0400156 dbusIdToValMap = pldm::responder::pdr_utils::populateMapping(
George Liu821ebc42021-01-26 14:36:11 +0800157 propertyType, dbusEntry["property_values"], stateValues);
George Liu36e81352020-07-01 14:40:30 +0800158 }
159 catch (const std::exception& e)
160 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600161 error(
Manojkiran Edac81d1122024-02-21 20:17:34 +0530162 "Failed to create effecter PDR, D-Bus object '{PATH}' returned {ERROR}",
163 "PATH", objectPath, "ERROR", e);
164 continue;
George Liu36e81352020-07-01 14:40:30 +0800165 }
166
George Liua2870722020-02-11 11:09:30 +0800167 dbusMappings.emplace_back(std::move(dbusMapping));
George Liu821ebc42021-01-26 14:36:11 +0800168 dbusValMaps.emplace_back(std::move(dbusIdToValMap));
George Liua2870722020-02-11 11:09:30 +0800169 }
170 handler.addDbusObjMaps(
171 pdr->effecter_id,
172 std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)));
Brad Bishop5079ac42021-08-19 18:35:06 -0400173 pldm::responder::pdr_utils::PdrEntry pdrEntry{};
George Liua2870722020-02-11 11:09:30 +0800174 pdrEntry.data = entry.data();
175 pdrEntry.size = pdrSize;
176 repo.addRecord(pdrEntry);
177 }
178}
179
180} // namespace pdr_state_effecter
181} // namespace responder
182} // namespace pldm