blob: 6891d8129a7d104618a905cb1507c0b81c44cbe7 [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
7#include <sdbusplus/server.hpp>
8#include <string>
9
10namespace pldm
11{
12namespace responder
13{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050014namespace utils
15{
16
17/** @struct CustomFD
18 *
19 * RAII wrapper for file descriptor.
20 */
21struct CustomFD
22{
23 CustomFD(const CustomFD&) = delete;
24 CustomFD& operator=(const CustomFD&) = delete;
25 CustomFD(CustomFD&&) = delete;
26 CustomFD& operator=(CustomFD&&) = delete;
27
28 CustomFD(int fd) : fd(fd)
29 {
30 }
31
32 ~CustomFD()
33 {
34 if (fd >= 0)
35 {
36 close(fd);
37 }
38 }
39
40 int operator()() const
41 {
42 return fd;
43 }
44
45 private:
46 int fd = -1;
47};
48
Sampa Misrab37be312019-07-03 02:26:41 -050049/** @brief Calculate the pad for PLDM data
50 *
51 * @param[in] data - Length of the data
52 * @return - uint8_t - number of pad bytes
53 */
54uint8_t getNumPadBytes(uint32_t data);
55
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050056} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -060057
58/**
59 * @brief Get the DBUS Service name for the input dbus path
60 * @param[in] bus - DBUS Bus Object
61 * @param[in] path - DBUS object path
62 * @param[in] interface - DBUS Interface
63 * @return std::string - the dbus service name
64 */
65std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
66 const std::string& interface);
67
68/** @brief Convert any Decimal number to BCD
69 *
70 * @tparam[in] decimal - Decimal number
71 * @return Corresponding BCD number
72 */
73template <typename T>
74T decimalToBcd(T decimal)
75{
76 T bcd = 0;
77 T rem = 0;
78 auto cnt = 0;
79
80 while (decimal)
81 {
82 rem = decimal % 10;
83 bcd = bcd + (rem << cnt);
84 decimal = decimal / 10;
85 cnt += 4;
86 }
87
88 return bcd;
89}
90
91} // namespace responder
92} // namespace pldm