blob: 5f366be16ed053856da518d192ac21d37f91c6c3 [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 Misra032bd502019-03-06 05:03:22 -06008#include <sdbusplus/server.hpp>
9#include <string>
Sampa Misraa2fa0702019-05-31 01:28:55 -050010#include <variant>
11#include <vector>
12
13#include "libpldm/base.h"
Sampa Misra032bd502019-03-06 05:03:22 -060014
15namespace pldm
16{
17namespace responder
18{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050019namespace utils
20{
21
22/** @struct CustomFD
23 *
24 * RAII wrapper for file descriptor.
25 */
26struct CustomFD
27{
28 CustomFD(const CustomFD&) = delete;
29 CustomFD& operator=(const CustomFD&) = delete;
30 CustomFD(CustomFD&&) = delete;
31 CustomFD& operator=(CustomFD&&) = delete;
32
33 CustomFD(int fd) : fd(fd)
34 {
35 }
36
37 ~CustomFD()
38 {
39 if (fd >= 0)
40 {
41 close(fd);
42 }
43 }
44
45 int operator()() const
46 {
47 return fd;
48 }
49
50 private:
51 int fd = -1;
52};
53
Sampa Misrab37be312019-07-03 02:26:41 -050054/** @brief Calculate the pad for PLDM data
55 *
56 * @param[in] data - Length of the data
57 * @return - uint8_t - number of pad bytes
58 */
59uint8_t getNumPadBytes(uint32_t data);
60
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050061} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -060062
63/**
64 * @brief Get the DBUS Service name for the input dbus path
65 * @param[in] bus - DBUS Bus Object
66 * @param[in] path - DBUS object path
67 * @param[in] interface - DBUS Interface
68 * @return std::string - the dbus service name
69 */
70std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
71 const std::string& interface);
72
73/** @brief Convert any Decimal number to BCD
74 *
75 * @tparam[in] decimal - Decimal number
76 * @return Corresponding BCD number
77 */
78template <typename T>
79T decimalToBcd(T decimal)
80{
81 T bcd = 0;
82 T rem = 0;
83 auto cnt = 0;
84
85 while (decimal)
86 {
87 rem = decimal % 10;
88 bcd = bcd + (rem << cnt);
89 decimal = decimal / 10;
90 cnt += 4;
91 }
92
93 return bcd;
94}
95
Sampa Misraa2fa0702019-05-31 01:28:55 -050096constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
97
98/**
99 * @class DBusHandler
100 *
101 * Wrapper class to handle the D-Bus calls
102 *
103 * This class contains the APIs to handle the D-Bus calls
104 * to cater the request from pldm requester.
105 * A class is created to mock the apis in the test cases
106 */
107class DBusHandler
108{
109 public:
110 /** @brief API to set a D-Bus property
111 *
112 * @param[in] objPath - Object path for the D-Bus object
113 * @param[in] dbusProp - The D-Bus property
114 * @param[in] dbusInterface - The D-Bus interface
115 * @param[in] value - The value to be set
116 * failure
117 */
118 template <typename T>
119 void setDbusProperty(const char* objPath, const char* dbusProp,
120 const char* dbusInterface,
121 const std::variant<T>& value) const
122 {
123 auto bus = sdbusplus::bus::new_default();
124 auto service = getService(bus, objPath, dbusInterface);
125 auto method = bus.new_method_call(service.c_str(), objPath,
126 dbusProperties, "Set");
127 method.append(dbusInterface, dbusProp, value);
128 bus.call_noreply(method);
129 }
130};
131
Sampa Misra032bd502019-03-06 05:03:22 -0600132} // namespace responder
133} // namespace pldm