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