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