blob: 1e4110047437484ce28a9ac680ffb322e2740e80 [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
7#include <xyz/openbmc_project/Common/error.hpp>
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -06008#include <xyz/openbmc_project/Logging/Create/client.hpp>
9#include <xyz/openbmc_project/ObjectMapper/client.hpp>
George Liu6492f522020-06-16 10:34:05 +080010
Tom Joseph54922072021-06-19 02:45:46 -070011#include <algorithm>
George Liu83409572019-12-24 18:42:54 +080012#include <array>
Tom Joseph54922072021-06-19 02:45:46 -070013#include <cctype>
George Liu83409572019-12-24 18:42:54 +080014#include <ctime>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050015#include <fstream>
George Liu83409572019-12-24 18:42:54 +080016#include <iostream>
17#include <map>
George Liu83409572019-12-24 18:42:54 +080018#include <stdexcept>
19#include <string>
20#include <vector>
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050021
Riya Dixit49cfb132023-03-02 04:26:53 -060022PHOSPHOR_LOG2_USING;
23
George Liu83409572019-12-24 18:42:54 +080024namespace pldm
25{
26namespace utils
27{
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -060028
Chau Ly8fa40db2024-04-02 09:32:01 +000029using ObjectMapper = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>;
30
31constexpr const char* MCTP_INTERFACE_CC = "au.com.codeconstruct.MCTP.Endpoint1";
32constexpr const char* MCTP_ENDPOINT_RECOVER_METHOD = "Recover";
33
Patrick Williams366507c2025-02-03 14:28:01 -050034std::vector<std::vector<uint8_t>> findStateEffecterPDR(
35 uint8_t /*tid*/, uint16_t entityID, uint16_t stateSetId,
36 const pldm_pdr* repo)
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050037{
38 uint8_t* outData = nullptr;
39 uint32_t size{};
40 const pldm_pdr_record* record{};
41 std::vector<std::vector<uint8_t>> pdrs;
42 try
43 {
44 do
45 {
46 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_EFFECTER_PDR,
47 record, &outData, &size);
48 if (record)
49 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +053050 auto pdr = new (outData) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050051 auto compositeEffecterCount = pdr->composite_effecter_count;
Chicago Duana7aacc32020-06-10 18:03:38 +080052 auto possible_states_start = pdr->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050053
54 for (auto effecters = 0x00; effecters < compositeEffecterCount;
55 effecters++)
56 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +053057 auto possibleStates = new (possible_states_start)
58 state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050059 auto setId = possibleStates->state_set_id;
60 auto possibleStateSize =
61 possibleStates->possible_states_size;
62
63 if (pdr->entity_type == entityID && setId == stateSetId)
64 {
65 std::vector<uint8_t> effecter_pdr(&outData[0],
66 &outData[size]);
67 pdrs.emplace_back(std::move(effecter_pdr));
68 break;
69 }
Chicago Duana7aacc32020-06-10 18:03:38 +080070 possible_states_start += possibleStateSize + sizeof(setId) +
71 sizeof(possibleStateSize);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050072 }
73 }
74
75 } while (record);
76 }
77 catch (const std::exception& e)
78 {
Riya Dixit76f2c602024-03-28 07:34:12 -050079 error("Failed to obtain a record, error - {ERROR}", "ERROR", e);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050080 }
81
82 return pdrs;
83}
84
Patrick Williams366507c2025-02-03 14:28:01 -050085std::vector<std::vector<uint8_t>> findStateSensorPDR(
86 uint8_t /*tid*/, uint16_t entityID, uint16_t stateSetId,
87 const pldm_pdr* repo)
Chicago Duan738e4d82020-05-28 16:39:19 +080088{
89 uint8_t* outData = nullptr;
90 uint32_t size{};
91 const pldm_pdr_record* record{};
92 std::vector<std::vector<uint8_t>> pdrs;
93 try
94 {
95 do
96 {
97 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_SENSOR_PDR,
98 record, &outData, &size);
99 if (record)
100 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530101 auto pdr = new (outData) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800102 auto compositeSensorCount = pdr->composite_sensor_count;
Chicago Duana7aacc32020-06-10 18:03:38 +0800103 auto possible_states_start = pdr->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800104
105 for (auto sensors = 0x00; sensors < compositeSensorCount;
106 sensors++)
107 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530108 auto possibleStates = new (possible_states_start)
109 state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800110 auto setId = possibleStates->state_set_id;
111 auto possibleStateSize =
112 possibleStates->possible_states_size;
113
114 if (pdr->entity_type == entityID && setId == stateSetId)
115 {
116 std::vector<uint8_t> sensor_pdr(&outData[0],
117 &outData[size]);
118 pdrs.emplace_back(std::move(sensor_pdr));
119 break;
120 }
Chicago Duana7aacc32020-06-10 18:03:38 +0800121 possible_states_start += possibleStateSize + sizeof(setId) +
122 sizeof(possibleStateSize);
Chicago Duan738e4d82020-05-28 16:39:19 +0800123 }
124 }
125
126 } while (record);
127 }
128 catch (const std::exception& e)
129 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500130 error(
131 "Failed to obtain a record with entity ID '{ENTITYID}', error - {ERROR}",
132 "ENTITYID", entityID, "ERROR", e);
Chicago Duan738e4d82020-05-28 16:39:19 +0800133 }
134
135 return pdrs;
136}
137
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500138uint8_t readHostEID()
139{
140 uint8_t eid{};
Brad Bishop06052cc2021-08-16 15:17:16 -0400141 std::ifstream eidFile{HOST_EID_PATH};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500142 if (!eidFile.good())
143 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500144 error("Failed to open remote terminus EID file at path '{PATH}'",
145 "PATH", static_cast<std::string>(HOST_EID_PATH));
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500146 }
147 else
148 {
149 std::string eidStr;
150 eidFile >> eidStr;
151 if (!eidStr.empty())
152 {
153 eid = atoi(eidStr.c_str());
154 }
155 else
156 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500157 error("Remote terminus EID file was empty");
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500158 }
159 }
160
161 return eid;
162}
George Liu83409572019-12-24 18:42:54 +0800163
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000164bool isValidEID(eid mctpEid)
165{
166 if (mctpEid == MCTP_ADDR_NULL || mctpEid < MCTP_START_VALID_EID ||
167 mctpEid == MCTP_ADDR_ANY)
168 {
169 return false;
170 }
171
172 return true;
173}
174
George Liu83409572019-12-24 18:42:54 +0800175uint8_t getNumPadBytes(uint32_t data)
176{
177 uint8_t pad;
178 pad = ((data % 4) ? (4 - data % 4) : 0);
179 return pad;
180} // end getNumPadBytes
181
182bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
183 uint8_t* hour, uint8_t* min, uint8_t* sec)
184{
185 constexpr uint64_t max_data = 29991231115959;
186 constexpr uint64_t min_data = 19700101000000;
187 if (data < min_data || data > max_data)
188 {
189 return false;
190 }
191
192 *year = data / 10000000000;
193 data = data % 10000000000;
194 *month = data / 100000000;
195 data = data % 100000000;
196 *day = data / 1000000;
197 data = data % 1000000;
198 *hour = data / 10000;
199 data = data % 10000;
200 *min = data / 100;
201 *sec = data % 100;
202
203 return true;
204}
205
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400206std::optional<std::vector<set_effecter_state_field>> parseEffecterData(
207 const std::vector<uint8_t>& effecterData, uint8_t effecterCount)
George Liu83409572019-12-24 18:42:54 +0800208{
George Liuba4c1fb2020-02-05 14:13:30 +0800209 std::vector<set_effecter_state_field> stateField;
210
211 if (effecterData.size() != effecterCount * 2)
George Liu83409572019-12-24 18:42:54 +0800212 {
George Liuba4c1fb2020-02-05 14:13:30 +0800213 return std::nullopt;
George Liu83409572019-12-24 18:42:54 +0800214 }
215
George Liuba4c1fb2020-02-05 14:13:30 +0800216 for (uint8_t i = 0; i < effecterCount; ++i)
George Liu83409572019-12-24 18:42:54 +0800217 {
George Liuba4c1fb2020-02-05 14:13:30 +0800218 uint8_t set_request = effecterData[i * 2] == PLDM_REQUEST_SET
219 ? PLDM_REQUEST_SET
220 : PLDM_NO_CHANGE;
221 set_effecter_state_field filed{set_request, effecterData[i * 2 + 1]};
222 stateField.emplace_back(std::move(filed));
George Liu83409572019-12-24 18:42:54 +0800223 }
224
George Liuba4c1fb2020-02-05 14:13:30 +0800225 return std::make_optional(std::move(stateField));
George Liu83409572019-12-24 18:42:54 +0800226}
227
George Liu0e02c322020-01-01 09:41:51 +0800228std::string DBusHandler::getService(const char* path,
229 const char* interface) const
George Liu83409572019-12-24 18:42:54 +0800230{
231 using DbusInterfaceList = std::vector<std::string>;
232 std::map<std::string, std::vector<std::string>> mapperResponse;
George Liu0e02c322020-01-01 09:41:51 +0800233 auto& bus = DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +0800234
Alexander Hansen70015ec2025-11-11 11:14:39 +0100235 auto mapper = bus.new_method_call(
236 ObjectMapper::default_service, ObjectMapper::instance_path,
237 ObjectMapper::interface, ObjectMapper::method_names::get_object);
George Liudf9a6d32020-12-22 16:27:16 +0800238
239 if (interface)
240 {
241 mapper.append(path, DbusInterfaceList({interface}));
242 }
243 else
244 {
245 mapper.append(path, DbusInterfaceList({}));
246 }
George Liu83409572019-12-24 18:42:54 +0800247
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500248 auto mapperResponseMsg = bus.call(mapper, dbusTimeout);
George Liu0e02c322020-01-01 09:41:51 +0800249 mapperResponseMsg.read(mapperResponse);
George Liu83409572019-12-24 18:42:54 +0800250 return mapperResponse.begin()->first;
251}
252
Patrick Williams366507c2025-02-03 14:28:01 -0500253GetSubTreeResponse DBusHandler::getSubtree(
254 const std::string& searchPath, int depth,
255 const std::vector<std::string>& ifaceList) const
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530256{
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530257 auto& bus = pldm::utils::DBusHandler::getBus();
Alexander Hansen70015ec2025-11-11 11:14:39 +0100258 auto method = bus.new_method_call(
259 ObjectMapper::default_service, ObjectMapper::instance_path,
260 ObjectMapper::interface, ObjectMapper::method_names::get_sub_tree);
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530261 method.append(searchPath, depth, ifaceList);
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500262 auto reply = bus.call(method, dbusTimeout);
Patrick Williams74bccf62025-11-05 00:20:51 -0500263 auto response = reply.unpack<GetSubTreeResponse>();
264
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530265 return response;
266}
267
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500268GetSubTreePathsResponse DBusHandler::getSubTreePaths(
269 const std::string& objectPath, int depth,
270 const std::vector<std::string>& ifaceList) const
271{
272 std::vector<std::string> paths;
273 auto& bus = pldm::utils::DBusHandler::getBus();
274 auto method = bus.new_method_call(
275 ObjectMapper::default_service, ObjectMapper::instance_path,
Alexander Hansen70015ec2025-11-11 11:14:39 +0100276 ObjectMapper::interface,
277 ObjectMapper::method_names::get_sub_tree_paths);
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500278 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();
Alexander Hansen70015ec2025-11-11 11:14:39 +0100289 auto method = bus.new_method_call(
290 ObjectMapper::default_service, ObjectMapper::instance_path,
291 ObjectMapper::interface, ObjectMapper::method_names::get_ancestors);
Delphine CC Chiu549e4bc2024-03-06 11:24:05 +0800292 method.append(path, ifaceList);
293 auto reply = bus.call(method, dbusTimeout);
Patrick Williams74bccf62025-11-05 00:20:51 -0500294 auto response = reply.unpack<GetAncestorsResponse>();
295
Delphine CC Chiu549e4bc2024-03-06 11:24:05 +0800296 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,
Alexander Hansen70015ec2025-11-11 11:14:39 +0100425 ObjectMapper::interface,
426 ObjectMapper::method_names::get_associated_sub_tree);
Unive Tienc40d4a62025-03-12 11:36:07 +0800427 method.append(objectPath, subtree, depth, ifaceList);
428 auto reply = bus.call(method, dbusTimeout);
Patrick Williams74bccf62025-11-05 00:20:51 -0500429 auto response = reply.unpack<GetAssociatedSubTreeResponse>();
430
Unive Tienc40d4a62025-03-12 11:36:07 +0800431 return response;
432}
433
Riya Dixit754041d2024-02-20 06:15:49 -0600434ObjectValueTree DBusHandler::getManagedObj(const char* service,
435 const char* rootPath)
436{
437 auto& bus = DBusHandler::getBus();
438 auto method = bus.new_method_call(service, rootPath,
439 "org.freedesktop.DBus.ObjectManager",
440 "GetManagedObjects");
441 return bus.call(method).unpack<ObjectValueTree>();
442}
443
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400444PropertyMap DBusHandler::getDbusPropertiesVariant(
445 const char* serviceName, const char* objPath,
446 const char* dbusInterface) const
Gilbert Chen44524a52022-02-14 12:12:25 +0000447{
448 auto& bus = DBusHandler::getBus();
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400449 auto method =
450 bus.new_method_call(serviceName, objPath, dbusProperties, "GetAll");
Gilbert Chen44524a52022-02-14 12:12:25 +0000451 method.append(dbusInterface);
452 return bus.call(method, dbusTimeout).unpack<PropertyMap>();
453}
454
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530455PropertyValue jsonEntryToDbusVal(std::string_view type,
456 const nlohmann::json& value)
457{
458 PropertyValue propValue{};
459 if (type == "uint8_t")
460 {
461 propValue = static_cast<uint8_t>(value);
462 }
463 else if (type == "uint16_t")
464 {
465 propValue = static_cast<uint16_t>(value);
466 }
467 else if (type == "uint32_t")
468 {
469 propValue = static_cast<uint32_t>(value);
470 }
471 else if (type == "uint64_t")
472 {
473 propValue = static_cast<uint64_t>(value);
474 }
475 else if (type == "int16_t")
476 {
477 propValue = static_cast<int16_t>(value);
478 }
479 else if (type == "int32_t")
480 {
481 propValue = static_cast<int32_t>(value);
482 }
483 else if (type == "int64_t")
484 {
485 propValue = static_cast<int64_t>(value);
486 }
487 else if (type == "bool")
488 {
489 propValue = static_cast<bool>(value);
490 }
491 else if (type == "double")
492 {
493 propValue = static_cast<double>(value);
494 }
495 else if (type == "string")
496 {
497 propValue = static_cast<std::string>(value);
498 }
499 else
500 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500501 error("Unknown D-Bus property type '{TYPE}'", "TYPE", type);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530502 }
503
504 return propValue;
505}
506
Tom Joseph250c4752020-04-15 10:32:45 +0530507uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
508 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500509 uint16_t stateSetId, bool localOrRemote)
Tom Joseph250c4752020-04-15 10:32:45 +0530510{
511 uint8_t* pdrData = nullptr;
512 uint32_t pdrSize{};
513 const pldm_pdr_record* record{};
514 do
515 {
516 record = pldm_pdr_find_record_by_type(pdrRepo, PLDM_STATE_EFFECTER_PDR,
517 record, &pdrData, &pdrSize);
Sampa Misraa4a96162020-07-14 05:33:46 -0500518 if (record && (localOrRemote ^ pldm_pdr_record_is_remote(record)))
Tom Joseph250c4752020-04-15 10:32:45 +0530519 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530520 auto pdr = new (pdrData) pldm_state_effecter_pdr;
Tom Joseph250c4752020-04-15 10:32:45 +0530521 auto compositeEffecterCount = pdr->composite_effecter_count;
522 auto possible_states_start = pdr->possible_states;
523
524 for (auto effecters = 0x00; effecters < compositeEffecterCount;
525 effecters++)
526 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530527 auto possibleStates = new (possible_states_start)
528 state_effecter_possible_states;
Tom Joseph250c4752020-04-15 10:32:45 +0530529 auto setId = possibleStates->state_set_id;
530 auto possibleStateSize = possibleStates->possible_states_size;
531
532 if (entityType == pdr->entity_type &&
533 entityInstance == pdr->entity_instance &&
534 containerId == pdr->container_id && stateSetId == setId)
535 {
536 return pdr->effecter_id;
537 }
538 possible_states_start += possibleStateSize + sizeof(setId) +
539 sizeof(possibleStateSize);
540 }
541 }
542 } while (record);
543
544 return PLDM_INVALID_EFFECTER_ID;
545}
546
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800547int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
548 uint8_t sensorOffset, uint8_t eventState,
549 uint8_t previousEventState)
550{
551 try
552 {
553 auto& bus = DBusHandler::getBus();
554 auto msg = bus.new_signal("/xyz/openbmc_project/pldm",
555 "xyz.openbmc_project.PLDM.Event",
556 "StateSensorEvent");
557 msg.append(tid, sensorId, sensorOffset, eventState, previousEventState);
558
559 msg.signal_send();
560 }
Patrick Williams51330582021-10-06 12:48:56 -0500561 catch (const std::exception& e)
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800562 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500563 error("Failed to emit pldm event signal, error - {ERROR}", "ERROR", e);
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800564 return PLDM_ERROR;
565 }
566
567 return PLDM_SUCCESS;
568}
569
Chau Ly8fa40db2024-04-02 09:32:01 +0000570void recoverMctpEndpoint(const std::string& endpointObjPath)
571{
572 auto& bus = DBusHandler::getBus();
573 try
574 {
575 std::string service = DBusHandler().getService(endpointObjPath.c_str(),
576 MCTP_INTERFACE_CC);
577
578 auto method = bus.new_method_call(
579 service.c_str(), endpointObjPath.c_str(), MCTP_INTERFACE_CC,
580 MCTP_ENDPOINT_RECOVER_METHOD);
581 bus.call_noreply(method, dbusTimeout);
582 }
583 catch (const std::exception& e)
584 {
585 error(
586 "failed to make a D-Bus call to recover MCTP Endpoint, ERROR {ERR_EXCEP}",
587 "ERR_EXCEP", e);
588 }
589}
590
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500591uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
592 uint16_t entityType, uint16_t entityInstance,
593 uint16_t containerId, uint16_t stateSetId)
594{
595 auto pdrs = findStateSensorPDR(tid, entityType, stateSetId, pdrRepo);
596 for (auto pdr : pdrs)
597 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530598 auto sensorPdr = new (pdr.data()) pldm_state_sensor_pdr;
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500599 auto compositeSensorCount = sensorPdr->composite_sensor_count;
600 auto possible_states_start = sensorPdr->possible_states;
601
602 for (auto sensors = 0x00; sensors < compositeSensorCount; sensors++)
603 {
Pavithra Barithaya39d13b72025-01-31 10:39:02 +0530604 auto possibleStates = new (possible_states_start)
605 state_sensor_possible_states;
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500606 auto setId = possibleStates->state_set_id;
607 auto possibleStateSize = possibleStates->possible_states_size;
608 if (entityType == sensorPdr->entity_type &&
609 entityInstance == sensorPdr->entity_instance &&
610 stateSetId == setId && containerId == sensorPdr->container_id)
611 {
612 return sensorPdr->sensor_id;
613 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400614 possible_states_start +=
615 possibleStateSize + sizeof(setId) + sizeof(possibleStateSize);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500616 }
617 }
618 return PLDM_INVALID_EFFECTER_ID;
619}
620
Tom Josephe5268cd2021-09-07 13:04:03 +0530621void printBuffer(bool isTx, const std::vector<uint8_t>& buffer)
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600622{
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530623 if (buffer.empty())
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600624 {
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530625 return;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600626 }
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530627
628 std::cout << (isTx ? "Tx: " : "Rx: ");
629
630 std::ranges::for_each(buffer, [](uint8_t byte) {
631 std::cout << std::format("{:02x} ", byte);
632 });
633
634 std::cout << std::endl;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600635}
636
Tom Joseph54922072021-06-19 02:45:46 -0700637std::string toString(const struct variable_field& var)
638{
639 if (var.ptr == nullptr || !var.length)
640 {
641 return "";
642 }
643
644 std::string str(reinterpret_cast<const char*>(var.ptr), var.length);
645 std::replace_if(
646 str.begin(), str.end(), [](const char& c) { return !isprint(c); }, ' ');
647 return str;
648}
649
George Liu872f0f62021-11-25 16:26:16 +0800650std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
651 std::string_view trimStr)
652{
653 std::vector<std::string> out;
George Liua9eacff2025-08-25 11:20:52 +0800654 size_t start = 0;
George Liu872f0f62021-11-25 16:26:16 +0800655 size_t end = 0;
656
657 while ((start = srcStr.find_first_not_of(delim, end)) != std::string::npos)
658 {
659 end = srcStr.find(delim, start);
660 std::string_view dstStr = srcStr.substr(start, end - start);
661 if (!trimStr.empty())
662 {
663 dstStr.remove_prefix(dstStr.find_first_not_of(trimStr));
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400664 dstStr.remove_suffix(
665 dstStr.size() - 1 - dstStr.find_last_not_of(trimStr));
George Liu872f0f62021-11-25 16:26:16 +0800666 }
667
668 if (!dstStr.empty())
669 {
George Liua9eacff2025-08-25 11:20:52 +0800670 out.emplace_back(dstStr);
George Liu872f0f62021-11-25 16:26:16 +0800671 }
672 }
673
674 return out;
675}
676
Manojkiran Edaef773052021-07-29 09:29:28 +0530677std::string getCurrentSystemTime()
678{
Manojkiran Eda09a89822024-04-24 11:02:44 +0530679 const auto zonedTime{std::chrono::zoned_time{
680 std::chrono::current_zone(), std::chrono::system_clock::now()}};
681 return std::format("{:%F %Z %T}", zonedTime);
Manojkiran Edaef773052021-07-29 09:29:28 +0530682}
683
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500684bool checkForFruPresence(const std::string& objPath)
685{
686 bool isPresent = false;
687 static constexpr auto presentInterface =
688 "xyz.openbmc_project.Inventory.Item";
689 static constexpr auto presentProperty = "Present";
690 try
691 {
692 auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant(
693 objPath.c_str(), presentProperty, presentInterface);
694 isPresent = std::get<bool>(propVal);
695 }
696 catch (const sdbusplus::exception::SdBusError& e)
697 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500698 error("Failed to check for FRU presence at {PATH}, error - {ERROR}",
699 "PATH", objPath, "ERROR", e);
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500700 }
701 return isPresent;
702}
703
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600704bool checkIfLogicalBitSet(const uint16_t& containerId)
705{
706 return !(containerId & 0x8000);
707}
708
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500709void setFruPresence(const std::string& fruObjPath, bool present)
710{
711 pldm::utils::PropertyValue value{present};
712 pldm::utils::DBusMapping dbusMapping;
713 dbusMapping.objectPath = fruObjPath;
714 dbusMapping.interface = "xyz.openbmc_project.Inventory.Item";
715 dbusMapping.propertyName = "Present";
716 dbusMapping.propertyType = "bool";
717 try
718 {
719 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
720 }
721 catch (const std::exception& e)
722 {
723 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500724 "Failed to set the present property on path '{PATH}', error - {ERROR}.",
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500725 "PATH", fruObjPath, "ERROR", e);
726 }
727}
728
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000729std::string_view trimNameForDbus(std::string& name)
730{
731 std::replace(name.begin(), name.end(), ' ', '_');
732 auto nullTerminatorPos = name.find('\0');
733 if (nullTerminatorPos != std::string::npos)
734 {
735 name.erase(nullTerminatorPos);
736 }
737 return name;
738}
Thu Nguyena34a64b2022-03-31 08:56:39 +0700739
740bool dbusPropValuesToDouble(const std::string_view& type,
741 const pldm::utils::PropertyValue& value,
742 double* doubleValue)
743{
744 if (!dbusValueNumericTypeNames.contains(type))
745 {
746 return false;
747 }
748
749 if (!doubleValue)
750 {
751 return false;
752 }
753
754 try
755 {
756 if (type == "uint8_t")
757 {
758 *doubleValue = static_cast<double>(std::get<uint8_t>(value));
759 }
760 else if (type == "int16_t")
761 {
762 *doubleValue = static_cast<double>(std::get<int16_t>(value));
763 }
764 else if (type == "uint16_t")
765 {
766 *doubleValue = static_cast<double>(std::get<uint16_t>(value));
767 }
768 else if (type == "int32_t")
769 {
770 *doubleValue = static_cast<double>(std::get<int32_t>(value));
771 }
772 else if (type == "uint32_t")
773 {
774 *doubleValue = static_cast<double>(std::get<uint32_t>(value));
775 }
776 else if (type == "int64_t")
777 {
778 *doubleValue = static_cast<double>(std::get<int64_t>(value));
779 }
780 else if (type == "uint64_t")
781 {
782 *doubleValue = static_cast<double>(std::get<uint64_t>(value));
783 }
784 else if (type == "double")
785 {
786 *doubleValue = static_cast<double>(std::get<double>(value));
787 }
788 else
789 {
790 return false;
791 }
792 }
793 catch (const std::exception& e)
794 {
795 return false;
796 }
797
798 return true;
799}
Dung Caob6d39432024-06-05 03:46:47 +0000800
Patrick Williams366507c2025-02-03 14:28:01 -0500801std::optional<std::string> fruFieldValuestring(const uint8_t* value,
802 const uint8_t& length)
Dung Caob6d39432024-06-05 03:46:47 +0000803{
804 if (!value || !length)
805 {
Dung Caob6d39432024-06-05 03:46:47 +0000806 return std::nullopt;
807 }
808
809 return std::string(reinterpret_cast<const char*>(value), length);
810}
811
812std::optional<uint32_t> fruFieldParserU32(const uint8_t* value,
813 const uint8_t& length)
814{
815 if (!value || length != sizeof(uint32_t))
816 {
817 lg2::error("Fru data to u32 invalid data.");
818 return std::nullopt;
819 }
820
821 uint32_t ret;
822 std::memcpy(&ret, value, length);
823 return ret;
824}
825
Pavithra Barithaya36ac5592025-06-16 11:41:37 +0530826SensorPDRs getStateSensorPDRsByType(uint16_t entityType, const pldm_pdr* repo)
827{
828 uint8_t* outData = nullptr;
829 uint32_t size{};
830 const pldm_pdr_record* record = nullptr;
831 SensorPDRs pdrs;
832
833 if (repo)
834 {
835 while ((record = pldm_pdr_find_record_by_type(
836 repo, PLDM_STATE_SENSOR_PDR, record, &outData, &size)))
837 {
838 auto pdr = new (outData) pldm_state_sensor_pdr;
839 if (pdr && pdr->entity_type == entityType)
840 {
841 pdrs.emplace_back(outData, outData + size);
842 }
843 }
844 }
845
846 return pdrs;
847}
848
849std::vector<pldm::pdr::SensorID> findSensorIds(
850 const pldm_pdr* pdrRepo, uint16_t entityType, uint16_t entityInstance,
851 uint16_t containerId)
852{
853 std::vector<uint16_t> sensorIDs;
854 auto pdrs = getStateSensorPDRsByType(entityType, pdrRepo);
855
856 for (const auto& pdr : pdrs)
857 {
858 auto sensorPdr =
859 reinterpret_cast<const pldm_state_sensor_pdr*>(pdr.data());
860
861 if (sensorPdr && sensorPdr->entity_type == entityType &&
862 sensorPdr->entity_instance == entityInstance &&
863 sensorPdr->container_id == containerId)
864 {
865 sensorIDs.emplace_back(sensorPdr->sensor_id);
866 }
867 }
868
869 return sensorIDs;
870}
871
Pavithra Barithayaa206b602025-08-21 14:37:13 +0530872EffecterPDRs getStateEffecterPDRsByType(uint16_t entityType,
873 const pldm_pdr* repo)
874{
875 uint8_t* outData = nullptr;
876 uint32_t size{};
877 const pldm_pdr_record* record = nullptr;
878 EffecterPDRs pdrs;
879 if (repo)
880 {
881 while ((record = pldm_pdr_find_record_by_type(
882 repo, PLDM_STATE_EFFECTER_PDR, record, &outData, &size)))
883 {
884 auto pdr = new (outData) pldm_state_effecter_pdr;
885 if (pdr && pdr->entity_type == entityType)
886 {
887 pdrs.emplace_back(outData, outData + size);
888 }
889 }
890 }
891 return pdrs;
892}
893
894std::vector<pldm::pdr::EffecterID> findEffecterIds(
895 const pldm_pdr* pdrRepo, uint16_t entityType, uint16_t entityInstance,
896 uint16_t containerId)
897{
898 std::vector<uint16_t> effecterIDs;
899 auto pdrs = getStateEffecterPDRsByType(entityType, pdrRepo);
900 for (const auto& pdr : pdrs)
901 {
902 auto effecterPdr =
903 reinterpret_cast<const pldm_state_effecter_pdr*>(pdr.data());
904 if (effecterPdr && effecterPdr->entity_type == entityType &&
905 effecterPdr->entity_instance == entityInstance &&
906 effecterPdr->container_id == containerId)
907 {
908 effecterIDs.emplace_back(effecterPdr->effecter_id);
909 }
910 }
911 return effecterIDs;
912}
913
Archana Kakanie26d13f2025-03-09 23:17:46 -0500914void setBiosAttr(const PendingAttributesList& biosAttrList)
915{
916 static constexpr auto SYSTEMD_PROPERTY_INTERFACE =
917 "org.freedesktop.DBus.Properties";
918 constexpr auto biosConfigPath = "/xyz/openbmc_project/bios_config/manager";
919 constexpr auto biosConfigIntf = "xyz.openbmc_project.BIOSConfig.Manager";
920
921 for (const auto& [attrName, biosAttrDetails] : biosAttrList)
922 {
923 auto& bus = DBusHandler::getBus();
924 try
925 {
926 auto service = pldm::utils::DBusHandler().getService(
927 biosConfigPath, biosConfigIntf);
928 auto method =
929 bus.new_method_call(service.c_str(), biosConfigPath,
930 SYSTEMD_PROPERTY_INTERFACE, "Set");
931 method.append(biosConfigIntf, "PendingAttributes",
932 std::variant<PendingAttributesList>(biosAttrList));
933 bus.call_noreply(method, dbusTimeout);
934 }
935 catch (const sdbusplus::exception::SdBusError& e)
936 {
937 AttributeType attrType;
938 AttributeValue attrValue;
939 std::tie(attrType, attrValue) = biosAttrDetails;
940 if (attrType ==
941 "xyz.openbmc_project.BIOSConfig.Manager.AttributeType.Integer")
942 {
943 info(
944 "Error setting the value {VALUE} to bios attribute {BIOS_ATTR}: {ERR_EXCEP}",
945 "VALUE", std::get<int64_t>(attrValue), "BIOS_ATTR",
946 attrName, "ERR_EXCEP", e);
947 }
948 else
949 {
950 info(
951 "Error setting the value {VALUE} to bios attribute {BIOS_ATTR}: {ERR_EXCEP}",
952 "VALUE", std::get<std::string>(attrValue), "BIOS_ATTR",
953 attrName, "ERR_EXCEP", e);
954 }
955 }
956 }
957}
Unive Tien7ad45b42025-08-18 06:04:53 +0000958
959long int generateSwId()
960{
961 return random() % 10000;
962}
963
George Liu83409572019-12-24 18:42:54 +0800964} // namespace utils
965} // namespace pldm