blob: 6e128bef4af4601711d52f3e6fbea93a6c9b68a7 [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;
145};
146
Sampa Misraa2fa0702019-05-31 01:28:55 -0500147/**
148 * @class DBusHandler
149 *
150 * Wrapper class to handle the D-Bus calls
151 *
152 * This class contains the APIs to handle the D-Bus calls
153 * to cater the request from pldm requester.
154 * A class is created to mock the apis in the test cases
155 */
George Liu1e44c732020-02-28 20:20:06 +0800156class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500157{
158 public:
George Liu0e02c322020-01-01 09:41:51 +0800159 /** @brief Get the bus connection. */
160 static auto& getBus()
161 {
162 static auto bus = sdbusplus::bus::new_default();
163 return bus;
164 }
165
166 /**
167 * @brief Get the DBUS Service name for the input dbus path
168 * @param[in] path - DBUS object path
169 * @param[in] interface - DBUS Interface
170 * @return std::string - the dbus service name
171 */
172 std::string getService(const char* path, const char* interface) const;
173
John Wang92b3c972019-10-17 11:06:41 +0800174 template <typename Variant>
175 auto getDbusPropertyVariant(const char* objPath, const char* dbusProp,
176 const char* dbusInterface)
177 {
John Wang92b3c972019-10-17 11:06:41 +0800178 Variant value;
George Liu0e02c322020-01-01 09:41:51 +0800179 auto& bus = DBusHandler::getBus();
180 auto service = getService(objPath, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800181 auto method = bus.new_method_call(service.c_str(), objPath,
182 dbusProperties, "Get");
183 method.append(dbusInterface, dbusProp);
George Liu0e02c322020-01-01 09:41:51 +0800184 auto reply = bus.call(method);
185 reply.read(value);
186
John Wang92b3c972019-10-17 11:06:41 +0800187 return value;
188 }
189
190 template <typename Property>
191 auto getDbusProperty(const char* objPath, const char* dbusProp,
192 const char* dbusInterface)
193 {
194 auto VariantValue = getDbusPropertyVariant<std::variant<Property>>(
195 objPath, dbusProp, dbusInterface);
196 return std::get<Property>(VariantValue);
197 }
George Liu1e44c732020-02-28 20:20:06 +0800198
199 /** @brief Set Dbus property
200 *
201 * @param[in] dBusMap - Object path, property name, interface and property
202 * type for the D-Bus object
203 * @param[in] value - The value to be set
204 */
205 void setDbusProperty(const DBusMapping& dBusMap,
206 const PropertyValue& value) const override;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500207};
208
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500209/** @brief Fetch parent D-Bus object based on pathname
210 *
211 * @param[in] dbusObj - child D-Bus object
212 *
213 * @return std::string - the parent D-Bus object path
214 */
215inline std::string findParent(const std::string& dbusObj)
216{
217 fs::path p(dbusObj);
218 return p.parent_path().string();
219}
220
George Liu83409572019-12-24 18:42:54 +0800221} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600222} // namespace pldm