blob: 14322412373c04556cec34c9406090346a44f1ba [file] [log] [blame]
Deepak Kodihalli76794492017-02-16 23:48:18 -06001#pragma once
2
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -06003#include "const.hpp"
Alpana Kumarif05effd2021-04-07 07:32:53 -05004#include "store.hpp"
Deepak Kodihalli76794492017-02-16 23:48:18 -06005#include "types.hpp"
6
SunnySrivastava1984d076da82020-03-05 05:33:35 -06007#include <iostream>
Alpana Kumari735dee92022-03-25 01:24:40 -05008#include <nlohmann/json.hpp>
SunnySrivastava1984d076da82020-03-05 05:33:35 -06009
10using namespace std;
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050011
Deepak Kodihalli76794492017-02-16 23:48:18 -060012namespace openpower
13{
14namespace vpd
15{
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050016
Santosh Puranikbd011b22020-01-23 04:05:25 -060017/** @brief Return the hex representation of the incoming byte
18 *
19 * @param [in] c - The input byte
20 * @returns The hex representation of the byte as a character.
21 */
22constexpr auto toHex(size_t c)
23{
24 constexpr auto map = "0123456789abcdef";
25 return map[c];
26}
Deepak Kodihalli76794492017-02-16 23:48:18 -060027
28namespace inventory
29{
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -060030/** @brief API to obtain a dictionary of path -> services
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050031 * where path is in subtree and services is of the type
32 * returned by the GetObject method.
33 *
34 * @param [in] root - Root path for object subtree
35 * @param [in] depth - Maximum subtree depth required
36 * @param [in] interfaces - Array to interfaces for which
37 * result is required.
38 * @return A dictionary of Path -> services
39 */
40MapperResponse
41 getObjectSubtreeForInterfaces(const std::string& root, const int32_t depth,
42 const std::vector<std::string>& interfaces);
43
Deepak Kodihalli76794492017-02-16 23:48:18 -060044} // namespace inventory
45
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060046/**@brief This API reads 2 Bytes of data and swap the read data
47 * @param[in] iterator- Pointer pointing to the data to be read
48 * @return returns 2 Byte data read at the given pointer
49 */
50openpower::vpd::constants::LE2ByteData
51 readUInt16LE(Binary::const_iterator iterator);
52
SunnySrivastava1984d076da82020-03-05 05:33:35 -060053/** @brief Encodes a keyword for D-Bus.
54 * @param[in] kw - kwd data in string format
55 * @param[in] encoding - required for kwd data
56 */
57string encodeKeyword(const string& kw, const string& encoding);
SunnySrivastava198443306542020-04-01 02:50:20 -050058
59/** @brief Reads a property from the inventory manager given object path,
60 * intreface and property.
61 * @param[in] obj - object path
62 * @param[in] inf - interface
63 * @param[in] prop - property whose value is fetched
64 * @return [out] - value of the property
65 */
66string readBusProperty(const string& obj, const string& inf,
67 const string& prop);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050068
69/**
70 * @brief API to create PEL entry
Sunny Srivastava0746eee2021-03-22 13:36:54 -050071 * @param[in] additionalData - Map holding the additional data
72 * @param[in] sev - Severity
73 * @param[in] errIntf - error interface
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050074 */
75void createPEL(const std::map<std::string, std::string>& additionalData,
Sunny Srivastava0746eee2021-03-22 13:36:54 -050076 const constants::PelSeverity& sev, const std::string& errIntf);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050077
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053078/**
79 * @brief getVpdFilePath
80 * Get vpd file path corresponding to the given object path.
81 * @param[in] - json file path
82 * @param[in] - Object path
83 * @return - Vpd file path
84 */
85inventory::VPDfilepath getVpdFilePath(const string& jsonFile,
86 const std::string& ObjPath);
87
88/**
89 * @brief isPathInJson
90 * API which checks for the presence of the given eeprom path in the given json.
91 * @param[in] - eepromPath
92 * @return - true if the eeprom is present in the json; false otherwise
93 */
94bool isPathInJson(const std::string& eepromPath);
95
96/**
97 * @brief isRecKwInDbusJson
98 * API which checks whether the given keyword under the given record is to be
99 * published on dbus or not. Checks against the keywords present in
100 * dbus_property.json.
101 * @param[in] - record name
102 * @param[in] - keyword name
103 * @return - true if the record-keyword pair is present in dbus_property.json;
104 * false otherwise.
105 */
106bool isRecKwInDbusJson(const std::string& record, const std::string& keyword);
107
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -0500108/**
109 * @brief Check the type of VPD.
110 *
111 * Checks the type of vpd based on the start tag.
112 * @param[in] vector - Vpd data in vector format
113 *
114 * @return enum of type vpdType
115 */
116constants::vpdType vpdTypeCheck(const Binary& vector);
117
SunnySrivastava19849a195542020-09-07 06:04:50 -0500118/*
119 * @brief This method does nothing. Just an empty function to return null
120 * at the end of variadic template args
121 */
122inline string getCommand()
123{
124 return "";
125}
126
127/**
128 * @brief This function to arrange all arguments to make commandy
129 * @param[in] arguments to create the command
130 * @return cmd - command string
131 */
132template <typename T, typename... Types>
133inline string getCommand(T arg1, Types... args)
134{
135 string cmd = " " + arg1 + getCommand(args...);
136
137 return cmd;
138}
139
140/**
141 * @brief This API takes arguments, creates a shell command line and executes
142 * them.
143 * @param[in] arguments for command
144 * @returns output of that command
145 */
146template <typename T, typename... Types>
147inline vector<string> executeCmd(T&& path, Types... args)
148{
149 vector<string> stdOutput;
150 array<char, 128> buffer;
151
152 string cmd = path + getCommand(args...);
153
154 unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
155 if (!pipe)
156 {
157 throw runtime_error("popen() failed!");
158 }
159 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
160 {
161 stdOutput.emplace_back(buffer.data());
162 }
163
164 return stdOutput;
165}
166
Alpana Kumarif05effd2021-04-07 07:32:53 -0500167/** @brief This API checks for IM and HW keywords, and based
168 * on these values decides which system json to be used.
169 * @param[in] vpdMap - parsed vpd
170 * @returns System json path
171 */
172string getSystemsJson(const Parsed& vpdMap);
173
174/** @brief Reads HW Keyword from the vpd
175 * @param[in] vpdMap - parsed vpd
176 * @returns value of HW Keyword
177 */
178const string getHW(const Parsed& vpdMap);
179
180/** @brief Reads IM Keyword from the vpd
181 * @param[in] vpdMap - parsed vpd
182 * @returns value of IM Keyword
183 */
184const string getIM(const Parsed& vpdMap);
185
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530186/** @brief Translate udev event generated path to a generic /sys/bus eeprom path
187 * @param[io] file - path generated from udev event.
188 */
189void udevToGenericPath(string& file);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600190
191/**
192 * @brief API to generate a vpd name in some pattern.
193 * This vpd-name denotes name of the bad vpd file.
194 * For i2c eeproms - the pattern of the vpd-name will be
195 * i2c-<bus-number>-<eeprom-address>. For spi eeproms - the pattern of the
196 * vpd-name will be spi-<spi-number>.
197 *
198 * @param[in] file - file path of the vpd
199 * @return the vpd-name.
200 */
201string getBadVpdName(const string& file);
202
203/**
204 * @brief API which dumps the broken/bad vpd in a directory
205 * When the vpd is bad, this api places the bad vpd file inside
206 * "/tmp/bad-vpd" in BMC, in order to collect bad VPD data as a part of user
207 * initiated BMC dump.
208 *
209 * @param[in] file - bad vpd file path
210 * @param[in] vpdVector - bad vpd vector
211 */
212void dumpBadVpd(const std::string& file, const Binary& vpdVector);
alpana077ce68722021-07-25 13:23:59 -0500213
214/*
215 * @brief This function fetches the value for given keyword in the given record
216 * from vpd data and returns this value.
217 *
218 * @param[in] vpdMap - vpd to find out the data
219 * @param[in] rec - Record under which desired keyword exists
220 * @param[in] kwd - keyword to read the data from
221 *
222 * @returns keyword value if record/keyword combination found
223 * empty string if record or keyword is not found.
224 */
225const string getKwVal(const Parsed& vpdMap, const string& rec,
226 const string& kwd);
227
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500228/** @brief This creates a complete command using all it's input parameters,
229 * to bind or unbind the driver.
230 * @param[in] devNameAddr - device address on that bus
231 * @param[in] busType - i2c, spi
232 * @param[in] driverType - type of driver like at24
233 * @param[in] bindOrUnbind - either bind or unbind
234 * @returns Command to bind or unbind the driver.
235 */
236inline string createBindUnbindDriverCmnd(const string& devNameAddr,
237 const string& busType,
238 const string& driverType,
239 const string& bindOrUnbind)
240{
241 return ("echo " + devNameAddr + " > /sys/bus/" + busType + "/drivers/" +
242 driverType + "/" + bindOrUnbind);
243}
244
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500245/**
246 * @brief Get Printable Value
247 *
248 * Checks if the vector value has non printable characters.
249 * Returns hex value if non printable char is found else
250 * returns ascii value.
251 *
252 * @param[in] vector - Reference of the Binary vector
253 * @return printable value - either in hex or in ascii.
254 */
Priyanga Ramasamyc9ecf8e2021-10-08 02:28:52 -0500255string getPrintableValue(const Binary& vec);
256
257/**
258 * @brief Convert byte array to hex string.
259 * @param[in] vec - byte array
260 * @return hexadecimal string of bytes.
261 */
262string byteArrayToHexString(const Binary& vec);
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500263
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600264/**
Alpana Kumari735dee92022-03-25 01:24:40 -0500265 * @brief Performs any pre-action needed to get the FRU setup for collection.
266 *
267 * @param[in] json - json object
268 * @param[in] file - eeprom file path
269 * @return - success or failure
270 */
271bool executePreAction(const nlohmann::json& json, const string& file);
272
273/**
274 * @brief This API will be called at the end of VPD collection to perform any
275 * post actions.
276 *
277 * @param[in] json - json object
278 * @param[in] file - eeprom file path
279 */
280void executePostFailAction(const nlohmann::json& json, const string& file);
281
282/**
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600283 * @brief Helper function to insert or merge in map.
284 *
285 * This method checks in the given inventory::InterfaceMap if the given
286 * interface key is existing or not. If the interface key already exists, given
287 * property map is inserted into it. If the key does'nt exist then given
288 * interface and property map pair is newly created. If the property present in
289 * propertymap already exist in the InterfaceMap, then the new property value is
290 * ignored.
291 *
292 * @param[in,out] map - map object of type inventory::InterfaceMap only.
293 * @param[in] interface - Interface name.
294 * @param[in] property - new property map that needs to be emplaced.
295 */
296void insertOrMerge(inventory::InterfaceMap& map,
297 const inventory::Interface& interface,
298 inventory::PropertyMap&& property);
299
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700300} // namespace vpd
Alpana Kumari735dee92022-03-25 01:24:40 -0500301} // namespace openpower