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" |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 4 | #include "types.hpp" |
| 5 | |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 6 | #include <iostream> |
| 7 | |
| 8 | using namespace std; |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 9 | |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 10 | namespace openpower |
| 11 | { |
| 12 | namespace vpd |
| 13 | { |
SunnySrivastava1984 | 945a02d | 2020-05-06 01:55:41 -0500 | [diff] [blame] | 14 | |
Santosh Puranik | bd011b2 | 2020-01-23 04:05:25 -0600 | [diff] [blame] | 15 | /** @brief Return the hex representation of the incoming byte |
| 16 | * |
| 17 | * @param [in] c - The input byte |
| 18 | * @returns The hex representation of the byte as a character. |
| 19 | */ |
| 20 | constexpr auto toHex(size_t c) |
| 21 | { |
| 22 | constexpr auto map = "0123456789abcdef"; |
| 23 | return map[c]; |
| 24 | } |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 25 | |
| 26 | namespace inventory |
| 27 | { |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 28 | /** @brief Api to obtain a dictionary of path -> services |
| 29 | * where path is in subtree and services is of the type |
| 30 | * returned by the GetObject method. |
| 31 | * |
| 32 | * @param [in] root - Root path for object subtree |
| 33 | * @param [in] depth - Maximum subtree depth required |
| 34 | * @param [in] interfaces - Array to interfaces for which |
| 35 | * result is required. |
| 36 | * @return A dictionary of Path -> services |
| 37 | */ |
| 38 | MapperResponse |
| 39 | getObjectSubtreeForInterfaces(const std::string& root, const int32_t depth, |
| 40 | const std::vector<std::string>& interfaces); |
| 41 | |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 42 | } // namespace inventory |
| 43 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 44 | /**@brief This API reads 2 Bytes of data and swap the read data |
| 45 | * @param[in] iterator- Pointer pointing to the data to be read |
| 46 | * @return returns 2 Byte data read at the given pointer |
| 47 | */ |
| 48 | openpower::vpd::constants::LE2ByteData |
| 49 | readUInt16LE(Binary::const_iterator iterator); |
| 50 | |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 51 | /** @brief Encodes a keyword for D-Bus. |
| 52 | * @param[in] kw - kwd data in string format |
| 53 | * @param[in] encoding - required for kwd data |
| 54 | */ |
| 55 | string encodeKeyword(const string& kw, const string& encoding); |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 56 | |
| 57 | /** @brief Reads a property from the inventory manager given object path, |
| 58 | * intreface and property. |
| 59 | * @param[in] obj - object path |
| 60 | * @param[in] inf - interface |
| 61 | * @param[in] prop - property whose value is fetched |
| 62 | * @return [out] - value of the property |
| 63 | */ |
| 64 | string readBusProperty(const string& obj, const string& inf, |
| 65 | const string& prop); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * @brief API to create PEL entry |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 69 | * @param[in] additionalData - Map holding the additional data |
| 70 | * @param[in] sev - Severity |
| 71 | * @param[in] errIntf - error interface |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 72 | */ |
| 73 | void createPEL(const std::map<std::string, std::string>& additionalData, |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 74 | const constants::PelSeverity& sev, const std::string& errIntf); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 75 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 76 | /** |
| 77 | * @brief getVpdFilePath |
| 78 | * Get vpd file path corresponding to the given object path. |
| 79 | * @param[in] - json file path |
| 80 | * @param[in] - Object path |
| 81 | * @return - Vpd file path |
| 82 | */ |
| 83 | inventory::VPDfilepath getVpdFilePath(const string& jsonFile, |
| 84 | const std::string& ObjPath); |
| 85 | |
| 86 | /** |
| 87 | * @brief isPathInJson |
| 88 | * API which checks for the presence of the given eeprom path in the given json. |
| 89 | * @param[in] - eepromPath |
| 90 | * @return - true if the eeprom is present in the json; false otherwise |
| 91 | */ |
| 92 | bool isPathInJson(const std::string& eepromPath); |
| 93 | |
| 94 | /** |
| 95 | * @brief isRecKwInDbusJson |
| 96 | * API which checks whether the given keyword under the given record is to be |
| 97 | * published on dbus or not. Checks against the keywords present in |
| 98 | * dbus_property.json. |
| 99 | * @param[in] - record name |
| 100 | * @param[in] - keyword name |
| 101 | * @return - true if the record-keyword pair is present in dbus_property.json; |
| 102 | * false otherwise. |
| 103 | */ |
| 104 | bool isRecKwInDbusJson(const std::string& record, const std::string& keyword); |
| 105 | |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 106 | /** |
| 107 | * @brief Check the type of VPD. |
| 108 | * |
| 109 | * Checks the type of vpd based on the start tag. |
| 110 | * @param[in] vector - Vpd data in vector format |
| 111 | * |
| 112 | * @return enum of type vpdType |
| 113 | */ |
| 114 | constants::vpdType vpdTypeCheck(const Binary& vector); |
| 115 | |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame^] | 116 | /* |
| 117 | * @brief This method does nothing. Just an empty function to return null |
| 118 | * at the end of variadic template args |
| 119 | */ |
| 120 | inline string getCommand() |
| 121 | { |
| 122 | return ""; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @brief This function to arrange all arguments to make commandy |
| 127 | * @param[in] arguments to create the command |
| 128 | * @return cmd - command string |
| 129 | */ |
| 130 | template <typename T, typename... Types> |
| 131 | inline string getCommand(T arg1, Types... args) |
| 132 | { |
| 133 | string cmd = " " + arg1 + getCommand(args...); |
| 134 | |
| 135 | return cmd; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @brief This API takes arguments, creates a shell command line and executes |
| 140 | * them. |
| 141 | * @param[in] arguments for command |
| 142 | * @returns output of that command |
| 143 | */ |
| 144 | template <typename T, typename... Types> |
| 145 | inline vector<string> executeCmd(T&& path, Types... args) |
| 146 | { |
| 147 | vector<string> stdOutput; |
| 148 | array<char, 128> buffer; |
| 149 | |
| 150 | string cmd = path + getCommand(args...); |
| 151 | |
| 152 | unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose); |
| 153 | if (!pipe) |
| 154 | { |
| 155 | throw runtime_error("popen() failed!"); |
| 156 | } |
| 157 | while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) |
| 158 | { |
| 159 | stdOutput.emplace_back(buffer.data()); |
| 160 | } |
| 161 | |
| 162 | return stdOutput; |
| 163 | } |
| 164 | |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 165 | } // namespace vpd |
| 166 | } // namespace openpower |