blob: 4649adcefa57fb8c5e950702ecdb5de9feb438e7 [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{
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050029/** @brief Api to obtain a dictionary of path -> services
30 * 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);
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700189} // namespace vpd
Alpana Kumarif05effd2021-04-07 07:32:53 -0500190} // namespace openpower