blob: eee9f437cdac8c251d164b170e67ae594ff6c939 [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
29using ObjectMapper = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050030
George Liudf9a6d32020-12-22 16:27:16 +080031Entities getParentEntites(const EntityAssociations& entityAssoc)
32{
33 Entities parents{};
34 for (const auto& et : entityAssoc)
35 {
36 parents.push_back(et[0]);
37 }
38
39 bool found = false;
40 for (auto it = parents.begin(); it != parents.end();
41 it = found ? parents.erase(it) : std::next(it))
42 {
43 uint16_t parent_contained_id =
44 pldm_entity_node_get_remote_container_id(*it);
45 found = false;
46 for (const auto& evs : entityAssoc)
47 {
48 for (size_t i = 1; i < evs.size() && !found; i++)
49 {
50 uint16_t node_contained_id =
51 pldm_entity_node_get_remote_container_id(evs[i]);
52
53 pldm_entity parent_entity = pldm_entity_extract(*it);
54 pldm_entity node_entity = pldm_entity_extract(evs[i]);
55
56 if (node_entity.entity_type == parent_entity.entity_type &&
57 node_entity.entity_instance_num ==
58 parent_entity.entity_instance_num &&
59 node_contained_id == parent_contained_id)
60 {
61 found = true;
62 }
63 }
64 if (found)
65 {
66 break;
67 }
68 }
69 }
70
71 return parents;
72}
73
74void addObjectPathEntityAssociations(const EntityAssociations& entityAssoc,
75 pldm_entity_node* entity,
76 const fs::path& path,
77 ObjectPathMaps& objPathMap)
78{
79 if (entity == nullptr)
80 {
81 return;
82 }
83
84 bool found = false;
85 pldm_entity node_entity = pldm_entity_extract(entity);
86 if (!entityMaps.contains(node_entity.entity_type))
87 {
88 lg2::info(
89 "{ENTITY_TYPE} Entity fetched from remote PLDM terminal does not exist.",
90 "ENTITY_TYPE", (int)node_entity.entity_type);
91 return;
92 }
93
94 std::string entityName = entityMaps.at(node_entity.entity_type);
95 for (const auto& ev : entityAssoc)
96 {
97 pldm_entity ev_entity = pldm_entity_extract(ev[0]);
98 if (ev_entity.entity_instance_num == node_entity.entity_instance_num &&
99 ev_entity.entity_type == node_entity.entity_type)
100 {
101 uint16_t node_contained_id =
102 pldm_entity_node_get_remote_container_id(ev[0]);
103 uint16_t entity_contained_id =
104 pldm_entity_node_get_remote_container_id(entity);
105
106 if (node_contained_id != entity_contained_id)
107 {
108 continue;
109 }
110
111 fs::path p = path / fs::path{entityName +
112 std::to_string(
113 node_entity.entity_instance_num)};
114 std::string entity_path = p.string();
115 // If the entity obtained from the remote PLDM terminal is not in
116 // the MAP, or there is no auxiliary name PDR, add it directly.
117 // Otherwise, check whether the DBus service of entity_path exists,
118 // and overwrite the entity if it does not exist.
119 if (!objPathMap.contains(entity_path))
120 {
121 objPathMap[entity_path] = entity;
122 }
123 else
124 {
125 try
126 {
127 pldm::utils::DBusHandler().getService(entity_path.c_str(),
128 nullptr);
129 }
130 catch (const std::exception& e)
131 {
132 objPathMap[entity_path] = entity;
133 }
134 }
135
136 for (size_t i = 1; i < ev.size(); i++)
137 {
138 addObjectPathEntityAssociations(entityAssoc, ev[i], p,
139 objPathMap);
140 }
141 found = true;
142 }
143 }
144
145 if (!found)
146 {
147 std::string dbusPath =
148 path / fs::path{entityName +
149 std::to_string(node_entity.entity_instance_num)};
150
151 try
152 {
153 pldm::utils::DBusHandler().getService(dbusPath.c_str(), nullptr);
154 }
155 catch (const std::exception& e)
156 {
157 objPathMap[dbusPath] = entity;
158 }
159 }
160}
161
162void updateEntityAssociation(const EntityAssociations& entityAssoc,
163 pldm_entity_association_tree* entityTree,
164 ObjectPathMaps& objPathMap)
165{
166 std::vector<pldm_entity_node*> parentsEntity =
167 getParentEntites(entityAssoc);
168 for (const auto& entity : parentsEntity)
169 {
170 fs::path path{"/xyz/openbmc_project/inventory"};
171 std::deque<std::string> paths{};
172 pldm_entity node_entity = pldm_entity_extract(entity);
173 auto node = pldm_entity_association_tree_find_with_locality(
174 entityTree, &node_entity, false);
175 if (!node)
176 {
177 continue;
178 }
179
180 bool found = true;
181 while (node)
182 {
183 if (!pldm_entity_is_exist_parent(node))
184 {
185 break;
186 }
187
188 pldm_entity parent = pldm_entity_get_parent(node);
189 try
190 {
191 paths.push_back(entityMaps.at(parent.entity_type) +
192 std::to_string(parent.entity_instance_num));
193 }
194 catch (const std::exception& e)
195 {
196 lg2::error(
197 "Parent entity not found in the entityMaps, type: {ENTITY_TYPE}, num: {NUM}, e: {ERROR}",
198 "ENTITY_TYPE", (int)parent.entity_type, "NUM",
199 (int)parent.entity_instance_num, "ERROR", e);
200 found = false;
201 break;
202 }
203
204 node = pldm_entity_association_tree_find_with_locality(
205 entityTree, &parent, false);
206 }
207
208 if (!found)
209 {
210 continue;
211 }
212
213 while (!paths.empty())
214 {
215 path = path / fs::path{paths.back()};
216 paths.pop_back();
217 }
218
219 addObjectPathEntityAssociations(entityAssoc, entity, path, objPathMap);
220 }
221}
222
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500223std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t /*tid*/,
224 uint16_t entityID,
225 uint16_t stateSetId,
226 const pldm_pdr* repo)
227{
228 uint8_t* outData = nullptr;
229 uint32_t size{};
230 const pldm_pdr_record* record{};
231 std::vector<std::vector<uint8_t>> pdrs;
232 try
233 {
234 do
235 {
236 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_EFFECTER_PDR,
237 record, &outData, &size);
238 if (record)
239 {
240 auto pdr = reinterpret_cast<pldm_state_effecter_pdr*>(outData);
241 auto compositeEffecterCount = pdr->composite_effecter_count;
Chicago Duana7aacc32020-06-10 18:03:38 +0800242 auto possible_states_start = pdr->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500243
244 for (auto effecters = 0x00; effecters < compositeEffecterCount;
245 effecters++)
246 {
247 auto possibleStates =
248 reinterpret_cast<state_effecter_possible_states*>(
Chicago Duana7aacc32020-06-10 18:03:38 +0800249 possible_states_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500250 auto setId = possibleStates->state_set_id;
251 auto possibleStateSize =
252 possibleStates->possible_states_size;
253
254 if (pdr->entity_type == entityID && setId == stateSetId)
255 {
256 std::vector<uint8_t> effecter_pdr(&outData[0],
257 &outData[size]);
258 pdrs.emplace_back(std::move(effecter_pdr));
259 break;
260 }
Chicago Duana7aacc32020-06-10 18:03:38 +0800261 possible_states_start += possibleStateSize + sizeof(setId) +
262 sizeof(possibleStateSize);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500263 }
264 }
265
266 } while (record);
267 }
268 catch (const std::exception& e)
269 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600270 error(" Failed to obtain a record. ERROR = {ERR_EXCEP}", "ERR_EXCEP",
271 e.what());
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500272 }
273
274 return pdrs;
275}
276
Chicago Duan738e4d82020-05-28 16:39:19 +0800277std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t /*tid*/,
278 uint16_t entityID,
279 uint16_t stateSetId,
280 const pldm_pdr* repo)
281{
282 uint8_t* outData = nullptr;
283 uint32_t size{};
284 const pldm_pdr_record* record{};
285 std::vector<std::vector<uint8_t>> pdrs;
286 try
287 {
288 do
289 {
290 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_SENSOR_PDR,
291 record, &outData, &size);
292 if (record)
293 {
294 auto pdr = reinterpret_cast<pldm_state_sensor_pdr*>(outData);
295 auto compositeSensorCount = pdr->composite_sensor_count;
Chicago Duana7aacc32020-06-10 18:03:38 +0800296 auto possible_states_start = pdr->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800297
298 for (auto sensors = 0x00; sensors < compositeSensorCount;
299 sensors++)
300 {
301 auto possibleStates =
302 reinterpret_cast<state_sensor_possible_states*>(
Chicago Duana7aacc32020-06-10 18:03:38 +0800303 possible_states_start);
Chicago Duan738e4d82020-05-28 16:39:19 +0800304 auto setId = possibleStates->state_set_id;
305 auto possibleStateSize =
306 possibleStates->possible_states_size;
307
308 if (pdr->entity_type == entityID && setId == stateSetId)
309 {
310 std::vector<uint8_t> sensor_pdr(&outData[0],
311 &outData[size]);
312 pdrs.emplace_back(std::move(sensor_pdr));
313 break;
314 }
Chicago Duana7aacc32020-06-10 18:03:38 +0800315 possible_states_start += possibleStateSize + sizeof(setId) +
316 sizeof(possibleStateSize);
Chicago Duan738e4d82020-05-28 16:39:19 +0800317 }
318 }
319
320 } while (record);
321 }
322 catch (const std::exception& e)
323 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600324 error(" Failed to obtain a record. ERROR = {ERR_EXCEP}", "ERR_EXCEP",
325 e.what());
Chicago Duan738e4d82020-05-28 16:39:19 +0800326 }
327
328 return pdrs;
329}
330
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500331uint8_t readHostEID()
332{
333 uint8_t eid{};
Brad Bishop06052cc2021-08-16 15:17:16 -0400334 std::ifstream eidFile{HOST_EID_PATH};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500335 if (!eidFile.good())
336 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600337 error("Could not open host EID file: {HOST_PATH}", "HOST_PATH",
338 static_cast<std::string>(HOST_EID_PATH));
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500339 }
340 else
341 {
342 std::string eidStr;
343 eidFile >> eidStr;
344 if (!eidStr.empty())
345 {
346 eid = atoi(eidStr.c_str());
347 }
348 else
349 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600350 error("Host EID file was empty");
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500351 }
352 }
353
354 return eid;
355}
George Liu83409572019-12-24 18:42:54 +0800356
357uint8_t getNumPadBytes(uint32_t data)
358{
359 uint8_t pad;
360 pad = ((data % 4) ? (4 - data % 4) : 0);
361 return pad;
362} // end getNumPadBytes
363
364bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
365 uint8_t* hour, uint8_t* min, uint8_t* sec)
366{
367 constexpr uint64_t max_data = 29991231115959;
368 constexpr uint64_t min_data = 19700101000000;
369 if (data < min_data || data > max_data)
370 {
371 return false;
372 }
373
374 *year = data / 10000000000;
375 data = data % 10000000000;
376 *month = data / 100000000;
377 data = data % 100000000;
378 *day = data / 1000000;
379 data = data % 1000000;
380 *hour = data / 10000;
381 data = data % 10000;
382 *min = data / 100;
383 *sec = data % 100;
384
385 return true;
386}
387
George Liuba4c1fb2020-02-05 14:13:30 +0800388std::optional<std::vector<set_effecter_state_field>>
389 parseEffecterData(const std::vector<uint8_t>& effecterData,
390 uint8_t effecterCount)
George Liu83409572019-12-24 18:42:54 +0800391{
George Liuba4c1fb2020-02-05 14:13:30 +0800392 std::vector<set_effecter_state_field> stateField;
393
394 if (effecterData.size() != effecterCount * 2)
George Liu83409572019-12-24 18:42:54 +0800395 {
George Liuba4c1fb2020-02-05 14:13:30 +0800396 return std::nullopt;
George Liu83409572019-12-24 18:42:54 +0800397 }
398
George Liuba4c1fb2020-02-05 14:13:30 +0800399 for (uint8_t i = 0; i < effecterCount; ++i)
George Liu83409572019-12-24 18:42:54 +0800400 {
George Liuba4c1fb2020-02-05 14:13:30 +0800401 uint8_t set_request = effecterData[i * 2] == PLDM_REQUEST_SET
402 ? PLDM_REQUEST_SET
403 : PLDM_NO_CHANGE;
404 set_effecter_state_field filed{set_request, effecterData[i * 2 + 1]};
405 stateField.emplace_back(std::move(filed));
George Liu83409572019-12-24 18:42:54 +0800406 }
407
George Liuba4c1fb2020-02-05 14:13:30 +0800408 return std::make_optional(std::move(stateField));
George Liu83409572019-12-24 18:42:54 +0800409}
410
George Liu0e02c322020-01-01 09:41:51 +0800411std::string DBusHandler::getService(const char* path,
412 const char* interface) const
George Liu83409572019-12-24 18:42:54 +0800413{
414 using DbusInterfaceList = std::vector<std::string>;
415 std::map<std::string, std::vector<std::string>> mapperResponse;
George Liu0e02c322020-01-01 09:41:51 +0800416 auto& bus = DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +0800417
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600418 auto mapper = bus.new_method_call(ObjectMapper::default_service,
419 ObjectMapper::instance_path,
420 ObjectMapper::interface, "GetObject");
George Liudf9a6d32020-12-22 16:27:16 +0800421
422 if (interface)
423 {
424 mapper.append(path, DbusInterfaceList({interface}));
425 }
426 else
427 {
428 mapper.append(path, DbusInterfaceList({}));
429 }
George Liu83409572019-12-24 18:42:54 +0800430
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500431 auto mapperResponseMsg = bus.call(mapper, dbusTimeout);
George Liu0e02c322020-01-01 09:41:51 +0800432 mapperResponseMsg.read(mapperResponse);
George Liu83409572019-12-24 18:42:54 +0800433 return mapperResponse.begin()->first;
434}
435
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530436GetSubTreeResponse
437 DBusHandler::getSubtree(const std::string& searchPath, int depth,
438 const std::vector<std::string>& ifaceList) const
439{
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530440 auto& bus = pldm::utils::DBusHandler::getBus();
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600441 auto method = bus.new_method_call(ObjectMapper::default_service,
442 ObjectMapper::instance_path,
443 ObjectMapper::interface, "GetSubTree");
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530444 method.append(searchPath, depth, ifaceList);
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500445 auto reply = bus.call(method, dbusTimeout);
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530446 GetSubTreeResponse response;
447 reply.read(response);
448 return response;
449}
450
Pavithra Barithayad28f08c2021-12-15 03:37:14 -0600451void reportError(const char* errorMsg, const Severity& sev)
George Liu83409572019-12-24 18:42:54 +0800452{
George Liu0e02c322020-01-01 09:41:51 +0800453 auto& bus = pldm::utils::DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +0800454
455 try
456 {
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600457 using LoggingCreate =
458 sdbusplus::client::xyz::openbmc_project::logging::Create<>;
459
Pavithra Barithayad28f08c2021-12-15 03:37:14 -0600460 std::string severity = "xyz.openbmc_project.Logging.Entry.Level.Error";
461
George Liu83409572019-12-24 18:42:54 +0800462 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
Pavithra Barithaya7b4d59a2024-02-05 09:09:30 -0600463 auto method = bus.new_method_call(LoggingCreate::default_service,
464 LoggingCreate::instance_path,
465 LoggingCreate::interface, "Create");
466
Pavithra Barithayad28f08c2021-12-15 03:37:14 -0600467 auto itr = sevMap.find(sev);
468 if (itr != sevMap.end())
469 {
470 severity = itr->second;
471 }
472
George Liu83409572019-12-24 18:42:54 +0800473 std::map<std::string, std::string> addlData{};
474 method.append(errorMsg, severity, addlData);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000475 bus.call_noreply(method, dbusTimeout);
George Liu83409572019-12-24 18:42:54 +0800476 }
477 catch (const std::exception& e)
478 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600479 error(
480 "failed to make a d-bus call to create error log, ERROR={ERR_EXCEP}",
481 "ERR_EXCEP", e.what());
George Liu83409572019-12-24 18:42:54 +0800482 }
483}
484
George Liu1e44c732020-02-28 20:20:06 +0800485void DBusHandler::setDbusProperty(const DBusMapping& dBusMap,
486 const PropertyValue& value) const
487{
488 auto setDbusValue = [&dBusMap, this](const auto& variant) {
489 auto& bus = getBus();
Patrick Williams6da4f912023-05-10 07:50:53 -0500490 auto service = getService(dBusMap.objectPath.c_str(),
491 dBusMap.interface.c_str());
George Liu1e44c732020-02-28 20:20:06 +0800492 auto method = bus.new_method_call(
493 service.c_str(), dBusMap.objectPath.c_str(), dbusProperties, "Set");
494 method.append(dBusMap.interface.c_str(), dBusMap.propertyName.c_str(),
495 variant);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000496 bus.call_noreply(method, dbusTimeout);
George Liu1e44c732020-02-28 20:20:06 +0800497 };
498
499 if (dBusMap.propertyType == "uint8_t")
500 {
501 std::variant<uint8_t> v = std::get<uint8_t>(value);
502 setDbusValue(v);
503 }
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600504 else if (dBusMap.propertyType == "bool")
505 {
506 std::variant<bool> v = std::get<bool>(value);
507 setDbusValue(v);
508 }
George Liu1e44c732020-02-28 20:20:06 +0800509 else if (dBusMap.propertyType == "int16_t")
510 {
511 std::variant<int16_t> v = std::get<int16_t>(value);
512 setDbusValue(v);
513 }
514 else if (dBusMap.propertyType == "uint16_t")
515 {
516 std::variant<uint16_t> v = std::get<uint16_t>(value);
517 setDbusValue(v);
518 }
519 else if (dBusMap.propertyType == "int32_t")
520 {
521 std::variant<int32_t> v = std::get<int32_t>(value);
522 setDbusValue(v);
523 }
524 else if (dBusMap.propertyType == "uint32_t")
525 {
526 std::variant<uint32_t> v = std::get<uint32_t>(value);
527 setDbusValue(v);
528 }
529 else if (dBusMap.propertyType == "int64_t")
530 {
531 std::variant<int64_t> v = std::get<int64_t>(value);
532 setDbusValue(v);
533 }
534 else if (dBusMap.propertyType == "uint64_t")
535 {
536 std::variant<uint64_t> v = std::get<uint64_t>(value);
537 setDbusValue(v);
538 }
539 else if (dBusMap.propertyType == "double")
540 {
541 std::variant<double> v = std::get<double>(value);
542 setDbusValue(v);
543 }
544 else if (dBusMap.propertyType == "string")
545 {
546 std::variant<std::string> v = std::get<std::string>(value);
547 setDbusValue(v);
548 }
549 else
550 {
551 throw std::invalid_argument("UnSpported Dbus Type");
552 }
553}
554
John Wang9e242422020-03-05 08:37:50 +0800555PropertyValue DBusHandler::getDbusPropertyVariant(
556 const char* objPath, const char* dbusProp, const char* dbusInterface) const
557{
558 auto& bus = DBusHandler::getBus();
559 auto service = getService(objPath, dbusInterface);
Patrick Williams6da4f912023-05-10 07:50:53 -0500560 auto method = bus.new_method_call(service.c_str(), objPath, dbusProperties,
561 "Get");
John Wang9e242422020-03-05 08:37:50 +0800562 method.append(dbusInterface, dbusProp);
Patrick Williams75b8f462024-02-07 10:59:26 -0600563 return bus.call(method, dbusTimeout).unpack<PropertyValue>();
John Wang9e242422020-03-05 08:37:50 +0800564}
565
Riya Dixit754041d2024-02-20 06:15:49 -0600566ObjectValueTree DBusHandler::getManagedObj(const char* service,
567 const char* rootPath)
568{
569 auto& bus = DBusHandler::getBus();
570 auto method = bus.new_method_call(service, rootPath,
571 "org.freedesktop.DBus.ObjectManager",
572 "GetManagedObjects");
573 return bus.call(method).unpack<ObjectValueTree>();
574}
575
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530576PropertyValue jsonEntryToDbusVal(std::string_view type,
577 const nlohmann::json& value)
578{
579 PropertyValue propValue{};
580 if (type == "uint8_t")
581 {
582 propValue = static_cast<uint8_t>(value);
583 }
584 else if (type == "uint16_t")
585 {
586 propValue = static_cast<uint16_t>(value);
587 }
588 else if (type == "uint32_t")
589 {
590 propValue = static_cast<uint32_t>(value);
591 }
592 else if (type == "uint64_t")
593 {
594 propValue = static_cast<uint64_t>(value);
595 }
596 else if (type == "int16_t")
597 {
598 propValue = static_cast<int16_t>(value);
599 }
600 else if (type == "int32_t")
601 {
602 propValue = static_cast<int32_t>(value);
603 }
604 else if (type == "int64_t")
605 {
606 propValue = static_cast<int64_t>(value);
607 }
608 else if (type == "bool")
609 {
610 propValue = static_cast<bool>(value);
611 }
612 else if (type == "double")
613 {
614 propValue = static_cast<double>(value);
615 }
616 else if (type == "string")
617 {
618 propValue = static_cast<std::string>(value);
619 }
620 else
621 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600622 error("Unknown D-Bus property type, TYPE={OTHER_TYPE}", "OTHER_TYPE",
623 type);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530624 }
625
626 return propValue;
627}
628
Tom Joseph250c4752020-04-15 10:32:45 +0530629uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
630 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500631 uint16_t stateSetId, bool localOrRemote)
Tom Joseph250c4752020-04-15 10:32:45 +0530632{
633 uint8_t* pdrData = nullptr;
634 uint32_t pdrSize{};
635 const pldm_pdr_record* record{};
636 do
637 {
638 record = pldm_pdr_find_record_by_type(pdrRepo, PLDM_STATE_EFFECTER_PDR,
639 record, &pdrData, &pdrSize);
Sampa Misraa4a96162020-07-14 05:33:46 -0500640 if (record && (localOrRemote ^ pldm_pdr_record_is_remote(record)))
Tom Joseph250c4752020-04-15 10:32:45 +0530641 {
642 auto pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrData);
643 auto compositeEffecterCount = pdr->composite_effecter_count;
644 auto possible_states_start = pdr->possible_states;
645
646 for (auto effecters = 0x00; effecters < compositeEffecterCount;
647 effecters++)
648 {
649 auto possibleStates =
650 reinterpret_cast<state_effecter_possible_states*>(
651 possible_states_start);
652 auto setId = possibleStates->state_set_id;
653 auto possibleStateSize = possibleStates->possible_states_size;
654
655 if (entityType == pdr->entity_type &&
656 entityInstance == pdr->entity_instance &&
657 containerId == pdr->container_id && stateSetId == setId)
658 {
659 return pdr->effecter_id;
660 }
661 possible_states_start += possibleStateSize + sizeof(setId) +
662 sizeof(possibleStateSize);
663 }
664 }
665 } while (record);
666
667 return PLDM_INVALID_EFFECTER_ID;
668}
669
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800670int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
671 uint8_t sensorOffset, uint8_t eventState,
672 uint8_t previousEventState)
673{
674 try
675 {
676 auto& bus = DBusHandler::getBus();
677 auto msg = bus.new_signal("/xyz/openbmc_project/pldm",
678 "xyz.openbmc_project.PLDM.Event",
679 "StateSensorEvent");
680 msg.append(tid, sensorId, sensorOffset, eventState, previousEventState);
681
682 msg.signal_send();
683 }
Patrick Williams51330582021-10-06 12:48:56 -0500684 catch (const std::exception& e)
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800685 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600686 error("Error emitting pldm event signal:ERROR={ERR_EXCEP}", "ERR_EXCEP",
687 e.what());
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800688 return PLDM_ERROR;
689 }
690
691 return PLDM_SUCCESS;
692}
693
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500694uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
695 uint16_t entityType, uint16_t entityInstance,
696 uint16_t containerId, uint16_t stateSetId)
697{
698 auto pdrs = findStateSensorPDR(tid, entityType, stateSetId, pdrRepo);
699 for (auto pdr : pdrs)
700 {
701 auto sensorPdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
702 auto compositeSensorCount = sensorPdr->composite_sensor_count;
703 auto possible_states_start = sensorPdr->possible_states;
704
705 for (auto sensors = 0x00; sensors < compositeSensorCount; sensors++)
706 {
707 auto possibleStates =
708 reinterpret_cast<state_sensor_possible_states*>(
709 possible_states_start);
710 auto setId = possibleStates->state_set_id;
711 auto possibleStateSize = possibleStates->possible_states_size;
712 if (entityType == sensorPdr->entity_type &&
713 entityInstance == sensorPdr->entity_instance &&
714 stateSetId == setId && containerId == sensorPdr->container_id)
715 {
716 return sensorPdr->sensor_id;
717 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500718 possible_states_start += possibleStateSize + sizeof(setId) +
719 sizeof(possibleStateSize);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500720 }
721 }
722 return PLDM_INVALID_EFFECTER_ID;
723}
724
Tom Josephe5268cd2021-09-07 13:04:03 +0530725void printBuffer(bool isTx, const std::vector<uint8_t>& buffer)
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600726{
Tom Josephe5268cd2021-09-07 13:04:03 +0530727 if (!buffer.empty())
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600728 {
Tom Josephe5268cd2021-09-07 13:04:03 +0530729 if (isTx)
730 {
731 std::cout << "Tx: ";
732 }
733 else
734 {
735 std::cout << "Rx: ";
736 }
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600737 std::ostringstream tempStream;
738 for (int byte : buffer)
739 {
740 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
741 << " ";
742 }
743 std::cout << tempStream.str() << std::endl;
744 }
745}
746
Tom Joseph54922072021-06-19 02:45:46 -0700747std::string toString(const struct variable_field& var)
748{
749 if (var.ptr == nullptr || !var.length)
750 {
751 return "";
752 }
753
754 std::string str(reinterpret_cast<const char*>(var.ptr), var.length);
755 std::replace_if(
756 str.begin(), str.end(), [](const char& c) { return !isprint(c); }, ' ');
757 return str;
758}
759
George Liu872f0f62021-11-25 16:26:16 +0800760std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
761 std::string_view trimStr)
762{
763 std::vector<std::string> out;
764 size_t start;
765 size_t end = 0;
766
767 while ((start = srcStr.find_first_not_of(delim, end)) != std::string::npos)
768 {
769 end = srcStr.find(delim, start);
770 std::string_view dstStr = srcStr.substr(start, end - start);
771 if (!trimStr.empty())
772 {
773 dstStr.remove_prefix(dstStr.find_first_not_of(trimStr));
774 dstStr.remove_suffix(dstStr.size() - 1 -
775 dstStr.find_last_not_of(trimStr));
776 }
777
778 if (!dstStr.empty())
779 {
780 out.push_back(std::string(dstStr));
781 }
782 }
783
784 return out;
785}
786
Manojkiran Edaef773052021-07-29 09:29:28 +0530787std::string getCurrentSystemTime()
788{
789 using namespace std::chrono;
790 const time_point<system_clock> tp = system_clock::now();
791 std::time_t tt = system_clock::to_time_t(tp);
792 auto ms = duration_cast<microseconds>(tp.time_since_epoch()) -
793 duration_cast<seconds>(tp.time_since_epoch());
794
795 std::stringstream ss;
796 ss << std::put_time(std::localtime(&tt), "%F %Z %T.")
797 << std::to_string(ms.count());
798 return ss.str();
799}
800
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500801bool checkForFruPresence(const std::string& objPath)
802{
803 bool isPresent = false;
804 static constexpr auto presentInterface =
805 "xyz.openbmc_project.Inventory.Item";
806 static constexpr auto presentProperty = "Present";
807 try
808 {
809 auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant(
810 objPath.c_str(), presentProperty, presentInterface);
811 isPresent = std::get<bool>(propVal);
812 }
813 catch (const sdbusplus::exception::SdBusError& e)
814 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600815 error(
816 "Failed to check for FRU presence for {OBJ_PATH} ERROR = {ERR_EXCEP}",
817 "OBJ_PATH", objPath.c_str(), "ERR_EXCEP", e.what());
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500818 }
819 return isPresent;
820}
821
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600822bool checkIfLogicalBitSet(const uint16_t& containerId)
823{
824 return !(containerId & 0x8000);
825}
826
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500827void setFruPresence(const std::string& fruObjPath, bool present)
828{
829 pldm::utils::PropertyValue value{present};
830 pldm::utils::DBusMapping dbusMapping;
831 dbusMapping.objectPath = fruObjPath;
832 dbusMapping.interface = "xyz.openbmc_project.Inventory.Item";
833 dbusMapping.propertyName = "Present";
834 dbusMapping.propertyType = "bool";
835 try
836 {
837 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
838 }
839 catch (const std::exception& e)
840 {
841 error(
842 "Failed to set the present property on path: '{PATH}' with {ERROR} ",
843 "PATH", fruObjPath, "ERROR", e);
844 }
845}
846
George Liu83409572019-12-24 18:42:54 +0800847} // namespace utils
848} // namespace pldm