blob: 97fc1ddf9f2662c5d40263712e62c67a79ca54bd [file] [log] [blame]
George Liu83409572019-12-24 18:42:54 +08001#include "utils.hpp"
2
George Liu6492f522020-06-16 10:34:05 +08003#include "libpldm/pdr.h"
4#include "libpldm/pldm_types.h"
5
6#include <xyz/openbmc_project/Common/error.hpp>
7
Tom Joseph54922072021-06-19 02:45:46 -07008#include <algorithm>
George Liu83409572019-12-24 18:42:54 +08009#include <array>
Tom Joseph54922072021-06-19 02:45:46 -070010#include <cctype>
George Liu83409572019-12-24 18:42:54 +080011#include <ctime>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050012#include <fstream>
George Liu83409572019-12-24 18:42:54 +080013#include <iostream>
14#include <map>
George Liu83409572019-12-24 18:42:54 +080015#include <stdexcept>
16#include <string>
17#include <vector>
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050018
George Liu83409572019-12-24 18:42:54 +080019namespace pldm
20{
21namespace utils
22{
23
24constexpr auto mapperBusName = "xyz.openbmc_project.ObjectMapper";
25constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
26constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper";
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050027constexpr auto eidPath = "/usr/share/pldm/host_eid";
28
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050029std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t /*tid*/,
30 uint16_t entityID,
31 uint16_t stateSetId,
32 const pldm_pdr* repo)
33{
34 uint8_t* outData = nullptr;
35 uint32_t size{};
36 const pldm_pdr_record* record{};
37 std::vector<std::vector<uint8_t>> pdrs;
38 try
39 {
40 do
41 {
42 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_EFFECTER_PDR,
43 record, &outData, &size);
44 if (record)
45 {
46 auto pdr = reinterpret_cast<pldm_state_effecter_pdr*>(outData);
47 auto compositeEffecterCount = pdr->composite_effecter_count;
Chicago Duana7aacc32020-06-10 18:03:38 +080048 auto possible_states_start = pdr->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050049
50 for (auto effecters = 0x00; effecters < compositeEffecterCount;
51 effecters++)
52 {
53 auto possibleStates =
54 reinterpret_cast<state_effecter_possible_states*>(
Chicago Duana7aacc32020-06-10 18:03:38 +080055 possible_states_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050056 auto setId = possibleStates->state_set_id;
57 auto possibleStateSize =
58 possibleStates->possible_states_size;
59
60 if (pdr->entity_type == entityID && setId == stateSetId)
61 {
62 std::vector<uint8_t> effecter_pdr(&outData[0],
63 &outData[size]);
64 pdrs.emplace_back(std::move(effecter_pdr));
65 break;
66 }
Chicago Duana7aacc32020-06-10 18:03:38 +080067 possible_states_start += possibleStateSize + sizeof(setId) +
68 sizeof(possibleStateSize);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050069 }
70 }
71
72 } while (record);
73 }
74 catch (const std::exception& e)
75 {
76 std::cerr << " Failed to obtain a record. ERROR =" << e.what()
77 << std::endl;
78 }
79
80 return pdrs;
81}
82
Chicago Duan738e4d82020-05-28 16:39:19 +080083std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t /*tid*/,
84 uint16_t entityID,
85 uint16_t stateSetId,
86 const pldm_pdr* repo)
87{
88 uint8_t* outData = nullptr;
89 uint32_t size{};
90 const pldm_pdr_record* record{};
91 std::vector<std::vector<uint8_t>> pdrs;
92 try
93 {
94 do
95 {
96 record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_SENSOR_PDR,
97 record, &outData, &size);
98 if (record)
99 {
100 auto pdr = reinterpret_cast<pldm_state_sensor_pdr*>(outData);
101 auto compositeSensorCount = pdr->composite_sensor_count;
Chicago Duana7aacc32020-06-10 18:03:38 +0800102 auto possible_states_start = pdr->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800103
104 for (auto sensors = 0x00; sensors < compositeSensorCount;
105 sensors++)
106 {
107 auto possibleStates =
108 reinterpret_cast<state_sensor_possible_states*>(
Chicago Duana7aacc32020-06-10 18:03:38 +0800109 possible_states_start);
Chicago Duan738e4d82020-05-28 16:39:19 +0800110 auto setId = possibleStates->state_set_id;
111 auto possibleStateSize =
112 possibleStates->possible_states_size;
113
114 if (pdr->entity_type == entityID && setId == stateSetId)
115 {
116 std::vector<uint8_t> sensor_pdr(&outData[0],
117 &outData[size]);
118 pdrs.emplace_back(std::move(sensor_pdr));
119 break;
120 }
Chicago Duana7aacc32020-06-10 18:03:38 +0800121 possible_states_start += possibleStateSize + sizeof(setId) +
122 sizeof(possibleStateSize);
Chicago Duan738e4d82020-05-28 16:39:19 +0800123 }
124 }
125
126 } while (record);
127 }
128 catch (const std::exception& e)
129 {
130 std::cerr << " Failed to obtain a record. ERROR =" << e.what()
131 << std::endl;
132 }
133
134 return pdrs;
135}
136
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500137uint8_t readHostEID()
138{
139 uint8_t eid{};
140 std::ifstream eidFile{eidPath};
141 if (!eidFile.good())
142 {
143 std::cerr << "Could not open host EID file"
144 << "\n";
145 }
146 else
147 {
148 std::string eidStr;
149 eidFile >> eidStr;
150 if (!eidStr.empty())
151 {
152 eid = atoi(eidStr.c_str());
153 }
154 else
155 {
156 std::cerr << "Host EID file was empty"
157 << "\n";
158 }
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
George Liu0e02c322020-01-01 09:41:51 +0800229 auto mapperResponseMsg = bus.call(mapper);
230 mapperResponseMsg.read(mapperResponse);
George Liu83409572019-12-24 18:42:54 +0800231 return mapperResponse.begin()->first;
232}
233
234void reportError(const char* errorMsg)
235{
236 static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
237 static constexpr auto logInterface = "xyz.openbmc_project.Logging.Create";
238
George Liu0e02c322020-01-01 09:41:51 +0800239 auto& bus = pldm::utils::DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +0800240
241 try
242 {
George Liu0e02c322020-01-01 09:41:51 +0800243 auto service = DBusHandler().getService(logObjPath, logInterface);
George Liu83409572019-12-24 18:42:54 +0800244 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
245 auto severity =
246 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
247 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
248 Error);
249 auto method = bus.new_method_call(service.c_str(), logObjPath,
250 logInterface, "Create");
251 std::map<std::string, std::string> addlData{};
252 method.append(errorMsg, severity, addlData);
253 bus.call_noreply(method);
254 }
255 catch (const std::exception& e)
256 {
257 std::cerr << "failed to make a d-bus call to create error log, ERROR="
258 << e.what() << "\n";
259 }
260}
261
George Liu1e44c732020-02-28 20:20:06 +0800262void DBusHandler::setDbusProperty(const DBusMapping& dBusMap,
263 const PropertyValue& value) const
264{
265 auto setDbusValue = [&dBusMap, this](const auto& variant) {
266 auto& bus = getBus();
267 auto service =
268 getService(dBusMap.objectPath.c_str(), dBusMap.interface.c_str());
269 auto method = bus.new_method_call(
270 service.c_str(), dBusMap.objectPath.c_str(), dbusProperties, "Set");
271 method.append(dBusMap.interface.c_str(), dBusMap.propertyName.c_str(),
272 variant);
273 bus.call_noreply(method);
274 };
275
276 if (dBusMap.propertyType == "uint8_t")
277 {
278 std::variant<uint8_t> v = std::get<uint8_t>(value);
279 setDbusValue(v);
280 }
Deepak Kodihallifd279e12020-02-02 05:20:43 -0600281 else if (dBusMap.propertyType == "bool")
282 {
283 std::variant<bool> v = std::get<bool>(value);
284 setDbusValue(v);
285 }
George Liu1e44c732020-02-28 20:20:06 +0800286 else if (dBusMap.propertyType == "int16_t")
287 {
288 std::variant<int16_t> v = std::get<int16_t>(value);
289 setDbusValue(v);
290 }
291 else if (dBusMap.propertyType == "uint16_t")
292 {
293 std::variant<uint16_t> v = std::get<uint16_t>(value);
294 setDbusValue(v);
295 }
296 else if (dBusMap.propertyType == "int32_t")
297 {
298 std::variant<int32_t> v = std::get<int32_t>(value);
299 setDbusValue(v);
300 }
301 else if (dBusMap.propertyType == "uint32_t")
302 {
303 std::variant<uint32_t> v = std::get<uint32_t>(value);
304 setDbusValue(v);
305 }
306 else if (dBusMap.propertyType == "int64_t")
307 {
308 std::variant<int64_t> v = std::get<int64_t>(value);
309 setDbusValue(v);
310 }
311 else if (dBusMap.propertyType == "uint64_t")
312 {
313 std::variant<uint64_t> v = std::get<uint64_t>(value);
314 setDbusValue(v);
315 }
316 else if (dBusMap.propertyType == "double")
317 {
318 std::variant<double> v = std::get<double>(value);
319 setDbusValue(v);
320 }
321 else if (dBusMap.propertyType == "string")
322 {
323 std::variant<std::string> v = std::get<std::string>(value);
324 setDbusValue(v);
325 }
326 else
327 {
328 throw std::invalid_argument("UnSpported Dbus Type");
329 }
330}
331
John Wang9e242422020-03-05 08:37:50 +0800332PropertyValue DBusHandler::getDbusPropertyVariant(
333 const char* objPath, const char* dbusProp, const char* dbusInterface) const
334{
335 auto& bus = DBusHandler::getBus();
336 auto service = getService(objPath, dbusInterface);
337 auto method =
338 bus.new_method_call(service.c_str(), objPath, dbusProperties, "Get");
339 method.append(dbusInterface, dbusProp);
340 PropertyValue value{};
341 auto reply = bus.call(method);
342 reply.read(value);
343 return value;
344}
345
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530346PropertyValue jsonEntryToDbusVal(std::string_view type,
347 const nlohmann::json& value)
348{
349 PropertyValue propValue{};
350 if (type == "uint8_t")
351 {
352 propValue = static_cast<uint8_t>(value);
353 }
354 else if (type == "uint16_t")
355 {
356 propValue = static_cast<uint16_t>(value);
357 }
358 else if (type == "uint32_t")
359 {
360 propValue = static_cast<uint32_t>(value);
361 }
362 else if (type == "uint64_t")
363 {
364 propValue = static_cast<uint64_t>(value);
365 }
366 else if (type == "int16_t")
367 {
368 propValue = static_cast<int16_t>(value);
369 }
370 else if (type == "int32_t")
371 {
372 propValue = static_cast<int32_t>(value);
373 }
374 else if (type == "int64_t")
375 {
376 propValue = static_cast<int64_t>(value);
377 }
378 else if (type == "bool")
379 {
380 propValue = static_cast<bool>(value);
381 }
382 else if (type == "double")
383 {
384 propValue = static_cast<double>(value);
385 }
386 else if (type == "string")
387 {
388 propValue = static_cast<std::string>(value);
389 }
390 else
391 {
392 std::cerr << "Unknown D-Bus property type, TYPE=" << type << "\n";
393 }
394
395 return propValue;
396}
397
Tom Joseph250c4752020-04-15 10:32:45 +0530398uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
399 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500400 uint16_t stateSetId, bool localOrRemote)
Tom Joseph250c4752020-04-15 10:32:45 +0530401{
402 uint8_t* pdrData = nullptr;
403 uint32_t pdrSize{};
404 const pldm_pdr_record* record{};
405 do
406 {
407 record = pldm_pdr_find_record_by_type(pdrRepo, PLDM_STATE_EFFECTER_PDR,
408 record, &pdrData, &pdrSize);
Sampa Misraa4a96162020-07-14 05:33:46 -0500409 if (record && (localOrRemote ^ pldm_pdr_record_is_remote(record)))
Tom Joseph250c4752020-04-15 10:32:45 +0530410 {
411 auto pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrData);
412 auto compositeEffecterCount = pdr->composite_effecter_count;
413 auto possible_states_start = pdr->possible_states;
414
415 for (auto effecters = 0x00; effecters < compositeEffecterCount;
416 effecters++)
417 {
418 auto possibleStates =
419 reinterpret_cast<state_effecter_possible_states*>(
420 possible_states_start);
421 auto setId = possibleStates->state_set_id;
422 auto possibleStateSize = possibleStates->possible_states_size;
423
424 if (entityType == pdr->entity_type &&
425 entityInstance == pdr->entity_instance &&
426 containerId == pdr->container_id && stateSetId == setId)
427 {
428 return pdr->effecter_id;
429 }
430 possible_states_start += possibleStateSize + sizeof(setId) +
431 sizeof(possibleStateSize);
432 }
433 }
434 } while (record);
435
436 return PLDM_INVALID_EFFECTER_ID;
437}
438
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800439int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
440 uint8_t sensorOffset, uint8_t eventState,
441 uint8_t previousEventState)
442{
443 try
444 {
445 auto& bus = DBusHandler::getBus();
446 auto msg = bus.new_signal("/xyz/openbmc_project/pldm",
447 "xyz.openbmc_project.PLDM.Event",
448 "StateSensorEvent");
449 msg.append(tid, sensorId, sensorOffset, eventState, previousEventState);
450
451 msg.signal_send();
452 }
453 catch (std::exception& e)
454 {
455 std::cerr << "Error emitting pldm event signal:"
456 << "ERROR=" << e.what() << "\n";
457 return PLDM_ERROR;
458 }
459
460 return PLDM_SUCCESS;
461}
462
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500463uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
464 uint16_t entityType, uint16_t entityInstance,
465 uint16_t containerId, uint16_t stateSetId)
466{
467 auto pdrs = findStateSensorPDR(tid, entityType, stateSetId, pdrRepo);
468 for (auto pdr : pdrs)
469 {
470 auto sensorPdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
471 auto compositeSensorCount = sensorPdr->composite_sensor_count;
472 auto possible_states_start = sensorPdr->possible_states;
473
474 for (auto sensors = 0x00; sensors < compositeSensorCount; sensors++)
475 {
476 auto possibleStates =
477 reinterpret_cast<state_sensor_possible_states*>(
478 possible_states_start);
479 auto setId = possibleStates->state_set_id;
480 auto possibleStateSize = possibleStates->possible_states_size;
481 if (entityType == sensorPdr->entity_type &&
482 entityInstance == sensorPdr->entity_instance &&
483 stateSetId == setId && containerId == sensorPdr->container_id)
484 {
485 return sensorPdr->sensor_id;
486 }
487 possible_states_start +=
488 possibleStateSize + sizeof(setId) + sizeof(possibleStateSize);
489 }
490 }
491 return PLDM_INVALID_EFFECTER_ID;
492}
493
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600494void printBuffer(const std::vector<uint8_t>& buffer, bool pldmVerbose)
495{
496 if (pldmVerbose && !buffer.empty())
497 {
498 std::ostringstream tempStream;
499 for (int byte : buffer)
500 {
501 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
502 << " ";
503 }
504 std::cout << tempStream.str() << std::endl;
505 }
506}
507
Tom Joseph54922072021-06-19 02:45:46 -0700508std::string toString(const struct variable_field& var)
509{
510 if (var.ptr == nullptr || !var.length)
511 {
512 return "";
513 }
514
515 std::string str(reinterpret_cast<const char*>(var.ptr), var.length);
516 std::replace_if(
517 str.begin(), str.end(), [](const char& c) { return !isprint(c); }, ' ');
518 return str;
519}
520
George Liu83409572019-12-24 18:42:54 +0800521} // namespace utils
522} // namespace pldm