blob: 6f2bfe4c1c80242cd9893f09d19704a0bee117f2 [file] [log] [blame]
Deepak Kodihalli76794492017-02-16 23:48:18 -06001#include "utils.hpp"
Patrick Venturec83c4dc2018-11-01 16:29:18 -07002
SunnySrivastava1984d076da82020-03-05 05:33:35 -06003#include "defines.hpp"
4
Patrick Venturec83c4dc2018-11-01 16:29:18 -07005#include <phosphor-logging/log.hpp>
6#include <sdbusplus/server.hpp>
Deepak Kodihalli76794492017-02-16 23:48:18 -06007
8namespace openpower
9{
10namespace vpd
11{
12
13namespace inventory
14{
15
16auto getPIMService()
17{
18 auto bus = sdbusplus::bus::new_default();
19 auto mapper =
Patrick Venturec83c4dc2018-11-01 16:29:18 -070020 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
21 "/xyz/openbmc_project/object_mapper",
22 "xyz.openbmc_project.ObjectMapper", "GetObject");
Deepak Kodihalli76794492017-02-16 23:48:18 -060023
24 mapper.append(pimPath);
25 mapper.append(std::vector<std::string>({pimIntf}));
26
27 auto result = bus.call(mapper);
Patrick Venturec83c4dc2018-11-01 16:29:18 -070028 if (result.is_method_error())
Deepak Kodihalli76794492017-02-16 23:48:18 -060029 {
30 throw std::runtime_error("ObjectMapper GetObject failed");
31 }
32
33 std::map<std::string, std::vector<std::string>> response;
34 result.read(response);
Patrick Venturec83c4dc2018-11-01 16:29:18 -070035 if (response.empty())
Deepak Kodihalli76794492017-02-16 23:48:18 -060036 {
37 throw std::runtime_error("ObjectMapper GetObject bad response");
38 }
39
40 return response.begin()->first;
41}
42
43void callPIM(ObjectMap&& objects)
44{
45 std::string service;
46
47 try
48 {
49 service = getPIMService();
50 auto bus = sdbusplus::bus::new_default();
Patrick Venturec83c4dc2018-11-01 16:29:18 -070051 auto pimMsg =
52 bus.new_method_call(service.c_str(), pimPath, pimIntf, "Notify");
Deepak Kodihalli76794492017-02-16 23:48:18 -060053 pimMsg.append(std::move(objects));
54 auto result = bus.call(pimMsg);
Patrick Venturec83c4dc2018-11-01 16:29:18 -070055 if (result.is_method_error())
Deepak Kodihalli76794492017-02-16 23:48:18 -060056 {
57 std::cerr << "PIM Notify() failed\n";
58 }
59 }
60 catch (const std::runtime_error& e)
61 {
62 using namespace phosphor::logging;
63 log<level::ERR>(e.what());
64 }
65}
66
67} // namespace inventory
68
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060069using namespace openpower::vpd::constants;
70LE2ByteData readUInt16LE(Binary::const_iterator iterator)
71{
72 LE2ByteData lowByte = *iterator;
73 LE2ByteData highByte = *(iterator + 1);
74 lowByte |= (highByte << 8);
75 return lowByte;
76}
77
SunnySrivastava1984d076da82020-03-05 05:33:35 -060078/** @brief Encodes a keyword for D-Bus.
79 */
80string encodeKeyword(const string& kw, const string& encoding)
81{
82 if (encoding == "MAC")
83 {
84 string res{};
85 size_t first = kw[0];
86 res += toHex(first >> 4);
87 res += toHex(first & 0x0f);
88 for (size_t i = 1; i < kw.size(); ++i)
89 {
90 res += ":";
91 res += toHex(kw[i] >> 4);
92 res += toHex(kw[i] & 0x0f);
93 }
94 return res;
95 }
96 else if (encoding == "DATE")
97 {
98 // Date, represent as
99 // <year>-<month>-<day> <hour>:<min>
100 string res{};
101 static constexpr uint8_t skipPrefix = 3;
102
103 auto strItr = kw.begin();
104 advance(strItr, skipPrefix);
105 for_each(strItr, kw.end(), [&res](size_t c) { res += c; });
106
107 res.insert(BD_YEAR_END, 1, '-');
108 res.insert(BD_MONTH_END, 1, '-');
109 res.insert(BD_DAY_END, 1, ' ');
110 res.insert(BD_HOUR_END, 1, ':');
111
112 return res;
113 }
114 else // default to string encoding
115 {
116 return string(kw.begin(), kw.end());
117 }
118}
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700119} // namespace vpd
120} // namespace openpower