blob: 8b33305671c3641d90a0b6091093de21b940011a [file] [log] [blame]
SunnySrivastava198443306542020-04-01 02:50:20 -05001#include "config.h"
2
Deepak Kodihalli76794492017-02-16 23:48:18 -06003#include "utils.hpp"
Patrick Venturec83c4dc2018-11-01 16:29:18 -07004
SunnySrivastava1984d076da82020-03-05 05:33:35 -06005#include "defines.hpp"
6
Patrick Venturec83c4dc2018-11-01 16:29:18 -07007#include <phosphor-logging/log.hpp>
8#include <sdbusplus/server.hpp>
Deepak Kodihalli76794492017-02-16 23:48:18 -06009
10namespace openpower
11{
12namespace vpd
13{
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050014using namespace openpower::vpd::constants;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050015using namespace inventory;
16using namespace phosphor::logging;
17
Deepak Kodihalli76794492017-02-16 23:48:18 -060018namespace inventory
19{
20
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050021std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
22 const std::string& interface)
Deepak Kodihalli76794492017-02-16 23:48:18 -060023{
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050024 auto mapper = bus.new_method_call(mapperDestination, mapperObjectPath,
25 mapperInterface, "GetObject");
26 mapper.append(path, std::vector<std::string>({interface}));
Deepak Kodihalli76794492017-02-16 23:48:18 -060027
28 std::map<std::string, std::vector<std::string>> response;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050029 try
30 {
31 auto reply = bus.call(mapper);
32 reply.read(response);
33 }
34 catch (const sdbusplus::exception::SdBusError& e)
35 {
36 log<level::ERR>("D-Bus call exception",
37 entry("OBJPATH=%s", mapperObjectPath),
38 entry("INTERFACE=%s", mapperInterface),
39 entry("EXCEPTION=%s", e.what()));
40
41 throw std::runtime_error("Service name is not found");
42 }
43
Patrick Venturec83c4dc2018-11-01 16:29:18 -070044 if (response.empty())
Deepak Kodihalli76794492017-02-16 23:48:18 -060045 {
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050046 throw std::runtime_error("Service name response is empty");
Deepak Kodihalli76794492017-02-16 23:48:18 -060047 }
48
49 return response.begin()->first;
50}
51
52void callPIM(ObjectMap&& objects)
53{
Deepak Kodihalli76794492017-02-16 23:48:18 -060054 try
55 {
Deepak Kodihalli76794492017-02-16 23:48:18 -060056 auto bus = sdbusplus::bus::new_default();
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050057 auto service = getService(bus, pimPath, pimIntf);
Patrick Venturec83c4dc2018-11-01 16:29:18 -070058 auto pimMsg =
59 bus.new_method_call(service.c_str(), pimPath, pimIntf, "Notify");
Deepak Kodihalli76794492017-02-16 23:48:18 -060060 pimMsg.append(std::move(objects));
61 auto result = bus.call(pimMsg);
Patrick Venturec83c4dc2018-11-01 16:29:18 -070062 if (result.is_method_error())
Deepak Kodihalli76794492017-02-16 23:48:18 -060063 {
64 std::cerr << "PIM Notify() failed\n";
65 }
66 }
67 catch (const std::runtime_error& e)
68 {
69 using namespace phosphor::logging;
70 log<level::ERR>(e.what());
71 }
72}
73
74} // namespace inventory
75
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050076vpdType vpdTypeCheck(const Binary& vpdVector)
77{
78 // Read first 3 Bytes to check the 11S bar code format
79 std::string is11SFormat = "";
80 for (uint8_t i = 0; i < FORMAT_11S_LEN; i++)
81 {
82 is11SFormat += vpdVector[MEMORY_VPD_DATA_START + i];
83 }
84
85 if (vpdVector[IPZ_DATA_START] == KW_VAL_PAIR_START_TAG)
86 {
87 // IPZ VPD FORMAT
88 return vpdType::IPZ_VPD;
89 }
90 else if (vpdVector[KW_VPD_DATA_START] == KW_VPD_START_TAG)
91 {
92 // KEYWORD VPD FORMAT
93 return vpdType::KEYWORD_VPD;
94 }
95 else if (is11SFormat.compare(MEMORY_VPD_START_TAG) == 0)
96 {
97 // Memory VPD format
98 return vpdType::MEMORY_VPD;
99 }
100
101 // INVALID VPD FORMAT
102 return vpdType::INVALID_VPD_FORMAT;
103}
104
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600105LE2ByteData readUInt16LE(Binary::const_iterator iterator)
106{
107 LE2ByteData lowByte = *iterator;
108 LE2ByteData highByte = *(iterator + 1);
109 lowByte |= (highByte << 8);
110 return lowByte;
111}
112
SunnySrivastava1984d076da82020-03-05 05:33:35 -0600113/** @brief Encodes a keyword for D-Bus.
114 */
115string encodeKeyword(const string& kw, const string& encoding)
116{
117 if (encoding == "MAC")
118 {
119 string res{};
120 size_t first = kw[0];
121 res += toHex(first >> 4);
122 res += toHex(first & 0x0f);
123 for (size_t i = 1; i < kw.size(); ++i)
124 {
125 res += ":";
126 res += toHex(kw[i] >> 4);
127 res += toHex(kw[i] & 0x0f);
128 }
129 return res;
130 }
131 else if (encoding == "DATE")
132 {
133 // Date, represent as
134 // <year>-<month>-<day> <hour>:<min>
135 string res{};
136 static constexpr uint8_t skipPrefix = 3;
137
138 auto strItr = kw.begin();
139 advance(strItr, skipPrefix);
140 for_each(strItr, kw.end(), [&res](size_t c) { res += c; });
141
142 res.insert(BD_YEAR_END, 1, '-');
143 res.insert(BD_MONTH_END, 1, '-');
144 res.insert(BD_DAY_END, 1, ' ');
145 res.insert(BD_HOUR_END, 1, ':');
146
147 return res;
148 }
149 else // default to string encoding
150 {
151 return string(kw.begin(), kw.end());
152 }
153}
SunnySrivastava198443306542020-04-01 02:50:20 -0500154
155string readBusProperty(const string& obj, const string& inf, const string& prop)
156{
157 std::string propVal{};
158 std::string object = INVENTORY_PATH + obj;
159 auto bus = sdbusplus::bus::new_default();
160 auto properties = bus.new_method_call(
161 "xyz.openbmc_project.Inventory.Manager", object.c_str(),
162 "org.freedesktop.DBus.Properties", "Get");
163 properties.append(inf);
164 properties.append(prop);
165 auto result = bus.call(properties);
166 if (!result.is_method_error())
167 {
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500168 variant<Binary, string> val;
SunnySrivastava198443306542020-04-01 02:50:20 -0500169 result.read(val);
SunnySrivastava198443306542020-04-01 02:50:20 -0500170 if (auto pVal = get_if<Binary>(&val))
171 {
172 propVal.assign(reinterpret_cast<const char*>(pVal->data()),
173 pVal->size());
174 }
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500175 else if (auto pVal = get_if<string>(&val))
176 {
177 propVal.assign(pVal->data(), pVal->size());
178 }
SunnySrivastava198443306542020-04-01 02:50:20 -0500179 }
180 return propVal;
181}
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500182
183void createPEL(const std::map<std::string, std::string>& additionalData,
184 const std::string& errIntf)
185{
186 try
187 {
188 auto bus = sdbusplus::bus::new_default();
189
190 auto service = getService(bus, loggerObjectPath, loggerCreateInterface);
191 auto method = bus.new_method_call(service.c_str(), loggerObjectPath,
192 loggerCreateInterface, "Create");
193
194 method.append(errIntf, "xyz.openbmc_project.Logging.Entry.Level.Error",
195 additionalData);
196 auto resp = bus.call(method);
197 }
198 catch (const sdbusplus::exception::SdBusError& e)
199 {
200 throw std::runtime_error(
201 "Error in invoking D-Bus logging create interface to register PEL");
202 }
203}
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700204} // namespace vpd
205} // namespace openpower