blob: 24d7b4cebaeb114b09def8649fb91ab2a6b57aaa [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;
Deepak Kodihalli76794492017-02-16 23:48:18 -060015namespace inventory
16{
17
18auto getPIMService()
19{
20 auto bus = sdbusplus::bus::new_default();
21 auto mapper =
Patrick Venturec83c4dc2018-11-01 16:29:18 -070022 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
23 "/xyz/openbmc_project/object_mapper",
24 "xyz.openbmc_project.ObjectMapper", "GetObject");
Deepak Kodihalli76794492017-02-16 23:48:18 -060025
26 mapper.append(pimPath);
27 mapper.append(std::vector<std::string>({pimIntf}));
28
29 auto result = bus.call(mapper);
Patrick Venturec83c4dc2018-11-01 16:29:18 -070030 if (result.is_method_error())
Deepak Kodihalli76794492017-02-16 23:48:18 -060031 {
32 throw std::runtime_error("ObjectMapper GetObject failed");
33 }
34
35 std::map<std::string, std::vector<std::string>> response;
36 result.read(response);
Patrick Venturec83c4dc2018-11-01 16:29:18 -070037 if (response.empty())
Deepak Kodihalli76794492017-02-16 23:48:18 -060038 {
39 throw std::runtime_error("ObjectMapper GetObject bad response");
40 }
41
42 return response.begin()->first;
43}
44
45void callPIM(ObjectMap&& objects)
46{
47 std::string service;
48
49 try
50 {
51 service = getPIMService();
52 auto bus = sdbusplus::bus::new_default();
Patrick Venturec83c4dc2018-11-01 16:29:18 -070053 auto pimMsg =
54 bus.new_method_call(service.c_str(), pimPath, pimIntf, "Notify");
Deepak Kodihalli76794492017-02-16 23:48:18 -060055 pimMsg.append(std::move(objects));
56 auto result = bus.call(pimMsg);
Patrick Venturec83c4dc2018-11-01 16:29:18 -070057 if (result.is_method_error())
Deepak Kodihalli76794492017-02-16 23:48:18 -060058 {
59 std::cerr << "PIM Notify() failed\n";
60 }
61 }
62 catch (const std::runtime_error& e)
63 {
64 using namespace phosphor::logging;
65 log<level::ERR>(e.what());
66 }
67}
68
69} // namespace inventory
70
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050071vpdType vpdTypeCheck(const Binary& vpdVector)
72{
73 // Read first 3 Bytes to check the 11S bar code format
74 std::string is11SFormat = "";
75 for (uint8_t i = 0; i < FORMAT_11S_LEN; i++)
76 {
77 is11SFormat += vpdVector[MEMORY_VPD_DATA_START + i];
78 }
79
80 if (vpdVector[IPZ_DATA_START] == KW_VAL_PAIR_START_TAG)
81 {
82 // IPZ VPD FORMAT
83 return vpdType::IPZ_VPD;
84 }
85 else if (vpdVector[KW_VPD_DATA_START] == KW_VPD_START_TAG)
86 {
87 // KEYWORD VPD FORMAT
88 return vpdType::KEYWORD_VPD;
89 }
90 else if (is11SFormat.compare(MEMORY_VPD_START_TAG) == 0)
91 {
92 // Memory VPD format
93 return vpdType::MEMORY_VPD;
94 }
95
96 // INVALID VPD FORMAT
97 return vpdType::INVALID_VPD_FORMAT;
98}
99
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600100LE2ByteData readUInt16LE(Binary::const_iterator iterator)
101{
102 LE2ByteData lowByte = *iterator;
103 LE2ByteData highByte = *(iterator + 1);
104 lowByte |= (highByte << 8);
105 return lowByte;
106}
107
SunnySrivastava1984d076da82020-03-05 05:33:35 -0600108/** @brief Encodes a keyword for D-Bus.
109 */
110string encodeKeyword(const string& kw, const string& encoding)
111{
112 if (encoding == "MAC")
113 {
114 string res{};
115 size_t first = kw[0];
116 res += toHex(first >> 4);
117 res += toHex(first & 0x0f);
118 for (size_t i = 1; i < kw.size(); ++i)
119 {
120 res += ":";
121 res += toHex(kw[i] >> 4);
122 res += toHex(kw[i] & 0x0f);
123 }
124 return res;
125 }
126 else if (encoding == "DATE")
127 {
128 // Date, represent as
129 // <year>-<month>-<day> <hour>:<min>
130 string res{};
131 static constexpr uint8_t skipPrefix = 3;
132
133 auto strItr = kw.begin();
134 advance(strItr, skipPrefix);
135 for_each(strItr, kw.end(), [&res](size_t c) { res += c; });
136
137 res.insert(BD_YEAR_END, 1, '-');
138 res.insert(BD_MONTH_END, 1, '-');
139 res.insert(BD_DAY_END, 1, ' ');
140 res.insert(BD_HOUR_END, 1, ':');
141
142 return res;
143 }
144 else // default to string encoding
145 {
146 return string(kw.begin(), kw.end());
147 }
148}
SunnySrivastava198443306542020-04-01 02:50:20 -0500149
150string readBusProperty(const string& obj, const string& inf, const string& prop)
151{
152 std::string propVal{};
153 std::string object = INVENTORY_PATH + obj;
154 auto bus = sdbusplus::bus::new_default();
155 auto properties = bus.new_method_call(
156 "xyz.openbmc_project.Inventory.Manager", object.c_str(),
157 "org.freedesktop.DBus.Properties", "Get");
158 properties.append(inf);
159 properties.append(prop);
160 auto result = bus.call(properties);
161 if (!result.is_method_error())
162 {
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500163 variant<Binary, string> val;
SunnySrivastava198443306542020-04-01 02:50:20 -0500164 result.read(val);
SunnySrivastava198443306542020-04-01 02:50:20 -0500165 if (auto pVal = get_if<Binary>(&val))
166 {
167 propVal.assign(reinterpret_cast<const char*>(pVal->data()),
168 pVal->size());
169 }
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500170 else if (auto pVal = get_if<string>(&val))
171 {
172 propVal.assign(pVal->data(), pVal->size());
173 }
SunnySrivastava198443306542020-04-01 02:50:20 -0500174 }
175 return propVal;
176}
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700177} // namespace vpd
178} // namespace openpower