blob: b9d07e427587421b1511df18013557d233b93412 [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
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05007#include <phosphor-logging/elog-errors.hpp>
Patrick Venturec83c4dc2018-11-01 16:29:18 -07008#include <phosphor-logging/log.hpp>
9#include <sdbusplus/server.hpp>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050010#include <xyz/openbmc_project/Common/error.hpp>
Deepak Kodihalli76794492017-02-16 23:48:18 -060011
12namespace openpower
13{
14namespace vpd
15{
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050016using namespace openpower::vpd::constants;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050017using namespace inventory;
18using namespace phosphor::logging;
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050019using namespace sdbusplus::xyz::openbmc_project::Common::Error;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050020
Deepak Kodihalli76794492017-02-16 23:48:18 -060021namespace inventory
22{
23
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050024std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
25 const std::string& interface)
Deepak Kodihalli76794492017-02-16 23:48:18 -060026{
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050027 auto mapper = bus.new_method_call(mapperDestination, mapperObjectPath,
28 mapperInterface, "GetObject");
29 mapper.append(path, std::vector<std::string>({interface}));
Deepak Kodihalli76794492017-02-16 23:48:18 -060030
31 std::map<std::string, std::vector<std::string>> response;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050032 try
33 {
34 auto reply = bus.call(mapper);
35 reply.read(response);
36 }
37 catch (const sdbusplus::exception::SdBusError& e)
38 {
39 log<level::ERR>("D-Bus call exception",
40 entry("OBJPATH=%s", mapperObjectPath),
41 entry("INTERFACE=%s", mapperInterface),
42 entry("EXCEPTION=%s", e.what()));
43
44 throw std::runtime_error("Service name is not found");
45 }
46
Patrick Venturec83c4dc2018-11-01 16:29:18 -070047 if (response.empty())
Deepak Kodihalli76794492017-02-16 23:48:18 -060048 {
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050049 throw std::runtime_error("Service name response is empty");
Deepak Kodihalli76794492017-02-16 23:48:18 -060050 }
51
52 return response.begin()->first;
53}
54
55void callPIM(ObjectMap&& objects)
56{
Deepak Kodihalli76794492017-02-16 23:48:18 -060057 try
58 {
Deepak Kodihalli76794492017-02-16 23:48:18 -060059 auto bus = sdbusplus::bus::new_default();
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050060 auto service = getService(bus, pimPath, pimIntf);
Patrick Venturec83c4dc2018-11-01 16:29:18 -070061 auto pimMsg =
62 bus.new_method_call(service.c_str(), pimPath, pimIntf, "Notify");
Deepak Kodihalli76794492017-02-16 23:48:18 -060063 pimMsg.append(std::move(objects));
64 auto result = bus.call(pimMsg);
Patrick Venturec83c4dc2018-11-01 16:29:18 -070065 if (result.is_method_error())
Deepak Kodihalli76794492017-02-16 23:48:18 -060066 {
67 std::cerr << "PIM Notify() failed\n";
68 }
69 }
70 catch (const std::runtime_error& e)
71 {
Deepak Kodihalli76794492017-02-16 23:48:18 -060072 log<level::ERR>(e.what());
73 }
74}
75
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050076MapperResponse
77 getObjectSubtreeForInterfaces(const std::string& root, const int32_t depth,
78 const std::vector<std::string>& interfaces)
79{
80 auto bus = sdbusplus::bus::new_default();
81 auto mapperCall = bus.new_method_call(mapperDestination, mapperObjectPath,
82 mapperInterface, "GetSubTree");
83 mapperCall.append(root);
84 mapperCall.append(depth);
85 mapperCall.append(interfaces);
86
87 MapperResponse result = {};
88
89 try
90 {
91 auto response = bus.call(mapperCall);
92
93 response.read(result);
94 }
95 catch (const sdbusplus::exception::SdBusError& e)
96 {
97 log<level::ERR>("Error in mapper GetSubTree",
98 entry("ERROR=%s", e.what()));
99 }
100
101 return result;
102}
103
Deepak Kodihalli76794492017-02-16 23:48:18 -0600104} // namespace inventory
105
SunnySrivastava1984945a02d2020-05-06 01:55:41 -0500106vpdType vpdTypeCheck(const Binary& vpdVector)
107{
108 // Read first 3 Bytes to check the 11S bar code format
109 std::string is11SFormat = "";
110 for (uint8_t i = 0; i < FORMAT_11S_LEN; i++)
111 {
112 is11SFormat += vpdVector[MEMORY_VPD_DATA_START + i];
113 }
114
115 if (vpdVector[IPZ_DATA_START] == KW_VAL_PAIR_START_TAG)
116 {
117 // IPZ VPD FORMAT
118 return vpdType::IPZ_VPD;
119 }
120 else if (vpdVector[KW_VPD_DATA_START] == KW_VPD_START_TAG)
121 {
122 // KEYWORD VPD FORMAT
123 return vpdType::KEYWORD_VPD;
124 }
125 else if (is11SFormat.compare(MEMORY_VPD_START_TAG) == 0)
126 {
127 // Memory VPD format
128 return vpdType::MEMORY_VPD;
129 }
130
131 // INVALID VPD FORMAT
132 return vpdType::INVALID_VPD_FORMAT;
133}
134
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600135LE2ByteData readUInt16LE(Binary::const_iterator iterator)
136{
137 LE2ByteData lowByte = *iterator;
138 LE2ByteData highByte = *(iterator + 1);
139 lowByte |= (highByte << 8);
140 return lowByte;
141}
142
SunnySrivastava1984d076da82020-03-05 05:33:35 -0600143/** @brief Encodes a keyword for D-Bus.
144 */
145string encodeKeyword(const string& kw, const string& encoding)
146{
147 if (encoding == "MAC")
148 {
149 string res{};
150 size_t first = kw[0];
151 res += toHex(first >> 4);
152 res += toHex(first & 0x0f);
153 for (size_t i = 1; i < kw.size(); ++i)
154 {
155 res += ":";
156 res += toHex(kw[i] >> 4);
157 res += toHex(kw[i] & 0x0f);
158 }
159 return res;
160 }
161 else if (encoding == "DATE")
162 {
163 // Date, represent as
164 // <year>-<month>-<day> <hour>:<min>
165 string res{};
166 static constexpr uint8_t skipPrefix = 3;
167
168 auto strItr = kw.begin();
169 advance(strItr, skipPrefix);
170 for_each(strItr, kw.end(), [&res](size_t c) { res += c; });
171
172 res.insert(BD_YEAR_END, 1, '-');
173 res.insert(BD_MONTH_END, 1, '-');
174 res.insert(BD_DAY_END, 1, ' ');
175 res.insert(BD_HOUR_END, 1, ':');
176
177 return res;
178 }
179 else // default to string encoding
180 {
181 return string(kw.begin(), kw.end());
182 }
183}
SunnySrivastava198443306542020-04-01 02:50:20 -0500184
185string readBusProperty(const string& obj, const string& inf, const string& prop)
186{
187 std::string propVal{};
188 std::string object = INVENTORY_PATH + obj;
189 auto bus = sdbusplus::bus::new_default();
190 auto properties = bus.new_method_call(
191 "xyz.openbmc_project.Inventory.Manager", object.c_str(),
192 "org.freedesktop.DBus.Properties", "Get");
193 properties.append(inf);
194 properties.append(prop);
195 auto result = bus.call(properties);
196 if (!result.is_method_error())
197 {
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500198 variant<Binary, string> val;
SunnySrivastava198443306542020-04-01 02:50:20 -0500199 result.read(val);
SunnySrivastava198443306542020-04-01 02:50:20 -0500200 if (auto pVal = get_if<Binary>(&val))
201 {
202 propVal.assign(reinterpret_cast<const char*>(pVal->data()),
203 pVal->size());
204 }
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500205 else if (auto pVal = get_if<string>(&val))
206 {
207 propVal.assign(pVal->data(), pVal->size());
208 }
SunnySrivastava198443306542020-04-01 02:50:20 -0500209 }
210 return propVal;
211}
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500212
213void createPEL(const std::map<std::string, std::string>& additionalData,
214 const std::string& errIntf)
215{
216 try
217 {
218 auto bus = sdbusplus::bus::new_default();
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500219 auto service = getService(bus, loggerObjectPath, loggerCreateInterface);
220 auto method = bus.new_method_call(service.c_str(), loggerObjectPath,
221 loggerCreateInterface, "Create");
222
223 method.append(errIntf, "xyz.openbmc_project.Logging.Entry.Level.Error",
224 additionalData);
225 auto resp = bus.call(method);
226 }
227 catch (const sdbusplus::exception::SdBusError& e)
228 {
229 throw std::runtime_error(
230 "Error in invoking D-Bus logging create interface to register PEL");
231 }
232}
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700233} // namespace vpd
234} // namespace openpower