| SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 1 | #include "config.h" | 
 | 2 |  | 
| Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 3 | #include "utils.hpp" | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 4 |  | 
| SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 5 | #include "defines.hpp" | 
 | 6 |  | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 7 | #include <phosphor-logging/log.hpp> | 
 | 8 | #include <sdbusplus/server.hpp> | 
| Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 9 |  | 
 | 10 | namespace openpower | 
 | 11 | { | 
 | 12 | namespace vpd | 
 | 13 | { | 
 | 14 |  | 
 | 15 | namespace inventory | 
 | 16 | { | 
 | 17 |  | 
 | 18 | auto getPIMService() | 
 | 19 | { | 
 | 20 |     auto bus = sdbusplus::bus::new_default(); | 
 | 21 |     auto mapper = | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 22 |         bus.new_method_call("xyz.openbmc_project.ObjectMapper", | 
 | 23 |                             "/xyz/openbmc_project/object_mapper", | 
 | 24 |                             "xyz.openbmc_project.ObjectMapper", "GetObject"); | 
| Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 25 |  | 
 | 26 |     mapper.append(pimPath); | 
 | 27 |     mapper.append(std::vector<std::string>({pimIntf})); | 
 | 28 |  | 
 | 29 |     auto result = bus.call(mapper); | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 30 |     if (result.is_method_error()) | 
| Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 31 |     { | 
 | 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 Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 37 |     if (response.empty()) | 
| Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 38 |     { | 
 | 39 |         throw std::runtime_error("ObjectMapper GetObject bad response"); | 
 | 40 |     } | 
 | 41 |  | 
 | 42 |     return response.begin()->first; | 
 | 43 | } | 
 | 44 |  | 
 | 45 | void callPIM(ObjectMap&& objects) | 
 | 46 | { | 
 | 47 |     std::string service; | 
 | 48 |  | 
 | 49 |     try | 
 | 50 |     { | 
 | 51 |         service = getPIMService(); | 
 | 52 |         auto bus = sdbusplus::bus::new_default(); | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 53 |         auto pimMsg = | 
 | 54 |             bus.new_method_call(service.c_str(), pimPath, pimIntf, "Notify"); | 
| Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 55 |         pimMsg.append(std::move(objects)); | 
 | 56 |         auto result = bus.call(pimMsg); | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 57 |         if (result.is_method_error()) | 
| Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 58 |         { | 
 | 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 |  | 
| SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 71 | using namespace openpower::vpd::constants; | 
 | 72 | LE2ByteData readUInt16LE(Binary::const_iterator iterator) | 
 | 73 | { | 
 | 74 |     LE2ByteData lowByte = *iterator; | 
 | 75 |     LE2ByteData highByte = *(iterator + 1); | 
 | 76 |     lowByte |= (highByte << 8); | 
 | 77 |     return lowByte; | 
 | 78 | } | 
 | 79 |  | 
| SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 80 | /** @brief Encodes a keyword for D-Bus. | 
 | 81 |  */ | 
 | 82 | string 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 | } | 
| SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 121 |  | 
 | 122 | string 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 |     { | 
| SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame^] | 135 |         variant<Binary, string> val; | 
| SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 136 |         result.read(val); | 
| SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 137 |         if (auto pVal = get_if<Binary>(&val)) | 
 | 138 |         { | 
 | 139 |             propVal.assign(reinterpret_cast<const char*>(pVal->data()), | 
 | 140 |                            pVal->size()); | 
 | 141 |         } | 
| SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame^] | 142 |         else if (auto pVal = get_if<string>(&val)) | 
 | 143 |         { | 
 | 144 |             propVal.assign(pVal->data(), pVal->size()); | 
 | 145 |         } | 
| SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 146 |     } | 
 | 147 |     return propVal; | 
 | 148 | } | 
| Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 149 | } // namespace vpd | 
 | 150 | } // namespace openpower |