blob: 47bab17c875de1d0cf9aa2df61993586f7781891 [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>
Santosh Puranik53b38ed2022-04-10 23:15:22 +05309#include <optional>
SunnySrivastava1984d076da82020-03-05 05:33:35 -060010
11using namespace std;
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050012
Deepak Kodihalli76794492017-02-16 23:48:18 -060013namespace openpower
14{
15namespace vpd
16{
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050017
Santosh Puranikbd011b22020-01-23 04:05:25 -060018/** @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 */
23constexpr auto toHex(size_t c)
24{
25 constexpr auto map = "0123456789abcdef";
26 return map[c];
27}
Deepak Kodihalli76794492017-02-16 23:48:18 -060028
29namespace inventory
30{
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -060031/** @brief API to obtain a dictionary of path -> services
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050032 * 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 */
41MapperResponse
42 getObjectSubtreeForInterfaces(const std::string& root, const int32_t depth,
43 const std::vector<std::string>& interfaces);
44
Deepak Kodihalli76794492017-02-16 23:48:18 -060045} // namespace inventory
46
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060047/**@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 */
51openpower::vpd::constants::LE2ByteData
52 readUInt16LE(Binary::const_iterator iterator);
53
SunnySrivastava1984d076da82020-03-05 05:33:35 -060054/** @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 */
58string encodeKeyword(const string& kw, const string& encoding);
SunnySrivastava198443306542020-04-01 02:50:20 -050059
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 */
67string readBusProperty(const string& obj, const string& inf,
68 const string& prop);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050069
70/**
71 * @brief API to create PEL entry
Sunny Srivastava0746eee2021-03-22 13:36:54 -050072 * @param[in] additionalData - Map holding the additional data
73 * @param[in] sev - Severity
74 * @param[in] errIntf - error interface
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050075 */
76void createPEL(const std::map<std::string, std::string>& additionalData,
Sunny Srivastava0746eee2021-03-22 13:36:54 -050077 const constants::PelSeverity& sev, const std::string& errIntf);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050078
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053079/**
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 */
86inventory::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 */
95bool 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 */
107bool isRecKwInDbusJson(const std::string& record, const std::string& keyword);
108
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -0500109/**
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 */
117constants::vpdType vpdTypeCheck(const Binary& vector);
118
SunnySrivastava19849a195542020-09-07 06:04:50 -0500119/*
120 * @brief This method does nothing. Just an empty function to return null
121 * at the end of variadic template args
122 */
123inline 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 */
133template <typename T, typename... Types>
134inline 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 */
147template <typename T, typename... Types>
148inline 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 Kumarif05effd2021-04-07 07:32:53 -0500168/** @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 */
173string 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 */
179const 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 */
185const string getIM(const Parsed& vpdMap);
186
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530187/** @brief Translate udev event generated path to a generic /sys/bus eeprom path
188 * @param[io] file - path generated from udev event.
189 */
190void udevToGenericPath(string& file);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600191
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 */
202string 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 */
213void dumpBadVpd(const std::string& file, const Binary& vpdVector);
alpana077ce68722021-07-25 13:23:59 -0500214
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 */
226const string getKwVal(const Parsed& vpdMap, const string& rec,
227 const string& kwd);
228
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500229/** @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 */
237inline 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 Ramasamy02434932021-10-07 16:26:05 -0500246/**
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 Ramasamyc9ecf8e2021-10-08 02:28:52 -0500256string 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 */
263string byteArrayToHexString(const Binary& vec);
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500264
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600265/**
Santosh Puranik53b38ed2022-04-10 23:15:22 +0530266 * @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 */
279std::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 Kumari735dee92022-03-25 01:24:40 -0500284 *
285 * @param[in] json - json object
286 * @param[in] file - eeprom file path
287 * @return - success or failure
288 */
289bool 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 */
298void executePostFailAction(const nlohmann::json& json, const string& file);
299
300/**
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600301 * @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 */
314void insertOrMerge(inventory::InterfaceMap& map,
315 const inventory::Interface& interface,
316 inventory::PropertyMap&& property);
317
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700318} // namespace vpd
Alpana Kumari735dee92022-03-25 01:24:40 -0500319} // namespace openpower