Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 1 | #include "utils.hpp" |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 2 | |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 3 | #include "defines.hpp" |
| 4 | |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
| 6 | #include <sdbusplus/server.hpp> |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 7 | |
| 8 | namespace openpower |
| 9 | { |
| 10 | namespace vpd |
| 11 | { |
| 12 | |
| 13 | namespace inventory |
| 14 | { |
| 15 | |
| 16 | auto getPIMService() |
| 17 | { |
| 18 | auto bus = sdbusplus::bus::new_default(); |
| 19 | auto mapper = |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 20 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 21 | "/xyz/openbmc_project/object_mapper", |
| 22 | "xyz.openbmc_project.ObjectMapper", "GetObject"); |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 23 | |
| 24 | mapper.append(pimPath); |
| 25 | mapper.append(std::vector<std::string>({pimIntf})); |
| 26 | |
| 27 | auto result = bus.call(mapper); |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 28 | if (result.is_method_error()) |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 29 | { |
| 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 Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 35 | if (response.empty()) |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 36 | { |
| 37 | throw std::runtime_error("ObjectMapper GetObject bad response"); |
| 38 | } |
| 39 | |
| 40 | return response.begin()->first; |
| 41 | } |
| 42 | |
| 43 | void callPIM(ObjectMap&& objects) |
| 44 | { |
| 45 | std::string service; |
| 46 | |
| 47 | try |
| 48 | { |
| 49 | service = getPIMService(); |
| 50 | auto bus = sdbusplus::bus::new_default(); |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 51 | auto pimMsg = |
| 52 | bus.new_method_call(service.c_str(), pimPath, pimIntf, "Notify"); |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 53 | pimMsg.append(std::move(objects)); |
| 54 | auto result = bus.call(pimMsg); |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 55 | if (result.is_method_error()) |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 56 | { |
| 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 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 69 | using namespace openpower::vpd::constants; |
| 70 | LE2ByteData readUInt16LE(Binary::const_iterator iterator) |
| 71 | { |
| 72 | LE2ByteData lowByte = *iterator; |
| 73 | LE2ByteData highByte = *(iterator + 1); |
| 74 | lowByte |= (highByte << 8); |
| 75 | return lowByte; |
| 76 | } |
| 77 | |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 78 | /** @brief Encodes a keyword for D-Bus. |
| 79 | */ |
| 80 | string 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 Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 119 | } // namespace vpd |
| 120 | } // namespace openpower |