blob: b4bcfcb44070c0fb213d3fbf76fa64ceaf92a897 [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>
8
Tom Joseph54922072021-06-19 02:45:46 -07009#include <algorithm>
George Liu83409572019-12-24 18:42:54 +080010#include <array>
Tom Joseph54922072021-06-19 02:45:46 -070011#include <cctype>
George Liu83409572019-12-24 18:42:54 +080012#include <ctime>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050013#include <fstream>
George Liu83409572019-12-24 18:42:54 +080014#include <iostream>
15#include <map>
George Liu83409572019-12-24 18:42:54 +080016#include <stdexcept>
17#include <string>
18#include <vector>
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050019
Riya Dixit49cfb132023-03-02 04:26:53 -060020PHOSPHOR_LOG2_USING;
21
George Liu83409572019-12-24 18:42:54 +080022namespace pldm
23{
24namespace utils
25{
George Liu83409572019-12-24 18:42:54 +080026constexpr auto mapperBusName = "xyz.openbmc_project.ObjectMapper";
27constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
28constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper";
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050029
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 Dixit49cfb132023-03-02 04:26:53 -060077 error(" Failed to obtain a record. ERROR = {ERR_EXCEP}", "ERR_EXCEP",
78 e.what());
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050079 }
80
81 return pdrs;
82}
83
Chicago Duan738e4d82020-05-28 16:39:19 +080084std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t /*tid*/,
85 uint16_t entityID,
86 uint16_t stateSetId,
87 const pldm_pdr* repo)
88{
89 uint8_t* outData = nullptr;
90 uint32_t size{};
91 const pldm_pdr_record* record{};
92 std::vector<std::vector<uint8_t>> pdrs;
93 try
94 {
95 do
96 {
97 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_SENSOR_PDR,
98 record, &outData, &size);
99 if (record)
100 {
101 auto pdr = reinterpret_cast<pldm_state_sensor_pdr*>(outData);
102 auto compositeSensorCount = pdr->composite_sensor_count;
Chicago Duana7aacc32020-06-10 18:03:38 +0800103 auto possible_states_start = pdr->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800104
105 for (auto sensors = 0x00; sensors < compositeSensorCount;
106 sensors++)
107 {
108 auto possibleStates =
109 reinterpret_cast<state_sensor_possible_states*>(
Chicago Duana7aacc32020-06-10 18:03:38 +0800110 possible_states_start);
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 Dixit49cfb132023-03-02 04:26:53 -0600131 error(" Failed to obtain a record. ERROR = {ERR_EXCEP}", "ERR_EXCEP",
132 e.what());
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 Dixit49cfb132023-03-02 04:26:53 -0600144 error("Could not open host EID file: {HOST_PATH}", "HOST_PATH",
145 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 Dixit49cfb132023-03-02 04:26:53 -0600157 error("Host 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
164uint8_t getNumPadBytes(uint32_t data)
165{
166 uint8_t pad;
167 pad = ((data % 4) ? (4 - data % 4) : 0);
168 return pad;
169} // end getNumPadBytes
170
171bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
172 uint8_t* hour, uint8_t* min, uint8_t* sec)
173{
174 constexpr uint64_t max_data = 29991231115959;
175 constexpr uint64_t min_data = 19700101000000;
176 if (data < min_data || data > max_data)
177 {
178 return false;
179 }
180
181 *year = data / 10000000000;
182 data = data % 10000000000;
183 *month = data / 100000000;
184 data = data % 100000000;
185 *day = data / 1000000;
186 data = data % 1000000;
187 *hour = data / 10000;
188 data = data % 10000;
189 *min = data / 100;
190 *sec = data % 100;
191
192 return true;
193}
194
George Liuba4c1fb2020-02-05 14:13:30 +0800195std::optional<std::vector<set_effecter_state_field>>
196 parseEffecterData(const std::vector<uint8_t>& effecterData,
197 uint8_t effecterCount)
George Liu83409572019-12-24 18:42:54 +0800198{
George Liuba4c1fb2020-02-05 14:13:30 +0800199 std::vector<set_effecter_state_field> stateField;
200
201 if (effecterData.size() != effecterCount * 2)
George Liu83409572019-12-24 18:42:54 +0800202 {
George Liuba4c1fb2020-02-05 14:13:30 +0800203 return std::nullopt;
George Liu83409572019-12-24 18:42:54 +0800204 }
205
George Liuba4c1fb2020-02-05 14:13:30 +0800206 for (uint8_t i = 0; i < effecterCount; ++i)
George Liu83409572019-12-24 18:42:54 +0800207 {
George Liuba4c1fb2020-02-05 14:13:30 +0800208 uint8_t set_request = effecterData[i * 2] == PLDM_REQUEST_SET
209 ? PLDM_REQUEST_SET
210 : PLDM_NO_CHANGE;
211 set_effecter_state_field filed{set_request, effecterData[i * 2 + 1]};
212 stateField.emplace_back(std::move(filed));
George Liu83409572019-12-24 18:42:54 +0800213 }
214
George Liuba4c1fb2020-02-05 14:13:30 +0800215 return std::make_optional(std::move(stateField));
George Liu83409572019-12-24 18:42:54 +0800216}
217
George Liu0e02c322020-01-01 09:41:51 +0800218std::string DBusHandler::getService(const char* path,
219 const char* interface) const
George Liu83409572019-12-24 18:42:54 +0800220{
221 using DbusInterfaceList = std::vector<std::string>;
222 std::map<std::string, std::vector<std::string>> mapperResponse;
George Liu0e02c322020-01-01 09:41:51 +0800223 auto& bus = DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +0800224
George Liu0e02c322020-01-01 09:41:51 +0800225 auto mapper = bus.new_method_call(mapperBusName, mapperPath,
226 mapperInterface, "GetObject");
227 mapper.append(path, DbusInterfaceList({interface}));
George Liu83409572019-12-24 18:42:54 +0800228
vkaverap@in.ibm.com9138c202023-05-19 07:50:47 -0500229 auto mapperResponseMsg = bus.call(
230 mapper,
231 std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
George Liu0e02c322020-01-01 09:41:51 +0800232 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);
vkaverap@in.ibm.com9138c202023-05-19 07:50:47 -0500244 auto reply = bus.call(
245 method,
246 std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530247 GetSubTreeResponse response;
248 reply.read(response);
249 return response;
250}
251
George Liu83409572019-12-24 18:42:54 +0800252void reportError(const char* errorMsg)
253{
254 static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
255 static constexpr auto logInterface = "xyz.openbmc_project.Logging.Create";
256
George Liu0e02c322020-01-01 09:41:51 +0800257 auto& bus = pldm::utils::DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +0800258
259 try
260 {
George Liu0e02c322020-01-01 09:41:51 +0800261 auto service = DBusHandler().getService(logObjPath, logInterface);
George Liu83409572019-12-24 18:42:54 +0800262 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
263 auto severity =
264 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
265 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
266 Error);
267 auto method = bus.new_method_call(service.c_str(), logObjPath,
268 logInterface, "Create");
269 std::map<std::string, std::string> addlData{};
270 method.append(errorMsg, severity, addlData);
271 bus.call_noreply(method);
272 }
273 catch (const std::exception& e)
274 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600275 error(
276 "failed to make a d-bus call to create error log, ERROR={ERR_EXCEP}",
277 "ERR_EXCEP", e.what());
George Liu83409572019-12-24 18:42:54 +0800278 }
279}
280
George Liu1e44c732020-02-28 20:20:06 +0800281void DBusHandler::setDbusProperty(const DBusMapping& dBusMap,
282 const PropertyValue& value) const
283{
284 auto setDbusValue = [&dBusMap, this](const auto& variant) {
285 auto& bus = getBus();
Patrick Williams6da4f912023-05-10 07:50:53 -0500286 auto service = getService(dBusMap.objectPath.c_str(),
287 dBusMap.interface.c_str());
George Liu1e44c732020-02-28 20:20:06 +0800288 auto method = bus.new_method_call(
289 service.c_str(), dBusMap.objectPath.c_str(), dbusProperties, "Set");
290 method.append(dBusMap.interface.c_str(), dBusMap.propertyName.c_str(),
291 variant);
292 bus.call_noreply(method);
293 };
294
295 if (dBusMap.propertyType == "uint8_t")
296 {
297 std::variant<uint8_t> v = std::get<uint8_t>(value);
298 setDbusValue(v);
299 }
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600300 else if (dBusMap.propertyType == "bool")
301 {
302 std::variant<bool> v = std::get<bool>(value);
303 setDbusValue(v);
304 }
George Liu1e44c732020-02-28 20:20:06 +0800305 else if (dBusMap.propertyType == "int16_t")
306 {
307 std::variant<int16_t> v = std::get<int16_t>(value);
308 setDbusValue(v);
309 }
310 else if (dBusMap.propertyType == "uint16_t")
311 {
312 std::variant<uint16_t> v = std::get<uint16_t>(value);
313 setDbusValue(v);
314 }
315 else if (dBusMap.propertyType == "int32_t")
316 {
317 std::variant<int32_t> v = std::get<int32_t>(value);
318 setDbusValue(v);
319 }
320 else if (dBusMap.propertyType == "uint32_t")
321 {
322 std::variant<uint32_t> v = std::get<uint32_t>(value);
323 setDbusValue(v);
324 }
325 else if (dBusMap.propertyType == "int64_t")
326 {
327 std::variant<int64_t> v = std::get<int64_t>(value);
328 setDbusValue(v);
329 }
330 else if (dBusMap.propertyType == "uint64_t")
331 {
332 std::variant<uint64_t> v = std::get<uint64_t>(value);
333 setDbusValue(v);
334 }
335 else if (dBusMap.propertyType == "double")
336 {
337 std::variant<double> v = std::get<double>(value);
338 setDbusValue(v);
339 }
340 else if (dBusMap.propertyType == "string")
341 {
342 std::variant<std::string> v = std::get<std::string>(value);
343 setDbusValue(v);
344 }
345 else
346 {
347 throw std::invalid_argument("UnSpported Dbus Type");
348 }
349}
350
John Wang9e242422020-03-05 08:37:50 +0800351PropertyValue DBusHandler::getDbusPropertyVariant(
352 const char* objPath, const char* dbusProp, const char* dbusInterface) const
353{
354 auto& bus = DBusHandler::getBus();
355 auto service = getService(objPath, dbusInterface);
Patrick Williams6da4f912023-05-10 07:50:53 -0500356 auto method = bus.new_method_call(service.c_str(), objPath, dbusProperties,
357 "Get");
John Wang9e242422020-03-05 08:37:50 +0800358 method.append(dbusInterface, dbusProp);
359 PropertyValue value{};
vkaverap@in.ibm.com9138c202023-05-19 07:50:47 -0500360 auto reply = bus.call(
361 method,
362 std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
John Wang9e242422020-03-05 08:37:50 +0800363 reply.read(value);
364 return value;
365}
366
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530367PropertyValue jsonEntryToDbusVal(std::string_view type,
368 const nlohmann::json& value)
369{
370 PropertyValue propValue{};
371 if (type == "uint8_t")
372 {
373 propValue = static_cast<uint8_t>(value);
374 }
375 else if (type == "uint16_t")
376 {
377 propValue = static_cast<uint16_t>(value);
378 }
379 else if (type == "uint32_t")
380 {
381 propValue = static_cast<uint32_t>(value);
382 }
383 else if (type == "uint64_t")
384 {
385 propValue = static_cast<uint64_t>(value);
386 }
387 else if (type == "int16_t")
388 {
389 propValue = static_cast<int16_t>(value);
390 }
391 else if (type == "int32_t")
392 {
393 propValue = static_cast<int32_t>(value);
394 }
395 else if (type == "int64_t")
396 {
397 propValue = static_cast<int64_t>(value);
398 }
399 else if (type == "bool")
400 {
401 propValue = static_cast<bool>(value);
402 }
403 else if (type == "double")
404 {
405 propValue = static_cast<double>(value);
406 }
407 else if (type == "string")
408 {
409 propValue = static_cast<std::string>(value);
410 }
411 else
412 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600413 error("Unknown D-Bus property type, TYPE={OTHER_TYPE}", "OTHER_TYPE",
414 type);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530415 }
416
417 return propValue;
418}
419
Tom Joseph250c4752020-04-15 10:32:45 +0530420uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
421 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500422 uint16_t stateSetId, bool localOrRemote)
Tom Joseph250c4752020-04-15 10:32:45 +0530423{
424 uint8_t* pdrData = nullptr;
425 uint32_t pdrSize{};
426 const pldm_pdr_record* record{};
427 do
428 {
429 record = pldm_pdr_find_record_by_type(pdrRepo, PLDM_STATE_EFFECTER_PDR,
430 record, &pdrData, &pdrSize);
Sampa Misraa4a96162020-07-14 05:33:46 -0500431 if (record && (localOrRemote ^ pldm_pdr_record_is_remote(record)))
Tom Joseph250c4752020-04-15 10:32:45 +0530432 {
433 auto pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrData);
434 auto compositeEffecterCount = pdr->composite_effecter_count;
435 auto possible_states_start = pdr->possible_states;
436
437 for (auto effecters = 0x00; effecters < compositeEffecterCount;
438 effecters++)
439 {
440 auto possibleStates =
441 reinterpret_cast<state_effecter_possible_states*>(
442 possible_states_start);
443 auto setId = possibleStates->state_set_id;
444 auto possibleStateSize = possibleStates->possible_states_size;
445
446 if (entityType == pdr->entity_type &&
447 entityInstance == pdr->entity_instance &&
448 containerId == pdr->container_id && stateSetId == setId)
449 {
450 return pdr->effecter_id;
451 }
452 possible_states_start += possibleStateSize + sizeof(setId) +
453 sizeof(possibleStateSize);
454 }
455 }
456 } while (record);
457
458 return PLDM_INVALID_EFFECTER_ID;
459}
460
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800461int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
462 uint8_t sensorOffset, uint8_t eventState,
463 uint8_t previousEventState)
464{
465 try
466 {
467 auto& bus = DBusHandler::getBus();
468 auto msg = bus.new_signal("/xyz/openbmc_project/pldm",
469 "xyz.openbmc_project.PLDM.Event",
470 "StateSensorEvent");
471 msg.append(tid, sensorId, sensorOffset, eventState, previousEventState);
472
473 msg.signal_send();
474 }
Patrick Williams51330582021-10-06 12:48:56 -0500475 catch (const std::exception& e)
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800476 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600477 error("Error emitting pldm event signal:ERROR={ERR_EXCEP}", "ERR_EXCEP",
478 e.what());
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800479 return PLDM_ERROR;
480 }
481
482 return PLDM_SUCCESS;
483}
484
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500485uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
486 uint16_t entityType, uint16_t entityInstance,
487 uint16_t containerId, uint16_t stateSetId)
488{
489 auto pdrs = findStateSensorPDR(tid, entityType, stateSetId, pdrRepo);
490 for (auto pdr : pdrs)
491 {
492 auto sensorPdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
493 auto compositeSensorCount = sensorPdr->composite_sensor_count;
494 auto possible_states_start = sensorPdr->possible_states;
495
496 for (auto sensors = 0x00; sensors < compositeSensorCount; sensors++)
497 {
498 auto possibleStates =
499 reinterpret_cast<state_sensor_possible_states*>(
500 possible_states_start);
501 auto setId = possibleStates->state_set_id;
502 auto possibleStateSize = possibleStates->possible_states_size;
503 if (entityType == sensorPdr->entity_type &&
504 entityInstance == sensorPdr->entity_instance &&
505 stateSetId == setId && containerId == sensorPdr->container_id)
506 {
507 return sensorPdr->sensor_id;
508 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500509 possible_states_start += possibleStateSize + sizeof(setId) +
510 sizeof(possibleStateSize);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500511 }
512 }
513 return PLDM_INVALID_EFFECTER_ID;
514}
515
Tom Josephe5268cd2021-09-07 13:04:03 +0530516void printBuffer(bool isTx, const std::vector<uint8_t>& buffer)
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600517{
Tom Josephe5268cd2021-09-07 13:04:03 +0530518 if (!buffer.empty())
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600519 {
Tom Josephe5268cd2021-09-07 13:04:03 +0530520 if (isTx)
521 {
522 std::cout << "Tx: ";
523 }
524 else
525 {
526 std::cout << "Rx: ";
527 }
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600528 std::ostringstream tempStream;
529 for (int byte : buffer)
530 {
531 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
532 << " ";
533 }
534 std::cout << tempStream.str() << std::endl;
535 }
536}
537
Tom Joseph54922072021-06-19 02:45:46 -0700538std::string toString(const struct variable_field& var)
539{
540 if (var.ptr == nullptr || !var.length)
541 {
542 return "";
543 }
544
545 std::string str(reinterpret_cast<const char*>(var.ptr), var.length);
546 std::replace_if(
547 str.begin(), str.end(), [](const char& c) { return !isprint(c); }, ' ');
548 return str;
549}
550
George Liu872f0f62021-11-25 16:26:16 +0800551std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
552 std::string_view trimStr)
553{
554 std::vector<std::string> out;
555 size_t start;
556 size_t end = 0;
557
558 while ((start = srcStr.find_first_not_of(delim, end)) != std::string::npos)
559 {
560 end = srcStr.find(delim, start);
561 std::string_view dstStr = srcStr.substr(start, end - start);
562 if (!trimStr.empty())
563 {
564 dstStr.remove_prefix(dstStr.find_first_not_of(trimStr));
565 dstStr.remove_suffix(dstStr.size() - 1 -
566 dstStr.find_last_not_of(trimStr));
567 }
568
569 if (!dstStr.empty())
570 {
571 out.push_back(std::string(dstStr));
572 }
573 }
574
575 return out;
576}
577
Manojkiran Edaef773052021-07-29 09:29:28 +0530578std::string getCurrentSystemTime()
579{
580 using namespace std::chrono;
581 const time_point<system_clock> tp = system_clock::now();
582 std::time_t tt = system_clock::to_time_t(tp);
583 auto ms = duration_cast<microseconds>(tp.time_since_epoch()) -
584 duration_cast<seconds>(tp.time_since_epoch());
585
586 std::stringstream ss;
587 ss << std::put_time(std::localtime(&tt), "%F %Z %T.")
588 << std::to_string(ms.count());
589 return ss.str();
590}
591
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500592bool checkForFruPresence(const std::string& objPath)
593{
594 bool isPresent = false;
595 static constexpr auto presentInterface =
596 "xyz.openbmc_project.Inventory.Item";
597 static constexpr auto presentProperty = "Present";
598 try
599 {
600 auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant(
601 objPath.c_str(), presentProperty, presentInterface);
602 isPresent = std::get<bool>(propVal);
603 }
604 catch (const sdbusplus::exception::SdBusError& e)
605 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600606 error(
607 "Failed to check for FRU presence for {OBJ_PATH} ERROR = {ERR_EXCEP}",
608 "OBJ_PATH", objPath.c_str(), "ERR_EXCEP", e.what());
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500609 }
610 return isPresent;
611}
612
George Liu83409572019-12-24 18:42:54 +0800613} // namespace utils
614} // namespace pldm