blob: c59b62ac506a89ee16b535d35682f332f6b8fe7b [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 {
Sampa Misraa4a96162020-07-14 05:33:46 -0500138 constexpr auto localOrRemote = false;
Tom Joseph250c4752020-04-15 10:32:45 +0530139 effecterId = findStateEffecterId(
140 pdrRepo, hostEffecterInfo[effecterInfoIndex].entityType,
141 hostEffecterInfo[effecterInfoIndex].entityInstance,
142 hostEffecterInfo[effecterInfoIndex].containerId,
143 hostEffecterInfo[effecterInfoIndex]
144 .dbusInfo[dbusInfoIndex]
Sampa Misraa4a96162020-07-14 05:33:46 -0500145 .state.stateSetId,
146 localOrRemote);
Tom Joseph250c4752020-04-15 10:32:45 +0530147 if (effecterId == PLDM_INVALID_EFFECTER_ID)
148 {
149 std::cerr << "Effecter id not found in pdr repo \n";
150 return;
151 }
152 }
153 constexpr auto hostStateInterface =
154 "xyz.openbmc_project.State.OperatingSystem.Status";
155 constexpr auto hostStatePath = "/xyz/openbmc_project/state/host0";
156
157 try
158 {
159 auto propVal = dbusHandler->getDbusPropertyVariant(
160 hostStatePath, "OperatingSystemState", hostStateInterface);
161 const auto& currHostState = std::get<std::string>(propVal);
162 if ((currHostState != "xyz.openbmc_project.State.OperatingSystem."
163 "Status.OSStatus.Standby") &&
164 (currHostState != "xyz.openbmc_project.State.OperatingSystem."
165 "Status.OSStatus.BootComplete"))
166 {
167 std::cout << "Host is not up. Current host state: "
168 << currHostState.c_str() << "\n";
169 return;
170 }
171 }
172 catch (const sdbusplus::exception::SdBusError& e)
173 {
174 std::cerr << "Error in getting current host state. Will still "
175 "continue to set the host effecter \n";
176 }
177 uint8_t newState{};
178 try
179 {
180 newState =
181 findNewStateValue(effecterInfoIndex, dbusInfoIndex, it->second);
182 }
183 catch (const std::out_of_range& e)
184 {
185 std::cerr << "new state not found in json"
186 << "\n";
187 return;
188 }
189
190 std::vector<set_effecter_state_field> stateField;
191 for (uint8_t i = 0; i < hostEffecterInfo[effecterInfoIndex].compEffecterCnt;
192 i++)
193 {
194 if (i == dbusInfoIndex)
195 {
196 stateField.push_back({PLDM_REQUEST_SET, newState});
197 }
198 else
199 {
200 stateField.push_back({PLDM_NO_CHANGE, 0});
201 }
202 }
203 int rc{};
204 try
205 {
206 rc = setHostStateEffecter(effecterInfoIndex, stateField, effecterId);
207 }
208 catch (const std::runtime_error& e)
209 {
210 std::cerr << "Could not set host state effecter \n";
211 return;
212 }
213 if (rc != PLDM_SUCCESS)
214 {
215 std::cerr << "Could not set the host state effecter, rc= " << rc
216 << " \n";
217 }
218}
219
220uint8_t
221 HostEffecterParser::findNewStateValue(size_t effecterInfoIndex,
222 size_t dbusInfoIndex,
223 const PropertyValue& propertyValue)
224{
225 const auto& propValues = hostEffecterInfo[effecterInfoIndex]
226 .dbusInfo[dbusInfoIndex]
227 .propertyValues;
228 auto it = std::find(propValues.begin(), propValues.end(), propertyValue);
229 uint8_t newState{};
230 if (it != propValues.end())
231 {
232 auto index = std::distance(propValues.begin(), it);
233 newState = hostEffecterInfo[effecterInfoIndex]
234 .dbusInfo[dbusInfoIndex]
235 .state.states[index];
236 }
237 else
238 {
239 throw std::out_of_range("new state not found in json");
240 }
241 return newState;
242}
243
244int HostEffecterParser::setHostStateEffecter(
245 size_t effecterInfoIndex, std::vector<set_effecter_state_field>& stateField,
246 uint16_t effecterId)
247{
248 uint8_t& mctpEid = hostEffecterInfo[effecterInfoIndex].mctpEid;
249 uint8_t& compEffCnt = hostEffecterInfo[effecterInfoIndex].compEffecterCnt;
250 auto instanceId = requester->getInstanceId(mctpEid);
251
252 std::vector<uint8_t> requestMsg(
253 sizeof(pldm_msg_hdr) + sizeof(effecterId) + sizeof(compEffCnt) +
254 sizeof(set_effecter_state_field) * compEffCnt,
255 0);
256 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
257 auto rc = encode_set_state_effecter_states_req(
258 instanceId, effecterId, compEffCnt, stateField.data(), request);
259
260 if (rc != PLDM_SUCCESS)
261 {
262 std::cerr << "Message encode failure. PLDM error code = " << std::hex
263 << std::showbase << rc << "\n";
264 requester->markFree(mctpEid, instanceId);
265 return rc;
266 }
267
268 if (verbose)
269 {
270 if (requestMsg.size())
271 {
272 std::ostringstream tempStream;
273 for (int byte : requestMsg)
274 {
275 tempStream << std::setfill('0') << std::setw(2) << std::hex
276 << byte << " ";
277 }
278 std::cout << tempStream.str() << std::endl;
279 }
280 }
281
282 uint8_t* responseMsg = nullptr;
283 size_t responseMsgSize{};
284
285 rc = pldm_send_recv(mctpEid, sockFd, requestMsg.data(), requestMsg.size(),
286 &responseMsg, &responseMsgSize);
287 std::unique_ptr<uint8_t, decltype(std::free)*> responseMsgPtr{responseMsg,
288 std::free};
289 requester->markFree(mctpEid, instanceId);
290
291 if (rc != PLDM_REQUESTER_SUCCESS)
292 {
293 std::cerr << "Failed to send message/receive response. RC = " << rc
294 << ", errno = " << errno << " for setting host effecter "
295 << effecterId << "\n";
296 pldm::utils::reportError(
297 "xyz.openbmc_project.bmc.pldm.InternalFailure");
298 return rc;
299 }
300 auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsgPtr.get());
301 uint8_t completionCode{};
302 rc = decode_set_state_effecter_states_resp(
303 responsePtr, responseMsgSize - sizeof(pldm_msg_hdr), &completionCode);
304 if (rc != PLDM_SUCCESS)
305 {
306 std::cerr << "Failed to decode setStateEffecterStates response, rc = "
307 << rc << "\n";
308 return rc;
309 }
310 if (completionCode != PLDM_SUCCESS)
311 {
312 std::cerr << "Failed setStateEffecterStates for effecter " << effecterId
313 << ". Response from Host " << (uint32_t)completionCode
314 << "\n";
315 pldm::utils::reportError(
316 "xyz.openbmc_project.bmc.pldm.InternalFailure");
317 }
318 return rc;
319}
320
321void HostEffecterParser::createHostEffecterMatch(const std::string& objectPath,
322 const std::string& interface,
323 size_t effecterInfoIndex,
324 size_t dbusInfoIndex,
325 uint16_t effecterId)
326{
327 using namespace sdbusplus::bus::match::rules;
328 effecterInfoMatch.emplace_back(
329 std::make_unique<sdbusplus::bus::match::match>(
330 pldm::utils::DBusHandler::getBus(),
331 propertiesChanged(objectPath, interface),
332 [this, effecterInfoIndex, dbusInfoIndex,
333 effecterId](sdbusplus::message::message& msg) {
334 DbusChgHostEffecterProps props;
335 std::string iface;
336 msg.read(iface, props);
337 processHostEffecterChangeNotification(
338 props, effecterInfoIndex, dbusInfoIndex, effecterId);
339 }));
340}
341
342} // namespace host_effecters
343} // namespace pldm