PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 3 | #include "editor_impl.hpp" |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 4 | #include "types.hpp" |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 5 | #include "utils.hpp" |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 6 | |
| 7 | #include <nlohmann/json.hpp> |
PriyangaRamasamy | 887a42a | 2020-09-03 00:33:57 +0530 | [diff] [blame] | 8 | #include <string> |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 9 | |
| 10 | using json = nlohmann::json; |
| 11 | |
| 12 | class VpdTool |
| 13 | { |
| 14 | private: |
| 15 | const std::string fruPath; |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 16 | const std::string recordName; |
| 17 | const std::string keyword; |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 18 | const std::string value; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 19 | |
Alpana Kumari | b6965f1 | 2020-06-01 00:32:21 -0500 | [diff] [blame] | 20 | // Store Type of FRU |
| 21 | std::string fruType; |
| 22 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 23 | /** |
| 24 | * @brief Debugger |
| 25 | * Displays the output in JSON. |
| 26 | * |
| 27 | * @param[in] output - json output to be displayed |
| 28 | */ |
| 29 | void debugger(json output); |
| 30 | |
| 31 | /** |
| 32 | * @brief make Dbus Call |
| 33 | * |
| 34 | * @param[in] objectName - dbus Object |
| 35 | * @param[in] interface - dbus Interface |
| 36 | * @param[in] kw - keyword under the interface |
| 37 | * |
| 38 | * @return dbus call response |
| 39 | */ |
| 40 | auto makeDBusCall(const std::string& objectName, |
| 41 | const std::string& interface, const std::string& kw); |
| 42 | |
| 43 | /** |
| 44 | * @brief Adds FRU type and Location Code |
| 45 | * Appends the type of the FRU and location code to the output |
| 46 | * |
| 47 | * @param[in] exIntf - extraInterfaces json from INVENTORY_JSON |
| 48 | * @param[in] object - The D-Bus object to read the location code from |
| 49 | * @param[out] kwVal - JSON object into which the FRU type and location code |
| 50 | * are placed |
| 51 | */ |
| 52 | void addFruTypeAndLocation(json exIntf, const std::string& object, |
| 53 | json& kwVal); |
| 54 | |
| 55 | /** |
| 56 | * @brief Get VINI properties |
| 57 | * Making a dbus call for properties [SN, PN, CC, FN, DR] |
| 58 | * under VINI interface. |
| 59 | * |
| 60 | * @param[in] invPath - Value of inventory Path |
| 61 | * @param[in] exIntf - extraInterfaces json from INVENTORY_JSON |
| 62 | * |
| 63 | * @return json output which gives the properties under invPath's VINI |
| 64 | * interface |
| 65 | */ |
| 66 | json getVINIProperties(std::string invPath, json exIntf); |
| 67 | |
| 68 | /** |
| 69 | * @brief Get ExtraInterface Properties |
| 70 | * Making a dbus call for those properties under extraInterfaces. |
| 71 | * |
| 72 | * @param[in] invPath - Value of inventory path |
| 73 | * @param[in] extraInterface - One of the invPath's extraInterfaces whose |
| 74 | * value is not null |
| 75 | * @param[in] prop - All properties of the extraInterface. |
| 76 | * |
| 77 | * @return json output which gives the properties under invPath's |
| 78 | * extraInterface. |
| 79 | */ |
| 80 | void getExtraInterfaceProperties(std::string invPath, |
| 81 | std::string extraInterface, json prop, |
| 82 | json exIntf, json& output); |
| 83 | |
| 84 | /** |
| 85 | * @brief Interface Decider |
| 86 | * Decides whether to make the dbus call for |
| 87 | * getting properites from extraInterface or from |
| 88 | * VINI interface, depending on the value of |
| 89 | * extraInterfaces object in the inventory json. |
| 90 | * |
| 91 | * @param[in] itemEEPROM - holds the reference of one of the EEPROM objects. |
| 92 | * |
| 93 | * @return json output for one of the EEPROM objects. |
| 94 | */ |
| 95 | json interfaceDecider(json& itemEEPROM); |
| 96 | |
| 97 | /** |
| 98 | * @brief Parse Inventory JSON |
| 99 | * Parses the complete inventory json and depending upon |
| 100 | * the user option makes the dbuscall for the frus |
| 101 | * via interfaceDecider function. |
| 102 | * |
| 103 | * @param[in] jsObject - Inventory json object |
| 104 | * @param[in] flag - flag which tells about the user option(either |
| 105 | * dumpInventory or dumpObject) |
| 106 | * @param[in] fruPath - fruPath is empty for dumpInventory option and holds |
| 107 | * valid fruPath for dumpObject option. |
| 108 | * |
| 109 | * @return output json |
| 110 | */ |
| 111 | json parseInvJson(const json& jsObject, char flag, std::string fruPath); |
| 112 | |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 113 | /** |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 114 | * @brief eraseInventoryPath |
| 115 | * Remove the INVENTORY_PATH - "/xyz/openbmc_project/inventory" |
| 116 | * for code convenience. |
| 117 | * |
| 118 | * @param[out] fru - Reference to the fru path whose INVENTORY_PATH is |
| 119 | * stripped off. |
| 120 | */ |
| 121 | void eraseInventoryPath(std::string& fru); |
| 122 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 123 | /** |
| 124 | * @brief printReturnCode |
| 125 | * Prints the return code of the program in console output, whenever |
| 126 | * the program fails to exit successfully. |
| 127 | * |
| 128 | * @param[in] returnCode - return code of the program. |
| 129 | */ |
PriyangaRamasamy | 6ee637a | 2021-02-12 04:49:02 -0600 | [diff] [blame] | 130 | void printReturnCode(int returnCode); |
| 131 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 132 | /** |
| 133 | * @brief Convert hex/ascii values to Binary |
| 134 | * @param[in] - value in hex/ascii. |
| 135 | * @param[out] - value in binary. |
| 136 | */ |
| 137 | openpower::vpd::Binary toBinary(const std::string& value); |
| 138 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 139 | public: |
| 140 | /** |
| 141 | * @brief Dump the complete inventory in JSON format |
| 142 | * |
| 143 | * @param[in] jsObject - Inventory JSON specified in configure file. |
| 144 | */ |
| 145 | void dumpInventory(const nlohmann::basic_json<>& jsObject); |
| 146 | |
| 147 | /** |
| 148 | * @brief Dump the given inventory object in JSON format |
| 149 | * |
| 150 | * @param[in] jsObject - Inventory JSON specified in configure file. |
| 151 | */ |
| 152 | void dumpObject(const nlohmann::basic_json<>& jsObject); |
| 153 | |
| 154 | /** |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 155 | * @brief Read keyword |
| 156 | * Read the given object path, record name and keyword |
| 157 | * from the inventory and display the value of the keyword |
| 158 | * in JSON format. |
| 159 | */ |
| 160 | void readKeyword(); |
| 161 | |
| 162 | /** |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 163 | * @brief Update Keyword |
| 164 | * Update the given keyword with the given value. |
| 165 | * |
| 166 | * @return return code (Success(0)/Failure(-1)) |
| 167 | */ |
| 168 | int updateKeyword(); |
| 169 | |
| 170 | /** |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 171 | * @brief Force Reset |
| 172 | * Clearing the inventory cache data and restarting the |
| 173 | * phosphor inventory manager and also retriggering all the |
| 174 | * udev events. |
| 175 | * |
| 176 | * @param[in] jsObject - Inventory JSON specified in configure file. |
| 177 | */ |
| 178 | void forceReset(const nlohmann::basic_json<>& jsObject); |
| 179 | |
| 180 | /** |
PriyangaRamasamy | 887a42a | 2020-09-03 00:33:57 +0530 | [diff] [blame] | 181 | * @brief Get Printable Value |
| 182 | * |
| 183 | * Checks if the vector value has non printable characters. |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 184 | * Returns hex value if non printable char is found else |
PriyangaRamasamy | 887a42a | 2020-09-03 00:33:57 +0530 | [diff] [blame] | 185 | * returns ascii value. |
| 186 | * |
| 187 | * @param[in] vector - Reference of the Binary vector |
| 188 | * @return printable value - either in hex or in ascii. |
| 189 | */ |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 190 | std::string getPrintableValue(const std::vector<unsigned char>& vector); |
| 191 | |
| 192 | /** |
| 193 | * @brief Update Hardware |
| 194 | * If the given record-keyword pair is present in dbus_properties.json, |
| 195 | * then will update the given data in both dbus and hardware. |
| 196 | * Else update the given data only in hardware. |
| 197 | * @return returncode (success/failure). |
| 198 | */ |
| 199 | int updateHardware(); |
PriyangaRamasamy | 887a42a | 2020-09-03 00:33:57 +0530 | [diff] [blame] | 200 | |
| 201 | /** |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 202 | * @brief Constructor |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 203 | * Constructor is called during the |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 204 | * object instantiation for dumpInventory option and |
| 205 | * forceReset option. |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 206 | */ |
| 207 | VpdTool() |
| 208 | { |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * @brief Constructor |
| 213 | * Constructor is called during the |
| 214 | * object instantiation for dumpObject option. |
| 215 | */ |
| 216 | VpdTool(const std::string&& fru) : fruPath(std::move(fru)) |
| 217 | { |
| 218 | } |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * @brief Constructor |
| 222 | * Constructor is called during the |
| 223 | * object instantiation for readKeyword option. |
| 224 | */ |
| 225 | VpdTool(const std::string&& fru, const std::string&& recName, |
| 226 | const std::string&& kw) : |
| 227 | fruPath(std::move(fru)), |
| 228 | recordName(std::move(recName)), keyword(std::move(kw)) |
| 229 | { |
| 230 | } |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 231 | |
| 232 | /** |
| 233 | * @brief Constructor |
| 234 | * Constructor is called during the |
| 235 | * object instantiation for updateKeyword option. |
| 236 | */ |
| 237 | |
| 238 | VpdTool(const std::string&& fru, const std::string&& recName, |
| 239 | const std::string&& kw, const std::string&& val) : |
| 240 | fruPath(std::move(fru)), |
| 241 | recordName(std::move(recName)), keyword(std::move(kw)), |
| 242 | value(std::move(val)) |
| 243 | { |
| 244 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 245 | }; |