blob: 85fb011ed19439c7edce805856008e5c4bd557ae [file] [log] [blame]
George Liua2870722020-02-11 11:09:30 +08001#pragma once
2
George Liua2870722020-02-11 11:09:30 +08003#include "libpldm/platform.h"
4
Sampa Misra12afe112020-05-25 11:40:44 -05005#include "pdr.hpp"
6#include "pdr_utils.hpp"
George Liu6492f522020-06-16 10:34:05 +08007
Manojkiran Edacc5f1582021-09-29 17:03:06 +05308#include <config.h>
9
George Liua2870722020-02-11 11:09:30 +080010namespace pldm
11{
12
13namespace responder
14{
15
16namespace pdr_state_effecter
17{
18
19using 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 {
46 std::cerr << "Malformed PDR JSON return "
47 "pdrEntry;- no state set "
48 "info, TYPE="
49 << PLDM_STATE_EFFECTER_PDR << "\n";
50 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 {
64 std::cerr << "Failed to get state effecter PDR.\n";
65 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);
95 }
96 }
97 catch (const std::exception& ex)
98 {
99 pdr->entity_type = e.value("type", 0);
100 pdr->entity_instance = e.value("instance", 0);
101 pdr->container_id = e.value("container", 0);
102 }
103
George Liua2870722020-02-11 11:09:30 +0800104 pdr->effecter_semantic_id = 0;
105 pdr->effecter_init = PLDM_NO_INIT;
106 pdr->has_description_pdr = false;
107 pdr->composite_effecter_count = effecters.size();
108
Brad Bishop5079ac42021-08-19 18:35:06 -0400109 pldm::responder::pdr_utils::DbusMappings dbusMappings{};
110 pldm::responder::pdr_utils::DbusValMaps dbusValMaps{};
George Liua2870722020-02-11 11:09:30 +0800111 uint8_t* start =
112 entry.data() + sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t);
113 for (const auto& effecter : effecters)
114 {
115 auto set = effecter.value("set", empty);
116 state_effecter_possible_states* possibleStates =
117 reinterpret_cast<state_effecter_possible_states*>(start);
118 possibleStates->state_set_id = set.value("id", 0);
119 possibleStates->possible_states_size = set.value("size", 0);
120
121 start += sizeof(possibleStates->state_set_id) +
122 sizeof(possibleStates->possible_states_size);
123 static const std::vector<uint8_t> emptyStates{};
Brad Bishop5079ac42021-08-19 18:35:06 -0400124 pldm::responder::pdr_utils::PossibleValues stateValues;
George Liua2870722020-02-11 11:09:30 +0800125 auto states = set.value("states", emptyStates);
126 for (const auto& state : states)
127 {
128 auto index = state / 8;
129 auto bit = state - (index * 8);
130 bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index);
131 bf->byte |= 1 << bit;
George Liuadbe1722020-05-09 19:20:19 +0800132 stateValues.emplace_back(state);
George Liua2870722020-02-11 11:09:30 +0800133 }
134 start += possibleStates->possible_states_size;
135
136 auto dbusEntry = effecter.value("dbus", empty);
137 auto objectPath = dbusEntry.value("path", "");
138 auto interface = dbusEntry.value("interface", "");
139 auto propertyName = dbusEntry.value("property_name", "");
140 auto propertyType = dbusEntry.value("property_type", "");
George Liu36e81352020-07-01 14:40:30 +0800141
Brad Bishop5079ac42021-08-19 18:35:06 -0400142 pldm::responder::pdr_utils::StatestoDbusVal dbusIdToValMap{};
George Liu821ebc42021-01-26 14:36:11 +0800143 pldm::utils::DBusMapping dbusMapping{};
George Liu36e81352020-07-01 14:40:30 +0800144 try
145 {
146 auto service =
147 dBusIntf.getService(objectPath.c_str(), interface.c_str());
George Liu821ebc42021-01-26 14:36:11 +0800148
149 dbusMapping = pldm::utils::DBusMapping{
150 objectPath, interface, propertyName, propertyType};
Brad Bishop5079ac42021-08-19 18:35:06 -0400151 dbusIdToValMap = pldm::responder::pdr_utils::populateMapping(
George Liu821ebc42021-01-26 14:36:11 +0800152 propertyType, dbusEntry["property_values"], stateValues);
George Liu36e81352020-07-01 14:40:30 +0800153 }
154 catch (const std::exception& e)
155 {
George Liu821ebc42021-01-26 14:36:11 +0800156 std::cerr << "D-Bus object path does not exist, effecter ID: "
157 << pdr->effecter_id << "\n";
George Liu36e81352020-07-01 14:40:30 +0800158 }
159
George Liua2870722020-02-11 11:09:30 +0800160 dbusMappings.emplace_back(std::move(dbusMapping));
George Liu821ebc42021-01-26 14:36:11 +0800161 dbusValMaps.emplace_back(std::move(dbusIdToValMap));
George Liua2870722020-02-11 11:09:30 +0800162 }
163 handler.addDbusObjMaps(
164 pdr->effecter_id,
165 std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)));
Brad Bishop5079ac42021-08-19 18:35:06 -0400166 pldm::responder::pdr_utils::PdrEntry pdrEntry{};
George Liua2870722020-02-11 11:09:30 +0800167 pdrEntry.data = entry.data();
168 pdrEntry.size = pdrSize;
169 repo.addRecord(pdrEntry);
170 }
171}
172
173} // namespace pdr_state_effecter
174} // namespace responder
175} // namespace pldm