blob: a69abe6e1e61f9474066f284a4acd9ea826b65c1 [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
52bool decodeEffecterData(const std::vector<uint8_t>& effecterData,
53 uint16_t& effecter_id,
54 std::vector<set_effecter_state_field>& stateField)
55{
56 int flag = 0;
57 for (auto data : effecterData)
58 {
59 switch (flag)
60 {
61 case 0:
62 effecter_id = data;
63 flag = 1;
64 break;
65 case 1:
66 if (data == PLDM_REQUEST_SET)
67 {
68 flag = 2;
69 }
70 else
71 {
72 stateField.push_back({PLDM_NO_CHANGE, 0});
73 }
74 break;
75 case 2:
76 stateField.push_back({PLDM_REQUEST_SET, data});
77 flag = 1;
78 break;
79 default:
80 break;
81 }
82 }
83
84 if (stateField.size() < 1 || stateField.size() > 8)
85 {
86 return false;
87 }
88
89 return true;
90}
91
George Liu0e02c322020-01-01 09:41:51 +080092std::string DBusHandler::getService(const char* path,
93 const char* interface) const
George Liu83409572019-12-24 18:42:54 +080094{
95 using DbusInterfaceList = std::vector<std::string>;
96 std::map<std::string, std::vector<std::string>> mapperResponse;
George Liu0e02c322020-01-01 09:41:51 +080097 auto& bus = DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +080098
George Liu0e02c322020-01-01 09:41:51 +080099 auto mapper = bus.new_method_call(mapperBusName, mapperPath,
100 mapperInterface, "GetObject");
101 mapper.append(path, DbusInterfaceList({interface}));
George Liu83409572019-12-24 18:42:54 +0800102
George Liu0e02c322020-01-01 09:41:51 +0800103 auto mapperResponseMsg = bus.call(mapper);
104 mapperResponseMsg.read(mapperResponse);
George Liu83409572019-12-24 18:42:54 +0800105 return mapperResponse.begin()->first;
106}
107
108void reportError(const char* errorMsg)
109{
110 static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
111 static constexpr auto logInterface = "xyz.openbmc_project.Logging.Create";
112
George Liu0e02c322020-01-01 09:41:51 +0800113 auto& bus = pldm::utils::DBusHandler::getBus();
George Liu83409572019-12-24 18:42:54 +0800114
115 try
116 {
George Liu0e02c322020-01-01 09:41:51 +0800117 auto service = DBusHandler().getService(logObjPath, logInterface);
George Liu83409572019-12-24 18:42:54 +0800118 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
119 auto severity =
120 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
121 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
122 Error);
123 auto method = bus.new_method_call(service.c_str(), logObjPath,
124 logInterface, "Create");
125 std::map<std::string, std::string> addlData{};
126 method.append(errorMsg, severity, addlData);
127 bus.call_noreply(method);
128 }
129 catch (const std::exception& e)
130 {
131 std::cerr << "failed to make a d-bus call to create error log, ERROR="
132 << e.what() << "\n";
133 }
134}
135
136} // namespace utils
137} // namespace pldm