blob: f7a0ae03239b70db82b27994c607e2a9973ccf72 [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>
George Liu6492f522020-06-16 10:34:05 +08005
Riya Dixit49cfb132023-03-02 04:26:53 -06006#include <phosphor-logging/lg2.hpp>
George Liu6492f522020-06-16 10:34:05 +08007#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
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050029std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t /*tid*/,
30 uint16_t entityID,
31 uint16_t stateSetId,
32 const pldm_pdr* repo)
33{
34 uint8_t* outData = nullptr;
35 uint32_t size{};
36 const pldm_pdr_record* record{};
37 std::vector<std::vector<uint8_t>> pdrs;
38 try
39 {
40 do
41 {
42 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_EFFECTER_PDR,
43 record, &outData, &size);
44 if (record)
45 {
46 auto pdr = reinterpret_cast<pldm_state_effecter_pdr*>(outData);
47 auto compositeEffecterCount = pdr->composite_effecter_count;
Chicago Duana7aacc32020-06-10 18:03:38 +080048 auto possible_states_start = pdr->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050049
50 for (auto effecters = 0x00; effecters < compositeEffecterCount;
51 effecters++)
52 {
53 auto possibleStates =
54 reinterpret_cast<state_effecter_possible_states*>(
Chicago Duana7aacc32020-06-10 18:03:38 +080055 possible_states_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050056 auto setId = possibleStates->state_set_id;
57 auto possibleStateSize =
58 possibleStates->possible_states_size;
59
60 if (pdr->entity_type == entityID && setId == stateSetId)
61 {
62 std::vector<uint8_t> effecter_pdr(&outData[0],
63 &outData[size]);
64 pdrs.emplace_back(std::move(effecter_pdr));
65 break;
66 }
Chicago Duana7aacc32020-06-10 18:03:38 +080067 possible_states_start += possibleStateSize + sizeof(setId) +
68 sizeof(possibleStateSize);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050069 }
70 }
71
72 } while (record);
73 }
74 catch (const std::exception& e)
75 {
Riya Dixit76f2c602024-03-28 07:34:12 -050076 error("Failed to obtain a record, error - {ERROR}", "ERROR", e);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050077 }
78
79 return pdrs;
80}
81
Chicago Duan738e4d82020-05-28 16:39:19 +080082std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t /*tid*/,
83 uint16_t entityID,
84 uint16_t stateSetId,
85 const pldm_pdr* repo)
86{
87 uint8_t* outData = nullptr;
88 uint32_t size{};
89 const pldm_pdr_record* record{};
90 std::vector<std::vector<uint8_t>> pdrs;
91 try
92 {
93 do
94 {
95 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_SENSOR_PDR,
96 record, &outData, &size);
97 if (record)
98 {
99 auto pdr = reinterpret_cast<pldm_state_sensor_pdr*>(outData);
100 auto compositeSensorCount = pdr->composite_sensor_count;
Chicago Duana7aacc32020-06-10 18:03:38 +0800101 auto possible_states_start = pdr->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800102
103 for (auto sensors = 0x00; sensors < compositeSensorCount;
104 sensors++)
105 {
106 auto possibleStates =
107 reinterpret_cast<state_sensor_possible_states*>(
Chicago Duana7aacc32020-06-10 18:03:38 +0800108 possible_states_start);
Chicago Duan738e4d82020-05-28 16:39:19 +0800109 auto setId = possibleStates->state_set_id;
110 auto possibleStateSize =
111 possibleStates->possible_states_size;
112
113 if (pdr->entity_type == entityID && setId == stateSetId)
114 {
115 std::vector<uint8_t> sensor_pdr(&outData[0],
116 &outData[size]);
117 pdrs.emplace_back(std::move(sensor_pdr));
118 break;
119 }
Chicago Duana7aacc32020-06-10 18:03:38 +0800120 possible_states_start += possibleStateSize + sizeof(setId) +
121 sizeof(possibleStateSize);
Chicago Duan738e4d82020-05-28 16:39:19 +0800122 }
123 }
124
125 } while (record);
126 }
127 catch (const std::exception& e)
128 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500129 error(
130 "Failed to obtain a record with entity ID '{ENTITYID}', error - {ERROR}",
131 "ENTITYID", entityID, "ERROR", e);
Chicago Duan738e4d82020-05-28 16:39:19 +0800132 }
133
134 return pdrs;
135}
136
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500137uint8_t readHostEID()
138{
139 uint8_t eid{};
Brad Bishop06052cc2021-08-16 15:17:16 -0400140 std::ifstream eidFile{HOST_EID_PATH};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500141 if (!eidFile.good())
142 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500143 error("Failed to open remote terminus EID file at path '{PATH}'",
144 "PATH", static_cast<std::string>(HOST_EID_PATH));
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500145 }
146 else
147 {
148 std::string eidStr;
149 eidFile >> eidStr;
150 if (!eidStr.empty())
151 {
152 eid = atoi(eidStr.c_str());
153 }
154 else
155 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500156 error("Remote terminus EID file was empty");
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500157 }
158 }
159
160 return eid;
161}
George Liu83409572019-12-24 18:42:54 +0800162
163uint8_t getNumPadBytes(uint32_t data)
164{
165 uint8_t pad;
166 pad = ((data % 4) ? (4 - data % 4) : 0);
167 return pad;
168} // end getNumPadBytes
169
170bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
171 uint8_t* hour, uint8_t* min, uint8_t* sec)
172{
173 constexpr uint64_t max_data = 29991231115959;
174 constexpr uint64_t min_data = 19700101000000;
175 if (data < min_data || data > max_data)
176 {
177 return false;
178 }
179
180 *year = data / 10000000000;
181 data = data % 10000000000;
182 *month = data / 100000000;
183 data = data % 100000000;
184 *day = data / 1000000;
185 data = data % 1000000;
186 *hour = data / 10000;
187 data = data % 10000;
188 *min = data / 100;
189 *sec = data % 100;
190
191 return true;
192}
193
George Liuba4c1fb2020-02-05 14:13:30 +0800194std::optional<std::vector<set_effecter_state_field>>
195 parseEffecterData(const std::vector<uint8_t>& effecterData,
196 uint8_t effecterCount)
George Liu83409572019-12-24 18:42:54 +0800197{
George Liuba4c1fb2020-02-05 14:13:30 +0800198 std::vector<set_effecter_state_field> stateField;
199
200 if (effecterData.size() != effecterCount * 2)
George Liu83409572019-12-24 18:42:54 +0800201 {
George Liuba4c1fb2020-02-05 14:13:30 +0800202 return std::nullopt;
George Liu83409572019-12-24 18:42:54 +0800203 }
204
George Liuba4c1fb2020-02-05 14:13:30 +0800205 for (uint8_t i = 0; i < effecterCount; ++i)
George Liu83409572019-12-24 18:42:54 +0800206 {
George Liuba4c1fb2020-02-05 14:13:30 +0800207 uint8_t set_request = effecterData[i * 2] == PLDM_REQUEST_SET
208 ? PLDM_REQUEST_SET
209 : PLDM_NO_CHANGE;
210 set_effecter_state_field filed{set_request, effecterData[i * 2 + 1]};
211 stateField.emplace_back(std::move(filed));
George Liu83409572019-12-24 18:42:54 +0800212 }
213
George Liuba4c1fb2020-02-05 14:13:30 +0800214 return std::make_optional(std::move(stateField));
George Liu83409572019-12-24 18:42:54 +0800215}
216
George Liu0e02c322020-01-01 09:41:51 +0800217std::string DBusHandler::getService(const char* path,
218 const char* interface) const
George Liu83409572019-12-24 18:42:54 +0800219{
220 using DbusInterfaceList = std::vector<std::string>;
221 std::map<std::string, std::vector<std::string>> mapperResponse;
George Liu0e02c322020-01-01 09:41:51 +0800222 auto& bus = DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +0800223
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600224 auto mapper = bus.new_method_call(ObjectMapper::default_service,
225 ObjectMapper::instance_path,
226 ObjectMapper::interface, "GetObject");
George Liudf9a6d32020-12-22 16:27:16 +0800227
228 if (interface)
229 {
230 mapper.append(path, DbusInterfaceList({interface}));
231 }
232 else
233 {
234 mapper.append(path, DbusInterfaceList({}));
235 }
George Liu83409572019-12-24 18:42:54 +0800236
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500237 auto mapperResponseMsg = bus.call(mapper, dbusTimeout);
George Liu0e02c322020-01-01 09:41:51 +0800238 mapperResponseMsg.read(mapperResponse);
George Liu83409572019-12-24 18:42:54 +0800239 return mapperResponse.begin()->first;
240}
241
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530242GetSubTreeResponse
243 DBusHandler::getSubtree(const std::string& searchPath, int depth,
244 const std::vector<std::string>& ifaceList) const
245{
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530246 auto& bus = pldm::utils::DBusHandler::getBus();
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600247 auto method = bus.new_method_call(ObjectMapper::default_service,
248 ObjectMapper::instance_path,
249 ObjectMapper::interface, "GetSubTree");
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530250 method.append(searchPath, depth, ifaceList);
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500251 auto reply = bus.call(method, dbusTimeout);
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530252 GetSubTreeResponse response;
253 reply.read(response);
254 return response;
255}
256
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500257GetSubTreePathsResponse DBusHandler::getSubTreePaths(
258 const std::string& objectPath, int depth,
259 const std::vector<std::string>& ifaceList) const
260{
261 std::vector<std::string> paths;
262 auto& bus = pldm::utils::DBusHandler::getBus();
263 auto method = bus.new_method_call(
264 ObjectMapper::default_service, ObjectMapper::instance_path,
265 ObjectMapper::interface, "GetSubTreePaths");
266 method.append(objectPath, depth, ifaceList);
267 auto reply = bus.call(method, dbusTimeout);
268
269 reply.read(paths);
270 return paths;
271}
272
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530273void reportError(const char* errorMsg)
George Liu83409572019-12-24 18:42:54 +0800274{
George Liu0e02c322020-01-01 09:41:51 +0800275 auto& bus = pldm::utils::DBusHandler::getBus();
Riya Dixit76f2c602024-03-28 07:34:12 -0500276 using LoggingCreate =
277 sdbusplus::client::xyz::openbmc_project::logging::Create<>;
George Liu83409572019-12-24 18:42:54 +0800278 try
279 {
George Liu83409572019-12-24 18:42:54 +0800280 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530281 auto severity =
282 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
283 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
284 Error);
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600285 auto method = bus.new_method_call(LoggingCreate::default_service,
286 LoggingCreate::instance_path,
287 LoggingCreate::interface, "Create");
288
George Liu83409572019-12-24 18:42:54 +0800289 std::map<std::string, std::string> addlData{};
290 method.append(errorMsg, severity, addlData);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000291 bus.call_noreply(method, dbusTimeout);
George Liu83409572019-12-24 18:42:54 +0800292 }
293 catch (const std::exception& e)
294 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600295 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500296 "Failed to do dbus call for creating error log for '{ERRMSG}' at path '{PATH}' and interface '{INTERFACE}', error - {ERROR}",
297 "ERRMSG", errorMsg, "PATH", LoggingCreate::instance_path,
298 "INTERFACE", LoggingCreate::interface, "ERROR", e);
George Liu83409572019-12-24 18:42:54 +0800299 }
300}
301
George Liu1e44c732020-02-28 20:20:06 +0800302void DBusHandler::setDbusProperty(const DBusMapping& dBusMap,
303 const PropertyValue& value) const
304{
305 auto setDbusValue = [&dBusMap, this](const auto& variant) {
306 auto& bus = getBus();
Patrick Williams6da4f912023-05-10 07:50:53 -0500307 auto service = getService(dBusMap.objectPath.c_str(),
308 dBusMap.interface.c_str());
George Liu1e44c732020-02-28 20:20:06 +0800309 auto method = bus.new_method_call(
310 service.c_str(), dBusMap.objectPath.c_str(), dbusProperties, "Set");
311 method.append(dBusMap.interface.c_str(), dBusMap.propertyName.c_str(),
312 variant);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000313 bus.call_noreply(method, dbusTimeout);
George Liu1e44c732020-02-28 20:20:06 +0800314 };
315
316 if (dBusMap.propertyType == "uint8_t")
317 {
318 std::variant<uint8_t> v = std::get<uint8_t>(value);
319 setDbusValue(v);
320 }
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600321 else if (dBusMap.propertyType == "bool")
322 {
323 std::variant<bool> v = std::get<bool>(value);
324 setDbusValue(v);
325 }
George Liu1e44c732020-02-28 20:20:06 +0800326 else if (dBusMap.propertyType == "int16_t")
327 {
328 std::variant<int16_t> v = std::get<int16_t>(value);
329 setDbusValue(v);
330 }
331 else if (dBusMap.propertyType == "uint16_t")
332 {
333 std::variant<uint16_t> v = std::get<uint16_t>(value);
334 setDbusValue(v);
335 }
336 else if (dBusMap.propertyType == "int32_t")
337 {
338 std::variant<int32_t> v = std::get<int32_t>(value);
339 setDbusValue(v);
340 }
341 else if (dBusMap.propertyType == "uint32_t")
342 {
343 std::variant<uint32_t> v = std::get<uint32_t>(value);
344 setDbusValue(v);
345 }
346 else if (dBusMap.propertyType == "int64_t")
347 {
348 std::variant<int64_t> v = std::get<int64_t>(value);
349 setDbusValue(v);
350 }
351 else if (dBusMap.propertyType == "uint64_t")
352 {
353 std::variant<uint64_t> v = std::get<uint64_t>(value);
354 setDbusValue(v);
355 }
356 else if (dBusMap.propertyType == "double")
357 {
358 std::variant<double> v = std::get<double>(value);
359 setDbusValue(v);
360 }
361 else if (dBusMap.propertyType == "string")
362 {
363 std::variant<std::string> v = std::get<std::string>(value);
364 setDbusValue(v);
365 }
366 else
367 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500368 error("Unsupported property type '{TYPE}'", "TYPE",
369 dBusMap.propertyType);
370 throw std::invalid_argument("UnSupported Dbus Type");
George Liu1e44c732020-02-28 20:20:06 +0800371 }
372}
373
John Wang9e242422020-03-05 08:37:50 +0800374PropertyValue DBusHandler::getDbusPropertyVariant(
375 const char* objPath, const char* dbusProp, const char* dbusInterface) const
376{
377 auto& bus = DBusHandler::getBus();
378 auto service = getService(objPath, dbusInterface);
Patrick Williams6da4f912023-05-10 07:50:53 -0500379 auto method = bus.new_method_call(service.c_str(), objPath, dbusProperties,
380 "Get");
John Wang9e242422020-03-05 08:37:50 +0800381 method.append(dbusInterface, dbusProp);
Patrick Williams75b8f462024-02-07 10:59:26 -0600382 return bus.call(method, dbusTimeout).unpack<PropertyValue>();
John Wang9e242422020-03-05 08:37:50 +0800383}
384
Riya Dixit754041d2024-02-20 06:15:49 -0600385ObjectValueTree DBusHandler::getManagedObj(const char* service,
386 const char* rootPath)
387{
388 auto& bus = DBusHandler::getBus();
389 auto method = bus.new_method_call(service, rootPath,
390 "org.freedesktop.DBus.ObjectManager",
391 "GetManagedObjects");
392 return bus.call(method).unpack<ObjectValueTree>();
393}
394
Gilbert Chen44524a52022-02-14 12:12:25 +0000395PropertyMap
396 DBusHandler::getDbusPropertiesVariant(const char* serviceName,
397 const char* objPath,
398 const char* dbusInterface) const
399{
400 auto& bus = DBusHandler::getBus();
401 auto method = bus.new_method_call(serviceName, objPath, dbusProperties,
402 "GetAll");
403 method.append(dbusInterface);
404 return bus.call(method, dbusTimeout).unpack<PropertyMap>();
405}
406
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530407PropertyValue jsonEntryToDbusVal(std::string_view type,
408 const nlohmann::json& value)
409{
410 PropertyValue propValue{};
411 if (type == "uint8_t")
412 {
413 propValue = static_cast<uint8_t>(value);
414 }
415 else if (type == "uint16_t")
416 {
417 propValue = static_cast<uint16_t>(value);
418 }
419 else if (type == "uint32_t")
420 {
421 propValue = static_cast<uint32_t>(value);
422 }
423 else if (type == "uint64_t")
424 {
425 propValue = static_cast<uint64_t>(value);
426 }
427 else if (type == "int16_t")
428 {
429 propValue = static_cast<int16_t>(value);
430 }
431 else if (type == "int32_t")
432 {
433 propValue = static_cast<int32_t>(value);
434 }
435 else if (type == "int64_t")
436 {
437 propValue = static_cast<int64_t>(value);
438 }
439 else if (type == "bool")
440 {
441 propValue = static_cast<bool>(value);
442 }
443 else if (type == "double")
444 {
445 propValue = static_cast<double>(value);
446 }
447 else if (type == "string")
448 {
449 propValue = static_cast<std::string>(value);
450 }
451 else
452 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500453 error("Unknown D-Bus property type '{TYPE}'", "TYPE", type);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530454 }
455
456 return propValue;
457}
458
Tom Joseph250c4752020-04-15 10:32:45 +0530459uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
460 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500461 uint16_t stateSetId, bool localOrRemote)
Tom Joseph250c4752020-04-15 10:32:45 +0530462{
463 uint8_t* pdrData = nullptr;
464 uint32_t pdrSize{};
465 const pldm_pdr_record* record{};
466 do
467 {
468 record = pldm_pdr_find_record_by_type(pdrRepo, PLDM_STATE_EFFECTER_PDR,
469 record, &pdrData, &pdrSize);
Sampa Misraa4a96162020-07-14 05:33:46 -0500470 if (record && (localOrRemote ^ pldm_pdr_record_is_remote(record)))
Tom Joseph250c4752020-04-15 10:32:45 +0530471 {
472 auto pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrData);
473 auto compositeEffecterCount = pdr->composite_effecter_count;
474 auto possible_states_start = pdr->possible_states;
475
476 for (auto effecters = 0x00; effecters < compositeEffecterCount;
477 effecters++)
478 {
479 auto possibleStates =
480 reinterpret_cast<state_effecter_possible_states*>(
481 possible_states_start);
482 auto setId = possibleStates->state_set_id;
483 auto possibleStateSize = possibleStates->possible_states_size;
484
485 if (entityType == pdr->entity_type &&
486 entityInstance == pdr->entity_instance &&
487 containerId == pdr->container_id && stateSetId == setId)
488 {
489 return pdr->effecter_id;
490 }
491 possible_states_start += possibleStateSize + sizeof(setId) +
492 sizeof(possibleStateSize);
493 }
494 }
495 } while (record);
496
497 return PLDM_INVALID_EFFECTER_ID;
498}
499
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800500int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
501 uint8_t sensorOffset, uint8_t eventState,
502 uint8_t previousEventState)
503{
504 try
505 {
506 auto& bus = DBusHandler::getBus();
507 auto msg = bus.new_signal("/xyz/openbmc_project/pldm",
508 "xyz.openbmc_project.PLDM.Event",
509 "StateSensorEvent");
510 msg.append(tid, sensorId, sensorOffset, eventState, previousEventState);
511
512 msg.signal_send();
513 }
Patrick Williams51330582021-10-06 12:48:56 -0500514 catch (const std::exception& e)
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800515 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500516 error("Failed to emit pldm event signal, error - {ERROR}", "ERROR", e);
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800517 return PLDM_ERROR;
518 }
519
520 return PLDM_SUCCESS;
521}
522
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500523uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
524 uint16_t entityType, uint16_t entityInstance,
525 uint16_t containerId, uint16_t stateSetId)
526{
527 auto pdrs = findStateSensorPDR(tid, entityType, stateSetId, pdrRepo);
528 for (auto pdr : pdrs)
529 {
530 auto sensorPdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
531 auto compositeSensorCount = sensorPdr->composite_sensor_count;
532 auto possible_states_start = sensorPdr->possible_states;
533
534 for (auto sensors = 0x00; sensors < compositeSensorCount; sensors++)
535 {
536 auto possibleStates =
537 reinterpret_cast<state_sensor_possible_states*>(
538 possible_states_start);
539 auto setId = possibleStates->state_set_id;
540 auto possibleStateSize = possibleStates->possible_states_size;
541 if (entityType == sensorPdr->entity_type &&
542 entityInstance == sensorPdr->entity_instance &&
543 stateSetId == setId && containerId == sensorPdr->container_id)
544 {
545 return sensorPdr->sensor_id;
546 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500547 possible_states_start += possibleStateSize + sizeof(setId) +
548 sizeof(possibleStateSize);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500549 }
550 }
551 return PLDM_INVALID_EFFECTER_ID;
552}
553
Tom Josephe5268cd2021-09-07 13:04:03 +0530554void printBuffer(bool isTx, const std::vector<uint8_t>& buffer)
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600555{
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530556 if (buffer.empty())
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600557 {
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530558 return;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600559 }
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530560
561 std::cout << (isTx ? "Tx: " : "Rx: ");
562
563 std::ranges::for_each(buffer, [](uint8_t byte) {
564 std::cout << std::format("{:02x} ", byte);
565 });
566
567 std::cout << std::endl;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600568}
569
Tom Joseph54922072021-06-19 02:45:46 -0700570std::string toString(const struct variable_field& var)
571{
572 if (var.ptr == nullptr || !var.length)
573 {
574 return "";
575 }
576
577 std::string str(reinterpret_cast<const char*>(var.ptr), var.length);
578 std::replace_if(
579 str.begin(), str.end(), [](const char& c) { return !isprint(c); }, ' ');
580 return str;
581}
582
George Liu872f0f62021-11-25 16:26:16 +0800583std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
584 std::string_view trimStr)
585{
586 std::vector<std::string> out;
587 size_t start;
588 size_t end = 0;
589
590 while ((start = srcStr.find_first_not_of(delim, end)) != std::string::npos)
591 {
592 end = srcStr.find(delim, start);
593 std::string_view dstStr = srcStr.substr(start, end - start);
594 if (!trimStr.empty())
595 {
596 dstStr.remove_prefix(dstStr.find_first_not_of(trimStr));
597 dstStr.remove_suffix(dstStr.size() - 1 -
598 dstStr.find_last_not_of(trimStr));
599 }
600
601 if (!dstStr.empty())
602 {
603 out.push_back(std::string(dstStr));
604 }
605 }
606
607 return out;
608}
609
Manojkiran Edaef773052021-07-29 09:29:28 +0530610std::string getCurrentSystemTime()
611{
Manojkiran Eda09a89822024-04-24 11:02:44 +0530612 const auto zonedTime{std::chrono::zoned_time{
613 std::chrono::current_zone(), std::chrono::system_clock::now()}};
614 return std::format("{:%F %Z %T}", zonedTime);
Manojkiran Edaef773052021-07-29 09:29:28 +0530615}
616
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500617bool checkForFruPresence(const std::string& objPath)
618{
619 bool isPresent = false;
620 static constexpr auto presentInterface =
621 "xyz.openbmc_project.Inventory.Item";
622 static constexpr auto presentProperty = "Present";
623 try
624 {
625 auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant(
626 objPath.c_str(), presentProperty, presentInterface);
627 isPresent = std::get<bool>(propVal);
628 }
629 catch (const sdbusplus::exception::SdBusError& e)
630 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500631 error("Failed to check for FRU presence at {PATH}, error - {ERROR}",
632 "PATH", objPath, "ERROR", e);
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500633 }
634 return isPresent;
635}
636
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600637bool checkIfLogicalBitSet(const uint16_t& containerId)
638{
639 return !(containerId & 0x8000);
640}
641
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500642void setFruPresence(const std::string& fruObjPath, bool present)
643{
644 pldm::utils::PropertyValue value{present};
645 pldm::utils::DBusMapping dbusMapping;
646 dbusMapping.objectPath = fruObjPath;
647 dbusMapping.interface = "xyz.openbmc_project.Inventory.Item";
648 dbusMapping.propertyName = "Present";
649 dbusMapping.propertyType = "bool";
650 try
651 {
652 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
653 }
654 catch (const std::exception& e)
655 {
656 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500657 "Failed to set the present property on path '{PATH}', error - {ERROR}.",
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500658 "PATH", fruObjPath, "ERROR", e);
659 }
660}
661
George Liu83409572019-12-24 18:42:54 +0800662} // namespace utils
663} // namespace pldm