blob: 1cd5a72276aaf8defde75abd13b0c6a83f764d47 [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
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050030std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t /*tid*/,
31 uint16_t entityID,
32 uint16_t stateSetId,
33 const pldm_pdr* repo)
34{
35 uint8_t* outData = nullptr;
36 uint32_t size{};
37 const pldm_pdr_record* record{};
38 std::vector<std::vector<uint8_t>> pdrs;
39 try
40 {
41 do
42 {
43 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_EFFECTER_PDR,
44 record, &outData, &size);
45 if (record)
46 {
47 auto pdr = reinterpret_cast<pldm_state_effecter_pdr*>(outData);
48 auto compositeEffecterCount = pdr->composite_effecter_count;
Chicago Duana7aacc32020-06-10 18:03:38 +080049 auto possible_states_start = pdr->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050050
51 for (auto effecters = 0x00; effecters < compositeEffecterCount;
52 effecters++)
53 {
54 auto possibleStates =
55 reinterpret_cast<state_effecter_possible_states*>(
Chicago Duana7aacc32020-06-10 18:03:38 +080056 possible_states_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050057 auto setId = possibleStates->state_set_id;
58 auto possibleStateSize =
59 possibleStates->possible_states_size;
60
61 if (pdr->entity_type == entityID && setId == stateSetId)
62 {
63 std::vector<uint8_t> effecter_pdr(&outData[0],
64 &outData[size]);
65 pdrs.emplace_back(std::move(effecter_pdr));
66 break;
67 }
Chicago Duana7aacc32020-06-10 18:03:38 +080068 possible_states_start += possibleStateSize + sizeof(setId) +
69 sizeof(possibleStateSize);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050070 }
71 }
72
73 } while (record);
74 }
75 catch (const std::exception& e)
76 {
Riya Dixit76f2c602024-03-28 07:34:12 -050077 error("Failed to obtain a record, error - {ERROR}", "ERROR", e);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050078 }
79
80 return pdrs;
81}
82
Chicago Duan738e4d82020-05-28 16:39:19 +080083std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t /*tid*/,
84 uint16_t entityID,
85 uint16_t stateSetId,
86 const pldm_pdr* repo)
87{
88 uint8_t* outData = nullptr;
89 uint32_t size{};
90 const pldm_pdr_record* record{};
91 std::vector<std::vector<uint8_t>> pdrs;
92 try
93 {
94 do
95 {
96 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_SENSOR_PDR,
97 record, &outData, &size);
98 if (record)
99 {
100 auto pdr = reinterpret_cast<pldm_state_sensor_pdr*>(outData);
101 auto compositeSensorCount = pdr->composite_sensor_count;
Chicago Duana7aacc32020-06-10 18:03:38 +0800102 auto possible_states_start = pdr->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800103
104 for (auto sensors = 0x00; sensors < compositeSensorCount;
105 sensors++)
106 {
107 auto possibleStates =
108 reinterpret_cast<state_sensor_possible_states*>(
Chicago Duana7aacc32020-06-10 18:03:38 +0800109 possible_states_start);
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
George Liuba4c1fb2020-02-05 14:13:30 +0800206std::optional<std::vector<set_effecter_state_field>>
207 parseEffecterData(const std::vector<uint8_t>& effecterData,
208 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
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530254GetSubTreeResponse
255 DBusHandler::getSubtree(const std::string& searchPath, int depth,
256 const std::vector<std::string>& ifaceList) const
257{
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
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530285void reportError(const char* errorMsg)
George Liu83409572019-12-24 18:42:54 +0800286{
George Liu0e02c322020-01-01 09:41:51 +0800287 auto& bus = pldm::utils::DBusHandler::getBus();
Riya Dixit76f2c602024-03-28 07:34:12 -0500288 using LoggingCreate =
289 sdbusplus::client::xyz::openbmc_project::logging::Create<>;
George Liu83409572019-12-24 18:42:54 +0800290 try
291 {
George Liu83409572019-12-24 18:42:54 +0800292 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530293 auto severity =
294 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
295 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
296 Error);
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600297 auto method = bus.new_method_call(LoggingCreate::default_service,
298 LoggingCreate::instance_path,
299 LoggingCreate::interface, "Create");
300
George Liu83409572019-12-24 18:42:54 +0800301 std::map<std::string, std::string> addlData{};
302 method.append(errorMsg, severity, addlData);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000303 bus.call_noreply(method, dbusTimeout);
George Liu83409572019-12-24 18:42:54 +0800304 }
305 catch (const std::exception& e)
306 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600307 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500308 "Failed to do dbus call for creating error log for '{ERRMSG}' at path '{PATH}' and interface '{INTERFACE}', error - {ERROR}",
309 "ERRMSG", errorMsg, "PATH", LoggingCreate::instance_path,
310 "INTERFACE", LoggingCreate::interface, "ERROR", e);
George Liu83409572019-12-24 18:42:54 +0800311 }
312}
313
George Liu1e44c732020-02-28 20:20:06 +0800314void DBusHandler::setDbusProperty(const DBusMapping& dBusMap,
315 const PropertyValue& value) const
316{
317 auto setDbusValue = [&dBusMap, this](const auto& variant) {
318 auto& bus = getBus();
Patrick Williams6da4f912023-05-10 07:50:53 -0500319 auto service = getService(dBusMap.objectPath.c_str(),
320 dBusMap.interface.c_str());
George Liu1e44c732020-02-28 20:20:06 +0800321 auto method = bus.new_method_call(
322 service.c_str(), dBusMap.objectPath.c_str(), dbusProperties, "Set");
323 method.append(dBusMap.interface.c_str(), dBusMap.propertyName.c_str(),
324 variant);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000325 bus.call_noreply(method, dbusTimeout);
George Liu1e44c732020-02-28 20:20:06 +0800326 };
327
328 if (dBusMap.propertyType == "uint8_t")
329 {
330 std::variant<uint8_t> v = std::get<uint8_t>(value);
331 setDbusValue(v);
332 }
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600333 else if (dBusMap.propertyType == "bool")
334 {
335 std::variant<bool> v = std::get<bool>(value);
336 setDbusValue(v);
337 }
George Liu1e44c732020-02-28 20:20:06 +0800338 else if (dBusMap.propertyType == "int16_t")
339 {
340 std::variant<int16_t> v = std::get<int16_t>(value);
341 setDbusValue(v);
342 }
343 else if (dBusMap.propertyType == "uint16_t")
344 {
345 std::variant<uint16_t> v = std::get<uint16_t>(value);
346 setDbusValue(v);
347 }
348 else if (dBusMap.propertyType == "int32_t")
349 {
350 std::variant<int32_t> v = std::get<int32_t>(value);
351 setDbusValue(v);
352 }
353 else if (dBusMap.propertyType == "uint32_t")
354 {
355 std::variant<uint32_t> v = std::get<uint32_t>(value);
356 setDbusValue(v);
357 }
358 else if (dBusMap.propertyType == "int64_t")
359 {
360 std::variant<int64_t> v = std::get<int64_t>(value);
361 setDbusValue(v);
362 }
363 else if (dBusMap.propertyType == "uint64_t")
364 {
365 std::variant<uint64_t> v = std::get<uint64_t>(value);
366 setDbusValue(v);
367 }
368 else if (dBusMap.propertyType == "double")
369 {
370 std::variant<double> v = std::get<double>(value);
371 setDbusValue(v);
372 }
373 else if (dBusMap.propertyType == "string")
374 {
375 std::variant<std::string> v = std::get<std::string>(value);
376 setDbusValue(v);
377 }
378 else
379 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500380 error("Unsupported property type '{TYPE}'", "TYPE",
381 dBusMap.propertyType);
382 throw std::invalid_argument("UnSupported Dbus Type");
George Liu1e44c732020-02-28 20:20:06 +0800383 }
384}
385
John Wang9e242422020-03-05 08:37:50 +0800386PropertyValue DBusHandler::getDbusPropertyVariant(
387 const char* objPath, const char* dbusProp, const char* dbusInterface) const
388{
389 auto& bus = DBusHandler::getBus();
390 auto service = getService(objPath, dbusInterface);
Patrick Williams6da4f912023-05-10 07:50:53 -0500391 auto method = bus.new_method_call(service.c_str(), objPath, dbusProperties,
392 "Get");
John Wang9e242422020-03-05 08:37:50 +0800393 method.append(dbusInterface, dbusProp);
Patrick Williams75b8f462024-02-07 10:59:26 -0600394 return bus.call(method, dbusTimeout).unpack<PropertyValue>();
John Wang9e242422020-03-05 08:37:50 +0800395}
396
Riya Dixit754041d2024-02-20 06:15:49 -0600397ObjectValueTree DBusHandler::getManagedObj(const char* service,
398 const char* rootPath)
399{
400 auto& bus = DBusHandler::getBus();
401 auto method = bus.new_method_call(service, rootPath,
402 "org.freedesktop.DBus.ObjectManager",
403 "GetManagedObjects");
404 return bus.call(method).unpack<ObjectValueTree>();
405}
406
Gilbert Chen44524a52022-02-14 12:12:25 +0000407PropertyMap
408 DBusHandler::getDbusPropertiesVariant(const char* serviceName,
409 const char* objPath,
410 const char* dbusInterface) const
411{
412 auto& bus = DBusHandler::getBus();
413 auto method = bus.new_method_call(serviceName, objPath, dbusProperties,
414 "GetAll");
415 method.append(dbusInterface);
416 return bus.call(method, dbusTimeout).unpack<PropertyMap>();
417}
418
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530419PropertyValue jsonEntryToDbusVal(std::string_view type,
420 const nlohmann::json& value)
421{
422 PropertyValue propValue{};
423 if (type == "uint8_t")
424 {
425 propValue = static_cast<uint8_t>(value);
426 }
427 else if (type == "uint16_t")
428 {
429 propValue = static_cast<uint16_t>(value);
430 }
431 else if (type == "uint32_t")
432 {
433 propValue = static_cast<uint32_t>(value);
434 }
435 else if (type == "uint64_t")
436 {
437 propValue = static_cast<uint64_t>(value);
438 }
439 else if (type == "int16_t")
440 {
441 propValue = static_cast<int16_t>(value);
442 }
443 else if (type == "int32_t")
444 {
445 propValue = static_cast<int32_t>(value);
446 }
447 else if (type == "int64_t")
448 {
449 propValue = static_cast<int64_t>(value);
450 }
451 else if (type == "bool")
452 {
453 propValue = static_cast<bool>(value);
454 }
455 else if (type == "double")
456 {
457 propValue = static_cast<double>(value);
458 }
459 else if (type == "string")
460 {
461 propValue = static_cast<std::string>(value);
462 }
463 else
464 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500465 error("Unknown D-Bus property type '{TYPE}'", "TYPE", type);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530466 }
467
468 return propValue;
469}
470
Tom Joseph250c4752020-04-15 10:32:45 +0530471uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
472 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500473 uint16_t stateSetId, bool localOrRemote)
Tom Joseph250c4752020-04-15 10:32:45 +0530474{
475 uint8_t* pdrData = nullptr;
476 uint32_t pdrSize{};
477 const pldm_pdr_record* record{};
478 do
479 {
480 record = pldm_pdr_find_record_by_type(pdrRepo, PLDM_STATE_EFFECTER_PDR,
481 record, &pdrData, &pdrSize);
Sampa Misraa4a96162020-07-14 05:33:46 -0500482 if (record && (localOrRemote ^ pldm_pdr_record_is_remote(record)))
Tom Joseph250c4752020-04-15 10:32:45 +0530483 {
484 auto pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrData);
485 auto compositeEffecterCount = pdr->composite_effecter_count;
486 auto possible_states_start = pdr->possible_states;
487
488 for (auto effecters = 0x00; effecters < compositeEffecterCount;
489 effecters++)
490 {
491 auto possibleStates =
492 reinterpret_cast<state_effecter_possible_states*>(
493 possible_states_start);
494 auto setId = possibleStates->state_set_id;
495 auto possibleStateSize = possibleStates->possible_states_size;
496
497 if (entityType == pdr->entity_type &&
498 entityInstance == pdr->entity_instance &&
499 containerId == pdr->container_id && stateSetId == setId)
500 {
501 return pdr->effecter_id;
502 }
503 possible_states_start += possibleStateSize + sizeof(setId) +
504 sizeof(possibleStateSize);
505 }
506 }
507 } while (record);
508
509 return PLDM_INVALID_EFFECTER_ID;
510}
511
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800512int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
513 uint8_t sensorOffset, uint8_t eventState,
514 uint8_t previousEventState)
515{
516 try
517 {
518 auto& bus = DBusHandler::getBus();
519 auto msg = bus.new_signal("/xyz/openbmc_project/pldm",
520 "xyz.openbmc_project.PLDM.Event",
521 "StateSensorEvent");
522 msg.append(tid, sensorId, sensorOffset, eventState, previousEventState);
523
524 msg.signal_send();
525 }
Patrick Williams51330582021-10-06 12:48:56 -0500526 catch (const std::exception& e)
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800527 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500528 error("Failed to emit pldm event signal, error - {ERROR}", "ERROR", e);
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800529 return PLDM_ERROR;
530 }
531
532 return PLDM_SUCCESS;
533}
534
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500535uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
536 uint16_t entityType, uint16_t entityInstance,
537 uint16_t containerId, uint16_t stateSetId)
538{
539 auto pdrs = findStateSensorPDR(tid, entityType, stateSetId, pdrRepo);
540 for (auto pdr : pdrs)
541 {
542 auto sensorPdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
543 auto compositeSensorCount = sensorPdr->composite_sensor_count;
544 auto possible_states_start = sensorPdr->possible_states;
545
546 for (auto sensors = 0x00; sensors < compositeSensorCount; sensors++)
547 {
548 auto possibleStates =
549 reinterpret_cast<state_sensor_possible_states*>(
550 possible_states_start);
551 auto setId = possibleStates->state_set_id;
552 auto possibleStateSize = possibleStates->possible_states_size;
553 if (entityType == sensorPdr->entity_type &&
554 entityInstance == sensorPdr->entity_instance &&
555 stateSetId == setId && containerId == sensorPdr->container_id)
556 {
557 return sensorPdr->sensor_id;
558 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500559 possible_states_start += possibleStateSize + sizeof(setId) +
560 sizeof(possibleStateSize);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500561 }
562 }
563 return PLDM_INVALID_EFFECTER_ID;
564}
565
Tom Josephe5268cd2021-09-07 13:04:03 +0530566void printBuffer(bool isTx, const std::vector<uint8_t>& buffer)
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600567{
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530568 if (buffer.empty())
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600569 {
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530570 return;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600571 }
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530572
573 std::cout << (isTx ? "Tx: " : "Rx: ");
574
575 std::ranges::for_each(buffer, [](uint8_t byte) {
576 std::cout << std::format("{:02x} ", byte);
577 });
578
579 std::cout << std::endl;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600580}
581
Tom Joseph54922072021-06-19 02:45:46 -0700582std::string toString(const struct variable_field& var)
583{
584 if (var.ptr == nullptr || !var.length)
585 {
586 return "";
587 }
588
589 std::string str(reinterpret_cast<const char*>(var.ptr), var.length);
590 std::replace_if(
591 str.begin(), str.end(), [](const char& c) { return !isprint(c); }, ' ');
592 return str;
593}
594
George Liu872f0f62021-11-25 16:26:16 +0800595std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
596 std::string_view trimStr)
597{
598 std::vector<std::string> out;
599 size_t start;
600 size_t end = 0;
601
602 while ((start = srcStr.find_first_not_of(delim, end)) != std::string::npos)
603 {
604 end = srcStr.find(delim, start);
605 std::string_view dstStr = srcStr.substr(start, end - start);
606 if (!trimStr.empty())
607 {
608 dstStr.remove_prefix(dstStr.find_first_not_of(trimStr));
609 dstStr.remove_suffix(dstStr.size() - 1 -
610 dstStr.find_last_not_of(trimStr));
611 }
612
613 if (!dstStr.empty())
614 {
615 out.push_back(std::string(dstStr));
616 }
617 }
618
619 return out;
620}
621
Manojkiran Edaef773052021-07-29 09:29:28 +0530622std::string getCurrentSystemTime()
623{
Manojkiran Eda09a89822024-04-24 11:02:44 +0530624 const auto zonedTime{std::chrono::zoned_time{
625 std::chrono::current_zone(), std::chrono::system_clock::now()}};
626 return std::format("{:%F %Z %T}", zonedTime);
Manojkiran Edaef773052021-07-29 09:29:28 +0530627}
628
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500629bool checkForFruPresence(const std::string& objPath)
630{
631 bool isPresent = false;
632 static constexpr auto presentInterface =
633 "xyz.openbmc_project.Inventory.Item";
634 static constexpr auto presentProperty = "Present";
635 try
636 {
637 auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant(
638 objPath.c_str(), presentProperty, presentInterface);
639 isPresent = std::get<bool>(propVal);
640 }
641 catch (const sdbusplus::exception::SdBusError& e)
642 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500643 error("Failed to check for FRU presence at {PATH}, error - {ERROR}",
644 "PATH", objPath, "ERROR", e);
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500645 }
646 return isPresent;
647}
648
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600649bool checkIfLogicalBitSet(const uint16_t& containerId)
650{
651 return !(containerId & 0x8000);
652}
653
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500654void setFruPresence(const std::string& fruObjPath, bool present)
655{
656 pldm::utils::PropertyValue value{present};
657 pldm::utils::DBusMapping dbusMapping;
658 dbusMapping.objectPath = fruObjPath;
659 dbusMapping.interface = "xyz.openbmc_project.Inventory.Item";
660 dbusMapping.propertyName = "Present";
661 dbusMapping.propertyType = "bool";
662 try
663 {
664 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
665 }
666 catch (const std::exception& e)
667 {
668 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500669 "Failed to set the present property on path '{PATH}', error - {ERROR}.",
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500670 "PATH", fruObjPath, "ERROR", e);
671 }
672}
673
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000674std::string_view trimNameForDbus(std::string& name)
675{
676 std::replace(name.begin(), name.end(), ' ', '_');
677 auto nullTerminatorPos = name.find('\0');
678 if (nullTerminatorPos != std::string::npos)
679 {
680 name.erase(nullTerminatorPos);
681 }
682 return name;
683}
George Liu83409572019-12-24 18:42:54 +0800684} // namespace utils
685} // namespace pldm