Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 3 | #include "const.hpp" |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 4 | #include "store.hpp" |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 5 | #include "types.hpp" |
| 6 | |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 7 | #include <iostream> |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 8 | #include <nlohmann/json.hpp> |
Santosh Puranik | 53b38ed | 2022-04-10 23:15:22 +0530 | [diff] [blame] | 9 | #include <optional> |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 10 | |
| 11 | using namespace std; |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 12 | |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 13 | namespace openpower |
| 14 | { |
| 15 | namespace vpd |
| 16 | { |
SunnySrivastava1984 | 945a02d | 2020-05-06 01:55:41 -0500 | [diff] [blame] | 17 | |
Santosh Puranik | bd011b2 | 2020-01-23 04:05:25 -0600 | [diff] [blame] | 18 | /** @brief Return the hex representation of the incoming byte |
| 19 | * |
| 20 | * @param [in] c - The input byte |
| 21 | * @returns The hex representation of the byte as a character. |
| 22 | */ |
| 23 | constexpr auto toHex(size_t c) |
| 24 | { |
| 25 | constexpr auto map = "0123456789abcdef"; |
| 26 | return map[c]; |
| 27 | } |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 28 | |
| 29 | namespace inventory |
| 30 | { |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 31 | /** @brief API to obtain a dictionary of path -> services |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 32 | * where path is in subtree and services is of the type |
| 33 | * returned by the GetObject method. |
| 34 | * |
| 35 | * @param [in] root - Root path for object subtree |
| 36 | * @param [in] depth - Maximum subtree depth required |
| 37 | * @param [in] interfaces - Array to interfaces for which |
| 38 | * result is required. |
| 39 | * @return A dictionary of Path -> services |
| 40 | */ |
| 41 | MapperResponse |
| 42 | getObjectSubtreeForInterfaces(const std::string& root, const int32_t depth, |
| 43 | const std::vector<std::string>& interfaces); |
| 44 | |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 45 | } // namespace inventory |
| 46 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 47 | /**@brief This API reads 2 Bytes of data and swap the read data |
| 48 | * @param[in] iterator- Pointer pointing to the data to be read |
| 49 | * @return returns 2 Byte data read at the given pointer |
| 50 | */ |
| 51 | openpower::vpd::constants::LE2ByteData |
| 52 | readUInt16LE(Binary::const_iterator iterator); |
| 53 | |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 54 | /** @brief Encodes a keyword for D-Bus. |
| 55 | * @param[in] kw - kwd data in string format |
| 56 | * @param[in] encoding - required for kwd data |
| 57 | */ |
| 58 | string encodeKeyword(const string& kw, const string& encoding); |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 59 | |
| 60 | /** @brief Reads a property from the inventory manager given object path, |
| 61 | * intreface and property. |
| 62 | * @param[in] obj - object path |
| 63 | * @param[in] inf - interface |
| 64 | * @param[in] prop - property whose value is fetched |
| 65 | * @return [out] - value of the property |
| 66 | */ |
| 67 | string readBusProperty(const string& obj, const string& inf, |
| 68 | const string& prop); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * @brief API to create PEL entry |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 72 | * @param[in] additionalData - Map holding the additional data |
| 73 | * @param[in] sev - Severity |
| 74 | * @param[in] errIntf - error interface |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 75 | */ |
| 76 | void createPEL(const std::map<std::string, std::string>& additionalData, |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 77 | const constants::PelSeverity& sev, const std::string& errIntf); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 78 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 79 | /** |
| 80 | * @brief getVpdFilePath |
| 81 | * Get vpd file path corresponding to the given object path. |
| 82 | * @param[in] - json file path |
| 83 | * @param[in] - Object path |
| 84 | * @return - Vpd file path |
| 85 | */ |
| 86 | inventory::VPDfilepath getVpdFilePath(const string& jsonFile, |
| 87 | const std::string& ObjPath); |
| 88 | |
| 89 | /** |
| 90 | * @brief isPathInJson |
| 91 | * API which checks for the presence of the given eeprom path in the given json. |
| 92 | * @param[in] - eepromPath |
| 93 | * @return - true if the eeprom is present in the json; false otherwise |
| 94 | */ |
| 95 | bool isPathInJson(const std::string& eepromPath); |
| 96 | |
| 97 | /** |
| 98 | * @brief isRecKwInDbusJson |
| 99 | * API which checks whether the given keyword under the given record is to be |
| 100 | * published on dbus or not. Checks against the keywords present in |
| 101 | * dbus_property.json. |
| 102 | * @param[in] - record name |
| 103 | * @param[in] - keyword name |
| 104 | * @return - true if the record-keyword pair is present in dbus_property.json; |
| 105 | * false otherwise. |
| 106 | */ |
| 107 | bool isRecKwInDbusJson(const std::string& record, const std::string& keyword); |
| 108 | |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 109 | /** |
| 110 | * @brief Check the type of VPD. |
| 111 | * |
| 112 | * Checks the type of vpd based on the start tag. |
| 113 | * @param[in] vector - Vpd data in vector format |
| 114 | * |
| 115 | * @return enum of type vpdType |
| 116 | */ |
| 117 | constants::vpdType vpdTypeCheck(const Binary& vector); |
| 118 | |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame] | 119 | /* |
| 120 | * @brief This method does nothing. Just an empty function to return null |
| 121 | * at the end of variadic template args |
| 122 | */ |
| 123 | inline string getCommand() |
| 124 | { |
| 125 | return ""; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @brief This function to arrange all arguments to make commandy |
| 130 | * @param[in] arguments to create the command |
| 131 | * @return cmd - command string |
| 132 | */ |
| 133 | template <typename T, typename... Types> |
| 134 | inline string getCommand(T arg1, Types... args) |
| 135 | { |
| 136 | string cmd = " " + arg1 + getCommand(args...); |
| 137 | |
| 138 | return cmd; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @brief This API takes arguments, creates a shell command line and executes |
| 143 | * them. |
| 144 | * @param[in] arguments for command |
| 145 | * @returns output of that command |
| 146 | */ |
| 147 | template <typename T, typename... Types> |
| 148 | inline vector<string> executeCmd(T&& path, Types... args) |
| 149 | { |
| 150 | vector<string> stdOutput; |
| 151 | array<char, 128> buffer; |
| 152 | |
| 153 | string cmd = path + getCommand(args...); |
| 154 | |
| 155 | unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose); |
| 156 | if (!pipe) |
| 157 | { |
| 158 | throw runtime_error("popen() failed!"); |
| 159 | } |
| 160 | while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) |
| 161 | { |
| 162 | stdOutput.emplace_back(buffer.data()); |
| 163 | } |
| 164 | |
| 165 | return stdOutput; |
| 166 | } |
| 167 | |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 168 | /** @brief This API checks for IM and HW keywords, and based |
| 169 | * on these values decides which system json to be used. |
| 170 | * @param[in] vpdMap - parsed vpd |
| 171 | * @returns System json path |
| 172 | */ |
| 173 | string getSystemsJson(const Parsed& vpdMap); |
| 174 | |
| 175 | /** @brief Reads HW Keyword from the vpd |
| 176 | * @param[in] vpdMap - parsed vpd |
| 177 | * @returns value of HW Keyword |
| 178 | */ |
| 179 | const string getHW(const Parsed& vpdMap); |
| 180 | |
| 181 | /** @brief Reads IM Keyword from the vpd |
| 182 | * @param[in] vpdMap - parsed vpd |
| 183 | * @returns value of IM Keyword |
| 184 | */ |
| 185 | const string getIM(const Parsed& vpdMap); |
| 186 | |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 187 | /** @brief Translate udev event generated path to a generic /sys/bus eeprom path |
| 188 | * @param[io] file - path generated from udev event. |
| 189 | */ |
| 190 | void udevToGenericPath(string& file); |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * @brief API to generate a vpd name in some pattern. |
| 194 | * This vpd-name denotes name of the bad vpd file. |
| 195 | * For i2c eeproms - the pattern of the vpd-name will be |
| 196 | * i2c-<bus-number>-<eeprom-address>. For spi eeproms - the pattern of the |
| 197 | * vpd-name will be spi-<spi-number>. |
| 198 | * |
| 199 | * @param[in] file - file path of the vpd |
| 200 | * @return the vpd-name. |
| 201 | */ |
| 202 | string getBadVpdName(const string& file); |
| 203 | |
| 204 | /** |
| 205 | * @brief API which dumps the broken/bad vpd in a directory |
| 206 | * When the vpd is bad, this api places the bad vpd file inside |
| 207 | * "/tmp/bad-vpd" in BMC, in order to collect bad VPD data as a part of user |
| 208 | * initiated BMC dump. |
| 209 | * |
| 210 | * @param[in] file - bad vpd file path |
| 211 | * @param[in] vpdVector - bad vpd vector |
| 212 | */ |
| 213 | void dumpBadVpd(const std::string& file, const Binary& vpdVector); |
alpana07 | 7ce6872 | 2021-07-25 13:23:59 -0500 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | * @brief This function fetches the value for given keyword in the given record |
| 217 | * from vpd data and returns this value. |
| 218 | * |
| 219 | * @param[in] vpdMap - vpd to find out the data |
| 220 | * @param[in] rec - Record under which desired keyword exists |
| 221 | * @param[in] kwd - keyword to read the data from |
| 222 | * |
| 223 | * @returns keyword value if record/keyword combination found |
| 224 | * empty string if record or keyword is not found. |
| 225 | */ |
| 226 | const string getKwVal(const Parsed& vpdMap, const string& rec, |
| 227 | const string& kwd); |
| 228 | |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 229 | /** @brief This creates a complete command using all it's input parameters, |
| 230 | * to bind or unbind the driver. |
| 231 | * @param[in] devNameAddr - device address on that bus |
| 232 | * @param[in] busType - i2c, spi |
| 233 | * @param[in] driverType - type of driver like at24 |
| 234 | * @param[in] bindOrUnbind - either bind or unbind |
| 235 | * @returns Command to bind or unbind the driver. |
| 236 | */ |
| 237 | inline string createBindUnbindDriverCmnd(const string& devNameAddr, |
| 238 | const string& busType, |
| 239 | const string& driverType, |
| 240 | const string& bindOrUnbind) |
| 241 | { |
| 242 | return ("echo " + devNameAddr + " > /sys/bus/" + busType + "/drivers/" + |
| 243 | driverType + "/" + bindOrUnbind); |
| 244 | } |
| 245 | |
Priyanga Ramasamy | 0243493 | 2021-10-07 16:26:05 -0500 | [diff] [blame] | 246 | /** |
| 247 | * @brief Get Printable Value |
| 248 | * |
| 249 | * Checks if the vector value has non printable characters. |
| 250 | * Returns hex value if non printable char is found else |
| 251 | * returns ascii value. |
| 252 | * |
| 253 | * @param[in] vector - Reference of the Binary vector |
| 254 | * @return printable value - either in hex or in ascii. |
| 255 | */ |
Priyanga Ramasamy | c9ecf8e | 2021-10-08 02:28:52 -0500 | [diff] [blame] | 256 | string getPrintableValue(const Binary& vec); |
| 257 | |
| 258 | /** |
| 259 | * @brief Convert byte array to hex string. |
| 260 | * @param[in] vec - byte array |
| 261 | * @return hexadecimal string of bytes. |
| 262 | */ |
| 263 | string byteArrayToHexString(const Binary& vec); |
Priyanga Ramasamy | 0243493 | 2021-10-07 16:26:05 -0500 | [diff] [blame] | 264 | |
Priyanga Ramasamy | aa8a893 | 2022-01-27 09:12:41 -0600 | [diff] [blame] | 265 | /** |
Santosh Puranik | 53b38ed | 2022-04-10 23:15:22 +0530 | [diff] [blame] | 266 | * @brief Return presence of the FRU. |
| 267 | * |
| 268 | * This API returns the presence information of the FRU corresponding to the |
| 269 | * given EEPROM. If the JSON contains no information about presence detect, this |
| 270 | * will return an empty optional. Else it will get the presence GPIO information |
| 271 | * from the JSON and return the appropriate present status. |
| 272 | * In case of GPIO find/read errors, it will return false. |
| 273 | * |
| 274 | * @param[in] json - The VPD JSON |
| 275 | * @param[in] file - EEPROM file path |
| 276 | * @return Empty optional if there is no presence info. Else returns presence |
| 277 | * based on the GPIO read. |
| 278 | */ |
| 279 | std::optional<bool> isPresent(const nlohmann::json& json, const string& file); |
| 280 | |
| 281 | /** |
| 282 | * @brief Performs any pre-action needed to get the FRU setup for |
| 283 | * collection. |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 284 | * |
| 285 | * @param[in] json - json object |
| 286 | * @param[in] file - eeprom file path |
| 287 | * @return - success or failure |
| 288 | */ |
| 289 | bool executePreAction(const nlohmann::json& json, const string& file); |
| 290 | |
| 291 | /** |
| 292 | * @brief This API will be called at the end of VPD collection to perform any |
| 293 | * post actions. |
| 294 | * |
| 295 | * @param[in] json - json object |
| 296 | * @param[in] file - eeprom file path |
| 297 | */ |
| 298 | void executePostFailAction(const nlohmann::json& json, const string& file); |
| 299 | |
| 300 | /** |
Priyanga Ramasamy | aa8a893 | 2022-01-27 09:12:41 -0600 | [diff] [blame] | 301 | * @brief Helper function to insert or merge in map. |
| 302 | * |
| 303 | * This method checks in the given inventory::InterfaceMap if the given |
| 304 | * interface key is existing or not. If the interface key already exists, given |
| 305 | * property map is inserted into it. If the key does'nt exist then given |
| 306 | * interface and property map pair is newly created. If the property present in |
| 307 | * propertymap already exist in the InterfaceMap, then the new property value is |
| 308 | * ignored. |
| 309 | * |
| 310 | * @param[in,out] map - map object of type inventory::InterfaceMap only. |
| 311 | * @param[in] interface - Interface name. |
| 312 | * @param[in] property - new property map that needs to be emplaced. |
| 313 | */ |
| 314 | void insertOrMerge(inventory::InterfaceMap& map, |
| 315 | const inventory::Interface& interface, |
| 316 | inventory::PropertyMap&& property); |
| 317 | |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 318 | } // namespace vpd |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 319 | } // namespace openpower |