blob: b2fc092ec0d0dcadcfebb33e412d274efd9dc746 [file] [log] [blame]
Tom Joseph250c4752020-04-15 10:32:45 +05301#include "dbus_to_host_effecters.hpp"
2
3#include "libpldm/pdr.h"
4#include "libpldm/platform.h"
5#include "libpldm/requester/pldm.h"
6
7#include <xyz/openbmc_project/Common/error.hpp>
8#include <xyz/openbmc_project/State/OperatingSystem/Status/server.hpp>
9
10#include <fstream>
11#include <iostream>
12
13namespace pldm
14{
15namespace host_effecters
16{
17
18using InternalFailure =
19 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
20
21constexpr auto hostEffecterJson = "dbus_to_host_effecter.json";
22
23void HostEffecterParser::populatePropVals(
24 const Json& dBusValues, std::vector<PropertyValue>& propertyValues,
25 const std::string& propertyType)
26
27{
28 for (const auto& elem : dBusValues)
29 {
30 auto value = jsonEntryToDbusVal(propertyType, elem);
31 propertyValues.emplace_back(value);
32 }
33}
34
35void HostEffecterParser::parseEffecterJson(const std::string& jsonPath)
36{
37 fs::path jsonDir(jsonPath);
38 if (!fs::exists(jsonDir) || fs::is_empty(jsonDir))
39 {
40 std::cerr << "Host Effecter json path does not exist, DIR=" << jsonPath
41 << "\n";
42 return;
43 }
44
45 fs::path jsonFilePath = jsonDir / hostEffecterJson;
46 if (!fs::exists(jsonFilePath))
47 {
48 std::cerr << "json does not exist, PATH=" << jsonFilePath << "\n";
49 throw InternalFailure();
50 }
51
52 std::ifstream jsonFile(jsonFilePath);
53 auto data = Json::parse(jsonFile, nullptr, false);
54 if (data.is_discarded())
55 {
56 std::cerr << "Parsing json file failed, FILE=" << jsonFilePath << "\n";
57 throw InternalFailure();
58 }
59 const Json empty{};
60 const std::vector<Json> emptyList{};
61
62 auto entries = data.value("entries", emptyList);
63 for (const auto& entry : entries)
64 {
65 EffecterInfo effecterInfo;
66 effecterInfo.mctpEid = entry.value("mctp_eid", 0xFF);
67 auto jsonEffecterInfo = entry.value("effecter_info", empty);
68 auto effecterId =
69 jsonEffecterInfo.value("effecterID", PLDM_INVALID_EFFECTER_ID);
70 effecterInfo.containerId = jsonEffecterInfo.value("containerID", 0);
71 effecterInfo.entityType = jsonEffecterInfo.value("entityType", 0);
72 effecterInfo.entityInstance =
73 jsonEffecterInfo.value("entityInstance", 0);
74 effecterInfo.compEffecterCnt =
75 jsonEffecterInfo.value("compositeEffecterCount", 0);
76 auto effecters = entry.value("effecters", emptyList);
77 for (const auto& effecter : effecters)
78 {
79 DBusEffecterMapping dbusInfo{};
80 auto jsonDbusInfo = effecter.value("dbus_info", empty);
81 dbusInfo.dbusMap.objectPath = jsonDbusInfo.value("object_path", "");
82 dbusInfo.dbusMap.interface = jsonDbusInfo.value("interface", "");
83 dbusInfo.dbusMap.propertyName =
84 jsonDbusInfo.value("property_name", "");
85 dbusInfo.dbusMap.propertyType =
86 jsonDbusInfo.value("property_type", "");
87 Json propertyValues = jsonDbusInfo["property_values"];
88
89 populatePropVals(propertyValues, dbusInfo.propertyValues,
90 dbusInfo.dbusMap.propertyType);
91
92 const std::vector<uint8_t> emptyStates{};
93 auto state = effecter.value("state", empty);
94 dbusInfo.state.stateSetId = state.value("id", 0);
95 auto states = state.value("state_values", emptyStates);
96 if (dbusInfo.propertyValues.size() != states.size())
97 {
98 std::cerr << "Number of states do not match with"
99 << " number of D-Bus property values in the json. "
100 << "Object path " << dbusInfo.dbusMap.objectPath
101 << " and property " << dbusInfo.dbusMap.propertyName
102 << " will not be monitored \n";
103 continue;
104 }
105 for (const auto& s : states)
106 {
107 dbusInfo.state.states.emplace_back(s);
108 }
109
110 auto effecterInfoIndex = hostEffecterInfo.size();
111 auto dbusInfoIndex = effecterInfo.dbusInfo.size();
112 createHostEffecterMatch(
113 dbusInfo.dbusMap.objectPath, dbusInfo.dbusMap.interface,
114 effecterInfoIndex, dbusInfoIndex, effecterId);
115 effecterInfo.dbusInfo.emplace_back(std::move(dbusInfo));
116 }
117 hostEffecterInfo.emplace_back(std::move(effecterInfo));
118 }
119}
120
121void HostEffecterParser::processHostEffecterChangeNotification(
122 const DbusChgHostEffecterProps& chProperties, size_t effecterInfoIndex,
123 size_t dbusInfoIndex, uint16_t effecterId)
124{
125 const auto& propertyName = hostEffecterInfo[effecterInfoIndex]
126 .dbusInfo[dbusInfoIndex]
127 .dbusMap.propertyName;
128
129 const auto& it = chProperties.find(propertyName);
130
131 if (it == chProperties.end())
132 {
133 return;
134 }
135
136 if (effecterId == PLDM_INVALID_EFFECTER_ID)
137 {
138 effecterId = findStateEffecterId(
139 pdrRepo, hostEffecterInfo[effecterInfoIndex].entityType,
140 hostEffecterInfo[effecterInfoIndex].entityInstance,
141 hostEffecterInfo[effecterInfoIndex].containerId,
142 hostEffecterInfo[effecterInfoIndex]
143 .dbusInfo[dbusInfoIndex]
144 .state.stateSetId);
145 if (effecterId == PLDM_INVALID_EFFECTER_ID)
146 {
147 std::cerr << "Effecter id not found in pdr repo \n";
148 return;
149 }
150 }
151 constexpr auto hostStateInterface =
152 "xyz.openbmc_project.State.OperatingSystem.Status";
153 constexpr auto hostStatePath = "/xyz/openbmc_project/state/host0";
154
155 try
156 {
157 auto propVal = dbusHandler->getDbusPropertyVariant(
158 hostStatePath, "OperatingSystemState", hostStateInterface);
159 const auto& currHostState = std::get<std::string>(propVal);
160 if ((currHostState != "xyz.openbmc_project.State.OperatingSystem."
161 "Status.OSStatus.Standby") &&
162 (currHostState != "xyz.openbmc_project.State.OperatingSystem."
163 "Status.OSStatus.BootComplete"))
164 {
165 std::cout << "Host is not up. Current host state: "
166 << currHostState.c_str() << "\n";
167 return;
168 }
169 }
170 catch (const sdbusplus::exception::SdBusError& e)
171 {
172 std::cerr << "Error in getting current host state. Will still "
173 "continue to set the host effecter \n";
174 }
175 uint8_t newState{};
176 try
177 {
178 newState =
179 findNewStateValue(effecterInfoIndex, dbusInfoIndex, it->second);
180 }
181 catch (const std::out_of_range& e)
182 {
183 std::cerr << "new state not found in json"
184 << "\n";
185 return;
186 }
187
188 std::vector<set_effecter_state_field> stateField;
189 for (uint8_t i = 0; i < hostEffecterInfo[effecterInfoIndex].compEffecterCnt;
190 i++)
191 {
192 if (i == dbusInfoIndex)
193 {
194 stateField.push_back({PLDM_REQUEST_SET, newState});
195 }
196 else
197 {
198 stateField.push_back({PLDM_NO_CHANGE, 0});
199 }
200 }
201 int rc{};
202 try
203 {
204 rc = setHostStateEffecter(effecterInfoIndex, stateField, effecterId);
205 }
206 catch (const std::runtime_error& e)
207 {
208 std::cerr << "Could not set host state effecter \n";
209 return;
210 }
211 if (rc != PLDM_SUCCESS)
212 {
213 std::cerr << "Could not set the host state effecter, rc= " << rc
214 << " \n";
215 }
216}
217
218uint8_t
219 HostEffecterParser::findNewStateValue(size_t effecterInfoIndex,
220 size_t dbusInfoIndex,
221 const PropertyValue& propertyValue)
222{
223 const auto& propValues = hostEffecterInfo[effecterInfoIndex]
224 .dbusInfo[dbusInfoIndex]
225 .propertyValues;
226 auto it = std::find(propValues.begin(), propValues.end(), propertyValue);
227 uint8_t newState{};
228 if (it != propValues.end())
229 {
230 auto index = std::distance(propValues.begin(), it);
231 newState = hostEffecterInfo[effecterInfoIndex]
232 .dbusInfo[dbusInfoIndex]
233 .state.states[index];
234 }
235 else
236 {
237 throw std::out_of_range("new state not found in json");
238 }
239 return newState;
240}
241
242int HostEffecterParser::setHostStateEffecter(
243 size_t effecterInfoIndex, std::vector<set_effecter_state_field>& stateField,
244 uint16_t effecterId)
245{
246 uint8_t& mctpEid = hostEffecterInfo[effecterInfoIndex].mctpEid;
247 uint8_t& compEffCnt = hostEffecterInfo[effecterInfoIndex].compEffecterCnt;
248 auto instanceId = requester->getInstanceId(mctpEid);
249
250 std::vector<uint8_t> requestMsg(
251 sizeof(pldm_msg_hdr) + sizeof(effecterId) + sizeof(compEffCnt) +
252 sizeof(set_effecter_state_field) * compEffCnt,
253 0);
254 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
255 auto rc = encode_set_state_effecter_states_req(
256 instanceId, effecterId, compEffCnt, stateField.data(), request);
257
258 if (rc != PLDM_SUCCESS)
259 {
260 std::cerr << "Message encode failure. PLDM error code = " << std::hex
261 << std::showbase << rc << "\n";
262 requester->markFree(mctpEid, instanceId);
263 return rc;
264 }
265
266 if (verbose)
267 {
268 if (requestMsg.size())
269 {
270 std::ostringstream tempStream;
271 for (int byte : requestMsg)
272 {
273 tempStream << std::setfill('0') << std::setw(2) << std::hex
274 << byte << " ";
275 }
276 std::cout << tempStream.str() << std::endl;
277 }
278 }
279
280 uint8_t* responseMsg = nullptr;
281 size_t responseMsgSize{};
282
283 rc = pldm_send_recv(mctpEid, sockFd, requestMsg.data(), requestMsg.size(),
284 &responseMsg, &responseMsgSize);
285 std::unique_ptr<uint8_t, decltype(std::free)*> responseMsgPtr{responseMsg,
286 std::free};
287 requester->markFree(mctpEid, instanceId);
288
289 if (rc != PLDM_REQUESTER_SUCCESS)
290 {
291 std::cerr << "Failed to send message/receive response. RC = " << rc
292 << ", errno = " << errno << " for setting host effecter "
293 << effecterId << "\n";
294 pldm::utils::reportError(
295 "xyz.openbmc_project.bmc.pldm.InternalFailure");
296 return rc;
297 }
298 auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsgPtr.get());
299 uint8_t completionCode{};
300 rc = decode_set_state_effecter_states_resp(
301 responsePtr, responseMsgSize - sizeof(pldm_msg_hdr), &completionCode);
302 if (rc != PLDM_SUCCESS)
303 {
304 std::cerr << "Failed to decode setStateEffecterStates response, rc = "
305 << rc << "\n";
306 return rc;
307 }
308 if (completionCode != PLDM_SUCCESS)
309 {
310 std::cerr << "Failed setStateEffecterStates for effecter " << effecterId
311 << ". Response from Host " << (uint32_t)completionCode
312 << "\n";
313 pldm::utils::reportError(
314 "xyz.openbmc_project.bmc.pldm.InternalFailure");
315 }
316 return rc;
317}
318
319void HostEffecterParser::createHostEffecterMatch(const std::string& objectPath,
320 const std::string& interface,
321 size_t effecterInfoIndex,
322 size_t dbusInfoIndex,
323 uint16_t effecterId)
324{
325 using namespace sdbusplus::bus::match::rules;
326 effecterInfoMatch.emplace_back(
327 std::make_unique<sdbusplus::bus::match::match>(
328 pldm::utils::DBusHandler::getBus(),
329 propertiesChanged(objectPath, interface),
330 [this, effecterInfoIndex, dbusInfoIndex,
331 effecterId](sdbusplus::message::message& msg) {
332 DbusChgHostEffecterProps props;
333 std::string iface;
334 msg.read(iface, props);
335 processHostEffecterChangeNotification(
336 props, effecterInfoIndex, dbusInfoIndex, effecterId);
337 }));
338}
339
340} // namespace host_effecters
341} // namespace pldm