blob: 374037e0e989af2fd1708f1f79988d88e987be7a [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
Alexander Hansen364275c2025-11-11 12:31:42 +01007#include <xyz/openbmc_project/BIOSConfig/Manager/client.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<>;
Alexander Hansen364275c2025-11-11 12:31:42 +010031using BIOSConfigManager =
32 sdbusplus::client::xyz::openbmc_project::bios_config::Manager<>;
Chau Ly8fa40db2024-04-02 09:32:01 +000033
34constexpr const char* MCTP_INTERFACE_CC = "au.com.codeconstruct.MCTP.Endpoint1";
35constexpr const char* MCTP_ENDPOINT_RECOVER_METHOD = "Recover";
36
Patrick Williams366507c2025-02-03 14:28:01 -050037std::vector<std::vector<uint8_t>> findStateEffecterPDR(
38 uint8_t /*tid*/, uint16_t entityID, uint16_t stateSetId,
39 const pldm_pdr* repo)
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050040{
41 uint8_t* outData = nullptr;
42 uint32_t size{};
43 const pldm_pdr_record* record{};
44 std::vector<std::vector<uint8_t>> pdrs;
45 try
46 {
47 do
48 {
49 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_EFFECTER_PDR,
50 record, &outData, &size);
51 if (record)
52 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +053053 auto pdr = new (outData) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050054 auto compositeEffecterCount = pdr->composite_effecter_count;
Chicago Duana7aacc32020-06-10 18:03:38 +080055 auto possible_states_start = pdr->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050056
57 for (auto effecters = 0x00; effecters < compositeEffecterCount;
58 effecters++)
59 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +053060 auto possibleStates = new (possible_states_start)
61 state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050062 auto setId = possibleStates->state_set_id;
63 auto possibleStateSize =
64 possibleStates->possible_states_size;
65
66 if (pdr->entity_type == entityID && setId == stateSetId)
67 {
68 std::vector<uint8_t> effecter_pdr(&outData[0],
69 &outData[size]);
70 pdrs.emplace_back(std::move(effecter_pdr));
71 break;
72 }
Chicago Duana7aacc32020-06-10 18:03:38 +080073 possible_states_start += possibleStateSize + sizeof(setId) +
74 sizeof(possibleStateSize);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050075 }
76 }
77
78 } while (record);
79 }
80 catch (const std::exception& e)
81 {
Riya Dixit76f2c602024-03-28 07:34:12 -050082 error("Failed to obtain a record, error - {ERROR}", "ERROR", e);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050083 }
84
85 return pdrs;
86}
87
Patrick Williams366507c2025-02-03 14:28:01 -050088std::vector<std::vector<uint8_t>> findStateSensorPDR(
89 uint8_t /*tid*/, uint16_t entityID, uint16_t stateSetId,
90 const pldm_pdr* repo)
Chicago Duan738e4d82020-05-28 16:39:19 +080091{
92 uint8_t* outData = nullptr;
93 uint32_t size{};
94 const pldm_pdr_record* record{};
95 std::vector<std::vector<uint8_t>> pdrs;
96 try
97 {
98 do
99 {
100 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_SENSOR_PDR,
101 record, &outData, &size);
102 if (record)
103 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530104 auto pdr = new (outData) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800105 auto compositeSensorCount = pdr->composite_sensor_count;
Chicago Duana7aacc32020-06-10 18:03:38 +0800106 auto possible_states_start = pdr->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800107
108 for (auto sensors = 0x00; sensors < compositeSensorCount;
109 sensors++)
110 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530111 auto possibleStates = new (possible_states_start)
112 state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800113 auto setId = possibleStates->state_set_id;
114 auto possibleStateSize =
115 possibleStates->possible_states_size;
116
117 if (pdr->entity_type == entityID && setId == stateSetId)
118 {
119 std::vector<uint8_t> sensor_pdr(&outData[0],
120 &outData[size]);
121 pdrs.emplace_back(std::move(sensor_pdr));
122 break;
123 }
Chicago Duana7aacc32020-06-10 18:03:38 +0800124 possible_states_start += possibleStateSize + sizeof(setId) +
125 sizeof(possibleStateSize);
Chicago Duan738e4d82020-05-28 16:39:19 +0800126 }
127 }
128
129 } while (record);
130 }
131 catch (const std::exception& e)
132 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500133 error(
134 "Failed to obtain a record with entity ID '{ENTITYID}', error - {ERROR}",
135 "ENTITYID", entityID, "ERROR", e);
Chicago Duan738e4d82020-05-28 16:39:19 +0800136 }
137
138 return pdrs;
139}
140
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500141uint8_t readHostEID()
142{
143 uint8_t eid{};
Brad Bishop06052cc2021-08-16 15:17:16 -0400144 std::ifstream eidFile{HOST_EID_PATH};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500145 if (!eidFile.good())
146 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500147 error("Failed to open remote terminus EID file at path '{PATH}'",
148 "PATH", static_cast<std::string>(HOST_EID_PATH));
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500149 }
150 else
151 {
152 std::string eidStr;
153 eidFile >> eidStr;
154 if (!eidStr.empty())
155 {
156 eid = atoi(eidStr.c_str());
157 }
158 else
159 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500160 error("Remote terminus EID file was empty");
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500161 }
162 }
163
164 return eid;
165}
George Liu83409572019-12-24 18:42:54 +0800166
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000167bool isValidEID(eid mctpEid)
168{
169 if (mctpEid == MCTP_ADDR_NULL || mctpEid < MCTP_START_VALID_EID ||
170 mctpEid == MCTP_ADDR_ANY)
171 {
172 return false;
173 }
174
175 return true;
176}
177
George Liu83409572019-12-24 18:42:54 +0800178uint8_t getNumPadBytes(uint32_t data)
179{
180 uint8_t pad;
181 pad = ((data % 4) ? (4 - data % 4) : 0);
182 return pad;
183} // end getNumPadBytes
184
185bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
186 uint8_t* hour, uint8_t* min, uint8_t* sec)
187{
188 constexpr uint64_t max_data = 29991231115959;
189 constexpr uint64_t min_data = 19700101000000;
190 if (data < min_data || data > max_data)
191 {
192 return false;
193 }
194
195 *year = data / 10000000000;
196 data = data % 10000000000;
197 *month = data / 100000000;
198 data = data % 100000000;
199 *day = data / 1000000;
200 data = data % 1000000;
201 *hour = data / 10000;
202 data = data % 10000;
203 *min = data / 100;
204 *sec = data % 100;
205
206 return true;
207}
208
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400209std::optional<std::vector<set_effecter_state_field>> parseEffecterData(
210 const std::vector<uint8_t>& effecterData, uint8_t effecterCount)
George Liu83409572019-12-24 18:42:54 +0800211{
George Liuba4c1fb2020-02-05 14:13:30 +0800212 std::vector<set_effecter_state_field> stateField;
213
214 if (effecterData.size() != effecterCount * 2)
George Liu83409572019-12-24 18:42:54 +0800215 {
George Liuba4c1fb2020-02-05 14:13:30 +0800216 return std::nullopt;
George Liu83409572019-12-24 18:42:54 +0800217 }
218
George Liuba4c1fb2020-02-05 14:13:30 +0800219 for (uint8_t i = 0; i < effecterCount; ++i)
George Liu83409572019-12-24 18:42:54 +0800220 {
George Liuba4c1fb2020-02-05 14:13:30 +0800221 uint8_t set_request = effecterData[i * 2] == PLDM_REQUEST_SET
222 ? PLDM_REQUEST_SET
223 : PLDM_NO_CHANGE;
224 set_effecter_state_field filed{set_request, effecterData[i * 2 + 1]};
225 stateField.emplace_back(std::move(filed));
George Liu83409572019-12-24 18:42:54 +0800226 }
227
George Liuba4c1fb2020-02-05 14:13:30 +0800228 return std::make_optional(std::move(stateField));
George Liu83409572019-12-24 18:42:54 +0800229}
230
George Liu0e02c322020-01-01 09:41:51 +0800231std::string DBusHandler::getService(const char* path,
232 const char* interface) const
George Liu83409572019-12-24 18:42:54 +0800233{
234 using DbusInterfaceList = std::vector<std::string>;
235 std::map<std::string, std::vector<std::string>> mapperResponse;
George Liu0e02c322020-01-01 09:41:51 +0800236 auto& bus = DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +0800237
Alexander Hansen70015ec2025-11-11 11:14:39 +0100238 auto mapper = bus.new_method_call(
239 ObjectMapper::default_service, ObjectMapper::instance_path,
240 ObjectMapper::interface, ObjectMapper::method_names::get_object);
George Liudf9a6d32020-12-22 16:27:16 +0800241
242 if (interface)
243 {
244 mapper.append(path, DbusInterfaceList({interface}));
245 }
246 else
247 {
248 mapper.append(path, DbusInterfaceList({}));
249 }
George Liu83409572019-12-24 18:42:54 +0800250
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500251 auto mapperResponseMsg = bus.call(mapper, dbusTimeout);
George Liu0e02c322020-01-01 09:41:51 +0800252 mapperResponseMsg.read(mapperResponse);
George Liu83409572019-12-24 18:42:54 +0800253 return mapperResponse.begin()->first;
254}
255
Patrick Williams366507c2025-02-03 14:28:01 -0500256GetSubTreeResponse DBusHandler::getSubtree(
257 const std::string& searchPath, int depth,
258 const std::vector<std::string>& ifaceList) const
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530259{
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530260 auto& bus = pldm::utils::DBusHandler::getBus();
Alexander Hansen70015ec2025-11-11 11:14:39 +0100261 auto method = bus.new_method_call(
262 ObjectMapper::default_service, ObjectMapper::instance_path,
263 ObjectMapper::interface, ObjectMapper::method_names::get_sub_tree);
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530264 method.append(searchPath, depth, ifaceList);
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500265 auto reply = bus.call(method, dbusTimeout);
Patrick Williams74bccf62025-11-05 00:20:51 -0500266 auto response = reply.unpack<GetSubTreeResponse>();
267
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530268 return response;
269}
270
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500271GetSubTreePathsResponse DBusHandler::getSubTreePaths(
272 const std::string& objectPath, int depth,
273 const std::vector<std::string>& ifaceList) const
274{
275 std::vector<std::string> paths;
276 auto& bus = pldm::utils::DBusHandler::getBus();
277 auto method = bus.new_method_call(
278 ObjectMapper::default_service, ObjectMapper::instance_path,
Alexander Hansen70015ec2025-11-11 11:14:39 +0100279 ObjectMapper::interface,
280 ObjectMapper::method_names::get_sub_tree_paths);
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500281 method.append(objectPath, depth, ifaceList);
282 auto reply = bus.call(method, dbusTimeout);
283
284 reply.read(paths);
285 return paths;
286}
287
Delphine CC Chiu549e4bc2024-03-06 11:24:05 +0800288GetAncestorsResponse DBusHandler::getAncestors(
289 const std::string& path, const std::vector<std::string>& ifaceList) const
290{
291 auto& bus = pldm::utils::DBusHandler::getBus();
Alexander Hansen70015ec2025-11-11 11:14:39 +0100292 auto method = bus.new_method_call(
293 ObjectMapper::default_service, ObjectMapper::instance_path,
294 ObjectMapper::interface, ObjectMapper::method_names::get_ancestors);
Delphine CC Chiu549e4bc2024-03-06 11:24:05 +0800295 method.append(path, ifaceList);
296 auto reply = bus.call(method, dbusTimeout);
Patrick Williams74bccf62025-11-05 00:20:51 -0500297 auto response = reply.unpack<GetAncestorsResponse>();
298
Delphine CC Chiu549e4bc2024-03-06 11:24:05 +0800299 return response;
300}
301
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530302void reportError(const char* errorMsg)
George Liu83409572019-12-24 18:42:54 +0800303{
George Liu0e02c322020-01-01 09:41:51 +0800304 auto& bus = pldm::utils::DBusHandler::getBus();
Riya Dixit76f2c602024-03-28 07:34:12 -0500305 using LoggingCreate =
306 sdbusplus::client::xyz::openbmc_project::logging::Create<>;
George Liu83409572019-12-24 18:42:54 +0800307 try
308 {
George Liu83409572019-12-24 18:42:54 +0800309 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530310 auto severity =
311 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
312 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
313 Error);
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600314 auto method = bus.new_method_call(LoggingCreate::default_service,
315 LoggingCreate::instance_path,
316 LoggingCreate::interface, "Create");
317
George Liu83409572019-12-24 18:42:54 +0800318 std::map<std::string, std::string> addlData{};
319 method.append(errorMsg, severity, addlData);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000320 bus.call_noreply(method, dbusTimeout);
George Liu83409572019-12-24 18:42:54 +0800321 }
322 catch (const std::exception& e)
323 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600324 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500325 "Failed to do dbus call for creating error log for '{ERRMSG}' at path '{PATH}' and interface '{INTERFACE}', error - {ERROR}",
326 "ERRMSG", errorMsg, "PATH", LoggingCreate::instance_path,
327 "INTERFACE", LoggingCreate::interface, "ERROR", e);
George Liu83409572019-12-24 18:42:54 +0800328 }
329}
330
George Liu1e44c732020-02-28 20:20:06 +0800331void DBusHandler::setDbusProperty(const DBusMapping& dBusMap,
332 const PropertyValue& value) const
333{
334 auto setDbusValue = [&dBusMap, this](const auto& variant) {
335 auto& bus = getBus();
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400336 auto service =
337 getService(dBusMap.objectPath.c_str(), dBusMap.interface.c_str());
George Liu1e44c732020-02-28 20:20:06 +0800338 auto method = bus.new_method_call(
339 service.c_str(), dBusMap.objectPath.c_str(), dbusProperties, "Set");
340 method.append(dBusMap.interface.c_str(), dBusMap.propertyName.c_str(),
341 variant);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000342 bus.call_noreply(method, dbusTimeout);
George Liu1e44c732020-02-28 20:20:06 +0800343 };
344
345 if (dBusMap.propertyType == "uint8_t")
346 {
347 std::variant<uint8_t> v = std::get<uint8_t>(value);
348 setDbusValue(v);
349 }
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600350 else if (dBusMap.propertyType == "bool")
351 {
352 std::variant<bool> v = std::get<bool>(value);
353 setDbusValue(v);
354 }
George Liu1e44c732020-02-28 20:20:06 +0800355 else if (dBusMap.propertyType == "int16_t")
356 {
357 std::variant<int16_t> v = std::get<int16_t>(value);
358 setDbusValue(v);
359 }
360 else if (dBusMap.propertyType == "uint16_t")
361 {
362 std::variant<uint16_t> v = std::get<uint16_t>(value);
363 setDbusValue(v);
364 }
365 else if (dBusMap.propertyType == "int32_t")
366 {
367 std::variant<int32_t> v = std::get<int32_t>(value);
368 setDbusValue(v);
369 }
370 else if (dBusMap.propertyType == "uint32_t")
371 {
372 std::variant<uint32_t> v = std::get<uint32_t>(value);
373 setDbusValue(v);
374 }
375 else if (dBusMap.propertyType == "int64_t")
376 {
377 std::variant<int64_t> v = std::get<int64_t>(value);
378 setDbusValue(v);
379 }
380 else if (dBusMap.propertyType == "uint64_t")
381 {
382 std::variant<uint64_t> v = std::get<uint64_t>(value);
383 setDbusValue(v);
384 }
385 else if (dBusMap.propertyType == "double")
386 {
387 std::variant<double> v = std::get<double>(value);
388 setDbusValue(v);
389 }
390 else if (dBusMap.propertyType == "string")
391 {
392 std::variant<std::string> v = std::get<std::string>(value);
393 setDbusValue(v);
394 }
Sora Sua8231fd2025-08-06 14:20:49 +0800395 else if (dBusMap.propertyType == "array[string]")
396 {
397 std::variant<std::vector<std::string>> v =
398 std::get<std::vector<std::string>>(value);
399 setDbusValue(v);
400 }
George Liu1e44c732020-02-28 20:20:06 +0800401 else
402 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500403 error("Unsupported property type '{TYPE}'", "TYPE",
404 dBusMap.propertyType);
405 throw std::invalid_argument("UnSupported Dbus Type");
George Liu1e44c732020-02-28 20:20:06 +0800406 }
407}
408
John Wang9e242422020-03-05 08:37:50 +0800409PropertyValue DBusHandler::getDbusPropertyVariant(
410 const char* objPath, const char* dbusProp, const char* dbusInterface) const
411{
412 auto& bus = DBusHandler::getBus();
413 auto service = getService(objPath, dbusInterface);
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400414 auto method =
415 bus.new_method_call(service.c_str(), objPath, dbusProperties, "Get");
John Wang9e242422020-03-05 08:37:50 +0800416 method.append(dbusInterface, dbusProp);
Patrick Williams75b8f462024-02-07 10:59:26 -0600417 return bus.call(method, dbusTimeout).unpack<PropertyValue>();
John Wang9e242422020-03-05 08:37:50 +0800418}
419
Unive Tienc40d4a62025-03-12 11:36:07 +0800420GetAssociatedSubTreeResponse DBusHandler::getAssociatedSubTree(
421 const sdbusplus::message::object_path& objectPath,
422 const sdbusplus::message::object_path& subtree, int depth,
423 const std::vector<std::string>& ifaceList) const
424{
425 auto& bus = DBusHandler::getBus();
426 auto method = bus.new_method_call(
427 ObjectMapper::default_service, ObjectMapper::instance_path,
Alexander Hansen70015ec2025-11-11 11:14:39 +0100428 ObjectMapper::interface,
429 ObjectMapper::method_names::get_associated_sub_tree);
Unive Tienc40d4a62025-03-12 11:36:07 +0800430 method.append(objectPath, subtree, depth, ifaceList);
431 auto reply = bus.call(method, dbusTimeout);
Patrick Williams74bccf62025-11-05 00:20:51 -0500432 auto response = reply.unpack<GetAssociatedSubTreeResponse>();
433
Unive Tienc40d4a62025-03-12 11:36:07 +0800434 return response;
435}
436
Riya Dixit754041d2024-02-20 06:15:49 -0600437ObjectValueTree DBusHandler::getManagedObj(const char* service,
438 const char* rootPath)
439{
440 auto& bus = DBusHandler::getBus();
441 auto method = bus.new_method_call(service, rootPath,
442 "org.freedesktop.DBus.ObjectManager",
443 "GetManagedObjects");
444 return bus.call(method).unpack<ObjectValueTree>();
445}
446
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400447PropertyMap DBusHandler::getDbusPropertiesVariant(
448 const char* serviceName, const char* objPath,
449 const char* dbusInterface) const
Gilbert Chen44524a52022-02-14 12:12:25 +0000450{
451 auto& bus = DBusHandler::getBus();
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400452 auto method =
453 bus.new_method_call(serviceName, objPath, dbusProperties, "GetAll");
Gilbert Chen44524a52022-02-14 12:12:25 +0000454 method.append(dbusInterface);
455 return bus.call(method, dbusTimeout).unpack<PropertyMap>();
456}
457
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530458PropertyValue jsonEntryToDbusVal(std::string_view type,
459 const nlohmann::json& value)
460{
461 PropertyValue propValue{};
462 if (type == "uint8_t")
463 {
464 propValue = static_cast<uint8_t>(value);
465 }
466 else if (type == "uint16_t")
467 {
468 propValue = static_cast<uint16_t>(value);
469 }
470 else if (type == "uint32_t")
471 {
472 propValue = static_cast<uint32_t>(value);
473 }
474 else if (type == "uint64_t")
475 {
476 propValue = static_cast<uint64_t>(value);
477 }
478 else if (type == "int16_t")
479 {
480 propValue = static_cast<int16_t>(value);
481 }
482 else if (type == "int32_t")
483 {
484 propValue = static_cast<int32_t>(value);
485 }
486 else if (type == "int64_t")
487 {
488 propValue = static_cast<int64_t>(value);
489 }
490 else if (type == "bool")
491 {
492 propValue = static_cast<bool>(value);
493 }
494 else if (type == "double")
495 {
496 propValue = static_cast<double>(value);
497 }
498 else if (type == "string")
499 {
500 propValue = static_cast<std::string>(value);
501 }
502 else
503 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500504 error("Unknown D-Bus property type '{TYPE}'", "TYPE", type);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530505 }
506
507 return propValue;
508}
509
Tom Joseph250c4752020-04-15 10:32:45 +0530510uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
511 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500512 uint16_t stateSetId, bool localOrRemote)
Tom Joseph250c4752020-04-15 10:32:45 +0530513{
514 uint8_t* pdrData = nullptr;
515 uint32_t pdrSize{};
516 const pldm_pdr_record* record{};
517 do
518 {
519 record = pldm_pdr_find_record_by_type(pdrRepo, PLDM_STATE_EFFECTER_PDR,
520 record, &pdrData, &pdrSize);
Sampa Misraa4a96162020-07-14 05:33:46 -0500521 if (record && (localOrRemote ^ pldm_pdr_record_is_remote(record)))
Tom Joseph250c4752020-04-15 10:32:45 +0530522 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530523 auto pdr = new (pdrData) pldm_state_effecter_pdr;
Tom Joseph250c4752020-04-15 10:32:45 +0530524 auto compositeEffecterCount = pdr->composite_effecter_count;
525 auto possible_states_start = pdr->possible_states;
526
527 for (auto effecters = 0x00; effecters < compositeEffecterCount;
528 effecters++)
529 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530530 auto possibleStates = new (possible_states_start)
531 state_effecter_possible_states;
Tom Joseph250c4752020-04-15 10:32:45 +0530532 auto setId = possibleStates->state_set_id;
533 auto possibleStateSize = possibleStates->possible_states_size;
534
535 if (entityType == pdr->entity_type &&
536 entityInstance == pdr->entity_instance &&
537 containerId == pdr->container_id && stateSetId == setId)
538 {
539 return pdr->effecter_id;
540 }
541 possible_states_start += possibleStateSize + sizeof(setId) +
542 sizeof(possibleStateSize);
543 }
544 }
545 } while (record);
546
547 return PLDM_INVALID_EFFECTER_ID;
548}
549
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800550int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
551 uint8_t sensorOffset, uint8_t eventState,
552 uint8_t previousEventState)
553{
554 try
555 {
556 auto& bus = DBusHandler::getBus();
557 auto msg = bus.new_signal("/xyz/openbmc_project/pldm",
558 "xyz.openbmc_project.PLDM.Event",
559 "StateSensorEvent");
560 msg.append(tid, sensorId, sensorOffset, eventState, previousEventState);
561
562 msg.signal_send();
563 }
Patrick Williams51330582021-10-06 12:48:56 -0500564 catch (const std::exception& e)
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800565 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500566 error("Failed to emit pldm event signal, error - {ERROR}", "ERROR", e);
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800567 return PLDM_ERROR;
568 }
569
570 return PLDM_SUCCESS;
571}
572
Chau Ly8fa40db2024-04-02 09:32:01 +0000573void recoverMctpEndpoint(const std::string& endpointObjPath)
574{
575 auto& bus = DBusHandler::getBus();
576 try
577 {
578 std::string service = DBusHandler().getService(endpointObjPath.c_str(),
579 MCTP_INTERFACE_CC);
580
581 auto method = bus.new_method_call(
582 service.c_str(), endpointObjPath.c_str(), MCTP_INTERFACE_CC,
583 MCTP_ENDPOINT_RECOVER_METHOD);
584 bus.call_noreply(method, dbusTimeout);
585 }
586 catch (const std::exception& e)
587 {
588 error(
589 "failed to make a D-Bus call to recover MCTP Endpoint, ERROR {ERR_EXCEP}",
590 "ERR_EXCEP", e);
591 }
592}
593
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500594uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
595 uint16_t entityType, uint16_t entityInstance,
596 uint16_t containerId, uint16_t stateSetId)
597{
598 auto pdrs = findStateSensorPDR(tid, entityType, stateSetId, pdrRepo);
599 for (auto pdr : pdrs)
600 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530601 auto sensorPdr = new (pdr.data()) pldm_state_sensor_pdr;
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500602 auto compositeSensorCount = sensorPdr->composite_sensor_count;
603 auto possible_states_start = sensorPdr->possible_states;
604
605 for (auto sensors = 0x00; sensors < compositeSensorCount; sensors++)
606 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530607 auto possibleStates = new (possible_states_start)
608 state_sensor_possible_states;
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500609 auto setId = possibleStates->state_set_id;
610 auto possibleStateSize = possibleStates->possible_states_size;
611 if (entityType == sensorPdr->entity_type &&
612 entityInstance == sensorPdr->entity_instance &&
613 stateSetId == setId && containerId == sensorPdr->container_id)
614 {
615 return sensorPdr->sensor_id;
616 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400617 possible_states_start +=
618 possibleStateSize + sizeof(setId) + sizeof(possibleStateSize);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500619 }
620 }
621 return PLDM_INVALID_EFFECTER_ID;
622}
623
Tom Josephe5268cd2021-09-07 13:04:03 +0530624void printBuffer(bool isTx, const std::vector<uint8_t>& buffer)
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600625{
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530626 if (buffer.empty())
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600627 {
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530628 return;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600629 }
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530630
631 std::cout << (isTx ? "Tx: " : "Rx: ");
632
633 std::ranges::for_each(buffer, [](uint8_t byte) {
634 std::cout << std::format("{:02x} ", byte);
635 });
636
637 std::cout << std::endl;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600638}
639
Tom Joseph54922072021-06-19 02:45:46 -0700640std::string toString(const struct variable_field& var)
641{
642 if (var.ptr == nullptr || !var.length)
643 {
644 return "";
645 }
646
647 std::string str(reinterpret_cast<const char*>(var.ptr), var.length);
648 std::replace_if(
649 str.begin(), str.end(), [](const char& c) { return !isprint(c); }, ' ');
650 return str;
651}
652
George Liu872f0f62021-11-25 16:26:16 +0800653std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
654 std::string_view trimStr)
655{
656 std::vector<std::string> out;
George Liua9eacff2025-08-25 11:20:52 +0800657 size_t start = 0;
George Liu872f0f62021-11-25 16:26:16 +0800658 size_t end = 0;
659
660 while ((start = srcStr.find_first_not_of(delim, end)) != std::string::npos)
661 {
662 end = srcStr.find(delim, start);
663 std::string_view dstStr = srcStr.substr(start, end - start);
664 if (!trimStr.empty())
665 {
666 dstStr.remove_prefix(dstStr.find_first_not_of(trimStr));
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400667 dstStr.remove_suffix(
668 dstStr.size() - 1 - dstStr.find_last_not_of(trimStr));
George Liu872f0f62021-11-25 16:26:16 +0800669 }
670
671 if (!dstStr.empty())
672 {
George Liua9eacff2025-08-25 11:20:52 +0800673 out.emplace_back(dstStr);
George Liu872f0f62021-11-25 16:26:16 +0800674 }
675 }
676
677 return out;
678}
679
Manojkiran Edaef773052021-07-29 09:29:28 +0530680std::string getCurrentSystemTime()
681{
Manojkiran Eda09a89822024-04-24 11:02:44 +0530682 const auto zonedTime{std::chrono::zoned_time{
683 std::chrono::current_zone(), std::chrono::system_clock::now()}};
684 return std::format("{:%F %Z %T}", zonedTime);
Manojkiran Edaef773052021-07-29 09:29:28 +0530685}
686
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500687bool checkForFruPresence(const std::string& objPath)
688{
689 bool isPresent = false;
690 static constexpr auto presentInterface =
691 "xyz.openbmc_project.Inventory.Item";
692 static constexpr auto presentProperty = "Present";
693 try
694 {
695 auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant(
696 objPath.c_str(), presentProperty, presentInterface);
697 isPresent = std::get<bool>(propVal);
698 }
699 catch (const sdbusplus::exception::SdBusError& e)
700 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500701 error("Failed to check for FRU presence at {PATH}, error - {ERROR}",
702 "PATH", objPath, "ERROR", e);
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500703 }
704 return isPresent;
705}
706
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600707bool checkIfLogicalBitSet(const uint16_t& containerId)
708{
709 return !(containerId & 0x8000);
710}
711
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500712void setFruPresence(const std::string& fruObjPath, bool present)
713{
714 pldm::utils::PropertyValue value{present};
715 pldm::utils::DBusMapping dbusMapping;
716 dbusMapping.objectPath = fruObjPath;
717 dbusMapping.interface = "xyz.openbmc_project.Inventory.Item";
718 dbusMapping.propertyName = "Present";
719 dbusMapping.propertyType = "bool";
720 try
721 {
722 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
723 }
724 catch (const std::exception& e)
725 {
726 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500727 "Failed to set the present property on path '{PATH}', error - {ERROR}.",
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500728 "PATH", fruObjPath, "ERROR", e);
729 }
730}
731
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000732std::string_view trimNameForDbus(std::string& name)
733{
734 std::replace(name.begin(), name.end(), ' ', '_');
735 auto nullTerminatorPos = name.find('\0');
736 if (nullTerminatorPos != std::string::npos)
737 {
738 name.erase(nullTerminatorPos);
739 }
740 return name;
741}
Thu Nguyena34a64b2022-03-31 08:56:39 +0700742
743bool dbusPropValuesToDouble(const std::string_view& type,
744 const pldm::utils::PropertyValue& value,
745 double* doubleValue)
746{
747 if (!dbusValueNumericTypeNames.contains(type))
748 {
749 return false;
750 }
751
752 if (!doubleValue)
753 {
754 return false;
755 }
756
757 try
758 {
759 if (type == "uint8_t")
760 {
761 *doubleValue = static_cast<double>(std::get<uint8_t>(value));
762 }
763 else if (type == "int16_t")
764 {
765 *doubleValue = static_cast<double>(std::get<int16_t>(value));
766 }
767 else if (type == "uint16_t")
768 {
769 *doubleValue = static_cast<double>(std::get<uint16_t>(value));
770 }
771 else if (type == "int32_t")
772 {
773 *doubleValue = static_cast<double>(std::get<int32_t>(value));
774 }
775 else if (type == "uint32_t")
776 {
777 *doubleValue = static_cast<double>(std::get<uint32_t>(value));
778 }
779 else if (type == "int64_t")
780 {
781 *doubleValue = static_cast<double>(std::get<int64_t>(value));
782 }
783 else if (type == "uint64_t")
784 {
785 *doubleValue = static_cast<double>(std::get<uint64_t>(value));
786 }
787 else if (type == "double")
788 {
789 *doubleValue = static_cast<double>(std::get<double>(value));
790 }
791 else
792 {
793 return false;
794 }
795 }
796 catch (const std::exception& e)
797 {
798 return false;
799 }
800
801 return true;
802}
Dung Caob6d39432024-06-05 03:46:47 +0000803
Patrick Williams366507c2025-02-03 14:28:01 -0500804std::optional<std::string> fruFieldValuestring(const uint8_t* value,
805 const uint8_t& length)
Dung Caob6d39432024-06-05 03:46:47 +0000806{
807 if (!value || !length)
808 {
Dung Caob6d39432024-06-05 03:46:47 +0000809 return std::nullopt;
810 }
811
812 return std::string(reinterpret_cast<const char*>(value), length);
813}
814
815std::optional<uint32_t> fruFieldParserU32(const uint8_t* value,
816 const uint8_t& length)
817{
818 if (!value || length != sizeof(uint32_t))
819 {
820 lg2::error("Fru data to u32 invalid data.");
821 return std::nullopt;
822 }
823
824 uint32_t ret;
825 std::memcpy(&ret, value, length);
826 return ret;
827}
828
Pavithra Barithaya36ac5592025-06-16 11:41:37 +0530829SensorPDRs getStateSensorPDRsByType(uint16_t entityType, const pldm_pdr* repo)
830{
831 uint8_t* outData = nullptr;
832 uint32_t size{};
833 const pldm_pdr_record* record = nullptr;
834 SensorPDRs pdrs;
835
836 if (repo)
837 {
838 while ((record = pldm_pdr_find_record_by_type(
839 repo, PLDM_STATE_SENSOR_PDR, record, &outData, &size)))
840 {
841 auto pdr = new (outData) pldm_state_sensor_pdr;
842 if (pdr && pdr->entity_type == entityType)
843 {
844 pdrs.emplace_back(outData, outData + size);
845 }
846 }
847 }
848
849 return pdrs;
850}
851
852std::vector<pldm::pdr::SensorID> findSensorIds(
853 const pldm_pdr* pdrRepo, uint16_t entityType, uint16_t entityInstance,
854 uint16_t containerId)
855{
856 std::vector<uint16_t> sensorIDs;
857 auto pdrs = getStateSensorPDRsByType(entityType, pdrRepo);
858
859 for (const auto& pdr : pdrs)
860 {
861 auto sensorPdr =
862 reinterpret_cast<const pldm_state_sensor_pdr*>(pdr.data());
863
864 if (sensorPdr && sensorPdr->entity_type == entityType &&
865 sensorPdr->entity_instance == entityInstance &&
866 sensorPdr->container_id == containerId)
867 {
868 sensorIDs.emplace_back(sensorPdr->sensor_id);
869 }
870 }
871
872 return sensorIDs;
873}
874
Pavithra Barithayaa206b602025-08-21 14:37:13 +0530875EffecterPDRs getStateEffecterPDRsByType(uint16_t entityType,
876 const pldm_pdr* repo)
877{
878 uint8_t* outData = nullptr;
879 uint32_t size{};
880 const pldm_pdr_record* record = nullptr;
881 EffecterPDRs pdrs;
882 if (repo)
883 {
884 while ((record = pldm_pdr_find_record_by_type(
885 repo, PLDM_STATE_EFFECTER_PDR, record, &outData, &size)))
886 {
887 auto pdr = new (outData) pldm_state_effecter_pdr;
888 if (pdr && pdr->entity_type == entityType)
889 {
890 pdrs.emplace_back(outData, outData + size);
891 }
892 }
893 }
894 return pdrs;
895}
896
897std::vector<pldm::pdr::EffecterID> findEffecterIds(
898 const pldm_pdr* pdrRepo, uint16_t entityType, uint16_t entityInstance,
899 uint16_t containerId)
900{
901 std::vector<uint16_t> effecterIDs;
902 auto pdrs = getStateEffecterPDRsByType(entityType, pdrRepo);
903 for (const auto& pdr : pdrs)
904 {
905 auto effecterPdr =
906 reinterpret_cast<const pldm_state_effecter_pdr*>(pdr.data());
907 if (effecterPdr && effecterPdr->entity_type == entityType &&
908 effecterPdr->entity_instance == entityInstance &&
909 effecterPdr->container_id == containerId)
910 {
911 effecterIDs.emplace_back(effecterPdr->effecter_id);
912 }
913 }
914 return effecterIDs;
915}
916
Archana Kakanie26d13f2025-03-09 23:17:46 -0500917void setBiosAttr(const PendingAttributesList& biosAttrList)
918{
919 static constexpr auto SYSTEMD_PROPERTY_INTERFACE =
920 "org.freedesktop.DBus.Properties";
Archana Kakanie26d13f2025-03-09 23:17:46 -0500921
922 for (const auto& [attrName, biosAttrDetails] : biosAttrList)
923 {
924 auto& bus = DBusHandler::getBus();
925 try
926 {
927 auto service = pldm::utils::DBusHandler().getService(
Alexander Hansen364275c2025-11-11 12:31:42 +0100928 biosConfigPath, BIOSConfigManager::interface);
Archana Kakanie26d13f2025-03-09 23:17:46 -0500929 auto method =
930 bus.new_method_call(service.c_str(), biosConfigPath,
931 SYSTEMD_PROPERTY_INTERFACE, "Set");
Alexander Hansen364275c2025-11-11 12:31:42 +0100932 method.append(BIOSConfigManager::interface,
933 BIOSConfigManager::property_names::pending_attributes,
Archana Kakanie26d13f2025-03-09 23:17:46 -0500934 std::variant<PendingAttributesList>(biosAttrList));
935 bus.call_noreply(method, dbusTimeout);
936 }
937 catch (const sdbusplus::exception::SdBusError& e)
938 {
939 AttributeType attrType;
940 AttributeValue attrValue;
941 std::tie(attrType, attrValue) = biosAttrDetails;
942 if (attrType ==
943 "xyz.openbmc_project.BIOSConfig.Manager.AttributeType.Integer")
944 {
945 info(
946 "Error setting the value {VALUE} to bios attribute {BIOS_ATTR}: {ERR_EXCEP}",
947 "VALUE", std::get<int64_t>(attrValue), "BIOS_ATTR",
948 attrName, "ERR_EXCEP", e);
949 }
950 else
951 {
952 info(
953 "Error setting the value {VALUE} to bios attribute {BIOS_ATTR}: {ERR_EXCEP}",
954 "VALUE", std::get<std::string>(attrValue), "BIOS_ATTR",
955 attrName, "ERR_EXCEP", e);
956 }
957 }
958 }
959}
Unive Tien7ad45b42025-08-18 06:04:53 +0000960
961long int generateSwId()
962{
963 return random() % 10000;
964}
965
George Liu83409572019-12-24 18:42:54 +0800966} // namespace utils
967} // namespace pldm