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 | { |
SunnySrivastava1984 | 945a02d | 2020-05-06 01:55:41 -0500 | [diff] [blame] | 14 | using namespace openpower::vpd::constants; |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame^] | 15 | using namespace inventory; |
| 16 | using namespace phosphor::logging; |
| 17 | |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 18 | namespace inventory |
| 19 | { |
| 20 | |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame^] | 21 | std::string getService(sdbusplus::bus::bus& bus, const std::string& path, |
| 22 | const std::string& interface) |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 23 | { |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame^] | 24 | auto mapper = bus.new_method_call(mapperDestination, mapperObjectPath, |
| 25 | mapperInterface, "GetObject"); |
| 26 | mapper.append(path, std::vector<std::string>({interface})); |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 27 | |
| 28 | std::map<std::string, std::vector<std::string>> response; |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame^] | 29 | try |
| 30 | { |
| 31 | auto reply = bus.call(mapper); |
| 32 | reply.read(response); |
| 33 | } |
| 34 | catch (const sdbusplus::exception::SdBusError& e) |
| 35 | { |
| 36 | log<level::ERR>("D-Bus call exception", |
| 37 | entry("OBJPATH=%s", mapperObjectPath), |
| 38 | entry("INTERFACE=%s", mapperInterface), |
| 39 | entry("EXCEPTION=%s", e.what())); |
| 40 | |
| 41 | throw std::runtime_error("Service name is not found"); |
| 42 | } |
| 43 | |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 44 | if (response.empty()) |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 45 | { |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame^] | 46 | throw std::runtime_error("Service name response is empty"); |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | return response.begin()->first; |
| 50 | } |
| 51 | |
| 52 | void callPIM(ObjectMap&& objects) |
| 53 | { |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 54 | try |
| 55 | { |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 56 | auto bus = sdbusplus::bus::new_default(); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame^] | 57 | auto service = getService(bus, pimPath, pimIntf); |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 58 | auto pimMsg = |
| 59 | bus.new_method_call(service.c_str(), pimPath, pimIntf, "Notify"); |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 60 | pimMsg.append(std::move(objects)); |
| 61 | auto result = bus.call(pimMsg); |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 62 | if (result.is_method_error()) |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 63 | { |
| 64 | std::cerr << "PIM Notify() failed\n"; |
| 65 | } |
| 66 | } |
| 67 | catch (const std::runtime_error& e) |
| 68 | { |
| 69 | using namespace phosphor::logging; |
| 70 | log<level::ERR>(e.what()); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | } // namespace inventory |
| 75 | |
SunnySrivastava1984 | 945a02d | 2020-05-06 01:55:41 -0500 | [diff] [blame] | 76 | vpdType vpdTypeCheck(const Binary& vpdVector) |
| 77 | { |
| 78 | // Read first 3 Bytes to check the 11S bar code format |
| 79 | std::string is11SFormat = ""; |
| 80 | for (uint8_t i = 0; i < FORMAT_11S_LEN; i++) |
| 81 | { |
| 82 | is11SFormat += vpdVector[MEMORY_VPD_DATA_START + i]; |
| 83 | } |
| 84 | |
| 85 | if (vpdVector[IPZ_DATA_START] == KW_VAL_PAIR_START_TAG) |
| 86 | { |
| 87 | // IPZ VPD FORMAT |
| 88 | return vpdType::IPZ_VPD; |
| 89 | } |
| 90 | else if (vpdVector[KW_VPD_DATA_START] == KW_VPD_START_TAG) |
| 91 | { |
| 92 | // KEYWORD VPD FORMAT |
| 93 | return vpdType::KEYWORD_VPD; |
| 94 | } |
| 95 | else if (is11SFormat.compare(MEMORY_VPD_START_TAG) == 0) |
| 96 | { |
| 97 | // Memory VPD format |
| 98 | return vpdType::MEMORY_VPD; |
| 99 | } |
| 100 | |
| 101 | // INVALID VPD FORMAT |
| 102 | return vpdType::INVALID_VPD_FORMAT; |
| 103 | } |
| 104 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 105 | LE2ByteData readUInt16LE(Binary::const_iterator iterator) |
| 106 | { |
| 107 | LE2ByteData lowByte = *iterator; |
| 108 | LE2ByteData highByte = *(iterator + 1); |
| 109 | lowByte |= (highByte << 8); |
| 110 | return lowByte; |
| 111 | } |
| 112 | |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 113 | /** @brief Encodes a keyword for D-Bus. |
| 114 | */ |
| 115 | string encodeKeyword(const string& kw, const string& encoding) |
| 116 | { |
| 117 | if (encoding == "MAC") |
| 118 | { |
| 119 | string res{}; |
| 120 | size_t first = kw[0]; |
| 121 | res += toHex(first >> 4); |
| 122 | res += toHex(first & 0x0f); |
| 123 | for (size_t i = 1; i < kw.size(); ++i) |
| 124 | { |
| 125 | res += ":"; |
| 126 | res += toHex(kw[i] >> 4); |
| 127 | res += toHex(kw[i] & 0x0f); |
| 128 | } |
| 129 | return res; |
| 130 | } |
| 131 | else if (encoding == "DATE") |
| 132 | { |
| 133 | // Date, represent as |
| 134 | // <year>-<month>-<day> <hour>:<min> |
| 135 | string res{}; |
| 136 | static constexpr uint8_t skipPrefix = 3; |
| 137 | |
| 138 | auto strItr = kw.begin(); |
| 139 | advance(strItr, skipPrefix); |
| 140 | for_each(strItr, kw.end(), [&res](size_t c) { res += c; }); |
| 141 | |
| 142 | res.insert(BD_YEAR_END, 1, '-'); |
| 143 | res.insert(BD_MONTH_END, 1, '-'); |
| 144 | res.insert(BD_DAY_END, 1, ' '); |
| 145 | res.insert(BD_HOUR_END, 1, ':'); |
| 146 | |
| 147 | return res; |
| 148 | } |
| 149 | else // default to string encoding |
| 150 | { |
| 151 | return string(kw.begin(), kw.end()); |
| 152 | } |
| 153 | } |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 154 | |
| 155 | string readBusProperty(const string& obj, const string& inf, const string& prop) |
| 156 | { |
| 157 | std::string propVal{}; |
| 158 | std::string object = INVENTORY_PATH + obj; |
| 159 | auto bus = sdbusplus::bus::new_default(); |
| 160 | auto properties = bus.new_method_call( |
| 161 | "xyz.openbmc_project.Inventory.Manager", object.c_str(), |
| 162 | "org.freedesktop.DBus.Properties", "Get"); |
| 163 | properties.append(inf); |
| 164 | properties.append(prop); |
| 165 | auto result = bus.call(properties); |
| 166 | if (!result.is_method_error()) |
| 167 | { |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 168 | variant<Binary, string> val; |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 169 | result.read(val); |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 170 | if (auto pVal = get_if<Binary>(&val)) |
| 171 | { |
| 172 | propVal.assign(reinterpret_cast<const char*>(pVal->data()), |
| 173 | pVal->size()); |
| 174 | } |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 175 | else if (auto pVal = get_if<string>(&val)) |
| 176 | { |
| 177 | propVal.assign(pVal->data(), pVal->size()); |
| 178 | } |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 179 | } |
| 180 | return propVal; |
| 181 | } |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame^] | 182 | |
| 183 | void createPEL(const std::map<std::string, std::string>& additionalData, |
| 184 | const std::string& errIntf) |
| 185 | { |
| 186 | try |
| 187 | { |
| 188 | auto bus = sdbusplus::bus::new_default(); |
| 189 | |
| 190 | auto service = getService(bus, loggerObjectPath, loggerCreateInterface); |
| 191 | auto method = bus.new_method_call(service.c_str(), loggerObjectPath, |
| 192 | loggerCreateInterface, "Create"); |
| 193 | |
| 194 | method.append(errIntf, "xyz.openbmc_project.Logging.Entry.Level.Error", |
| 195 | additionalData); |
| 196 | auto resp = bus.call(method); |
| 197 | } |
| 198 | catch (const sdbusplus::exception::SdBusError& e) |
| 199 | { |
| 200 | throw std::runtime_error( |
| 201 | "Error in invoking D-Bus logging create interface to register PEL"); |
| 202 | } |
| 203 | } |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 204 | } // namespace vpd |
| 205 | } // namespace openpower |