blob: 846297a0e649f63b95d83b17ad8be79b6acd15dc [file] [log] [blame]
George Liu83409572019-12-24 18:42:54 +08001#include "utils.hpp"
2
3#include <array>
4#include <ctime>
5#include <iostream>
6#include <map>
George Liu83409572019-12-24 18:42:54 +08007#include <stdexcept>
8#include <string>
9#include <vector>
10#include <xyz/openbmc_project/Common/error.hpp>
11
12namespace pldm
13{
14namespace utils
15{
16
17constexpr auto mapperBusName = "xyz.openbmc_project.ObjectMapper";
18constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
19constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper";
20
21uint8_t getNumPadBytes(uint32_t data)
22{
23 uint8_t pad;
24 pad = ((data % 4) ? (4 - data % 4) : 0);
25 return pad;
26} // end getNumPadBytes
27
28bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
29 uint8_t* hour, uint8_t* min, uint8_t* sec)
30{
31 constexpr uint64_t max_data = 29991231115959;
32 constexpr uint64_t min_data = 19700101000000;
33 if (data < min_data || data > max_data)
34 {
35 return false;
36 }
37
38 *year = data / 10000000000;
39 data = data % 10000000000;
40 *month = data / 100000000;
41 data = data % 100000000;
42 *day = data / 1000000;
43 data = data % 1000000;
44 *hour = data / 10000;
45 data = data % 10000;
46 *min = data / 100;
47 *sec = data % 100;
48
49 return true;
50}
51
George Liuba4c1fb2020-02-05 14:13:30 +080052std::optional<std::vector<set_effecter_state_field>>
53 parseEffecterData(const std::vector<uint8_t>& effecterData,
54 uint8_t effecterCount)
George Liu83409572019-12-24 18:42:54 +080055{
George Liuba4c1fb2020-02-05 14:13:30 +080056 std::vector<set_effecter_state_field> stateField;
57
58 if (effecterData.size() != effecterCount * 2)
George Liu83409572019-12-24 18:42:54 +080059 {
George Liuba4c1fb2020-02-05 14:13:30 +080060 return std::nullopt;
George Liu83409572019-12-24 18:42:54 +080061 }
62
George Liuba4c1fb2020-02-05 14:13:30 +080063 for (uint8_t i = 0; i < effecterCount; ++i)
George Liu83409572019-12-24 18:42:54 +080064 {
George Liuba4c1fb2020-02-05 14:13:30 +080065 uint8_t set_request = effecterData[i * 2] == PLDM_REQUEST_SET
66 ? PLDM_REQUEST_SET
67 : PLDM_NO_CHANGE;
68 set_effecter_state_field filed{set_request, effecterData[i * 2 + 1]};
69 stateField.emplace_back(std::move(filed));
George Liu83409572019-12-24 18:42:54 +080070 }
71
George Liuba4c1fb2020-02-05 14:13:30 +080072 return std::make_optional(std::move(stateField));
George Liu83409572019-12-24 18:42:54 +080073}
74
George Liu0e02c322020-01-01 09:41:51 +080075std::string DBusHandler::getService(const char* path,
76 const char* interface) const
George Liu83409572019-12-24 18:42:54 +080077{
78 using DbusInterfaceList = std::vector<std::string>;
79 std::map<std::string, std::vector<std::string>> mapperResponse;
George Liu0e02c322020-01-01 09:41:51 +080080 auto& bus = DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +080081
George Liu0e02c322020-01-01 09:41:51 +080082 auto mapper = bus.new_method_call(mapperBusName, mapperPath,
83 mapperInterface, "GetObject");
84 mapper.append(path, DbusInterfaceList({interface}));
George Liu83409572019-12-24 18:42:54 +080085
George Liu0e02c322020-01-01 09:41:51 +080086 auto mapperResponseMsg = bus.call(mapper);
87 mapperResponseMsg.read(mapperResponse);
George Liu83409572019-12-24 18:42:54 +080088 return mapperResponse.begin()->first;
89}
90
91void reportError(const char* errorMsg)
92{
93 static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
94 static constexpr auto logInterface = "xyz.openbmc_project.Logging.Create";
95
George Liu0e02c322020-01-01 09:41:51 +080096 auto& bus = pldm::utils::DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +080097
98 try
99 {
George Liu0e02c322020-01-01 09:41:51 +0800100 auto service = DBusHandler().getService(logObjPath, logInterface);
George Liu83409572019-12-24 18:42:54 +0800101 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
102 auto severity =
103 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
104 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
105 Error);
106 auto method = bus.new_method_call(service.c_str(), logObjPath,
107 logInterface, "Create");
108 std::map<std::string, std::string> addlData{};
109 method.append(errorMsg, severity, addlData);
110 bus.call_noreply(method);
111 }
112 catch (const std::exception& e)
113 {
114 std::cerr << "failed to make a d-bus call to create error log, ERROR="
115 << e.what() << "\n";
116 }
117}
118
119} // namespace utils
120} // namespace pldm