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