blob: 9e621f1f271521d8fefee5d825c0997700e33b9b [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
Patrick Williams16c2a0a2024-08-16 15:20:59 -040030std::vector<std::vector<uint8_t>>
31 findStateEffecterPDR(uint8_t /*tid*/, uint16_t entityID,
32 uint16_t stateSetId, const pldm_pdr* repo)
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050033{
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
Patrick Williams16c2a0a2024-08-16 15:20:59 -040082std::vector<std::vector<uint8_t>>
83 findStateSensorPDR(uint8_t /*tid*/, uint16_t entityID, uint16_t stateSetId,
84 const pldm_pdr* repo)
Chicago Duan738e4d82020-05-28 16:39:19 +080085{
86 uint8_t* outData = nullptr;
87 uint32_t size{};
88 const pldm_pdr_record* record{};
89 std::vector<std::vector<uint8_t>> pdrs;
90 try
91 {
92 do
93 {
94 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_SENSOR_PDR,
95 record, &outData, &size);
96 if (record)
97 {
98 auto pdr = reinterpret_cast<pldm_state_sensor_pdr*>(outData);
99 auto compositeSensorCount = pdr->composite_sensor_count;
Chicago Duana7aacc32020-06-10 18:03:38 +0800100 auto possible_states_start = pdr->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800101
102 for (auto sensors = 0x00; sensors < compositeSensorCount;
103 sensors++)
104 {
105 auto possibleStates =
106 reinterpret_cast<state_sensor_possible_states*>(
Chicago Duana7aacc32020-06-10 18:03:38 +0800107 possible_states_start);
Chicago Duan738e4d82020-05-28 16:39:19 +0800108 auto setId = possibleStates->state_set_id;
109 auto possibleStateSize =
110 possibleStates->possible_states_size;
111
112 if (pdr->entity_type == entityID && setId == stateSetId)
113 {
114 std::vector<uint8_t> sensor_pdr(&outData[0],
115 &outData[size]);
116 pdrs.emplace_back(std::move(sensor_pdr));
117 break;
118 }
Chicago Duana7aacc32020-06-10 18:03:38 +0800119 possible_states_start += possibleStateSize + sizeof(setId) +
120 sizeof(possibleStateSize);
Chicago Duan738e4d82020-05-28 16:39:19 +0800121 }
122 }
123
124 } while (record);
125 }
126 catch (const std::exception& e)
127 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500128 error(
129 "Failed to obtain a record with entity ID '{ENTITYID}', error - {ERROR}",
130 "ENTITYID", entityID, "ERROR", e);
Chicago Duan738e4d82020-05-28 16:39:19 +0800131 }
132
133 return pdrs;
134}
135
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500136uint8_t readHostEID()
137{
138 uint8_t eid{};
Brad Bishop06052cc2021-08-16 15:17:16 -0400139 std::ifstream eidFile{HOST_EID_PATH};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500140 if (!eidFile.good())
141 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500142 error("Failed to open remote terminus EID file at path '{PATH}'",
143 "PATH", static_cast<std::string>(HOST_EID_PATH));
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500144 }
145 else
146 {
147 std::string eidStr;
148 eidFile >> eidStr;
149 if (!eidStr.empty())
150 {
151 eid = atoi(eidStr.c_str());
152 }
153 else
154 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500155 error("Remote terminus EID file was empty");
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500156 }
157 }
158
159 return eid;
160}
George Liu83409572019-12-24 18:42:54 +0800161
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000162bool isValidEID(eid mctpEid)
163{
164 if (mctpEid == MCTP_ADDR_NULL || mctpEid < MCTP_START_VALID_EID ||
165 mctpEid == MCTP_ADDR_ANY)
166 {
167 return false;
168 }
169
170 return true;
171}
172
George Liu83409572019-12-24 18:42:54 +0800173uint8_t getNumPadBytes(uint32_t data)
174{
175 uint8_t pad;
176 pad = ((data % 4) ? (4 - data % 4) : 0);
177 return pad;
178} // end getNumPadBytes
179
180bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
181 uint8_t* hour, uint8_t* min, uint8_t* sec)
182{
183 constexpr uint64_t max_data = 29991231115959;
184 constexpr uint64_t min_data = 19700101000000;
185 if (data < min_data || data > max_data)
186 {
187 return false;
188 }
189
190 *year = data / 10000000000;
191 data = data % 10000000000;
192 *month = data / 100000000;
193 data = data % 100000000;
194 *day = data / 1000000;
195 data = data % 1000000;
196 *hour = data / 10000;
197 data = data % 10000;
198 *min = data / 100;
199 *sec = data % 100;
200
201 return true;
202}
203
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400204std::optional<std::vector<set_effecter_state_field>> parseEffecterData(
205 const std::vector<uint8_t>& effecterData, uint8_t effecterCount)
George Liu83409572019-12-24 18:42:54 +0800206{
George Liuba4c1fb2020-02-05 14:13:30 +0800207 std::vector<set_effecter_state_field> stateField;
208
209 if (effecterData.size() != effecterCount * 2)
George Liu83409572019-12-24 18:42:54 +0800210 {
George Liuba4c1fb2020-02-05 14:13:30 +0800211 return std::nullopt;
George Liu83409572019-12-24 18:42:54 +0800212 }
213
George Liuba4c1fb2020-02-05 14:13:30 +0800214 for (uint8_t i = 0; i < effecterCount; ++i)
George Liu83409572019-12-24 18:42:54 +0800215 {
George Liuba4c1fb2020-02-05 14:13:30 +0800216 uint8_t set_request = effecterData[i * 2] == PLDM_REQUEST_SET
217 ? PLDM_REQUEST_SET
218 : PLDM_NO_CHANGE;
219 set_effecter_state_field filed{set_request, effecterData[i * 2 + 1]};
220 stateField.emplace_back(std::move(filed));
George Liu83409572019-12-24 18:42:54 +0800221 }
222
George Liuba4c1fb2020-02-05 14:13:30 +0800223 return std::make_optional(std::move(stateField));
George Liu83409572019-12-24 18:42:54 +0800224}
225
George Liu0e02c322020-01-01 09:41:51 +0800226std::string DBusHandler::getService(const char* path,
227 const char* interface) const
George Liu83409572019-12-24 18:42:54 +0800228{
229 using DbusInterfaceList = std::vector<std::string>;
230 std::map<std::string, std::vector<std::string>> mapperResponse;
George Liu0e02c322020-01-01 09:41:51 +0800231 auto& bus = DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +0800232
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600233 auto mapper = bus.new_method_call(ObjectMapper::default_service,
234 ObjectMapper::instance_path,
235 ObjectMapper::interface, "GetObject");
George Liudf9a6d32020-12-22 16:27:16 +0800236
237 if (interface)
238 {
239 mapper.append(path, DbusInterfaceList({interface}));
240 }
241 else
242 {
243 mapper.append(path, DbusInterfaceList({}));
244 }
George Liu83409572019-12-24 18:42:54 +0800245
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500246 auto mapperResponseMsg = bus.call(mapper, dbusTimeout);
George Liu0e02c322020-01-01 09:41:51 +0800247 mapperResponseMsg.read(mapperResponse);
George Liu83409572019-12-24 18:42:54 +0800248 return mapperResponse.begin()->first;
249}
250
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530251GetSubTreeResponse
252 DBusHandler::getSubtree(const std::string& searchPath, int depth,
253 const std::vector<std::string>& ifaceList) const
254{
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530255 auto& bus = pldm::utils::DBusHandler::getBus();
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600256 auto method = bus.new_method_call(ObjectMapper::default_service,
257 ObjectMapper::instance_path,
258 ObjectMapper::interface, "GetSubTree");
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530259 method.append(searchPath, depth, ifaceList);
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500260 auto reply = bus.call(method, dbusTimeout);
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530261 GetSubTreeResponse response;
262 reply.read(response);
263 return response;
264}
265
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500266GetSubTreePathsResponse DBusHandler::getSubTreePaths(
267 const std::string& objectPath, int depth,
268 const std::vector<std::string>& ifaceList) const
269{
270 std::vector<std::string> paths;
271 auto& bus = pldm::utils::DBusHandler::getBus();
272 auto method = bus.new_method_call(
273 ObjectMapper::default_service, ObjectMapper::instance_path,
274 ObjectMapper::interface, "GetSubTreePaths");
275 method.append(objectPath, depth, ifaceList);
276 auto reply = bus.call(method, dbusTimeout);
277
278 reply.read(paths);
279 return paths;
280}
281
Delphine CC Chiuc00a2552024-03-06 11:24:05 +0800282GetAncestorsResponse
283 DBusHandler::getAncestors(const std::string& searchPath,
284 const std::vector<std::string>& ifaceList) const
285{
286 auto& bus = pldm::utils::DBusHandler::getBus();
287 auto method = bus.new_method_call(ObjectMapper::default_service,
288 ObjectMapper::instance_path,
289 ObjectMapper::interface, "GetAncestors");
290 method.append(searchPath, ifaceList);
291 auto reply = bus.call(method, dbusTimeout);
292 GetAncestorsResponse response;
293 reply.read(response);
294 return response;
295}
296
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530297void reportError(const char* errorMsg)
George Liu83409572019-12-24 18:42:54 +0800298{
George Liu0e02c322020-01-01 09:41:51 +0800299 auto& bus = pldm::utils::DBusHandler::getBus();
Riya Dixit76f2c602024-03-28 07:34:12 -0500300 using LoggingCreate =
301 sdbusplus::client::xyz::openbmc_project::logging::Create<>;
George Liu83409572019-12-24 18:42:54 +0800302 try
303 {
George Liu83409572019-12-24 18:42:54 +0800304 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530305 auto severity =
306 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
307 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
308 Error);
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600309 auto method = bus.new_method_call(LoggingCreate::default_service,
310 LoggingCreate::instance_path,
311 LoggingCreate::interface, "Create");
312
George Liu83409572019-12-24 18:42:54 +0800313 std::map<std::string, std::string> addlData{};
314 method.append(errorMsg, severity, addlData);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000315 bus.call_noreply(method, dbusTimeout);
George Liu83409572019-12-24 18:42:54 +0800316 }
317 catch (const std::exception& e)
318 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600319 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500320 "Failed to do dbus call for creating error log for '{ERRMSG}' at path '{PATH}' and interface '{INTERFACE}', error - {ERROR}",
321 "ERRMSG", errorMsg, "PATH", LoggingCreate::instance_path,
322 "INTERFACE", LoggingCreate::interface, "ERROR", e);
George Liu83409572019-12-24 18:42:54 +0800323 }
324}
325
George Liu1e44c732020-02-28 20:20:06 +0800326void DBusHandler::setDbusProperty(const DBusMapping& dBusMap,
327 const PropertyValue& value) const
328{
329 auto setDbusValue = [&dBusMap, this](const auto& variant) {
330 auto& bus = getBus();
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400331 auto service =
332 getService(dBusMap.objectPath.c_str(), dBusMap.interface.c_str());
George Liu1e44c732020-02-28 20:20:06 +0800333 auto method = bus.new_method_call(
334 service.c_str(), dBusMap.objectPath.c_str(), dbusProperties, "Set");
335 method.append(dBusMap.interface.c_str(), dBusMap.propertyName.c_str(),
336 variant);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000337 bus.call_noreply(method, dbusTimeout);
George Liu1e44c732020-02-28 20:20:06 +0800338 };
339
340 if (dBusMap.propertyType == "uint8_t")
341 {
342 std::variant<uint8_t> v = std::get<uint8_t>(value);
343 setDbusValue(v);
344 }
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600345 else if (dBusMap.propertyType == "bool")
346 {
347 std::variant<bool> v = std::get<bool>(value);
348 setDbusValue(v);
349 }
George Liu1e44c732020-02-28 20:20:06 +0800350 else if (dBusMap.propertyType == "int16_t")
351 {
352 std::variant<int16_t> v = std::get<int16_t>(value);
353 setDbusValue(v);
354 }
355 else if (dBusMap.propertyType == "uint16_t")
356 {
357 std::variant<uint16_t> v = std::get<uint16_t>(value);
358 setDbusValue(v);
359 }
360 else if (dBusMap.propertyType == "int32_t")
361 {
362 std::variant<int32_t> v = std::get<int32_t>(value);
363 setDbusValue(v);
364 }
365 else if (dBusMap.propertyType == "uint32_t")
366 {
367 std::variant<uint32_t> v = std::get<uint32_t>(value);
368 setDbusValue(v);
369 }
370 else if (dBusMap.propertyType == "int64_t")
371 {
372 std::variant<int64_t> v = std::get<int64_t>(value);
373 setDbusValue(v);
374 }
375 else if (dBusMap.propertyType == "uint64_t")
376 {
377 std::variant<uint64_t> v = std::get<uint64_t>(value);
378 setDbusValue(v);
379 }
380 else if (dBusMap.propertyType == "double")
381 {
382 std::variant<double> v = std::get<double>(value);
383 setDbusValue(v);
384 }
385 else if (dBusMap.propertyType == "string")
386 {
387 std::variant<std::string> v = std::get<std::string>(value);
388 setDbusValue(v);
389 }
390 else
391 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500392 error("Unsupported property type '{TYPE}'", "TYPE",
393 dBusMap.propertyType);
394 throw std::invalid_argument("UnSupported Dbus Type");
George Liu1e44c732020-02-28 20:20:06 +0800395 }
396}
397
John Wang9e242422020-03-05 08:37:50 +0800398PropertyValue DBusHandler::getDbusPropertyVariant(
399 const char* objPath, const char* dbusProp, const char* dbusInterface) const
400{
401 auto& bus = DBusHandler::getBus();
402 auto service = getService(objPath, dbusInterface);
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400403 auto method =
404 bus.new_method_call(service.c_str(), objPath, dbusProperties, "Get");
John Wang9e242422020-03-05 08:37:50 +0800405 method.append(dbusInterface, dbusProp);
Patrick Williams75b8f462024-02-07 10:59:26 -0600406 return bus.call(method, dbusTimeout).unpack<PropertyValue>();
John Wang9e242422020-03-05 08:37:50 +0800407}
408
Riya Dixit754041d2024-02-20 06:15:49 -0600409ObjectValueTree DBusHandler::getManagedObj(const char* service,
410 const char* rootPath)
411{
412 auto& bus = DBusHandler::getBus();
413 auto method = bus.new_method_call(service, rootPath,
414 "org.freedesktop.DBus.ObjectManager",
415 "GetManagedObjects");
416 return bus.call(method).unpack<ObjectValueTree>();
417}
418
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400419PropertyMap DBusHandler::getDbusPropertiesVariant(
420 const char* serviceName, const char* objPath,
421 const char* dbusInterface) const
Gilbert Chen44524a52022-02-14 12:12:25 +0000422{
423 auto& bus = DBusHandler::getBus();
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400424 auto method =
425 bus.new_method_call(serviceName, objPath, dbusProperties, "GetAll");
Gilbert Chen44524a52022-02-14 12:12:25 +0000426 method.append(dbusInterface);
427 return bus.call(method, dbusTimeout).unpack<PropertyMap>();
428}
429
Unive Tien852160b2024-12-06 15:58:21 +0800430PropertyMap DBusHandler::getAll(const std::string& service,
431 const std::string& objPath,
432 const std::string& dbusInterface) const
433{
434 auto& bus = DBusHandler::getBus();
435 auto method =
436 bus.new_method_call(service.c_str(), objPath.c_str(),
437 "org.freedesktop.DBus.Properties", "GetAll");
438 method.append(dbusInterface);
439
440 auto response = bus.call(method);
441 PropertyMap result{};
442 response.read(result);
443
444 return result;
445}
446
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530447PropertyValue jsonEntryToDbusVal(std::string_view type,
448 const nlohmann::json& value)
449{
450 PropertyValue propValue{};
451 if (type == "uint8_t")
452 {
453 propValue = static_cast<uint8_t>(value);
454 }
455 else if (type == "uint16_t")
456 {
457 propValue = static_cast<uint16_t>(value);
458 }
459 else if (type == "uint32_t")
460 {
461 propValue = static_cast<uint32_t>(value);
462 }
463 else if (type == "uint64_t")
464 {
465 propValue = static_cast<uint64_t>(value);
466 }
467 else if (type == "int16_t")
468 {
469 propValue = static_cast<int16_t>(value);
470 }
471 else if (type == "int32_t")
472 {
473 propValue = static_cast<int32_t>(value);
474 }
475 else if (type == "int64_t")
476 {
477 propValue = static_cast<int64_t>(value);
478 }
479 else if (type == "bool")
480 {
481 propValue = static_cast<bool>(value);
482 }
483 else if (type == "double")
484 {
485 propValue = static_cast<double>(value);
486 }
487 else if (type == "string")
488 {
489 propValue = static_cast<std::string>(value);
490 }
491 else
492 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500493 error("Unknown D-Bus property type '{TYPE}'", "TYPE", type);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530494 }
495
496 return propValue;
497}
498
Tom Joseph250c4752020-04-15 10:32:45 +0530499uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
500 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500501 uint16_t stateSetId, bool localOrRemote)
Tom Joseph250c4752020-04-15 10:32:45 +0530502{
503 uint8_t* pdrData = nullptr;
504 uint32_t pdrSize{};
505 const pldm_pdr_record* record{};
506 do
507 {
508 record = pldm_pdr_find_record_by_type(pdrRepo, PLDM_STATE_EFFECTER_PDR,
509 record, &pdrData, &pdrSize);
Sampa Misraa4a96162020-07-14 05:33:46 -0500510 if (record && (localOrRemote ^ pldm_pdr_record_is_remote(record)))
Tom Joseph250c4752020-04-15 10:32:45 +0530511 {
512 auto pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrData);
513 auto compositeEffecterCount = pdr->composite_effecter_count;
514 auto possible_states_start = pdr->possible_states;
515
516 for (auto effecters = 0x00; effecters < compositeEffecterCount;
517 effecters++)
518 {
519 auto possibleStates =
520 reinterpret_cast<state_effecter_possible_states*>(
521 possible_states_start);
522 auto setId = possibleStates->state_set_id;
523 auto possibleStateSize = possibleStates->possible_states_size;
524
525 if (entityType == pdr->entity_type &&
526 entityInstance == pdr->entity_instance &&
527 containerId == pdr->container_id && stateSetId == setId)
528 {
529 return pdr->effecter_id;
530 }
531 possible_states_start += possibleStateSize + sizeof(setId) +
532 sizeof(possibleStateSize);
533 }
534 }
535 } while (record);
536
537 return PLDM_INVALID_EFFECTER_ID;
538}
539
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800540int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
541 uint8_t sensorOffset, uint8_t eventState,
542 uint8_t previousEventState)
543{
544 try
545 {
546 auto& bus = DBusHandler::getBus();
547 auto msg = bus.new_signal("/xyz/openbmc_project/pldm",
548 "xyz.openbmc_project.PLDM.Event",
549 "StateSensorEvent");
550 msg.append(tid, sensorId, sensorOffset, eventState, previousEventState);
551
552 msg.signal_send();
553 }
Patrick Williams51330582021-10-06 12:48:56 -0500554 catch (const std::exception& e)
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800555 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500556 error("Failed to emit pldm event signal, error - {ERROR}", "ERROR", e);
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800557 return PLDM_ERROR;
558 }
559
560 return PLDM_SUCCESS;
561}
562
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500563uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
564 uint16_t entityType, uint16_t entityInstance,
565 uint16_t containerId, uint16_t stateSetId)
566{
567 auto pdrs = findStateSensorPDR(tid, entityType, stateSetId, pdrRepo);
568 for (auto pdr : pdrs)
569 {
570 auto sensorPdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
571 auto compositeSensorCount = sensorPdr->composite_sensor_count;
572 auto possible_states_start = sensorPdr->possible_states;
573
574 for (auto sensors = 0x00; sensors < compositeSensorCount; sensors++)
575 {
576 auto possibleStates =
577 reinterpret_cast<state_sensor_possible_states*>(
578 possible_states_start);
579 auto setId = possibleStates->state_set_id;
580 auto possibleStateSize = possibleStates->possible_states_size;
581 if (entityType == sensorPdr->entity_type &&
582 entityInstance == sensorPdr->entity_instance &&
583 stateSetId == setId && containerId == sensorPdr->container_id)
584 {
585 return sensorPdr->sensor_id;
586 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400587 possible_states_start +=
588 possibleStateSize + sizeof(setId) + sizeof(possibleStateSize);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500589 }
590 }
591 return PLDM_INVALID_EFFECTER_ID;
592}
593
Tom Josephe5268cd2021-09-07 13:04:03 +0530594void printBuffer(bool isTx, const std::vector<uint8_t>& buffer)
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600595{
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530596 if (buffer.empty())
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600597 {
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530598 return;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600599 }
Manojkiran Edacd4cd452024-04-23 08:53:17 +0530600
601 std::cout << (isTx ? "Tx: " : "Rx: ");
602
603 std::ranges::for_each(buffer, [](uint8_t byte) {
604 std::cout << std::format("{:02x} ", byte);
605 });
606
607 std::cout << std::endl;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600608}
609
Tom Joseph54922072021-06-19 02:45:46 -0700610std::string toString(const struct variable_field& var)
611{
612 if (var.ptr == nullptr || !var.length)
613 {
614 return "";
615 }
616
617 std::string str(reinterpret_cast<const char*>(var.ptr), var.length);
618 std::replace_if(
619 str.begin(), str.end(), [](const char& c) { return !isprint(c); }, ' ');
620 return str;
621}
622
George Liu872f0f62021-11-25 16:26:16 +0800623std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
624 std::string_view trimStr)
625{
626 std::vector<std::string> out;
627 size_t start;
628 size_t end = 0;
629
630 while ((start = srcStr.find_first_not_of(delim, end)) != std::string::npos)
631 {
632 end = srcStr.find(delim, start);
633 std::string_view dstStr = srcStr.substr(start, end - start);
634 if (!trimStr.empty())
635 {
636 dstStr.remove_prefix(dstStr.find_first_not_of(trimStr));
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400637 dstStr.remove_suffix(
638 dstStr.size() - 1 - dstStr.find_last_not_of(trimStr));
George Liu872f0f62021-11-25 16:26:16 +0800639 }
640
641 if (!dstStr.empty())
642 {
643 out.push_back(std::string(dstStr));
644 }
645 }
646
647 return out;
648}
649
Manojkiran Edaef773052021-07-29 09:29:28 +0530650std::string getCurrentSystemTime()
651{
Manojkiran Eda09a89822024-04-24 11:02:44 +0530652 const auto zonedTime{std::chrono::zoned_time{
653 std::chrono::current_zone(), std::chrono::system_clock::now()}};
654 return std::format("{:%F %Z %T}", zonedTime);
Manojkiran Edaef773052021-07-29 09:29:28 +0530655}
656
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500657bool checkForFruPresence(const std::string& objPath)
658{
659 bool isPresent = false;
660 static constexpr auto presentInterface =
661 "xyz.openbmc_project.Inventory.Item";
662 static constexpr auto presentProperty = "Present";
663 try
664 {
665 auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant(
666 objPath.c_str(), presentProperty, presentInterface);
667 isPresent = std::get<bool>(propVal);
668 }
669 catch (const sdbusplus::exception::SdBusError& e)
670 {
Riya Dixit76f2c602024-03-28 07:34:12 -0500671 error("Failed to check for FRU presence at {PATH}, error - {ERROR}",
672 "PATH", objPath, "ERROR", e);
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500673 }
674 return isPresent;
675}
676
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600677bool checkIfLogicalBitSet(const uint16_t& containerId)
678{
679 return !(containerId & 0x8000);
680}
681
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500682void setFruPresence(const std::string& fruObjPath, bool present)
683{
684 pldm::utils::PropertyValue value{present};
685 pldm::utils::DBusMapping dbusMapping;
686 dbusMapping.objectPath = fruObjPath;
687 dbusMapping.interface = "xyz.openbmc_project.Inventory.Item";
688 dbusMapping.propertyName = "Present";
689 dbusMapping.propertyType = "bool";
690 try
691 {
692 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
693 }
694 catch (const std::exception& e)
695 {
696 error(
Riya Dixit76f2c602024-03-28 07:34:12 -0500697 "Failed to set the present property on path '{PATH}', error - {ERROR}.",
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500698 "PATH", fruObjPath, "ERROR", e);
699 }
700}
701
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000702std::string_view trimNameForDbus(std::string& name)
703{
704 std::replace(name.begin(), name.end(), ' ', '_');
705 auto nullTerminatorPos = name.find('\0');
706 if (nullTerminatorPos != std::string::npos)
707 {
708 name.erase(nullTerminatorPos);
709 }
710 return name;
711}
Thu Nguyena34a64b2022-03-31 08:56:39 +0700712
713bool dbusPropValuesToDouble(const std::string_view& type,
714 const pldm::utils::PropertyValue& value,
715 double* doubleValue)
716{
717 if (!dbusValueNumericTypeNames.contains(type))
718 {
719 return false;
720 }
721
722 if (!doubleValue)
723 {
724 return false;
725 }
726
727 try
728 {
729 if (type == "uint8_t")
730 {
731 *doubleValue = static_cast<double>(std::get<uint8_t>(value));
732 }
733 else if (type == "int16_t")
734 {
735 *doubleValue = static_cast<double>(std::get<int16_t>(value));
736 }
737 else if (type == "uint16_t")
738 {
739 *doubleValue = static_cast<double>(std::get<uint16_t>(value));
740 }
741 else if (type == "int32_t")
742 {
743 *doubleValue = static_cast<double>(std::get<int32_t>(value));
744 }
745 else if (type == "uint32_t")
746 {
747 *doubleValue = static_cast<double>(std::get<uint32_t>(value));
748 }
749 else if (type == "int64_t")
750 {
751 *doubleValue = static_cast<double>(std::get<int64_t>(value));
752 }
753 else if (type == "uint64_t")
754 {
755 *doubleValue = static_cast<double>(std::get<uint64_t>(value));
756 }
757 else if (type == "double")
758 {
759 *doubleValue = static_cast<double>(std::get<double>(value));
760 }
761 else
762 {
763 return false;
764 }
765 }
766 catch (const std::exception& e)
767 {
768 return false;
769 }
770
771 return true;
772}
George Liu83409572019-12-24 18:42:54 +0800773} // namespace utils
774} // namespace pldm