blob: bfbfb5dedde6022b123de199af802102c805c711 [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
George Liu1e44c732020-02-28 20:20:06 +0800120struct DBusMapping
121{
122 std::string objectPath; //!< D-Bus object path
123 std::string interface; //!< D-Bus interface
124 std::string propertyName; //!< D-Bus property name
125 std::string propertyType; //!< D-Bus property type
126};
127
128using PropertyValue =
129 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
130 uint64_t, double, std::string>;
131
132/**
133 * @brief The interface for DBusHandler
134 */
135class DBusHandlerInterface
136{
137 public:
138 virtual ~DBusHandlerInterface() = default;
139
140 virtual void setDbusProperty(const DBusMapping& dBusMap,
141 const PropertyValue& value) const = 0;
142};
143
Sampa Misraa2fa0702019-05-31 01:28:55 -0500144/**
145 * @class DBusHandler
146 *
147 * Wrapper class to handle the D-Bus calls
148 *
149 * This class contains the APIs to handle the D-Bus calls
150 * to cater the request from pldm requester.
151 * A class is created to mock the apis in the test cases
152 */
George Liu1e44c732020-02-28 20:20:06 +0800153class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500154{
155 public:
George Liu0e02c322020-01-01 09:41:51 +0800156 /** @brief Get the bus connection. */
157 static auto& getBus()
158 {
159 static auto bus = sdbusplus::bus::new_default();
160 return bus;
161 }
162
163 /**
164 * @brief Get the DBUS Service name for the input dbus path
165 * @param[in] path - DBUS object path
166 * @param[in] interface - DBUS Interface
167 * @return std::string - the dbus service name
168 */
169 std::string getService(const char* path, const char* interface) const;
170
John Wang92b3c972019-10-17 11:06:41 +0800171 template <typename Variant>
172 auto getDbusPropertyVariant(const char* objPath, const char* dbusProp,
173 const char* dbusInterface)
174 {
John Wang92b3c972019-10-17 11:06:41 +0800175 Variant value;
George Liu0e02c322020-01-01 09:41:51 +0800176 auto& bus = DBusHandler::getBus();
177 auto service = getService(objPath, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800178 auto method = bus.new_method_call(service.c_str(), objPath,
179 dbusProperties, "Get");
180 method.append(dbusInterface, dbusProp);
George Liu0e02c322020-01-01 09:41:51 +0800181 auto reply = bus.call(method);
182 reply.read(value);
183
John Wang92b3c972019-10-17 11:06:41 +0800184 return value;
185 }
186
187 template <typename Property>
188 auto getDbusProperty(const char* objPath, const char* dbusProp,
189 const char* dbusInterface)
190 {
191 auto VariantValue = getDbusPropertyVariant<std::variant<Property>>(
192 objPath, dbusProp, dbusInterface);
193 return std::get<Property>(VariantValue);
194 }
George Liu1e44c732020-02-28 20:20:06 +0800195
196 /** @brief Set Dbus property
197 *
198 * @param[in] dBusMap - Object path, property name, interface and property
199 * type for the D-Bus object
200 * @param[in] value - The value to be set
201 */
202 void setDbusProperty(const DBusMapping& dBusMap,
203 const PropertyValue& value) const override;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500204};
205
George Liu83409572019-12-24 18:42:54 +0800206} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600207} // namespace pldm