blob: db49f5105baa0c7f436bb5ee7159c7a42e59214a [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>
Sampa Misraaa8ae722019-12-12 03:20:40 -06008#include <iostream>
Sampa Misra032bd502019-03-06 05:03:22 -06009#include <sdbusplus/server.hpp>
10#include <string>
Sampa Misraa2fa0702019-05-31 01:28:55 -050011#include <variant>
12#include <vector>
Sampa Misraaa8ae722019-12-12 03:20:40 -060013#include <xyz/openbmc_project/Logging/Entry/server.hpp>
Sampa Misraa2fa0702019-05-31 01:28:55 -050014
15#include "libpldm/base.h"
George Liu83409572019-12-24 18:42:54 +080016#include "libpldm/bios.h"
17#include "libpldm/platform.h"
Sampa Misra032bd502019-03-06 05:03:22 -060018
19namespace pldm
20{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050021namespace utils
22{
23
24/** @struct CustomFD
25 *
26 * RAII wrapper for file descriptor.
27 */
28struct CustomFD
29{
30 CustomFD(const CustomFD&) = delete;
31 CustomFD& operator=(const CustomFD&) = delete;
32 CustomFD(CustomFD&&) = delete;
33 CustomFD& operator=(CustomFD&&) = delete;
34
35 CustomFD(int fd) : fd(fd)
36 {
37 }
38
39 ~CustomFD()
40 {
41 if (fd >= 0)
42 {
43 close(fd);
44 }
45 }
46
47 int operator()() const
48 {
49 return fd;
50 }
51
52 private:
53 int fd = -1;
54};
55
Sampa Misrab37be312019-07-03 02:26:41 -050056/** @brief Calculate the pad for PLDM data
57 *
58 * @param[in] data - Length of the data
59 * @return - uint8_t - number of pad bytes
60 */
61uint8_t getNumPadBytes(uint32_t data);
62
George Liu83409572019-12-24 18:42:54 +080063/** @brief Convert uint64 to date
64 *
65 * @param[in] data - time date of uint64
66 * @param[out] year - year number in dec
67 * @param[out] month - month number in dec
68 * @param[out] day - day of the month in dec
69 * @param[out] hour - number of hours in dec
70 * @param[out] min - number of minutes in dec
71 * @param[out] sec - number of seconds in dec
72 * @return true if decode success, false if decode faild
73 */
74bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
75 uint8_t* hour, uint8_t* min, uint8_t* sec);
76
77/** @brief Convert effecter data to structure of set_effecter_state_field
78 *
79 * @param[in] effecterData - the date of effecter
George Liuba4c1fb2020-02-05 14:13:30 +080080 * @param[in] effecterCount - the number of individual sets of effecter
81 * information
82 * @return[out] parse success and get a valid set_effecter_state_field
83 * structure, return nullopt means parse failed
George Liu83409572019-12-24 18:42:54 +080084 */
George Liuba4c1fb2020-02-05 14:13:30 +080085std::optional<std::vector<set_effecter_state_field>>
86 parseEffecterData(const std::vector<uint8_t>& effecterData,
87 uint8_t effecterCount);
Sampa Misra032bd502019-03-06 05:03:22 -060088
89/**
Sampa Misraaa8ae722019-12-12 03:20:40 -060090 * @brief creates an error log
91 * @param[in] errorMsg - the error message
92 */
93void reportError(const char* errorMsg);
94
Sampa Misra032bd502019-03-06 05:03:22 -060095/** @brief Convert any Decimal number to BCD
96 *
97 * @tparam[in] decimal - Decimal number
98 * @return Corresponding BCD number
99 */
100template <typename T>
101T decimalToBcd(T decimal)
102{
103 T bcd = 0;
104 T rem = 0;
105 auto cnt = 0;
106
107 while (decimal)
108 {
109 rem = decimal % 10;
110 bcd = bcd + (rem << cnt);
111 decimal = decimal / 10;
112 cnt += 4;
113 }
114
115 return bcd;
116}
117
Sampa Misraa2fa0702019-05-31 01:28:55 -0500118constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
119
120/**
121 * @class DBusHandler
122 *
123 * Wrapper class to handle the D-Bus calls
124 *
125 * This class contains the APIs to handle the D-Bus calls
126 * to cater the request from pldm requester.
127 * A class is created to mock the apis in the test cases
128 */
129class DBusHandler
130{
131 public:
George Liu0e02c322020-01-01 09:41:51 +0800132 /** @brief Get the bus connection. */
133 static auto& getBus()
134 {
135 static auto bus = sdbusplus::bus::new_default();
136 return bus;
137 }
138
139 /**
140 * @brief Get the DBUS Service name for the input dbus path
141 * @param[in] path - DBUS object path
142 * @param[in] interface - DBUS Interface
143 * @return std::string - the dbus service name
144 */
145 std::string getService(const char* path, const char* interface) const;
146
Sampa Misraa2fa0702019-05-31 01:28:55 -0500147 /** @brief API to set a D-Bus property
148 *
149 * @param[in] objPath - Object path for the D-Bus object
150 * @param[in] dbusProp - The D-Bus property
151 * @param[in] dbusInterface - The D-Bus interface
152 * @param[in] value - The value to be set
153 * failure
154 */
155 template <typename T>
156 void setDbusProperty(const char* objPath, const char* dbusProp,
157 const char* dbusInterface,
158 const std::variant<T>& value) const
159 {
George Liu0e02c322020-01-01 09:41:51 +0800160 auto& bus = DBusHandler::getBus();
161 auto service = getService(objPath, dbusInterface);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500162 auto method = bus.new_method_call(service.c_str(), objPath,
163 dbusProperties, "Set");
164 method.append(dbusInterface, dbusProp, value);
165 bus.call_noreply(method);
166 }
John Wang92b3c972019-10-17 11:06:41 +0800167
168 template <typename Variant>
169 auto getDbusPropertyVariant(const char* objPath, const char* dbusProp,
170 const char* dbusInterface)
171 {
John Wang92b3c972019-10-17 11:06:41 +0800172 Variant value;
George Liu0e02c322020-01-01 09:41:51 +0800173 auto& bus = DBusHandler::getBus();
174 auto service = getService(objPath, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800175 auto method = bus.new_method_call(service.c_str(), objPath,
176 dbusProperties, "Get");
177 method.append(dbusInterface, dbusProp);
George Liu0e02c322020-01-01 09:41:51 +0800178 auto reply = bus.call(method);
179 reply.read(value);
180
John Wang92b3c972019-10-17 11:06:41 +0800181 return value;
182 }
183
184 template <typename Property>
185 auto getDbusProperty(const char* objPath, const char* dbusProp,
186 const char* dbusInterface)
187 {
188 auto VariantValue = getDbusPropertyVariant<std::variant<Property>>(
189 objPath, dbusProp, dbusInterface);
190 return std::get<Property>(VariantValue);
191 }
Sampa Misraa2fa0702019-05-31 01:28:55 -0500192};
193
George Liu83409572019-12-24 18:42:54 +0800194} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600195} // namespace pldm