blob: 06c256c2485da5dd7a2a429e7df3652114998eb8 [file] [log] [blame]
George Liu83409572019-12-24 18:42:54 +08001#include "utils.hpp"
2
George Liuc453e162022-12-21 17:16:23 +08003#include <libpldm/pdr.h>
4#include <libpldm/pldm_types.h>
Gilbert Chen6c7fed42022-02-22 15:40:17 +00005#include <linux/mctp.h>
George Liu6492f522020-06-16 10:34:05 +08006
Riya Dixit49cfb132023-03-02 04:26:53 -06007#include <phosphor-logging/lg2.hpp>
George Liu6492f522020-06-16 10:34:05 +08008#include <xyz/openbmc_project/Common/error.hpp>
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -06009#include <xyz/openbmc_project/Logging/Create/client.hpp>
10#include <xyz/openbmc_project/ObjectMapper/client.hpp>
George Liu6492f522020-06-16 10:34:05 +080011
Tom Joseph54922072021-06-19 02:45:46 -070012#include <algorithm>
George Liu83409572019-12-24 18:42:54 +080013#include <array>
Tom Joseph54922072021-06-19 02:45:46 -070014#include <cctype>
George Liu83409572019-12-24 18:42:54 +080015#include <ctime>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050016#include <fstream>
George Liu83409572019-12-24 18:42:54 +080017#include <iostream>
18#include <map>
George Liu83409572019-12-24 18:42:54 +080019#include <stdexcept>
20#include <string>
21#include <vector>
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050022
Riya Dixit49cfb132023-03-02 04:26:53 -060023PHOSPHOR_LOG2_USING;
24
George Liu83409572019-12-24 18:42:54 +080025namespace pldm
26{
27namespace utils
28{
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -060029
Chau Ly8fa40db2024-04-02 09:32:01 +000030using ObjectMapper = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>;
31
32constexpr const char* MCTP_INTERFACE_CC = "au.com.codeconstruct.MCTP.Endpoint1";
33constexpr const char* MCTP_ENDPOINT_RECOVER_METHOD = "Recover";
34
Patrick Williams366507c2025-02-03 14:28:01 -050035std::vector<std::vector<uint8_t>> findStateEffecterPDR(
36 uint8_t /*tid*/, uint16_t entityID, uint16_t stateSetId,
37 const pldm_pdr* repo)
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050038{
39 uint8_t* outData = nullptr;
40 uint32_t size{};
41 const pldm_pdr_record* record{};
42 std::vector<std::vector<uint8_t>> pdrs;
43 try
44 {
45 do
46 {
47 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_EFFECTER_PDR,
48 record, &outData, &size);
49 if (record)
50 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +053051 auto pdr = new (outData) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050052 auto compositeEffecterCount = pdr->composite_effecter_count;
Chicago Duana7aacc32020-06-10 18:03:38 +080053 auto possible_states_start = pdr->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050054
55 for (auto effecters = 0x00; effecters < compositeEffecterCount;
56 effecters++)
57 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +053058 auto possibleStates = new (possible_states_start)
59 state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050060 auto setId = possibleStates->state_set_id;
61 auto possibleStateSize =
62 possibleStates->possible_states_size;
63
64 if (pdr->entity_type == entityID && setId == stateSetId)
65 {
66 std::vector<uint8_t> effecter_pdr(&outData[0],
67 &outData[size]);
68 pdrs.emplace_back(std::move(effecter_pdr));
69 break;
70 }
Chicago Duana7aacc32020-06-10 18:03:38 +080071 possible_states_start += possibleStateSize + sizeof(setId) +
72 sizeof(possibleStateSize);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050073 }
74 }
75
76 } while (record);
77 }
78 catch (const std::exception& e)
79 {
Riya Dixit76f2c602024-03-28 07:34:12 -050080 error("Failed to obtain a record, error - {ERROR}", "ERROR", e);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050081 }
82
83 return pdrs;
84}
85
Patrick Williams366507c2025-02-03 14:28:01 -050086std::vector<std::vector<uint8_t>> findStateSensorPDR(
87 uint8_t /*tid*/, uint16_t entityID, uint16_t stateSetId,
88 const pldm_pdr* repo)
Chicago Duan738e4d82020-05-28 16:39:19 +080089{
90 uint8_t* outData = nullptr;
91 uint32_t size{};
92 const pldm_pdr_record* record{};
93 std::vector<std::vector<uint8_t>> pdrs;
94 try
95 {
96 do
97 {
98 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_SENSOR_PDR,
99 record, &outData, &size);
100 if (record)
101 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530102 auto pdr = new (outData) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800103 auto compositeSensorCount = pdr->composite_sensor_count;
Chicago Duana7aacc32020-06-10 18:03:38 +0800104 auto possible_states_start = pdr->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800105
106 for (auto sensors = 0x00; sensors < compositeSensorCount;
107 sensors++)
108 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530109 auto possibleStates = new (possible_states_start)
110 state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800111 auto setId = possibleStates->state_set_id;
112 auto possibleStateSize =
113 possibleStates->possible_states_size;
114
115 if (pdr->entity_type == entityID && setId == stateSetId)
116 {
117 std::vector<uint8_t> sensor_pdr(&outData[0],
118 &outData[size]);
119 pdrs.emplace_back(std::move(sensor_pdr));
120 break;
121 }
Chicago Duana7aacc32020-06-10 18:03:38 +0800122 possible_states_start += possibleStateSize + sizeof(setId) +
123 sizeof(possibleStateSize);
Chicago Duan738e4d82020-05-28 16:39:19 +0800124 }
125 }
126
127 } while (record);
128 }
129 catch (const std::exception& e)
130 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500131 error(
132 "Failed to obtain a record with entity ID '{ENTITYID}', error - {ERROR}",
133 "ENTITYID", entityID, "ERROR", e);
Chicago Duan738e4d82020-05-28 16:39:19 +0800134 }
135
136 return pdrs;
137}
138
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500139uint8_t readHostEID()
140{
141 uint8_t eid{};
Brad Bishop06052cc2021-08-16 15:17:16 -0400142 std::ifstream eidFile{HOST_EID_PATH};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500143 if (!eidFile.good())
144 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500145 error("Failed to open remote terminus EID file at path '{PATH}'",
146 "PATH", static_cast<std::string>(HOST_EID_PATH));
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500147 }
148 else
149 {
150 std::string eidStr;
151 eidFile >> eidStr;
152 if (!eidStr.empty())
153 {
154 eid = atoi(eidStr.c_str());
155 }
156 else
157 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500158 error("Remote terminus EID file was empty");
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500159 }
160 }
161
162 return eid;
163}
George Liu83409572019-12-24 18:42:54 +0800164
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000165bool isValidEID(eid mctpEid)
166{
167 if (mctpEid == MCTP_ADDR_NULL || mctpEid < MCTP_START_VALID_EID ||
168 mctpEid == MCTP_ADDR_ANY)
169 {
170 return false;
171 }
172
173 return true;
174}
175
George Liu83409572019-12-24 18:42:54 +0800176uint8_t getNumPadBytes(uint32_t data)
177{
178 uint8_t pad;
179 pad = ((data % 4) ? (4 - data % 4) : 0);
180 return pad;
181} // end getNumPadBytes
182
183bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
184 uint8_t* hour, uint8_t* min, uint8_t* sec)
185{
186 constexpr uint64_t max_data = 29991231115959;
187 constexpr uint64_t min_data = 19700101000000;
188 if (data < min_data || data > max_data)
189 {
190 return false;
191 }
192
193 *year = data / 10000000000;
194 data = data % 10000000000;
195 *month = data / 100000000;
196 data = data % 100000000;
197 *day = data / 1000000;
198 data = data % 1000000;
199 *hour = data / 10000;
200 data = data % 10000;
201 *min = data / 100;
202 *sec = data % 100;
203
204 return true;
205}
206
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400207std::optional<std::vector<set_effecter_state_field>> parseEffecterData(
208 const std::vector<uint8_t>& effecterData, uint8_t effecterCount)
George Liu83409572019-12-24 18:42:54 +0800209{
George Liuba4c1fb2020-02-05 14:13:30 +0800210 std::vector<set_effecter_state_field> stateField;
211
212 if (effecterData.size() != effecterCount * 2)
George Liu83409572019-12-24 18:42:54 +0800213 {
George Liuba4c1fb2020-02-05 14:13:30 +0800214 return std::nullopt;
George Liu83409572019-12-24 18:42:54 +0800215 }
216
George Liuba4c1fb2020-02-05 14:13:30 +0800217 for (uint8_t i = 0; i < effecterCount; ++i)
George Liu83409572019-12-24 18:42:54 +0800218 {
George Liuba4c1fb2020-02-05 14:13:30 +0800219 uint8_t set_request = effecterData[i * 2] == PLDM_REQUEST_SET
220 ? PLDM_REQUEST_SET
221 : PLDM_NO_CHANGE;
222 set_effecter_state_field filed{set_request, effecterData[i * 2 + 1]};
223 stateField.emplace_back(std::move(filed));
George Liu83409572019-12-24 18:42:54 +0800224 }
225
George Liuba4c1fb2020-02-05 14:13:30 +0800226 return std::make_optional(std::move(stateField));
George Liu83409572019-12-24 18:42:54 +0800227}
228
George Liu0e02c322020-01-01 09:41:51 +0800229std::string DBusHandler::getService(const char* path,
230 const char* interface) const
George Liu83409572019-12-24 18:42:54 +0800231{
232 using DbusInterfaceList = std::vector<std::string>;
233 std::map<std::string, std::vector<std::string>> mapperResponse;
George Liu0e02c322020-01-01 09:41:51 +0800234 auto& bus = DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +0800235
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600236 auto mapper = bus.new_method_call(ObjectMapper::default_service,
237 ObjectMapper::instance_path,
238 ObjectMapper::interface, "GetObject");
George Liudf9a6d32020-12-22 16:27:16 +0800239
240 if (interface)
241 {
242 mapper.append(path, DbusInterfaceList({interface}));
243 }
244 else
245 {
246 mapper.append(path, DbusInterfaceList({}));
247 }
George Liu83409572019-12-24 18:42:54 +0800248
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500249 auto mapperResponseMsg = bus.call(mapper, dbusTimeout);
George Liu0e02c322020-01-01 09:41:51 +0800250 mapperResponseMsg.read(mapperResponse);
George Liu83409572019-12-24 18:42:54 +0800251 return mapperResponse.begin()->first;
252}
253
Patrick Williams366507c2025-02-03 14:28:01 -0500254GetSubTreeResponse DBusHandler::getSubtree(
255 const std::string& searchPath, int depth,
256 const std::vector<std::string>& ifaceList) const
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530257{
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530258 auto& bus = pldm::utils::DBusHandler::getBus();
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600259 auto method = bus.new_method_call(ObjectMapper::default_service,
260 ObjectMapper::instance_path,
261 ObjectMapper::interface, "GetSubTree");
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530262 method.append(searchPath, depth, ifaceList);
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500263 auto reply = bus.call(method, dbusTimeout);
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530264 GetSubTreeResponse response;
265 reply.read(response);
266 return response;
267}
268
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500269GetSubTreePathsResponse DBusHandler::getSubTreePaths(
270 const std::string& objectPath, int depth,
271 const std::vector<std::string>& ifaceList) const
272{
273 std::vector<std::string> paths;
274 auto& bus = pldm::utils::DBusHandler::getBus();
275 auto method = bus.new_method_call(
276 ObjectMapper::default_service, ObjectMapper::instance_path,
277 ObjectMapper::interface, "GetSubTreePaths");
278 method.append(objectPath, depth, ifaceList);
279 auto reply = bus.call(method, dbusTimeout);
280
281 reply.read(paths);
282 return paths;
283}
284
Delphine CC Chiu549e4bc2024-03-06 11:24:05 +0800285GetAncestorsResponse DBusHandler::getAncestors(
286 const std::string& path, const std::vector<std::string>& ifaceList) const
287{
288 auto& bus = pldm::utils::DBusHandler::getBus();
289 auto method = bus.new_method_call(ObjectMapper::default_service,
290 ObjectMapper::instance_path,
291 ObjectMapper::interface, "GetAncestors");
292 method.append(path, ifaceList);
293 auto reply = bus.call(method, dbusTimeout);
294 GetAncestorsResponse response;
295 reply.read(response);
296 return response;
297}
298
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530299void reportError(const char* errorMsg)
George Liu83409572019-12-24 18:42:54 +0800300{
George Liu0e02c322020-01-01 09:41:51 +0800301 auto& bus = pldm::utils::DBusHandler::getBus();
Riya Dixit76f2c602024-03-28 07:34:12 -0500302 using LoggingCreate =
303 sdbusplus::client::xyz::openbmc_project::logging::Create<>;
George Liu83409572019-12-24 18:42:54 +0800304 try
305 {
George Liu83409572019-12-24 18:42:54 +0800306 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530307 auto severity =
308 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
309 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
310 Error);
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600311 auto method = bus.new_method_call(LoggingCreate::default_service,
312 LoggingCreate::instance_path,
313 LoggingCreate::interface, "Create");
314
George Liu83409572019-12-24 18:42:54 +0800315 std::map<std::string, std::string> addlData{};
316 method.append(errorMsg, severity, addlData);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000317 bus.call_noreply(method, dbusTimeout);
George Liu83409572019-12-24 18:42:54 +0800318 }
319 catch (const std::exception& e)
320 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600321 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500322 "Failed to do dbus call for creating error log for '{ERRMSG}' at path '{PATH}' and interface '{INTERFACE}', error - {ERROR}",
323 "ERRMSG", errorMsg, "PATH", LoggingCreate::instance_path,
324 "INTERFACE", LoggingCreate::interface, "ERROR", e);
George Liu83409572019-12-24 18:42:54 +0800325 }
326}
327
George Liu1e44c732020-02-28 20:20:06 +0800328void DBusHandler::setDbusProperty(const DBusMapping& dBusMap,
329 const PropertyValue& value) const
330{
331 auto setDbusValue = [&dBusMap, this](const auto& variant) {
332 auto& bus = getBus();
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400333 auto service =
334 getService(dBusMap.objectPath.c_str(), dBusMap.interface.c_str());
George Liu1e44c732020-02-28 20:20:06 +0800335 auto method = bus.new_method_call(
336 service.c_str(), dBusMap.objectPath.c_str(), dbusProperties, "Set");
337 method.append(dBusMap.interface.c_str(), dBusMap.propertyName.c_str(),
338 variant);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000339 bus.call_noreply(method, dbusTimeout);
George Liu1e44c732020-02-28 20:20:06 +0800340 };
341
342 if (dBusMap.propertyType == "uint8_t")
343 {
344 std::variant<uint8_t> v = std::get<uint8_t>(value);
345 setDbusValue(v);
346 }
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600347 else if (dBusMap.propertyType == "bool")
348 {
349 std::variant<bool> v = std::get<bool>(value);
350 setDbusValue(v);
351 }
George Liu1e44c732020-02-28 20:20:06 +0800352 else if (dBusMap.propertyType == "int16_t")
353 {
354 std::variant<int16_t> v = std::get<int16_t>(value);
355 setDbusValue(v);
356 }
357 else if (dBusMap.propertyType == "uint16_t")
358 {
359 std::variant<uint16_t> v = std::get<uint16_t>(value);
360 setDbusValue(v);
361 }
362 else if (dBusMap.propertyType == "int32_t")
363 {
364 std::variant<int32_t> v = std::get<int32_t>(value);
365 setDbusValue(v);
366 }
367 else if (dBusMap.propertyType == "uint32_t")
368 {
369 std::variant<uint32_t> v = std::get<uint32_t>(value);
370 setDbusValue(v);
371 }
372 else if (dBusMap.propertyType == "int64_t")
373 {
374 std::variant<int64_t> v = std::get<int64_t>(value);
375 setDbusValue(v);
376 }
377 else if (dBusMap.propertyType == "uint64_t")
378 {
379 std::variant<uint64_t> v = std::get<uint64_t>(value);
380 setDbusValue(v);
381 }
382 else if (dBusMap.propertyType == "double")
383 {
384 std::variant<double> v = std::get<double>(value);
385 setDbusValue(v);
386 }
387 else if (dBusMap.propertyType == "string")
388 {
389 std::variant<std::string> v = std::get<std::string>(value);
390 setDbusValue(v);
391 }
392 else
393 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500394 error("Unsupported property type '{TYPE}'", "TYPE",
395 dBusMap.propertyType);
396 throw std::invalid_argument("UnSupported Dbus Type");
George Liu1e44c732020-02-28 20:20:06 +0800397 }
398}
399
John Wang9e242422020-03-05 08:37:50 +0800400PropertyValue DBusHandler::getDbusPropertyVariant(
401 const char* objPath, const char* dbusProp, const char* dbusInterface) const
402{
403 auto& bus = DBusHandler::getBus();
404 auto service = getService(objPath, dbusInterface);
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400405 auto method =
406 bus.new_method_call(service.c_str(), objPath, dbusProperties, "Get");
John Wang9e242422020-03-05 08:37:50 +0800407 method.append(dbusInterface, dbusProp);
Patrick Williams75b8f462024-02-07 10:59:26 -0600408 return bus.call(method, dbusTimeout).unpack<PropertyValue>();
John Wang9e242422020-03-05 08:37:50 +0800409}
410
Unive Tienc40d4a62025-03-12 11:36:07 +0800411GetAssociatedSubTreeResponse DBusHandler::getAssociatedSubTree(
412 const sdbusplus::message::object_path& objectPath,
413 const sdbusplus::message::object_path& subtree, int depth,
414 const std::vector<std::string>& ifaceList) const
415{
416 auto& bus = DBusHandler::getBus();
417 auto method = bus.new_method_call(
418 ObjectMapper::default_service, ObjectMapper::instance_path,
419 ObjectMapper::interface, "GetAssociatedSubTree");
420 method.append(objectPath, subtree, depth, ifaceList);
421 auto reply = bus.call(method, dbusTimeout);
422 GetAssociatedSubTreeResponse response;
423 reply.read(response);
424 return response;
425}
426
Riya Dixit754041d2024-02-20 06:15:49 -0600427ObjectValueTree DBusHandler::getManagedObj(const char* service,
428 const char* rootPath)
429{
430 auto& bus = DBusHandler::getBus();
431 auto method = bus.new_method_call(service, rootPath,
432 "org.freedesktop.DBus.ObjectManager",
433 "GetManagedObjects");
434 return bus.call(method).unpack<ObjectValueTree>();
435}
436
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400437PropertyMap DBusHandler::getDbusPropertiesVariant(
438 const char* serviceName, const char* objPath,
439 const char* dbusInterface) const
Gilbert Chen44524a52022-02-14 12:12:25 +0000440{
441 auto& bus = DBusHandler::getBus();
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400442 auto method =
443 bus.new_method_call(serviceName, objPath, dbusProperties, "GetAll");
Gilbert Chen44524a52022-02-14 12:12:25 +0000444 method.append(dbusInterface);
445 return bus.call(method, dbusTimeout).unpack<PropertyMap>();
446}
447
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530448PropertyValue jsonEntryToDbusVal(std::string_view type,
449 const nlohmann::json& value)
450{
451 PropertyValue propValue{};
452 if (type == "uint8_t")
453 {
454 propValue = static_cast<uint8_t>(value);
455 }
456 else if (type == "uint16_t")
457 {
458 propValue = static_cast<uint16_t>(value);
459 }
460 else if (type == "uint32_t")
461 {
462 propValue = static_cast<uint32_t>(value);
463 }
464 else if (type == "uint64_t")
465 {
466 propValue = static_cast<uint64_t>(value);
467 }
468 else if (type == "int16_t")
469 {
470 propValue = static_cast<int16_t>(value);
471 }
472 else if (type == "int32_t")
473 {
474 propValue = static_cast<int32_t>(value);
475 }
476 else if (type == "int64_t")
477 {
478 propValue = static_cast<int64_t>(value);
479 }
480 else if (type == "bool")
481 {
482 propValue = static_cast<bool>(value);
483 }
484 else if (type == "double")
485 {
486 propValue = static_cast<double>(value);
487 }
488 else if (type == "string")
489 {
490 propValue = static_cast<std::string>(value);
491 }
492 else
493 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500494 error("Unknown D-Bus property type '{TYPE}'", "TYPE", type);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530495 }
496
497 return propValue;
498}
499
Tom Joseph250c4752020-04-15 10:32:45 +0530500uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
501 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500502 uint16_t stateSetId, bool localOrRemote)
Tom Joseph250c4752020-04-15 10:32:45 +0530503{
504 uint8_t* pdrData = nullptr;
505 uint32_t pdrSize{};
506 const pldm_pdr_record* record{};
507 do
508 {
509 record = pldm_pdr_find_record_by_type(pdrRepo, PLDM_STATE_EFFECTER_PDR,
510 record, &pdrData, &pdrSize);
Sampa Misraa4a96162020-07-14 05:33:46 -0500511 if (record && (localOrRemote ^ pldm_pdr_record_is_remote(record)))
Tom Joseph250c4752020-04-15 10:32:45 +0530512 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530513 auto pdr = new (pdrData) pldm_state_effecter_pdr;
Tom Joseph250c4752020-04-15 10:32:45 +0530514 auto compositeEffecterCount = pdr->composite_effecter_count;
515 auto possible_states_start = pdr->possible_states;
516
517 for (auto effecters = 0x00; effecters < compositeEffecterCount;
518 effecters++)
519 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530520 auto possibleStates = new (possible_states_start)
521 state_effecter_possible_states;
Tom Joseph250c4752020-04-15 10:32:45 +0530522 auto setId = possibleStates->state_set_id;
523 auto possibleStateSize = possibleStates->possible_states_size;
524
525 if (entityType == pdr->entity_type &&
526 entityInstance == pdr->entity_instance &&
527 containerId == pdr->container_id && stateSetId == setId)
528 {
529 return pdr->effecter_id;
530 }
531 possible_states_start += possibleStateSize + sizeof(setId) +
532 sizeof(possibleStateSize);
533 }
534 }
535 } while (record);
536
537 return PLDM_INVALID_EFFECTER_ID;
538}
539
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800540int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
541 uint8_t sensorOffset, uint8_t eventState,
542 uint8_t previousEventState)
543{
544 try
545 {
546 auto& bus = DBusHandler::getBus();
547 auto msg = bus.new_signal("/xyz/openbmc_project/pldm",
548 "xyz.openbmc_project.PLDM.Event",
549 "StateSensorEvent");
550 msg.append(tid, sensorId, sensorOffset, eventState, previousEventState);
551
552 msg.signal_send();
553 }
Patrick Williams51330582021-10-06 12:48:56 -0500554 catch (const std::exception& e)
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800555 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500556 error("Failed to emit pldm event signal, error - {ERROR}", "ERROR", e);
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800557 return PLDM_ERROR;
558 }
559
560 return PLDM_SUCCESS;
561}
562
Chau Ly8fa40db2024-04-02 09:32:01 +0000563void recoverMctpEndpoint(const std::string& endpointObjPath)
564{
565 auto& bus = DBusHandler::getBus();
566 try
567 {
568 std::string service = DBusHandler().getService(endpointObjPath.c_str(),
569 MCTP_INTERFACE_CC);
570
571 auto method = bus.new_method_call(
572 service.c_str(), endpointObjPath.c_str(), MCTP_INTERFACE_CC,
573 MCTP_ENDPOINT_RECOVER_METHOD);
574 bus.call_noreply(method, dbusTimeout);
575 }
576 catch (const std::exception& e)
577 {
578 error(
579 "failed to make a D-Bus call to recover MCTP Endpoint, ERROR {ERR_EXCEP}",
580 "ERR_EXCEP", e);
581 }
582}
583
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500584uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
585 uint16_t entityType, uint16_t entityInstance,
586 uint16_t containerId, uint16_t stateSetId)
587{
588 auto pdrs = findStateSensorPDR(tid, entityType, stateSetId, pdrRepo);
589 for (auto pdr : pdrs)
590 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530591 auto sensorPdr = new (pdr.data()) pldm_state_sensor_pdr;
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500592 auto compositeSensorCount = sensorPdr->composite_sensor_count;
593 auto possible_states_start = sensorPdr->possible_states;
594
595 for (auto sensors = 0x00; sensors < compositeSensorCount; sensors++)
596 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530597 auto possibleStates = new (possible_states_start)
598 state_sensor_possible_states;
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500599 auto setId = possibleStates->state_set_id;
600 auto possibleStateSize = possibleStates->possible_states_size;
601 if (entityType == sensorPdr->entity_type &&
602 entityInstance == sensorPdr->entity_instance &&
603 stateSetId == setId && containerId == sensorPdr->container_id)
604 {
605 return sensorPdr->sensor_id;
606 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400607 possible_states_start +=
608 possibleStateSize + sizeof(setId) + sizeof(possibleStateSize);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500609 }
610 }
611 return PLDM_INVALID_EFFECTER_ID;
612}
613
Tom Josephe5268cd2021-09-07 13:04:03 +0530614void printBuffer(bool isTx, const std::vector<uint8_t>& buffer)
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600615{
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530616 if (buffer.empty())
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600617 {
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530618 return;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600619 }
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530620
621 std::cout << (isTx ? "Tx: " : "Rx: ");
622
623 std::ranges::for_each(buffer, [](uint8_t byte) {
624 std::cout << std::format("{:02x} ", byte);
625 });
626
627 std::cout << std::endl;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600628}
629
Tom Joseph54922072021-06-19 02:45:46 -0700630std::string toString(const struct variable_field& var)
631{
632 if (var.ptr == nullptr || !var.length)
633 {
634 return "";
635 }
636
637 std::string str(reinterpret_cast<const char*>(var.ptr), var.length);
638 std::replace_if(
639 str.begin(), str.end(), [](const char& c) { return !isprint(c); }, ' ');
640 return str;
641}
642
George Liu872f0f62021-11-25 16:26:16 +0800643std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
644 std::string_view trimStr)
645{
646 std::vector<std::string> out;
647 size_t start;
648 size_t end = 0;
649
650 while ((start = srcStr.find_first_not_of(delim, end)) != std::string::npos)
651 {
652 end = srcStr.find(delim, start);
653 std::string_view dstStr = srcStr.substr(start, end - start);
654 if (!trimStr.empty())
655 {
656 dstStr.remove_prefix(dstStr.find_first_not_of(trimStr));
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400657 dstStr.remove_suffix(
658 dstStr.size() - 1 - dstStr.find_last_not_of(trimStr));
George Liu872f0f62021-11-25 16:26:16 +0800659 }
660
661 if (!dstStr.empty())
662 {
663 out.push_back(std::string(dstStr));
664 }
665 }
666
667 return out;
668}
669
Manojkiran Edaef773052021-07-29 09:29:28 +0530670std::string getCurrentSystemTime()
671{
Manojkiran Eda09a89822024-04-24 11:02:44 +0530672 const auto zonedTime{std::chrono::zoned_time{
673 std::chrono::current_zone(), std::chrono::system_clock::now()}};
674 return std::format("{:%F %Z %T}", zonedTime);
Manojkiran Edaef773052021-07-29 09:29:28 +0530675}
676
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500677bool checkForFruPresence(const std::string& objPath)
678{
679 bool isPresent = false;
680 static constexpr auto presentInterface =
681 "xyz.openbmc_project.Inventory.Item";
682 static constexpr auto presentProperty = "Present";
683 try
684 {
685 auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant(
686 objPath.c_str(), presentProperty, presentInterface);
687 isPresent = std::get<bool>(propVal);
688 }
689 catch (const sdbusplus::exception::SdBusError& e)
690 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500691 error("Failed to check for FRU presence at {PATH}, error - {ERROR}",
692 "PATH", objPath, "ERROR", e);
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500693 }
694 return isPresent;
695}
696
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600697bool checkIfLogicalBitSet(const uint16_t& containerId)
698{
699 return !(containerId & 0x8000);
700}
701
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500702void setFruPresence(const std::string& fruObjPath, bool present)
703{
704 pldm::utils::PropertyValue value{present};
705 pldm::utils::DBusMapping dbusMapping;
706 dbusMapping.objectPath = fruObjPath;
707 dbusMapping.interface = "xyz.openbmc_project.Inventory.Item";
708 dbusMapping.propertyName = "Present";
709 dbusMapping.propertyType = "bool";
710 try
711 {
712 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
713 }
714 catch (const std::exception& e)
715 {
716 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500717 "Failed to set the present property on path '{PATH}', error - {ERROR}.",
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500718 "PATH", fruObjPath, "ERROR", e);
719 }
720}
721
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000722std::string_view trimNameForDbus(std::string& name)
723{
724 std::replace(name.begin(), name.end(), ' ', '_');
725 auto nullTerminatorPos = name.find('\0');
726 if (nullTerminatorPos != std::string::npos)
727 {
728 name.erase(nullTerminatorPos);
729 }
730 return name;
731}
Thu Nguyena34a64b2022-03-31 08:56:39 +0700732
733bool dbusPropValuesToDouble(const std::string_view& type,
734 const pldm::utils::PropertyValue& value,
735 double* doubleValue)
736{
737 if (!dbusValueNumericTypeNames.contains(type))
738 {
739 return false;
740 }
741
742 if (!doubleValue)
743 {
744 return false;
745 }
746
747 try
748 {
749 if (type == "uint8_t")
750 {
751 *doubleValue = static_cast<double>(std::get<uint8_t>(value));
752 }
753 else if (type == "int16_t")
754 {
755 *doubleValue = static_cast<double>(std::get<int16_t>(value));
756 }
757 else if (type == "uint16_t")
758 {
759 *doubleValue = static_cast<double>(std::get<uint16_t>(value));
760 }
761 else if (type == "int32_t")
762 {
763 *doubleValue = static_cast<double>(std::get<int32_t>(value));
764 }
765 else if (type == "uint32_t")
766 {
767 *doubleValue = static_cast<double>(std::get<uint32_t>(value));
768 }
769 else if (type == "int64_t")
770 {
771 *doubleValue = static_cast<double>(std::get<int64_t>(value));
772 }
773 else if (type == "uint64_t")
774 {
775 *doubleValue = static_cast<double>(std::get<uint64_t>(value));
776 }
777 else if (type == "double")
778 {
779 *doubleValue = static_cast<double>(std::get<double>(value));
780 }
781 else
782 {
783 return false;
784 }
785 }
786 catch (const std::exception& e)
787 {
788 return false;
789 }
790
791 return true;
792}
Dung Caob6d39432024-06-05 03:46:47 +0000793
Patrick Williams366507c2025-02-03 14:28:01 -0500794std::optional<std::string> fruFieldValuestring(const uint8_t* value,
795 const uint8_t& length)
Dung Caob6d39432024-06-05 03:46:47 +0000796{
797 if (!value || !length)
798 {
799 lg2::error("Fru data to string invalid data.");
800 return std::nullopt;
801 }
802
803 return std::string(reinterpret_cast<const char*>(value), length);
804}
805
806std::optional<uint32_t> fruFieldParserU32(const uint8_t* value,
807 const uint8_t& length)
808{
809 if (!value || length != sizeof(uint32_t))
810 {
811 lg2::error("Fru data to u32 invalid data.");
812 return std::nullopt;
813 }
814
815 uint32_t ret;
816 std::memcpy(&ret, value, length);
817 return ret;
818}
819
George Liu83409572019-12-24 18:42:54 +0800820} // namespace utils
821} // namespace pldm