blob: 6002b9c2adf938f750e045bbdf535d68ec427c65 [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{
14
15namespace 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
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060071using namespace openpower::vpd::constants;
72LE2ByteData readUInt16LE(Binary::const_iterator iterator)
73{
74 LE2ByteData lowByte = *iterator;
75 LE2ByteData highByte = *(iterator + 1);
76 lowByte |= (highByte << 8);
77 return lowByte;
78}
79
SunnySrivastava1984d076da82020-03-05 05:33:35 -060080/** @brief Encodes a keyword for D-Bus.
81 */
82string encodeKeyword(const string& kw, const string& encoding)
83{
84 if (encoding == "MAC")
85 {
86 string res{};
87 size_t first = kw[0];
88 res += toHex(first >> 4);
89 res += toHex(first & 0x0f);
90 for (size_t i = 1; i < kw.size(); ++i)
91 {
92 res += ":";
93 res += toHex(kw[i] >> 4);
94 res += toHex(kw[i] & 0x0f);
95 }
96 return res;
97 }
98 else if (encoding == "DATE")
99 {
100 // Date, represent as
101 // <year>-<month>-<day> <hour>:<min>
102 string res{};
103 static constexpr uint8_t skipPrefix = 3;
104
105 auto strItr = kw.begin();
106 advance(strItr, skipPrefix);
107 for_each(strItr, kw.end(), [&res](size_t c) { res += c; });
108
109 res.insert(BD_YEAR_END, 1, '-');
110 res.insert(BD_MONTH_END, 1, '-');
111 res.insert(BD_DAY_END, 1, ' ');
112 res.insert(BD_HOUR_END, 1, ':');
113
114 return res;
115 }
116 else // default to string encoding
117 {
118 return string(kw.begin(), kw.end());
119 }
120}
SunnySrivastava198443306542020-04-01 02:50:20 -0500121
122string readBusProperty(const string& obj, const string& inf, const string& prop)
123{
124 std::string propVal{};
125 std::string object = INVENTORY_PATH + obj;
126 auto bus = sdbusplus::bus::new_default();
127 auto properties = bus.new_method_call(
128 "xyz.openbmc_project.Inventory.Manager", object.c_str(),
129 "org.freedesktop.DBus.Properties", "Get");
130 properties.append(inf);
131 properties.append(prop);
132 auto result = bus.call(properties);
133 if (!result.is_method_error())
134 {
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500135 variant<Binary, string> val;
SunnySrivastava198443306542020-04-01 02:50:20 -0500136 result.read(val);
SunnySrivastava198443306542020-04-01 02:50:20 -0500137 if (auto pVal = get_if<Binary>(&val))
138 {
139 propVal.assign(reinterpret_cast<const char*>(pVal->data()),
140 pVal->size());
141 }
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500142 else if (auto pVal = get_if<string>(&val))
143 {
144 propVal.assign(pVal->data(), pVal->size());
145 }
SunnySrivastava198443306542020-04-01 02:50:20 -0500146 }
147 return propVal;
148}
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700149} // namespace vpd
150} // namespace openpower