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