blob: 710282cb848d072b2560a2dfef00ae59c54dbb14 [file] [log] [blame]
Sampa Misra032bd502019-03-06 05:03:22 -06001#pragma once
2
George Liu6492f522020-06-16 10:34:05 +08003#include "libpldm/base.h"
4#include "libpldm/bios.h"
5#include "libpldm/platform.h"
6
Sampa Misraaea5dde2020-08-31 08:33:47 -05007#include "types.hpp"
8
Sampa Misra032bd502019-03-06 05:03:22 -06009#include <stdint.h>
10#include <systemd/sd-bus.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050011#include <unistd.h>
Sampa Misra032bd502019-03-06 05:03:22 -060012
George Liu6492f522020-06-16 10:34:05 +080013#include <nlohmann/json.hpp>
14#include <sdbusplus/server.hpp>
15#include <xyz/openbmc_project/Logging/Entry/server.hpp>
16
Sampa Misraa2fa0702019-05-31 01:28:55 -050017#include <exception>
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050018#include <filesystem>
Sampa Misraaa8ae722019-12-12 03:20:40 -060019#include <iostream>
Sampa Misra032bd502019-03-06 05:03:22 -060020#include <string>
Sampa Misraa2fa0702019-05-31 01:28:55 -050021#include <variant>
22#include <vector>
Sampa Misra032bd502019-03-06 05:03:22 -060023
24namespace pldm
25{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050026namespace utils
27{
28
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050029namespace fs = std::filesystem;
Tom Joseph250c4752020-04-15 10:32:45 +053030using Json = nlohmann::json;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050031
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050032/** @struct CustomFD
33 *
34 * RAII wrapper for file descriptor.
35 */
36struct CustomFD
37{
38 CustomFD(const CustomFD&) = delete;
39 CustomFD& operator=(const CustomFD&) = delete;
40 CustomFD(CustomFD&&) = delete;
41 CustomFD& operator=(CustomFD&&) = delete;
42
43 CustomFD(int fd) : fd(fd)
George Liu6492f522020-06-16 10:34:05 +080044 {}
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050045
46 ~CustomFD()
47 {
48 if (fd >= 0)
49 {
50 close(fd);
51 }
52 }
53
54 int operator()() const
55 {
56 return fd;
57 }
58
59 private:
60 int fd = -1;
61};
62
Sampa Misrab37be312019-07-03 02:26:41 -050063/** @brief Calculate the pad for PLDM data
64 *
65 * @param[in] data - Length of the data
66 * @return - uint8_t - number of pad bytes
67 */
68uint8_t getNumPadBytes(uint32_t data);
69
George Liu83409572019-12-24 18:42:54 +080070/** @brief Convert uint64 to date
71 *
72 * @param[in] data - time date of uint64
73 * @param[out] year - year number in dec
74 * @param[out] month - month number in dec
75 * @param[out] day - day of the month in dec
76 * @param[out] hour - number of hours in dec
77 * @param[out] min - number of minutes in dec
78 * @param[out] sec - number of seconds in dec
79 * @return true if decode success, false if decode faild
80 */
81bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
82 uint8_t* hour, uint8_t* min, uint8_t* sec);
83
84/** @brief Convert effecter data to structure of set_effecter_state_field
85 *
86 * @param[in] effecterData - the date of effecter
George Liuba4c1fb2020-02-05 14:13:30 +080087 * @param[in] effecterCount - the number of individual sets of effecter
88 * information
89 * @return[out] parse success and get a valid set_effecter_state_field
90 * structure, return nullopt means parse failed
George Liu83409572019-12-24 18:42:54 +080091 */
George Liuba4c1fb2020-02-05 14:13:30 +080092std::optional<std::vector<set_effecter_state_field>>
93 parseEffecterData(const std::vector<uint8_t>& effecterData,
94 uint8_t effecterCount);
Sampa Misra032bd502019-03-06 05:03:22 -060095
96/**
Sampa Misraaa8ae722019-12-12 03:20:40 -060097 * @brief creates an error log
98 * @param[in] errorMsg - the error message
99 */
100void reportError(const char* errorMsg);
101
Sampa Misra032bd502019-03-06 05:03:22 -0600102/** @brief Convert any Decimal number to BCD
103 *
104 * @tparam[in] decimal - Decimal number
105 * @return Corresponding BCD number
106 */
107template <typename T>
108T decimalToBcd(T decimal)
109{
110 T bcd = 0;
111 T rem = 0;
112 auto cnt = 0;
113
114 while (decimal)
115 {
116 rem = decimal % 10;
117 bcd = bcd + (rem << cnt);
118 decimal = decimal / 10;
119 cnt += 4;
120 }
121
122 return bcd;
123}
124
Sampa Misraa2fa0702019-05-31 01:28:55 -0500125constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500126constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
Sampa Misraa2fa0702019-05-31 01:28:55 -0500127
George Liu1e44c732020-02-28 20:20:06 +0800128struct DBusMapping
129{
130 std::string objectPath; //!< D-Bus object path
131 std::string interface; //!< D-Bus interface
132 std::string propertyName; //!< D-Bus property name
133 std::string propertyType; //!< D-Bus property type
134};
135
136using PropertyValue =
137 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
138 uint64_t, double, std::string>;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500139using DbusProp = std::string;
140using DbusChangedProps = std::map<DbusProp, PropertyValue>;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500141using DBusInterfaceAdded = std::vector<
142 std::pair<pldm::dbus::Interface,
143 std::vector<std::pair<pldm::dbus::Property,
144 std::variant<pldm::dbus::Property>>>>>;
George Liu1e44c732020-02-28 20:20:06 +0800145
146/**
147 * @brief The interface for DBusHandler
148 */
149class DBusHandlerInterface
150{
151 public:
152 virtual ~DBusHandlerInterface() = default;
153
George Liu36e81352020-07-01 14:40:30 +0800154 virtual std::string getService(const char* path,
155 const char* interface) const = 0;
156
George Liu1e44c732020-02-28 20:20:06 +0800157 virtual void setDbusProperty(const DBusMapping& dBusMap,
158 const PropertyValue& value) const = 0;
John Wang9e242422020-03-05 08:37:50 +0800159
160 virtual PropertyValue
161 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
162 const char* dbusInterface) const = 0;
George Liu1e44c732020-02-28 20:20:06 +0800163};
164
Sampa Misraa2fa0702019-05-31 01:28:55 -0500165/**
166 * @class DBusHandler
167 *
168 * Wrapper class to handle the D-Bus calls
169 *
170 * This class contains the APIs to handle the D-Bus calls
171 * to cater the request from pldm requester.
172 * A class is created to mock the apis in the test cases
173 */
George Liu1e44c732020-02-28 20:20:06 +0800174class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500175{
176 public:
George Liu0e02c322020-01-01 09:41:51 +0800177 /** @brief Get the bus connection. */
178 static auto& getBus()
179 {
180 static auto bus = sdbusplus::bus::new_default();
181 return bus;
182 }
183
184 /**
185 * @brief Get the DBUS Service name for the input dbus path
John Wang9e242422020-03-05 08:37:50 +0800186 *
George Liu0e02c322020-01-01 09:41:51 +0800187 * @param[in] path - DBUS object path
188 * @param[in] interface - DBUS Interface
John Wang9e242422020-03-05 08:37:50 +0800189 *
George Liu0e02c322020-01-01 09:41:51 +0800190 * @return std::string - the dbus service name
John Wang9e242422020-03-05 08:37:50 +0800191 *
192 * @throw sdbusplus::exception::SdBusError when it fails
George Liu0e02c322020-01-01 09:41:51 +0800193 */
George Liu36e81352020-07-01 14:40:30 +0800194 std::string getService(const char* path,
195 const char* interface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800196
John Wang9e242422020-03-05 08:37:50 +0800197 /** @brief Get property(type: variant) from the requested dbus
198 *
199 * @param[in] objPath - The Dbus object path
200 * @param[in] dbusProp - The property name to get
201 * @param[in] dbusInterface - The Dbus interface
202 *
203 * @return The value of the property(type: variant)
204 *
205 * @throw sdbusplus::exception::SdBusError when it fails
206 */
207 PropertyValue
208 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
209 const char* dbusInterface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800210
John Wang9e242422020-03-05 08:37:50 +0800211 /** @brief The template function to get property from the requested dbus
212 * path
213 *
214 * @tparam Property - Excepted type of the property on dbus
215 *
216 * @param[in] objPath - The Dbus object path
217 * @param[in] dbusProp - The property name to get
218 * @param[in] dbusInterface - The Dbus interface
219 *
220 * @return The value of the property
221 *
222 * @throw sdbusplus::exception::SdBusError when dbus request fails
223 * std::bad_variant_access when \p Property and property on dbus do
224 * not match
225 */
John Wang92b3c972019-10-17 11:06:41 +0800226 template <typename Property>
227 auto getDbusProperty(const char* objPath, const char* dbusProp,
228 const char* dbusInterface)
229 {
John Wang9e242422020-03-05 08:37:50 +0800230 auto VariantValue =
231 getDbusPropertyVariant(objPath, dbusProp, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800232 return std::get<Property>(VariantValue);
233 }
George Liu1e44c732020-02-28 20:20:06 +0800234
235 /** @brief Set Dbus property
236 *
237 * @param[in] dBusMap - Object path, property name, interface and property
238 * type for the D-Bus object
239 * @param[in] value - The value to be set
John Wang9e242422020-03-05 08:37:50 +0800240 *
241 * @throw sdbusplus::exception::SdBusError when it fails
George Liu1e44c732020-02-28 20:20:06 +0800242 */
243 void setDbusProperty(const DBusMapping& dBusMap,
244 const PropertyValue& value) const override;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500245};
246
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500247/** @brief Fetch parent D-Bus object based on pathname
248 *
249 * @param[in] dbusObj - child D-Bus object
250 *
251 * @return std::string - the parent D-Bus object path
252 */
253inline std::string findParent(const std::string& dbusObj)
254{
255 fs::path p(dbusObj);
256 return p.parent_path().string();
257}
258
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500259/** @brief Read (static) MCTP EID of host firmware from a file
260 *
261 * @return uint8_t - MCTP EID
262 */
263uint8_t readHostEID();
Tom Joseph250c4752020-04-15 10:32:45 +0530264
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530265/** @brief Convert a value in the JSON to a D-Bus property value
266 *
267 * @param[in] type - type of the D-Bus property
268 * @param[in] value - value in the JSON file
269 *
270 * @return PropertyValue - the D-Bus property value
271 */
272PropertyValue jsonEntryToDbusVal(std::string_view type,
273 const nlohmann::json& value);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500274
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500275/** @brief Find State Effecter PDR
276 * @param[in] tid - PLDM terminus ID.
277 * @param[in] entityID - entity that can be associated with PLDM State set.
278 * @param[in] stateSetId - value that identifies PLDM State set.
279 * @param[in] repo - pointer to BMC's primary PDR repo.
280 * @return array[array[uint8_t]] - StateEffecterPDRs
281 */
282std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t tid,
283 uint16_t entityID,
284 uint16_t stateSetId,
285 const pldm_pdr* repo);
Chicago Duan738e4d82020-05-28 16:39:19 +0800286/** @brief Find State Sensor PDR
287 * @param[in] tid - PLDM terminus ID.
288 * @param[in] entityID - entity that can be associated with PLDM State set.
289 * @param[in] stateSetId - value that identifies PLDM State set.
290 * @param[in] repo - pointer to BMC's primary PDR repo.
291 * @return array[array[uint8_t]] - StateSensorPDRs
292 */
293std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t tid,
294 uint16_t entityID,
295 uint16_t stateSetId,
296 const pldm_pdr* repo);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500297
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500298/** @brief Find sensor id from a state sensor PDR
299 *
300 * @param[in] pdrRepo - PDR repository
301 * @param[in] tid - terminus id
302 * @param[in] entityType - entity type
303 * @param[in] entityInstance - entity instance number
304 * @param[in] containerId - container id
305 * @param[in] stateSetId - state set id
306 *
307 * @return uint16_t - the sensor id
308 */
309uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
310 uint16_t entityType, uint16_t entityInstance,
311 uint16_t containerId, uint16_t stateSetId);
312
Tom Joseph250c4752020-04-15 10:32:45 +0530313/** @brief Find effecter id from a state effecter pdr
314 * @param[in] pdrRepo - PDR repository
315 * @param[in] entityType - entity type
316 * @param[in] entityInstance - entity instance number
317 * @param[in] containerId - container id
318 * @param[in] stateSetId - state set id
Sampa Misraa4a96162020-07-14 05:33:46 -0500319 * @param[in] localOrRemote - true for checking local repo and false for remote
320 * repo
Tom Joseph250c4752020-04-15 10:32:45 +0530321 *
322 * @return uint16_t - the effecter id
323 */
324uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
325 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500326 uint16_t stateSetId, bool localOrRemote);
Tom Joseph250c4752020-04-15 10:32:45 +0530327
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800328/** @brief Emit the sensor event signal
329 *
330 * @param[in] tid - the terminus id
331 * @param[in] sensorId - sensorID value of the sensor
332 * @param[in] sensorOffset - Identifies which state sensor within a
333 * composite state sensor the event is being returned for
334 * @param[in] eventState - The event state value from the state change that
335 * triggered the event message
336 * @param[in] previousEventState - The event state value for the state from
337 * which the present event state was entered.
338 * @return PLDM completion code
339 */
340int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
341 uint8_t sensorOffset, uint8_t eventState,
342 uint8_t previousEventState);
343
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600344/** @brief Print the buffer
345 *
346 * @param[in] buffer - Buffer to print
347 * @param[in] pldmVerbose -verbosity flag - true/false
348 *
349 * @return - None
350 */
351void printBuffer(const std::vector<uint8_t>& buffer, bool pldmVerbose);
352
George Liu83409572019-12-24 18:42:54 +0800353} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600354} // namespace pldm