1a34a64bbSThu Nguyen #include "dbus_to_terminus_effecters.hpp"
2a34a64bbSThu Nguyen
3a34a64bbSThu Nguyen #include <libpldm/pdr.h>
4a34a64bbSThu Nguyen #include <libpldm/platform.h>
5a34a64bbSThu Nguyen
6a34a64bbSThu Nguyen #include <phosphor-logging/lg2.hpp>
7a34a64bbSThu Nguyen #include <xyz/openbmc_project/Common/error.hpp>
8a34a64bbSThu Nguyen #include <xyz/openbmc_project/State/Boot/Progress/client.hpp>
9a34a64bbSThu Nguyen #include <xyz/openbmc_project/State/OperatingSystem/Status/server.hpp>
10a34a64bbSThu Nguyen
11a34a64bbSThu Nguyen #include <fstream>
12a34a64bbSThu Nguyen
13a34a64bbSThu Nguyen PHOSPHOR_LOG2_USING;
14a34a64bbSThu Nguyen
15a34a64bbSThu Nguyen using namespace pldm::utils;
16a34a64bbSThu Nguyen
17a34a64bbSThu Nguyen namespace pldm
18a34a64bbSThu Nguyen {
19a34a64bbSThu Nguyen namespace host_effecters
20a34a64bbSThu Nguyen {
21a34a64bbSThu Nguyen using InternalFailure =
22a34a64bbSThu Nguyen sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
23a34a64bbSThu Nguyen
24a34a64bbSThu Nguyen constexpr auto hostEffecterJson = "dbus_to_terminus_effecter.json";
25a34a64bbSThu Nguyen
populatePropVals(const Json & dBusValues,std::vector<PropertyValue> & propertyValues,const std::string & propertyType)26a34a64bbSThu Nguyen void HostEffecterParser::populatePropVals(
27a34a64bbSThu Nguyen const Json& dBusValues, std::vector<PropertyValue>& propertyValues,
28a34a64bbSThu Nguyen const std::string& propertyType)
29a34a64bbSThu Nguyen
30a34a64bbSThu Nguyen {
31a34a64bbSThu Nguyen for (const auto& elem : dBusValues)
32a34a64bbSThu Nguyen {
33a34a64bbSThu Nguyen auto value = jsonEntryToDbusVal(propertyType, elem);
34a34a64bbSThu Nguyen propertyValues.emplace_back(value);
35a34a64bbSThu Nguyen }
36a34a64bbSThu Nguyen }
37a34a64bbSThu Nguyen
parseEffecterJson(const std::string & jsonPath)38a34a64bbSThu Nguyen void HostEffecterParser::parseEffecterJson(const std::string& jsonPath)
39a34a64bbSThu Nguyen {
40a34a64bbSThu Nguyen fs::path jsonDir(jsonPath);
41a34a64bbSThu Nguyen if (!fs::exists(jsonDir) || fs::is_empty(jsonDir))
42a34a64bbSThu Nguyen {
43a34a64bbSThu Nguyen error("Effecter json file for remote terminus '{PATH}' does not exist.",
44a34a64bbSThu Nguyen "PATH", jsonPath);
45a34a64bbSThu Nguyen return;
46a34a64bbSThu Nguyen }
47a34a64bbSThu Nguyen
48a34a64bbSThu Nguyen fs::path jsonFilePath = jsonDir / hostEffecterJson;
49a34a64bbSThu Nguyen if (!fs::exists(jsonFilePath))
50a34a64bbSThu Nguyen {
51a34a64bbSThu Nguyen error("Json at path '{PATH}' does not exist.", "PATH", jsonFilePath);
52a34a64bbSThu Nguyen throw InternalFailure();
53a34a64bbSThu Nguyen }
54a34a64bbSThu Nguyen
55a34a64bbSThu Nguyen std::ifstream jsonFile(jsonFilePath);
56a34a64bbSThu Nguyen auto data = Json::parse(jsonFile, nullptr, false);
57a34a64bbSThu Nguyen if (data.is_discarded())
58a34a64bbSThu Nguyen {
59a34a64bbSThu Nguyen error("Failed to parse json file {PATH}", "PATH", jsonFilePath);
60a34a64bbSThu Nguyen throw InternalFailure();
61a34a64bbSThu Nguyen }
62a34a64bbSThu Nguyen const Json empty{};
63a34a64bbSThu Nguyen const std::vector<Json> emptyList{};
64a34a64bbSThu Nguyen
65a34a64bbSThu Nguyen auto entries = data.value("entries", emptyList);
66a34a64bbSThu Nguyen for (const auto& entry : entries)
67a34a64bbSThu Nguyen {
68a34a64bbSThu Nguyen EffecterInfo effecterInfo;
69a34a64bbSThu Nguyen effecterInfo.mctpEid = entry.value("mctp_eid", 0xFF);
70a34a64bbSThu Nguyen auto jsonEffecterInfo = entry.value("effecter_info", empty);
71a34a64bbSThu Nguyen auto effecterId =
72a34a64bbSThu Nguyen jsonEffecterInfo.value("effecterID", PLDM_INVALID_EFFECTER_ID);
73a34a64bbSThu Nguyen /* default as PLDM_STATE_EFFECTER_PDR */
74a34a64bbSThu Nguyen auto effecterPdrType =
75a34a64bbSThu Nguyen jsonEffecterInfo.value("effecterPdrType", PLDM_STATE_EFFECTER_PDR);
76a34a64bbSThu Nguyen if (effecterPdrType != PLDM_STATE_EFFECTER_PDR &&
77a34a64bbSThu Nguyen effecterPdrType != PLDM_NUMERIC_EFFECTER_PDR)
78a34a64bbSThu Nguyen {
79a34a64bbSThu Nguyen error(
80a34a64bbSThu Nguyen "Effecter PDRType not supported {TYPE} of effecterID '{EFFECTERID}'",
81a34a64bbSThu Nguyen "TYPE", effecterPdrType, "EFFECTERID", effecterId);
82a34a64bbSThu Nguyen continue;
83a34a64bbSThu Nguyen }
84a34a64bbSThu Nguyen effecterInfo.effecterPdrType = effecterPdrType;
85a34a64bbSThu Nguyen effecterInfo.containerId = jsonEffecterInfo.value("containerID", 0);
86a34a64bbSThu Nguyen effecterInfo.entityType = jsonEffecterInfo.value("entityType", 0);
87a34a64bbSThu Nguyen effecterInfo.entityInstance =
88a34a64bbSThu Nguyen jsonEffecterInfo.value("entityInstance", 0);
89a34a64bbSThu Nguyen effecterInfo.compEffecterCnt =
90a34a64bbSThu Nguyen jsonEffecterInfo.value("compositeEffecterCount", 0);
91a34a64bbSThu Nguyen effecterInfo.checkHostState =
92a34a64bbSThu Nguyen jsonEffecterInfo.value("checkHostState", true);
93a34a64bbSThu Nguyen auto effecters = entry.value("effecters", emptyList);
94a34a64bbSThu Nguyen
95a34a64bbSThu Nguyen if (effecterPdrType == PLDM_NUMERIC_EFFECTER_PDR)
96a34a64bbSThu Nguyen {
97a34a64bbSThu Nguyen for (const auto& effecter : effecters)
98a34a64bbSThu Nguyen {
99a34a64bbSThu Nguyen DBusNumericEffecterMapping dbusInfo{};
100a34a64bbSThu Nguyen auto jsonDbusInfo = effecter.value("dbus_info", empty);
101a34a64bbSThu Nguyen dbusInfo.dataSize = effecter.value("effecterDataSize", 0);
102a34a64bbSThu Nguyen dbusInfo.unitModifier = effecter.value("unitModifier", 0);
103a34a64bbSThu Nguyen dbusInfo.resolution = effecter.value("resolution", 1);
104a34a64bbSThu Nguyen dbusInfo.offset = effecter.value("offset", 0);
105a34a64bbSThu Nguyen dbusInfo.dbusMap.objectPath =
106a34a64bbSThu Nguyen jsonDbusInfo.value("object_path", "");
107a34a64bbSThu Nguyen dbusInfo.dbusMap.interface =
108a34a64bbSThu Nguyen jsonDbusInfo.value("interface", "");
109a34a64bbSThu Nguyen dbusInfo.dbusMap.propertyName =
110a34a64bbSThu Nguyen jsonDbusInfo.value("property_name", "");
111a34a64bbSThu Nguyen dbusInfo.dbusMap.propertyType =
112a34a64bbSThu Nguyen jsonDbusInfo.value("property_type", "");
113a34a64bbSThu Nguyen /**
114a34a64bbSThu Nguyen * Only support these property type for Numeric Effecter D-Bus
115a34a64bbSThu Nguyen * property:
116a34a64bbSThu Nguyen * "uint8_t", "int16_t", "uint16_t", "int32_t", "uint32_t",
117a34a64bbSThu Nguyen * "int64_t", "uint64_t", "double"
118a34a64bbSThu Nguyen */
119a34a64bbSThu Nguyen if (!dbusValueNumericTypeNames.contains(
120a34a64bbSThu Nguyen dbusInfo.dbusMap.propertyType))
121a34a64bbSThu Nguyen {
122a34a64bbSThu Nguyen lg2::error(
123a34a64bbSThu Nguyen "Invalid PropertyType {TYPE} of object path {PATH} property name {NAME}",
124a34a64bbSThu Nguyen "TYPE", dbusInfo.dbusMap.propertyType, "PATH",
125a34a64bbSThu Nguyen dbusInfo.dbusMap.objectPath, "NAME",
126a34a64bbSThu Nguyen dbusInfo.dbusMap.propertyName);
127a34a64bbSThu Nguyen continue;
128a34a64bbSThu Nguyen }
129a34a64bbSThu Nguyen
130a34a64bbSThu Nguyen dbusInfo.propertyValue =
131a34a64bbSThu Nguyen std::numeric_limits<double>::quiet_NaN();
132a34a64bbSThu Nguyen auto effecterInfoIndex = hostEffecterInfo.size();
133a34a64bbSThu Nguyen auto dbusInfoIndex = effecterInfo.dbusInfo.size();
134a34a64bbSThu Nguyen createHostEffecterMatch(
135a34a64bbSThu Nguyen dbusInfo.dbusMap.objectPath, dbusInfo.dbusMap.interface,
136a34a64bbSThu Nguyen effecterInfoIndex, dbusInfoIndex, effecterId);
137a34a64bbSThu Nguyen effecterInfo.dbusNumericEffecterInfo.emplace_back(
138a34a64bbSThu Nguyen std::move(dbusInfo));
139a34a64bbSThu Nguyen }
140a34a64bbSThu Nguyen hostEffecterInfo.emplace_back(std::move(effecterInfo));
141a34a64bbSThu Nguyen continue;
142a34a64bbSThu Nguyen }
143a34a64bbSThu Nguyen
144a34a64bbSThu Nguyen for (const auto& effecter : effecters)
145a34a64bbSThu Nguyen {
146a34a64bbSThu Nguyen DBusEffecterMapping dbusInfo{};
147a34a64bbSThu Nguyen auto jsonDbusInfo = effecter.value("dbus_info", empty);
148a34a64bbSThu Nguyen dbusInfo.dbusMap.objectPath = jsonDbusInfo.value("object_path", "");
149a34a64bbSThu Nguyen dbusInfo.dbusMap.interface = jsonDbusInfo.value("interface", "");
150a34a64bbSThu Nguyen dbusInfo.dbusMap.propertyName =
151a34a64bbSThu Nguyen jsonDbusInfo.value("property_name", "");
152a34a64bbSThu Nguyen dbusInfo.dbusMap.propertyType =
153a34a64bbSThu Nguyen jsonDbusInfo.value("property_type", "");
154a34a64bbSThu Nguyen Json propertyValues = jsonDbusInfo["property_values"];
155a34a64bbSThu Nguyen
156a34a64bbSThu Nguyen populatePropVals(propertyValues, dbusInfo.propertyValues,
157a34a64bbSThu Nguyen dbusInfo.dbusMap.propertyType);
158a34a64bbSThu Nguyen
159a34a64bbSThu Nguyen const std::vector<uint8_t> emptyStates{};
160a34a64bbSThu Nguyen auto state = effecter.value("state", empty);
161a34a64bbSThu Nguyen dbusInfo.state.stateSetId = state.value("id", 0);
162a34a64bbSThu Nguyen auto states = state.value("state_values", emptyStates);
163a34a64bbSThu Nguyen if (dbusInfo.propertyValues.size() != states.size())
164a34a64bbSThu Nguyen {
165a34a64bbSThu Nguyen error(
166a34a64bbSThu Nguyen "Number of states do not match with number of D-Bus property values in the json. Object path at '{PATH}' and property '{PROPERTY}' will not be monitored",
167a34a64bbSThu Nguyen "PATH", dbusInfo.dbusMap.objectPath, "PROPERTY",
168a34a64bbSThu Nguyen dbusInfo.dbusMap.propertyName);
169a34a64bbSThu Nguyen continue;
170a34a64bbSThu Nguyen }
171a34a64bbSThu Nguyen for (const auto& s : states)
172a34a64bbSThu Nguyen {
173a34a64bbSThu Nguyen dbusInfo.state.states.emplace_back(s);
174a34a64bbSThu Nguyen }
175a34a64bbSThu Nguyen
176a34a64bbSThu Nguyen auto effecterInfoIndex = hostEffecterInfo.size();
177a34a64bbSThu Nguyen auto dbusInfoIndex = effecterInfo.dbusInfo.size();
178a34a64bbSThu Nguyen createHostEffecterMatch(
179a34a64bbSThu Nguyen dbusInfo.dbusMap.objectPath, dbusInfo.dbusMap.interface,
180a34a64bbSThu Nguyen effecterInfoIndex, dbusInfoIndex, effecterId);
181a34a64bbSThu Nguyen effecterInfo.dbusInfo.emplace_back(std::move(dbusInfo));
182a34a64bbSThu Nguyen }
183a34a64bbSThu Nguyen hostEffecterInfo.emplace_back(std::move(effecterInfo));
184a34a64bbSThu Nguyen }
185a34a64bbSThu Nguyen }
186a34a64bbSThu Nguyen
isHostOn(void)187a34a64bbSThu Nguyen bool HostEffecterParser::isHostOn(void)
188a34a64bbSThu Nguyen {
189a34a64bbSThu Nguyen using BootProgress =
190a34a64bbSThu Nguyen sdbusplus::client::xyz::openbmc_project::state::boot::Progress<>;
191a34a64bbSThu Nguyen constexpr auto hostStatePath = "/xyz/openbmc_project/state/host0";
192a34a64bbSThu Nguyen try
193a34a64bbSThu Nguyen {
194a34a64bbSThu Nguyen auto propVal = dbusHandler->getDbusPropertyVariant(
195a34a64bbSThu Nguyen hostStatePath, "BootProgress", BootProgress::interface);
196a34a64bbSThu Nguyen
197a34a64bbSThu Nguyen using Stages = BootProgress::ProgressStages;
198a34a64bbSThu Nguyen auto currHostState = sdbusplus::message::convert_from_string<Stages>(
199a34a64bbSThu Nguyen std::get<std::string>(propVal))
200a34a64bbSThu Nguyen .value();
201a34a64bbSThu Nguyen
202a34a64bbSThu Nguyen if (currHostState != Stages::SystemInitComplete &&
203a34a64bbSThu Nguyen currHostState != Stages::OSRunning &&
204a34a64bbSThu Nguyen currHostState != Stages::SystemSetup &&
205a34a64bbSThu Nguyen currHostState != Stages::OEM)
206a34a64bbSThu Nguyen {
207a34a64bbSThu Nguyen info(
208a34a64bbSThu Nguyen "Remote terminus is not up/active, current remote terminus state is: '{CURRENT_HOST_STATE}'",
209a34a64bbSThu Nguyen "CURRENT_HOST_STATE", currHostState);
210a34a64bbSThu Nguyen return false;
211a34a64bbSThu Nguyen }
212a34a64bbSThu Nguyen }
213a34a64bbSThu Nguyen catch (const sdbusplus::exception_t& e)
214a34a64bbSThu Nguyen {
215a34a64bbSThu Nguyen error(
216a34a64bbSThu Nguyen "Error in getting current remote terminus state. Will still continue to set the remote terminus effecter, error - {ERROR}",
217a34a64bbSThu Nguyen "ERROR", e);
218a34a64bbSThu Nguyen return false;
219a34a64bbSThu Nguyen }
220a34a64bbSThu Nguyen
221a34a64bbSThu Nguyen return true;
222a34a64bbSThu Nguyen }
223a34a64bbSThu Nguyen
processHostEffecterChangeNotification(const DbusChgHostEffecterProps & chProperties,size_t effecterInfoIndex,size_t dbusInfoIndex,uint16_t effecterId)224a34a64bbSThu Nguyen void HostEffecterParser::processHostEffecterChangeNotification(
225a34a64bbSThu Nguyen const DbusChgHostEffecterProps& chProperties, size_t effecterInfoIndex,
226a34a64bbSThu Nguyen size_t dbusInfoIndex, uint16_t effecterId)
227a34a64bbSThu Nguyen {
228a34a64bbSThu Nguyen const auto& pdrType = hostEffecterInfo[effecterInfoIndex].effecterPdrType;
229a34a64bbSThu Nguyen if (pdrType == PLDM_NUMERIC_EFFECTER_PDR)
230a34a64bbSThu Nguyen {
231a34a64bbSThu Nguyen processTerminusNumericEffecterChangeNotification(
232a34a64bbSThu Nguyen chProperties, effecterInfoIndex, dbusInfoIndex, effecterId);
233a34a64bbSThu Nguyen return;
234a34a64bbSThu Nguyen }
235a34a64bbSThu Nguyen const auto& propertyName = hostEffecterInfo[effecterInfoIndex]
236a34a64bbSThu Nguyen .dbusInfo[dbusInfoIndex]
237a34a64bbSThu Nguyen .dbusMap.propertyName;
238a34a64bbSThu Nguyen
239a34a64bbSThu Nguyen const auto& it = chProperties.find(propertyName);
240a34a64bbSThu Nguyen
241a34a64bbSThu Nguyen if (it == chProperties.end())
242a34a64bbSThu Nguyen {
243a34a64bbSThu Nguyen return;
244a34a64bbSThu Nguyen }
245a34a64bbSThu Nguyen
246a34a64bbSThu Nguyen if (effecterId == PLDM_INVALID_EFFECTER_ID)
247a34a64bbSThu Nguyen {
248a34a64bbSThu Nguyen constexpr auto localOrRemote = false;
249a34a64bbSThu Nguyen effecterId = findStateEffecterId(
250a34a64bbSThu Nguyen pdrRepo, hostEffecterInfo[effecterInfoIndex].entityType,
251a34a64bbSThu Nguyen hostEffecterInfo[effecterInfoIndex].entityInstance,
252a34a64bbSThu Nguyen hostEffecterInfo[effecterInfoIndex].containerId,
253a34a64bbSThu Nguyen hostEffecterInfo[effecterInfoIndex]
254a34a64bbSThu Nguyen .dbusInfo[dbusInfoIndex]
255a34a64bbSThu Nguyen .state.stateSetId,
256a34a64bbSThu Nguyen localOrRemote);
257a34a64bbSThu Nguyen if (effecterId == PLDM_INVALID_EFFECTER_ID)
258a34a64bbSThu Nguyen {
259a34a64bbSThu Nguyen error(
260a34a64bbSThu Nguyen "Effecter ID '{EFFECTERID}' of entity type '{TYPE}', entityInstance '{INSTANCE}' and containerID '{CONTAINER_ID}' not found in pdr repo",
261a34a64bbSThu Nguyen "EFFECTERID", effecterId, "TYPE",
262a34a64bbSThu Nguyen hostEffecterInfo[effecterInfoIndex].entityType, "INSTANCE",
263a34a64bbSThu Nguyen hostEffecterInfo[effecterInfoIndex].entityInstance,
264a34a64bbSThu Nguyen "CONTAINER_ID",
265a34a64bbSThu Nguyen hostEffecterInfo[effecterInfoIndex].containerId);
266a34a64bbSThu Nguyen return;
267a34a64bbSThu Nguyen }
268a34a64bbSThu Nguyen }
269a34a64bbSThu Nguyen
270a34a64bbSThu Nguyen if (!isHostOn())
271a34a64bbSThu Nguyen {
272a34a64bbSThu Nguyen return;
273a34a64bbSThu Nguyen }
274a34a64bbSThu Nguyen
275a34a64bbSThu Nguyen uint8_t newState{};
276a34a64bbSThu Nguyen try
277a34a64bbSThu Nguyen {
278a34a64bbSThu Nguyen newState =
279a34a64bbSThu Nguyen findNewStateValue(effecterInfoIndex, dbusInfoIndex, it->second);
280a34a64bbSThu Nguyen }
281a34a64bbSThu Nguyen catch (const std::out_of_range& e)
282a34a64bbSThu Nguyen {
283a34a64bbSThu Nguyen error("Failed to find new state '{NEW_STATE}' in json, error - {ERROR}",
284a34a64bbSThu Nguyen "ERROR", e, "NEW_STATE", newState);
285a34a64bbSThu Nguyen return;
286a34a64bbSThu Nguyen }
287a34a64bbSThu Nguyen
288a34a64bbSThu Nguyen std::vector<set_effecter_state_field> stateField;
289a34a64bbSThu Nguyen for (uint8_t i = 0; i < hostEffecterInfo[effecterInfoIndex].compEffecterCnt;
290a34a64bbSThu Nguyen i++)
291a34a64bbSThu Nguyen {
292a34a64bbSThu Nguyen if (i == dbusInfoIndex)
293a34a64bbSThu Nguyen {
294a34a64bbSThu Nguyen stateField.push_back({PLDM_REQUEST_SET, newState});
295a34a64bbSThu Nguyen }
296a34a64bbSThu Nguyen else
297a34a64bbSThu Nguyen {
298a34a64bbSThu Nguyen stateField.push_back({PLDM_NO_CHANGE, 0});
299a34a64bbSThu Nguyen }
300a34a64bbSThu Nguyen }
301a34a64bbSThu Nguyen int rc{};
302a34a64bbSThu Nguyen try
303a34a64bbSThu Nguyen {
304a34a64bbSThu Nguyen rc = setHostStateEffecter(effecterInfoIndex, stateField, effecterId);
305a34a64bbSThu Nguyen }
306a34a64bbSThu Nguyen catch (const std::runtime_error& e)
307a34a64bbSThu Nguyen {
308a34a64bbSThu Nguyen error(
309a34a64bbSThu Nguyen "Failed to set remote terminus state effecter for effecter ID '{EFFECTERID}', error - {ERROR}",
310a34a64bbSThu Nguyen "ERROR", e, "EFFECTERID", effecterId);
311a34a64bbSThu Nguyen return;
312a34a64bbSThu Nguyen }
313a34a64bbSThu Nguyen if (rc != PLDM_SUCCESS)
314a34a64bbSThu Nguyen {
315a34a64bbSThu Nguyen error(
316a34a64bbSThu Nguyen "Failed to set the remote terminus state effecter for effecter ID '{EFFECTERID}', response code '{RC}'",
317a34a64bbSThu Nguyen "EFFECTERID", effecterId, "RC", rc);
318a34a64bbSThu Nguyen }
319a34a64bbSThu Nguyen }
320a34a64bbSThu Nguyen
adjustValue(double value,double offset,double resolution,int8_t modify)321a34a64bbSThu Nguyen double HostEffecterParser::adjustValue(double value, double offset,
322a34a64bbSThu Nguyen double resolution, int8_t modify)
323a34a64bbSThu Nguyen {
324a34a64bbSThu Nguyen double unitModifier = std::pow(10, signed(modify));
325a34a64bbSThu Nguyen return std::round((value - offset) * resolution / unitModifier);
326a34a64bbSThu Nguyen }
327a34a64bbSThu Nguyen
processTerminusNumericEffecterChangeNotification(const DbusChgHostEffecterProps & chProperties,size_t effecterInfoIndex,size_t dbusInfoIndex,uint16_t effecterId)328a34a64bbSThu Nguyen void HostEffecterParser::processTerminusNumericEffecterChangeNotification(
329a34a64bbSThu Nguyen const DbusChgHostEffecterProps& chProperties, size_t effecterInfoIndex,
330a34a64bbSThu Nguyen size_t dbusInfoIndex, uint16_t effecterId)
331a34a64bbSThu Nguyen {
332a34a64bbSThu Nguyen const auto& checkHost = hostEffecterInfo[effecterInfoIndex].checkHostState;
333a34a64bbSThu Nguyen const auto& propValues = hostEffecterInfo[effecterInfoIndex]
334a34a64bbSThu Nguyen .dbusNumericEffecterInfo[dbusInfoIndex];
335a34a64bbSThu Nguyen const auto& propertyName = propValues.dbusMap.propertyName;
336a34a64bbSThu Nguyen const auto& propertyType = propValues.dbusMap.propertyType;
337a34a64bbSThu Nguyen
338a34a64bbSThu Nguyen if (effecterId == PLDM_INVALID_EFFECTER_ID)
339a34a64bbSThu Nguyen {
340a34a64bbSThu Nguyen lg2::error(
341a34a64bbSThu Nguyen "Dbus to PLDM Numeric Effecter setting requires valid effecter ID. Invalid effecter ID {EFFECTER_ID}",
342a34a64bbSThu Nguyen "EFFECTER_ID", effecterId);
343a34a64bbSThu Nguyen }
344a34a64bbSThu Nguyen
345a34a64bbSThu Nguyen if (!dbusValueNumericTypeNames.contains(propertyType))
346a34a64bbSThu Nguyen {
347a34a64bbSThu Nguyen lg2::error(
348a34a64bbSThu Nguyen "DBus Value to PLDM Numeric Effecter setting only supports D-Bus Numeric data type. Invalid type {TYPE}",
349a34a64bbSThu Nguyen "TYPE", propertyType);
350a34a64bbSThu Nguyen return;
351a34a64bbSThu Nguyen }
352a34a64bbSThu Nguyen
353a34a64bbSThu Nguyen const auto& it = chProperties.find(propertyName);
354a34a64bbSThu Nguyen
355a34a64bbSThu Nguyen if (it == chProperties.end())
356a34a64bbSThu Nguyen {
357a34a64bbSThu Nguyen return;
358a34a64bbSThu Nguyen }
359a34a64bbSThu Nguyen
360a34a64bbSThu Nguyen double val = std::numeric_limits<double>::quiet_NaN();
361a34a64bbSThu Nguyen if (!pldm::utils::dbusPropValuesToDouble(propertyType, it->second, &val))
362a34a64bbSThu Nguyen {
363a34a64bbSThu Nguyen lg2::error(
364a34a64bbSThu Nguyen "DBus Value to PLDM Numeric Effecter setting only supports Numeric D-Bus data type. Invalid type {TYPE}",
365a34a64bbSThu Nguyen "TYPE", propertyType);
366a34a64bbSThu Nguyen return;
367a34a64bbSThu Nguyen }
368a34a64bbSThu Nguyen
369a34a64bbSThu Nguyen /* Update the current value of D-Bus interface*/
370a34a64bbSThu Nguyen if (!std::isnan(val) && std::isnan(propValues.propertyValue))
371a34a64bbSThu Nguyen {
372a34a64bbSThu Nguyen hostEffecterInfo[effecterInfoIndex]
373a34a64bbSThu Nguyen .dbusNumericEffecterInfo[dbusInfoIndex]
374a34a64bbSThu Nguyen .propertyValue = val;
375a34a64bbSThu Nguyen return;
376a34a64bbSThu Nguyen }
377a34a64bbSThu Nguyen
378*04ac9971SManojkiran Eda /* Bypass the setting when the current value is NA or setting value is NA */
379a34a64bbSThu Nguyen if (std::isnan(propValues.propertyValue) || std::isnan(val))
380a34a64bbSThu Nguyen {
381a34a64bbSThu Nguyen return;
382a34a64bbSThu Nguyen }
383a34a64bbSThu Nguyen
384a34a64bbSThu Nguyen /* Setting value equals the D-Bus value which is real value of effecter */
385a34a64bbSThu Nguyen if (val == propValues.propertyValue)
386a34a64bbSThu Nguyen {
387a34a64bbSThu Nguyen return;
388a34a64bbSThu Nguyen }
389a34a64bbSThu Nguyen
390a34a64bbSThu Nguyen double rawValue = adjustValue(val, propValues.offset, propValues.resolution,
391a34a64bbSThu Nguyen propValues.unitModifier);
392a34a64bbSThu Nguyen
393a34a64bbSThu Nguyen if (checkHost && !isHostOn())
394a34a64bbSThu Nguyen {
395a34a64bbSThu Nguyen return;
396a34a64bbSThu Nguyen }
397a34a64bbSThu Nguyen
398a34a64bbSThu Nguyen try
399a34a64bbSThu Nguyen {
400a34a64bbSThu Nguyen auto rc = setTerminusNumericEffecter(effecterInfoIndex, effecterId,
401a34a64bbSThu Nguyen propValues.dataSize, rawValue);
402a34a64bbSThu Nguyen if (rc)
403a34a64bbSThu Nguyen {
404a34a64bbSThu Nguyen error(
405a34a64bbSThu Nguyen "Could not set the numeric effecter ID '{EFFECTERID}' return code '{RC}'",
406a34a64bbSThu Nguyen "EFFECTERID", effecterId, "RC", rc);
407a34a64bbSThu Nguyen return;
408a34a64bbSThu Nguyen }
409a34a64bbSThu Nguyen }
410a34a64bbSThu Nguyen catch (const std::runtime_error& e)
411a34a64bbSThu Nguyen {
412a34a64bbSThu Nguyen error("Could not set numeric effecter ID= '{EFFECTERID}'", "EFFECTERID",
413a34a64bbSThu Nguyen effecterId);
414a34a64bbSThu Nguyen return;
415a34a64bbSThu Nguyen }
416a34a64bbSThu Nguyen
417a34a64bbSThu Nguyen hostEffecterInfo[effecterInfoIndex]
418a34a64bbSThu Nguyen .dbusNumericEffecterInfo[dbusInfoIndex]
419a34a64bbSThu Nguyen .propertyValue = val;
420a34a64bbSThu Nguyen
421a34a64bbSThu Nguyen return;
422a34a64bbSThu Nguyen }
423a34a64bbSThu Nguyen
findNewStateValue(size_t effecterInfoIndex,size_t dbusInfoIndex,const PropertyValue & propertyValue)424a34a64bbSThu Nguyen uint8_t HostEffecterParser::findNewStateValue(
425a34a64bbSThu Nguyen size_t effecterInfoIndex, size_t dbusInfoIndex,
426a34a64bbSThu Nguyen const PropertyValue& propertyValue)
427a34a64bbSThu Nguyen {
428a34a64bbSThu Nguyen const auto& propValues = hostEffecterInfo[effecterInfoIndex]
429a34a64bbSThu Nguyen .dbusInfo[dbusInfoIndex]
430a34a64bbSThu Nguyen .propertyValues;
431a34a64bbSThu Nguyen auto it = std::find(propValues.begin(), propValues.end(), propertyValue);
432a34a64bbSThu Nguyen uint8_t newState{};
433a34a64bbSThu Nguyen if (it != propValues.end())
434a34a64bbSThu Nguyen {
435a34a64bbSThu Nguyen auto index = std::distance(propValues.begin(), it);
436a34a64bbSThu Nguyen newState = hostEffecterInfo[effecterInfoIndex]
437a34a64bbSThu Nguyen .dbusInfo[dbusInfoIndex]
438a34a64bbSThu Nguyen .state.states[index];
439a34a64bbSThu Nguyen }
440a34a64bbSThu Nguyen else
441a34a64bbSThu Nguyen {
442a34a64bbSThu Nguyen throw std::out_of_range("new state not found in json");
443a34a64bbSThu Nguyen }
444a34a64bbSThu Nguyen return newState;
445a34a64bbSThu Nguyen }
446a34a64bbSThu Nguyen
getEffecterDataSize(uint8_t effecterDataSize)447a34a64bbSThu Nguyen size_t getEffecterDataSize(uint8_t effecterDataSize)
448a34a64bbSThu Nguyen {
449a34a64bbSThu Nguyen switch (effecterDataSize)
450a34a64bbSThu Nguyen {
451a34a64bbSThu Nguyen case PLDM_EFFECTER_DATA_SIZE_UINT8:
452a34a64bbSThu Nguyen return sizeof(uint8_t);
453a34a64bbSThu Nguyen case PLDM_EFFECTER_DATA_SIZE_SINT8:
454a34a64bbSThu Nguyen return sizeof(int8_t);
455a34a64bbSThu Nguyen case PLDM_EFFECTER_DATA_SIZE_UINT16:
456a34a64bbSThu Nguyen return sizeof(uint16_t);
457a34a64bbSThu Nguyen case PLDM_EFFECTER_DATA_SIZE_SINT16:
458a34a64bbSThu Nguyen return sizeof(int16_t);
459a34a64bbSThu Nguyen case PLDM_EFFECTER_DATA_SIZE_UINT32:
460a34a64bbSThu Nguyen return sizeof(uint32_t);
461a34a64bbSThu Nguyen case PLDM_EFFECTER_DATA_SIZE_SINT32:
462a34a64bbSThu Nguyen return sizeof(int32_t);
463a34a64bbSThu Nguyen default:
464a34a64bbSThu Nguyen return 0;
465a34a64bbSThu Nguyen }
466a34a64bbSThu Nguyen }
467a34a64bbSThu Nguyen
setTerminusNumericEffecter(size_t effecterInfoIndex,uint16_t effecterId,uint8_t dataSize,double rawValue)468a34a64bbSThu Nguyen int HostEffecterParser::setTerminusNumericEffecter(
469a34a64bbSThu Nguyen size_t effecterInfoIndex, uint16_t effecterId, uint8_t dataSize,
470a34a64bbSThu Nguyen double rawValue)
471a34a64bbSThu Nguyen {
472a34a64bbSThu Nguyen uint8_t& mctpEid = hostEffecterInfo[effecterInfoIndex].mctpEid;
473a34a64bbSThu Nguyen auto instanceId = instanceIdDb->next(mctpEid);
474a34a64bbSThu Nguyen int rc = PLDM_ERROR;
475a34a64bbSThu Nguyen std::vector<uint8_t> requestMsg;
476a34a64bbSThu Nguyen
477a34a64bbSThu Nguyen /**
478a34a64bbSThu Nguyen * PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES = 4. It includes the 1 byte
479a34a64bbSThu Nguyen * value for effecterValue as `Table 48 - SetNumericEffecterValue command
480a34a64bbSThu Nguyen * format` DSP0248 V1.3.0 So the payload_length of `SetNumericEffecterValue`
481a34a64bbSThu Nguyen * request message will be payload_length =
482a34a64bbSThu Nguyen * PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES - 1 + sizeof(dataType)
483a34a64bbSThu Nguyen */
484a34a64bbSThu Nguyen size_t payload_length = PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES - 1 +
485a34a64bbSThu Nguyen getEffecterDataSize(dataSize);
486a34a64bbSThu Nguyen requestMsg.resize(sizeof(pldm_msg_hdr) + payload_length);
487a34a64bbSThu Nguyen auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
488a34a64bbSThu Nguyen switch (dataSize)
489a34a64bbSThu Nguyen {
490a34a64bbSThu Nguyen case PLDM_EFFECTER_DATA_SIZE_UINT8:
491a34a64bbSThu Nguyen {
492a34a64bbSThu Nguyen auto value_uint8 = (uint8_t)rawValue;
493a34a64bbSThu Nguyen rc = encode_set_numeric_effecter_value_req(
494a34a64bbSThu Nguyen instanceId, effecterId, dataSize,
495a34a64bbSThu Nguyen reinterpret_cast<uint8_t*>(&value_uint8), request,
496a34a64bbSThu Nguyen payload_length);
497a34a64bbSThu Nguyen break;
498a34a64bbSThu Nguyen }
499a34a64bbSThu Nguyen case PLDM_EFFECTER_DATA_SIZE_SINT8:
500a34a64bbSThu Nguyen {
501a34a64bbSThu Nguyen auto value_int8 = (int8_t)rawValue;
502a34a64bbSThu Nguyen rc = encode_set_numeric_effecter_value_req(
503a34a64bbSThu Nguyen instanceId, effecterId, dataSize,
504a34a64bbSThu Nguyen reinterpret_cast<uint8_t*>(&value_int8), request,
505a34a64bbSThu Nguyen payload_length);
506a34a64bbSThu Nguyen break;
507a34a64bbSThu Nguyen }
508a34a64bbSThu Nguyen case PLDM_EFFECTER_DATA_SIZE_UINT16:
509a34a64bbSThu Nguyen {
510a34a64bbSThu Nguyen auto value_uint16 = (uint16_t)rawValue;
511a34a64bbSThu Nguyen rc = encode_set_numeric_effecter_value_req(
512a34a64bbSThu Nguyen instanceId, effecterId, dataSize,
513a34a64bbSThu Nguyen reinterpret_cast<uint8_t*>(&value_uint16), request,
514a34a64bbSThu Nguyen payload_length);
515a34a64bbSThu Nguyen break;
516a34a64bbSThu Nguyen }
517a34a64bbSThu Nguyen case PLDM_EFFECTER_DATA_SIZE_SINT16:
518a34a64bbSThu Nguyen {
519a34a64bbSThu Nguyen auto value_int16 = (int16_t)rawValue;
520a34a64bbSThu Nguyen rc = encode_set_numeric_effecter_value_req(
521a34a64bbSThu Nguyen instanceId, effecterId, dataSize,
522a34a64bbSThu Nguyen reinterpret_cast<uint8_t*>(&value_int16), request,
523a34a64bbSThu Nguyen payload_length);
524a34a64bbSThu Nguyen break;
525a34a64bbSThu Nguyen }
526a34a64bbSThu Nguyen case PLDM_EFFECTER_DATA_SIZE_UINT32:
527a34a64bbSThu Nguyen {
528a34a64bbSThu Nguyen auto value_uint32 = (uint32_t)rawValue;
529a34a64bbSThu Nguyen rc = encode_set_numeric_effecter_value_req(
530a34a64bbSThu Nguyen instanceId, effecterId, dataSize,
531a34a64bbSThu Nguyen reinterpret_cast<uint8_t*>(&value_uint32), request,
532a34a64bbSThu Nguyen payload_length);
533a34a64bbSThu Nguyen break;
534a34a64bbSThu Nguyen }
535a34a64bbSThu Nguyen case PLDM_EFFECTER_DATA_SIZE_SINT32:
536a34a64bbSThu Nguyen {
537a34a64bbSThu Nguyen auto value_int32 = (int32_t)rawValue;
538a34a64bbSThu Nguyen rc = encode_set_numeric_effecter_value_req(
539a34a64bbSThu Nguyen instanceId, effecterId, dataSize,
540a34a64bbSThu Nguyen reinterpret_cast<uint8_t*>(&value_int32), request,
541a34a64bbSThu Nguyen payload_length);
542a34a64bbSThu Nguyen break;
543a34a64bbSThu Nguyen }
544a34a64bbSThu Nguyen default:
545a34a64bbSThu Nguyen break;
546a34a64bbSThu Nguyen }
547a34a64bbSThu Nguyen
548a34a64bbSThu Nguyen if (rc)
549a34a64bbSThu Nguyen {
550a34a64bbSThu Nguyen error(
551a34a64bbSThu Nguyen "Failed to encode set numeric effecter request message for effecter ID '{EFFECTERID}' and instanceID '{INSTANCE}' with error code '{RC}'",
552a34a64bbSThu Nguyen "EFFECTERID", effecterId, "INSTANCE", instanceId, "RC", lg2::hex,
553a34a64bbSThu Nguyen rc);
554a34a64bbSThu Nguyen
555a34a64bbSThu Nguyen instanceIdDb->free(mctpEid, instanceId);
556a34a64bbSThu Nguyen return rc;
557a34a64bbSThu Nguyen }
558a34a64bbSThu Nguyen
559a34a64bbSThu Nguyen auto setNumericEffecterRespHandler = [effecterId](mctp_eid_t /*eid*/,
560a34a64bbSThu Nguyen const pldm_msg* response,
561a34a64bbSThu Nguyen size_t respMsgLen) {
562a34a64bbSThu Nguyen if (response == nullptr || !respMsgLen)
563a34a64bbSThu Nguyen {
564a34a64bbSThu Nguyen error(
565a34a64bbSThu Nguyen "Failed to receive response for setNumericEffecterValue command");
566a34a64bbSThu Nguyen return;
567a34a64bbSThu Nguyen }
568a34a64bbSThu Nguyen uint8_t completionCode{};
569a34a64bbSThu Nguyen auto rc = decode_set_numeric_effecter_value_resp(response, respMsgLen,
570a34a64bbSThu Nguyen &completionCode);
571a34a64bbSThu Nguyen
572a34a64bbSThu Nguyen if (rc)
573a34a64bbSThu Nguyen {
574a34a64bbSThu Nguyen error(
575a34a64bbSThu Nguyen "Failed to decode set numeric effecter response message for effecter ID '{EFFECTERID}' with error code '{RC}'",
576a34a64bbSThu Nguyen "EFFECTERID", effecterId, "RC", lg2::hex, rc);
577a34a64bbSThu Nguyen }
578a34a64bbSThu Nguyen if (completionCode)
579a34a64bbSThu Nguyen {
580a34a64bbSThu Nguyen error(
581a34a64bbSThu Nguyen "Failed to set numeric effecter for effecter ID '{EFFECTERID}' with complete code '{CC}'",
582a34a64bbSThu Nguyen "EFFECTERID", effecterId, "CC", lg2::hex, completionCode);
583a34a64bbSThu Nguyen }
584a34a64bbSThu Nguyen };
585a34a64bbSThu Nguyen
586a34a64bbSThu Nguyen rc = handler->registerRequest(
587a34a64bbSThu Nguyen mctpEid, instanceId, PLDM_PLATFORM, PLDM_SET_NUMERIC_EFFECTER_VALUE,
588a34a64bbSThu Nguyen std::move(requestMsg), std::move(setNumericEffecterRespHandler));
589a34a64bbSThu Nguyen if (rc)
590a34a64bbSThu Nguyen {
591a34a64bbSThu Nguyen error("Failed to send request to set an effecter on Host");
592a34a64bbSThu Nguyen }
593a34a64bbSThu Nguyen return rc;
594a34a64bbSThu Nguyen }
595a34a64bbSThu Nguyen
setHostStateEffecter(size_t effecterInfoIndex,std::vector<set_effecter_state_field> & stateField,uint16_t effecterId)596a34a64bbSThu Nguyen int HostEffecterParser::setHostStateEffecter(
597a34a64bbSThu Nguyen size_t effecterInfoIndex, std::vector<set_effecter_state_field>& stateField,
598a34a64bbSThu Nguyen uint16_t effecterId)
599a34a64bbSThu Nguyen {
600a34a64bbSThu Nguyen uint8_t& mctpEid = hostEffecterInfo[effecterInfoIndex].mctpEid;
601a34a64bbSThu Nguyen uint8_t& compEffCnt = hostEffecterInfo[effecterInfoIndex].compEffecterCnt;
602a34a64bbSThu Nguyen auto instanceId = instanceIdDb->next(mctpEid);
603a34a64bbSThu Nguyen
604a34a64bbSThu Nguyen std::vector<uint8_t> requestMsg(
605a34a64bbSThu Nguyen sizeof(pldm_msg_hdr) + sizeof(effecterId) + sizeof(compEffCnt) +
606a34a64bbSThu Nguyen sizeof(set_effecter_state_field) * compEffCnt,
607a34a64bbSThu Nguyen 0);
608a34a64bbSThu Nguyen auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
609a34a64bbSThu Nguyen auto rc = encode_set_state_effecter_states_req(
610a34a64bbSThu Nguyen instanceId, effecterId, compEffCnt, stateField.data(), request);
611a34a64bbSThu Nguyen
612a34a64bbSThu Nguyen if (rc != PLDM_SUCCESS)
613a34a64bbSThu Nguyen {
614a34a64bbSThu Nguyen error(
615a34a64bbSThu Nguyen "Failed to encode set state effecter states message for effecter ID '{EFFECTERID}' and instanceID '{INSTANCE}' with response code '{RC}'",
616a34a64bbSThu Nguyen "EFFECTERID", effecterId, "INSTANCE", instanceId, "RC", lg2::hex,
617a34a64bbSThu Nguyen rc);
618a34a64bbSThu Nguyen instanceIdDb->free(mctpEid, instanceId);
619a34a64bbSThu Nguyen return rc;
620a34a64bbSThu Nguyen }
621a34a64bbSThu Nguyen
622a34a64bbSThu Nguyen auto setStateEffecterStatesRespHandler = [](mctp_eid_t /*eid*/,
623a34a64bbSThu Nguyen const pldm_msg* response,
624a34a64bbSThu Nguyen size_t respMsgLen) {
625a34a64bbSThu Nguyen if (response == nullptr || !respMsgLen)
626a34a64bbSThu Nguyen {
627a34a64bbSThu Nguyen error(
628a34a64bbSThu Nguyen "Failed to receive response for setting state effecter states.");
629a34a64bbSThu Nguyen return;
630a34a64bbSThu Nguyen }
631a34a64bbSThu Nguyen uint8_t completionCode{};
632a34a64bbSThu Nguyen auto rc = decode_set_state_effecter_states_resp(response, respMsgLen,
633a34a64bbSThu Nguyen &completionCode);
634a34a64bbSThu Nguyen if (rc)
635a34a64bbSThu Nguyen {
636a34a64bbSThu Nguyen error(
637a34a64bbSThu Nguyen "Failed to decode response of set state effecter states, response code '{RC}'",
638a34a64bbSThu Nguyen "RC", rc);
639a34a64bbSThu Nguyen pldm::utils::reportError(
640a34a64bbSThu Nguyen "xyz.openbmc_project.bmc.pldm.SetHostEffecterFailed");
641a34a64bbSThu Nguyen }
642a34a64bbSThu Nguyen if (completionCode)
643a34a64bbSThu Nguyen {
644a34a64bbSThu Nguyen error(
645a34a64bbSThu Nguyen "Failed to set a remote terminus effecter, completion code '{CC}'",
646a34a64bbSThu Nguyen "CC", completionCode);
647a34a64bbSThu Nguyen pldm::utils::reportError(
648a34a64bbSThu Nguyen "xyz.openbmc_project.bmc.pldm.SetHostEffecterFailed");
649a34a64bbSThu Nguyen }
650a34a64bbSThu Nguyen };
651a34a64bbSThu Nguyen
652a34a64bbSThu Nguyen rc = handler->registerRequest(
653a34a64bbSThu Nguyen mctpEid, instanceId, PLDM_PLATFORM, PLDM_SET_STATE_EFFECTER_STATES,
654a34a64bbSThu Nguyen std::move(requestMsg), std::move(setStateEffecterStatesRespHandler));
655a34a64bbSThu Nguyen if (rc)
656a34a64bbSThu Nguyen {
657a34a64bbSThu Nguyen error(
658a34a64bbSThu Nguyen "Failed to send request to set an effecter on remote terminus for effecter ID '{EFFECTERID}', response code '{RC}'",
659a34a64bbSThu Nguyen "EFFECTERID", effecterId, "RC", rc);
660a34a64bbSThu Nguyen }
661a34a64bbSThu Nguyen return rc;
662a34a64bbSThu Nguyen }
663a34a64bbSThu Nguyen
createHostEffecterMatch(const std::string & objectPath,const std::string & interface,size_t effecterInfoIndex,size_t dbusInfoIndex,uint16_t effecterId)664a34a64bbSThu Nguyen void HostEffecterParser::createHostEffecterMatch(
665a34a64bbSThu Nguyen const std::string& objectPath, const std::string& interface,
666a34a64bbSThu Nguyen size_t effecterInfoIndex, size_t dbusInfoIndex, uint16_t effecterId)
667a34a64bbSThu Nguyen {
668a34a64bbSThu Nguyen using namespace sdbusplus::bus::match::rules;
669a34a64bbSThu Nguyen effecterInfoMatch.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
670a34a64bbSThu Nguyen pldm::utils::DBusHandler::getBus(),
671a34a64bbSThu Nguyen propertiesChanged(objectPath, interface),
672a34a64bbSThu Nguyen [this, effecterInfoIndex, dbusInfoIndex,
673a34a64bbSThu Nguyen effecterId](sdbusplus::message_t& msg) {
674a34a64bbSThu Nguyen DbusChgHostEffecterProps props;
675a34a64bbSThu Nguyen std::string iface;
676a34a64bbSThu Nguyen msg.read(iface, props);
677a34a64bbSThu Nguyen processHostEffecterChangeNotification(props, effecterInfoIndex,
678a34a64bbSThu Nguyen dbusInfoIndex, effecterId);
679a34a64bbSThu Nguyen }));
680a34a64bbSThu Nguyen }
681a34a64bbSThu Nguyen
682a34a64bbSThu Nguyen } // namespace host_effecters
683a34a64bbSThu Nguyen } // namespace pldm
684