blob: abf525be01bae793d0c8d0be4cfbc9ed5ca00c58 [file] [log] [blame]
Sampa Misra032bd502019-03-06 05:03:22 -06001#pragma once
2
3#include <stdint.h>
4#include <systemd/sd-bus.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05005#include <unistd.h>
Sampa Misra032bd502019-03-06 05:03:22 -06006
Sampa Misraa2fa0702019-05-31 01:28:55 -05007#include <exception>
Deepak Kodihalli3cd61812020-03-10 06:38:45 -05008#include <filesystem>
Sampa Misraaa8ae722019-12-12 03:20:40 -06009#include <iostream>
TOM JOSEPHd4d97a52020-03-23 14:36:34 +053010#include <nlohmann/json.hpp>
Sampa Misra032bd502019-03-06 05:03:22 -060011#include <sdbusplus/server.hpp>
12#include <string>
Sampa Misraa2fa0702019-05-31 01:28:55 -050013#include <variant>
14#include <vector>
Sampa Misraaa8ae722019-12-12 03:20:40 -060015#include <xyz/openbmc_project/Logging/Entry/server.hpp>
Sampa Misraa2fa0702019-05-31 01:28:55 -050016
17#include "libpldm/base.h"
George Liu83409572019-12-24 18:42:54 +080018#include "libpldm/bios.h"
19#include "libpldm/platform.h"
Sampa Misra032bd502019-03-06 05:03:22 -060020
21namespace pldm
22{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050023namespace utils
24{
25
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050026namespace fs = std::filesystem;
27
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050028/** @struct CustomFD
29 *
30 * RAII wrapper for file descriptor.
31 */
32struct CustomFD
33{
34 CustomFD(const CustomFD&) = delete;
35 CustomFD& operator=(const CustomFD&) = delete;
36 CustomFD(CustomFD&&) = delete;
37 CustomFD& operator=(CustomFD&&) = delete;
38
39 CustomFD(int fd) : fd(fd)
40 {
41 }
42
43 ~CustomFD()
44 {
45 if (fd >= 0)
46 {
47 close(fd);
48 }
49 }
50
51 int operator()() const
52 {
53 return fd;
54 }
55
56 private:
57 int fd = -1;
58};
59
Sampa Misrab37be312019-07-03 02:26:41 -050060/** @brief Calculate the pad for PLDM data
61 *
62 * @param[in] data - Length of the data
63 * @return - uint8_t - number of pad bytes
64 */
65uint8_t getNumPadBytes(uint32_t data);
66
George Liu83409572019-12-24 18:42:54 +080067/** @brief Convert uint64 to date
68 *
69 * @param[in] data - time date of uint64
70 * @param[out] year - year number in dec
71 * @param[out] month - month number in dec
72 * @param[out] day - day of the month in dec
73 * @param[out] hour - number of hours in dec
74 * @param[out] min - number of minutes in dec
75 * @param[out] sec - number of seconds in dec
76 * @return true if decode success, false if decode faild
77 */
78bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
79 uint8_t* hour, uint8_t* min, uint8_t* sec);
80
81/** @brief Convert effecter data to structure of set_effecter_state_field
82 *
83 * @param[in] effecterData - the date of effecter
George Liuba4c1fb2020-02-05 14:13:30 +080084 * @param[in] effecterCount - the number of individual sets of effecter
85 * information
86 * @return[out] parse success and get a valid set_effecter_state_field
87 * structure, return nullopt means parse failed
George Liu83409572019-12-24 18:42:54 +080088 */
George Liuba4c1fb2020-02-05 14:13:30 +080089std::optional<std::vector<set_effecter_state_field>>
90 parseEffecterData(const std::vector<uint8_t>& effecterData,
91 uint8_t effecterCount);
Sampa Misra032bd502019-03-06 05:03:22 -060092
93/**
Sampa Misraaa8ae722019-12-12 03:20:40 -060094 * @brief creates an error log
95 * @param[in] errorMsg - the error message
96 */
97void reportError(const char* errorMsg);
98
Sampa Misra032bd502019-03-06 05:03:22 -060099/** @brief Convert any Decimal number to BCD
100 *
101 * @tparam[in] decimal - Decimal number
102 * @return Corresponding BCD number
103 */
104template <typename T>
105T decimalToBcd(T decimal)
106{
107 T bcd = 0;
108 T rem = 0;
109 auto cnt = 0;
110
111 while (decimal)
112 {
113 rem = decimal % 10;
114 bcd = bcd + (rem << cnt);
115 decimal = decimal / 10;
116 cnt += 4;
117 }
118
119 return bcd;
120}
121
Sampa Misraa2fa0702019-05-31 01:28:55 -0500122constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
123
George Liu1e44c732020-02-28 20:20:06 +0800124struct DBusMapping
125{
126 std::string objectPath; //!< D-Bus object path
127 std::string interface; //!< D-Bus interface
128 std::string propertyName; //!< D-Bus property name
129 std::string propertyType; //!< D-Bus property type
130};
131
132using PropertyValue =
133 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
134 uint64_t, double, std::string>;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500135using DbusProp = std::string;
136using DbusChangedProps = std::map<DbusProp, PropertyValue>;
George Liu1e44c732020-02-28 20:20:06 +0800137
138/**
139 * @brief The interface for DBusHandler
140 */
141class DBusHandlerInterface
142{
143 public:
144 virtual ~DBusHandlerInterface() = default;
145
146 virtual void setDbusProperty(const DBusMapping& dBusMap,
147 const PropertyValue& value) const = 0;
John Wang9e242422020-03-05 08:37:50 +0800148
149 virtual PropertyValue
150 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
151 const char* dbusInterface) const = 0;
George Liu1e44c732020-02-28 20:20:06 +0800152};
153
Sampa Misraa2fa0702019-05-31 01:28:55 -0500154/**
155 * @class DBusHandler
156 *
157 * Wrapper class to handle the D-Bus calls
158 *
159 * This class contains the APIs to handle the D-Bus calls
160 * to cater the request from pldm requester.
161 * A class is created to mock the apis in the test cases
162 */
George Liu1e44c732020-02-28 20:20:06 +0800163class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500164{
165 public:
George Liu0e02c322020-01-01 09:41:51 +0800166 /** @brief Get the bus connection. */
167 static auto& getBus()
168 {
169 static auto bus = sdbusplus::bus::new_default();
170 return bus;
171 }
172
173 /**
174 * @brief Get the DBUS Service name for the input dbus path
John Wang9e242422020-03-05 08:37:50 +0800175 *
George Liu0e02c322020-01-01 09:41:51 +0800176 * @param[in] path - DBUS object path
177 * @param[in] interface - DBUS Interface
John Wang9e242422020-03-05 08:37:50 +0800178 *
George Liu0e02c322020-01-01 09:41:51 +0800179 * @return std::string - the dbus service name
John Wang9e242422020-03-05 08:37:50 +0800180 *
181 * @throw sdbusplus::exception::SdBusError when it fails
George Liu0e02c322020-01-01 09:41:51 +0800182 */
183 std::string getService(const char* path, const char* interface) const;
184
John Wang9e242422020-03-05 08:37:50 +0800185 /** @brief Get property(type: variant) from the requested dbus
186 *
187 * @param[in] objPath - The Dbus object path
188 * @param[in] dbusProp - The property name to get
189 * @param[in] dbusInterface - The Dbus interface
190 *
191 * @return The value of the property(type: variant)
192 *
193 * @throw sdbusplus::exception::SdBusError when it fails
194 */
195 PropertyValue
196 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
197 const char* dbusInterface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800198
John Wang9e242422020-03-05 08:37:50 +0800199 /** @brief The template function to get property from the requested dbus
200 * path
201 *
202 * @tparam Property - Excepted type of the property on dbus
203 *
204 * @param[in] objPath - The Dbus object path
205 * @param[in] dbusProp - The property name to get
206 * @param[in] dbusInterface - The Dbus interface
207 *
208 * @return The value of the property
209 *
210 * @throw sdbusplus::exception::SdBusError when dbus request fails
211 * std::bad_variant_access when \p Property and property on dbus do
212 * not match
213 */
John Wang92b3c972019-10-17 11:06:41 +0800214 template <typename Property>
215 auto getDbusProperty(const char* objPath, const char* dbusProp,
216 const char* dbusInterface)
217 {
John Wang9e242422020-03-05 08:37:50 +0800218 auto VariantValue =
219 getDbusPropertyVariant(objPath, dbusProp, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800220 return std::get<Property>(VariantValue);
221 }
George Liu1e44c732020-02-28 20:20:06 +0800222
223 /** @brief Set Dbus property
224 *
225 * @param[in] dBusMap - Object path, property name, interface and property
226 * type for the D-Bus object
227 * @param[in] value - The value to be set
John Wang9e242422020-03-05 08:37:50 +0800228 *
229 * @throw sdbusplus::exception::SdBusError when it fails
George Liu1e44c732020-02-28 20:20:06 +0800230 */
231 void setDbusProperty(const DBusMapping& dBusMap,
232 const PropertyValue& value) const override;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500233};
234
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500235/** @brief Fetch parent D-Bus object based on pathname
236 *
237 * @param[in] dbusObj - child D-Bus object
238 *
239 * @return std::string - the parent D-Bus object path
240 */
241inline std::string findParent(const std::string& dbusObj)
242{
243 fs::path p(dbusObj);
244 return p.parent_path().string();
245}
246
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500247/** @brief Read (static) MCTP EID of host firmware from a file
248 *
249 * @return uint8_t - MCTP EID
250 */
251uint8_t readHostEID();
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530252/** @brief Convert a value in the JSON to a D-Bus property value
253 *
254 * @param[in] type - type of the D-Bus property
255 * @param[in] value - value in the JSON file
256 *
257 * @return PropertyValue - the D-Bus property value
258 */
259PropertyValue jsonEntryToDbusVal(std::string_view type,
260 const nlohmann::json& value);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500261
George Liu83409572019-12-24 18:42:54 +0800262} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600263} // namespace pldm