blob: 80e140130f7a31f57fffcf022a071b819164d592 [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 }
Sora Sua8231fd2025-08-06 14:20:49 +0800392 else if (dBusMap.propertyType == "array[string]")
393 {
394 std::variant<std::vector<std::string>> v =
395 std::get<std::vector<std::string>>(value);
396 setDbusValue(v);
397 }
George Liu1e44c732020-02-28 20:20:06 +0800398 else
399 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500400 error("Unsupported property type '{TYPE}'", "TYPE",
401 dBusMap.propertyType);
402 throw std::invalid_argument("UnSupported Dbus Type");
George Liu1e44c732020-02-28 20:20:06 +0800403 }
404}
405
John Wang9e242422020-03-05 08:37:50 +0800406PropertyValue DBusHandler::getDbusPropertyVariant(
407 const char* objPath, const char* dbusProp, const char* dbusInterface) const
408{
409 auto& bus = DBusHandler::getBus();
410 auto service = getService(objPath, dbusInterface);
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400411 auto method =
412 bus.new_method_call(service.c_str(), objPath, dbusProperties, "Get");
John Wang9e242422020-03-05 08:37:50 +0800413 method.append(dbusInterface, dbusProp);
Patrick Williams75b8f462024-02-07 10:59:26 -0600414 return bus.call(method, dbusTimeout).unpack<PropertyValue>();
John Wang9e242422020-03-05 08:37:50 +0800415}
416
Unive Tienc40d4a62025-03-12 11:36:07 +0800417GetAssociatedSubTreeResponse DBusHandler::getAssociatedSubTree(
418 const sdbusplus::message::object_path& objectPath,
419 const sdbusplus::message::object_path& subtree, int depth,
420 const std::vector<std::string>& ifaceList) const
421{
422 auto& bus = DBusHandler::getBus();
423 auto method = bus.new_method_call(
424 ObjectMapper::default_service, ObjectMapper::instance_path,
425 ObjectMapper::interface, "GetAssociatedSubTree");
426 method.append(objectPath, subtree, depth, ifaceList);
427 auto reply = bus.call(method, dbusTimeout);
428 GetAssociatedSubTreeResponse response;
429 reply.read(response);
430 return response;
431}
432
Riya Dixit754041d2024-02-20 06:15:49 -0600433ObjectValueTree DBusHandler::getManagedObj(const char* service,
434 const char* rootPath)
435{
436 auto& bus = DBusHandler::getBus();
437 auto method = bus.new_method_call(service, rootPath,
438 "org.freedesktop.DBus.ObjectManager",
439 "GetManagedObjects");
440 return bus.call(method).unpack<ObjectValueTree>();
441}
442
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400443PropertyMap DBusHandler::getDbusPropertiesVariant(
444 const char* serviceName, const char* objPath,
445 const char* dbusInterface) const
Gilbert Chen44524a52022-02-14 12:12:25 +0000446{
447 auto& bus = DBusHandler::getBus();
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400448 auto method =
449 bus.new_method_call(serviceName, objPath, dbusProperties, "GetAll");
Gilbert Chen44524a52022-02-14 12:12:25 +0000450 method.append(dbusInterface);
451 return bus.call(method, dbusTimeout).unpack<PropertyMap>();
452}
453
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530454PropertyValue jsonEntryToDbusVal(std::string_view type,
455 const nlohmann::json& value)
456{
457 PropertyValue propValue{};
458 if (type == "uint8_t")
459 {
460 propValue = static_cast<uint8_t>(value);
461 }
462 else if (type == "uint16_t")
463 {
464 propValue = static_cast<uint16_t>(value);
465 }
466 else if (type == "uint32_t")
467 {
468 propValue = static_cast<uint32_t>(value);
469 }
470 else if (type == "uint64_t")
471 {
472 propValue = static_cast<uint64_t>(value);
473 }
474 else if (type == "int16_t")
475 {
476 propValue = static_cast<int16_t>(value);
477 }
478 else if (type == "int32_t")
479 {
480 propValue = static_cast<int32_t>(value);
481 }
482 else if (type == "int64_t")
483 {
484 propValue = static_cast<int64_t>(value);
485 }
486 else if (type == "bool")
487 {
488 propValue = static_cast<bool>(value);
489 }
490 else if (type == "double")
491 {
492 propValue = static_cast<double>(value);
493 }
494 else if (type == "string")
495 {
496 propValue = static_cast<std::string>(value);
497 }
498 else
499 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500500 error("Unknown D-Bus property type '{TYPE}'", "TYPE", type);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530501 }
502
503 return propValue;
504}
505
Tom Joseph250c4752020-04-15 10:32:45 +0530506uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
507 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500508 uint16_t stateSetId, bool localOrRemote)
Tom Joseph250c4752020-04-15 10:32:45 +0530509{
510 uint8_t* pdrData = nullptr;
511 uint32_t pdrSize{};
512 const pldm_pdr_record* record{};
513 do
514 {
515 record = pldm_pdr_find_record_by_type(pdrRepo, PLDM_STATE_EFFECTER_PDR,
516 record, &pdrData, &pdrSize);
Sampa Misraa4a96162020-07-14 05:33:46 -0500517 if (record && (localOrRemote ^ pldm_pdr_record_is_remote(record)))
Tom Joseph250c4752020-04-15 10:32:45 +0530518 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530519 auto pdr = new (pdrData) pldm_state_effecter_pdr;
Tom Joseph250c4752020-04-15 10:32:45 +0530520 auto compositeEffecterCount = pdr->composite_effecter_count;
521 auto possible_states_start = pdr->possible_states;
522
523 for (auto effecters = 0x00; effecters < compositeEffecterCount;
524 effecters++)
525 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530526 auto possibleStates = new (possible_states_start)
527 state_effecter_possible_states;
Tom Joseph250c4752020-04-15 10:32:45 +0530528 auto setId = possibleStates->state_set_id;
529 auto possibleStateSize = possibleStates->possible_states_size;
530
531 if (entityType == pdr->entity_type &&
532 entityInstance == pdr->entity_instance &&
533 containerId == pdr->container_id && stateSetId == setId)
534 {
535 return pdr->effecter_id;
536 }
537 possible_states_start += possibleStateSize + sizeof(setId) +
538 sizeof(possibleStateSize);
539 }
540 }
541 } while (record);
542
543 return PLDM_INVALID_EFFECTER_ID;
544}
545
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800546int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
547 uint8_t sensorOffset, uint8_t eventState,
548 uint8_t previousEventState)
549{
550 try
551 {
552 auto& bus = DBusHandler::getBus();
553 auto msg = bus.new_signal("/xyz/openbmc_project/pldm",
554 "xyz.openbmc_project.PLDM.Event",
555 "StateSensorEvent");
556 msg.append(tid, sensorId, sensorOffset, eventState, previousEventState);
557
558 msg.signal_send();
559 }
Patrick Williams51330582021-10-06 12:48:56 -0500560 catch (const std::exception& e)
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800561 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500562 error("Failed to emit pldm event signal, error - {ERROR}", "ERROR", e);
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800563 return PLDM_ERROR;
564 }
565
566 return PLDM_SUCCESS;
567}
568
Chau Ly8fa40db2024-04-02 09:32:01 +0000569void recoverMctpEndpoint(const std::string& endpointObjPath)
570{
571 auto& bus = DBusHandler::getBus();
572 try
573 {
574 std::string service = DBusHandler().getService(endpointObjPath.c_str(),
575 MCTP_INTERFACE_CC);
576
577 auto method = bus.new_method_call(
578 service.c_str(), endpointObjPath.c_str(), MCTP_INTERFACE_CC,
579 MCTP_ENDPOINT_RECOVER_METHOD);
580 bus.call_noreply(method, dbusTimeout);
581 }
582 catch (const std::exception& e)
583 {
584 error(
585 "failed to make a D-Bus call to recover MCTP Endpoint, ERROR {ERR_EXCEP}",
586 "ERR_EXCEP", e);
587 }
588}
589
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500590uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
591 uint16_t entityType, uint16_t entityInstance,
592 uint16_t containerId, uint16_t stateSetId)
593{
594 auto pdrs = findStateSensorPDR(tid, entityType, stateSetId, pdrRepo);
595 for (auto pdr : pdrs)
596 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530597 auto sensorPdr = new (pdr.data()) pldm_state_sensor_pdr;
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500598 auto compositeSensorCount = sensorPdr->composite_sensor_count;
599 auto possible_states_start = sensorPdr->possible_states;
600
601 for (auto sensors = 0x00; sensors < compositeSensorCount; sensors++)
602 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530603 auto possibleStates = new (possible_states_start)
604 state_sensor_possible_states;
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500605 auto setId = possibleStates->state_set_id;
606 auto possibleStateSize = possibleStates->possible_states_size;
607 if (entityType == sensorPdr->entity_type &&
608 entityInstance == sensorPdr->entity_instance &&
609 stateSetId == setId && containerId == sensorPdr->container_id)
610 {
611 return sensorPdr->sensor_id;
612 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400613 possible_states_start +=
614 possibleStateSize + sizeof(setId) + sizeof(possibleStateSize);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500615 }
616 }
617 return PLDM_INVALID_EFFECTER_ID;
618}
619
Tom Josephe5268cd2021-09-07 13:04:03 +0530620void printBuffer(bool isTx, const std::vector<uint8_t>& buffer)
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600621{
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530622 if (buffer.empty())
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600623 {
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530624 return;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600625 }
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530626
627 std::cout << (isTx ? "Tx: " : "Rx: ");
628
629 std::ranges::for_each(buffer, [](uint8_t byte) {
630 std::cout << std::format("{:02x} ", byte);
631 });
632
633 std::cout << std::endl;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600634}
635
Tom Joseph54922072021-06-19 02:45:46 -0700636std::string toString(const struct variable_field& var)
637{
638 if (var.ptr == nullptr || !var.length)
639 {
640 return "";
641 }
642
643 std::string str(reinterpret_cast<const char*>(var.ptr), var.length);
644 std::replace_if(
645 str.begin(), str.end(), [](const char& c) { return !isprint(c); }, ' ');
646 return str;
647}
648
George Liu872f0f62021-11-25 16:26:16 +0800649std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
650 std::string_view trimStr)
651{
652 std::vector<std::string> out;
George Liua9eacff2025-08-25 11:20:52 +0800653 size_t start = 0;
George Liu872f0f62021-11-25 16:26:16 +0800654 size_t end = 0;
655
656 while ((start = srcStr.find_first_not_of(delim, end)) != std::string::npos)
657 {
658 end = srcStr.find(delim, start);
659 std::string_view dstStr = srcStr.substr(start, end - start);
660 if (!trimStr.empty())
661 {
662 dstStr.remove_prefix(dstStr.find_first_not_of(trimStr));
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400663 dstStr.remove_suffix(
664 dstStr.size() - 1 - dstStr.find_last_not_of(trimStr));
George Liu872f0f62021-11-25 16:26:16 +0800665 }
666
667 if (!dstStr.empty())
668 {
George Liua9eacff2025-08-25 11:20:52 +0800669 out.emplace_back(dstStr);
George Liu872f0f62021-11-25 16:26:16 +0800670 }
671 }
672
673 return out;
674}
675
Manojkiran Edaef773052021-07-29 09:29:28 +0530676std::string getCurrentSystemTime()
677{
Manojkiran Eda09a89822024-04-24 11:02:44 +0530678 const auto zonedTime{std::chrono::zoned_time{
679 std::chrono::current_zone(), std::chrono::system_clock::now()}};
680 return std::format("{:%F %Z %T}", zonedTime);
Manojkiran Edaef773052021-07-29 09:29:28 +0530681}
682
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500683bool checkForFruPresence(const std::string& objPath)
684{
685 bool isPresent = false;
686 static constexpr auto presentInterface =
687 "xyz.openbmc_project.Inventory.Item";
688 static constexpr auto presentProperty = "Present";
689 try
690 {
691 auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant(
692 objPath.c_str(), presentProperty, presentInterface);
693 isPresent = std::get<bool>(propVal);
694 }
695 catch (const sdbusplus::exception::SdBusError& e)
696 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500697 error("Failed to check for FRU presence at {PATH}, error - {ERROR}",
698 "PATH", objPath, "ERROR", e);
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500699 }
700 return isPresent;
701}
702
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600703bool checkIfLogicalBitSet(const uint16_t& containerId)
704{
705 return !(containerId & 0x8000);
706}
707
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500708void setFruPresence(const std::string& fruObjPath, bool present)
709{
710 pldm::utils::PropertyValue value{present};
711 pldm::utils::DBusMapping dbusMapping;
712 dbusMapping.objectPath = fruObjPath;
713 dbusMapping.interface = "xyz.openbmc_project.Inventory.Item";
714 dbusMapping.propertyName = "Present";
715 dbusMapping.propertyType = "bool";
716 try
717 {
718 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
719 }
720 catch (const std::exception& e)
721 {
722 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500723 "Failed to set the present property on path '{PATH}', error - {ERROR}.",
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500724 "PATH", fruObjPath, "ERROR", e);
725 }
726}
727
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000728std::string_view trimNameForDbus(std::string& name)
729{
730 std::replace(name.begin(), name.end(), ' ', '_');
731 auto nullTerminatorPos = name.find('\0');
732 if (nullTerminatorPos != std::string::npos)
733 {
734 name.erase(nullTerminatorPos);
735 }
736 return name;
737}
Thu Nguyena34a64b2022-03-31 08:56:39 +0700738
739bool dbusPropValuesToDouble(const std::string_view& type,
740 const pldm::utils::PropertyValue& value,
741 double* doubleValue)
742{
743 if (!dbusValueNumericTypeNames.contains(type))
744 {
745 return false;
746 }
747
748 if (!doubleValue)
749 {
750 return false;
751 }
752
753 try
754 {
755 if (type == "uint8_t")
756 {
757 *doubleValue = static_cast<double>(std::get<uint8_t>(value));
758 }
759 else if (type == "int16_t")
760 {
761 *doubleValue = static_cast<double>(std::get<int16_t>(value));
762 }
763 else if (type == "uint16_t")
764 {
765 *doubleValue = static_cast<double>(std::get<uint16_t>(value));
766 }
767 else if (type == "int32_t")
768 {
769 *doubleValue = static_cast<double>(std::get<int32_t>(value));
770 }
771 else if (type == "uint32_t")
772 {
773 *doubleValue = static_cast<double>(std::get<uint32_t>(value));
774 }
775 else if (type == "int64_t")
776 {
777 *doubleValue = static_cast<double>(std::get<int64_t>(value));
778 }
779 else if (type == "uint64_t")
780 {
781 *doubleValue = static_cast<double>(std::get<uint64_t>(value));
782 }
783 else if (type == "double")
784 {
785 *doubleValue = static_cast<double>(std::get<double>(value));
786 }
787 else
788 {
789 return false;
790 }
791 }
792 catch (const std::exception& e)
793 {
794 return false;
795 }
796
797 return true;
798}
Dung Caob6d39432024-06-05 03:46:47 +0000799
Patrick Williams366507c2025-02-03 14:28:01 -0500800std::optional<std::string> fruFieldValuestring(const uint8_t* value,
801 const uint8_t& length)
Dung Caob6d39432024-06-05 03:46:47 +0000802{
803 if (!value || !length)
804 {
Dung Caob6d39432024-06-05 03:46:47 +0000805 return std::nullopt;
806 }
807
808 return std::string(reinterpret_cast<const char*>(value), length);
809}
810
811std::optional<uint32_t> fruFieldParserU32(const uint8_t* value,
812 const uint8_t& length)
813{
814 if (!value || length != sizeof(uint32_t))
815 {
816 lg2::error("Fru data to u32 invalid data.");
817 return std::nullopt;
818 }
819
820 uint32_t ret;
821 std::memcpy(&ret, value, length);
822 return ret;
823}
824
Pavithra Barithaya36ac5592025-06-16 11:41:37 +0530825SensorPDRs getStateSensorPDRsByType(uint16_t entityType, const pldm_pdr* repo)
826{
827 uint8_t* outData = nullptr;
828 uint32_t size{};
829 const pldm_pdr_record* record = nullptr;
830 SensorPDRs pdrs;
831
832 if (repo)
833 {
834 while ((record = pldm_pdr_find_record_by_type(
835 repo, PLDM_STATE_SENSOR_PDR, record, &outData, &size)))
836 {
837 auto pdr = new (outData) pldm_state_sensor_pdr;
838 if (pdr && pdr->entity_type == entityType)
839 {
840 pdrs.emplace_back(outData, outData + size);
841 }
842 }
843 }
844
845 return pdrs;
846}
847
848std::vector<pldm::pdr::SensorID> findSensorIds(
849 const pldm_pdr* pdrRepo, uint16_t entityType, uint16_t entityInstance,
850 uint16_t containerId)
851{
852 std::vector<uint16_t> sensorIDs;
853 auto pdrs = getStateSensorPDRsByType(entityType, pdrRepo);
854
855 for (const auto& pdr : pdrs)
856 {
857 auto sensorPdr =
858 reinterpret_cast<const pldm_state_sensor_pdr*>(pdr.data());
859
860 if (sensorPdr && sensorPdr->entity_type == entityType &&
861 sensorPdr->entity_instance == entityInstance &&
862 sensorPdr->container_id == containerId)
863 {
864 sensorIDs.emplace_back(sensorPdr->sensor_id);
865 }
866 }
867
868 return sensorIDs;
869}
870
George Liu83409572019-12-24 18:42:54 +0800871} // namespace utils
872} // namespace pldm