blob: fecf12e0a327aa9de655e428b6079be0c92f08f4 [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
80 * @param[out] effecter_id - a handle that is used to identify and access the
81 * effecter
82 * @param[out] stateField - structure of set_effecter_state_field
83 */
84bool decodeEffecterData(const std::vector<uint8_t>& effecterData,
85 uint16_t& effecter_id,
86 std::vector<set_effecter_state_field>& stateField);
Sampa Misra032bd502019-03-06 05:03:22 -060087
88/**
Sampa Misraaa8ae722019-12-12 03:20:40 -060089 * @brief creates an error log
90 * @param[in] errorMsg - the error message
91 */
92void reportError(const char* errorMsg);
93
Sampa Misra032bd502019-03-06 05:03:22 -060094/** @brief Convert any Decimal number to BCD
95 *
96 * @tparam[in] decimal - Decimal number
97 * @return Corresponding BCD number
98 */
99template <typename T>
100T decimalToBcd(T decimal)
101{
102 T bcd = 0;
103 T rem = 0;
104 auto cnt = 0;
105
106 while (decimal)
107 {
108 rem = decimal % 10;
109 bcd = bcd + (rem << cnt);
110 decimal = decimal / 10;
111 cnt += 4;
112 }
113
114 return bcd;
115}
116
Sampa Misraa2fa0702019-05-31 01:28:55 -0500117constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
118
119/**
120 * @class DBusHandler
121 *
122 * Wrapper class to handle the D-Bus calls
123 *
124 * This class contains the APIs to handle the D-Bus calls
125 * to cater the request from pldm requester.
126 * A class is created to mock the apis in the test cases
127 */
128class DBusHandler
129{
130 public:
George Liu0e02c322020-01-01 09:41:51 +0800131 /** @brief Get the bus connection. */
132 static auto& getBus()
133 {
134 static auto bus = sdbusplus::bus::new_default();
135 return bus;
136 }
137
138 /**
139 * @brief Get the DBUS Service name for the input dbus path
140 * @param[in] path - DBUS object path
141 * @param[in] interface - DBUS Interface
142 * @return std::string - the dbus service name
143 */
144 std::string getService(const char* path, const char* interface) const;
145
Sampa Misraa2fa0702019-05-31 01:28:55 -0500146 /** @brief API to set a D-Bus property
147 *
148 * @param[in] objPath - Object path for the D-Bus object
149 * @param[in] dbusProp - The D-Bus property
150 * @param[in] dbusInterface - The D-Bus interface
151 * @param[in] value - The value to be set
152 * failure
153 */
154 template <typename T>
155 void setDbusProperty(const char* objPath, const char* dbusProp,
156 const char* dbusInterface,
157 const std::variant<T>& value) const
158 {
George Liu0e02c322020-01-01 09:41:51 +0800159 auto& bus = DBusHandler::getBus();
160 auto service = getService(objPath, dbusInterface);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500161 auto method = bus.new_method_call(service.c_str(), objPath,
162 dbusProperties, "Set");
163 method.append(dbusInterface, dbusProp, value);
164 bus.call_noreply(method);
165 }
John Wang92b3c972019-10-17 11:06:41 +0800166
167 template <typename Variant>
168 auto getDbusPropertyVariant(const char* objPath, const char* dbusProp,
169 const char* dbusInterface)
170 {
John Wang92b3c972019-10-17 11:06:41 +0800171 Variant value;
George Liu0e02c322020-01-01 09:41:51 +0800172 auto& bus = DBusHandler::getBus();
173 auto service = getService(objPath, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800174 auto method = bus.new_method_call(service.c_str(), objPath,
175 dbusProperties, "Get");
176 method.append(dbusInterface, dbusProp);
George Liu0e02c322020-01-01 09:41:51 +0800177 auto reply = bus.call(method);
178 reply.read(value);
179
John Wang92b3c972019-10-17 11:06:41 +0800180 return value;
181 }
182
183 template <typename Property>
184 auto getDbusProperty(const char* objPath, const char* dbusProp,
185 const char* dbusInterface)
186 {
187 auto VariantValue = getDbusPropertyVariant<std::variant<Property>>(
188 objPath, dbusProp, dbusInterface);
189 return std::get<Property>(VariantValue);
190 }
Sampa Misraa2fa0702019-05-31 01:28:55 -0500191};
192
George Liu83409572019-12-24 18:42:54 +0800193} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600194} // namespace pldm