blob: 7d06fa88f8e0d397244f6bacf89f3b287e1ce942 [file] [log] [blame]
Brad Bishop06052cc2021-08-16 15:17:16 -04001#include "config.h"
2
George Liu83409572019-12-24 18:42:54 +08003#include "utils.hpp"
4
George Liuc453e162022-12-21 17:16:23 +08005#include <libpldm/pdr.h>
6#include <libpldm/pldm_types.h>
George Liu6492f522020-06-16 10:34:05 +08007
Riya Dixit49cfb132023-03-02 04:26:53 -06008#include <phosphor-logging/lg2.hpp>
George Liu6492f522020-06-16 10:34:05 +08009#include <xyz/openbmc_project/Common/error.hpp>
10
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{
George Liu83409572019-12-24 18:42:54 +080028constexpr auto mapperBusName = "xyz.openbmc_project.ObjectMapper";
29constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
30constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper";
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050031
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050032std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t /*tid*/,
33 uint16_t entityID,
34 uint16_t stateSetId,
35 const pldm_pdr* repo)
36{
37 uint8_t* outData = nullptr;
38 uint32_t size{};
39 const pldm_pdr_record* record{};
40 std::vector<std::vector<uint8_t>> pdrs;
41 try
42 {
43 do
44 {
45 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_EFFECTER_PDR,
46 record, &outData, &size);
47 if (record)
48 {
49 auto pdr = reinterpret_cast<pldm_state_effecter_pdr*>(outData);
50 auto compositeEffecterCount = pdr->composite_effecter_count;
Chicago Duana7aacc32020-06-10 18:03:38 +080051 auto possible_states_start = pdr->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050052
53 for (auto effecters = 0x00; effecters < compositeEffecterCount;
54 effecters++)
55 {
56 auto possibleStates =
57 reinterpret_cast<state_effecter_possible_states*>(
Chicago Duana7aacc32020-06-10 18:03:38 +080058 possible_states_start);
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 Dixit49cfb132023-03-02 04:26:53 -060079 error(" Failed to obtain a record. ERROR = {ERR_EXCEP}", "ERR_EXCEP",
80 e.what());
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050081 }
82
83 return pdrs;
84}
85
Chicago Duan738e4d82020-05-28 16:39:19 +080086std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t /*tid*/,
87 uint16_t entityID,
88 uint16_t stateSetId,
89 const pldm_pdr* repo)
90{
91 uint8_t* outData = nullptr;
92 uint32_t size{};
93 const pldm_pdr_record* record{};
94 std::vector<std::vector<uint8_t>> pdrs;
95 try
96 {
97 do
98 {
99 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_SENSOR_PDR,
100 record, &outData, &size);
101 if (record)
102 {
103 auto pdr = reinterpret_cast<pldm_state_sensor_pdr*>(outData);
104 auto compositeSensorCount = pdr->composite_sensor_count;
Chicago Duana7aacc32020-06-10 18:03:38 +0800105 auto possible_states_start = pdr->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800106
107 for (auto sensors = 0x00; sensors < compositeSensorCount;
108 sensors++)
109 {
110 auto possibleStates =
111 reinterpret_cast<state_sensor_possible_states*>(
Chicago Duana7aacc32020-06-10 18:03:38 +0800112 possible_states_start);
Chicago Duan738e4d82020-05-28 16:39:19 +0800113 auto setId = possibleStates->state_set_id;
114 auto possibleStateSize =
115 possibleStates->possible_states_size;
116
117 if (pdr->entity_type == entityID && setId == stateSetId)
118 {
119 std::vector<uint8_t> sensor_pdr(&outData[0],
120 &outData[size]);
121 pdrs.emplace_back(std::move(sensor_pdr));
122 break;
123 }
Chicago Duana7aacc32020-06-10 18:03:38 +0800124 possible_states_start += possibleStateSize + sizeof(setId) +
125 sizeof(possibleStateSize);
Chicago Duan738e4d82020-05-28 16:39:19 +0800126 }
127 }
128
129 } while (record);
130 }
131 catch (const std::exception& e)
132 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600133 error(" Failed to obtain a record. ERROR = {ERR_EXCEP}", "ERR_EXCEP",
134 e.what());
Chicago Duan738e4d82020-05-28 16:39:19 +0800135 }
136
137 return pdrs;
138}
139
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500140uint8_t readHostEID()
141{
142 uint8_t eid{};
Brad Bishop06052cc2021-08-16 15:17:16 -0400143 std::ifstream eidFile{HOST_EID_PATH};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500144 if (!eidFile.good())
145 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600146 error("Could not open host EID file: {HOST_PATH}", "HOST_PATH",
147 static_cast<std::string>(HOST_EID_PATH));
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500148 }
149 else
150 {
151 std::string eidStr;
152 eidFile >> eidStr;
153 if (!eidStr.empty())
154 {
155 eid = atoi(eidStr.c_str());
156 }
157 else
158 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600159 error("Host EID file was empty");
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500160 }
161 }
162
163 return eid;
164}
George Liu83409572019-12-24 18:42:54 +0800165
166uint8_t getNumPadBytes(uint32_t data)
167{
168 uint8_t pad;
169 pad = ((data % 4) ? (4 - data % 4) : 0);
170 return pad;
171} // end getNumPadBytes
172
173bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
174 uint8_t* hour, uint8_t* min, uint8_t* sec)
175{
176 constexpr uint64_t max_data = 29991231115959;
177 constexpr uint64_t min_data = 19700101000000;
178 if (data < min_data || data > max_data)
179 {
180 return false;
181 }
182
183 *year = data / 10000000000;
184 data = data % 10000000000;
185 *month = data / 100000000;
186 data = data % 100000000;
187 *day = data / 1000000;
188 data = data % 1000000;
189 *hour = data / 10000;
190 data = data % 10000;
191 *min = data / 100;
192 *sec = data % 100;
193
194 return true;
195}
196
George Liuba4c1fb2020-02-05 14:13:30 +0800197std::optional<std::vector<set_effecter_state_field>>
198 parseEffecterData(const std::vector<uint8_t>& effecterData,
199 uint8_t effecterCount)
George Liu83409572019-12-24 18:42:54 +0800200{
George Liuba4c1fb2020-02-05 14:13:30 +0800201 std::vector<set_effecter_state_field> stateField;
202
203 if (effecterData.size() != effecterCount * 2)
George Liu83409572019-12-24 18:42:54 +0800204 {
George Liuba4c1fb2020-02-05 14:13:30 +0800205 return std::nullopt;
George Liu83409572019-12-24 18:42:54 +0800206 }
207
George Liuba4c1fb2020-02-05 14:13:30 +0800208 for (uint8_t i = 0; i < effecterCount; ++i)
George Liu83409572019-12-24 18:42:54 +0800209 {
George Liuba4c1fb2020-02-05 14:13:30 +0800210 uint8_t set_request = effecterData[i * 2] == PLDM_REQUEST_SET
211 ? PLDM_REQUEST_SET
212 : PLDM_NO_CHANGE;
213 set_effecter_state_field filed{set_request, effecterData[i * 2 + 1]};
214 stateField.emplace_back(std::move(filed));
George Liu83409572019-12-24 18:42:54 +0800215 }
216
George Liuba4c1fb2020-02-05 14:13:30 +0800217 return std::make_optional(std::move(stateField));
George Liu83409572019-12-24 18:42:54 +0800218}
219
George Liu0e02c322020-01-01 09:41:51 +0800220std::string DBusHandler::getService(const char* path,
221 const char* interface) const
George Liu83409572019-12-24 18:42:54 +0800222{
223 using DbusInterfaceList = std::vector<std::string>;
224 std::map<std::string, std::vector<std::string>> mapperResponse;
George Liu0e02c322020-01-01 09:41:51 +0800225 auto& bus = DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +0800226
George Liu0e02c322020-01-01 09:41:51 +0800227 auto mapper = bus.new_method_call(mapperBusName, mapperPath,
228 mapperInterface, "GetObject");
229 mapper.append(path, DbusInterfaceList({interface}));
George Liu83409572019-12-24 18:42:54 +0800230
George Liu0e02c322020-01-01 09:41:51 +0800231 auto mapperResponseMsg = bus.call(mapper);
232 mapperResponseMsg.read(mapperResponse);
George Liu83409572019-12-24 18:42:54 +0800233 return mapperResponse.begin()->first;
234}
235
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530236GetSubTreeResponse
237 DBusHandler::getSubtree(const std::string& searchPath, int depth,
238 const std::vector<std::string>& ifaceList) const
239{
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530240 auto& bus = pldm::utils::DBusHandler::getBus();
241 auto method = bus.new_method_call(mapperBusName, mapperPath,
242 mapperInterface, "GetSubTree");
243 method.append(searchPath, depth, ifaceList);
244 auto reply = bus.call(method);
245 GetSubTreeResponse response;
246 reply.read(response);
247 return response;
248}
249
George Liu83409572019-12-24 18:42:54 +0800250void reportError(const char* errorMsg)
251{
252 static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
253 static constexpr auto logInterface = "xyz.openbmc_project.Logging.Create";
254
George Liu0e02c322020-01-01 09:41:51 +0800255 auto& bus = pldm::utils::DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +0800256
257 try
258 {
George Liu0e02c322020-01-01 09:41:51 +0800259 auto service = DBusHandler().getService(logObjPath, logInterface);
George Liu83409572019-12-24 18:42:54 +0800260 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
261 auto severity =
262 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
263 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
264 Error);
265 auto method = bus.new_method_call(service.c_str(), logObjPath,
266 logInterface, "Create");
267 std::map<std::string, std::string> addlData{};
268 method.append(errorMsg, severity, addlData);
269 bus.call_noreply(method);
270 }
271 catch (const std::exception& e)
272 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600273 error(
274 "failed to make a d-bus call to create error log, ERROR={ERR_EXCEP}",
275 "ERR_EXCEP", e.what());
George Liu83409572019-12-24 18:42:54 +0800276 }
277}
278
George Liu1e44c732020-02-28 20:20:06 +0800279void DBusHandler::setDbusProperty(const DBusMapping& dBusMap,
280 const PropertyValue& value) const
281{
282 auto setDbusValue = [&dBusMap, this](const auto& variant) {
283 auto& bus = getBus();
284 auto service =
285 getService(dBusMap.objectPath.c_str(), dBusMap.interface.c_str());
286 auto method = bus.new_method_call(
287 service.c_str(), dBusMap.objectPath.c_str(), dbusProperties, "Set");
288 method.append(dBusMap.interface.c_str(), dBusMap.propertyName.c_str(),
289 variant);
290 bus.call_noreply(method);
291 };
292
293 if (dBusMap.propertyType == "uint8_t")
294 {
295 std::variant<uint8_t> v = std::get<uint8_t>(value);
296 setDbusValue(v);
297 }
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600298 else if (dBusMap.propertyType == "bool")
299 {
300 std::variant<bool> v = std::get<bool>(value);
301 setDbusValue(v);
302 }
George Liu1e44c732020-02-28 20:20:06 +0800303 else if (dBusMap.propertyType == "int16_t")
304 {
305 std::variant<int16_t> v = std::get<int16_t>(value);
306 setDbusValue(v);
307 }
308 else if (dBusMap.propertyType == "uint16_t")
309 {
310 std::variant<uint16_t> v = std::get<uint16_t>(value);
311 setDbusValue(v);
312 }
313 else if (dBusMap.propertyType == "int32_t")
314 {
315 std::variant<int32_t> v = std::get<int32_t>(value);
316 setDbusValue(v);
317 }
318 else if (dBusMap.propertyType == "uint32_t")
319 {
320 std::variant<uint32_t> v = std::get<uint32_t>(value);
321 setDbusValue(v);
322 }
323 else if (dBusMap.propertyType == "int64_t")
324 {
325 std::variant<int64_t> v = std::get<int64_t>(value);
326 setDbusValue(v);
327 }
328 else if (dBusMap.propertyType == "uint64_t")
329 {
330 std::variant<uint64_t> v = std::get<uint64_t>(value);
331 setDbusValue(v);
332 }
333 else if (dBusMap.propertyType == "double")
334 {
335 std::variant<double> v = std::get<double>(value);
336 setDbusValue(v);
337 }
338 else if (dBusMap.propertyType == "string")
339 {
340 std::variant<std::string> v = std::get<std::string>(value);
341 setDbusValue(v);
342 }
343 else
344 {
345 throw std::invalid_argument("UnSpported Dbus Type");
346 }
347}
348
John Wang9e242422020-03-05 08:37:50 +0800349PropertyValue DBusHandler::getDbusPropertyVariant(
350 const char* objPath, const char* dbusProp, const char* dbusInterface) const
351{
352 auto& bus = DBusHandler::getBus();
353 auto service = getService(objPath, dbusInterface);
354 auto method =
355 bus.new_method_call(service.c_str(), objPath, dbusProperties, "Get");
356 method.append(dbusInterface, dbusProp);
357 PropertyValue value{};
358 auto reply = bus.call(method);
359 reply.read(value);
360 return value;
361}
362
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530363PropertyValue jsonEntryToDbusVal(std::string_view type,
364 const nlohmann::json& value)
365{
366 PropertyValue propValue{};
367 if (type == "uint8_t")
368 {
369 propValue = static_cast<uint8_t>(value);
370 }
371 else if (type == "uint16_t")
372 {
373 propValue = static_cast<uint16_t>(value);
374 }
375 else if (type == "uint32_t")
376 {
377 propValue = static_cast<uint32_t>(value);
378 }
379 else if (type == "uint64_t")
380 {
381 propValue = static_cast<uint64_t>(value);
382 }
383 else if (type == "int16_t")
384 {
385 propValue = static_cast<int16_t>(value);
386 }
387 else if (type == "int32_t")
388 {
389 propValue = static_cast<int32_t>(value);
390 }
391 else if (type == "int64_t")
392 {
393 propValue = static_cast<int64_t>(value);
394 }
395 else if (type == "bool")
396 {
397 propValue = static_cast<bool>(value);
398 }
399 else if (type == "double")
400 {
401 propValue = static_cast<double>(value);
402 }
403 else if (type == "string")
404 {
405 propValue = static_cast<std::string>(value);
406 }
407 else
408 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600409 error("Unknown D-Bus property type, TYPE={OTHER_TYPE}", "OTHER_TYPE",
410 type);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530411 }
412
413 return propValue;
414}
415
Tom Joseph250c4752020-04-15 10:32:45 +0530416uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
417 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500418 uint16_t stateSetId, bool localOrRemote)
Tom Joseph250c4752020-04-15 10:32:45 +0530419{
420 uint8_t* pdrData = nullptr;
421 uint32_t pdrSize{};
422 const pldm_pdr_record* record{};
423 do
424 {
425 record = pldm_pdr_find_record_by_type(pdrRepo, PLDM_STATE_EFFECTER_PDR,
426 record, &pdrData, &pdrSize);
Sampa Misraa4a96162020-07-14 05:33:46 -0500427 if (record && (localOrRemote ^ pldm_pdr_record_is_remote(record)))
Tom Joseph250c4752020-04-15 10:32:45 +0530428 {
429 auto pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrData);
430 auto compositeEffecterCount = pdr->composite_effecter_count;
431 auto possible_states_start = pdr->possible_states;
432
433 for (auto effecters = 0x00; effecters < compositeEffecterCount;
434 effecters++)
435 {
436 auto possibleStates =
437 reinterpret_cast<state_effecter_possible_states*>(
438 possible_states_start);
439 auto setId = possibleStates->state_set_id;
440 auto possibleStateSize = possibleStates->possible_states_size;
441
442 if (entityType == pdr->entity_type &&
443 entityInstance == pdr->entity_instance &&
444 containerId == pdr->container_id && stateSetId == setId)
445 {
446 return pdr->effecter_id;
447 }
448 possible_states_start += possibleStateSize + sizeof(setId) +
449 sizeof(possibleStateSize);
450 }
451 }
452 } while (record);
453
454 return PLDM_INVALID_EFFECTER_ID;
455}
456
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800457int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
458 uint8_t sensorOffset, uint8_t eventState,
459 uint8_t previousEventState)
460{
461 try
462 {
463 auto& bus = DBusHandler::getBus();
464 auto msg = bus.new_signal("/xyz/openbmc_project/pldm",
465 "xyz.openbmc_project.PLDM.Event",
466 "StateSensorEvent");
467 msg.append(tid, sensorId, sensorOffset, eventState, previousEventState);
468
469 msg.signal_send();
470 }
Patrick Williams51330582021-10-06 12:48:56 -0500471 catch (const std::exception& e)
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800472 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600473 error("Error emitting pldm event signal:ERROR={ERR_EXCEP}", "ERR_EXCEP",
474 e.what());
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800475 return PLDM_ERROR;
476 }
477
478 return PLDM_SUCCESS;
479}
480
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500481uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
482 uint16_t entityType, uint16_t entityInstance,
483 uint16_t containerId, uint16_t stateSetId)
484{
485 auto pdrs = findStateSensorPDR(tid, entityType, stateSetId, pdrRepo);
486 for (auto pdr : pdrs)
487 {
488 auto sensorPdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
489 auto compositeSensorCount = sensorPdr->composite_sensor_count;
490 auto possible_states_start = sensorPdr->possible_states;
491
492 for (auto sensors = 0x00; sensors < compositeSensorCount; sensors++)
493 {
494 auto possibleStates =
495 reinterpret_cast<state_sensor_possible_states*>(
496 possible_states_start);
497 auto setId = possibleStates->state_set_id;
498 auto possibleStateSize = possibleStates->possible_states_size;
499 if (entityType == sensorPdr->entity_type &&
500 entityInstance == sensorPdr->entity_instance &&
501 stateSetId == setId && containerId == sensorPdr->container_id)
502 {
503 return sensorPdr->sensor_id;
504 }
505 possible_states_start +=
506 possibleStateSize + sizeof(setId) + sizeof(possibleStateSize);
507 }
508 }
509 return PLDM_INVALID_EFFECTER_ID;
510}
511
Tom Josephe5268cd2021-09-07 13:04:03 +0530512void printBuffer(bool isTx, const std::vector<uint8_t>& buffer)
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600513{
Tom Josephe5268cd2021-09-07 13:04:03 +0530514 if (!buffer.empty())
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600515 {
Tom Josephe5268cd2021-09-07 13:04:03 +0530516 if (isTx)
517 {
518 std::cout << "Tx: ";
519 }
520 else
521 {
522 std::cout << "Rx: ";
523 }
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600524 std::ostringstream tempStream;
525 for (int byte : buffer)
526 {
527 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
528 << " ";
529 }
530 std::cout << tempStream.str() << std::endl;
531 }
532}
533
Tom Joseph54922072021-06-19 02:45:46 -0700534std::string toString(const struct variable_field& var)
535{
536 if (var.ptr == nullptr || !var.length)
537 {
538 return "";
539 }
540
541 std::string str(reinterpret_cast<const char*>(var.ptr), var.length);
542 std::replace_if(
543 str.begin(), str.end(), [](const char& c) { return !isprint(c); }, ' ');
544 return str;
545}
546
George Liu872f0f62021-11-25 16:26:16 +0800547std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
548 std::string_view trimStr)
549{
550 std::vector<std::string> out;
551 size_t start;
552 size_t end = 0;
553
554 while ((start = srcStr.find_first_not_of(delim, end)) != std::string::npos)
555 {
556 end = srcStr.find(delim, start);
557 std::string_view dstStr = srcStr.substr(start, end - start);
558 if (!trimStr.empty())
559 {
560 dstStr.remove_prefix(dstStr.find_first_not_of(trimStr));
561 dstStr.remove_suffix(dstStr.size() - 1 -
562 dstStr.find_last_not_of(trimStr));
563 }
564
565 if (!dstStr.empty())
566 {
567 out.push_back(std::string(dstStr));
568 }
569 }
570
571 return out;
572}
573
Manojkiran Edaef773052021-07-29 09:29:28 +0530574std::string getCurrentSystemTime()
575{
576 using namespace std::chrono;
577 const time_point<system_clock> tp = system_clock::now();
578 std::time_t tt = system_clock::to_time_t(tp);
579 auto ms = duration_cast<microseconds>(tp.time_since_epoch()) -
580 duration_cast<seconds>(tp.time_since_epoch());
581
582 std::stringstream ss;
583 ss << std::put_time(std::localtime(&tt), "%F %Z %T.")
584 << std::to_string(ms.count());
585 return ss.str();
586}
587
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500588bool checkForFruPresence(const std::string& objPath)
589{
590 bool isPresent = false;
591 static constexpr auto presentInterface =
592 "xyz.openbmc_project.Inventory.Item";
593 static constexpr auto presentProperty = "Present";
594 try
595 {
596 auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant(
597 objPath.c_str(), presentProperty, presentInterface);
598 isPresent = std::get<bool>(propVal);
599 }
600 catch (const sdbusplus::exception::SdBusError& e)
601 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600602 error(
603 "Failed to check for FRU presence for {OBJ_PATH} ERROR = {ERR_EXCEP}",
604 "OBJ_PATH", objPath.c_str(), "ERR_EXCEP", e.what());
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500605 }
606 return isPresent;
607}
608
George Liu83409572019-12-24 18:42:54 +0800609} // namespace utils
610} // namespace pldm