blob: 567138a9e65706ae5bf321e10c5400d1a7ae81d6 [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>
Sampa Misra032bd502019-03-06 05:03:22 -060010#include <sdbusplus/server.hpp>
11#include <string>
Sampa Misraa2fa0702019-05-31 01:28:55 -050012#include <variant>
13#include <vector>
Sampa Misraaa8ae722019-12-12 03:20:40 -060014#include <xyz/openbmc_project/Logging/Entry/server.hpp>
Sampa Misraa2fa0702019-05-31 01:28:55 -050015
16#include "libpldm/base.h"
George Liu83409572019-12-24 18:42:54 +080017#include "libpldm/bios.h"
18#include "libpldm/platform.h"
Sampa Misra032bd502019-03-06 05:03:22 -060019
20namespace pldm
21{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050022namespace utils
23{
24
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050025namespace fs = std::filesystem;
26
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050027/** @struct CustomFD
28 *
29 * RAII wrapper for file descriptor.
30 */
31struct CustomFD
32{
33 CustomFD(const CustomFD&) = delete;
34 CustomFD& operator=(const CustomFD&) = delete;
35 CustomFD(CustomFD&&) = delete;
36 CustomFD& operator=(CustomFD&&) = delete;
37
38 CustomFD(int fd) : fd(fd)
39 {
40 }
41
42 ~CustomFD()
43 {
44 if (fd >= 0)
45 {
46 close(fd);
47 }
48 }
49
50 int operator()() const
51 {
52 return fd;
53 }
54
55 private:
56 int fd = -1;
57};
58
Sampa Misrab37be312019-07-03 02:26:41 -050059/** @brief Calculate the pad for PLDM data
60 *
61 * @param[in] data - Length of the data
62 * @return - uint8_t - number of pad bytes
63 */
64uint8_t getNumPadBytes(uint32_t data);
65
George Liu83409572019-12-24 18:42:54 +080066/** @brief Convert uint64 to date
67 *
68 * @param[in] data - time date of uint64
69 * @param[out] year - year number in dec
70 * @param[out] month - month number in dec
71 * @param[out] day - day of the month in dec
72 * @param[out] hour - number of hours in dec
73 * @param[out] min - number of minutes in dec
74 * @param[out] sec - number of seconds in dec
75 * @return true if decode success, false if decode faild
76 */
77bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
78 uint8_t* hour, uint8_t* min, uint8_t* sec);
79
80/** @brief Convert effecter data to structure of set_effecter_state_field
81 *
82 * @param[in] effecterData - the date of effecter
George Liuba4c1fb2020-02-05 14:13:30 +080083 * @param[in] effecterCount - the number of individual sets of effecter
84 * information
85 * @return[out] parse success and get a valid set_effecter_state_field
86 * structure, return nullopt means parse failed
George Liu83409572019-12-24 18:42:54 +080087 */
George Liuba4c1fb2020-02-05 14:13:30 +080088std::optional<std::vector<set_effecter_state_field>>
89 parseEffecterData(const std::vector<uint8_t>& effecterData,
90 uint8_t effecterCount);
Sampa Misra032bd502019-03-06 05:03:22 -060091
92/**
Sampa Misraaa8ae722019-12-12 03:20:40 -060093 * @brief creates an error log
94 * @param[in] errorMsg - the error message
95 */
96void reportError(const char* errorMsg);
97
Sampa Misra032bd502019-03-06 05:03:22 -060098/** @brief Convert any Decimal number to BCD
99 *
100 * @tparam[in] decimal - Decimal number
101 * @return Corresponding BCD number
102 */
103template <typename T>
104T decimalToBcd(T decimal)
105{
106 T bcd = 0;
107 T rem = 0;
108 auto cnt = 0;
109
110 while (decimal)
111 {
112 rem = decimal % 10;
113 bcd = bcd + (rem << cnt);
114 decimal = decimal / 10;
115 cnt += 4;
116 }
117
118 return bcd;
119}
120
Sampa Misraa2fa0702019-05-31 01:28:55 -0500121constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
122
George Liu1e44c732020-02-28 20:20:06 +0800123struct DBusMapping
124{
125 std::string objectPath; //!< D-Bus object path
126 std::string interface; //!< D-Bus interface
127 std::string propertyName; //!< D-Bus property name
128 std::string propertyType; //!< D-Bus property type
129};
130
131using PropertyValue =
132 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
133 uint64_t, double, std::string>;
134
135/**
136 * @brief The interface for DBusHandler
137 */
138class DBusHandlerInterface
139{
140 public:
141 virtual ~DBusHandlerInterface() = default;
142
143 virtual void setDbusProperty(const DBusMapping& dBusMap,
144 const PropertyValue& value) const = 0;
John Wang9e242422020-03-05 08:37:50 +0800145
146 virtual PropertyValue
147 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
148 const char* dbusInterface) const = 0;
George Liu1e44c732020-02-28 20:20:06 +0800149};
150
Sampa Misraa2fa0702019-05-31 01:28:55 -0500151/**
152 * @class DBusHandler
153 *
154 * Wrapper class to handle the D-Bus calls
155 *
156 * This class contains the APIs to handle the D-Bus calls
157 * to cater the request from pldm requester.
158 * A class is created to mock the apis in the test cases
159 */
George Liu1e44c732020-02-28 20:20:06 +0800160class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500161{
162 public:
George Liu0e02c322020-01-01 09:41:51 +0800163 /** @brief Get the bus connection. */
164 static auto& getBus()
165 {
166 static auto bus = sdbusplus::bus::new_default();
167 return bus;
168 }
169
170 /**
171 * @brief Get the DBUS Service name for the input dbus path
John Wang9e242422020-03-05 08:37:50 +0800172 *
George Liu0e02c322020-01-01 09:41:51 +0800173 * @param[in] path - DBUS object path
174 * @param[in] interface - DBUS Interface
John Wang9e242422020-03-05 08:37:50 +0800175 *
George Liu0e02c322020-01-01 09:41:51 +0800176 * @return std::string - the dbus service name
John Wang9e242422020-03-05 08:37:50 +0800177 *
178 * @throw sdbusplus::exception::SdBusError when it fails
George Liu0e02c322020-01-01 09:41:51 +0800179 */
180 std::string getService(const char* path, const char* interface) const;
181
John Wang9e242422020-03-05 08:37:50 +0800182 /** @brief Get property(type: variant) from the requested dbus
183 *
184 * @param[in] objPath - The Dbus object path
185 * @param[in] dbusProp - The property name to get
186 * @param[in] dbusInterface - The Dbus interface
187 *
188 * @return The value of the property(type: variant)
189 *
190 * @throw sdbusplus::exception::SdBusError when it fails
191 */
192 PropertyValue
193 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
194 const char* dbusInterface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800195
John Wang9e242422020-03-05 08:37:50 +0800196 /** @brief The template function to get property from the requested dbus
197 * path
198 *
199 * @tparam Property - Excepted type of the property on dbus
200 *
201 * @param[in] objPath - The Dbus object path
202 * @param[in] dbusProp - The property name to get
203 * @param[in] dbusInterface - The Dbus interface
204 *
205 * @return The value of the property
206 *
207 * @throw sdbusplus::exception::SdBusError when dbus request fails
208 * std::bad_variant_access when \p Property and property on dbus do
209 * not match
210 */
John Wang92b3c972019-10-17 11:06:41 +0800211 template <typename Property>
212 auto getDbusProperty(const char* objPath, const char* dbusProp,
213 const char* dbusInterface)
214 {
John Wang9e242422020-03-05 08:37:50 +0800215 auto VariantValue =
216 getDbusPropertyVariant(objPath, dbusProp, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800217 return std::get<Property>(VariantValue);
218 }
George Liu1e44c732020-02-28 20:20:06 +0800219
220 /** @brief Set Dbus property
221 *
222 * @param[in] dBusMap - Object path, property name, interface and property
223 * type for the D-Bus object
224 * @param[in] value - The value to be set
John Wang9e242422020-03-05 08:37:50 +0800225 *
226 * @throw sdbusplus::exception::SdBusError when it fails
George Liu1e44c732020-02-28 20:20:06 +0800227 */
228 void setDbusProperty(const DBusMapping& dBusMap,
229 const PropertyValue& value) const override;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500230};
231
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500232/** @brief Fetch parent D-Bus object based on pathname
233 *
234 * @param[in] dbusObj - child D-Bus object
235 *
236 * @return std::string - the parent D-Bus object path
237 */
238inline std::string findParent(const std::string& dbusObj)
239{
240 fs::path p(dbusObj);
241 return p.parent_path().string();
242}
243
George Liu83409572019-12-24 18:42:54 +0800244} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600245} // namespace pldm